Skip to content

Commit

Permalink
chore: instantiatable linking pallet (#781)
Browse files Browse the repository at this point in the history
Part of KILTprotocol/ticket#3650. Built on top
of #779.
  • Loading branch information
ntn-x2 authored Nov 6, 2024
1 parent 37ec4d1 commit 6f7f587
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 160 deletions.
116 changes: 58 additions & 58 deletions pallets/pallet-did-lookup/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

//! Benchmarking
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_benchmarking::{account, benchmarks_instance_pallet, impl_benchmark_test_suite};
use frame_support::{
crypto::ecdsa::ECDSAExt,
traits::{
Expand Down Expand Up @@ -50,23 +50,23 @@ const SEED: u32 = 0;

// Free 2x deposit amount + existential deposit so that we can use this function
// to link an account two times to two different DIDs.
fn make_free_for_did<T: Config>(account: &AccountIdOf<T>)
fn make_free_for_did<T: Config<I>, I: 'static>(account: &AccountIdOf<T>)
where
<T as Config>::Currency: Mutate<T::AccountId>,
<T as Config<I>>::Currency: Mutate<T::AccountId>,
{
let balance = <CurrencyOf<T> as Inspect<AccountIdOf<T>>>::minimum_balance()
+ <T as Config>::Deposit::get()
+ <T as Config>::Deposit::get();
CurrencyOf::<T>::set_balance(account, balance);
let balance = <CurrencyOf<T, I> as Inspect<AccountIdOf<T>>>::minimum_balance()
+ <T as Config<I>>::Deposit::get()
+ <T as Config<I>>::Deposit::get();
CurrencyOf::<T, I>::set_balance(account, balance);
}

benchmarks! {
benchmarks_instance_pallet! {
where_clause {
where
T::AccountId: From<sr25519::Public> + From<ed25519::Public> + Into<LinkableAccountId> + Into<AccountId32> + From<sp_runtime::AccountId32>,
T::DidIdentifier: From<T::AccountId>,
T::EnsureOrigin: GenerateBenchmarkOrigin<T::RuntimeOrigin, T::AccountId, T::DidIdentifier>,
<T as Config>::Currency: Mutate<T::AccountId>,
<T as Config<I>>::DidIdentifier: From<T::AccountId>,
<T as Config<I>>::EnsureOrigin: GenerateBenchmarkOrigin<T::RuntimeOrigin, T::AccountId, <T as Config<I>>::DidIdentifier>,
<T as Config<I>>::Currency: Mutate<T::AccountId>,
}

associate_account_multisig_sr25519 {
Expand All @@ -87,19 +87,19 @@ benchmarks! {
))
.ok_or("Error while building signature.")?;

make_free_for_did::<T>(&caller);
make_free_for_did::<T, I>(&caller);

// Add existing connected_acc -> previous_did connection that will be replaced
Pallet::<T>::add_association(caller.clone(), previous_did.clone(), linkable_id.clone()).expect("should create previous association");
assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_some());
Pallet::<T, I>::add_association(caller.clone(), previous_did.clone(), linkable_id.clone()).expect("should create previous association");
assert!(ConnectedAccounts::<T, I>::get(&previous_did, linkable_id.clone()).is_some());
let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
let id_arg = linkable_id.clone();
let req = AssociateAccountRequest::Polkadot(connected_acc_id.into(), sig.into());
}: associate_account<T::RuntimeOrigin>(origin, req, expire_at)
verify {
assert!(ConnectedDids::<T>::get(linkable_id.clone()).is_some());
assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_none());
assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_some());
assert!(ConnectedDids::<T, I>::get(linkable_id.clone()).is_some());
assert!(ConnectedAccounts::<T, I>::get(&previous_did, linkable_id.clone()).is_none());
assert!(ConnectedAccounts::<T, I>::get(did, linkable_id).is_some());
}

associate_account_multisig_ed25519 {
Expand All @@ -120,19 +120,19 @@ benchmarks! {
))
.ok_or("Error while building signature.")?;

make_free_for_did::<T>(&caller);
make_free_for_did::<T, I>(&caller);

// Add existing connected_acc -> previous_did connection that will be replaced
Pallet::<T>::add_association(caller.clone(), previous_did.clone(), linkable_id.clone()).expect("should create previous association");
assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_some());
Pallet::<T, I>::add_association(caller.clone(), previous_did.clone(), linkable_id.clone()).expect("should create previous association");
assert!(ConnectedAccounts::<T, I>::get(&previous_did, linkable_id.clone()).is_some());
let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
let id_arg = linkable_id.clone();
let req = AssociateAccountRequest::Polkadot(connected_acc_id.into(), sig.into());
}: associate_account<T::RuntimeOrigin>(origin, req, expire_at)
verify {
assert!(ConnectedDids::<T>::get(linkable_id.clone()).is_some());
assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_none());
assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_some());
assert!(ConnectedDids::<T, I>::get(linkable_id.clone()).is_some());
assert!(ConnectedAccounts::<T, I>::get(&previous_did, linkable_id.clone()).is_none());
assert!(ConnectedAccounts::<T, I>::get(did, linkable_id).is_some());
}

