Skip to content

Commit

Permalink
Combine adding/updating schema operations in the Trust registry tog…
Browse files Browse the repository at this point in the history
…ether (#151)
  • Loading branch information
olegnn authored Feb 7, 2024
1 parent ea0516e commit bc3590a
Show file tree
Hide file tree
Showing 12 changed files with 1,575 additions and 926 deletions.
3 changes: 1 addition & 2 deletions pallets/core/src/common/state_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ crate::def_state_change! {
status_list_credential::UpdateStatusListCredential,
status_list_credential::RemoveStatusListCredential,
trust_registry::InitOrUpdateTrustRegistry,
trust_registry::AddSchemaMetadata,
trust_registry::UpdateSchemaMetadata,
trust_registry::SetSchemasMetadata,
trust_registry::UpdateDelegatedIssuers,
trust_registry::SuspendIssuers,
trust_registry::UnsuspendIssuers
Expand Down
31 changes: 10 additions & 21 deletions pallets/core/src/modules/trust_registry/actions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use super::*;
use crate::{common::TypesAndLimits, impl_action_with_nonce, util::BoundedBytes};
use alloc::collections::{BTreeMap, BTreeSet};
use crate::{
common::TypesAndLimits,
impl_action_with_nonce,
util::{BoundedBytes, MultiTargetUpdate},
};
use alloc::collections::BTreeSet;
use frame_support::{CloneNoBound, DebugNoBound, EqNoBound, PartialEqNoBound};
use utils::BoundedString;

Expand Down Expand Up @@ -37,24 +41,10 @@ pub struct InitOrUpdateTrustRegistry<T: TypesAndLimits> {
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[scale_info(skip_type_params(T))]
#[scale_info(omit_prefix)]
pub struct AddSchemaMetadata<T: TypesAndLimits> {
pub struct SetSchemasMetadata<T: TypesAndLimits> {
pub registry_id: TrustRegistryId,
pub schemas: BTreeMap<TrustRegistrySchemaId, TrustRegistrySchemaMetadata<T>>,
pub nonce: T::BlockNumber,
}

#[derive(Encode, Decode, scale_info_derive::TypeInfo, DebugNoBound, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
)]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[scale_info(skip_type_params(T))]
#[scale_info(omit_prefix)]
pub struct UpdateSchemaMetadata<T: TypesAndLimits> {
pub registry_id: TrustRegistryId,
pub schemas: BTreeMap<TrustRegistrySchemaId, TrustRegistrySchemaMetadataUpdate<T>>,
pub schemas:
MultiTargetUpdate<TrustRegistrySchemaId, TrustRegistrySchemaMetadataModification<T>>,
pub nonce: T::BlockNumber,
}

Expand Down Expand Up @@ -111,8 +101,7 @@ impl_action_with_nonce!(

impl_action_with_nonce!(
for TrustRegistryId:
AddSchemaMetadata with schemas.len() as len, registry_id as target,
UpdateSchemaMetadata with schemas.len() as len, registry_id as target,
SetSchemasMetadata with schemas.len() as len, registry_id as target,
SuspendIssuers with issuers.len() as len, registry_id as target,
UnsuspendIssuers with issuers.len() as len, registry_id as target
);
118 changes: 44 additions & 74 deletions pallets/core/src/modules/trust_registry/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use sp_runtime::traits::TryCollect;
use sp_std::prelude::*;

const TRUST_REGISTRY_MAX_NAME: u32 = 30;
const TRUST_REGISTRY_GOV_FRAMEWORK: u32 = 100;
const SCHEMA_ISSUERS: u32 = 50;
const SCHEMA_VERIFIERS: u32 = 50;
const SCHEMA_ISSUER_PRICES: u32 = 20;
Expand All @@ -25,6 +26,7 @@ crate::bench_with_all_pairs! {
init_or_update_trust_registry_sr25519 for sr25519, init_or_update_trust_registry_ed25519 for ed25519, init_or_update_trust_registry_secp256k1 for secp256k1 {
{
let n in 1 .. TRUST_REGISTRY_MAX_NAME as u32;
let g in 1 .. TRUST_REGISTRY_GOV_FRAMEWORK as u32;
}
let pair as Pair;
let caller = whitelisted_caller();
Expand All @@ -41,7 +43,7 @@ crate::bench_with_all_pairs! {
let init_or_update_trust_registry = InitOrUpdateTrustRegistry {
registry_id: TrustRegistryId(id),
nonce: 1u32.into(),
gov_framework: Bytes(vec![1; 100]).try_into().unwrap(),
gov_framework: Bytes(vec![1; g as usize]).try_into().unwrap(),
name: (0..n).map(|idx| (98 + idx) as u8 as char).collect::<String>().try_into().unwrap()
};
let sig = pair.sign(&init_or_update_trust_registry.to_state_change().encode());
Expand All @@ -55,65 +57,7 @@ crate::bench_with_all_pairs! {
});
}

add_schema_metadata_sr25519 for sr25519, add_schema_metadata_ed25519 for ed25519, add_schema_metadata_secp256k1 for secp256k1 {
{
let i in 0 .. SCHEMA_ISSUERS as u32;
let v in 0 .. SCHEMA_VERIFIERS as u32;
let s in 1 .. SCHEMAS_COUNT as u32;
}
let pair as Pair;
let caller = whitelisted_caller();
let did = Did([0; Did::BYTE_SIZE]);
let public = pair.public();

crate::did::Pallet::<T>::new_onchain_(
did,
vec![UncheckedDidKey::new_with_all_relationships(public)],
Default::default(),
).unwrap();

let id = [1u8; 32].into();
ActionWrapper::<T, _, _>::new(1u32.into(), Convener(did.into()), InitOrUpdateTrustRegistry {
registry_id: TrustRegistryId(id),
nonce: 1u32.into(),
gov_framework: Bytes(vec![1; 100]).try_into().unwrap(),
name: (0..10).map(|idx| (98 + idx) as u8 as char).collect::<String>().try_into().unwrap()
}).execute::<T, _, _, _, _>(|action, set| Pallet::<T>::init_or_update_trust_registry_(action.action, set, Convener(did.into()))).unwrap();

let schemas: BTreeMap<_, _> = (0..s)
.map(|idx|
(
TrustRegistrySchemaId([idx as u8; 32]),
TrustRegistrySchemaMetadata {
issuers: IssuersWith((0..i).map(|idx|
(
Issuer(Did([idx as u8; 32]).into()),
VerificationPrices(
(0..SCHEMA_ISSUER_PRICES)
.map(|p_idx| ((0..SCHEMA_ISSUER_PRICE_SYMBOL).map(|idx| (98 + idx + p_idx) as u8 as char).collect::<String>().try_into().unwrap(), Price(1000)))
.collect::<BTreeMap<_, _>>()
.try_into()
.unwrap())
)
).try_collect().unwrap()),
verifiers: SchemaVerifiers((0..v).map(|idx| Verifier(Did([idx as u8; 32]).into())).try_collect().unwrap())
}
)
).collect();

let add_schema_metadata = AddSchemaMetadata {
registry_id: TrustRegistryId(id),
schemas: schemas.clone(),
nonce: 1u32.into()
};
let sig = pair.sign(&add_schema_metadata.to_state_change().encode());
let signature = DidSignature::new(did, 1u32, sig).into();
}: add_schema_metadata(RawOrigin::Signed(caller), add_schema_metadata.clone(), signature)
verify {
assert_eq!(schemas, TrustRegistrySchemasMetadata::<T>::iter().map(|(schema_id, _, metadata)| (schema_id, metadata)).collect::<BTreeMap<_, _>>());
}

update_schema_metadata_sr25519 for sr25519, update_schema_metadata_ed25519 for ed25519, update_schema_metadata_secp256k1 for secp256k1 {
set_schemas_metadata_sr25519 for sr25519, set_schemas_metadata_ed25519 for ed25519, set_schemas_metadata_secp256k1 for secp256k1 {
{
let i in 0 .. SCHEMA_ISSUERS as u32;
let v in 0 .. SCHEMA_VERIFIERS as u32;
Expand Down Expand Up @@ -149,30 +93,44 @@ crate::bench_with_all_pairs! {
Issuer(Did([255 - idx as u8; 32]).into()),
VerificationPrices(
(0..SCHEMA_ISSUER_PRICES)
.map(|p_idx| ((0..SCHEMA_ISSUER_PRICE_SYMBOL).map(|idx| (98 + idx + p_idx) as u8 as char).collect::<String>().try_into().unwrap(), Price(1000)))
.map(|p_idx| (
(0..SCHEMA_ISSUER_PRICE_SYMBOL)
.map(|idx| (98 + idx + p_idx) as u8 as char)
.collect::<String>()
.try_into().unwrap(),
VerificationPrice(1000)
))
.collect::<BTreeMap<_, _>>()
.try_into()
.unwrap())
.unwrap()
)
)
).try_collect().unwrap()),
verifiers: SchemaVerifiers((0..SCHEMA_VERIFIERS - v).map(|idx| Verifier(Did([255 - idx as u8; 32]).into())).try_collect().unwrap())
}
)
).collect();

AddSchemaMetadata {
SetSchemasMetadata {
registry_id: TrustRegistryId(id),
schemas: schemas.clone(),
schemas: schemas.clone().into_iter().map(|(id, schema)| (id, SetOrAddOrRemoveOrModify::Set(schema))).collect(),
nonce: 2u32.into()
}.execute_readonly(|action, set| Pallet::<T>::add_schema_metadata_(action, set, Convener(did.into()))).unwrap();
}.execute_readonly(|action, set| Pallet::<T>::set_schemas_metadata_(action, set, ConvenerOrIssuerOrVerifier(did.into()))).unwrap();

let update_issuers = schemas.keys().map(
|schema_id| {
IssuersUpdate::<T>::Modify(
MultiTargetUpdate::from_iter((0..i).map(|idx| (Issuer(Did([idx as u8; 32]).into()), SetOrModify::Set(
MultiTargetUpdate::from_iter((0..i).map(|idx| (Issuer(Did([idx as u8; 32]).into()), SetOrAddOrRemoveOrModify::Set(
VerificationPrices(
(0..SCHEMA_ISSUER_PRICES)
.map(|p_idx| ((0..SCHEMA_ISSUER_PRICE_SYMBOL).map(|idx| (98 + idx + p_idx) as u8 as char).collect::<String>().try_into().unwrap(), Price(1000)))
.map(|p_idx| (
(0..SCHEMA_ISSUER_PRICE_SYMBOL)
.map(|idx| (98 + idx + p_idx) as u8 as char)
.collect::<String>()
.try_into()
.unwrap(),
VerificationPrice(1000)
))
.collect::<BTreeMap<_, _>>()
.try_into()
.unwrap()
Expand All @@ -189,29 +147,34 @@ crate::bench_with_all_pairs! {
}
);

let schemas_updates: BTreeMap<_, _> =
let schemas_updates: MultiTargetUpdate<_, _> =
update_issuers
.zip(update_verifiers)
.zip(schemas.keys())
.map(|((issuers, verifiers), id)| (id.clone(), TrustRegistrySchemaMetadataUpdate {
.map(|((issuers, verifiers), id)| (id.clone(), SetOrAddOrRemoveOrModify::Modify(OnlyExistent(TrustRegistrySchemaMetadataUpdate {
verifiers: Some(verifiers),
issuers: Some(issuers)
}))
}))))
.collect();

let update_schema_metadata = UpdateSchemaMetadata {
let set_schemas_metadata = SetSchemasMetadata {
registry_id: TrustRegistryId(id),
schemas: schemas_updates.clone(),
nonce: 1u32.into()
};

for (key, update) in schemas_updates {
update.apply_update(schemas.get_mut(&key).unwrap());
let mut opt = schemas.remove(&key);
update.apply_update(&mut opt);

if let Some(schema) = opt {
schemas.insert(key, schema);
}
}

let sig = pair.sign(&update_schema_metadata.to_state_change().encode());
let sig = pair.sign(&set_schemas_metadata.to_state_change().encode());
let signature = DidSignature::new(did, 1u32, sig).into();
}: update_schema_metadata(RawOrigin::Signed(caller), update_schema_metadata, signature)
}: set_schemas_metadata(RawOrigin::Signed(caller), set_schemas_metadata, signature)
verify {
assert_eq!(schemas, TrustRegistrySchemasMetadata::<T>::iter().map(|(schema_id, _, metadata)| (schema_id, metadata)).collect::<BTreeMap<_, _>>());
}
Expand Down Expand Up @@ -341,11 +304,18 @@ crate::bench_with_all_pairs! {
TrustRegistryIssuerSchemas::<T>::insert(init_or_update_trust_registry.registry_id, issuer, IssuerSchemas(Default::default()));
}

SuspendIssuers {
registry_id: TrustRegistryId(id),
issuers: issuers.clone().into_iter().collect(),
nonce: 1u32.into()
}.execute_readonly::<T, _, _, _, _>(|action, reg_info| Pallet::<T>::suspend_issuers_(action, reg_info, Convener(did.into()))).unwrap();

let unsuspend_issuers = UnsuspendIssuers {
registry_id: TrustRegistryId(id),
issuers: issuers.into_iter().collect(),
nonce: 1u32.into()
};

let sig = pair.sign(&unsuspend_issuers.to_state_change().encode());
let signature = DidSignature::new(did, 1u32, sig).into();
}: unsuspend_issuers(RawOrigin::Signed(caller), unsuspend_issuers.clone(), signature)
Expand Down
Loading

0 comments on commit bc3590a

Please sign in to comment.