Skip to content

Commit

Permalink
Split BaseLedger trait (#861)
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee authored May 25, 2023
1 parent d770c96 commit 9f2e922
Show file tree
Hide file tree
Showing 41 changed files with 361 additions and 331 deletions.
2 changes: 1 addition & 1 deletion agents/rust/aries-vcx-agent/src/services/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ServiceSchemas {
}

pub async fn schema_json(&self, thread_id: &str) -> AgentResult<String> {
let ledger = Arc::clone(&self.profile).inject_ledger();
let ledger = Arc::clone(&self.profile).inject_anoncreds_ledger_read();
Ok(ledger.get_schema(thread_id, None).await?)
}

Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/common/anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub mod integration_tests {
)
.await;

let ledger = Arc::clone(&holder_setup.profile).inject_ledger();
let ledger = Arc::clone(&holder_setup.profile).inject_anoncreds_ledger_read();

let (_, first_rev_reg_delta, first_timestamp) =
ledger.get_rev_reg_delta_json(&rev_reg_id, None, None).await.unwrap();
Expand Down
18 changes: 9 additions & 9 deletions aries_vcx/src/common/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::profile::profile::Profile;
use crate::errors::error::prelude::*;

pub async fn rotate_verkey_apply(profile: &Arc<dyn Profile>, did: &str, temp_vk: &str) -> VcxResult<()> {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_indy_ledger_write();

let nym_result = ledger.publish_nym(did, did, Some(temp_vk), None, None).await?;

Expand Down Expand Up @@ -41,7 +41,7 @@ pub async fn rotate_verkey(profile: &Arc<dyn Profile>, did: &str) -> VcxResult<(
}

pub async fn get_verkey_from_ledger(profile: &Arc<dyn Profile>, did: &str) -> VcxResult<String> {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_indy_ledger_read();

let nym_response: String = ledger.get_nym(did).await?;
let nym_json: Value = serde_json::from_str(&nym_response).map_err(|err| {
Expand Down Expand Up @@ -75,17 +75,17 @@ pub async fn get_verkey_from_ledger(profile: &Arc<dyn Profile>, did: &str) -> Vc
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
use aries_vcx_core::indy::utils::mocks::pool_mocks::{enable_pool_mocks, PoolMocks};

use crate::utils::devsetup::*;
use crate::utils::mockdata::mockdata_pool;

use super::*;

#[tokio::test]
#[ignore]
#[cfg(not(feature = "vdr_proxy_ledger"))]
async fn test_pool_rotate_verkey_fails() {
use super::*;

use aries_vcx_core::indy::utils::mocks::pool_mocks::{enable_pool_mocks, PoolMocks};

use crate::utils::devsetup::*;
use crate::utils::mockdata::mockdata_pool;

SetupProfile::run_indy(|setup| async move {
enable_pool_mocks();

Expand Down
16 changes: 8 additions & 8 deletions aries_vcx/src/common/ledger/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub async fn add_new_did(
) -> VcxResult<(String, String)> {
let (did, verkey) = profile.inject_wallet().create_and_store_my_did(None, None).await?;

let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_indy_ledger_write();

let res = ledger
.publish_nym(submitter_did, &did, Some(&verkey), None, role)
Expand Down Expand Up @@ -190,7 +190,7 @@ pub async fn get_service(profile: &Arc<dyn Profile>, did: &String) -> VcxResult<
None => did_raw,
Some((_, value)) => value.to_string(),
};
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_indy_ledger_read();
let attr_resp = ledger.get_attr(&did_raw, "endpoint").await?;
let data = get_data_from_response(&attr_resp)?;
if data["endpoint"].is_object() {
Expand All @@ -207,7 +207,7 @@ pub async fn get_service(profile: &Arc<dyn Profile>, did: &String) -> VcxResult<
}

pub async fn parse_legacy_endpoint_attrib(profile: &Arc<dyn Profile>, did_raw: &str) -> VcxResult<AriesService> {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_indy_ledger_read();
let attr_resp = ledger.get_attr(did_raw, "service").await?;
let data = get_data_from_response(&attr_resp)?;
let ser_service = match data["service"].as_str() {
Expand All @@ -227,28 +227,28 @@ pub async fn parse_legacy_endpoint_attrib(profile: &Arc<dyn Profile>, did_raw: &

pub async fn write_endpoint_legacy(profile: &Arc<dyn Profile>, did: &str, service: &AriesService) -> VcxResult<String> {
let attrib_json = json!({ "service": service }).to_string();
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_indy_ledger_write();
let res = ledger.add_attr(did, &attrib_json).await?;
check_response(&res)?;
Ok(res)
}

pub async fn write_endpoint(profile: &Arc<dyn Profile>, did: &str, service: &EndpointDidSov) -> VcxResult<String> {
let attrib_json = json!({ "endpoint": service }).to_string();
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_indy_ledger_write();
let res = ledger.add_attr(did, &attrib_json).await?;
check_response(&res)?;
Ok(res)
}

pub async fn add_attr(profile: &Arc<dyn Profile>, did: &str, attr: &str) -> VcxResult<()> {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_indy_ledger_write();
let res = ledger.add_attr(did, &attr).await?;
check_response(&res)
}

pub async fn get_attr(profile: &Arc<dyn Profile>, did: &str, attr_name: &str) -> VcxResult<String> {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_indy_ledger_read();
let attr_resp = ledger.get_attr(did, attr_name).await?;
let data = get_data_from_response(&attr_resp)?;
match data.get(attr_name) {
Expand All @@ -259,7 +259,7 @@ pub async fn get_attr(profile: &Arc<dyn Profile>, did: &str, attr_name: &str) ->
}

pub async fn clear_attr(profile: &Arc<dyn Profile>, did: &str, attr_name: &str) -> VcxResult<String> {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_indy_ledger_write();

ledger
.add_attr(did, &json!({ attr_name: Value::Null }).to_string())
Expand Down
35 changes: 20 additions & 15 deletions aries_vcx/src/common/primitives/credential_definition.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use aries_vcx_core::errors::error::AriesVcxCoreErrorKind;
use aries_vcx_core::ledger::base_ledger::BaseLedger;
use aries_vcx_core::ledger::base_ledger::AnoncredsLedgerRead;

use crate::core::profile::profile::Profile;
use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult};
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Default for PublicEntityStateType {
}

async fn _try_get_cred_def_from_ledger(
ledger: &Arc<dyn BaseLedger>,
ledger: &Arc<dyn AnoncredsLedgerRead>,
issuer_did: &str,
cred_def_id: &str,
) -> VcxResult<Option<String>> {
Expand Down Expand Up @@ -140,7 +140,7 @@ impl CredentialDef {
schema_id,
tag,
} = config;
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
let schema_json = ledger.get_schema(&schema_id, Some(&issuer_did)).await?;
let (cred_def_id, cred_def_json) =
generate_cred_def(profile, &issuer_did, &schema_json, &tag, None, Some(support_revocation)).await?;
Expand Down Expand Up @@ -170,8 +170,10 @@ impl CredentialDef {
self.issuer_did,
self.id
);
let ledger = Arc::clone(profile).inject_ledger();
if let Some(ledger_cred_def_json) = _try_get_cred_def_from_ledger(&ledger, &self.issuer_did, &self.id).await? {
let ledger_read = Arc::clone(profile).inject_anoncreds_ledger_read();
if let Some(ledger_cred_def_json) =
_try_get_cred_def_from_ledger(&ledger_read, &self.issuer_did, &self.id).await?
{
return Err(AriesVcxError::from_msg(
AriesVcxErrorKind::CredDefAlreadyCreated,
format!(
Expand All @@ -180,6 +182,7 @@ impl CredentialDef {
),
));
}
let ledger = Arc::clone(profile).inject_anoncreds_ledger_write();
ledger.publish_cred_def(&self.cred_def_json, &self.issuer_did).await?;
Ok(Self {
state: PublicEntityStateType::Published,
Expand Down Expand Up @@ -230,7 +233,7 @@ impl CredentialDef {
}

pub async fn update_state(&mut self, profile: &Arc<dyn Profile>) -> VcxResult<u32> {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
if (ledger.get_cred_def(&self.id, None).await).is_ok() {
self.state = PublicEntityStateType::Published
}
Expand Down Expand Up @@ -289,8 +292,9 @@ pub mod integration_tests {
let (schema_id, _) =
create_and_write_test_schema(&setup.profile, &setup.institution_did, DEFAULT_SCHEMA_ATTRS).await;

let ledger = Arc::clone(&setup.profile).inject_ledger();
let schema_json = ledger.get_schema(&schema_id, None).await.unwrap();
let ledger_read = Arc::clone(&setup.profile).inject_anoncreds_ledger_read();
let ledger_write = Arc::clone(&setup.profile).inject_anoncreds_ledger_write();
let schema_json = ledger_read.get_schema(&schema_id, None).await.unwrap();

let (cred_def_id, cred_def_json_local) = generate_cred_def(
&setup.profile,
Expand All @@ -303,14 +307,14 @@ pub mod integration_tests {
.await
.unwrap();

ledger
ledger_write
.publish_cred_def(&cred_def_json_local, &setup.institution_did)
.await
.unwrap();

std::thread::sleep(std::time::Duration::from_secs(2));

let cred_def_json_ledger = ledger
let cred_def_json_ledger = ledger_read
.get_cred_def(&cred_def_id, Some(&setup.institution_did))
.await
.unwrap();
Expand All @@ -327,8 +331,9 @@ pub mod integration_tests {
SetupProfile::run_indy(|setup| async move {
let (schema_id, _) =
create_and_write_test_schema(&setup.profile, &setup.institution_did, DEFAULT_SCHEMA_ATTRS).await;
let ledger = Arc::clone(&setup.profile).inject_ledger();
let schema_json = ledger.get_schema(&schema_id, None).await.unwrap();
let ledger_read = Arc::clone(&setup.profile).inject_anoncreds_ledger_read();
let ledger_write = Arc::clone(&setup.profile).inject_anoncreds_ledger_write();
let schema_json = ledger_read.get_schema(&schema_id, None).await.unwrap();

let (cred_def_id, cred_def_json) = generate_cred_def(
&setup.profile,
Expand All @@ -340,7 +345,7 @@ pub mod integration_tests {
)
.await
.unwrap();
ledger
ledger_write
.publish_cred_def(&cred_def_json, &setup.institution_did)
.await
.unwrap();
Expand All @@ -355,11 +360,11 @@ pub mod integration_tests {
)
.await
.unwrap();
ledger
ledger_write
.publish_rev_reg_def(&json!(rev_reg_def_json).to_string(), &setup.institution_did)
.await
.unwrap();
ledger
ledger_write
.publish_rev_reg_delta(&rev_reg_def_id, &rev_reg_entry_json, &setup.institution_did)
.await
.unwrap();
Expand Down
8 changes: 4 additions & 4 deletions aries_vcx/src/common/primitives/credential_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Schema {
source_id: &str,
schema_id: &str,
) -> VcxResult<Self> {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
let schema_json = ledger.get_schema(schema_id, None).await?;
let schema_data: SchemaData = serde_json::from_str(&schema_json).map_err(|err| {
AriesVcxError::from_msg(
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Schema {
});
}

let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_write();
ledger
.publish_schema(&self.schema_json, &self.submitter_did, endorser_did)
.await?;
Expand Down Expand Up @@ -153,7 +153,7 @@ impl Schema {
}

pub async fn update_state(&mut self, profile: &Arc<dyn Profile>) -> VcxResult<u32> {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
if ledger.get_schema(&self.schema_id, None).await.is_ok() {
self.state = PublicEntityStateType::Published
}
Expand All @@ -164,7 +164,7 @@ impl Schema {
if !self.schema_json.is_empty() {
Ok(self.schema_json.clone())
} else {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
Ok(ledger.get_schema(&self.schema_id, None).await?)
}
}
Expand Down
10 changes: 5 additions & 5 deletions aries_vcx/src/common/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub mod integration_tests {
let (_, _, _, _, rev_reg_id, _, _) =
create_and_store_credential_def(&setup.profile, &setup.institution_did, attrs).await;

let ledger = Arc::clone(&setup.profile).inject_ledger();
let ledger = Arc::clone(&setup.profile).inject_anoncreds_ledger_read();
let _json = ledger.get_rev_reg_def_json(&rev_reg_id).await.unwrap();
})
.await;
Expand All @@ -68,7 +68,7 @@ pub mod integration_tests {
let (_, _, _, _, rev_reg_id, _, _) =
create_and_store_credential_def(&setup.profile, &setup.institution_did, attrs).await;

let ledger = Arc::clone(&setup.profile).inject_ledger();
let ledger = Arc::clone(&setup.profile).inject_anoncreds_ledger_read();
let (id, _delta, _timestamp) = ledger.get_rev_reg_delta_json(&rev_reg_id, None, None).await.unwrap();

assert_eq!(id, rev_reg_id);
Expand All @@ -84,7 +84,7 @@ pub mod integration_tests {
let (_, _, _, _, rev_reg_id, _, _) =
create_and_store_credential_def(&setup.profile, &setup.institution_did, attrs).await;

let ledger = Arc::clone(&setup.profile).inject_ledger();
let ledger = Arc::clone(&setup.profile).inject_anoncreds_ledger_read();
let (id, _rev_reg, _timestamp) = ledger
.get_rev_reg(&rev_reg_id, time::OffsetDateTime::now_utc().unix_timestamp() as u64)
.await
Expand All @@ -103,7 +103,7 @@ pub mod integration_tests {
let (_, _, cred_def_id, cred_def_json, _) =
create_and_store_nonrevocable_credential_def(&setup.profile, &setup.institution_did, attrs).await;

let ledger = Arc::clone(&setup.profile).inject_ledger();
let ledger = Arc::clone(&setup.profile).inject_anoncreds_ledger_read();
let cred_def = ledger.get_cred_def(&cred_def_id, None).await.unwrap();

assert_eq!(
Expand All @@ -121,7 +121,7 @@ pub mod integration_tests {
let (schema_id, _schema_json) =
create_and_write_test_schema(&setup.profile, &setup.institution_did, DEFAULT_SCHEMA_ATTRS).await;

let ledger = Arc::clone(&setup.profile).inject_ledger();
let ledger = Arc::clone(&setup.profile).inject_anoncreds_ledger_read();
let rc = ledger.get_schema(&schema_id, None).await;

let retrieved_schema = rc.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions aries_vcx/src/common/primitives/revocation_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl RevocationRegistry {
&self.rev_reg_def
);
self.rev_reg_def.value.tails_location = String::from(tails_url);
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_write();
ledger
.publish_rev_reg_def(&json!(self.rev_reg_def).to_string(), issuer_did)
.await
Expand All @@ -121,7 +121,7 @@ impl RevocationRegistry {
issuer_did,
self.rev_reg_id
);
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_write();
ledger
.publish_rev_reg_delta(&self.rev_reg_id, &self.rev_reg_entry, issuer_did)
.await
Expand Down Expand Up @@ -196,7 +196,7 @@ impl RevocationRegistry {

pub async fn publish_local_revocations(&self, profile: &Arc<dyn Profile>, submitter_did: &str) -> VcxResult<()> {
let anoncreds = Arc::clone(profile).inject_anoncreds();
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_write();

if let Some(delta) = anoncreds.get_rev_reg_delta(&self.rev_reg_id).await? {
ledger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl RevocationRegistryDelta {
from: Option<u64>,
to: Option<u64>,
) -> VcxResult<Self> {
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
let (_, rev_reg_delta_json, _) = ledger.get_rev_reg_delta_json(rev_reg_id, from, to).await?;
serde_json::from_str(&rev_reg_delta_json).map_err(|err| {
AriesVcxError::from_msg(
Expand Down
6 changes: 3 additions & 3 deletions aries_vcx/src/common/proofs/prover/prover_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub async fn build_schemas_json_prover(
"build_schemas_json_prover >>> credentials_identifiers: {:?}",
credentials_identifiers
);
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
let mut rtn: Value = json!({});

for cred_info in credentials_identifiers {
Expand Down Expand Up @@ -62,7 +62,7 @@ pub async fn build_cred_defs_json_prover(
"build_cred_defs_json_prover >>> credentials_identifiers: {:?}",
credentials_identifiers
);
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
let mut rtn: Value = json!({});

for cred_info in credentials_identifiers {
Expand Down Expand Up @@ -167,7 +167,7 @@ pub async fn build_rev_states_json(
"build_rev_states_json >> credentials_identifiers: {:?}",
credentials_identifiers
);
let ledger = Arc::clone(profile).inject_ledger();
let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
let anoncreds = Arc::clone(profile).inject_anoncreds();
let mut rtn: Value = json!({});
let mut timestamps: HashMap<String, u64> = HashMap::new();
Expand Down
Loading

0 comments on commit 9f2e922

Please sign in to comment.