associate_account_multisig_ecdsa {
Expand All @@ -153,19 +153,19 @@ benchmarks! {
))
.ok_or("Error while building signature.")?;

make_free_for_did::<T>(&caller);
make_free_for_did::<T, I>(&caller);

// Add existing connected_acc -> previous_did connection that will be replaced
Pallet::<T>::add_association(caller.clone(), previous_did.clone(), linkable_id.clone()).expect("should create previous association");
assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_some());
Pallet::<T, I>::add_association(caller.clone(), previous_did.clone(), linkable_id.clone()).expect("should create previous association");
assert!(ConnectedAccounts::<T, I>::get(&previous_did, linkable_id.clone()).is_some());
let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
let id_arg = linkable_id.clone();
let req = AssociateAccountRequest::Polkadot(connected_acc_id, sig.into());
}: associate_account<T::RuntimeOrigin>(origin, req, expire_at)
verify {
assert!(ConnectedDids::<T>::get(linkable_id.clone()).is_some());
assert!(ConnectedAccounts::<T>::get(&previous_did, linkable_id.clone()).is_none());
assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_some());
assert!(ConnectedDids::<T, I>::get(linkable_id.clone()).is_some());
assert!(ConnectedAccounts::<T, I>::get(&previous_did, linkable_id.clone()).is_none());
assert!(ConnectedAccounts::<T, I>::get(did, linkable_id).is_some());
}

associate_eth_account {
Expand All @@ -188,18 +188,18 @@ benchmarks! {
&Keccak256::digest(wrapped_payload).try_into().unwrap(),
).ok_or("Error while building signature.")?;

make_free_for_did::<T>(&caller);
make_free_for_did::<T, I>(&caller);

// Add existing connected_acc -> previous_did connection that will be replaced
Pallet::<T>::add_association(caller.clone(), previous_did.clone(), eth_account.into()).expect("should create previous association");
assert!(ConnectedAccounts::<T>::get(&previous_did, LinkableAccountId::from(eth_account)).is_some());
Pallet::<T, I>::add_association(caller.clone(), previous_did.clone(), eth_account.into()).expect("should create previous association");
assert!(ConnectedAccounts::<T, I>::get(&previous_did, LinkableAccountId::from(eth_account)).is_some());
let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
let req = AssociateAccountRequest::Ethereum(eth_account, sig.into());
}: associate_account<T::RuntimeOrigin>(origin, req, expire_at)
verify {
assert!(ConnectedDids::<T>::get(LinkableAccountId::from(eth_account)).is_some());
assert!(ConnectedAccounts::<T>::get(&previous_did, LinkableAccountId::from(eth_account)).is_none());
assert!(ConnectedAccounts::<T>::get(did, LinkableAccountId::from(eth_account)).is_some());
assert!(ConnectedDids::<T, I>::get(LinkableAccountId::from(eth_account)).is_some());
assert!(ConnectedAccounts::<T, I>::get(&previous_did, LinkableAccountId::from(eth_account)).is_none());
assert!(ConnectedAccounts::<T, I>::get(did, LinkableAccountId::from(eth_account)).is_some());
}

associate_sender {
Expand All @@ -208,69 +208,69 @@ benchmarks! {
let did: T::DidIdentifier = account("did", 0, SEED);
let previous_did: T::DidIdentifier = account("prev", 0, SEED + 1);

make_free_for_did::<T>(&caller);
make_free_for_did::<T, I>(&caller);

// Add existing sender -> previous_did connection that will be replaced
Pallet::<T>::add_association(caller.clone(), previous_did.clone(), caller.clone().into()).expect("should create previous association");
assert!(ConnectedAccounts::<T>::get(&previous_did, &linkable_id).is_some());
Pallet::<T, I>::add_association(caller.clone(), previous_did.clone(), caller.clone().into()).expect("should create previous association");
assert!(ConnectedAccounts::<T, I>::get(&previous_did, &linkable_id).is_some());
let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
}: _<T::RuntimeOrigin>(origin)
verify {
assert!(ConnectedDids::<T>::get(&linkable_id).is_some());
assert!(ConnectedAccounts::<T>::get(previous_did, &linkable_id).is_none());
assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_some());
assert!(ConnectedDids::<T, I>::get(&linkable_id).is_some());
assert!(ConnectedAccounts::<T, I>::get(previous_did, &linkable_id).is_none());
assert!(ConnectedAccounts::<T, I>::get(did, linkable_id).is_some());
}

