Skip to content

Commit

Permalink
Merge pull request hyperledger-archives#2200 from Artemkaaas/bugfix/u…
Browse files Browse the repository at this point in the history
…pdating-message-status

VCX Aries: should not update status of messages on agency
  • Loading branch information
Artemkaaas authored Jun 22, 2020
2 parents 9854cfa + 6400a2e commit 1b9a5db
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 301 deletions.
20 changes: 1 addition & 19 deletions vcx/libvcx/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ impl From<(Connection, ActorDidExchangeState)> for ConnectionV3 {
}
}

use v3::messages::a2a::{A2AMessage, MessageId};
use v3::messages::a2a::A2AMessage;
use v3::messages::connection::did_doc::DidDoc;

pub fn get_messages(handle: u32) -> VcxResult<HashMap<String, A2AMessage>> {
Expand Down Expand Up @@ -1294,24 +1294,6 @@ pub fn send_message_to_self_endpoint(message: A2AMessage, did_doc: &DidDoc) -> V
ConnectionV3::send_message_to_self_endpoint(&message, did_doc)
}

pub fn add_pending_messages(handle: u32, messages: HashMap<MessageId, String>) -> VcxResult<()> {
CONNECTION_MAP.get(handle, |connection| {
match connection {
Connections::V1(_) => Err(VcxError::from(VcxErrorKind::InvalidConnectionHandle)),
Connections::V3(ref connection) => connection.add_pending_messages(messages.clone())
}
})
}

pub fn remove_pending_message(handle: u32, id: &MessageId) -> VcxResult<()> {
CONNECTION_MAP.get(handle, |connection| {
match connection {
Connections::V1(_) => Err(VcxError::from(VcxErrorKind::InvalidConnectionHandle)),
Connections::V3(ref connection) => connection.remove_pending_message(id.clone())
}
})
}

