Skip to content

Commit

Permalink
Implement Storage trait for agent's ObjectCache (#671)
Browse files Browse the repository at this point in the history
* Change ObjectCache function names to reflect typical Rust interface
* Hide ObjectCache behind an interface

Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee authored Nov 29, 2022
1 parent d7e013a commit 1b04104
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 81 deletions.
19 changes: 10 additions & 9 deletions agents/rust/aries-vcx-agent/src/services/connection.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
}

Expand All @@ -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<()> {
Expand All @@ -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(())
}

Expand All @@ -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(())
}

Expand All @@ -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(())
}

Expand All @@ -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(())
}

Expand All @@ -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(())
}

Expand All @@ -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(())
}

Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -29,15 +30,15 @@ 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<()> {
let cred_def = self.cred_defs.get(thread_id)?;
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(())
}

Expand Down
11 changes: 6 additions & 5 deletions agents/rust/aries-vcx-agent/src/services/holder.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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),
)
Expand All @@ -82,7 +83,7 @@ impl ServiceCredentialsHolder {
) -> AgentResult<String> {
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),
)
Expand All @@ -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),
)
Expand All @@ -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),
)
Expand Down Expand Up @@ -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)
}
}
13 changes: 7 additions & 6 deletions agents/rust/aries-vcx-agent/src/services/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,7 +61,7 @@ impl ServiceCredentialsIssuer {
proposal: &CredentialProposal,
) -> AgentResult<String> {
let issuer = Issuer::create_from_proposal("", proposal)?;
self.creds_issuer.set(
self.creds_issuer.insert(
&issuer.get_thread_id()?,
IssuerWrapper::new(issuer, connection_id),
)
Expand All @@ -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),
)
Expand All @@ -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),
)?;
Expand All @@ -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),
)?;
Expand All @@ -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),
)?;
Expand All @@ -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)
}
}

33 changes: 8 additions & 25 deletions agents/rust/aries-vcx-agent/src/services/mediated_connection.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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)
}

Expand All @@ -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<()> {
Expand All @@ -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(())
}

Expand All @@ -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(())
}

Expand All @@ -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<Vec<(PresentationRequest, String)>> {
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)
}
}

Expand Down
11 changes: 6 additions & 5 deletions agents/rust/aries-vcx-agent/src/services/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,7 +87,7 @@ impl ServiceProver {
) -> AgentResult<String> {
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),
)
Expand All @@ -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),
)
Expand Down Expand Up @@ -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),
)?;
Expand All @@ -152,7 +153,7 @@ impl ServiceProver {
) -> AgentResult<String> {
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),
)
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<String> {
Expand All @@ -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(())
}

Expand Down
Loading

0 comments on commit 1b04104

Please sign in to comment.