remove_sender_association {
let caller: T::AccountId = account("caller", 0, SEED);
let linkable_id: LinkableAccountId = caller.clone().into();
let did: T::DidIdentifier = account("did", 0, SEED);

make_free_for_did::<T>(&caller);
Pallet::<T>::add_association(caller.clone(), did.clone(), linkable_id.clone()).expect("should create association");
make_free_for_did::<T, I>(&caller);
Pallet::<T, I>::add_association(caller.clone(), did.clone(), linkable_id.clone()).expect("should create association");

let origin = RawOrigin::Signed(caller);
}: _(origin)
verify {
assert!(ConnectedDids::<T>::get(&linkable_id).is_none());
assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_none());
assert!(ConnectedDids::<T, I>::get(&linkable_id).is_none());
assert!(ConnectedAccounts::<T, I>::get(did, linkable_id).is_none());
}

remove_account_association {
let caller: T::AccountId = account("caller", 0, SEED);
let linkable_id: LinkableAccountId = caller.clone().into();
let did: T::DidIdentifier = account("did", 0, SEED);
make_free_for_did::<T>(&caller);
make_free_for_did::<T, I>(&caller);

Pallet::<T>::add_association(caller.clone(), did.clone(), linkable_id.clone()).expect("should create association");
Pallet::<T, I>::add_association(caller.clone(), did.clone(), linkable_id.clone()).expect("should create association");

let origin = T::EnsureOrigin::generate_origin(caller, did.clone());
let id_arg = linkable_id.clone();
}: _<T::RuntimeOrigin>(origin, id_arg)
verify {
assert!(ConnectedDids::<T>::get(&linkable_id).is_none());
assert!(ConnectedAccounts::<T>::get(did, linkable_id).is_none());
assert!(ConnectedDids::<T, I>::get(&linkable_id).is_none());
assert!(ConnectedAccounts::<T, I>::get(did, linkable_id).is_none());
}

change_deposit_owner {
let deposit_owner_old: T::AccountId = account("caller", 0, SEED);
let deposit_owner_new: T::AccountId = account("caller", 1, SEED);
let linkable_id: LinkableAccountId = deposit_owner_old.clone().into();
let did: T::DidIdentifier = account("did", 0, SEED);
make_free_for_did::<T>(&deposit_owner_old);
make_free_for_did::<T>(&deposit_owner_new);
make_free_for_did::<T, I>(&deposit_owner_old);
make_free_for_did::<T, I>(&deposit_owner_new);

Pallet::<T>::add_association(deposit_owner_old, did.clone(), linkable_id.clone()).expect("should create association");
Pallet::<T, I>::add_association(deposit_owner_old, did.clone(), linkable_id.clone()).expect("should create association");

let origin = T::EnsureOrigin::generate_origin(deposit_owner_new.clone(), did);
let id_arg = linkable_id.clone();
}: _<T::RuntimeOrigin>(origin, id_arg)
verify {
assert_eq!(
ConnectedDids::<T>::get(&linkable_id).expect("should retain link").deposit,
ConnectedDids::<T, I>::get(&linkable_id).expect("should retain link").deposit,
Deposit {
owner: deposit_owner_new,
amount: <T as Config>::Deposit::get(),
amount: <T as Config<I>>::Deposit::get(),
},
);
}
Expand All @@ -279,9 +279,9 @@ benchmarks! {
let deposit_owner: T::AccountId = account("caller", 0, SEED);
let linkable_id: LinkableAccountId = deposit_owner.clone().into();
let did: T::DidIdentifier = account("did", 0, SEED);
make_free_for_did::<T>(&deposit_owner);
make_free_for_did::<T, I>(&deposit_owner);

Pallet::<T>::add_association(
Pallet::<T, I>::add_association(
deposit_owner.clone(),
did,
linkable_id.clone()
Expand All @@ -292,10 +292,10 @@ benchmarks! {
}: _(origin, id_arg)
verify {
assert_eq!(
ConnectedDids::<T>::get(&linkable_id).expect("should retain link").deposit,
ConnectedDids::<T, I>::get(&linkable_id).expect("should retain link").deposit,
Deposit {
owner: deposit_owner,
amount: <T as Config>::Deposit::get(),
amount: <T as Config<I>>::Deposit::get(),
},
);
}
Expand Down
Loading

0 comments on commit 6f7f587

Please sign in to comment.