pub fn is_v3_connection(connection_handle: u32) -> VcxResult<bool> {
CONNECTION_MAP.get(connection_handle, |connection| {
match connection {
Expand Down
2 changes: 1 addition & 1 deletion vcx/libvcx/src/messages/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub enum PayloadTypes {

#[derive(Clone, Deserialize, Serialize, Debug, PartialEq)]
pub struct PayloadTypeV1 {
name: String,
pub name: String,
ver: String,
fmt: String,
}
Expand Down
1 change: 1 addition & 0 deletions vcx/libvcx/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ macro_rules! secret {
($val:expr) => {{ "_" }};
}

#[cfg(test)]
macro_rules! map (
{ $($key:expr => $value:expr),+ } => {
{
Expand Down
21 changes: 3 additions & 18 deletions vcx/libvcx/src/v3/handlers/connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use error::prelude::*;
use v3::handlers::connection::states::{DidExchangeSM, Actor, ActorDidExchangeState};
use v3::handlers::connection::messages::DidExchangeMessages;
use v3::handlers::connection::agent::AgentInfo;
use v3::messages::a2a::{A2AMessage, MessageId};
use v3::messages::a2a::A2AMessage;
use v3::messages::connection::invite::Invitation;

use std::collections::HashMap;
Expand Down Expand Up @@ -126,16 +126,11 @@ impl Connection {
pub fn update_state_with_message(&mut self, message: &str) -> VcxResult<()> {
trace!("Connection: update_state_with_message: {}", message);

let agent_info = self.agent_info().clone();

let message: Message = ::serde_json::from_str(&message)
let message: A2AMessage = ::serde_json::from_str(&message)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidOption,
format!("Cannot updated state with messages: Message deserialization failed: {:?}", err)))?;

let a2a_message = self.decode_message(&message)?;
self.handle_message(a2a_message.into())?;

agent_info.update_message_status(message.uid)?;
self.handle_message(message.into())?;

Ok(())
}
Expand Down Expand Up @@ -217,16 +212,6 @@ impl Connection {
Ok(())
}

pub fn add_pending_messages(&self, messages: HashMap<MessageId, String>) -> VcxResult<()> {
trace!("Connection::add_pending_messages >>> messages: {:?}", messages);
self.connection_sm.add_pending_messages(messages)
}

pub fn remove_pending_message(&self, id: MessageId) -> VcxResult<()> {
trace!("Connection::remove_pending_message >>> id: {:?}", id);
self.connection_sm.remove_pending_message(id)
}

pub fn send_discovery_features(&mut self, query: Option<String>, comment: Option<String>) -> VcxResult<()> {
trace!("Connection::send_discovery_features_query >>> query: {:?}, comment: {:?}", query, comment);
self.handle_message(DidExchangeMessages::DiscoverFeatures((query, comment)))
Expand Down
21 changes: 0 additions & 21 deletions vcx/libvcx/src/v3/handlers/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,6 @@ pub mod tests {
::connection::update_message_status(alice.connection_handle, uid).unwrap();
}

// Pending Message
{
faber.activate();

let message = _ack();
::connection::send_message(faber.connection_handle, message.to_a2a_message()).unwrap();

alice.activate();

let messages = ::connection::get_messages(alice.connection_handle).unwrap();
assert_eq!(1, messages.len());
let uid = messages.keys().next().unwrap().clone();

::connection::add_pending_messages(alice.connection_handle, map!( message.id.clone() => uid )).unwrap();

::connection::remove_pending_message(alice.connection_handle, &message.id).unwrap();

let messages = ::connection::get_messages(alice.connection_handle).unwrap();
assert_eq!(0, messages.len());
}

// Download Messages
{
use messages::get_message::{download_messages, MessageByConnection, Message};
Expand Down
28 changes: 0 additions & 28 deletions vcx/libvcx/src/v3/handlers/connection/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ use v3::messages::ack::Ack;
use v3::messages::connection::did_doc::DidDoc;
use v3::messages::discovery::query::Query;
use v3::messages::discovery::disclose::{Disclose, ProtocolDescriptor};
use v3::messages::a2a::MessageId;
use v3::messages::a2a::protocol_registry::ProtocolRegistry;

use std::collections::HashMap;

use error::prelude::*;
use v3::utils::pending_message::PendingMessage;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DidExchangeSM {
Expand Down Expand Up @@ -672,32 +670,6 @@ impl DidExchangeSM {
ActorDidExchangeState::Invitee(_) => Actor::Invitee
}
}

pub fn add_pending_messages(&self, messages: HashMap<MessageId, String>) -> VcxResult<()> {
match self.state {
ActorDidExchangeState::Inviter(DidExchangeState::Completed(ref _state)) |
ActorDidExchangeState::Invitee(DidExchangeState::Completed(ref _state)) => {
for (key, value) in messages {
PendingMessage::add(&key.0, &value).ok();
}
}
_ => {}
};
Ok(())
}

pub fn remove_pending_message(&self, id: MessageId) -> VcxResult<()> {
match self.state {
ActorDidExchangeState::Inviter(DidExchangeState::Completed(ref _state)) |
ActorDidExchangeState::Invitee(DidExchangeState::Completed(ref _state)) => {
if let Some(uid_) = PendingMessage::get(&id.0) {
self.agent_info.update_message_status(uid_.to_string()).ok();
}
}
_ => {}
};
Ok(())
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand Down
5 changes: 3 additions & 2 deletions vcx/libvcx/src/v3/handlers/issuance/holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ impl HolderSM {

match self.find_message_to_handle(messages) {
Some((uid, msg)) => {
let state = self.handle_message(msg.into())?;
connection::update_message_status(conn_handle, uid)?;
self.handle_message(msg.into())
Ok(state)

}
None => Ok(self)
}
Expand Down Expand Up @@ -119,7 +121,6 @@ impl HolderSM {
Ok((cred_request, req_meta, cred_def_json)) => {
let cred_request = cred_request
.set_thread_id(&thread_id);
connection::remove_pending_message(connection_handle, &state_data.offer.id)?;
connection::send_message(connection_handle, cred_request.to_a2a_message())?;
HolderState::RequestSent((state_data, req_meta, cred_def_json, connection_handle).into())
}
Expand Down
7 changes: 4 additions & 3 deletions vcx/libvcx/src/v3/handlers/issuance/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use issuer_credential::encode_attributes;
use v3::messages::status::Status;
use std::collections::HashMap;
use connection::{send_message, get_messages};
use connection::update_message_status;
use connection;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IssuerSM {
Expand Down Expand Up @@ -54,8 +54,9 @@ impl IssuerSM {

match self.find_message_to_handle(messages) {
Some((uid, msg)) => {
update_message_status(conn_handle, uid)?;
self.handle_message(msg.into())
let state = self.handle_message(msg.into())?;
connection::update_message_status(conn_handle, uid)?;
Ok(state)
}
None => Ok(self)
}
Expand Down
36 changes: 14 additions & 22 deletions vcx/libvcx/src/v3/handlers/issuance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ pub mod states;
pub mod messages;
pub mod holder;

use std::collections::HashMap;
use error::prelude::*;
use messages::get_message::Message;
use v3::messages::a2a::{A2AMessage, MessageId};
use v3::messages::a2a::A2AMessage;
use v3::handlers::issuance::issuer::IssuerSM;
use v3::handlers::issuance::messages::CredentialIssuanceMessage;
use v3::handlers::issuance::holder::HolderSM;
Expand Down Expand Up @@ -51,10 +49,9 @@ impl Issuer {
pub fn update_status(&mut self, msg: Option<String>) -> VcxResult<()> {
match msg {
Some(msg) => {
let message: Message = ::serde_json::from_str(&msg)
let message: A2AMessage = ::serde_json::from_str(&msg)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidOption, format!("Cannot deserialize Message: {:?}", err)))?;

let message = connection::decode_message(self.issuer_sm.get_connection_handle(), message)?;
self.step(message.into())
}
None => {
Expand Down Expand Up @@ -97,10 +94,9 @@ impl Holder {
pub fn update_state(&mut self, msg: Option<String>) -> VcxResult<()> {
match msg {
Some(msg) => {
let message: Message = ::serde_json::from_str(&msg)
let message: A2AMessage = ::serde_json::from_str(&msg)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidOption, format!("Cannot update state: Message deserialization failed: {:?}", err)))?;

let message = connection::decode_message(self.holder_sm.get_connection_handle(), message)?;
self.step(message.into())
}
None => {
Expand Down Expand Up @@ -134,34 +130,30 @@ impl Holder {
pub fn get_credential_offer_message(connection_handle: u32, msg_id: &str) -> VcxResult<CredentialOffer> {
let message = connection::get_message_by_id(connection_handle, msg_id.to_string())?;

let (id, credential_offer): (MessageId, CredentialOffer) = match message {
A2AMessage::CredentialOffer(credential_offer) => (credential_offer.id.clone(), credential_offer),
msg => return Err(VcxError::from_msg(VcxErrorKind::InvalidMessages, format!("Message of different type was received: {:?}", msg)))
let credential_offer: CredentialOffer = match message {
A2AMessage::CredentialOffer(credential_offer) => credential_offer,
msg => {
return Err(VcxError::from_msg(VcxErrorKind::InvalidMessages,
format!("Message of different type was received: {:?}", msg)));
}
};

connection::add_pending_messages(connection_handle, map! { id => msg_id.to_string() })?;

Ok(credential_offer)
}

pub fn get_credential_offer_messages(conn_handle: u32) -> VcxResult<Vec<CredentialOffer>> {
let messages = connection::get_messages(conn_handle)?;
let (uids, msgs): (HashMap<MessageId, String>, Vec<CredentialOffer>) = messages
let msgs: Vec<CredentialOffer> = messages
.into_iter()
.filter_map(|(uid, a2a_message)| {
.filter_map(|(_, a2a_message)| {
match a2a_message {
A2AMessage::CredentialOffer(credential_offer) => {
Some((uid, credential_offer.id.clone(), credential_offer))
Some(credential_offer)
}
_ => None
}
}).fold((HashMap::new(), vec![]), |(mut uids, mut msgs), (uid, id, msg)| {
uids.insert(id, uid);
msgs.push(msg);
(uids, msgs)
});

connection::add_pending_messages(conn_handle, uids)?;
})
.collect();

Ok(msgs)
}
Expand Down
43 changes: 13 additions & 30 deletions vcx/libvcx/src/v3/handlers/proof_presentation/prover/prover.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
use messages::get_message::Message;
use error::prelude::*;
use utils::libindy::anoncreds;
use std::convert::TryInto;
use std::collections::HashMap;

use v3::handlers::proof_presentation::prover::states::ProverSM;
use v3::handlers::proof_presentation::prover::messages::ProverMessages;
use v3::messages::a2a::{A2AMessage, MessageId};
use v3::messages::a2a::A2AMessage;
use v3::messages::proof_presentation::presentation_proposal::PresentationPreview;
use v3::messages::proof_presentation::presentation_request::PresentationRequest;
use connection;

use messages::proofs::proof_message::ProofMessage;

use std::sync::Mutex;
use v3::messages::proof_presentation::presentation::Presentation;

lazy_static! {
pub static ref PENDING_PRESENTATION_REQUESTS: Mutex<HashMap<MessageId, String>> = Default::default();
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Prover {
prover_sm: ProverSM
Expand Down Expand Up @@ -93,16 +86,10 @@ impl Prover {
pub fn update_state_with_message(&mut self, message: &str) -> VcxResult<()> {
trace!("Prover::update_state_with_message >>> message: {:?}", message);

let message: Message = ::serde_json::from_str(&message)
let a2a_message: A2AMessage = ::serde_json::from_str(&message)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidOption, format!("Cannot updated state with message: Message deserialization failed: {:?}", err)))?;

let connection_handle = self.prover_sm.connection_handle()?;

let uid = message.uid.clone();
let a2a_message = connection::decode_message(connection_handle, message)?;

self.handle_message(a2a_message.into())?;
connection::update_message_status(connection_handle, uid)?;

Ok(())
}
Expand All @@ -117,36 +104,32 @@ impl Prover {

let message = connection::get_message_by_id(connection_handle, msg_id.to_string())?;

let (id, presentation_request): (MessageId, PresentationRequest) = match message {
A2AMessage::PresentationRequest(presentation_request) => (presentation_request.id.clone(), presentation_request),
msg => return Err(VcxError::from_msg(VcxErrorKind::InvalidMessages, format!("Message of different type was received: {:?}", msg)))
let presentation_request: PresentationRequest = match message {
A2AMessage::PresentationRequest(presentation_request) => presentation_request,
msg => {
return Err(VcxError::from_msg(VcxErrorKind::InvalidMessages,
format!("Message of different type was received: {:?}", msg)));
}
};

connection::add_pending_messages(connection_handle, map! { id => msg_id.to_string() })?;

Ok(presentation_request)
}

pub fn get_presentation_request_messages(connection_handle: u32, match_name: Option<&str>) -> VcxResult<Vec<PresentationRequest>> {
trace!("Prover::get_presentation_request_messages >>> connection_handle: {:?}, match_name: {:?}", connection_handle, match_name);

let (uids, presentation_requests): (HashMap<MessageId, String>, Vec<PresentationRequest>) =
let presentation_requests: Vec<PresentationRequest> =
connection::get_messages(connection_handle)?
.into_iter()
.filter_map(|(uid, message)| {
.filter_map(|(_, message)| {
match message {
A2AMessage::PresentationRequest(presentation_request) => {
Some((uid, presentation_request.id.clone(), presentation_request))
Some(presentation_request)
}
_ => None,
}
}).fold((HashMap::new(), Vec::new()), |(mut uids, mut messages), (uid, id, presentation_request)| {
uids.insert(id, uid);
messages.push(presentation_request);
(uids, messages)
});

connection::add_pending_messages(connection_handle, uids)?;
})
.collect();

Ok(presentation_requests)
}
Expand Down
Loading

0 comments on commit 1b9a5db

Please sign in to comment.