From 1b041049f9c0ac7545c24bd8628869dfd368ed4b Mon Sep 17 00:00:00 2001 From: Miroslav Kovar Date: Tue, 29 Nov 2022 09:11:47 +0100 Subject: [PATCH] Implement Storage trait for agent's ObjectCache (#671) * Change ObjectCache function names to reflect typical Rust interface * Hide ObjectCache behind an interface Signed-off-by: Miroslav Kovar --- .../src/services/connection.rs | 19 ++++---- .../src/services/credential_definition.rs | 5 ++- .../aries-vcx-agent/src/services/holder.rs | 11 ++--- .../aries-vcx-agent/src/services/issuer.rs | 13 +++--- .../src/services/mediated_connection.rs | 33 ++++---------- .../aries-vcx-agent/src/services/prover.rs | 11 ++--- .../src/services/revocation_registry.rs | 5 ++- .../aries-vcx-agent/src/services/schema.rs | 5 ++- .../aries-vcx-agent/src/services/verifier.rs | 7 +-- .../rust/aries-vcx-agent/src/storage/mod.rs | 12 ++++++ .../src/storage/object_cache.rs | 43 +++++++++---------- 11 files changed, 83 insertions(+), 81 deletions(-) diff --git a/agents/rust/aries-vcx-agent/src/services/connection.rs b/agents/rust/aries-vcx-agent/src/services/connection.rs index 0da8fec685..d5aaf5aaf9 100644 --- a/agents/rust/aries-vcx-agent/src/services/connection.rs +++ b/agents/rust/aries-vcx-agent/src/services/connection.rs @@ -1,6 +1,7 @@ use std::sync::{Arc, Mutex}; use crate::error::*; +use crate::storage::Storage; use crate::storage::object_cache::ObjectCache; use aries_vcx::handlers::connection::connection::{Connection, ConnectionState}; use aries_vcx::indy::ledger::transactions::into_did_doc; @@ -39,7 +40,7 @@ impl ServiceConnections { .get_invite_details() .ok_or_else(|| AgentError::from_kind(AgentErrorKind::InviteDetails))? .clone(); - self.connections.set(&inviter.get_thread_id(), inviter)?; + self.connections.insert(&inviter.get_thread_id(), inviter)?; Ok(invite) } @@ -48,7 +49,7 @@ impl ServiceConnections { let invitee = Connection::create_invitee(self.wallet_handle, did_doc) .await? .process_invite(invite)?; - self.connections.set(&invitee.get_thread_id(), invitee) + self.connections.insert(&invitee.get_thread_id(), invitee) } pub async fn send_request(&self, thread_id: &str) -> AgentResult<()> { @@ -57,7 +58,7 @@ impl ServiceConnections { .get(thread_id)? .send_request(self.wallet_handle, self.service_endpoint.clone(), vec![], None) .await?; - self.connections.set(thread_id, invitee)?; + self.connections.insert(thread_id, invitee)?; Ok(()) } @@ -67,7 +68,7 @@ impl ServiceConnections { .get(thread_id)? .process_request(self.wallet_handle, request, self.service_endpoint.clone(), vec![], None) .await?; - self.connections.set(thread_id, inviter)?; + self.connections.insert(thread_id, inviter)?; Ok(()) } @@ -77,7 +78,7 @@ impl ServiceConnections { .get(thread_id)? .send_response(self.wallet_handle, None) .await?; - self.connections.set(thread_id, inviter)?; + self.connections.insert(thread_id, inviter)?; Ok(()) } @@ -87,7 +88,7 @@ impl ServiceConnections { .get(thread_id)? .process_response(self.wallet_handle, response, None) .await?; - self.connections.set(thread_id, invitee)?; + self.connections.insert(thread_id, invitee)?; Ok(()) } @@ -97,7 +98,7 @@ impl ServiceConnections { .get(thread_id)? .send_ack(self.wallet_handle, None) .await?; - self.connections.set(thread_id, invitee)?; + self.connections.insert(thread_id, invitee)?; Ok(()) } @@ -107,7 +108,7 @@ impl ServiceConnections { .get(thread_id)? .process_ack(A2AMessage::Ack(ack)) .await?; - self.connections.set(thread_id, inviter)?; + self.connections.insert(thread_id, inviter)?; Ok(()) } @@ -133,6 +134,6 @@ impl ServiceConnections { pub fn exists_by_id(&self, thread_id: &str) -> bool { - self.connections.has_id(thread_id) + self.connections.contains_key(thread_id) } } diff --git a/agents/rust/aries-vcx-agent/src/services/credential_definition.rs b/agents/rust/aries-vcx-agent/src/services/credential_definition.rs index 8866e52c80..219443838a 100644 --- a/agents/rust/aries-vcx-agent/src/services/credential_definition.rs +++ b/agents/rust/aries-vcx-agent/src/services/credential_definition.rs @@ -1,6 +1,7 @@ use std::sync::Mutex; use crate::error::*; +use crate::storage::Storage; use crate::storage::object_cache::ObjectCache; use aries_vcx::indy::primitives::credential_definition::{CredentialDef, CredentialDefConfig}; use aries_vcx::vdrtools::{PoolHandle, WalletHandle}; @@ -29,7 +30,7 @@ impl ServiceCredentialDefinitions { true, ) .await?; - self.cred_defs.set(&cd.get_cred_def_id(), cd) + self.cred_defs.insert(&cd.get_cred_def_id(), cd) } pub async fn publish_cred_def(&self, thread_id: &str) -> AgentResult<()> { @@ -37,7 +38,7 @@ impl ServiceCredentialDefinitions { let cred_def = cred_def .publish_cred_def(self.wallet_handle, self.pool_handle) .await?; - self.cred_defs.set(thread_id, cred_def)?; + self.cred_defs.insert(thread_id, cred_def)?; Ok(()) } diff --git a/agents/rust/aries-vcx-agent/src/services/holder.rs b/agents/rust/aries-vcx-agent/src/services/holder.rs index f738d78af1..9035507ac1 100644 --- a/agents/rust/aries-vcx-agent/src/services/holder.rs +++ b/agents/rust/aries-vcx-agent/src/services/holder.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use crate::error::*; +use crate::storage::Storage; use crate::services::connection::ServiceConnections; use crate::storage::object_cache::ObjectCache; use aries_vcx::handlers::issuance::holder::Holder; @@ -69,7 +70,7 @@ impl ServiceCredentialsHolder { connection.send_message_closure(self.wallet_handle, None).await?, ) .await?; - self.creds_holder.set( + self.creds_holder.insert( &holder.get_thread_id()?, HolderWrapper::new(holder, connection_id), ) @@ -82,7 +83,7 @@ impl ServiceCredentialsHolder { ) -> AgentResult { self.service_connections.get_by_id(connection_id)?; let holder = Holder::create_from_offer("", offer)?; - self.creds_holder.set( + self.creds_holder.insert( &holder.get_thread_id()?, HolderWrapper::new(holder, connection_id), ) @@ -108,7 +109,7 @@ impl ServiceCredentialsHolder { connection.send_message_closure(self.wallet_handle, None).await?, ) .await?; - self.creds_holder.set( + self.creds_holder.insert( &holder.get_thread_id()?, HolderWrapper::new(holder, &connection_id), ) @@ -130,7 +131,7 @@ impl ServiceCredentialsHolder { connection.send_message_closure(self.wallet_handle, None).await?, ) .await?; - self.creds_holder.set( + self.creds_holder.insert( &holder.get_thread_id()?, HolderWrapper::new(holder, &connection_id), ) @@ -166,6 +167,6 @@ impl ServiceCredentialsHolder { } pub fn exists_by_id(&self, thread_id: &str) -> bool { - self.creds_holder.has_id(thread_id) + self.creds_holder.contains_key(thread_id) } } diff --git a/agents/rust/aries-vcx-agent/src/services/issuer.rs b/agents/rust/aries-vcx-agent/src/services/issuer.rs index d13581085e..4ffcb34673 100644 --- a/agents/rust/aries-vcx-agent/src/services/issuer.rs +++ b/agents/rust/aries-vcx-agent/src/services/issuer.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use crate::error::*; use crate::services::connection::ServiceConnections; +use crate::storage::Storage; use crate::storage::object_cache::ObjectCache; use aries_vcx::handlers::issuance::issuer::Issuer; use aries_vcx::messages::issuance::credential_ack::CredentialAck; @@ -60,7 +61,7 @@ impl ServiceCredentialsIssuer { proposal: &CredentialProposal, ) -> AgentResult { let issuer = Issuer::create_from_proposal("", proposal)?; - self.creds_issuer.set( + self.creds_issuer.insert( &issuer.get_thread_id()?, IssuerWrapper::new(issuer, connection_id), ) @@ -85,7 +86,7 @@ impl ServiceCredentialsIssuer { issuer .send_credential_offer(connection.send_message_closure(self.wallet_handle, None).await?) .await?; - self.creds_issuer.set( + self.creds_issuer.insert( &issuer.get_thread_id()?, IssuerWrapper::new(issuer, &connection_id), ) @@ -97,7 +98,7 @@ impl ServiceCredentialsIssuer { connection_id, } = self.creds_issuer.get(thread_id)?; issuer.process_credential_request(request)?; - self.creds_issuer.set( + self.creds_issuer.insert( &issuer.get_thread_id()?, IssuerWrapper::new(issuer, &connection_id), )?; @@ -110,7 +111,7 @@ impl ServiceCredentialsIssuer { connection_id, } = self.creds_issuer.get(thread_id)?; issuer.process_credential_ack(ack)?; - self.creds_issuer.set( + self.creds_issuer.insert( &issuer.get_thread_id()?, IssuerWrapper::new(issuer, &connection_id), )?; @@ -129,7 +130,7 @@ impl ServiceCredentialsIssuer { connection.send_message_closure(self.wallet_handle, None).await?, ) .await?; - self.creds_issuer.set( + self.creds_issuer.insert( &issuer.get_thread_id()?, IssuerWrapper::new(issuer, &connection_id), )?; @@ -156,7 +157,7 @@ impl ServiceCredentialsIssuer { } pub fn exists_by_id(&self, thread_id: &str) -> bool { - self.creds_issuer.has_id(thread_id) + self.creds_issuer.contains_key(thread_id) } } diff --git a/agents/rust/aries-vcx-agent/src/services/mediated_connection.rs b/agents/rust/aries-vcx-agent/src/services/mediated_connection.rs index 2dfb678699..2a32672fe0 100644 --- a/agents/rust/aries-vcx-agent/src/services/mediated_connection.rs +++ b/agents/rust/aries-vcx-agent/src/services/mediated_connection.rs @@ -1,13 +1,13 @@ use std::sync::Arc; use crate::error::*; +use crate::storage::Storage; use crate::storage::object_cache::ObjectCache; use aries_vcx::messages::connection::invite::Invitation; use aries_vcx::messages::connection::request::Request; use aries_vcx::messages::issuance::credential_offer::CredentialOffer; use aries_vcx::messages::issuance::credential_proposal::CredentialProposal; use aries_vcx::messages::proof_presentation::presentation_proposal::PresentationProposal; -use aries_vcx::messages::proof_presentation::presentation_request::PresentationRequest; use aries_vcx::{ agency_client::{agency_client::AgencyClient, configuration::AgencyClientConfig}, handlers::connection::mediated_connection::{MediatedConnection, ConnectionState}, @@ -59,7 +59,7 @@ impl ServiceMediatedConnections { .ok_or_else(|| AgentError::from_kind(AgentErrorKind::InviteDetails))? .clone(); self.mediated_connections - .set(&connection.get_thread_id(), connection)?; + .insert(&connection.get_thread_id(), connection)?; Ok(invite) } @@ -75,7 +75,7 @@ impl ServiceMediatedConnections { ) .await?; self.mediated_connections - .set(&connection.get_thread_id(), connection) + .insert(&connection.get_thread_id(), connection) } pub async fn send_request(&self, thread_id: &str) -> AgentResult<()> { @@ -86,7 +86,7 @@ impl ServiceMediatedConnections { connection .find_message_and_update_state(self.wallet_handle, &self.agency_client()?) .await?; - self.mediated_connections.set(thread_id, connection)?; + self.mediated_connections.insert(thread_id, connection)?; Ok(()) } @@ -96,14 +96,14 @@ impl ServiceMediatedConnections { .process_request(self.wallet_handle, &self.agency_client()?, request) .await?; connection.send_response(self.wallet_handle).await?; - self.mediated_connections.set(thread_id, connection)?; + self.mediated_connections.insert(thread_id, connection)?; Ok(()) } pub async fn send_ping(&self, thread_id: &str) -> AgentResult<()> { let mut connection = self.mediated_connections.get(thread_id)?; connection.send_ping(self.wallet_handle, None).await?; - self.mediated_connections.set(thread_id, connection)?; + self.mediated_connections.insert(thread_id, connection)?; Ok(()) } @@ -116,29 +116,12 @@ impl ServiceMediatedConnections { connection .find_message_and_update_state(self.wallet_handle, &self.agency_client()?) .await?; - self.mediated_connections.set(thread_id, connection)?; + self.mediated_connections.insert(thread_id, connection)?; Ok(self.mediated_connections.get(thread_id)?.get_state()) } pub fn exists_by_id(&self, thread_id: &str) -> bool { - self.mediated_connections.has_id(thread_id) - } - - pub async fn get_all_proof_requests(&self) -> AgentResult> { - let agency_client = self.agency_client()?; - let mut requests = Vec::<(PresentationRequest, String)>::new(); - for connection in self.mediated_connections.get_all()? { - for (uid, message) in connection.get_messages(&agency_client).await?.into_iter() { - if let A2AMessage::PresentationRequest(request) = message { - connection - .update_message_status(&uid, &agency_client) - .await - .ok(); - requests.push((request, connection.get_thread_id())); - } - } - } - Ok(requests) + self.mediated_connections.contains_key(thread_id) } } diff --git a/agents/rust/aries-vcx-agent/src/services/prover.rs b/agents/rust/aries-vcx-agent/src/services/prover.rs index f67d45ae00..3fec0b6333 100644 --- a/agents/rust/aries-vcx-agent/src/services/prover.rs +++ b/agents/rust/aries-vcx-agent/src/services/prover.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::sync::Arc; use crate::error::*; +use crate::storage::Storage; use crate::storage::object_cache::ObjectCache; use aries_vcx::handlers::proof_presentation::prover::Prover; use aries_vcx::messages::proof_presentation::presentation_ack::PresentationAck; @@ -86,7 +87,7 @@ impl ServiceProver { ) -> AgentResult { self.service_connections.get_by_id(connection_id)?; let prover = Prover::create_from_request("", request)?; - self.provers.set( + self.provers.insert( &prover.get_thread_id()?, ProverWrapper::new(prover, connection_id), ) @@ -105,7 +106,7 @@ impl ServiceProver { connection.send_message_closure(self.wallet_handle, None).await?, ) .await?; - self.provers.set( + self.provers.insert( &prover.get_thread_id()?, ProverWrapper::new(prover, connection_id), ) @@ -138,7 +139,7 @@ impl ServiceProver { connection.send_message_closure(self.wallet_handle, None).await?, ) .await?; - self.provers.set( + self.provers.insert( &prover.get_thread_id()?, ProverWrapper::new(prover, &connection_id), )?; @@ -152,7 +153,7 @@ impl ServiceProver { ) -> AgentResult { let ProverWrapper { mut prover, connection_id } = self.provers.get(thread_id)?; prover.process_presentation_ack(ack)?; - self.provers.set( + self.provers.insert( &prover.get_thread_id()?, ProverWrapper::new(prover, &connection_id), ) @@ -164,6 +165,6 @@ impl ServiceProver { } pub fn exists_by_id(&self, thread_id: &str) -> bool { - self.provers.has_id(thread_id) + self.provers.contains_key(thread_id) } } diff --git a/agents/rust/aries-vcx-agent/src/services/revocation_registry.rs b/agents/rust/aries-vcx-agent/src/services/revocation_registry.rs index e92a8f93b7..7c0717637c 100644 --- a/agents/rust/aries-vcx-agent/src/services/revocation_registry.rs +++ b/agents/rust/aries-vcx-agent/src/services/revocation_registry.rs @@ -2,6 +2,7 @@ use std::path::Path; use std::sync::Mutex; use crate::error::*; +use crate::storage::Storage; use crate::storage::object_cache::ObjectCache; use aries_vcx::indy::primitives::revocation_registry::RevocationRegistry; use aries_vcx::vdrtools::{PoolHandle, WalletHandle}; @@ -43,7 +44,7 @@ impl ServiceRevocationRegistries { 1, ) .await?; - self.rev_regs.set(&rev_reg.get_rev_reg_id(), rev_reg) + self.rev_regs.insert(&rev_reg.get_rev_reg_id(), rev_reg) } pub fn tails_file_path(&self, thread_id: &str) -> AgentResult { @@ -64,7 +65,7 @@ impl ServiceRevocationRegistries { rev_reg .publish_revocation_primitives(self.wallet_handle, self.pool_handle, tails_url) .await?; - self.rev_regs.set(thread_id, rev_reg)?; + self.rev_regs.insert(thread_id, rev_reg)?; Ok(()) } diff --git a/agents/rust/aries-vcx-agent/src/services/schema.rs b/agents/rust/aries-vcx-agent/src/services/schema.rs index 860ac9b2cf..0fd16c64c2 100644 --- a/agents/rust/aries-vcx-agent/src/services/schema.rs +++ b/agents/rust/aries-vcx-agent/src/services/schema.rs @@ -1,6 +1,7 @@ use std::sync::Mutex; use crate::error::*; +use crate::storage::Storage; use crate::storage::object_cache::ObjectCache; use aries_vcx::indy::ledger::transactions::get_schema_json; use aries_vcx::indy::primitives::credential_schema::Schema; @@ -30,7 +31,7 @@ impl ServiceSchemas { attributes: &Vec, ) -> AgentResult { let schema = Schema::create("", &self.issuer_did, name, version, attributes).await?; - self.schemas.set(&schema.get_schema_id(), schema) + self.schemas.insert(&schema.get_schema_id(), schema) } pub async fn publish_schema(&self, thread_id: &str) -> AgentResult<()> { @@ -38,7 +39,7 @@ impl ServiceSchemas { let schema = schema .publish(self.wallet_handle, self.pool_handle, None) .await?; - self.schemas.set(thread_id, schema)?; + self.schemas.insert(thread_id, schema)?; Ok(()) } diff --git a/agents/rust/aries-vcx-agent/src/services/verifier.rs b/agents/rust/aries-vcx-agent/src/services/verifier.rs index 6c8b26f566..68ebddab0f 100644 --- a/agents/rust/aries-vcx-agent/src/services/verifier.rs +++ b/agents/rust/aries-vcx-agent/src/services/verifier.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use crate::error::*; +use crate::storage::Storage; use crate::storage::object_cache::ObjectCache; use aries_vcx::handlers::proof_presentation::verifier::Verifier; use aries_vcx::indy::proofs::proof_request::PresentationRequestData; @@ -63,7 +64,7 @@ impl ServiceVerifier { verifier .send_presentation_request(connection.send_message_closure(self.wallet_handle, None).await?) .await?; - self.verifiers.set( + self.verifiers.insert( &verifier.get_thread_id()?, VerifierWrapper::new(verifier, connection_id), ) @@ -78,7 +79,7 @@ impl ServiceVerifier { let VerifierWrapper { mut verifier, connection_id } = self.verifiers.get(thread_id)?; let connection = self.service_connections.get_by_id(&connection_id)?; verifier.verify_presentation(self.wallet_handle, self.pool_handle, presentation, connection.send_message_closure(self.wallet_handle, None).await?).await?; - self.verifiers.set( + self.verifiers.insert( thread_id, VerifierWrapper::new(verifier, &connection_id), )?; @@ -91,6 +92,6 @@ impl ServiceVerifier { } pub fn exists_by_id(&self, thread_id: &str) -> bool { - self.verifiers.has_id(thread_id) + self.verifiers.contains_key(thread_id) } } diff --git a/agents/rust/aries-vcx-agent/src/storage/mod.rs b/agents/rust/aries-vcx-agent/src/storage/mod.rs index e58bfa13bb..b4d47556d8 100644 --- a/agents/rust/aries-vcx-agent/src/storage/mod.rs +++ b/agents/rust/aries-vcx-agent/src/storage/mod.rs @@ -1 +1,13 @@ +use crate::AgentResult; + pub(crate) mod object_cache; + +pub trait Storage { + type Value; + fn get(&self, id: &str) -> AgentResult; + fn insert(&self, id: &str, obj: T) -> AgentResult; + fn contains_key(&self, id: &str) -> bool; + fn find_by(&self, closure: F) -> AgentResult> + where + F: FnMut((&String, &Self::Value)) -> Option; +} diff --git a/agents/rust/aries-vcx-agent/src/storage/object_cache.rs b/agents/rust/aries-vcx-agent/src/storage/object_cache.rs index 793856d1c9..e7dd2391d5 100644 --- a/agents/rust/aries-vcx-agent/src/storage/object_cache.rs +++ b/agents/rust/aries-vcx-agent/src/storage/object_cache.rs @@ -4,6 +4,8 @@ use std::sync::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::error::*; +use super::Storage; + pub struct ObjectCache where T: Clone, @@ -16,8 +18,8 @@ impl ObjectCache where T: Clone, { - pub fn new(cache_name: &str) -> ObjectCache { - ObjectCache { + pub fn new(cache_name: &str) -> Self { + Self { store: Default::default(), cache_name: cache_name.to_string(), } @@ -54,16 +56,14 @@ where } } } +} - pub fn has_id(&self, id: &str) -> bool { - let store = match self._lock_store_read() { - Ok(g) => g, - Err(_) => return false, - }; - store.contains_key(id) - } - pub fn get(&self, id: &str) -> AgentResult { +impl Storage for ObjectCache +where T: Clone { + type Value = Mutex; + + fn get(&self, id: &str) -> AgentResult { let store = self._lock_store_read()?; match store.get(id) { Some(m) => match m.lock() { @@ -86,7 +86,7 @@ where } } - pub fn set(&self, id: &str, obj: T) -> AgentResult { + fn insert(&self, id: &str, obj: T) -> AgentResult { let mut store = self._lock_store_write()?; match store.insert(id.to_string(), Mutex::new(obj)) { @@ -95,20 +95,19 @@ where } } - pub fn find_by(&self, closure: F) -> AgentResult> + fn contains_key(&self, id: &str) -> bool { + let store = match self._lock_store_read() { + Ok(g) => g, + Err(_) => return false, + }; + store.contains_key(id) + } + + fn find_by(&self, closure: F) -> AgentResult> where - F: FnMut((&String, &Mutex)) -> Option, + F: FnMut((&String, &Self::Value)) -> Option, { let store = self._lock_store_read()?; Ok(store.iter().filter_map(closure).collect()) } - - pub fn get_all(&self) -> AgentResult> { - let store = self._lock_store_read()?; - Ok(store - .iter() - .map(|(_, v)| v.lock().unwrap().deref().clone()) - .into_iter() - .collect()) - } }