From 410eae329e5e6897b8a983aa14a329861a558efa Mon Sep 17 00:00:00 2001 From: weichweich Date: Wed, 12 Jan 2022 15:45:40 +0100 Subject: [PATCH 01/60] wip: remove delegation, abstract ac --- Cargo.lock | 1 - pallets/attestation/Cargo.toml | 9 +- pallets/attestation/src/attestations.rs | 5 +- pallets/attestation/src/benchmarking.rs | 38 +- pallets/attestation/src/default_weights.rs | 28 +- pallets/attestation/src/lib.rs | 163 ++-- pallets/attestation/src/mock.rs | 89 +- pallets/attestation/src/tests.rs | 979 ++------------------- 8 files changed, 150 insertions(+), 1162 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c7620f6182..df56c1331b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -371,7 +371,6 @@ name = "attestation" version = "1.3.1" dependencies = [ "ctype", - "delegation", "frame-benchmarking", "frame-support", "frame-system", diff --git a/pallets/attestation/Cargo.toml b/pallets/attestation/Cargo.toml index 040cf60741..521b0fd202 100644 --- a/pallets/attestation/Cargo.toml +++ b/pallets/attestation/Cargo.toml @@ -14,7 +14,7 @@ substrate-wasm-builder-runner = {version = "3.0.0"} [dev-dependencies] ctype = {features = ["mock"], path = "../ctype"} -delegation = {features = ["mock"], path = "../delegation"} +kilt-support = {features = ["mock"], path = "../../support"} runtime-common = {default-features = false, path = "../../runtimes/common"} pallet-balances = {branch = "polkadot-v0.9.12", default-features = false, git = "https://github.com/paritytech/substrate"} @@ -26,9 +26,8 @@ sp-keystore = {branch = "polkadot-v0.9.12", default-features = false, git = "htt [dependencies] # Internal dependencies ctype = {default-features = false, path = "../ctype"} -delegation = {default-features = false, path = "../delegation"} -runtime-common = {default-features = false, optional = true, path = "../../runtimes/common"} kilt-support = {default-features = false, path = "../../support"} +runtime-common = {default-features = false, optional = true, path = "../../runtimes/common"} #External dependencies codec = {default-features = false, features = ["derive"], package = "parity-scale-codec", version = "2.3.1"} @@ -37,7 +36,7 @@ frame-support = {branch = "polkadot-v0.9.12", default-features = false, git = "h frame-system = {branch = "polkadot-v0.9.12", default-features = false, git = "https://github.com/paritytech/substrate"} log = {default-features = false, version = "0.4.14"} pallet-balances = {optional = true, branch = "polkadot-v0.9.12", default-features = false, git = "https://github.com/paritytech/substrate"} -scale-info = { version = "1.0", default-features = false, features = ["derive"] } +scale-info = {version = "1.0", default-features = false, features = ["derive"]} serde = {optional = true, version = "1.0.124"} sp-core = {branch = "polkadot-v0.9.12", default-features = false, git = "https://github.com/paritytech/substrate", optional = true} sp-io = {branch = "polkadot-v0.9.12", default-features = false, git = "https://github.com/paritytech/substrate", optional = true} @@ -56,7 +55,6 @@ mock = [ "sp-keystore", ] runtime-benchmarks = [ - "delegation/runtime-benchmarks", "frame-benchmarking", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", @@ -65,7 +63,6 @@ runtime-benchmarks = [ std = [ "codec/std", "ctype/std", - "delegation/std", "frame-support/std", "frame-system/std", "runtime-common/std", diff --git a/pallets/attestation/src/attestations.rs b/pallets/attestation/src/attestations.rs index f8133ec750..f9a5609c51 100644 --- a/pallets/attestation/src/attestations.rs +++ b/pallets/attestation/src/attestations.rs @@ -18,11 +18,10 @@ use codec::{Decode, Encode}; use ctype::CtypeHashOf; -use delegation::DelegationNodeIdOf; use kilt_support::deposit::Deposit; use scale_info::TypeInfo; -use crate::{AccountIdOf, AttesterOf, BalanceOf, Config}; +use crate::{AccountIdOf, AttesterOf, AuthorizationIdOf, BalanceOf, Config}; /// An on-chain attestation written by an attester. #[derive(Clone, Debug, Encode, Decode, PartialEq, TypeInfo)] @@ -34,7 +33,7 @@ pub struct AttestationDetails { pub attester: AttesterOf, /// \[OPTIONAL\] The ID of the delegation node used to authorize the /// attester. - pub delegation_id: Option>, + pub authorization_id: Option>, /// The flag indicating whether the attestation has been revoked or not. pub revoked: bool, /// The deposit that was taken to incentivise fair use of the on chain diff --git a/pallets/attestation/src/benchmarking.rs b/pallets/attestation/src/benchmarking.rs index 32f7c5e4b4..a372d8284d 100644 --- a/pallets/attestation/src/benchmarking.rs +++ b/pallets/attestation/src/benchmarking.rs @@ -19,12 +19,10 @@ use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite}; use frame_support::traits::{Currency, Get}; use frame_system::RawOrigin; -use sp_core::sr25519; use sp_runtime::traits::Hash; use sp_std::num::NonZeroU32; -use delegation::{benchmarking::setup_delegations, Config as DelegationConfig, Permissions}; -use kilt_support::{signature::VerifySignature, traits::GenerateBenchmarkOrigin}; +use kilt_support::traits::GenerateBenchmarkOrigin; use crate::*; @@ -35,33 +33,24 @@ benchmarks! { where_clause { where T: core::fmt::Debug, - T::DelegationNodeId: From, - T::CtypeCreatorId: From, - T::DelegationEntityId: From, - <::DelegationSignatureVerification as VerifySignature>::Signature: From<( - T::DelegationEntityId, - <::DelegationSignatureVerification as VerifySignature>::Payload, - )>, - ::EnsureOrigin: GenerateBenchmarkOrigin, - ::EnsureOrigin: GenerateBenchmarkOrigin, + ::EnsureOrigin: GenerateBenchmarkOrigin, } add { let sender: T::AccountId = account("sender", 0, SEED); let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); - let (_, _, delegate_public, delegation_id) = setup_delegations::(1, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::ATTEST)?; - let delegate_acc: T::DelegationEntityId = delegate_public.into(); + ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); let origin = ::EnsureOrigin::generate_origin(sender.clone(), delegate_acc.clone()); - }: _(origin, claim_hash, ctype_hash, Some(delegation_id)) + }: _(origin, claim_hash, ctype_hash, Some(attestation_id)) verify { assert!(Attestations::::contains_key(claim_hash)); assert_eq!(Pallet::::attestations(claim_hash), Some(AttestationDetails { ctype_hash, attester: delegate_acc, - delegation_id: Some(delegation_id), + attestation_id: Some(attestation_id), revoked: false, deposit: kilt_support::deposit::Deposit { owner: sender, @@ -77,14 +66,11 @@ benchmarks! { let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); - let (root_public, _, delegate_public, delegation_id) = setup_delegations::(d, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::ATTEST | Permissions::DELEGATE)?; - let root_acc: T::DelegationEntityId = root_public.into(); - let delegate_acc: T::DelegationEntityId = delegate_public.into(); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); // attest with leaf account let origin = ::EnsureOrigin::generate_origin(sender.clone(), delegate_acc.clone()); - Pallet::::add(origin, claim_hash, ctype_hash, Some(delegation_id))?; + Pallet::::add(origin, claim_hash, ctype_hash, Some(attestation_id))?; // revoke with root account, s.t. delegation tree needs to be traversed let origin = ::EnsureOrigin::generate_origin(sender.clone(), root_acc); }: _(origin, claim_hash, d) @@ -93,7 +79,7 @@ benchmarks! { assert_eq!(Attestations::::get(claim_hash), Some(AttestationDetails { ctype_hash, attester: delegate_acc, - delegation_id: Some(delegation_id), + attestation_id: Some(attestation_id), revoked: true, deposit: kilt_support::deposit::Deposit { owner: sender, @@ -109,14 +95,11 @@ benchmarks! { let ctype_hash: T::Hash = T::Hash::default(); let sender: T::AccountId = account("sender", 0, SEED); - let (root_public, _, delegate_public, delegation_id) = setup_delegations::(d, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::ATTEST | Permissions::DELEGATE)?; - let root_acc: T::DelegationEntityId = root_public.into(); - let delegate_acc: T::DelegationEntityId = delegate_public.into(); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); // attest with leaf account let origin = ::EnsureOrigin::generate_origin(sender.clone(), delegate_acc); - Pallet::::add(origin, claim_hash, ctype_hash, Some(delegation_id))?; + Pallet::::add(origin, claim_hash, ctype_hash, Some(attestation_id))?; // revoke with root account, s.t. delegation tree needs to be traversed let origin = ::EnsureOrigin::generate_origin(sender, root_acc); }: _(origin, claim_hash, d) @@ -129,14 +112,11 @@ benchmarks! { let ctype_hash: T::Hash = T::Hash::default(); let sender: T::AccountId = account("sender", 0, SEED); - let (root_public, _, delegate_public, delegation_id) = setup_delegations::(1, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::ATTEST | Permissions::DELEGATE)?; - let root_acc: T::DelegationEntityId = root_public.into(); - let delegate_acc: T::DelegationEntityId = delegate_public.into(); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); // attest with leaf account let origin = ::EnsureOrigin::generate_origin(sender.clone(), delegate_acc); - Pallet::::add(origin, claim_hash, ctype_hash, Some(delegation_id))?; + Pallet::::add(origin, claim_hash, ctype_hash, Some(attestation_id))?; // revoke with root account, s.t. delegation tree needs to be traversed let origin = RawOrigin::Signed(sender); }: _(origin, claim_hash) diff --git a/pallets/attestation/src/default_weights.rs b/pallets/attestation/src/default_weights.rs index a8bddd95bb..a4ec767c23 100644 --- a/pallets/attestation/src/default_weights.rs +++ b/pallets/attestation/src/default_weights.rs @@ -47,8 +47,8 @@ use sp_std::marker::PhantomData; /// Weight functions needed for attestation. pub trait WeightInfo { fn add() -> Weight; - fn revoke(d: u32, ) -> Weight; - fn remove(d: u32, ) -> Weight; + fn revoke() -> Weight; + fn remove() -> Weight; fn reclaim_deposit() -> Weight; } @@ -60,20 +60,20 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(6_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } - fn revoke(d: u32, ) -> Weight { + fn revoke() -> Weight { (37_029_000_u64) // Standard Error: 44_000 - .saturating_add((6_325_000_u64).saturating_mul(d as Weight)) + .saturating_add(6_325_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(d as Weight))) + .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - fn remove(d: u32, ) -> Weight { + fn remove() -> Weight { (64_058_000_u64) // Standard Error: 44_000 - .saturating_add((6_317_000_u64).saturating_mul(d as Weight)) + .saturating_add(6_317_000_u64) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(d as Weight))) + .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } fn reclaim_deposit() -> Weight { @@ -90,20 +90,20 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(6_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) } - fn revoke(d: u32, ) -> Weight { + fn revoke() -> Weight { (37_029_000_u64) // Standard Error: 44_000 - .saturating_add((6_325_000_u64).saturating_mul(d as Weight)) + .saturating_add(6_325_000_u64) .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(d as Weight))) + .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - fn remove(d: u32, ) -> Weight { + fn remove() -> Weight { (64_058_000_u64) // Standard Error: 44_000 - .saturating_add((6_317_000_u64).saturating_mul(d as Weight)) + .saturating_add(6_317_000_u64) .saturating_add(RocksDbWeight::get().reads(4_u64)) - .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(d as Weight))) + .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) } fn reclaim_deposit() -> Weight { diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 0fe886b478..3710c09145 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -90,21 +90,45 @@ pub use crate::{attestations::AttestationDetails, default_weights::WeightInfo, p #[frame_support::pallet] pub mod pallet { use super::*; - use ctype::CtypeHashOf; - use delegation::DelegationNodeIdOf; + use frame_support::dispatch::Weight; use frame_support::{ pallet_prelude::*, traits::{Currency, Get, ReservableCurrency}, BoundedVec, }; use frame_system::pallet_prelude::*; + use sp_runtime::traits::Zero; + use sp_runtime::DispatchError; + + use ctype::CtypeHashOf; use kilt_support::{deposit::Deposit, traits::CallSources}; + pub trait AttestationAccessControl { + fn can_attest(who: &T::AttesterId, authorization_id: &T::AuthorizationId) -> Result; + fn can_revoke(who: &T::AttesterId, attestation: &AttestationDetails) -> Result; + fn can_remove(who: &T::AttesterId, attestation: &AttestationDetails) -> Result; + } + + impl AttestationAccessControl for () { + fn can_attest(_who: &T::AttesterId, _authorization_id: &T::AuthorizationId) -> Result { + Ok(Weight::zero()) + } + fn can_revoke(_who: &T::AttesterId, _attestation: &AttestationDetails) -> Result { + Ok(Weight::zero()) + } + fn can_remove(_who: &T::AttesterId, _attestation: &AttestationDetails) -> Result { + Ok(Weight::zero()) + } + } + /// Type of a claim hash. pub(crate) type ClaimHashOf = ::Hash; /// Type of an attester identifier. - pub(crate) type AttesterOf = delegation::DelegatorIdOf; + pub(crate) type AttesterOf = ::AttesterId; + + /// Authorization id type + pub(crate) type AuthorizationIdOf = ::AuthorizationId; pub(crate) type AccountIdOf = ::AccountId; @@ -113,7 +137,7 @@ pub mod pallet { pub(crate) type CurrencyOf = ::Currency; #[pallet::config] - pub trait Config: frame_system::Config + ctype::Config + delegation::Config { + pub trait Config: frame_system::Config + ctype::Config { type EnsureOrigin: EnsureOrigin< Success = ::OriginSuccess, ::Origin, @@ -133,6 +157,12 @@ pub mod pallet { /// the same delegation. #[pallet::constant] type MaxDelegatedAttestations: Get; + + type AttesterId: Parameter; + + type AuthorizationId: Parameter; + + type AccessControl: AttestationAccessControl; } #[pallet::pallet] @@ -149,6 +179,7 @@ pub mod pallet { #[pallet::getter(fn attestations)] pub type Attestations = StorageMap<_, Blake2_128Concat, ClaimHashOf, AttestationDetails>; + // TODO: Migrate... Either delete or rename /// Delegated attestations stored on chain. /// /// It maps from a delegation ID to a vector of claim hashes. @@ -157,7 +188,7 @@ pub mod pallet { pub type DelegatedAttestations = StorageMap< _, Blake2_128Concat, - DelegationNodeIdOf, + AuthorizationIdOf, BoundedVec, ::MaxDelegatedAttestations>, >; @@ -170,7 +201,7 @@ pub mod pallet { AttesterOf, ClaimHashOf, CtypeHashOf, - Option>, + Option>, ), /// An attestation has been revoked. /// \[account id, claim hash\] @@ -195,15 +226,6 @@ pub mod pallet { /// The attestation CType does not match the CType specified in the /// delegation hierarchy root. CTypeMismatch, - /// The delegation node does not include the permission to create new - /// attestations. Only when the revoker is not the original attester. - DelegationUnauthorizedToAttest, - /// The delegation node has already been revoked. - /// Only when the revoker is not the original attester. - DelegationRevoked, - /// The delegation node owner is different than the attester. - /// Only when the revoker is not the original attester. - NotDelegatedToAttester, /// The call origin is not authorized to change the attestation. Unauthorized, /// The maximum number of delegated attestations has already been @@ -240,7 +262,7 @@ pub mod pallet { origin: OriginFor, claim_hash: ClaimHashOf, ctype_hash: CtypeHashOf, - delegation_id: Option>, + authorization_id: Option>, ) -> DispatchResult { let source = ::EnsureOrigin::ensure_origin(origin)?; let payer = source.sender(); @@ -257,34 +279,9 @@ pub mod pallet { ); // Check for validity of the delegation node if specified. - let delegation_record = if let Some(delegation_id) = delegation_id { - let delegation = >::get(delegation_id) - .ok_or(delegation::Error::::DelegationNotFound)?; - - ensure!(!delegation.details.revoked, Error::::DelegationRevoked); - - ensure!(delegation.details.owner == who, Error::::NotDelegatedToAttester); - - ensure!( - (delegation.details.permissions & delegation::Permissions::ATTEST) - == delegation::Permissions::ATTEST, - Error::::DelegationUnauthorizedToAttest - ); - - // Check if the CType of the delegation is matching the CType of the attestation - let root = >::get(delegation.hierarchy_root_id) - .ok_or(delegation::Error::::HierarchyNotFound)?; - ensure!(root.ctype_hash == ctype_hash, Error::::CTypeMismatch); - - // If the attestation is based on a delegation, store separately - let mut delegated_attestations = >::get(delegation_id).unwrap_or_default(); - delegated_attestations - .try_push(claim_hash) - .map_err(|_| Error::::MaxDelegatedAttestationsExceeded)?; - Some((delegation_id, delegated_attestations)) - } else { - None - }; + if let Some(ref authorization_id) = authorization_id { + T::AccessControl::can_attest(&who, authorization_id)?; + } let deposit = Pallet::::reserve_deposit(payer, deposit_amount)?; @@ -292,23 +289,18 @@ pub mod pallet { log::debug!("insert Attestation"); - // write delegation record, if any - if let Some((id, delegated_attestation)) = delegation_record { - >::insert(id, delegated_attestation); - } - - >::insert( + Attestations::::insert( &claim_hash, AttestationDetails { ctype_hash, attester: who.clone(), - delegation_id, + authorization_id: authorization_id.clone(), revoked: false, deposit, }, ); - Self::deposit_event(Event::AttestationCreated(who, claim_hash, ctype_hash, delegation_id)); + Self::deposit_event(Event::AttestationCreated(who, claim_hash, ctype_hash, authorization_id)); Ok(()) } @@ -329,12 +321,8 @@ pub mod pallet { /// - Reads per delegation step P: delegation::Delegations /// - Writes: Attestations, DelegatedAttestations /// # - #[pallet::weight(::WeightInfo::revoke(*max_parent_checks))] - pub fn revoke( - origin: OriginFor, - claim_hash: ClaimHashOf, - max_parent_checks: u32, - ) -> DispatchResultWithPostInfo { + #[pallet::weight(::WeightInfo::revoke())] + pub fn revoke(origin: OriginFor, claim_hash: ClaimHashOf) -> DispatchResultWithPostInfo { let source = ::EnsureOrigin::ensure_origin(origin)?; let who = source.subject(); @@ -342,16 +330,14 @@ pub mod pallet { ensure!(!attestation.revoked, Error::::AlreadyRevoked); - let delegation_depth = if attestation.attester != who { - Self::verify_delegated_access(&who, &attestation, max_parent_checks)? - } else { - 0 - }; + if attestation.attester != who { + T::AccessControl::can_revoke(&who, &attestation)?; + } // *** No Fail beyond this point *** log::debug!("revoking Attestation"); - >::insert( + Attestations::::insert( &claim_hash, AttestationDetails { revoked: true, @@ -361,7 +347,7 @@ pub mod pallet { Self::deposit_event(Event::AttestationRevoked(who, claim_hash)); - Ok(Some(::WeightInfo::revoke(delegation_depth)).into()) + Ok(Some(::WeightInfo::revoke()).into()) } /// Remove an attestation. @@ -381,22 +367,16 @@ pub mod pallet { /// - Reads per delegation step P: delegation::Delegations /// - Writes: Attestations, DelegatedAttestations /// # - #[pallet::weight(::WeightInfo::remove(*max_parent_checks))] - pub fn remove( - origin: OriginFor, - claim_hash: ClaimHashOf, - max_parent_checks: u32, - ) -> DispatchResultWithPostInfo { + #[pallet::weight(::WeightInfo::remove())] + pub fn remove(origin: OriginFor, claim_hash: ClaimHashOf) -> DispatchResultWithPostInfo { let source = ::EnsureOrigin::ensure_origin(origin)?; let who = source.subject(); let attestation = Attestations::::get(&claim_hash).ok_or(Error::::AttestationNotFound)?; - let delegation_depth = if attestation.attester != who { - Self::verify_delegated_access(&who, &attestation, max_parent_checks)? - } else { - 0 - }; + if attestation.attester != who { + T::AccessControl::can_remove(&who, &attestation)?; + } // *** No Fail beyond this point *** @@ -405,7 +385,7 @@ pub mod pallet { Self::remove_attestation(attestation, claim_hash); Self::deposit_event(Event::AttestationRemoved(who, claim_hash)); - Ok(Some(::WeightInfo::remove(delegation_depth)).into()) + Ok(Some(::WeightInfo::remove()).into()) } /// Reclaim a storage deposit by removing an attestation @@ -436,28 +416,6 @@ pub mod pallet { } impl Pallet { - /// Check the delegation tree if the attester is authorized to access - /// the attestation. - fn verify_delegated_access( - attester: &AttesterOf, - attestation: &AttestationDetails, - max_parent_checks: u32, - ) -> Result { - // if there is no delegation id, access to this attestation wasn't delegated to - // anyone. - let delegation_id = attestation.delegation_id.ok_or(Error::::Unauthorized)?; - ensure!( - max_parent_checks <= T::MaxParentChecks::get(), - delegation::Error::::MaxParentChecksTooLarge - ); - // Check whether the sender of the revocation controls the delegation node and - // that the delegation has not been revoked - let (is_delegating, delegation_depth) = - >::is_delegating(attester, &delegation_id, max_parent_checks)?; - ensure!(is_delegating, Error::::Unauthorized); - Ok(delegation_depth) - } - /// Reserve the deposit and record the deposit on chain. /// /// Fails if the `payer` has a balance less than deposit. @@ -476,13 +434,6 @@ pub mod pallet { fn remove_attestation(attestation: AttestationDetails, claim_hash: ClaimHashOf) { kilt_support::free_deposit::, CurrencyOf>(&attestation.deposit); Attestations::::remove(&claim_hash); - if let Some(delegation_id) = attestation.delegation_id { - DelegatedAttestations::::mutate(&delegation_id, |maybe_attestations| { - if let Some(attestations) = maybe_attestations.as_mut() { - attestations.retain(|&elem| elem != claim_hash); - } - }); - } } } } diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index e7c37b427f..a531b79698 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -24,12 +24,11 @@ //! tests. use ctype::CtypeHashOf; -use delegation::DelegationNodeIdOf; use frame_support::traits::Get; use kilt_support::deposit::Deposit; use sp_core::H256; -use crate::{AccountIdOf, AttestationDetails, AttesterOf, BalanceOf, ClaimHashOf, Config}; +use crate::{pallet::AuthorizationIdOf, AccountIdOf, AttestationDetails, AttesterOf, BalanceOf, ClaimHashOf, Config}; #[cfg(test)] pub use crate::mock::runtime::*; @@ -37,7 +36,7 @@ pub use crate::mock::runtime::*; pub struct AttestationCreationDetails { pub claim_hash: ClaimHashOf, pub ctype_hash: CtypeHashOf, - pub delegation_id: Option>, + pub authorization_id: Option>, } pub fn generate_base_attestation_creation_details( @@ -47,21 +46,7 @@ pub fn generate_base_attestation_creation_details( AttestationCreationDetails { claim_hash, ctype_hash: attestation.ctype_hash, - delegation_id: attestation.delegation_id, - } -} - -pub struct AttestationRevocationDetails { - pub claim_hash: ClaimHashOf, - pub max_parent_checks: u32, -} - -pub fn generate_base_attestation_revocation_details( - claim_hash: ClaimHashOf, -) -> AttestationRevocationDetails { - AttestationRevocationDetails { - claim_hash, - max_parent_checks: 0u32, + authorization_id: attestation.authorization_id, } } @@ -72,7 +57,7 @@ where { AttestationDetails { attester, - delegation_id: None, + authorization_id: None, ctype_hash: ctype::mock::get_ctype_hash::(true), revoked: false, deposit: Deposit::, BalanceOf> { @@ -96,12 +81,8 @@ pub(crate) mod runtime { }; use std::sync::Arc; - use delegation::{mock::DelegationHierarchyInitialization, DelegationNode}; - use kilt_support::{ - mock::{mock_origin, SubjectId}, - signature::EqualVerify, - }; - use runtime_common::constants::{attestation::ATTESTATION_DEPOSIT, delegation::DELEGATION_DEPOSIT, MILLI_KILT}; + use kilt_support::mock::{mock_origin, SubjectId}; + use runtime_common::constants::{attestation::ATTESTATION_DEPOSIT, MILLI_KILT}; use super::*; use crate::Pallet; @@ -109,10 +90,7 @@ pub(crate) mod runtime { type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; - type TestCtypeOwner = SubjectId; type TestCtypeHash = runtime_common::Hash; - type TestDelegationNodeId = runtime_common::Hash; - type TestDelegatorId = SubjectId; type TestClaimHash = runtime_common::Hash; type TestBalance = runtime_common::Balance; @@ -125,7 +103,6 @@ pub(crate) mod runtime { System: frame_system::{Pallet, Call, Config, Storage, Event}, Attestation: crate::{Pallet, Call, Storage, Event}, Ctype: ctype::{Pallet, Call, Storage, Event}, - Delegation: delegation::{Pallet, Call, Storage, Event}, Balances: pallet_balances::{Pallet, Call, Storage, Event}, MockOrigin: mock_origin::{Pallet, Origin}, } @@ -186,7 +163,7 @@ pub(crate) mod runtime { } impl ctype::Config for Test { - type CtypeCreatorId = TestCtypeOwner; + type CtypeCreatorId = SubjectId; type EnsureOrigin = mock_origin::EnsureDoubleOrigin; type OriginSuccess = mock_origin::DoubleOrigin; type Event = (); @@ -197,35 +174,6 @@ pub(crate) mod runtime { type FeeCollector = (); } - parameter_types! { - pub const MaxSignatureByteLength: u16 = 64; - pub const MaxParentChecks: u32 = 5; - pub const MaxRevocations: u32 = 5; - pub const MaxRemovals: u32 = 5; - #[derive(Clone)] - pub const MaxChildren: u32 = 1000; - pub const DelegationDeposit: TestBalance = DELEGATION_DEPOSIT; - } - - impl delegation::Config for Test { - type Signature = (Self::DelegationEntityId, Vec); - type DelegationSignatureVerification = EqualVerify>; - type DelegationEntityId = TestDelegatorId; - type DelegationNodeId = TestDelegationNodeId; - type EnsureOrigin = mock_origin::EnsureDoubleOrigin; - type OriginSuccess = mock_origin::DoubleOrigin; - type Event = (); - type MaxSignatureByteLength = MaxSignatureByteLength; - type MaxParentChecks = MaxParentChecks; - type MaxRevocations = MaxRevocations; - type MaxRemovals = MaxRemovals; - type MaxChildren = MaxChildren; - type WeightInfo = (); - - type Currency = Balances; - type Deposit = DelegationDeposit; - } - impl mock_origin::Config for Test { type Origin = Origin; type AccountId = runtime_common::AccountId; @@ -246,6 +194,9 @@ pub(crate) mod runtime { type Currency = Balances; type Deposit = Deposit; type MaxDelegatedAttestations = MaxDelegatedAttestations; + type AttesterId = SubjectId; + type AuthorizationId = (); + type AccessControl = (); } pub(crate) const ACCOUNT_00: runtime_common::AccountId = runtime_common::AccountId::new([1u8; 32]); @@ -279,9 +230,6 @@ pub(crate) mod runtime { #[derive(Clone, Default)] pub struct ExtBuilder { - delegation_hierarchies: DelegationHierarchyInitialization, - delegations: Vec<(TestDelegationNodeId, DelegationNode)>, - /// initial ctypes & owners ctypes: Vec<(TestCtypeHash, CtypeCreatorOf)>, /// endowed accounts with balances @@ -290,19 +238,6 @@ pub(crate) mod runtime { } impl ExtBuilder { - pub fn with_delegation_hierarchies( - mut self, - delegation_hierarchies: DelegationHierarchyInitialization, - ) -> Self { - self.delegation_hierarchies = delegation_hierarchies; - self - } - - pub fn with_delegations(mut self, delegations: Vec<(TestDelegationNodeId, DelegationNode)>) -> Self { - self.delegations = delegations; - self - } - pub fn with_ctypes(mut self, ctypes: Vec<(TestCtypeHash, CtypeCreatorOf)>) -> Self { self.ctypes = ctypes; self @@ -333,14 +268,12 @@ pub(crate) mod runtime { ctype::Ctypes::::insert(ctype.0, ctype.1.clone()); } - delegation::mock::initialize_pallet(self.delegations, self.delegation_hierarchies); - for (claim_hash, details) in self.attestations { Pallet::::reserve_deposit(details.deposit.owner.clone(), details.deposit.amount) .expect("Should have balance"); crate::Attestations::::insert(&claim_hash, details.clone()); - if let Some(delegation_id) = details.delegation_id.as_ref() { + if let Some(delegation_id) = details.authorization_id.as_ref() { crate::DelegatedAttestations::::try_mutate(delegation_id, |attestations| { let attestations = attestations.get_or_insert_with(Default::default); attestations.try_push(claim_hash) diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 606fce99e8..29e725a042 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -19,21 +19,19 @@ use frame_support::{assert_noop, assert_ok}; use sp_runtime::traits::Zero; -use ctype::mock as ctype_mock; -use delegation::mock::{self as delegation_mock, DELEGATION_ID_SEED_1, DELEGATION_ID_SEED_2}; use kilt_support::mock::mock_origin::DoubleOrigin; use crate::{ self as attestation, mock::{runtime::Balances, *}, - AttesterOf, Config, DelegatedAttestations, + AttesterOf, Config, }; // ############################################################################# -// submit_attestation_creation_operation +// add #[test] -fn attest_no_delegation_successful() { +fn test_attest_successful() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = get_claim_hash(true); let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); @@ -47,75 +45,27 @@ fn attest_no_delegation_successful() { .execute_with(|| { assert_ok!(Attestation::add( DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, + claim_hash, operation.ctype_hash, - operation.delegation_id + operation.authorization_id )); let stored_attestation = Attestation::attestations(&claim_hash).expect("Attestation should be present on chain."); assert_eq!(stored_attestation.ctype_hash, operation.ctype_hash); assert_eq!(stored_attestation.attester, attester); - assert_eq!(stored_attestation.delegation_id, operation.delegation_id); + assert_eq!(stored_attestation.authorization_id, operation.authorization_id); assert!(!stored_attestation.revoked); }); } #[test] -fn attest_with_delegation_successful() { - let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); - - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details(); - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attester.clone(), - Some(hierarchy_root_id), - ACCOUNT_00, - ); - delegation_node.details.permissions = delegation::Permissions::ATTEST; - let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let operation = generate_base_attestation_creation_details(claim_hash, attestation); - - ExtBuilder::default() - .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - attester.clone(), - ACCOUNT_00, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .build() - .execute_with(|| { - assert_ok!(Attestation::add( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.ctype_hash, - operation.delegation_id - )); - let stored_attestation = - Attestation::attestations(&claim_hash).expect("Attestation should be present on chain."); - - assert_eq!(stored_attestation.ctype_hash, operation.ctype_hash); - assert_eq!(stored_attestation.attester, attester); - assert_eq!(stored_attestation.delegation_id, operation.delegation_id); - assert!(!stored_attestation.revoked); - - let delegated_attestations = Attestation::delegated_attestations(&delegation_id) - .expect("Attested delegation should be present on chain."); - - assert_eq!(delegated_attestations, vec![claim_hash]); - }); +fn test_attest_with_authorization_successful() { + todo!() } #[test] -fn ctype_not_present_attest_error() { +fn test_attest_ctype_not_found() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = get_claim_hash(true); let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); @@ -129,9 +79,9 @@ fn ctype_not_present_attest_error() { assert_noop!( Attestation::add( DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, + claim_hash, operation.ctype_hash, - operation.delegation_id + operation.authorization_id ), ctype::Error::::CTypeNotFound ); @@ -139,7 +89,7 @@ fn ctype_not_present_attest_error() { } #[test] -fn duplicate_attest_error() { +fn test_attest_already_exists() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = get_claim_hash(true); @@ -155,9 +105,9 @@ fn duplicate_attest_error() { assert_noop!( Attestation::add( DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, + claim_hash, operation.ctype_hash, - operation.delegation_id + operation.authorization_id ), attestation::Error::::AlreadyAttested ); @@ -165,276 +115,28 @@ fn duplicate_attest_error() { } #[test] -fn delegation_not_found_attest_error() { - let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let operation = generate_base_attestation_creation_details(claim_hash, attestation); - - ExtBuilder::default() - .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) - .build() - .execute_with(|| { - assert_noop!( - Attestation::add( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.ctype_hash, - operation.delegation_id - ), - delegation::Error::::DelegationNotFound - ); - }); -} - -#[test] -fn delegation_revoked_attest_error() { - let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - // Delegation node does not have permissions to attest. - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attester.clone(), - Some(hierarchy_root_id), - ACCOUNT_00, - ); - delegation_node.details.permissions = delegation::Permissions::ATTEST; - delegation_node.details.revoked = true; - let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let operation = generate_base_attestation_creation_details(claim_hash, attestation); - - ExtBuilder::default() - .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - attester.clone(), - ACCOUNT_00, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .build() - .execute_with(|| { - assert_noop!( - Attestation::add( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.ctype_hash, - operation.delegation_id - ), - attestation::Error::::DelegationRevoked - ); - }); -} - -#[test] -fn not_delegation_owner_attest_error() { - let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let alternative_owner = sr25519_did_from_seed(&BOB_SEED); - - let claim_hash = get_claim_hash(true); - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut delegation_node = delegation_mock::generate_base_delegation_node::( - hierarchy_root_id, - alternative_owner, - Some(hierarchy_root_id), - ACCOUNT_00, - ); - delegation_node.details.permissions = delegation::Permissions::ATTEST; - let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let operation = generate_base_attestation_creation_details(claim_hash, attestation); - - ExtBuilder::default() - .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - attester.clone(), - ACCOUNT_00, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .build() - .execute_with(|| { - assert_noop!( - Attestation::add( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.ctype_hash, - operation.delegation_id - ), - attestation::Error::::NotDelegatedToAttester - ); - }); -} - -#[test] -fn unauthorised_permissions_attest_error() { - let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - // Delegation node does not have permissions to attest. - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attester.clone(), - Some(hierarchy_root_id), - ACCOUNT_00, - ); - let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let operation = generate_base_attestation_creation_details(claim_hash, attestation); - - ExtBuilder::default() - .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - attester.clone(), - ACCOUNT_00, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .build() - .execute_with(|| { - assert_noop!( - Attestation::add( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.ctype_hash, - operation.delegation_id - ), - attestation::Error::::DelegationUnauthorizedToAttest - ); - }); -} - -#[test] -fn root_not_present_attest_error() { - let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - let alternative_hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(false); - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attester.clone(), - Some(alternative_hierarchy_root_id), - ACCOUNT_00, - ); - delegation_node.details.permissions = delegation::Permissions::ATTEST; - let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let operation = generate_base_attestation_creation_details(claim_hash, attestation); - - ExtBuilder::default() - .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) - .with_delegation_hierarchies(vec![( - alternative_hierarchy_root_id, - hierarchy_details, - attester.clone(), - ACCOUNT_00, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .build() - .execute_with(|| { - assert_noop!( - Attestation::add( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.ctype_hash, - operation.delegation_id - ), - delegation::Error::::HierarchyNotFound - ); - }); -} - -#[test] -fn root_ctype_mismatch_attest_error() { - let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); - let alternative_ctype_hash = ctype_mock::get_ctype_hash::(false); - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let mut hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - hierarchy_details.ctype_hash = alternative_ctype_hash; - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attester.clone(), - Some(hierarchy_root_id), - ACCOUNT_00, - ); - delegation_node.details.permissions = delegation::Permissions::ATTEST; - let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let operation = generate_base_attestation_creation_details(claim_hash, attestation); - - ExtBuilder::default() - .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - attester.clone(), - ACCOUNT_00, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .build() - .execute_with(|| { - assert_noop!( - Attestation::add( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.ctype_hash, - operation.delegation_id - ), - attestation::Error::::CTypeMismatch - ); - }); +fn test_attest_unauthorized() { + todo!() } // ############################################################################# -// submit_attestation_revocation_operation +// revoke #[test] -fn revoke_and_remove_direct_successful() { +fn test_revoke() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = get_claim_hash(true); let attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); - let operation = generate_base_attestation_revocation_details::(claim_hash); - ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) - .with_attestations(vec![(operation.claim_hash, attestation)]) + .with_attestations(vec![(claim_hash, attestation)]) .build() .execute_with(|| { assert_ok!(Attestation::revoke( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks + claim_hash, )); let stored_attestation = Attestation::attestations(claim_hash).expect("Attestation should be present on chain."); @@ -444,8 +146,7 @@ fn revoke_and_remove_direct_successful() { assert_ok!(Attestation::remove( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks + claim_hash, )); assert!(Attestation::attestations(claim_hash).is_none()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); @@ -453,267 +154,36 @@ fn revoke_and_remove_direct_successful() { } #[test] -fn revoke_with_delegation_successful() { - let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - revoker.clone(), - Some(hierarchy_root_id), - ACCOUNT_00, - ); - delegation_node.details.permissions = delegation::Permissions::ATTEST; - // Attestation owned by a different user, but delegation owned by the user - // submitting the operation. - let mut attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let mut operation = generate_base_attestation_revocation_details::(claim_hash); - // Set to 0 as we only need to check the delegation node itself and no parent. - operation.max_parent_checks = 0u32; - - ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01, ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - revoker.clone(), - ACCOUNT_01, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_ok!(Attestation::revoke( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - )); - let stored_attestation = - Attestation::attestations(operation.claim_hash).expect("Attestation should be present on chain."); - - assert!(stored_attestation.revoked); - }); +fn test_authorized_revoke() { + todo!() } #[test] -fn revoke_with_parent_delegation_successful() { - let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - let parent_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut parent_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - revoker.clone(), - Some(hierarchy_root_id), - ACCOUNT_00, - ); - parent_node.details.permissions = delegation::Permissions::ATTEST; - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_2); - let delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attestation_owner.clone(), - Some(parent_id), - ACCOUNT_00, - ); - let mut attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let mut operation = generate_base_attestation_revocation_details::(claim_hash); - // Set to 1 as the delegation referenced in the attestation is the child of the - // node we want to use - operation.max_parent_checks = 1u32; - - ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01, ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - revoker.clone(), - ACCOUNT_00, - )]) - .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_ok!(Attestation::revoke( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - )); - let stored_attestation = - Attestation::attestations(claim_hash).expect("Attestation should be present on chain."); - - assert!(stored_attestation.revoked); - }); +fn test_unauthorized_revoke() { + todo!() } #[test] -fn revoke_parent_delegation_no_attestation_permissions_successful() { - let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - let parent_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut parent_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - revoker.clone(), - Some(hierarchy_root_id), - ACCOUNT_00, - ); - parent_node.details.permissions = delegation::Permissions::DELEGATE; - - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_2); - let delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attestation_owner.clone(), - Some(parent_id), - ACCOUNT_00, - ); - - let mut attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let mut operation = generate_base_attestation_revocation_details::(claim_hash); - // Set to 1 as the delegation referenced in the attestation is the child of the - // node we want to use - operation.max_parent_checks = 1u32; - - ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01, ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - revoker.clone(), - ACCOUNT_00, - )]) - .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_ok!(Attestation::revoke( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - )); - let stored_attestation = - Attestation::attestations(claim_hash).expect("Attestation should be present on chain."); - - assert!(stored_attestation.revoked); - }); -} - -#[test] -fn revoke_parent_delegation_with_direct_delegation_revoked_successful() { - let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - let parent_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut parent_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - revoker.clone(), - Some(hierarchy_root_id), - ACCOUNT_00, - ); - parent_node.details.permissions = delegation::Permissions::ATTEST; - - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_2); - let mut delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attestation_owner.clone(), - Some(parent_id), - ACCOUNT_00, - ); - - delegation_node.details.revoked = true; - let mut attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let mut operation = generate_base_attestation_revocation_details::(claim_hash); - // Set to 1 as the delegation referenced in the attestation is the child of the - // node we want to use - operation.max_parent_checks = 1u32; - - ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01, ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - revoker.clone(), - ACCOUNT_01, - )]) - .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_ok!(Attestation::revoke( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - )); - let stored_attestation = - Attestation::attestations(claim_hash).expect("Attestation should be present on chain."); - - assert!(stored_attestation.revoked); - }); -} - -#[test] -fn attestation_not_present_revoke_error() { +fn test_revoke_not_found() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = get_claim_hash(true); let attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); - let operation = generate_base_attestation_revocation_details::(claim_hash); - ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) .build() .execute_with(|| { assert_noop!( - Attestation::revoke( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - ), + Attestation::revoke(DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash,), attestation::Error::::AttestationNotFound ); }); } #[test] -fn already_revoked_revoke_error() { +fn test_already_revoked() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = get_claim_hash(true); @@ -721,276 +191,59 @@ fn already_revoked_revoke_error() { let mut attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); attestation.revoked = true; - let operation = generate_base_attestation_revocation_details::(claim_hash); - ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) - .with_attestations(vec![(operation.claim_hash, attestation)]) + .with_attestations(vec![(claim_hash, attestation)]) .build() .execute_with(|| { assert_noop!( - Attestation::revoke( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - ), + Attestation::revoke(DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash,), attestation::Error::::AlreadyRevoked ); }); } -#[test] -fn unauthorised_attestation_revoke_error() { - let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - - // Attestation owned by a different user - let attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - - let operation = generate_base_attestation_revocation_details::(claim_hash); - - ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01, ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_noop!( - Attestation::revoke( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - ), - attestation::Error::::Unauthorized - ); - }); -} - -#[test] -fn max_parent_lookups_revoke_error() { - let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - let parent_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let parent_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - revoker.clone(), - Some(hierarchy_root_id), - ACCOUNT_00, - ); - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_2); - let mut delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attestation_owner.clone(), - Some(parent_id), - ACCOUNT_00, - ); - - delegation_node.details.permissions = delegation::Permissions::ATTEST; - let mut attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let mut operation = generate_base_attestation_revocation_details::(claim_hash); - operation.max_parent_checks = 0u32; - - ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01, ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - revoker.clone(), - ACCOUNT_00, - )]) - .with_delegations(vec![(parent_id, parent_node), (delegation_id, delegation_node)]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_noop!( - Attestation::revoke( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - ), - delegation::Error::::MaxSearchDepthReached - ); - }); -} - -#[test] -fn revoked_delegation_revoke_error() { - let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - revoker.clone(), - Some(hierarchy_root_id), - ACCOUNT_00, - ); - delegation_node.details.permissions = delegation::Permissions::ATTEST; - delegation_node.details.revoked = true; - let mut attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let operation = generate_base_attestation_revocation_details::(claim_hash); - - ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01, ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - revoker.clone(), - ACCOUNT_00, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_noop!( - Attestation::revoke( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - ), - attestation::Error::::Unauthorized - ); - }); -} - // ############################################################################# // remove attestation #[test] -fn subject_remove_direct_successful() { +fn test_remove() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = get_claim_hash(true); let attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); - let operation = generate_base_attestation_revocation_details::(claim_hash); - ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) - .with_attestations(vec![(operation.claim_hash, attestation)]) + .with_attestations(vec![(claim_hash, attestation)]) .build() .execute_with(|| { assert_ok!(Attestation::remove( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), - operation.claim_hash, - operation.max_parent_checks + claim_hash, )); assert!(Attestation::attestations(claim_hash).is_none()) }); } #[test] -fn reclaim_deposit() { - let deposit_owner: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attester: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - let attestation = generate_base_attestation::(attester, ACCOUNT_00); - - let operation = generate_base_attestation_revocation_details::(claim_hash); - - ExtBuilder::default() - .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(attestation.ctype_hash, deposit_owner)]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_noop!( - Attestation::reclaim_deposit(Origin::signed(ACCOUNT_01), operation.claim_hash), - attestation::Error::::Unauthorized, - ); - assert_ok!(Attestation::reclaim_deposit( - Origin::signed(ACCOUNT_00), - operation.claim_hash, - )); - assert!(Attestation::attestations(claim_hash).is_none()) - }); +fn test_authorized_remove() { + todo!() } #[test] -fn remove_with_delegation_successful() { - let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details::(); - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attester.clone(), - Some(hierarchy_root_id), - ACCOUNT_01, - ); - delegation_node.details.permissions = delegation::Permissions::ATTEST; - // Attestation owned by a different user, but delegation owned by the user - // submitting the operation. - let mut attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let mut operation = generate_base_attestation_revocation_details::(claim_hash); - // Set to 0 as we only need to check the delegation node itself and no parent. - operation.max_parent_checks = 0u32; - - ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01, ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - attester.clone(), - ACCOUNT_01, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); - assert_ok!(Attestation::remove( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - )); - assert!(Attestation::attestations(operation.claim_hash).is_none()); - assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); - }); +fn test_unauthorised_remove() { + todo!() } #[test] -fn attestation_not_present_remove_error() { +fn test_remove_not_found() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = get_claim_hash(true); let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - let operation = generate_base_attestation_revocation_details::(claim_hash); - ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) @@ -999,163 +252,39 @@ fn attestation_not_present_remove_error() { assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); assert_noop!( - Attestation::remove( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - ), + Attestation::remove(DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash,), attestation::Error::::AttestationNotFound ); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); }); } -#[test] -fn unauthorised_attestation_remove_error() { - let remover: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - - // Attestation owned by a different user - let attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - - let operation = generate_base_attestation_revocation_details::(claim_hash); - - ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01.clone(), ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, remover.clone())]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); - assert_noop!( - Attestation::remove( - DoubleOrigin(ACCOUNT_00, remover.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - ), - attestation::Error::::Unauthorized - ); - assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); - }); -} +// ############################################################################# +// reclaim deposit #[test] -fn revoked_delegation_remove_error() { - let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); +fn test_reclaim_deposit() { + let deposit_owner: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); + let attester: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = get_claim_hash(true); - - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details(); - - let delegation_id = delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let mut delegation_node = delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attester.clone(), - Some(hierarchy_root_id), - ACCOUNT_01, - ); - - delegation_node.details.permissions = delegation::Permissions::ATTEST; - delegation_node.details.revoked = true; - let mut attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let operation = generate_base_attestation_revocation_details::(claim_hash); + let attestation = generate_base_attestation::(attester, ACCOUNT_00); ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01.clone(), ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - attester.clone(), - ACCOUNT_01, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .with_attestations(vec![(operation.claim_hash, attestation)]) + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, deposit_owner)]) + .with_attestations(vec![(claim_hash, attestation)]) .build() .execute_with(|| { - assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); assert_noop!( - Attestation::remove( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - ), - attestation::Error::::Unauthorized + Attestation::reclaim_deposit(Origin::signed(ACCOUNT_01), claim_hash), + attestation::Error::::Unauthorized, ); - assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); + assert_ok!(Attestation::reclaim_deposit(Origin::signed(ACCOUNT_00), claim_hash,)); + assert!(Attestation::attestations(claim_hash).is_none()) }); } #[test] -fn remove_delegated_attestation() { - let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let attestation_owner: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); - - let hierarchy_root_id = delegation_mock::get_delegation_hierarchy_id::(true); - let hierarchy_details = delegation_mock::generate_base_delegation_hierarchy_details(); - let (delegation_id, mut delegation_node) = ( - delegation_mock::delegation_id_from_seed::(DELEGATION_ID_SEED_1), - delegation_mock::generate_base_delegation_node( - hierarchy_root_id, - attester.clone(), - Some(hierarchy_root_id), - ACCOUNT_01, - ), - ); - delegation_node.details.permissions = delegation::Permissions::ATTEST; - let mut attestation = generate_base_attestation::(attestation_owner, ACCOUNT_00); - attestation.delegation_id = Some(delegation_id); - - let operation = generate_base_attestation_revocation_details::(claim_hash); - - ExtBuilder::default() - .with_balances(vec![ - (ACCOUNT_00, ::Deposit::get() * 100), - (ACCOUNT_01, ::Deposit::get() * 100), - ]) - .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - attester.clone(), - ACCOUNT_01, - )]) - .with_delegations(vec![(delegation_id, delegation_node)]) - .with_attestations(vec![(operation.claim_hash, attestation)]) - .build() - .execute_with(|| { - assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); - assert!( - DelegatedAttestations::::get(delegation_id) - .unwrap_or_default() - .iter() - .any(|&ch| ch == operation.claim_hash), - "delegated attestation entry should be present before removal" - ); - - assert_ok!(Attestation::remove( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - operation.claim_hash, - operation.max_parent_checks - )); - assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); - assert!( - !DelegatedAttestations::::get(delegation_id) - .unwrap_or_default() - .iter() - .any(|&ch| ch == operation.claim_hash), - "delegated attestation entry should be removed" - ); - }); +fn test_reclaim_deposit_not_found() { + todo!() } From f3bd66aab384c888bd6c3a6ce3294c4b69429d67 Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 13 Jan 2022 16:32:13 +0100 Subject: [PATCH 02/60] wip: generic ac in attestations --- pallets/attestation/Cargo.toml | 1 + pallets/attestation/src/benchmarking.rs | 49 ++++---- pallets/attestation/src/lib.rs | 9 +- pallets/attestation/src/mock.rs | 54 +++++++-- pallets/attestation/src/tests.rs | 142 +++++++++++++++++------- 5 files changed, 171 insertions(+), 84 deletions(-) diff --git a/pallets/attestation/Cargo.toml b/pallets/attestation/Cargo.toml index 521b0fd202..3dfb3bd4c6 100644 --- a/pallets/attestation/Cargo.toml +++ b/pallets/attestation/Cargo.toml @@ -58,6 +58,7 @@ runtime-benchmarks = [ "frame-benchmarking", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", + "kilt-support/runtime-benchmarks", "sp-core", ] std = [ diff --git a/pallets/attestation/src/benchmarking.rs b/pallets/attestation/src/benchmarking.rs index a372d8284d..fe3a4b5159 100644 --- a/pallets/attestation/src/benchmarking.rs +++ b/pallets/attestation/src/benchmarking.rs @@ -20,13 +20,11 @@ use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite}; use frame_support::traits::{Currency, Get}; use frame_system::RawOrigin; use sp_runtime::traits::Hash; -use sp_std::num::NonZeroU32; use kilt_support::traits::GenerateBenchmarkOrigin; use crate::*; -const ONE_CHILD_PER_LEVEL: Option = NonZeroU32::new(1); const SEED: u32 = 0; benchmarks! { @@ -34,23 +32,25 @@ benchmarks! { where T: core::fmt::Debug, ::EnsureOrigin: GenerateBenchmarkOrigin, + T::AttesterId: Default } add { let sender: T::AccountId = account("sender", 0, SEED); + let attester: T::AttesterId = account("attester", 0, SEED); let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); - let origin = ::EnsureOrigin::generate_origin(sender.clone(), delegate_acc.clone()); - }: _(origin, claim_hash, ctype_hash, Some(attestation_id)) + let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); + }: _(origin, claim_hash, ctype_hash, None) verify { assert!(Attestations::::contains_key(claim_hash)); assert_eq!(Pallet::::attestations(claim_hash), Some(AttestationDetails { ctype_hash, - attester: delegate_acc, - attestation_id: Some(attestation_id), + attester, + authorization_id: None, revoked: false, deposit: kilt_support::deposit::Deposit { owner: sender, @@ -60,26 +60,22 @@ benchmarks! { } revoke { - let d in 1 .. T::MaxParentChecks::get(); - let sender: T::AccountId = account("sender", 0, SEED); + let attester: T::AttesterId = account("attester", 0, SEED); let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); - // attest with leaf account - let origin = ::EnsureOrigin::generate_origin(sender.clone(), delegate_acc.clone()); - Pallet::::add(origin, claim_hash, ctype_hash, Some(attestation_id))?; - // revoke with root account, s.t. delegation tree needs to be traversed - let origin = ::EnsureOrigin::generate_origin(sender.clone(), root_acc); - }: _(origin, claim_hash, d) + let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); + Pallet::::add(origin.clone(), claim_hash, ctype_hash, None)?; + }: _(origin, claim_hash) verify { assert!(Attestations::::contains_key(claim_hash)); assert_eq!(Attestations::::get(claim_hash), Some(AttestationDetails { ctype_hash, - attester: delegate_acc, - attestation_id: Some(attestation_id), + attester, + authorization_id: None, revoked: true, deposit: kilt_support::deposit::Deposit { owner: sender, @@ -89,20 +85,17 @@ benchmarks! { } remove { - let d in 1 .. T::MaxParentChecks::get(); - let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); + let attester: T::AttesterId = account("attester", 0, SEED); let sender: T::AccountId = account("sender", 0, SEED); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); - // attest with leaf account - let origin = ::EnsureOrigin::generate_origin(sender.clone(), delegate_acc); - Pallet::::add(origin, claim_hash, ctype_hash, Some(attestation_id))?; - // revoke with root account, s.t. delegation tree needs to be traversed - let origin = ::EnsureOrigin::generate_origin(sender, root_acc); - }: _(origin, claim_hash, d) + let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); + Pallet::::add(origin, claim_hash, ctype_hash, None)?; + let origin = ::EnsureOrigin::generate_origin(sender, attester); + }: _(origin, claim_hash) verify { assert!(!Attestations::::contains_key(claim_hash)); } @@ -111,13 +104,13 @@ benchmarks! { let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); + let attester: T::AttesterId = account("attester", 0, SEED); let sender: T::AccountId = account("sender", 0, SEED); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); - // attest with leaf account - let origin = ::EnsureOrigin::generate_origin(sender.clone(), delegate_acc); - Pallet::::add(origin, claim_hash, ctype_hash, Some(attestation_id))?; - // revoke with root account, s.t. delegation tree needs to be traversed + let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester); + Pallet::::add(origin, claim_hash, ctype_hash, None)?; + // revoke with root account let origin = RawOrigin::Signed(sender); }: _(origin, claim_hash) verify { diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 3710c09145..cb7de87c08 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -97,7 +97,6 @@ pub mod pallet { BoundedVec, }; use frame_system::pallet_prelude::*; - use sp_runtime::traits::Zero; use sp_runtime::DispatchError; use ctype::CtypeHashOf; @@ -111,13 +110,13 @@ pub mod pallet { impl AttestationAccessControl for () { fn can_attest(_who: &T::AttesterId, _authorization_id: &T::AuthorizationId) -> Result { - Ok(Weight::zero()) + Err(DispatchError::Other("Unimplemented")) } fn can_revoke(_who: &T::AttesterId, _attestation: &AttestationDetails) -> Result { - Ok(Weight::zero()) + Err(DispatchError::Other("Unimplemented")) } fn can_remove(_who: &T::AttesterId, _attestation: &AttestationDetails) -> Result { - Ok(Weight::zero()) + Err(DispatchError::Other("Unimplemented")) } } @@ -147,7 +146,7 @@ pub mod pallet { type WeightInfo: WeightInfo; /// The currency that is used to reserve funds for each attestation. - type Currency: Currency> + ReservableCurrency>; + type Currency: ReservableCurrency>; /// The deposit that is required for storing an attestation. #[pallet::constant] diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index a531b79698..c56a0bd386 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -24,11 +24,15 @@ //! tests. use ctype::CtypeHashOf; -use frame_support::traits::Get; +use frame_support::{dispatch::Weight, traits::Get}; use kilt_support::deposit::Deposit; use sp_core::H256; +use sp_runtime::DispatchError; -use crate::{pallet::AuthorizationIdOf, AccountIdOf, AttestationDetails, AttesterOf, BalanceOf, ClaimHashOf, Config}; +use crate::{ + pallet::AuthorizationIdOf, AccountIdOf, AttestationAccessControl, AttestationDetails, AttesterOf, BalanceOf, + ClaimHashOf, Config, +}; #[cfg(test)] pub use crate::mock::runtime::*; @@ -67,6 +71,36 @@ where } } +pub struct MockAccessControl; +impl AttestationAccessControl for MockAccessControl +where + T: Config::AttesterId>, +{ + fn can_attest(who: &T::AttesterId, authorization_id: &T::AuthorizationId) -> Result { + if who == authorization_id { + Ok(0) + } else { + Err(DispatchError::Other("Unauthorized")) + } + } + + fn can_revoke(who: &T::AttesterId, attestation: &AttestationDetails) -> Result { + if attestation.authorization_id.as_ref().map_or(false, |auth| auth == who) { + Ok(0) + } else { + Err(DispatchError::Other("Unauthorized")) + } + } + + fn can_remove(who: &T::AttesterId, attestation: &AttestationDetails) -> Result { + if attestation.authorization_id.as_ref().map_or(false, |auth| auth == who) { + Ok(0) + } else { + Err(DispatchError::Other("Unauthorized")) + } + } +} + /// Mocks that are only used internally #[cfg(test)] pub(crate) mod runtime { @@ -195,8 +229,8 @@ pub(crate) mod runtime { type Deposit = Deposit; type MaxDelegatedAttestations = MaxDelegatedAttestations; type AttesterId = SubjectId; - type AuthorizationId = (); - type AccessControl = (); + type AuthorizationId = SubjectId; + type AccessControl = MockAccessControl; } pub(crate) const ACCOUNT_00: runtime_common::AccountId = runtime_common::AccountId::new([1u8; 32]); @@ -205,8 +239,8 @@ pub(crate) mod runtime { pub(crate) const ALICE_SEED: [u8; 32] = [1u8; 32]; pub(crate) const BOB_SEED: [u8; 32] = [2u8; 32]; - const DEFAULT_CLAIM_HASH_SEED: u64 = 1u64; - const ALTERNATIVE_CLAIM_HASH_SEED: u64 = 2u64; + pub const CLAIM_HASH_SEED_01: u64 = 1u64; + pub const CLAIM_HASH_SEED_02: u64 = 2u64; pub fn ed25519_did_from_seed(seed: &[u8; 32]) -> SubjectId { MultiSigner::from(ed25519::Pair::from_seed(seed).public()) @@ -220,12 +254,8 @@ pub(crate) mod runtime { .into() } - pub fn get_claim_hash(default: bool) -> TestClaimHash { - if default { - TestClaimHash::from_low_u64_be(DEFAULT_CLAIM_HASH_SEED) - } else { - TestClaimHash::from_low_u64_be(ALTERNATIVE_CLAIM_HASH_SEED) - } + pub fn claim_hash_from_seed(seed: u64) -> TestClaimHash { + TestClaimHash::from_low_u64_be(seed) } #[derive(Clone, Default)] diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 29e725a042..63771aae94 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -16,8 +16,9 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org +use ctype::mock::get_ctype_hash; use frame_support::{assert_noop, assert_ok}; -use sp_runtime::traits::Zero; +use sp_runtime::{traits::Zero, DispatchError}; use kilt_support::mock::mock_origin::DoubleOrigin; @@ -33,44 +34,88 @@ use crate::{ #[test] fn test_attest_successful() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); - let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let ctype_hash = get_ctype_hash::(true); + let authorization_id = None; - let operation = generate_base_attestation_creation_details(claim_hash, attestation); + ExtBuilder::default() + .with_ctypes(vec![(ctype_hash, attester.clone())]) + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .build() + .execute_with(|| { + assert_ok!(Attestation::add( + DoubleOrigin(ACCOUNT_00, attester.clone()).into(), + claim_hash, + ctype_hash, + authorization_id.clone() + )); + let stored_attestation = + Attestation::attestations(&claim_hash).expect("Attestation should be present on chain."); + + assert_eq!(stored_attestation.ctype_hash, ctype_hash); + assert_eq!(stored_attestation.attester, attester); + assert_eq!(stored_attestation.authorization_id, authorization_id); + assert!(!stored_attestation.revoked); + }); +} + +#[test] +fn test_attest_authorized() { + let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let ctype = get_ctype_hash::(true); + let authorization_id = Some(attester.clone()); ExtBuilder::default() - .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) + .with_ctypes(vec![(ctype, attester.clone())]) .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .build() .execute_with(|| { assert_ok!(Attestation::add( DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, - operation.ctype_hash, - operation.authorization_id + ctype, + authorization_id.clone() )); let stored_attestation = Attestation::attestations(&claim_hash).expect("Attestation should be present on chain."); - assert_eq!(stored_attestation.ctype_hash, operation.ctype_hash); + assert_eq!(stored_attestation.ctype_hash, ctype); assert_eq!(stored_attestation.attester, attester); - assert_eq!(stored_attestation.authorization_id, operation.authorization_id); + assert_eq!(stored_attestation.authorization_id, authorization_id); assert!(!stored_attestation.revoked); }); } #[test] -fn test_attest_with_authorization_successful() { - todo!() +fn test_attest_unauthorized() { + let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); + let bob: AttesterOf = sr25519_did_from_seed(&BOB_SEED); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let ctype = get_ctype_hash::(true); + + ExtBuilder::default() + .with_ctypes(vec![(ctype, attester.clone())]) + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .build() + .execute_with(|| { + assert_eq!( + Attestation::add( + DoubleOrigin(ACCOUNT_00, attester.clone()).into(), + claim_hash, + ctype, + Some(bob) + ), + Err(DispatchError::Other("Unauthorized")) + ); + }); } #[test] fn test_attest_ctype_not_found() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); - let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - - let operation = generate_base_attestation_creation_details(claim_hash, attestation); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let ctype_hash = get_ctype_hash::(true); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) @@ -80,8 +125,8 @@ fn test_attest_ctype_not_found() { Attestation::add( DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, - operation.ctype_hash, - operation.authorization_id + ctype_hash, + None ), ctype::Error::::CTypeNotFound ); @@ -91,41 +136,34 @@ fn test_attest_ctype_not_found() { #[test] fn test_attest_already_exists() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); - + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - let operation = generate_base_attestation_creation_details(claim_hash, attestation.clone()); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(operation.ctype_hash, attester.clone())]) - .with_attestations(vec![(claim_hash, attestation)]) + .with_ctypes(vec![(attestation.ctype_hash.clone(), attester.clone())]) + .with_attestations(vec![(claim_hash, attestation.clone())]) .build() .execute_with(|| { assert_noop!( Attestation::add( DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, - operation.ctype_hash, - operation.authorization_id + attestation.ctype_hash, + None ), attestation::Error::::AlreadyAttested ); }); } -#[test] -fn test_attest_unauthorized() { - todo!() -} - // ############################################################################# // revoke #[test] -fn test_revoke() { +fn test_revoke_remove() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); ExtBuilder::default() @@ -155,7 +193,28 @@ fn test_revoke() { #[test] fn test_authorized_revoke() { - todo!() + let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); + let revoker: AttesterOf = sr25519_did_from_seed(&BOB_SEED); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); + attestation.authorization_id = Some(revoker.clone()); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) + .with_attestations(vec![(claim_hash, attestation)]) + .build() + .execute_with(|| { + assert_ok!(Attestation::revoke( + DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), + claim_hash, + )); + let stored_attestation = + Attestation::attestations(claim_hash).expect("Attestation should be present on chain."); + + assert!(stored_attestation.revoked); + assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); + }); } #[test] @@ -166,7 +225,7 @@ fn test_unauthorized_revoke() { #[test] fn test_revoke_not_found() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); @@ -185,7 +244,7 @@ fn test_revoke_not_found() { #[test] fn test_already_revoked() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); // Attestation already revoked let mut attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); @@ -210,7 +269,7 @@ fn test_already_revoked() { #[test] fn test_remove() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); ExtBuilder::default() @@ -228,19 +287,19 @@ fn test_remove() { } #[test] -fn test_authorized_remove() { +fn test_remove_authorized() { todo!() } #[test] -fn test_unauthorised_remove() { +fn test_remove_unauthorised() { todo!() } #[test] fn test_remove_not_found() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); - let claim_hash = get_claim_hash(true); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); @@ -266,7 +325,7 @@ fn test_remove_not_found() { fn test_reclaim_deposit() { let deposit_owner: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let attester: AttesterOf = sr25519_did_from_seed(&BOB_SEED); - let claim_hash = get_claim_hash(true); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let attestation = generate_base_attestation::(attester, ACCOUNT_00); ExtBuilder::default() @@ -284,6 +343,11 @@ fn test_reclaim_deposit() { }); } +#[test] +fn test_reclaim_others_deposit() { + todo!() +} + #[test] fn test_reclaim_deposit_not_found() { todo!() From 3ac6c2f00c55036a016c5b1bea804b3fc273e524 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 18 Jan 2022 10:20:07 +0100 Subject: [PATCH 03/60] wip: allow for parameter in ac --- pallets/attestation/src/lib.rs | 70 ++++++++++++++++++++++++-------- pallets/attestation/src/mock.rs | 27 ++++++++---- pallets/attestation/src/tests.rs | 46 ++++++++++++++++----- 3 files changed, 106 insertions(+), 37 deletions(-) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index cb7de87c08..8e492b0189 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -103,21 +103,39 @@ pub mod pallet { use kilt_support::{deposit::Deposit, traits::CallSources}; pub trait AttestationAccessControl { - fn can_attest(who: &T::AttesterId, authorization_id: &T::AuthorizationId) -> Result; - fn can_revoke(who: &T::AttesterId, attestation: &AttestationDetails) -> Result; - fn can_remove(who: &T::AttesterId, attestation: &AttestationDetails) -> Result; + fn can_attest(&self, who: &T::AttesterId) -> Result; + fn can_revoke(&self, who: &T::AttesterId, attestation: &AttestationDetails) + -> Result; + fn can_remove(&self, who: &T::AttesterId, attestation: &AttestationDetails) + -> Result; + fn authorization_id(&self) -> T::AuthorizationId; } - impl AttestationAccessControl for () { - fn can_attest(_who: &T::AttesterId, _authorization_id: &T::AuthorizationId) -> Result { + impl AttestationAccessControl for () + where + T: Config, + T::AuthorizationId: Default, + { + fn can_attest(&self, _who: &T::AttesterId) -> Result { Err(DispatchError::Other("Unimplemented")) } - fn can_revoke(_who: &T::AttesterId, _attestation: &AttestationDetails) -> Result { + fn can_revoke( + &self, + _who: &T::AttesterId, + _attestation: &AttestationDetails, + ) -> Result { Err(DispatchError::Other("Unimplemented")) } - fn can_remove(_who: &T::AttesterId, _attestation: &AttestationDetails) -> Result { + fn can_remove( + &self, + _who: &T::AttesterId, + _attestation: &AttestationDetails, + ) -> Result { Err(DispatchError::Other("Unimplemented")) } + fn authorization_id(&self) -> T::AuthorizationId { + Default::default() + } } /// Type of a claim hash. @@ -161,7 +179,7 @@ pub mod pallet { type AuthorizationId: Parameter; - type AccessControl: AttestationAccessControl; + type AccessControl: Parameter + AttestationAccessControl; } #[pallet::pallet] @@ -261,7 +279,7 @@ pub mod pallet { origin: OriginFor, claim_hash: ClaimHashOf, ctype_hash: CtypeHashOf, - authorization_id: Option>, + authorization: Option, ) -> DispatchResult { let source = ::EnsureOrigin::ensure_origin(origin)?; let payer = source.sender(); @@ -278,9 +296,8 @@ pub mod pallet { ); // Check for validity of the delegation node if specified. - if let Some(ref authorization_id) = authorization_id { - T::AccessControl::can_attest(&who, authorization_id)?; - } + authorization.as_ref().map(|ac| ac.can_attest(&who)).transpose()?; + let authorization_id = authorization.as_ref().map(|ac| ac.authorization_id()); let deposit = Pallet::::reserve_deposit(payer, deposit_amount)?; @@ -288,18 +305,23 @@ pub mod pallet { log::debug!("insert Attestation"); + Self::deposit_event(Event::AttestationCreated( + who.clone(), + claim_hash, + ctype_hash, + authorization_id.clone(), + )); Attestations::::insert( &claim_hash, AttestationDetails { ctype_hash, attester: who.clone(), - authorization_id: authorization_id.clone(), + authorization_id, revoked: false, deposit, }, ); - Self::deposit_event(Event::AttestationCreated(who, claim_hash, ctype_hash, authorization_id)); Ok(()) } @@ -321,7 +343,11 @@ pub mod pallet { /// - Writes: Attestations, DelegatedAttestations /// # #[pallet::weight(::WeightInfo::revoke())] - pub fn revoke(origin: OriginFor, claim_hash: ClaimHashOf) -> DispatchResultWithPostInfo { + pub fn revoke( + origin: OriginFor, + claim_hash: ClaimHashOf, + authorization: Option, + ) -> DispatchResultWithPostInfo { let source = ::EnsureOrigin::ensure_origin(origin)?; let who = source.subject(); @@ -330,7 +356,9 @@ pub mod pallet { ensure!(!attestation.revoked, Error::::AlreadyRevoked); if attestation.attester != who { - T::AccessControl::can_revoke(&who, &attestation)?; + authorization + .ok_or(Error::::Unauthorized)? + .can_revoke(&who, &attestation)?; } // *** No Fail beyond this point *** @@ -367,14 +395,20 @@ pub mod pallet { /// - Writes: Attestations, DelegatedAttestations /// # #[pallet::weight(::WeightInfo::remove())] - pub fn remove(origin: OriginFor, claim_hash: ClaimHashOf) -> DispatchResultWithPostInfo { + pub fn remove( + origin: OriginFor, + claim_hash: ClaimHashOf, + authorization: Option, + ) -> DispatchResultWithPostInfo { let source = ::EnsureOrigin::ensure_origin(origin)?; let who = source.subject(); let attestation = Attestations::::get(&claim_hash).ok_or(Error::::AttestationNotFound)?; if attestation.attester != who { - T::AccessControl::can_remove(&who, &attestation)?; + authorization + .ok_or(Error::::Unauthorized)? + .can_remove(&who, &attestation)?; } // *** No Fail beyond this point *** diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index c56a0bd386..0ce3968c10 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -23,12 +23,15 @@ //! other tests. Internal functions/structs can only be used in attestation //! tests. -use ctype::CtypeHashOf; +use codec::{Decode, Encode}; use frame_support::{dispatch::Weight, traits::Get}; -use kilt_support::deposit::Deposit; +use scale_info::TypeInfo; use sp_core::H256; use sp_runtime::DispatchError; +use ctype::CtypeHashOf; +use kilt_support::deposit::Deposit; + use crate::{ pallet::AuthorizationIdOf, AccountIdOf, AttestationAccessControl, AttestationDetails, AttesterOf, BalanceOf, ClaimHashOf, Config, @@ -71,20 +74,22 @@ where } } -pub struct MockAccessControl; -impl AttestationAccessControl for MockAccessControl +#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq, Eq)] +#[scale_info(skip_type_params(T))] +pub struct MockAccessControl(pub T::AttesterId); +impl AttestationAccessControl for MockAccessControl where T: Config::AttesterId>, { - fn can_attest(who: &T::AttesterId, authorization_id: &T::AuthorizationId) -> Result { - if who == authorization_id { + fn can_attest(&self, who: &T::AttesterId) -> Result { + if who == &self.0 { Ok(0) } else { Err(DispatchError::Other("Unauthorized")) } } - fn can_revoke(who: &T::AttesterId, attestation: &AttestationDetails) -> Result { + fn can_revoke(&self, who: &T::AttesterId, attestation: &AttestationDetails) -> Result { if attestation.authorization_id.as_ref().map_or(false, |auth| auth == who) { Ok(0) } else { @@ -92,13 +97,17 @@ where } } - fn can_remove(who: &T::AttesterId, attestation: &AttestationDetails) -> Result { + fn can_remove(&self, who: &T::AttesterId, attestation: &AttestationDetails) -> Result { if attestation.authorization_id.as_ref().map_or(false, |auth| auth == who) { Ok(0) } else { Err(DispatchError::Other("Unauthorized")) } } + + fn authorization_id(&self) -> T::AuthorizationId { + self.0.clone() + } } /// Mocks that are only used internally @@ -230,7 +239,7 @@ pub(crate) mod runtime { type MaxDelegatedAttestations = MaxDelegatedAttestations; type AttesterId = SubjectId; type AuthorizationId = SubjectId; - type AccessControl = MockAccessControl; + type AccessControl = MockAccessControl; } pub(crate) const ACCOUNT_00: runtime_common::AccountId = runtime_common::AccountId::new([1u8; 32]); diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 63771aae94..b58f92bff0 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -25,7 +25,7 @@ use kilt_support::mock::mock_origin::DoubleOrigin; use crate::{ self as attestation, mock::{runtime::Balances, *}, - AttesterOf, Config, + AttestationAccessControl, AttesterOf, Config, }; // ############################################################################# @@ -54,7 +54,10 @@ fn test_attest_successful() { assert_eq!(stored_attestation.ctype_hash, ctype_hash); assert_eq!(stored_attestation.attester, attester); - assert_eq!(stored_attestation.authorization_id, authorization_id); + assert_eq!( + stored_attestation.authorization_id, + authorization_id.map(|ac| ac.authorization_id()) + ); assert!(!stored_attestation.revoked); }); } @@ -64,7 +67,7 @@ fn test_attest_authorized() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ctype = get_ctype_hash::(true); - let authorization_id = Some(attester.clone()); + let authorization_id = Some(MockAccessControl(attester.clone())); ExtBuilder::default() .with_ctypes(vec![(ctype, attester.clone())]) @@ -82,7 +85,10 @@ fn test_attest_authorized() { assert_eq!(stored_attestation.ctype_hash, ctype); assert_eq!(stored_attestation.attester, attester); - assert_eq!(stored_attestation.authorization_id, authorization_id); + assert_eq!( + stored_attestation.authorization_id, + authorization_id.map(|ac| ac.authorization_id()) + ); assert!(!stored_attestation.revoked); }); } @@ -93,6 +99,7 @@ fn test_attest_unauthorized() { let bob: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ctype = get_ctype_hash::(true); + let authorization_id = Some(MockAccessControl(bob.clone())); ExtBuilder::default() .with_ctypes(vec![(ctype, attester.clone())]) @@ -104,7 +111,7 @@ fn test_attest_unauthorized() { DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, ctype, - Some(bob) + authorization_id ), Err(DispatchError::Other("Unauthorized")) ); @@ -175,6 +182,7 @@ fn test_revoke_remove() { assert_ok!(Attestation::revoke( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash, + None )); let stored_attestation = Attestation::attestations(claim_hash).expect("Attestation should be present on chain."); @@ -185,6 +193,7 @@ fn test_revoke_remove() { assert_ok!(Attestation::remove( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash, + None )); assert!(Attestation::attestations(claim_hash).is_none()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); @@ -196,6 +205,7 @@ fn test_authorized_revoke() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let revoker: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let authorization_id = Some(MockAccessControl(revoker.clone())); let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); attestation.authorization_id = Some(revoker.clone()); @@ -208,6 +218,7 @@ fn test_authorized_revoke() { assert_ok!(Attestation::revoke( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash, + authorization_id )); let stored_attestation = Attestation::attestations(claim_hash).expect("Attestation should be present on chain."); @@ -226,7 +237,7 @@ fn test_unauthorized_revoke() { fn test_revoke_not_found() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - + let authorization_id = Some(MockAccessControl(revoker.clone())); let attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); ExtBuilder::default() @@ -235,7 +246,11 @@ fn test_revoke_not_found() { .build() .execute_with(|| { assert_noop!( - Attestation::revoke(DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash,), + Attestation::revoke( + DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), + claim_hash, + authorization_id + ), attestation::Error::::AttestationNotFound ); }); @@ -245,6 +260,7 @@ fn test_revoke_not_found() { fn test_already_revoked() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let authorization_id = Some(MockAccessControl(revoker.clone())); // Attestation already revoked let mut attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); @@ -257,7 +273,11 @@ fn test_already_revoked() { .build() .execute_with(|| { assert_noop!( - Attestation::revoke(DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash,), + Attestation::revoke( + DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), + claim_hash, + authorization_id + ), attestation::Error::::AlreadyRevoked ); }); @@ -271,6 +291,7 @@ fn test_remove() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); + let authorization_id = Some(MockAccessControl(revoker.clone())); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) @@ -281,6 +302,7 @@ fn test_remove() { assert_ok!(Attestation::remove( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash, + authorization_id )); assert!(Attestation::attestations(claim_hash).is_none()) }); @@ -300,7 +322,7 @@ fn test_remove_unauthorised() { fn test_remove_not_found() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - + let authorization_id = Some(MockAccessControl(attester.clone())); let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); ExtBuilder::default() @@ -311,7 +333,11 @@ fn test_remove_not_found() { assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); assert_noop!( - Attestation::remove(DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash,), + Attestation::remove( + DoubleOrigin(ACCOUNT_00, attester.clone()).into(), + claim_hash, + authorization_id + ), attestation::Error::::AttestationNotFound ); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); From 8a78bf83238eaaf8ce65f05873ac55893655c716 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 18 Jan 2022 14:53:24 +0100 Subject: [PATCH 04/60] remove runtime dependency from pallet --- pallets/attestation/Cargo.toml | 4 -- pallets/attestation/src/benchmarking.rs | 4 +- pallets/attestation/src/mock.rs | 55 ++++++++++++++----------- pallets/ctype/Cargo.toml | 4 -- pallets/ctype/src/mock.rs | 35 +++++++++------- 5 files changed, 53 insertions(+), 49 deletions(-) diff --git a/pallets/attestation/Cargo.toml b/pallets/attestation/Cargo.toml index 3dfb3bd4c6..d129b709c7 100644 --- a/pallets/attestation/Cargo.toml +++ b/pallets/attestation/Cargo.toml @@ -15,7 +15,6 @@ substrate-wasm-builder-runner = {version = "3.0.0"} [dev-dependencies] ctype = {features = ["mock"], path = "../ctype"} kilt-support = {features = ["mock"], path = "../../support"} -runtime-common = {default-features = false, path = "../../runtimes/common"} pallet-balances = {branch = "polkadot-v0.9.12", default-features = false, git = "https://github.com/paritytech/substrate"} serde = {version = "1.0.124"} @@ -27,7 +26,6 @@ sp-keystore = {branch = "polkadot-v0.9.12", default-features = false, git = "htt # Internal dependencies ctype = {default-features = false, path = "../ctype"} kilt-support = {default-features = false, path = "../../support"} -runtime-common = {default-features = false, optional = true, path = "../../runtimes/common"} #External dependencies codec = {default-features = false, features = ["derive"], package = "parity-scale-codec", version = "2.3.1"} @@ -47,7 +45,6 @@ sp-std = {branch = "polkadot-v0.9.12", default-features = false, git = "https:// [features] default = ["std"] mock = [ - "runtime-common", "pallet-balances", "serde", "sp-core", @@ -66,7 +63,6 @@ std = [ "ctype/std", "frame-support/std", "frame-system/std", - "runtime-common/std", "kilt-support/std", "log/std", "pallet-balances/std", diff --git a/pallets/attestation/src/benchmarking.rs b/pallets/attestation/src/benchmarking.rs index fe3a4b5159..509522530a 100644 --- a/pallets/attestation/src/benchmarking.rs +++ b/pallets/attestation/src/benchmarking.rs @@ -69,7 +69,7 @@ benchmarks! { let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); Pallet::::add(origin.clone(), claim_hash, ctype_hash, None)?; - }: _(origin, claim_hash) + }: _(origin, claim_hash, None) verify { assert!(Attestations::::contains_key(claim_hash)); assert_eq!(Attestations::::get(claim_hash), Some(AttestationDetails { @@ -95,7 +95,7 @@ benchmarks! { let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); Pallet::::add(origin, claim_hash, ctype_hash, None)?; let origin = ::EnsureOrigin::generate_origin(sender, attester); - }: _(origin, claim_hash) + }: _(origin, claim_hash, None) verify { assert!(!Attestations::::contains_key(claim_hash)); } diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index 0ce3968c10..40a4f2ff59 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -120,12 +120,11 @@ pub(crate) mod runtime { use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Verify}, - MultiSigner, + MultiSignature, MultiSigner, }; use std::sync::Arc; use kilt_support::mock::{mock_origin, SubjectId}; - use runtime_common::constants::{attestation::ATTESTATION_DEPOSIT, MILLI_KILT}; use super::*; use crate::Pallet; @@ -133,9 +132,15 @@ pub(crate) mod runtime { type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; - type TestCtypeHash = runtime_common::Hash; - type TestClaimHash = runtime_common::Hash; - type TestBalance = runtime_common::Balance; + pub type Hash = sp_core::H256; + pub type Balance = u128; + pub type Signature = MultiSignature; + pub type AccountPublic = ::Signer; + pub type AccountId = ::AccountId; + + pub const UNIT: Balance = 10u128.pow(15); + pub const MILLI_UNIT: Balance = 10u128.pow(12); + pub const ATTESTATION_DEPOSIT: Balance = 10 * MILLI_UNIT; frame_support::construct_runtime!( pub enum Test where @@ -161,9 +166,9 @@ pub(crate) mod runtime { type Call = Call; type Index = u64; type BlockNumber = u64; - type Hash = runtime_common::Hash; + type Hash = Hash; type Hashing = BlakeTwo256; - type AccountId = <::Signer as IdentifyAccount>::AccountId; + type AccountId = AccountId; type Lookup = IdentityLookup; type Header = Header; type Event = (); @@ -172,7 +177,7 @@ pub(crate) mod runtime { type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; + type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); type BaseCallFilter = frame_support::traits::Everything; @@ -184,13 +189,13 @@ pub(crate) mod runtime { } parameter_types! { - pub const ExistentialDeposit: TestBalance = MILLI_KILT; + pub const ExistentialDeposit: Balance = MILLI_UNIT; pub const MaxLocks: u32 = 50; pub const MaxReserves: u32 = 50; } impl pallet_balances::Config for Test { - type Balance = TestBalance; + type Balance = Balance; type DustRemoval = (); type Event = (); type ExistentialDeposit = ExistentialDeposit; @@ -202,13 +207,13 @@ pub(crate) mod runtime { } parameter_types! { - pub const Fee: TestBalance = 500; + pub const Fee: Balance = 500; } impl ctype::Config for Test { type CtypeCreatorId = SubjectId; - type EnsureOrigin = mock_origin::EnsureDoubleOrigin; - type OriginSuccess = mock_origin::DoubleOrigin; + type EnsureOrigin = mock_origin::EnsureDoubleOrigin; + type OriginSuccess = mock_origin::DoubleOrigin; type Event = (); type WeightInfo = (); @@ -219,18 +224,18 @@ pub(crate) mod runtime { impl mock_origin::Config for Test { type Origin = Origin; - type AccountId = runtime_common::AccountId; + type AccountId = AccountId; type SubjectId = SubjectId; } parameter_types! { pub const MaxDelegatedAttestations: u32 = 1000; - pub const Deposit: TestBalance = ATTESTATION_DEPOSIT; + pub const Deposit: Balance = ATTESTATION_DEPOSIT; } impl Config for Test { - type EnsureOrigin = mock_origin::EnsureDoubleOrigin>; - type OriginSuccess = mock_origin::DoubleOrigin>; + type EnsureOrigin = mock_origin::EnsureDoubleOrigin>; + type OriginSuccess = mock_origin::DoubleOrigin>; type Event = (); type WeightInfo = (); @@ -242,8 +247,8 @@ pub(crate) mod runtime { type AccessControl = MockAccessControl; } - pub(crate) const ACCOUNT_00: runtime_common::AccountId = runtime_common::AccountId::new([1u8; 32]); - pub(crate) const ACCOUNT_01: runtime_common::AccountId = runtime_common::AccountId::new([2u8; 32]); + pub(crate) const ACCOUNT_00: AccountId = AccountId::new([1u8; 32]); + pub(crate) const ACCOUNT_01: AccountId = AccountId::new([2u8; 32]); pub(crate) const ALICE_SEED: [u8; 32] = [1u8; 32]; pub(crate) const BOB_SEED: [u8; 32] = [2u8; 32]; @@ -263,21 +268,21 @@ pub(crate) mod runtime { .into() } - pub fn claim_hash_from_seed(seed: u64) -> TestClaimHash { - TestClaimHash::from_low_u64_be(seed) + pub fn claim_hash_from_seed(seed: u64) -> Hash { + Hash::from_low_u64_be(seed) } #[derive(Clone, Default)] pub struct ExtBuilder { /// initial ctypes & owners - ctypes: Vec<(TestCtypeHash, CtypeCreatorOf)>, + ctypes: Vec<(CtypeHashOf, CtypeCreatorOf)>, /// endowed accounts with balances balances: Vec<(AccountIdOf, BalanceOf)>, - attestations: Vec<(TestClaimHash, AttestationDetails)>, + attestations: Vec<(ClaimHashOf, AttestationDetails)>, } impl ExtBuilder { - pub fn with_ctypes(mut self, ctypes: Vec<(TestCtypeHash, CtypeCreatorOf)>) -> Self { + pub fn with_ctypes(mut self, ctypes: Vec<(CtypeHashOf, CtypeCreatorOf)>) -> Self { self.ctypes = ctypes; self } @@ -287,7 +292,7 @@ pub(crate) mod runtime { self } - pub fn with_attestations(mut self, attestations: Vec<(TestClaimHash, AttestationDetails)>) -> Self { + pub fn with_attestations(mut self, attestations: Vec<(ClaimHashOf, AttestationDetails)>) -> Self { self.attestations = attestations; self } diff --git a/pallets/ctype/Cargo.toml b/pallets/ctype/Cargo.toml index 913b8e5640..5a2c470a01 100644 --- a/pallets/ctype/Cargo.toml +++ b/pallets/ctype/Cargo.toml @@ -13,7 +13,6 @@ targets = ["x86_64-unknown-linux-gnu"] substrate-wasm-builder-runner = {version = "3.0.0"} [dev-dependencies] -runtime-common = {default-features = false, path = "../../runtimes/common"} kilt-support = {features = ["mock"], path = "../../support"} pallet-balances = {branch = "polkadot-v0.9.12", default-features = false, git = "https://github.com/paritytech/substrate"} serde = {version = "1.0.124"} @@ -27,7 +26,6 @@ scale-info = {version = "1.0", default-features = false, features = ["derive"]} serde = {optional = true, version = "1.0.124"} # Internal dependencies -runtime-common = {default-features = false, optional = true, path = "../../runtimes/common"} kilt-support = {default-features = false, path = "../../support"} # Substrate dependencies @@ -44,7 +42,6 @@ sp-std = {branch = "polkadot-v0.9.12", default-features = false, git = "https:// [features] default = ["std"] mock = [ - "runtime-common", "pallet-balances", "serde", "sp-core", @@ -62,7 +59,6 @@ std = [ "frame-benchmarking/std", "frame-support/std", "frame-system/std", - "runtime-common/std", "kilt-support/std", "log/std", "pallet-balances/std", diff --git a/pallets/ctype/src/mock.rs b/pallets/ctype/src/mock.rs index b9824911c1..f5540bffd6 100644 --- a/pallets/ctype/src/mock.rs +++ b/pallets/ctype/src/mock.rs @@ -36,21 +36,28 @@ where #[cfg(test)] pub mod runtime { - use frame_support::parameter_types; + use frame_support::{parameter_types, weights::constants::RocksDbWeight}; use kilt_support::mock::{mock_origin, SubjectId}; - use runtime_common::{Balance, Header, RocksDbWeight}; use sp_runtime::{ + testing::Header, traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Verify}, - AccountId32, + AccountId32, MultiSignature, }; use crate::{BalanceOf, Ctypes}; use super::*; - pub type TestCtypeHash = runtime_common::Hash; pub type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; pub type Block = frame_system::mocking::MockBlock; + pub type Hash = sp_core::H256; + pub type Balance = u128; + pub type Signature = MultiSignature; + pub type AccountPublic = ::Signer; + pub type AccountId = ::AccountId; + + pub const UNIT: Balance = 10u128.pow(15); + pub const MILLI_UNIT: Balance = 10u128.pow(12); frame_support::construct_runtime!( pub enum Test where @@ -75,9 +82,9 @@ pub mod runtime { type Call = Call; type Index = u64; type BlockNumber = u64; - type Hash = runtime_common::Hash; + type Hash = Hash; type Hashing = BlakeTwo256; - type AccountId = <::Signer as IdentifyAccount>::AccountId; + type AccountId = AccountId; type Lookup = IdentityLookup; type Header = Header; type Event = (); @@ -117,7 +124,7 @@ pub mod runtime { impl mock_origin::Config for Test { type Origin = Origin; - type AccountId = runtime_common::AccountId; + type AccountId = AccountId; type SubjectId = SubjectId; } @@ -127,8 +134,8 @@ pub mod runtime { impl Config for Test { type CtypeCreatorId = SubjectId; - type EnsureOrigin = mock_origin::EnsureDoubleOrigin; - type OriginSuccess = mock_origin::DoubleOrigin; + type EnsureOrigin = mock_origin::EnsureDoubleOrigin; + type OriginSuccess = mock_origin::DoubleOrigin; type Event = (); type WeightInfo = (); @@ -138,21 +145,21 @@ pub mod runtime { } pub(crate) const DID_00: SubjectId = SubjectId(AccountId32::new([1u8; 32])); - pub(crate) const ACCOUNT_00: runtime_common::AccountId = runtime_common::AccountId::new([1u8; 32]); + pub(crate) const ACCOUNT_00: AccountId = AccountId::new([1u8; 32]); #[derive(Clone, Default)] pub(crate) struct ExtBuilder { - ctypes_stored: Vec<(TestCtypeHash, SubjectId)>, - balances: Vec<(runtime_common::AccountId, BalanceOf)>, + ctypes_stored: Vec<(CtypeHashOf, SubjectId)>, + balances: Vec<(AccountId, BalanceOf)>, } impl ExtBuilder { - pub(crate) fn with_ctypes(mut self, ctypes: Vec<(TestCtypeHash, SubjectId)>) -> Self { + pub(crate) fn with_ctypes(mut self, ctypes: Vec<(CtypeHashOf, SubjectId)>) -> Self { self.ctypes_stored = ctypes; self } - pub(crate) fn with_balances(mut self, balances: Vec<(runtime_common::AccountId, BalanceOf)>) -> Self { + pub(crate) fn with_balances(mut self, balances: Vec<(AccountId, BalanceOf)>) -> Self { self.balances = balances; self } From 83660d1cbc7ead98f6ab38b6a227983100ad1c5d Mon Sep 17 00:00:00 2001 From: weichweich Date: Wed, 19 Jan 2022 09:47:28 +0100 Subject: [PATCH 05/60] add weight --- pallets/attestation/src/lib.rs | 4 ++++ pallets/attestation/src/mock.rs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 8e492b0189..478d55286e 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -109,6 +109,7 @@ pub mod pallet { fn can_remove(&self, who: &T::AttesterId, attestation: &AttestationDetails) -> Result; fn authorization_id(&self) -> T::AuthorizationId; + fn weight(&self) -> Weight; } impl AttestationAccessControl for () @@ -136,6 +137,9 @@ pub mod pallet { fn authorization_id(&self) -> T::AuthorizationId { Default::default() } + fn weight(&self) -> Weight { + 0 + } } /// Type of a claim hash. diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index 40a4f2ff59..9f62053623 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -108,6 +108,10 @@ where fn authorization_id(&self) -> T::AuthorizationId { self.0.clone() } + + fn weight(&self) -> Weight { + 0 + } } /// Mocks that are only used internally From db479e8d67bfefdbe011b1af37c8535a353bee48 Mon Sep 17 00:00:00 2001 From: weichweich Date: Wed, 19 Jan 2022 10:32:05 +0100 Subject: [PATCH 06/60] remove runtime dependency from delegations --- pallets/delegation/Cargo.toml | 3 -- pallets/delegation/src/mock.rs | 86 ++++++++++++++++++---------------- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/pallets/delegation/Cargo.toml b/pallets/delegation/Cargo.toml index 6778cad6c9..f65bbc6756 100644 --- a/pallets/delegation/Cargo.toml +++ b/pallets/delegation/Cargo.toml @@ -14,7 +14,6 @@ substrate-wasm-builder-runner = {version = "3.0.0"} [dev-dependencies] ctype = {features = ["mock"], path = "../ctype"} -runtime-common = {default-features = false, path = "../../runtimes/common"} kilt-support = {features = ["mock"], path = "../../support"} # External dependencies @@ -29,7 +28,6 @@ sp-keystore = {branch = "polkadot-v0.9.12", default-features = false, git = "htt [dependencies] # Internal dependencies ctype = {default-features = false, path = "../ctype"} -runtime-common = {default-features = false, path = "../../runtimes/common"} kilt-support = {default-features = false, path = "../../support"} #External dependencies @@ -73,7 +71,6 @@ std = [ "ctype/std", "frame-support/std", "frame-system/std", - "runtime-common/std", "kilt-support/std", "log/std", "pallet-balances/std", diff --git a/pallets/delegation/src/mock.rs b/pallets/delegation/src/mock.rs index 4c02ab5712..55394606db 100644 --- a/pallets/delegation/src/mock.rs +++ b/pallets/delegation/src/mock.rs @@ -166,7 +166,7 @@ where #[cfg(test)] pub mod runtime { - use crate::{migrations::DelegationStorageVersion, BalanceOf}; + use crate::{migrations::DelegationStorageVersion, BalanceOf, DelegateSignatureTypeOf, DelegationNodeIdOf}; use super::*; @@ -176,8 +176,8 @@ pub mod runtime { use sp_keystore::{testing::KeyStore, KeystoreExt}; use sp_runtime::{ testing::Header, - traits::{BlakeTwo256, IdentifyAccount, IdentityLookup}, - MultiSigner, + traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Verify}, + MultiSignature, MultiSigner, }; use sp_std::sync::Arc; @@ -185,15 +185,19 @@ pub mod runtime { mock::{mock_origin, SubjectId}, signature::EqualVerify, }; - use runtime_common::constants::delegation::DELEGATION_DEPOSIT; pub type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; pub type Block = frame_system::mocking::MockBlock; - type TestDelegationNodeId = runtime_common::Hash; - type TestDelegateSignature = (SubjectId, Vec); - type TestBalance = runtime_common::Balance; - type TestCtypeHash = runtime_common::Hash; + pub type Hash = sp_core::H256; + pub type Balance = u128; + pub type Signature = MultiSignature; + pub type AccountPublic = ::Signer; + pub type AccountId = ::AccountId; + + pub const UNIT: Balance = 10u128.pow(15); + pub const MILLI_UNIT: Balance = 10u128.pow(12); + pub const DELEGATION_DEPOSIT: Balance = 10 * MILLI_UNIT; frame_support::construct_runtime!( pub enum Test where @@ -219,9 +223,9 @@ pub mod runtime { type Call = Call; type Index = u64; type BlockNumber = u64; - type Hash = runtime_common::Hash; + type Hash = Hash; type Hashing = BlakeTwo256; - type AccountId = runtime_common::AccountId; + type AccountId = AccountId; type Lookup = IdentityLookup; type Header = Header; type Event = (); @@ -230,7 +234,7 @@ pub mod runtime { type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; + type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); type BaseCallFilter = frame_support::traits::Everything; @@ -242,13 +246,13 @@ pub mod runtime { } parameter_types! { - pub const ExistentialDeposit: TestBalance = 0; + pub const ExistentialDeposit: Balance = 0; pub const MaxLocks: u32 = 50; pub const MaxReserves: u32 = 50; } impl pallet_balances::Config for Test { - type Balance = TestBalance; + type Balance = Balance; type DustRemoval = (); type Event = (); type ExistentialDeposit = ExistentialDeposit; @@ -261,18 +265,18 @@ pub mod runtime { impl mock_origin::Config for Test { type Origin = Origin; - type AccountId = runtime_common::AccountId; + type AccountId = AccountId; type SubjectId = SubjectId; } parameter_types! { - pub const Fee: TestBalance = 500; + pub const Fee: Balance = 500; } impl ctype::Config for Test { type CtypeCreatorId = SubjectId; - type EnsureOrigin = mock_origin::EnsureDoubleOrigin; - type OriginSuccess = mock_origin::DoubleOrigin; + type EnsureOrigin = mock_origin::EnsureDoubleOrigin; + type OriginSuccess = mock_origin::DoubleOrigin; type Event = (); type WeightInfo = (); @@ -288,16 +292,16 @@ pub mod runtime { pub const MaxRemovals: u32 = 5; #[derive(Clone)] pub const MaxChildren: u32 = 1000; - pub const DepositMock: TestBalance = DELEGATION_DEPOSIT; + pub const DepositMock: Balance = DELEGATION_DEPOSIT; } impl Config for Test { - type Signature = TestDelegateSignature; + type Signature = (SubjectId, Vec); type DelegationSignatureVerification = EqualVerify>; type DelegationEntityId = SubjectId; - type DelegationNodeId = TestDelegationNodeId; - type EnsureOrigin = mock_origin::EnsureDoubleOrigin; - type OriginSuccess = mock_origin::DoubleOrigin; + type DelegationNodeId = Hash; + type EnsureOrigin = mock_origin::EnsureDoubleOrigin; + type OriginSuccess = mock_origin::DoubleOrigin; type Event = (); type MaxSignatureByteLength = MaxSignatureByteLength; type MaxParentChecks = MaxParentChecks; @@ -309,9 +313,9 @@ pub mod runtime { type WeightInfo = (); } - pub(crate) const ACCOUNT_00: runtime_common::AccountId = runtime_common::AccountId::new([1u8; 32]); - pub(crate) const ACCOUNT_01: runtime_common::AccountId = runtime_common::AccountId::new([2u8; 32]); - pub(crate) const ACCOUNT_02: runtime_common::AccountId = runtime_common::AccountId::new([3u8; 32]); + pub(crate) const ACCOUNT_00: AccountId = AccountId::new([1u8; 32]); + pub(crate) const ACCOUNT_01: AccountId = AccountId::new([2u8; 32]); + pub(crate) const ACCOUNT_02: AccountId = AccountId::new([3u8; 32]); pub(crate) const ALICE_SEED: [u8; 32] = [0u8; 32]; pub(crate) const BOB_SEED: [u8; 32] = [1u8; 32]; @@ -334,17 +338,17 @@ pub mod runtime { } pub struct DelegationCreationOperation { - pub delegation_id: TestDelegationNodeId, - pub hierarchy_id: TestDelegationNodeId, - pub parent_id: TestDelegationNodeId, + pub delegation_id: DelegationNodeIdOf, + pub hierarchy_id: DelegationNodeIdOf, + pub parent_id: DelegationNodeIdOf, pub delegate: SubjectId, pub permissions: Permissions, - pub delegate_signature: TestDelegateSignature, + pub delegate_signature: DelegateSignatureTypeOf, } pub fn generate_base_delegation_creation_operation( - delegation_id: TestDelegationNodeId, - delegate_signature: TestDelegateSignature, + delegation_id: DelegationNodeIdOf, + delegate_signature: DelegateSignatureTypeOf, delegation_node: DelegationNode, ) -> DelegationCreationOperation { DelegationCreationOperation { @@ -360,29 +364,29 @@ pub mod runtime { } pub struct DelegationHierarchyRevocationOperation { - pub id: TestDelegationNodeId, + pub id: DelegationNodeIdOf, pub max_children: u32, } pub fn generate_base_delegation_hierarchy_revocation_operation( - id: TestDelegationNodeId, + id: DelegationNodeIdOf, ) -> DelegationHierarchyRevocationOperation { DelegationHierarchyRevocationOperation { id, max_children: 0u32 } } pub struct DelegationRevocationOperation { - pub delegation_id: TestDelegationNodeId, + pub delegation_id: DelegationNodeIdOf, pub max_parent_checks: u32, pub max_revocations: u32, } pub struct DelegationDepositClaimOperation { - pub delegation_id: TestDelegationNodeId, + pub delegation_id: DelegationNodeIdOf, pub max_removals: u32, } pub fn generate_base_delegation_revocation_operation( - delegation_id: TestDelegationNodeId, + delegation_id: DelegationNodeIdOf, ) -> DelegationRevocationOperation { DelegationRevocationOperation { delegation_id, @@ -392,7 +396,7 @@ pub mod runtime { } pub fn generate_base_delegation_deposit_claim_operation( - delegation_id: TestDelegationNodeId, + delegation_id: DelegationNodeIdOf, ) -> DelegationDepositClaimOperation { DelegationDepositClaimOperation { delegation_id, @@ -405,9 +409,9 @@ pub mod runtime { /// endowed accounts with balances balances: Vec<(AccountIdOf, BalanceOf)>, /// initial ctypes & owners - ctypes: Vec<(TestCtypeHash, SubjectId)>, + ctypes: Vec<(CtypeHashOf, SubjectId)>, delegation_hierarchies_stored: DelegationHierarchyInitialization, - delegations_stored: Vec<(TestDelegationNodeId, DelegationNode)>, + delegations_stored: Vec<(DelegationNodeIdOf, DelegationNode)>, storage_version: DelegationStorageVersion, } @@ -425,12 +429,12 @@ pub mod runtime { self } - pub fn with_ctypes(mut self, ctypes: Vec<(TestCtypeHash, SubjectId)>) -> Self { + pub fn with_ctypes(mut self, ctypes: Vec<(CtypeHashOf, SubjectId)>) -> Self { self.ctypes = ctypes; self } - pub fn with_delegations(mut self, delegations: Vec<(TestDelegationNodeId, DelegationNode)>) -> Self { + pub fn with_delegations(mut self, delegations: Vec<(DelegationNodeIdOf, DelegationNode)>) -> Self { self.delegations_stored = delegations; self } From 39e801fa64c1d9c19528b7b086cb31cf95a262b6 Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 20 Jan 2022 11:10:29 +0100 Subject: [PATCH 07/60] allow for different types than in Config this makes it possible to aggregate multiple AttestationAccessControl impls in the runtime --- pallets/attestation/src/lib.rs | 35 +++++++++++---------------------- pallets/attestation/src/mock.rs | 2 +- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 478d55286e..3edadf66e7 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -102,39 +102,28 @@ pub mod pallet { use ctype::CtypeHashOf; use kilt_support::{deposit::Deposit, traits::CallSources}; - pub trait AttestationAccessControl { - fn can_attest(&self, who: &T::AttesterId) -> Result; - fn can_revoke(&self, who: &T::AttesterId, attestation: &AttestationDetails) - -> Result; - fn can_remove(&self, who: &T::AttesterId, attestation: &AttestationDetails) - -> Result; - fn authorization_id(&self) -> T::AuthorizationId; + pub trait AttestationAccessControl { + fn can_attest(&self, who: &AttesterId) -> Result; + fn can_revoke(&self, who: &AttesterId, attestation: &AttestationDetails) -> Result; + fn can_remove(&self, who: &AttesterId, attestation: &AttestationDetails) -> Result; + fn authorization_id(&self) -> AuthorizationId; fn weight(&self) -> Weight; } - impl AttestationAccessControl for () + impl AttestationAccessControl for () where - T: Config, - T::AuthorizationId: Default, + AuthorizationId: Default, { - fn can_attest(&self, _who: &T::AttesterId) -> Result { + fn can_attest(&self, _who: &AttesterId) -> Result { Err(DispatchError::Other("Unimplemented")) } - fn can_revoke( - &self, - _who: &T::AttesterId, - _attestation: &AttestationDetails, - ) -> Result { + fn can_revoke(&self, _who: &AttesterId, _attestation: &AttestationDetails) -> Result { Err(DispatchError::Other("Unimplemented")) } - fn can_remove( - &self, - _who: &T::AttesterId, - _attestation: &AttestationDetails, - ) -> Result { + fn can_remove(&self, _who: &AttesterId, _attestation: &AttestationDetails) -> Result { Err(DispatchError::Other("Unimplemented")) } - fn authorization_id(&self) -> T::AuthorizationId { + fn authorization_id(&self) -> AuthorizationId { Default::default() } fn weight(&self) -> Weight { @@ -183,7 +172,7 @@ pub mod pallet { type AuthorizationId: Parameter; - type AccessControl: Parameter + AttestationAccessControl; + type AccessControl: Parameter + AttestationAccessControl; } #[pallet::pallet] diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index 9f62053623..ca472274ea 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -77,7 +77,7 @@ where #[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq, Eq)] #[scale_info(skip_type_params(T))] pub struct MockAccessControl(pub T::AttesterId); -impl AttestationAccessControl for MockAccessControl +impl AttestationAccessControl for MockAccessControl where T: Config::AttesterId>, { From efb6659155152f442d5d8431419fb0c2fa761bdf Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 20 Jan 2022 11:11:14 +0100 Subject: [PATCH 08/60] impl AttestationAccessControl for delegation --- pallets/delegation/Cargo.toml | 1 + pallets/delegation/src/default_weights.rs | 21 ++++++++ pallets/delegation/src/lib.rs | 64 +++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/pallets/delegation/Cargo.toml b/pallets/delegation/Cargo.toml index f65bbc6756..397dc015ee 100644 --- a/pallets/delegation/Cargo.toml +++ b/pallets/delegation/Cargo.toml @@ -27,6 +27,7 @@ sp-keystore = {branch = "polkadot-v0.9.12", default-features = false, git = "htt [dependencies] # Internal dependencies +attestation = {default-features = false, path = "../attestation"} ctype = {default-features = false, path = "../ctype"} kilt-support = {default-features = false, path = "../../support"} diff --git a/pallets/delegation/src/default_weights.rs b/pallets/delegation/src/default_weights.rs index 7a56e321c8..b5473a0411 100644 --- a/pallets/delegation/src/default_weights.rs +++ b/pallets/delegation/src/default_weights.rs @@ -53,6 +53,7 @@ pub trait WeightInfo { fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight; fn remove_delegation(r: u32, ) -> Weight; fn reclaim_deposit(r: u32, ) -> Weight; + fn is_delegating(r: u32, ) -> Weight; } /// Weights for delegation using the Substrate node and recommended hardware. @@ -122,6 +123,16 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().writes(2_u64)) .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) } + // TODO: run benchmarks + fn is_delegating(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } } // For backwards compatibility and tests @@ -174,4 +185,14 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().writes(2_u64)) .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) } + // TODO: run benchmarks + fn is_delegating(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } } diff --git a/pallets/delegation/src/lib.rs b/pallets/delegation/src/lib.rs index f7bde6fa95..9dfe674962 100644 --- a/pallets/delegation/src/lib.rs +++ b/pallets/delegation/src/lib.rs @@ -271,6 +271,8 @@ pub mod pallet { UnauthorizedRemoval, /// The delegation creator is not allowed to create the delegation. UnauthorizedDelegation, + /// The access was not granted because of an insufficient delegation. + AccessDenied, /// Max number of revocations for delegation nodes has been reached for /// the operation. ExceededRevocationBounds, @@ -939,3 +941,65 @@ impl Pallet { Ok((removals, consumed_weight)) } } + +pub struct DelegationAc(DelegationNodeIdOf, u32); +impl attestation::AttestationAccessControl, DelegationNodeIdOf, T> for DelegationAc +where + T: Config::AttesterId> + attestation::Config, + ::AuthorizationId: PartialEq>, +{ + fn can_attest(&self, who: &T::AttesterId) -> Result { + match Pallet::::is_delegating(who, &self.0, self.1)? { + (true, checks) => Ok(::WeightInfo::is_delegating(checks)), + _ => Err(Error::::AccessDenied.into()), + } + } + + fn can_revoke( + &self, + who: &T::AttesterId, + attestation: &attestation::AttestationDetails, + ) -> Result { + ensure!( + attestation + .authorization_id + .as_ref() + .map(|id| id == &self.0) + .unwrap_or(false), + Error::::AccessDenied + ); + + match Pallet::::is_delegating(who, &self.0, self.1)? { + (true, checks) => Ok(::WeightInfo::is_delegating(checks)), + _ => Err(Error::::AccessDenied.into()), + } + } + + fn can_remove( + &self, + who: &T::AttesterId, + attestation: &attestation::AttestationDetails, + ) -> Result { + ensure!( + attestation + .authorization_id + .as_ref() + .map(|id| id == &self.0) + .unwrap_or(false), + Error::::AccessDenied + ); + + match Pallet::::is_delegating(who, &self.0, self.1)? { + (true, checks) => Ok(::WeightInfo::is_delegating(checks)), + _ => Err(Error::::AccessDenied.into()), + } + } + + fn authorization_id(&self) -> DelegationNodeIdOf { + self.0 + } + + fn weight(&self) -> Weight { + ::WeightInfo::is_delegating(self.1) + } +} From c4e2b1ff94c3188489ff574e4d959a0c8f1da2e0 Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 20 Jan 2022 11:11:27 +0100 Subject: [PATCH 09/60] impl AttestationAccessControl in runtime --- Cargo.lock | 5 +- runtimes/common/Cargo.toml | 2 + runtimes/common/src/authorization.rs | 57 +++++++++++++++++++ runtimes/common/src/lib.rs | 1 + runtimes/peregrine/src/lib.rs | 5 ++ runtimes/peregrine/src/weights/attestation.rs | 10 +--- runtimes/peregrine/src/weights/delegation.rs | 11 ++++ runtimes/spiritnet/src/lib.rs | 3 + runtimes/spiritnet/src/weights/attestation.rs | 10 +--- runtimes/spiritnet/src/weights/delegation.rs | 11 ++++ runtimes/standalone/src/lib.rs | 3 + 11 files changed, 101 insertions(+), 17 deletions(-) create mode 100644 runtimes/common/src/authorization.rs diff --git a/Cargo.lock b/Cargo.lock index df56c1331b..5c764d0dda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -378,7 +378,6 @@ dependencies = [ "log", "pallet-balances", "parity-scale-codec", - "runtime-common", "scale-info", "serde", "sp-core", @@ -1419,7 +1418,6 @@ dependencies = [ "log", "pallet-balances", "parity-scale-codec", - "runtime-common", "scale-info", "serde", "sp-core", @@ -1831,6 +1829,7 @@ dependencies = [ name = "delegation" version = "1.3.1" dependencies = [ + "attestation", "bitflags", "ctype", "env_logger 0.8.4", @@ -1841,7 +1840,6 @@ dependencies = [ "log", "pallet-balances", "parity-scale-codec", - "runtime-common", "scale-info", "serde", "sp-core", @@ -8538,6 +8536,7 @@ dependencies = [ name = "runtime-common" version = "0.1.0" dependencies = [ + "attestation", "frame-support", "frame-system", "pallet-authorship", diff --git a/runtimes/common/Cargo.toml b/runtimes/common/Cargo.toml index 7fcb168e7d..407752d3c5 100644 --- a/runtimes/common/Cargo.toml +++ b/runtimes/common/Cargo.toml @@ -13,6 +13,8 @@ scale-info = {version = "1.0", default-features = false, features = ["derive"]} serde = {version = "1.0.124", optional = true, features = ["derive"]} smallvec = "1.7.0" +attestation = {default-features = false, path = "../../pallets/attestation"} + frame-support = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.12"} frame-system = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.12"} pallet-authorship = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.12"} diff --git a/runtimes/common/src/authorization.rs b/runtimes/common/src/authorization.rs new file mode 100644 index 0000000000..71e7f557a5 --- /dev/null +++ b/runtimes/common/src/authorization.rs @@ -0,0 +1,57 @@ +use frame_support::dispatch::Weight; +use sp_runtime::DispatchError; + +use attestation::AttestationAccessControl; + +pub enum AuthorizationId { + Delegation(DelegationId), +} + +pub enum PalletAuthorize { + Delegation(DelegationAc), +} + +impl AttestationAccessControl, T> + for PalletAuthorize +where + T: attestation::Config>, + DelegationAc: AttestationAccessControl, +{ + fn can_attest(&self, who: &T::AttesterId) -> Result { + match self { + PalletAuthorize::Delegation(ac) => ac.can_attest(who), + } + } + + fn can_revoke( + &self, + who: &T::AttesterId, + attestation: &attestation::AttestationDetails, + ) -> Result { + match self { + PalletAuthorize::Delegation(ac) => ac.can_revoke(who, attestation), + } + } + + fn can_remove( + &self, + who: &T::AttesterId, + attestation: &attestation::AttestationDetails, + ) -> Result { + match self { + PalletAuthorize::Delegation(ac) => ac.can_revoke(who, attestation), + } + } + + fn authorization_id(&self) -> T::AuthorizationId { + match self { + PalletAuthorize::Delegation(ac) => AuthorizationId::Delegation(ac.authorization_id()), + } + } + + fn weight(&self) -> Weight { + match self { + PalletAuthorize::Delegation(ac) => ac.weight(), + } + } +} diff --git a/runtimes/common/src/lib.rs b/runtimes/common/src/lib.rs index c299cf1b94..4fa3ab7bdd 100644 --- a/runtimes/common/src/lib.rs +++ b/runtimes/common/src/lib.rs @@ -40,6 +40,7 @@ use sp_runtime::{ pub mod constants; pub mod fees; pub mod pallet_id; +pub mod authorization; #[cfg(feature = "runtime-benchmarks")] pub mod benchmarks; diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 275647ca5c..d9eb30fef8 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -45,6 +45,7 @@ use sp_std::prelude::*; use sp_version::RuntimeVersion; use runtime_common::{ + authorization::{AuthorizationId, PalletAuthorize}, constants::{self, KILT, MICRO_KILT, MILLI_KILT}, fees::{ToAuthor, WeightToFee}, pallet_id, AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockNumber, BlockWeights, DidIdentifier, @@ -470,6 +471,10 @@ impl attestation::Config for Runtime { type Currency = Balances; type Deposit = AttestationDeposit; type MaxDelegatedAttestations = MaxDelegatedAttestations; + + type AttesterId = DidIdentifier; + type AuthorizationId = (); //AuthorizationId; + type AccessControl = (); //PalletAuthorize; } parameter_types! { diff --git a/runtimes/peregrine/src/weights/attestation.rs b/runtimes/peregrine/src/weights/attestation.rs index 0fb79508e9..47e9cef7fd 100644 --- a/runtimes/peregrine/src/weights/attestation.rs +++ b/runtimes/peregrine/src/weights/attestation.rs @@ -52,20 +52,16 @@ impl attestation::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().reads(6_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } - fn revoke(d: u32, ) -> Weight { + fn revoke() -> Weight { (37_043_000_u64) // Standard Error: 45_000 - .saturating_add((6_394_000_u64).saturating_mul(d as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(d as Weight))) .saturating_add(T::DbWeight::get().writes(1_u64)) } - fn remove(d: u32, ) -> Weight { + fn remove() -> Weight { (64_305_000_u64) // Standard Error: 41_000 - .saturating_add((6_297_000_u64).saturating_mul(d as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(d as Weight))) .saturating_add(T::DbWeight::get().writes(3_u64)) } fn reclaim_deposit() -> Weight { @@ -73,4 +69,4 @@ impl attestation::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } -} \ No newline at end of file +} diff --git a/runtimes/peregrine/src/weights/delegation.rs b/runtimes/peregrine/src/weights/delegation.rs index 7f6ce9c50c..9acaf77270 100644 --- a/runtimes/peregrine/src/weights/delegation.rs +++ b/runtimes/peregrine/src/weights/delegation.rs @@ -112,4 +112,15 @@ impl delegation::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(2 as Weight)) .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) } + + // TODO: run benchmarks + fn is_delegating(r: u32, ) -> Weight { + (53_098_000 as Weight) + // Standard Error: 75_000 + .saturating_add((39_041_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) + .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) + } } diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index 172b9055cf..b73bd74d2c 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -476,6 +476,9 @@ impl attestation::Config for Runtime { type Currency = Balances; type Deposit = AttestationDeposit; type MaxDelegatedAttestations = MaxDelegatedAttestations; + type AttesterId = DidIdentifier; + type AuthorizationId = (); + type AccessControl = (); } parameter_types! { diff --git a/runtimes/spiritnet/src/weights/attestation.rs b/runtimes/spiritnet/src/weights/attestation.rs index b77dd3645f..361aa84123 100644 --- a/runtimes/spiritnet/src/weights/attestation.rs +++ b/runtimes/spiritnet/src/weights/attestation.rs @@ -52,20 +52,16 @@ impl attestation::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().reads(6_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } - fn revoke(d: u32, ) -> Weight { + fn revoke() -> Weight { (37_282_000_u64) // Standard Error: 42_000 - .saturating_add((6_344_000_u64).saturating_mul(d as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(d as Weight))) .saturating_add(T::DbWeight::get().writes(1_u64)) } - fn remove(d: u32, ) -> Weight { + fn remove() -> Weight { (64_367_000_u64) // Standard Error: 58_000 - .saturating_add((6_230_000_u64).saturating_mul(d as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(d as Weight))) .saturating_add(T::DbWeight::get().writes(3_u64)) } fn reclaim_deposit() -> Weight { @@ -73,4 +69,4 @@ impl attestation::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } -} \ No newline at end of file +} diff --git a/runtimes/spiritnet/src/weights/delegation.rs b/runtimes/spiritnet/src/weights/delegation.rs index 750aea260d..1dce96c896 100644 --- a/runtimes/spiritnet/src/weights/delegation.rs +++ b/runtimes/spiritnet/src/weights/delegation.rs @@ -112,4 +112,15 @@ impl delegation::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(2 as Weight)) .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) } + + // TODO: run benchmarks + fn is_delegating(r: u32) -> Weight { + (53_370_000 as Weight) + // Standard Error: 126_000 + .saturating_add((39_346_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) + .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) + } } diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index b9aa2df58f..d7a46906bb 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -314,6 +314,9 @@ impl attestation::Config for Runtime { type Currency = Balances; type Deposit = AttestationDeposit; type MaxDelegatedAttestations = MaxDelegatedAttestations; + type AttesterId = DidIdentifier; + type AuthorizationId = (); + type AccessControl = (); } parameter_types! { From 12f9f68c872af49c0996e5234ba5b5cdfa33483d Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 24 Jan 2022 09:24:33 +0100 Subject: [PATCH 10/60] don't pass the attestation we cant change the authorizationID otherwise --- pallets/attestation/src/lib.rs | 22 +++++++++-------- pallets/attestation/src/mock.rs | 10 ++++---- pallets/delegation/src/lib.rs | 36 +++++++--------------------- runtimes/common/src/authorization.rs | 31 ++++++++++++++---------- runtimes/peregrine/src/lib.rs | 6 ++--- runtimes/spiritnet/src/lib.rs | 6 +++-- 6 files changed, 50 insertions(+), 61 deletions(-) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index de32b46339..5658c0d9d5 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -90,8 +90,8 @@ pub use crate::{attestations::AttestationDetails, default_weights::WeightInfo, p #[frame_support::pallet] pub mod pallet { use super::*; - use frame_support::dispatch::Weight; use frame_support::{ + dispatch::Weight, pallet_prelude::*, traits::{Currency, Get, ReservableCurrency}, BoundedVec, @@ -102,25 +102,25 @@ pub mod pallet { use ctype::CtypeHashOf; use kilt_support::{deposit::Deposit, traits::CallSources}; - pub trait AttestationAccessControl { + pub trait AttestationAccessControl { fn can_attest(&self, who: &AttesterId) -> Result; - fn can_revoke(&self, who: &AttesterId, attestation: &AttestationDetails) -> Result; - fn can_remove(&self, who: &AttesterId, attestation: &AttestationDetails) -> Result; + fn can_revoke(&self, who: &AttesterId, authorization_id: &AuthorizationId) -> Result; + fn can_remove(&self, who: &AttesterId, authorization_id: &AuthorizationId) -> Result; fn authorization_id(&self) -> AuthorizationId; fn weight(&self) -> Weight; } - impl AttestationAccessControl for () + impl AttestationAccessControl for () where AuthorizationId: Default, { fn can_attest(&self, _who: &AttesterId) -> Result { Err(DispatchError::Other("Unimplemented")) } - fn can_revoke(&self, _who: &AttesterId, _attestation: &AttestationDetails) -> Result { + fn can_revoke(&self, _who: &AttesterId, _authorization_id: &AuthorizationId) -> Result { Err(DispatchError::Other("Unimplemented")) } - fn can_remove(&self, _who: &AttesterId, _attestation: &AttestationDetails) -> Result { + fn can_remove(&self, _who: &AttesterId, _authorization_id: &AuthorizationId) -> Result { Err(DispatchError::Other("Unimplemented")) } fn authorization_id(&self) -> AuthorizationId { @@ -172,7 +172,7 @@ pub mod pallet { type AuthorizationId: Parameter; - type AccessControl: Parameter + AttestationAccessControl; + type AccessControl: Parameter + AttestationAccessControl; } #[pallet::pallet] @@ -349,9 +349,10 @@ pub mod pallet { ensure!(!attestation.revoked, Error::::AlreadyRevoked); if attestation.attester != who { + let expected_auth = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; authorization .ok_or(Error::::Unauthorized)? - .can_revoke(&who, &attestation)?; + .can_revoke(&who, &expected_auth)?; } // *** No Fail beyond this point *** @@ -399,9 +400,10 @@ pub mod pallet { let attestation = Attestations::::get(&claim_hash).ok_or(Error::::AttestationNotFound)?; if attestation.attester != who { + let expected_auth = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; authorization .ok_or(Error::::Unauthorized)? - .can_remove(&who, &attestation)?; + .can_remove(&who, &expected_auth)?; } // *** No Fail beyond this point *** diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index 6486ec14be..c11dfcf3ab 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -77,7 +77,7 @@ where #[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq, Eq)] #[scale_info(skip_type_params(T))] pub struct MockAccessControl(pub T::AttesterId); -impl AttestationAccessControl for MockAccessControl +impl AttestationAccessControl for MockAccessControl where T: Config::AttesterId>, { @@ -89,16 +89,16 @@ where } } - fn can_revoke(&self, who: &T::AttesterId, attestation: &AttestationDetails) -> Result { - if attestation.authorization_id.as_ref().map_or(false, |auth| auth == who) { + fn can_revoke(&self, who: &T::AttesterId, authorization_id: &T::AuthorizationId) -> Result { + if authorization_id == who { Ok(0) } else { Err(DispatchError::Other("Unauthorized")) } } - fn can_remove(&self, who: &T::AttesterId, attestation: &AttestationDetails) -> Result { - if attestation.authorization_id.as_ref().map_or(false, |auth| auth == who) { + fn can_remove(&self, who: &T::AttesterId, authorization_id: &T::AuthorizationId) -> Result { + if authorization_id == who { Ok(0) } else { Err(DispatchError::Other("Unauthorized")) diff --git a/pallets/delegation/src/lib.rs b/pallets/delegation/src/lib.rs index 99812853a8..24408d19ea 100644 --- a/pallets/delegation/src/lib.rs +++ b/pallets/delegation/src/lib.rs @@ -88,6 +88,7 @@ mod tests; pub use crate::{default_weights::WeightInfo, delegation_hierarchy::*, pallet::*}; +use codec::{Decode, Encode}; use frame_support::{ dispatch::DispatchResult, ensure, @@ -95,6 +96,7 @@ use frame_support::{ traits::{Get, ReservableCurrency}, }; use migrations::DelegationStorageVersion; +use scale_info::TypeInfo; use sp_runtime::{traits::Hash, DispatchError}; use sp_std::vec::Vec; @@ -942,11 +944,11 @@ impl Pallet { } } +#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] pub struct DelegationAc(DelegationNodeIdOf, u32); -impl attestation::AttestationAccessControl, DelegationNodeIdOf, T> for DelegationAc +impl attestation::AttestationAccessControl, DelegationNodeIdOf> for DelegationAc where T: Config::AttesterId> + attestation::Config, - ::AuthorizationId: PartialEq>, { fn can_attest(&self, who: &T::AttesterId) -> Result { match Pallet::::is_delegating(who, &self.0, self.1)? { @@ -955,19 +957,8 @@ where } } - fn can_revoke( - &self, - who: &T::AttesterId, - attestation: &attestation::AttestationDetails, - ) -> Result { - ensure!( - attestation - .authorization_id - .as_ref() - .map(|id| id == &self.0) - .unwrap_or(false), - Error::::AccessDenied - ); + fn can_revoke(&self, who: &T::AttesterId, auth_id: &DelegationNodeIdOf) -> Result { + ensure!(auth_id == &self.0, Error::::AccessDenied); match Pallet::::is_delegating(who, &self.0, self.1)? { (true, checks) => Ok(::WeightInfo::is_delegating(checks)), @@ -975,19 +966,8 @@ where } } - fn can_remove( - &self, - who: &T::AttesterId, - attestation: &attestation::AttestationDetails, - ) -> Result { - ensure!( - attestation - .authorization_id - .as_ref() - .map(|id| id == &self.0) - .unwrap_or(false), - Error::::AccessDenied - ); + fn can_remove(&self, who: &T::AttesterId, auth_id: &DelegationNodeIdOf) -> Result { + ensure!(auth_id == &self.0, Error::::AccessDenied); match Pallet::::is_delegating(who, &self.0, self.1)? { (true, checks) => Ok(::WeightInfo::is_delegating(checks)), diff --git a/runtimes/common/src/authorization.rs b/runtimes/common/src/authorization.rs index 71e7f557a5..37bb14d606 100644 --- a/runtimes/common/src/authorization.rs +++ b/runtimes/common/src/authorization.rs @@ -1,23 +1,26 @@ +use codec::{Decode, Encode}; use frame_support::dispatch::Weight; +use scale_info::TypeInfo; use sp_runtime::DispatchError; use attestation::AttestationAccessControl; +#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] pub enum AuthorizationId { Delegation(DelegationId), } +#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] pub enum PalletAuthorize { Delegation(DelegationAc), } -impl AttestationAccessControl, T> +impl AttestationAccessControl> for PalletAuthorize where - T: attestation::Config>, - DelegationAc: AttestationAccessControl, + DelegationAc: AttestationAccessControl, { - fn can_attest(&self, who: &T::AttesterId) -> Result { + fn can_attest(&self, who: &AttesterId) -> Result { match self { PalletAuthorize::Delegation(ac) => ac.can_attest(who), } @@ -25,25 +28,27 @@ where fn can_revoke( &self, - who: &T::AttesterId, - attestation: &attestation::AttestationDetails, + who: &AttesterId, + auth_id: &AuthorizationId, ) -> Result { - match self { - PalletAuthorize::Delegation(ac) => ac.can_revoke(who, attestation), + match (self, auth_id) { + (PalletAuthorize::Delegation(ac), AuthorizationId::Delegation(auth_id)) => ac.can_revoke(who, auth_id), + // _ => Err(DispatchError::Other("unauthorized")), } } fn can_remove( &self, - who: &T::AttesterId, - attestation: &attestation::AttestationDetails, + who: &AttesterId, + auth_id: &AuthorizationId, ) -> Result { - match self { - PalletAuthorize::Delegation(ac) => ac.can_revoke(who, attestation), + match (self, auth_id) { + (PalletAuthorize::Delegation(ac), AuthorizationId::Delegation(auth_id)) => ac.can_remove(who, auth_id), + // _ => Err(DispatchError::Other("unauthorized")), } } - fn authorization_id(&self) -> T::AuthorizationId { + fn authorization_id(&self) -> AuthorizationId { match self { PalletAuthorize::Delegation(ac) => AuthorizationId::Delegation(ac.authorization_id()), } diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 89497fb321..706dfb12d4 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -45,6 +45,7 @@ use sp_runtime::{ use sp_std::prelude::*; use sp_version::RuntimeVersion; +use delegation::DelegationAc; use runtime_common::{ authorization::{AuthorizationId, PalletAuthorize}, constants::{self, KILT, MICRO_KILT, MILLI_KILT}, @@ -473,10 +474,9 @@ impl attestation::Config for Runtime { type Currency = Balances; type Deposit = AttestationDeposit; type MaxDelegatedAttestations = MaxDelegatedAttestations; - type AttesterId = DidIdentifier; - type AuthorizationId = (); //AuthorizationId; - type AccessControl = (); //PalletAuthorize; + type AuthorizationId = AuthorizationId<::DelegationNodeId>; + type AccessControl = PalletAuthorize>; } parameter_types! { diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index 5ce3c9841e..ba7c2b8820 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -45,7 +45,9 @@ use sp_runtime::{ use sp_std::prelude::*; use sp_version::RuntimeVersion; +use delegation::DelegationAc; use runtime_common::{ + authorization::{AuthorizationId, PalletAuthorize}, constants::{self, KILT, MICRO_KILT, MILLI_KILT}, fees::{ToAuthor, WeightToFee}, pallet_id, AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockNumber, BlockWeights, DidIdentifier, @@ -478,8 +480,8 @@ impl attestation::Config for Runtime { type Deposit = AttestationDeposit; type MaxDelegatedAttestations = MaxDelegatedAttestations; type AttesterId = DidIdentifier; - type AuthorizationId = (); - type AccessControl = (); + type AuthorizationId = AuthorizationId<::DelegationNodeId>; + type AccessControl = PalletAuthorize>; } parameter_types! { From be8a35919e615979c3f701c48db715e4ebea5971 Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 24 Jan 2022 10:37:48 +0100 Subject: [PATCH 11/60] add weight calculation --- pallets/attestation/src/lib.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 5658c0d9d5..7616a402ac 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -267,7 +267,10 @@ pub mod pallet { /// DelegatedAttestations /// - Writes: Attestations, (DelegatedAttestations) /// # - #[pallet::weight(::WeightInfo::add())] + #[pallet::weight( + ::WeightInfo::add() + + authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0) + )] pub fn add( origin: OriginFor, claim_hash: ClaimHashOf, @@ -335,7 +338,10 @@ pub mod pallet { /// - Reads per delegation step P: delegation::Delegations /// - Writes: Attestations, DelegatedAttestations /// # - #[pallet::weight(::WeightInfo::revoke())] + #[pallet::weight( + ::WeightInfo::revoke() + + authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0) + )] pub fn revoke( origin: OriginFor, claim_hash: ClaimHashOf, @@ -388,7 +394,10 @@ pub mod pallet { /// - Reads per delegation step P: delegation::Delegations /// - Writes: Attestations, DelegatedAttestations /// # - #[pallet::weight(::WeightInfo::remove())] + #[pallet::weight( + ::WeightInfo::remove() + + authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0) + )] pub fn remove( origin: OriginFor, claim_hash: ClaimHashOf, From 27f1f63dd2e676e5bdf2e8b000baccc216339983 Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 24 Jan 2022 15:46:56 +0100 Subject: [PATCH 12/60] tests: attestations --- pallets/attestation/src/mock.rs | 1 + pallets/attestation/src/tests.rs | 128 +++++++++++++++++++++++++------ 2 files changed, 107 insertions(+), 22 deletions(-) diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index c11dfcf3ab..5bde441059 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -256,6 +256,7 @@ pub(crate) mod runtime { pub(crate) const ALICE_SEED: [u8; 32] = [1u8; 32]; pub(crate) const BOB_SEED: [u8; 32] = [2u8; 32]; + pub(crate) const CHARLIE_SEED: [u8; 32] = [3u8; 32]; pub const CLAIM_HASH_SEED_01: u64 = 1u64; pub const CLAIM_HASH_SEED_02: u64 = 2u64; diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 10d13facc5..f93fe5c373 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -230,7 +230,30 @@ fn test_authorized_revoke() { #[test] fn test_unauthorized_revoke() { - todo!() + let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); + let revoker: AttesterOf = sr25519_did_from_seed(&BOB_SEED); + let evil: AttesterOf = sr25519_did_from_seed(&CHARLIE_SEED); + + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let authorization_id = Some(MockAccessControl(revoker.clone())); + let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); + attestation.authorization_id = Some(revoker.clone()); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) + .with_attestations(vec![(claim_hash, attestation)]) + .build() + .execute_with(|| { + assert_noop!( + Attestation::revoke( + DoubleOrigin(ACCOUNT_00, evil.clone()).into(), + claim_hash, + authorization_id + ), + DispatchError::Other("Unauthorized") + ); + }); } #[test] @@ -288,34 +311,73 @@ fn test_already_revoked() { #[test] fn test_remove() { - let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); + let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); - let authorization_id = Some(MockAccessControl(revoker.clone())); + let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); + let authorization_id = None; ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) .with_attestations(vec![(claim_hash, attestation)]) .build() .execute_with(|| { assert_ok!(Attestation::remove( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), + DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, authorization_id )); - assert!(Attestation::attestations(claim_hash).is_none()) + assert!(Attestation::attestations(claim_hash).is_none()); }); } #[test] fn test_remove_authorized() { - todo!() + let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); + let revoker: AttesterOf = sr25519_did_from_seed(&BOB_SEED); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); + attestation.authorization_id = Some(revoker.clone()); + let authorization_id = Some(MockAccessControl(revoker.clone())); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) + .with_attestations(vec![(claim_hash, attestation)]) + .build() + .execute_with(|| { + assert_ok!(Attestation::remove( + DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), + claim_hash, + authorization_id + )); + assert!(Attestation::attestations(claim_hash).is_none()); + }); } #[test] fn test_remove_unauthorised() { - todo!() + let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); + let evil: AttesterOf = sr25519_did_from_seed(&BOB_SEED); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); + let authorization_id = Some(MockAccessControl(evil.clone())); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) + .with_attestations(vec![(claim_hash, attestation)]) + .build() + .execute_with(|| { + assert_noop!( + Attestation::remove( + DoubleOrigin(ACCOUNT_00, evil.clone()).into(), + claim_hash, + authorization_id + ), + attestation::Error::::Unauthorized + ); + }); } #[test] @@ -331,7 +393,6 @@ fn test_remove_not_found() { .build() .execute_with(|| { assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); - assert_noop!( Attestation::remove( DoubleOrigin(ACCOUNT_00, attester.clone()).into(), @@ -340,7 +401,6 @@ fn test_remove_not_found() { ), attestation::Error::::AttestationNotFound ); - assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); }); } @@ -349,14 +409,32 @@ fn test_remove_not_found() { #[test] fn test_reclaim_deposit() { - let deposit_owner: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let attester: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let attestation = generate_base_attestation::(attester, ACCOUNT_00); + let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(attestation.ctype_hash, deposit_owner)]) + .with_ctypes(vec![(attestation.ctype_hash, attester)]) + .with_attestations(vec![(claim_hash, attestation)]) + .build() + .execute_with(|| { + assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); + assert_ok!(Attestation::reclaim_deposit(Origin::signed(ACCOUNT_00), claim_hash)); + assert!(Attestation::attestations(claim_hash).is_none()); + assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); + }); +} + +#[test] +fn test_reclaim_unauthorized() { + let attester: AttesterOf = sr25519_did_from_seed(&BOB_SEED); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, attester)]) .with_attestations(vec![(claim_hash, attestation)]) .build() .execute_with(|| { @@ -364,17 +442,23 @@ fn test_reclaim_deposit() { Attestation::reclaim_deposit(Origin::signed(ACCOUNT_01), claim_hash), attestation::Error::::Unauthorized, ); - assert_ok!(Attestation::reclaim_deposit(Origin::signed(ACCOUNT_00), claim_hash,)); - assert!(Attestation::attestations(claim_hash).is_none()) }); } -#[test] -fn test_reclaim_others_deposit() { - todo!() -} - #[test] fn test_reclaim_deposit_not_found() { - todo!() + let attester: AttesterOf = sr25519_did_from_seed(&BOB_SEED); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, attester)]) + .build() + .execute_with(|| { + assert_noop!( + Attestation::reclaim_deposit(Origin::signed(ACCOUNT_01), claim_hash), + attestation::Error::::AttestationNotFound, + ); + }); } From 9cb1b903959b49370a56d7c5574b9b4a9005af02 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 25 Jan 2022 10:37:23 +0100 Subject: [PATCH 13/60] fix: benchmarks --- pallets/attestation/src/benchmarking.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pallets/attestation/src/benchmarking.rs b/pallets/attestation/src/benchmarking.rs index d088da129d..efc21be412 100644 --- a/pallets/attestation/src/benchmarking.rs +++ b/pallets/attestation/src/benchmarking.rs @@ -32,7 +32,8 @@ benchmarks! { where T: core::fmt::Debug, ::EnsureOrigin: GenerateBenchmarkOrigin, - T::AttesterId: Default + T::AttesterId: Default, + T: ctype::Config, } add { @@ -41,6 +42,7 @@ benchmarks! { let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); + ctype::Ctypes::::insert(&ctype_hash, T::CtypeCreatorId::from(attester.clone())); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); @@ -65,6 +67,7 @@ benchmarks! { let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); + ctype::Ctypes::::insert(&ctype_hash, T::CtypeCreatorId::from(attester.clone())); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); @@ -85,11 +88,12 @@ benchmarks! { } remove { + let attester: T::AttesterId = account("attester", 0, SEED); + let sender: T::AccountId = account("sender", 0, SEED); let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); - let attester: T::AttesterId = account("attester", 0, SEED); - let sender: T::AccountId = account("sender", 0, SEED); + ctype::Ctypes::::insert(&ctype_hash, T::CtypeCreatorId::from(attester.clone())); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); @@ -101,11 +105,12 @@ benchmarks! { } reclaim_deposit { + let attester: T::AttesterId = account("attester", 0, SEED); + let sender: T::AccountId = account("sender", 0, SEED); let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); - let attester: T::AttesterId = account("attester", 0, SEED); - let sender: T::AccountId = account("sender", 0, SEED); + ctype::Ctypes::::insert(&ctype_hash, T::CtypeCreatorId::from(attester.clone())); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester); From 38f4b20f5df2f03a8055447b43fcc11e8389eec6 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 25 Jan 2022 11:58:56 +0100 Subject: [PATCH 14/60] bench: delegation --- pallets/delegation/src/benchmarking.rs | 46 ++++++++++++++- pallets/delegation/src/default_weights.rs | 62 ++++++++++++++++---- pallets/delegation/src/lib.rs | 21 ++++--- runtimes/peregrine/src/weights/delegation.rs | 34 ++++++++--- runtimes/spiritnet/src/weights/delegation.rs | 34 ++++++++--- 5 files changed, 157 insertions(+), 40 deletions(-) diff --git a/pallets/delegation/src/benchmarking.rs b/pallets/delegation/src/benchmarking.rs index 1eb4b6d5f1..887f3ab99f 100644 --- a/pallets/delegation/src/benchmarking.rs +++ b/pallets/delegation/src/benchmarking.rs @@ -26,12 +26,14 @@ use frame_support::{ traits::{Currency, Get}, }; use frame_system::RawOrigin; -use kilt_support::{signature::VerifySignature, traits::GenerateBenchmarkOrigin}; use sp_core::{offchain::KeyTypeId, sr25519}; use sp_io::crypto::sr25519_generate; use sp_runtime::traits::Zero; use sp_std::{num::NonZeroU32, vec::Vec}; +use attestation::AttestationAccessControl; +use kilt_support::{signature::VerifySignature, traits::GenerateBenchmarkOrigin}; + const SEED: u32 = 0; const ONE_CHILD_PER_LEVEL: Option = NonZeroU32::new(1); @@ -376,6 +378,48 @@ benchmarks! { assert!(!DelegationNodes::::contains_key(leaf_id)); assert!(::Currency::reserved_balance(&sender).is_zero()); } + + can_attest { + let r in 1 .. T::MaxRevocations::get(); + let c in 1 .. T::MaxParentChecks::get(); + + let sender: T::AccountId = account("sender", 0, SEED); + let (root_acc, _, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; + let root_acc: T::DelegationEntityId = root_acc.into(); + + let ac = DelegationAc::(leaf_id, c); + + }: { ac.can_attest(&root_acc).expect("Should be allowed") } + verify { + } + + can_revoke { + let r in 1 .. T::MaxRevocations::get(); + let c in 1 .. T::MaxParentChecks::get(); + + let sender: T::AccountId = account("sender", 0, SEED); + let (root_acc, root_id, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; + let root_acc: T::DelegationEntityId = root_acc.into(); + + let ac = DelegationAc::(leaf_id, c); + + }: { ac.can_revoke(&root_acc, &leaf_id).expect("Should be allowed") } + verify { + } + + can_remove { + let r in 1 .. T::MaxRevocations::get(); + let c in 1 .. T::MaxParentChecks::get(); + + let sender: T::AccountId = account("sender", 0, SEED); + let (root_acc, root_id, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; + let root_acc: T::DelegationEntityId = root_acc.into(); + + let ac = DelegationAc::(leaf_id, c); + + }: { ac.can_remove(&root_acc, &leaf_id).expect("Should be allowed") } + verify { + } } impl_benchmark_test_suite! { diff --git a/pallets/delegation/src/default_weights.rs b/pallets/delegation/src/default_weights.rs index 4480247241..c90487ef84 100644 --- a/pallets/delegation/src/default_weights.rs +++ b/pallets/delegation/src/default_weights.rs @@ -53,7 +53,9 @@ pub trait WeightInfo { fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight; fn remove_delegation(r: u32, ) -> Weight; fn reclaim_deposit(r: u32, ) -> Weight; - fn is_delegating(r: u32, ) -> Weight; + fn can_attest(r: u32, ) -> Weight; + fn can_revoke(r: u32, ) -> Weight; + fn can_remove(r: u32, ) -> Weight; } /// Weights for delegation using the Substrate node and recommended hardware. @@ -124,7 +126,25 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) } // TODO: run benchmarks - fn is_delegating(r: u32, ) -> Weight { + fn can_attest(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } + fn can_revoke(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } + fn can_remove(r: u32, ) -> Weight { (61_171_000_u64) // Standard Error: 99_000 .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) @@ -185,14 +205,32 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().writes(2_u64)) .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) } - // TODO: run benchmarks - fn is_delegating(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(RocksDbWeight::get().writes(2_u64)) - .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) - } + // TODO: run benchmarks + fn can_attest(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } + fn can_revoke(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } + fn can_remove(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } } diff --git a/pallets/delegation/src/lib.rs b/pallets/delegation/src/lib.rs index 24408d19ea..ae303c721b 100644 --- a/pallets/delegation/src/lib.rs +++ b/pallets/delegation/src/lib.rs @@ -946,31 +946,28 @@ impl Pallet { #[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] pub struct DelegationAc(DelegationNodeIdOf, u32); -impl attestation::AttestationAccessControl, DelegationNodeIdOf> for DelegationAc -where - T: Config::AttesterId> + attestation::Config, -{ - fn can_attest(&self, who: &T::AttesterId) -> Result { +impl attestation::AttestationAccessControl, DelegationNodeIdOf> for DelegationAc { + fn can_attest(&self, who: &DelegatorIdOf) -> Result { match Pallet::::is_delegating(who, &self.0, self.1)? { - (true, checks) => Ok(::WeightInfo::is_delegating(checks)), + (true, checks) => Ok(::WeightInfo::can_attest(checks)), _ => Err(Error::::AccessDenied.into()), } } - fn can_revoke(&self, who: &T::AttesterId, auth_id: &DelegationNodeIdOf) -> Result { + fn can_revoke(&self, who: &DelegatorIdOf, auth_id: &DelegationNodeIdOf) -> Result { ensure!(auth_id == &self.0, Error::::AccessDenied); match Pallet::::is_delegating(who, &self.0, self.1)? { - (true, checks) => Ok(::WeightInfo::is_delegating(checks)), + (true, checks) => Ok(::WeightInfo::can_revoke(checks)), _ => Err(Error::::AccessDenied.into()), } } - fn can_remove(&self, who: &T::AttesterId, auth_id: &DelegationNodeIdOf) -> Result { + fn can_remove(&self, who: &DelegatorIdOf, auth_id: &DelegationNodeIdOf) -> Result { ensure!(auth_id == &self.0, Error::::AccessDenied); match Pallet::::is_delegating(who, &self.0, self.1)? { - (true, checks) => Ok(::WeightInfo::is_delegating(checks)), + (true, checks) => Ok(::WeightInfo::can_remove(checks)), _ => Err(Error::::AccessDenied.into()), } } @@ -980,6 +977,8 @@ where } fn weight(&self) -> Weight { - ::WeightInfo::is_delegating(self.1) + ::WeightInfo::can_attest(self.1) + .max(::WeightInfo::can_revoke(self.1)) + .max(::WeightInfo::can_remove(self.1)) } } diff --git a/runtimes/peregrine/src/weights/delegation.rs b/runtimes/peregrine/src/weights/delegation.rs index fb3e5fc01e..e247aafe01 100644 --- a/runtimes/peregrine/src/weights/delegation.rs +++ b/runtimes/peregrine/src/weights/delegation.rs @@ -114,13 +114,31 @@ impl delegation::WeightInfo for WeightInfo { } // TODO: run benchmarks - fn is_delegating(r: u32, ) -> Weight { - (53_098_000 as Weight) - // Standard Error: 75_000 - .saturating_add((39_041_000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) - .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) + fn can_attest(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } + fn can_revoke(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } + fn can_remove(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) } } diff --git a/runtimes/spiritnet/src/weights/delegation.rs b/runtimes/spiritnet/src/weights/delegation.rs index 2fbce009e9..712ab78c3b 100644 --- a/runtimes/spiritnet/src/weights/delegation.rs +++ b/runtimes/spiritnet/src/weights/delegation.rs @@ -114,13 +114,31 @@ impl delegation::WeightInfo for WeightInfo { } // TODO: run benchmarks - fn is_delegating(r: u32) -> Weight { - (53_370_000 as Weight) - // Standard Error: 126_000 - .saturating_add((39_346_000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) - .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) + fn can_attest(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } + fn can_revoke(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } + fn can_remove(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) } } From 94c6bde9d4c1a41fbe6056abd0db051bc9ae6317 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 25 Jan 2022 12:00:04 +0100 Subject: [PATCH 15/60] fmt --- runtimes/common/src/authorization.rs | 17 +++++++++++++++++ runtimes/common/src/lib.rs | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/runtimes/common/src/authorization.rs b/runtimes/common/src/authorization.rs index 37bb14d606..ae8c65b4f7 100644 --- a/runtimes/common/src/authorization.rs +++ b/runtimes/common/src/authorization.rs @@ -1,3 +1,20 @@ +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2022 BOTLabs GmbH + +// The KILT Blockchain is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The KILT Blockchain is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// If you feel like getting in touch with us, you can do so at info@botlabs.org use codec::{Decode, Encode}; use frame_support::dispatch::Weight; use scale_info::TypeInfo; diff --git a/runtimes/common/src/lib.rs b/runtimes/common/src/lib.rs index e33f6b02da..9ff9a63900 100644 --- a/runtimes/common/src/lib.rs +++ b/runtimes/common/src/lib.rs @@ -37,10 +37,10 @@ use sp_runtime::{ FixedPointNumber, MultiSignature, Perquintill, }; +pub mod authorization; pub mod constants; pub mod fees; pub mod pallet_id; -pub mod authorization; #[cfg(feature = "runtime-benchmarks")] pub mod benchmarks; From 988b43b9032158063e38280e57b83e9019b25427 Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Tue, 25 Jan 2022 13:15:54 +0100 Subject: [PATCH 16/60] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/peregrine/src/weights/delegation.rs --template=.maintain/runtime-weight-template.hbs --- runtimes/peregrine/src/weights/delegation.rs | 108 +++++++++---------- 1 file changed, 53 insertions(+), 55 deletions(-) diff --git a/runtimes/peregrine/src/weights/delegation.rs b/runtimes/peregrine/src/weights/delegation.rs index e247aafe01..22c7c949e2 100644 --- a/runtimes/peregrine/src/weights/delegation.rs +++ b/runtimes/peregrine/src/weights/delegation.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-11-08, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-01-25, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -36,7 +36,6 @@ // --output=./runtimes/peregrine/src/weights/delegation.rs // --template=.maintain/runtime-weight-template.hbs - #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] #![allow(unused_imports)] @@ -53,37 +52,35 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (48_478_000 as Weight) + (45_651_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (59_829_000 as Weight) + (54_268_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (23_761_000 as Weight) - // Standard Error: 52_000 - .saturating_add((19_062_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 52_000 - .saturating_add((43_000 as Weight).saturating_mul(c as Weight)) + (20_661_000 as Weight) + // Standard Error: 55_000 + .saturating_add((17_404_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 55_000 + .saturating_add((121_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) - fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (41_094_000 as Weight) - // Standard Error: 27_000 - .saturating_add((41_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 27_000 - .saturating_add((5_758_000 as Weight).saturating_mul(c as Weight)) + fn revoke_delegation_leaf(_r: u32, c: u32, ) -> Weight { + (38_367_000 as Weight) + // Standard Error: 32_000 + .saturating_add((5_464_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -92,53 +89,54 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (63_362_000 as Weight) - // Standard Error: 83_000 - .saturating_add((38_970_000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2 as Weight)) - .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) - .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) + (62_337_000 as Weight) + // Standard Error: 74_000 + .saturating_add((31_937_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (53_098_000 as Weight) - // Standard Error: 75_000 - .saturating_add((39_041_000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) - .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) + (53_133_000 as Weight) + // Standard Error: 72_000 + .saturating_add((32_084_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } - - // TODO: run benchmarks - fn can_attest(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + // Storage: Delegation DelegationNodes (r:6 w:0) + fn can_attest(r: u32, c: u32, ) -> Weight { + (8_620_000 as Weight) + // Standard Error: 20_000 + .saturating_add((39_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 20_000 + .saturating_add((5_387_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } - fn can_revoke(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + // Storage: Delegation DelegationNodes (r:6 w:0) + fn can_revoke(r: u32, c: u32, ) -> Weight { + (8_913_000 as Weight) + // Standard Error: 20_000 + .saturating_add((13_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 20_000 + .saturating_add((5_351_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } - fn can_remove(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + // Storage: Delegation DelegationNodes (r:6 w:0) + fn can_remove(r: u32, c: u32, ) -> Weight { + (8_903_000 as Weight) + // Standard Error: 21_000 + .saturating_add((22_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 21_000 + .saturating_add((5_356_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } } From 6946c2f429efe4f67acb869a226c27cdb60127e6 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 25 Jan 2022 13:21:35 +0100 Subject: [PATCH 17/60] Revert "cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/peregrine/src/weights/delegation.rs --template=.maintain/runtime-weight-template.hbs" This reverts commit 988b43b9032158063e38280e57b83e9019b25427. --- runtimes/peregrine/src/weights/delegation.rs | 108 ++++++++++--------- 1 file changed, 55 insertions(+), 53 deletions(-) diff --git a/runtimes/peregrine/src/weights/delegation.rs b/runtimes/peregrine/src/weights/delegation.rs index 22c7c949e2..e247aafe01 100644 --- a/runtimes/peregrine/src/weights/delegation.rs +++ b/runtimes/peregrine/src/weights/delegation.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-01-25, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-11-08, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -36,6 +36,7 @@ // --output=./runtimes/peregrine/src/weights/delegation.rs // --template=.maintain/runtime-weight-template.hbs + #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] #![allow(unused_imports)] @@ -52,35 +53,37 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (45_651_000 as Weight) + (48_478_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (54_268_000 as Weight) + (59_829_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (20_661_000 as Weight) - // Standard Error: 55_000 - .saturating_add((17_404_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 55_000 - .saturating_add((121_000 as Weight).saturating_mul(c as Weight)) + (23_761_000 as Weight) + // Standard Error: 52_000 + .saturating_add((19_062_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 52_000 + .saturating_add((43_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) - fn revoke_delegation_leaf(_r: u32, c: u32, ) -> Weight { - (38_367_000 as Weight) - // Standard Error: 32_000 - .saturating_add((5_464_000 as Weight).saturating_mul(c as Weight)) + fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { + (41_094_000 as Weight) + // Standard Error: 27_000 + .saturating_add((41_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 27_000 + .saturating_add((5_758_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -89,54 +92,53 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (62_337_000 as Weight) - // Standard Error: 74_000 - .saturating_add((31_937_000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(3 as Weight)) - .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) - .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) + (63_362_000 as Weight) + // Standard Error: 83_000 + .saturating_add((38_970_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) + .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (53_133_000 as Weight) - // Standard Error: 72_000 - .saturating_add((32_084_000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2 as Weight)) - .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) - .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) - } - // Storage: Delegation DelegationNodes (r:6 w:0) - fn can_attest(r: u32, c: u32, ) -> Weight { - (8_620_000 as Weight) - // Standard Error: 20_000 - .saturating_add((39_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 20_000 - .saturating_add((5_387_000 as Weight).saturating_mul(c as Weight)) + (53_098_000 as Weight) + // Standard Error: 75_000 + .saturating_add((39_041_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) + .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) + .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) } - // Storage: Delegation DelegationNodes (r:6 w:0) - fn can_revoke(r: u32, c: u32, ) -> Weight { - (8_913_000 as Weight) - // Standard Error: 20_000 - .saturating_add((13_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 20_000 - .saturating_add((5_351_000 as Weight).saturating_mul(c as Weight)) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) + + // TODO: run benchmarks + fn can_attest(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) } - // Storage: Delegation DelegationNodes (r:6 w:0) - fn can_remove(r: u32, c: u32, ) -> Weight { - (8_903_000 as Weight) - // Standard Error: 21_000 - .saturating_add((22_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 21_000 - .saturating_add((5_356_000 as Weight).saturating_mul(c as Weight)) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) + fn can_revoke(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + } + fn can_remove(r: u32, ) -> Weight { + (61_171_000_u64) + // Standard Error: 99_000 + .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) } } From 7fb5ae4725f61e765bb60b3995b74ac273849ee5 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 25 Jan 2022 13:21:55 +0100 Subject: [PATCH 18/60] bench: remove unused parameter --- pallets/delegation/src/benchmarking.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/pallets/delegation/src/benchmarking.rs b/pallets/delegation/src/benchmarking.rs index 887f3ab99f..17204bd6bb 100644 --- a/pallets/delegation/src/benchmarking.rs +++ b/pallets/delegation/src/benchmarking.rs @@ -380,7 +380,6 @@ benchmarks! { } can_attest { - let r in 1 .. T::MaxRevocations::get(); let c in 1 .. T::MaxParentChecks::get(); let sender: T::AccountId = account("sender", 0, SEED); @@ -394,7 +393,6 @@ benchmarks! { } can_revoke { - let r in 1 .. T::MaxRevocations::get(); let c in 1 .. T::MaxParentChecks::get(); let sender: T::AccountId = account("sender", 0, SEED); @@ -408,7 +406,6 @@ benchmarks! { } can_remove { - let r in 1 .. T::MaxRevocations::get(); let c in 1 .. T::MaxParentChecks::get(); let sender: T::AccountId = account("sender", 0, SEED); From 743cfb8e83da3a7f43cc53c90061710493802768 Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Tue, 25 Jan 2022 13:25:17 +0100 Subject: [PATCH 19/60] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=pallets/delegation/src/default_weights.rs --template=.maintain/weight-template.hbs --- pallets/delegation/src/default_weights.rs | 265 +++++++++++----------- 1 file changed, 136 insertions(+), 129 deletions(-) diff --git a/pallets/delegation/src/default_weights.rs b/pallets/delegation/src/default_weights.rs index c90487ef84..a8a3d7c75d 100644 --- a/pallets/delegation/src/default_weights.rs +++ b/pallets/delegation/src/default_weights.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-11-08, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ +//! DATE: 2022-01-25, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -36,7 +36,6 @@ // --output=pallets/delegation/src/default_weights.rs // --template=.maintain/weight-template.hbs - #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] #![allow(unused_imports)] @@ -53,9 +52,9 @@ pub trait WeightInfo { fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight; fn remove_delegation(r: u32, ) -> Weight; fn reclaim_deposit(r: u32, ) -> Weight; - fn can_attest(r: u32, ) -> Weight; - fn can_revoke(r: u32, ) -> Weight; - fn can_remove(r: u32, ) -> Weight; + fn can_attest(c: u32, ) -> Weight; + fn can_revoke(c: u32, ) -> Weight; + fn can_remove(c: u32, ) -> Weight; } /// Weights for delegation using the Substrate node and recommended hardware. @@ -66,171 +65,179 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (47_416_000_u64) - .saturating_add(T::DbWeight::get().reads(3_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + (46_379_000 as Weight) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (57_220_000_u64) - .saturating_add(T::DbWeight::get().reads(3_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + (54_088_000 as Weight) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (22_152_000_u64) + (20_970_000 as Weight) // Standard Error: 55_000 - .saturating_add((18_681_000_u64).saturating_mul(r as Weight)) + .saturating_add((17_347_000 as Weight).saturating_mul(r as Weight)) // Standard Error: 55_000 - .saturating_add((95_000_u64).saturating_mul(c as Weight)) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(r as Weight))) + .saturating_add((66_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (39_250_000_u64) - // Standard Error: 34_000 - .saturating_add((147_000_u64).saturating_mul(r as Weight)) - // Standard Error: 34_000 - .saturating_add((5_748_000_u64).saturating_mul(c as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c as Weight))) - .saturating_add(T::DbWeight::get().writes(1_u64)) + (37_590_000 as Weight) + // Standard Error: 27_000 + .saturating_add((65_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 27_000 + .saturating_add((5_529_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + (62_289_000 as Weight) + // Standard Error: 69_000 + .saturating_add((31_560_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (49_884_000_u64) - // Standard Error: 64_000 - .saturating_add((38_418_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) - } - // TODO: run benchmarks - fn can_attest(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) - } - fn can_revoke(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) - } - fn can_remove(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + (53_477_000 as Weight) + // Standard Error: 79_000 + .saturating_add((31_743_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) + } + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_attest(c: u32, ) -> Weight { + (8_676_000 as Weight) + // Standard Error: 28_000 + .saturating_add((5_522_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) + } + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_revoke(c: u32, ) -> Weight { + (8_730_000 as Weight) + // Standard Error: 25_000 + .saturating_add((5_519_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) + } + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_remove(c: u32, ) -> Weight { + (8_897_000 as Weight) + // Standard Error: 28_000 + .saturating_add((5_451_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } } // For backwards compatibility and tests impl WeightInfo for () { + // Storage: Delegation DelegationHierarchies (r:1 w:1) + // Storage: Ctype Ctypes (r:1 w:0) + // Storage: System Account (r:1 w:1) + // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (47_416_000_u64) - .saturating_add(RocksDbWeight::get().reads(3_u64)) - .saturating_add(RocksDbWeight::get().writes(3_u64)) + (46_379_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(3 as Weight)) + .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } + // Storage: Delegation DelegationNodes (r:2 w:2) + // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (57_220_000_u64) - .saturating_add(RocksDbWeight::get().reads(3_u64)) - .saturating_add(RocksDbWeight::get().writes(3_u64)) + (54_088_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(3 as Weight)) + .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } + // Storage: Delegation DelegationNodes (r:1 w:1) + // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (22_152_000_u64) + (20_970_000 as Weight) // Standard Error: 55_000 - .saturating_add((18_681_000_u64).saturating_mul(r as Weight)) + .saturating_add((17_347_000 as Weight).saturating_mul(r as Weight)) // Standard Error: 55_000 - .saturating_add((95_000_u64).saturating_mul(c as Weight)) - .saturating_add(RocksDbWeight::get().reads(1_u64)) - .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(r as Weight))) - .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(r as Weight))) + .saturating_add((66_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(RocksDbWeight::get().reads(1 as Weight)) + .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } + // Storage: Delegation DelegationNodes (r:6 w:1) + // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (39_250_000_u64) - // Standard Error: 34_000 - .saturating_add((147_000_u64).saturating_mul(r as Weight)) - // Standard Error: 34_000 - .saturating_add((5_748_000_u64).saturating_mul(c as Weight)) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c as Weight))) - .saturating_add(RocksDbWeight::get().writes(1_u64)) + (37_590_000 as Weight) + // Standard Error: 27_000 + .saturating_add((65_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 27_000 + .saturating_add((5_529_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } + // Storage: Delegation DelegationNodes (r:2 w:2) + // Storage: System Account (r:1 w:1) + // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(RocksDbWeight::get().writes(2_u64)) - .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + (62_289_000 as Weight) + // Standard Error: 69_000 + .saturating_add((31_560_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(RocksDbWeight::get().reads(3 as Weight)) + .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().writes(3 as Weight)) + .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } + // Storage: Delegation DelegationNodes (r:2 w:2) + // Storage: System Account (r:1 w:1) + // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (49_884_000_u64) - // Standard Error: 64_000 - .saturating_add((38_418_000_u64).saturating_mul(r as Weight)) - .saturating_add(RocksDbWeight::get().reads(1_u64)) - .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(RocksDbWeight::get().writes(2_u64)) - .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) - } - // TODO: run benchmarks - fn can_attest(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(RocksDbWeight::get().writes(2_u64)) - .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) - } - fn can_revoke(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(RocksDbWeight::get().writes(2_u64)) - .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) - } - fn can_remove(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(RocksDbWeight::get().writes(2_u64)) - .saturating_add(RocksDbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + (53_477_000 as Weight) + // Standard Error: 79_000 + .saturating_add((31_743_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(RocksDbWeight::get().writes(3 as Weight)) + .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) + } + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_attest(c: u32, ) -> Weight { + (8_676_000 as Weight) + // Standard Error: 28_000 + .saturating_add((5_522_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(RocksDbWeight::get().reads(1 as Weight)) + .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) + } + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_revoke(c: u32, ) -> Weight { + (8_730_000 as Weight) + // Standard Error: 25_000 + .saturating_add((5_519_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(RocksDbWeight::get().reads(1 as Weight)) + .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) + } + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_remove(c: u32, ) -> Weight { + (8_897_000 as Weight) + // Standard Error: 28_000 + .saturating_add((5_451_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(RocksDbWeight::get().reads(1 as Weight)) + .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } } From 2ab2a2cd603e601e62aba713998d09aa84ded74a Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Tue, 25 Jan 2022 13:28:27 +0100 Subject: [PATCH 20/60] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=pallets/delegation/src/default_weights.rs --template=.maintain/weight-template.hbs --- pallets/delegation/src/default_weights.rs | 108 +++++++++++----------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/pallets/delegation/src/default_weights.rs b/pallets/delegation/src/default_weights.rs index a8a3d7c75d..0c984d2770 100644 --- a/pallets/delegation/src/default_weights.rs +++ b/pallets/delegation/src/default_weights.rs @@ -65,25 +65,25 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (46_379_000 as Weight) + (46_899_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (54_088_000 as Weight) + (55_555_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (20_970_000 as Weight) - // Standard Error: 55_000 - .saturating_add((17_347_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 55_000 - .saturating_add((66_000 as Weight).saturating_mul(c as Weight)) + (22_158_000 as Weight) + // Standard Error: 56_000 + .saturating_add((17_812_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 56_000 + .saturating_add((65_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) @@ -91,11 +91,11 @@ impl WeightInfo for SubstrateWeight { // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (37_590_000 as Weight) - // Standard Error: 27_000 - .saturating_add((65_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 27_000 - .saturating_add((5_529_000 as Weight).saturating_mul(c as Weight)) + (39_004_000 as Weight) + // Standard Error: 31_000 + .saturating_add((101_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 31_000 + .saturating_add((5_945_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -104,9 +104,9 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (62_289_000 as Weight) - // Standard Error: 69_000 - .saturating_add((31_560_000 as Weight).saturating_mul(r as Weight)) + (64_475_000 as Weight) + // Standard Error: 68_000 + .saturating_add((32_285_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -116,9 +116,9 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (53_477_000 as Weight) - // Standard Error: 79_000 - .saturating_add((31_743_000 as Weight).saturating_mul(r as Weight)) + (55_017_000 as Weight) + // Standard Error: 84_000 + .saturating_add((32_384_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -126,25 +126,25 @@ impl WeightInfo for SubstrateWeight { } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_attest(c: u32, ) -> Weight { - (8_676_000 as Weight) - // Standard Error: 28_000 - .saturating_add((5_522_000 as Weight).saturating_mul(c as Weight)) + (9_002_000 as Weight) + // Standard Error: 26_000 + .saturating_add((5_910_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { - (8_730_000 as Weight) - // Standard Error: 25_000 - .saturating_add((5_519_000 as Weight).saturating_mul(c as Weight)) + (9_119_000 as Weight) + // Standard Error: 36_000 + .saturating_add((5_889_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_remove(c: u32, ) -> Weight { - (8_897_000 as Weight) - // Standard Error: 28_000 - .saturating_add((5_451_000 as Weight).saturating_mul(c as Weight)) + (9_343_000 as Weight) + // Standard Error: 30_000 + .saturating_add((5_832_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } @@ -157,25 +157,25 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (46_379_000 as Weight) + (46_899_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (54_088_000 as Weight) + (55_555_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (20_970_000 as Weight) - // Standard Error: 55_000 - .saturating_add((17_347_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 55_000 - .saturating_add((66_000 as Weight).saturating_mul(c as Weight)) + (22_158_000 as Weight) + // Standard Error: 56_000 + .saturating_add((17_812_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 56_000 + .saturating_add((65_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) @@ -183,11 +183,11 @@ impl WeightInfo for () { // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (37_590_000 as Weight) - // Standard Error: 27_000 - .saturating_add((65_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 27_000 - .saturating_add((5_529_000 as Weight).saturating_mul(c as Weight)) + (39_004_000 as Weight) + // Standard Error: 31_000 + .saturating_add((101_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 31_000 + .saturating_add((5_945_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) @@ -196,9 +196,9 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (62_289_000 as Weight) - // Standard Error: 69_000 - .saturating_add((31_560_000 as Weight).saturating_mul(r as Weight)) + (64_475_000 as Weight) + // Standard Error: 68_000 + .saturating_add((32_285_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) @@ -208,9 +208,9 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (53_477_000 as Weight) - // Standard Error: 79_000 - .saturating_add((31_743_000 as Weight).saturating_mul(r as Weight)) + (55_017_000 as Weight) + // Standard Error: 84_000 + .saturating_add((32_384_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) @@ -218,25 +218,25 @@ impl WeightInfo for () { } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_attest(c: u32, ) -> Weight { - (8_676_000 as Weight) - // Standard Error: 28_000 - .saturating_add((5_522_000 as Weight).saturating_mul(c as Weight)) + (9_002_000 as Weight) + // Standard Error: 26_000 + .saturating_add((5_910_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { - (8_730_000 as Weight) - // Standard Error: 25_000 - .saturating_add((5_519_000 as Weight).saturating_mul(c as Weight)) + (9_119_000 as Weight) + // Standard Error: 36_000 + .saturating_add((5_889_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_remove(c: u32, ) -> Weight { - (8_897_000 as Weight) - // Standard Error: 28_000 - .saturating_add((5_451_000 as Weight).saturating_mul(c as Weight)) + (9_343_000 as Weight) + // Standard Error: 30_000 + .saturating_add((5_832_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } From a5cf8c9e9a5b5f3478c8648eacf2fa002dd3740b Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Tue, 25 Jan 2022 13:31:36 +0100 Subject: [PATCH 21/60] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=spiritnet-dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/spiritnet/src/weights/delegation.rs --template=.maintain/runtime-weight-template.hbs --- runtimes/spiritnet/src/weights/delegation.rs | 102 +++++++++---------- 1 file changed, 47 insertions(+), 55 deletions(-) diff --git a/runtimes/spiritnet/src/weights/delegation.rs b/runtimes/spiritnet/src/weights/delegation.rs index 712ab78c3b..9bd8bf0de1 100644 --- a/runtimes/spiritnet/src/weights/delegation.rs +++ b/runtimes/spiritnet/src/weights/delegation.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-11-08, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-01-25, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("spiritnet-dev"), DB CACHE: 128 // Executed Command: @@ -36,7 +36,6 @@ // --output=./runtimes/spiritnet/src/weights/delegation.rs // --template=.maintain/runtime-weight-template.hbs - #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] #![allow(unused_imports)] @@ -53,37 +52,35 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (48_311_000 as Weight) + (45_498_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (59_067_000 as Weight) + (53_812_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (21_963_000 as Weight) - // Standard Error: 46_000 - .saturating_add((19_693_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 46_000 - .saturating_add((139_000 as Weight).saturating_mul(c as Weight)) + (20_242_000 as Weight) + // Standard Error: 52_000 + .saturating_add((17_467_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 52_000 + .saturating_add((179_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) - fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (41_137_000 as Weight) - // Standard Error: 29_000 - .saturating_add((108_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 29_000 - .saturating_add((6_051_000 as Weight).saturating_mul(c as Weight)) + fn revoke_delegation_leaf(_r: u32, c: u32, ) -> Weight { + (38_384_000 as Weight) + // Standard Error: 28_000 + .saturating_add((5_370_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -92,53 +89,48 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (62_336_000 as Weight) - // Standard Error: 87_000 - .saturating_add((39_220_000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2 as Weight)) - .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) - .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) + (62_215_000 as Weight) + // Standard Error: 94_000 + .saturating_add((31_974_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (53_370_000 as Weight) - // Standard Error: 126_000 - .saturating_add((39_346_000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) - .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) + (53_556_000 as Weight) + // Standard Error: 76_000 + .saturating_add((31_906_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } - - // TODO: run benchmarks - fn can_attest(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_attest(c: u32, ) -> Weight { + (8_421_000 as Weight) + // Standard Error: 18_000 + .saturating_add((5_462_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } - fn can_revoke(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_revoke(c: u32, ) -> Weight { + (8_605_000 as Weight) + // Standard Error: 18_000 + .saturating_add((5_452_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } - fn can_remove(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_remove(c: u32, ) -> Weight { + (8_663_000 as Weight) + // Standard Error: 22_000 + .saturating_add((5_427_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } } From b97935c11f8ec4799000b32db8b8ce5bf9a50cb6 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 25 Jan 2022 13:59:03 +0100 Subject: [PATCH 22/60] =?UTF-8?q?=F0=9F=A7=B9=F0=9F=90=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pallets/attestation/src/benchmarking.rs | 8 ++++---- pallets/attestation/src/lib.rs | 6 +++--- pallets/attestation/src/tests.rs | 16 ++++++++-------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pallets/attestation/src/benchmarking.rs b/pallets/attestation/src/benchmarking.rs index efc21be412..e00a6d29a7 100644 --- a/pallets/attestation/src/benchmarking.rs +++ b/pallets/attestation/src/benchmarking.rs @@ -42,7 +42,7 @@ benchmarks! { let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); - ctype::Ctypes::::insert(&ctype_hash, T::CtypeCreatorId::from(attester.clone())); + ctype::Ctypes::::insert(&ctype_hash, attester.clone()); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); @@ -67,7 +67,7 @@ benchmarks! { let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); - ctype::Ctypes::::insert(&ctype_hash, T::CtypeCreatorId::from(attester.clone())); + ctype::Ctypes::::insert(&ctype_hash, attester.clone()); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); @@ -93,7 +93,7 @@ benchmarks! { let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); - ctype::Ctypes::::insert(&ctype_hash, T::CtypeCreatorId::from(attester.clone())); + ctype::Ctypes::::insert(&ctype_hash, attester.clone()); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester.clone()); @@ -110,7 +110,7 @@ benchmarks! { let claim_hash: T::Hash = T::Hashing::hash(b"claim"); let ctype_hash: T::Hash = T::Hash::default(); - ctype::Ctypes::::insert(&ctype_hash, T::CtypeCreatorId::from(attester.clone())); + ctype::Ctypes::::insert(&ctype_hash, attester.clone()); ::Currency::make_free_balance_be(&sender, ::Deposit::get() + ::Deposit::get()); let origin = ::EnsureOrigin::generate_origin(sender.clone(), attester); diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 7616a402ac..0796d457af 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -311,7 +311,7 @@ pub mod pallet { &claim_hash, AttestationDetails { ctype_hash, - attester: who.clone(), + attester: who, authorization_id, revoked: false, deposit, @@ -358,7 +358,7 @@ pub mod pallet { let expected_auth = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; authorization .ok_or(Error::::Unauthorized)? - .can_revoke(&who, &expected_auth)?; + .can_revoke(&who, expected_auth)?; } // *** No Fail beyond this point *** @@ -412,7 +412,7 @@ pub mod pallet { let expected_auth = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; authorization .ok_or(Error::::Unauthorized)? - .can_remove(&who, &expected_auth)?; + .can_remove(&who, expected_auth)?; } // *** No Fail beyond this point *** diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index f93fe5c373..d89889a56b 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -99,7 +99,7 @@ fn test_attest_unauthorized() { let bob: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ctype = get_ctype_hash::(true); - let authorization_id = Some(MockAccessControl(bob.clone())); + let authorization_id = Some(MockAccessControl(bob)); ExtBuilder::default() .with_ctypes(vec![(ctype, attester.clone())]) @@ -148,7 +148,7 @@ fn test_attest_already_exists() { ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(attestation.ctype_hash.clone(), attester.clone())]) + .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) .with_attestations(vec![(claim_hash, attestation.clone())]) .build() .execute_with(|| { @@ -211,7 +211,7 @@ fn test_authorized_revoke() { ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) + .with_ctypes(vec![(attestation.ctype_hash, attester)]) .with_attestations(vec![(claim_hash, attestation)]) .build() .execute_with(|| { @@ -237,17 +237,17 @@ fn test_unauthorized_revoke() { let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let authorization_id = Some(MockAccessControl(revoker.clone())); let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - attestation.authorization_id = Some(revoker.clone()); + attestation.authorization_id = Some(revoker); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) + .with_ctypes(vec![(attestation.ctype_hash, attester)]) .with_attestations(vec![(claim_hash, attestation)]) .build() .execute_with(|| { assert_noop!( Attestation::revoke( - DoubleOrigin(ACCOUNT_00, evil.clone()).into(), + DoubleOrigin(ACCOUNT_00, evil).into(), claim_hash, authorization_id ), @@ -336,7 +336,7 @@ fn test_remove_authorized() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let revoker: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); + let mut attestation = generate_base_attestation::(attester, ACCOUNT_00); attestation.authorization_id = Some(revoker.clone()); let authorization_id = Some(MockAccessControl(revoker.clone())); @@ -365,7 +365,7 @@ fn test_remove_unauthorised() { ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(attestation.ctype_hash, attester.clone())]) + .with_ctypes(vec![(attestation.ctype_hash, attester)]) .with_attestations(vec![(claim_hash, attestation)]) .build() .execute_with(|| { From 74516728376a067b9f098e1f725792a527ac0df9 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 25 Jan 2022 14:15:58 +0100 Subject: [PATCH 23/60] =?UTF-8?q?=F0=9F=A7=B9=F0=9F=90=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pallets/attestation/src/tests.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index d89889a56b..9b199daffb 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -246,11 +246,7 @@ fn test_unauthorized_revoke() { .build() .execute_with(|| { assert_noop!( - Attestation::revoke( - DoubleOrigin(ACCOUNT_00, evil).into(), - claim_hash, - authorization_id - ), + Attestation::revoke(DoubleOrigin(ACCOUNT_00, evil).into(), claim_hash, authorization_id), DispatchError::Other("Unauthorized") ); }); From 8533e02302bae086eae4ba46c709413e9f1cfe31 Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 27 Jan 2022 14:32:42 +0100 Subject: [PATCH 24/60] refactor: move delegationAc to mod --- pallets/delegation/src/access_control.rs | 80 ++++++++++++++++++++++++ pallets/delegation/src/lib.rs | 43 +------------ 2 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 pallets/delegation/src/access_control.rs diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs new file mode 100644 index 0000000000..05c9221d1d --- /dev/null +++ b/pallets/delegation/src/access_control.rs @@ -0,0 +1,80 @@ +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2022 BOTLabs GmbH + +// The KILT Blockchain is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The KILT Blockchain is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// If you feel like getting in touch with us, you can do so at info@botlabs.org + +use codec::{Decode, Encode}; +use frame_support::{dispatch::Weight, ensure}; +use scale_info::TypeInfo; +use sp_runtime::DispatchError; + +use crate::{Config, DelegationNodeIdOf, DelegatorIdOf, Error, Pallet, default_weights::WeightInfo}; + +/// Controls the access to attestations. +/// +/// Can attest if all conditions are fulfilled +/// * delegation node of sender is not revoked +/// * delegation node of sender has ATTEST permission +/// * the CType of the delegation root matches the CType of the attestation +/// +/// Can revoke if all conditions are fulfilled +/// * sender delegation node is not revoked +/// * sender delegation node is equal to OR parent of the delegation node +/// stored in the attestation +/// +/// Can revoke if all conditions are fulfilled +/// * sender delegation node is not revoked +/// * sender delegation node is equal to OR parent of the delegation node +/// stored in the attestation +#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] +pub struct DelegationAc(pub(crate) DelegationNodeIdOf, pub(crate) u32); + +impl attestation::AttestationAccessControl, DelegationNodeIdOf> for DelegationAc { + fn can_attest(&self, who: &DelegatorIdOf) -> Result { + match Pallet::::is_delegating(who, &self.0, self.1)? { + (true, checks) => Ok(::WeightInfo::can_attest(checks)), + _ => Err(Error::::AccessDenied.into()), + } + } + + fn can_revoke(&self, who: &DelegatorIdOf, auth_id: &DelegationNodeIdOf) -> Result { + ensure!(auth_id == &self.0, Error::::AccessDenied); + + match Pallet::::is_delegating(who, &self.0, self.1)? { + (true, checks) => Ok(::WeightInfo::can_revoke(checks)), + _ => Err(Error::::AccessDenied.into()), + } + } + + fn can_remove(&self, who: &DelegatorIdOf, auth_id: &DelegationNodeIdOf) -> Result { + ensure!(auth_id == &self.0, Error::::AccessDenied); + + match Pallet::::is_delegating(who, &self.0, self.1)? { + (true, checks) => Ok(::WeightInfo::can_remove(checks)), + _ => Err(Error::::AccessDenied.into()), + } + } + + fn authorization_id(&self) -> DelegationNodeIdOf { + self.0 + } + + fn weight(&self) -> Weight { + ::WeightInfo::can_attest(self.1) + .max(::WeightInfo::can_revoke(self.1)) + .max(::WeightInfo::can_remove(self.1)) + } +} diff --git a/pallets/delegation/src/lib.rs b/pallets/delegation/src/lib.rs index ae303c721b..ef52a40e19 100644 --- a/pallets/delegation/src/lib.rs +++ b/pallets/delegation/src/lib.rs @@ -76,6 +76,7 @@ pub mod default_weights; pub mod delegation_hierarchy; pub mod migrations; +mod access_control; #[cfg(any(feature = "mock", test))] pub mod mock; @@ -86,7 +87,8 @@ pub mod benchmarking; #[cfg(test)] mod tests; -pub use crate::{default_weights::WeightInfo, delegation_hierarchy::*, pallet::*}; + +pub use crate::{access_control::DelegationAc, default_weights::WeightInfo, delegation_hierarchy::*, pallet::*}; use codec::{Decode, Encode}; use frame_support::{ @@ -943,42 +945,3 @@ impl Pallet { Ok((removals, consumed_weight)) } } - -#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] -pub struct DelegationAc(DelegationNodeIdOf, u32); -impl attestation::AttestationAccessControl, DelegationNodeIdOf> for DelegationAc { - fn can_attest(&self, who: &DelegatorIdOf) -> Result { - match Pallet::::is_delegating(who, &self.0, self.1)? { - (true, checks) => Ok(::WeightInfo::can_attest(checks)), - _ => Err(Error::::AccessDenied.into()), - } - } - - fn can_revoke(&self, who: &DelegatorIdOf, auth_id: &DelegationNodeIdOf) -> Result { - ensure!(auth_id == &self.0, Error::::AccessDenied); - - match Pallet::::is_delegating(who, &self.0, self.1)? { - (true, checks) => Ok(::WeightInfo::can_revoke(checks)), - _ => Err(Error::::AccessDenied.into()), - } - } - - fn can_remove(&self, who: &DelegatorIdOf, auth_id: &DelegationNodeIdOf) -> Result { - ensure!(auth_id == &self.0, Error::::AccessDenied); - - match Pallet::::is_delegating(who, &self.0, self.1)? { - (true, checks) => Ok(::WeightInfo::can_remove(checks)), - _ => Err(Error::::AccessDenied.into()), - } - } - - fn authorization_id(&self) -> DelegationNodeIdOf { - self.0 - } - - fn weight(&self) -> Weight { - ::WeightInfo::can_attest(self.1) - .max(::WeightInfo::can_revoke(self.1)) - .max(::WeightInfo::can_remove(self.1)) - } -} From f963bfc68a55d148fdd32e950d153deece27c2af Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 27 Jan 2022 14:32:51 +0100 Subject: [PATCH 25/60] fix: saturating add --- pallets/attestation/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 0796d457af..d2a214299a 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -269,7 +269,7 @@ pub mod pallet { /// # #[pallet::weight( ::WeightInfo::add() - + authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0) + .saturating_add(authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0)) )] pub fn add( origin: OriginFor, @@ -340,7 +340,7 @@ pub mod pallet { /// # #[pallet::weight( ::WeightInfo::revoke() - + authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0) + .saturating_add(authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0)) )] pub fn revoke( origin: OriginFor, @@ -396,7 +396,7 @@ pub mod pallet { /// # #[pallet::weight( ::WeightInfo::remove() - + authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0) + .saturating_add(authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0)) )] pub fn remove( origin: OriginFor, From 466df1db15a9175fc2bd7081bf34e01b5eee0bed Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 27 Jan 2022 14:54:13 +0100 Subject: [PATCH 26/60] refactor: move AttestationAccessControl to mod --- pallets/attestation/src/access_control.rs | 68 +++++++++++++++++++++++ pallets/attestation/src/lib.rs | 36 ++---------- pallets/delegation/src/lib.rs | 6 +- 3 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 pallets/attestation/src/access_control.rs diff --git a/pallets/attestation/src/access_control.rs b/pallets/attestation/src/access_control.rs new file mode 100644 index 0000000000..fb2e8fec0e --- /dev/null +++ b/pallets/attestation/src/access_control.rs @@ -0,0 +1,68 @@ +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2022 BOTLabs GmbH + +// The KILT Blockchain is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The KILT Blockchain is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// If you feel like getting in touch with us, you can do so at info@botlabs.org + +use frame_support::dispatch::Weight; +use sp_runtime::DispatchError; + +/// Allow for more complex schemes on who can attest, revoke and remove. +pub trait AttestationAccessControl { + /// Decides whether the account is allowed to attest with the given + /// information provided by the sender (&self). + fn can_attest(&self, who: &AttesterId) -> Result; + + /// Decides whether the account is allowed to revoke the attestation with + /// the `authorization_id` and the access information provided by the sender + /// (&self). + fn can_revoke(&self, who: &AttesterId, authorization_id: &AuthorizationId) -> Result; + + /// Decides whether the account is allowed to remove the attestation with + /// the `authorization_id` and the access information provided by the sender + /// (&self). + fn can_remove(&self, who: &AttesterId, authorization_id: &AuthorizationId) -> Result; + + /// The authorization ID that the sender provided. This will be used for new + /// attestations. + /// + /// NOTE: This method must not read storage or do any heavy computation + /// since it's not covered by the weight returned by `self.weight()`. + fn authorization_id(&self) -> AuthorizationId; + + /// The maximum weight of `can_attest`, `can_revoke` and `can_remove`. + fn weight(&self) -> Weight; +} + +impl AttestationAccessControl for () +where + AuthorizationId: Default, +{ + fn can_attest(&self, _who: &AttesterId) -> Result { + Err(DispatchError::Other("Unimplemented")) + } + fn can_revoke(&self, _who: &AttesterId, _authorization_id: &AuthorizationId) -> Result { + Err(DispatchError::Other("Unimplemented")) + } + fn can_remove(&self, _who: &AttesterId, _authorization_id: &AuthorizationId) -> Result { + Err(DispatchError::Other("Unimplemented")) + } + fn authorization_id(&self) -> AuthorizationId { + Default::default() + } + fn weight(&self) -> Weight { + 0 + } +} diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index d2a214299a..43f7f75f18 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -82,16 +82,18 @@ pub mod mock; #[cfg(feature = "runtime-benchmarks")] pub mod benchmarking; +mod access_control; #[cfg(test)] mod tests; -pub use crate::{attestations::AttestationDetails, default_weights::WeightInfo, pallet::*}; +pub use crate::{ + access_control::AttestationAccessControl, attestations::AttestationDetails, default_weights::WeightInfo, pallet::*, +}; #[frame_support::pallet] pub mod pallet { use super::*; use frame_support::{ - dispatch::Weight, pallet_prelude::*, traits::{Currency, Get, ReservableCurrency}, BoundedVec, @@ -101,36 +103,6 @@ pub mod pallet { use ctype::CtypeHashOf; use kilt_support::{deposit::Deposit, traits::CallSources}; - - pub trait AttestationAccessControl { - fn can_attest(&self, who: &AttesterId) -> Result; - fn can_revoke(&self, who: &AttesterId, authorization_id: &AuthorizationId) -> Result; - fn can_remove(&self, who: &AttesterId, authorization_id: &AuthorizationId) -> Result; - fn authorization_id(&self) -> AuthorizationId; - fn weight(&self) -> Weight; - } - - impl AttestationAccessControl for () - where - AuthorizationId: Default, - { - fn can_attest(&self, _who: &AttesterId) -> Result { - Err(DispatchError::Other("Unimplemented")) - } - fn can_revoke(&self, _who: &AttesterId, _authorization_id: &AuthorizationId) -> Result { - Err(DispatchError::Other("Unimplemented")) - } - fn can_remove(&self, _who: &AttesterId, _authorization_id: &AuthorizationId) -> Result { - Err(DispatchError::Other("Unimplemented")) - } - fn authorization_id(&self) -> AuthorizationId { - Default::default() - } - fn weight(&self) -> Weight { - 0 - } - } - /// Type of a claim hash. pub(crate) type ClaimHashOf = ::Hash; diff --git a/pallets/delegation/src/lib.rs b/pallets/delegation/src/lib.rs index ef52a40e19..d31c8497f8 100644 --- a/pallets/delegation/src/lib.rs +++ b/pallets/delegation/src/lib.rs @@ -73,10 +73,10 @@ #![cfg_attr(not(feature = "std"), no_std)] #![allow(clippy::unused_unit)] +mod access_control; pub mod default_weights; pub mod delegation_hierarchy; pub mod migrations; -mod access_control; #[cfg(any(feature = "mock", test))] pub mod mock; @@ -87,10 +87,9 @@ pub mod benchmarking; #[cfg(test)] mod tests; - pub use crate::{access_control::DelegationAc, default_weights::WeightInfo, delegation_hierarchy::*, pallet::*}; -use codec::{Decode, Encode}; +use codec::Encode; use frame_support::{ dispatch::DispatchResult, ensure, @@ -98,7 +97,6 @@ use frame_support::{ traits::{Get, ReservableCurrency}, }; use migrations::DelegationStorageVersion; -use scale_info::TypeInfo; use sp_runtime::{traits::Hash, DispatchError}; use sp_std::vec::Vec; From a48fa14f94de4b29ff8560d8a1a1dcd27f9af6e7 Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 27 Jan 2022 15:10:41 +0100 Subject: [PATCH 27/60] style: NL for the devil --- pallets/attestation/src/mock.rs | 1 + pallets/delegation/src/access_control.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index 5bde441059..a3188569d4 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -77,6 +77,7 @@ where #[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq, Eq)] #[scale_info(skip_type_params(T))] pub struct MockAccessControl(pub T::AttesterId); + impl AttestationAccessControl for MockAccessControl where T: Config::AttesterId>, diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index 05c9221d1d..b99d7a202e 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -21,7 +21,7 @@ use frame_support::{dispatch::Weight, ensure}; use scale_info::TypeInfo; use sp_runtime::DispatchError; -use crate::{Config, DelegationNodeIdOf, DelegatorIdOf, Error, Pallet, default_weights::WeightInfo}; +use crate::{default_weights::WeightInfo, Config, DelegationNodeIdOf, DelegatorIdOf, Error, Pallet}; /// Controls the access to attestations. /// From 89800d909b9524c4177c9fbc914960e725a4f836 Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 27 Jan 2022 15:16:26 +0100 Subject: [PATCH 28/60] style: rename to `attestation_auth_id` --- pallets/attestation/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 43f7f75f18..d4496d0f21 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -327,10 +327,10 @@ pub mod pallet { ensure!(!attestation.revoked, Error::::AlreadyRevoked); if attestation.attester != who { - let expected_auth = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; + let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; authorization .ok_or(Error::::Unauthorized)? - .can_revoke(&who, expected_auth)?; + .can_revoke(&who, attestation_auth_id)?; } // *** No Fail beyond this point *** @@ -381,10 +381,10 @@ pub mod pallet { let attestation = Attestations::::get(&claim_hash).ok_or(Error::::AttestationNotFound)?; if attestation.attester != who { - let expected_auth = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; + let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; authorization .ok_or(Error::::Unauthorized)? - .can_remove(&who, expected_auth)?; + .can_remove(&who, attestation_auth_id)?; } // *** No Fail beyond this point *** From 7eec4c597f8d86de567ccc04a0e1416772984922 Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 27 Jan 2022 15:18:09 +0100 Subject: [PATCH 29/60] doc: better words --- pallets/delegation/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/delegation/src/lib.rs b/pallets/delegation/src/lib.rs index d31c8497f8..f71fd954f7 100644 --- a/pallets/delegation/src/lib.rs +++ b/pallets/delegation/src/lib.rs @@ -273,7 +273,7 @@ pub mod pallet { UnauthorizedRemoval, /// The delegation creator is not allowed to create the delegation. UnauthorizedDelegation, - /// The access was not granted because of an insufficient delegation. + /// The operation wasn't allowed because of insufficient rights. AccessDenied, /// Max number of revocations for delegation nodes has been reached for /// the operation. From 0edb89197fabd3fe439708dc751866187125c599 Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 27 Jan 2022 18:19:43 +0100 Subject: [PATCH 30/60] add ctype and claimhash --- pallets/attestation/src/access_control.rs | 41 ++++++++++--- pallets/attestation/src/lib.rs | 28 ++++++--- pallets/attestation/src/mock.rs | 26 ++++++-- pallets/delegation/src/access_control.rs | 75 +++++++++++++++++++++-- pallets/delegation/src/benchmarking.rs | 15 ++++- runtimes/common/src/authorization.rs | 29 ++++++--- 6 files changed, 177 insertions(+), 37 deletions(-) diff --git a/pallets/attestation/src/access_control.rs b/pallets/attestation/src/access_control.rs index fb2e8fec0e..58de9eae51 100644 --- a/pallets/attestation/src/access_control.rs +++ b/pallets/attestation/src/access_control.rs @@ -20,20 +20,32 @@ use frame_support::dispatch::Weight; use sp_runtime::DispatchError; /// Allow for more complex schemes on who can attest, revoke and remove. -pub trait AttestationAccessControl { +pub trait AttestationAccessControl { /// Decides whether the account is allowed to attest with the given /// information provided by the sender (&self). - fn can_attest(&self, who: &AttesterId) -> Result; + fn can_attest(&self, who: &AttesterId, ctype: &Ctype, claim: &ClaimHash) -> Result; /// Decides whether the account is allowed to revoke the attestation with /// the `authorization_id` and the access information provided by the sender /// (&self). - fn can_revoke(&self, who: &AttesterId, authorization_id: &AuthorizationId) -> Result; + fn can_revoke( + &self, + who: &AttesterId, + ctype: &Ctype, + claim: &ClaimHash, + authorization_id: &AuthorizationId, + ) -> Result; /// Decides whether the account is allowed to remove the attestation with /// the `authorization_id` and the access information provided by the sender /// (&self). - fn can_remove(&self, who: &AttesterId, authorization_id: &AuthorizationId) -> Result; + fn can_remove( + &self, + who: &AttesterId, + ctype: &Ctype, + claim: &ClaimHash, + authorization_id: &AuthorizationId, + ) -> Result; /// The authorization ID that the sender provided. This will be used for new /// attestations. @@ -46,17 +58,30 @@ pub trait AttestationAccessControl { fn weight(&self) -> Weight; } -impl AttestationAccessControl for () +impl + AttestationAccessControl for () where AuthorizationId: Default, { - fn can_attest(&self, _who: &AttesterId) -> Result { + fn can_attest(&self, _who: &AttesterId, ctype: &Ctype, claim: &ClaimHash) -> Result { Err(DispatchError::Other("Unimplemented")) } - fn can_revoke(&self, _who: &AttesterId, _authorization_id: &AuthorizationId) -> Result { + fn can_revoke( + &self, + _who: &AttesterId, + ctype: &Ctype, + claim: &ClaimHash, + _authorization_id: &AuthorizationId, + ) -> Result { Err(DispatchError::Other("Unimplemented")) } - fn can_remove(&self, _who: &AttesterId, _authorization_id: &AuthorizationId) -> Result { + fn can_remove( + &self, + _who: &AttesterId, + ctype: &Ctype, + claim: &ClaimHash, + _authorization_id: &AuthorizationId, + ) -> Result { Err(DispatchError::Other("Unimplemented")) } fn authorization_id(&self) -> AuthorizationId { diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index d4496d0f21..a85aac221d 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -104,7 +104,7 @@ pub mod pallet { use ctype::CtypeHashOf; use kilt_support::{deposit::Deposit, traits::CallSources}; /// Type of a claim hash. - pub(crate) type ClaimHashOf = ::Hash; + pub type ClaimHashOf = ::Hash; /// Type of an attester identifier. pub(crate) type AttesterOf = ::AttesterId; @@ -144,7 +144,8 @@ pub mod pallet { type AuthorizationId: Parameter; - type AccessControl: Parameter + AttestationAccessControl; + type AccessControl: Parameter + + AttestationAccessControl, ClaimHashOf>; } #[pallet::pallet] @@ -264,7 +265,10 @@ pub mod pallet { ); // Check for validity of the delegation node if specified. - authorization.as_ref().map(|ac| ac.can_attest(&who)).transpose()?; + authorization + .as_ref() + .map(|ac| ac.can_attest(&who, &ctype_hash, &claim_hash)) + .transpose()?; let authorization_id = authorization.as_ref().map(|ac| ac.authorization_id()); let deposit = Pallet::::reserve_deposit(payer, deposit_amount)?; @@ -328,9 +332,12 @@ pub mod pallet { if attestation.attester != who { let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; - authorization - .ok_or(Error::::Unauthorized)? - .can_revoke(&who, attestation_auth_id)?; + authorization.ok_or(Error::::Unauthorized)?.can_revoke( + &who, + &attestation.ctype_hash, + &claim_hash, + attestation_auth_id, + )?; } // *** No Fail beyond this point *** @@ -382,9 +389,12 @@ pub mod pallet { if attestation.attester != who { let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::::Unauthorized)?; - authorization - .ok_or(Error::::Unauthorized)? - .can_remove(&who, attestation_auth_id)?; + authorization.ok_or(Error::::Unauthorized)?.can_remove( + &who, + &attestation.ctype_hash, + &claim_hash, + attestation_auth_id, + )?; } // *** No Fail beyond this point *** diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index a3188569d4..b6b4914000 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -78,11 +78,17 @@ where #[scale_info(skip_type_params(T))] pub struct MockAccessControl(pub T::AttesterId); -impl AttestationAccessControl for MockAccessControl +impl AttestationAccessControl, ClaimHashOf> + for MockAccessControl where T: Config::AttesterId>, { - fn can_attest(&self, who: &T::AttesterId) -> Result { + fn can_attest( + &self, + who: &T::AttesterId, + ctype: &CtypeHashOf, + claim: &ClaimHashOf, + ) -> Result { if who == &self.0 { Ok(0) } else { @@ -90,7 +96,13 @@ where } } - fn can_revoke(&self, who: &T::AttesterId, authorization_id: &T::AuthorizationId) -> Result { + fn can_revoke( + &self, + who: &T::AttesterId, + ctype: &CtypeHashOf, + claim: &ClaimHashOf, + authorization_id: &T::AuthorizationId, + ) -> Result { if authorization_id == who { Ok(0) } else { @@ -98,7 +110,13 @@ where } } - fn can_remove(&self, who: &T::AttesterId, authorization_id: &T::AuthorizationId) -> Result { + fn can_remove( + &self, + who: &T::AttesterId, + ctype: &CtypeHashOf, + claim: &ClaimHashOf, + authorization_id: &T::AuthorizationId, + ) -> Result { if authorization_id == who { Ok(0) } else { diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index b99d7a202e..c524d90090 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -21,6 +21,9 @@ use frame_support::{dispatch::Weight, ensure}; use scale_info::TypeInfo; use sp_runtime::DispatchError; +use attestation::ClaimHashOf; +use ctype::CtypeHashOf; + use crate::{default_weights::WeightInfo, Config, DelegationNodeIdOf, DelegatorIdOf, Error, Pallet}; /// Controls the access to attestations. @@ -42,15 +45,29 @@ use crate::{default_weights::WeightInfo, Config, DelegationNodeIdOf, DelegatorId #[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] pub struct DelegationAc(pub(crate) DelegationNodeIdOf, pub(crate) u32); -impl attestation::AttestationAccessControl, DelegationNodeIdOf> for DelegationAc { - fn can_attest(&self, who: &DelegatorIdOf) -> Result { +impl + attestation::AttestationAccessControl, DelegationNodeIdOf, CtypeHashOf, ClaimHashOf> + for DelegationAc +{ + fn can_attest( + &self, + who: &DelegatorIdOf, + ctype: &CtypeHashOf, + claim: &ClaimHashOf, + ) -> Result { match Pallet::::is_delegating(who, &self.0, self.1)? { (true, checks) => Ok(::WeightInfo::can_attest(checks)), _ => Err(Error::::AccessDenied.into()), } } - fn can_revoke(&self, who: &DelegatorIdOf, auth_id: &DelegationNodeIdOf) -> Result { + fn can_revoke( + &self, + who: &DelegatorIdOf, + ctype: &CtypeHashOf, + claim: &ClaimHashOf, + auth_id: &DelegationNodeIdOf, + ) -> Result { ensure!(auth_id == &self.0, Error::::AccessDenied); match Pallet::::is_delegating(who, &self.0, self.1)? { @@ -59,7 +76,13 @@ impl attestation::AttestationAccessControl, Delegati } } - fn can_remove(&self, who: &DelegatorIdOf, auth_id: &DelegationNodeIdOf) -> Result { + fn can_remove( + &self, + who: &DelegatorIdOf, + ctype: &CtypeHashOf, + claim: &ClaimHashOf, + auth_id: &DelegationNodeIdOf, + ) -> Result { ensure!(auth_id == &self.0, Error::::AccessDenied); match Pallet::::is_delegating(who, &self.0, self.1)? { @@ -78,3 +101,47 @@ impl attestation::AttestationAccessControl, Delegati .max(::WeightInfo::can_remove(self.1)) } } + +#[cfg(test)] +mod tests { + + #[test] + fn test_can_attest() { + todo!() + } + + #[test] + fn test_can_attest_missing_permission() { + todo!() + } + + #[test] + fn test_can_attest_missing_node() { + todo!() + } + + #[test] + fn test_can_attest_wrong_ctype() { + todo!() + } + + #[test] + fn test_can_revoke_same_node() { + todo!() + } + + #[test] + fn test_can_revoke_parent() { + todo!() + } + + #[test] + fn test_can_revoke_same_node_revoked() { + todo!() + } + + #[test] + fn test_can_revoke_parent_revoked() { + todo!() + } +} diff --git a/pallets/delegation/src/benchmarking.rs b/pallets/delegation/src/benchmarking.rs index 17204bd6bb..a6b12ab419 100644 --- a/pallets/delegation/src/benchmarking.rs +++ b/pallets/delegation/src/benchmarking.rs @@ -382,39 +382,48 @@ benchmarks! { can_attest { let c in 1 .. T::MaxParentChecks::get(); + let ctype = Default::default(); + let claim = Default::default(); + let sender: T::AccountId = account("sender", 0, SEED); let (root_acc, _, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; let root_acc: T::DelegationEntityId = root_acc.into(); let ac = DelegationAc::(leaf_id, c); - }: { ac.can_attest(&root_acc).expect("Should be allowed") } + }: { ac.can_attest(&root_acc, &ctype, &claim).expect("Should be allowed") } verify { } can_revoke { let c in 1 .. T::MaxParentChecks::get(); + let ctype = Default::default(); + let claim = Default::default(); + let sender: T::AccountId = account("sender", 0, SEED); let (root_acc, root_id, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; let root_acc: T::DelegationEntityId = root_acc.into(); let ac = DelegationAc::(leaf_id, c); - }: { ac.can_revoke(&root_acc, &leaf_id).expect("Should be allowed") } + }: { ac.can_revoke(&root_acc, &ctype, &claim, &leaf_id).expect("Should be allowed") } verify { } can_remove { let c in 1 .. T::MaxParentChecks::get(); + let ctype = Default::default(); + let claim = Default::default(); + let sender: T::AccountId = account("sender", 0, SEED); let (root_acc, root_id, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; let root_acc: T::DelegationEntityId = root_acc.into(); let ac = DelegationAc::(leaf_id, c); - }: { ac.can_remove(&root_acc, &leaf_id).expect("Should be allowed") } + }: { ac.can_remove(&root_acc, &ctype, &claim, &leaf_id).expect("Should be allowed") } verify { } } diff --git a/runtimes/common/src/authorization.rs b/runtimes/common/src/authorization.rs index ae8c65b4f7..8e3cd780dc 100644 --- a/runtimes/common/src/authorization.rs +++ b/runtimes/common/src/authorization.rs @@ -32,36 +32,47 @@ pub enum PalletAuthorize { Delegation(DelegationAc), } -impl AttestationAccessControl> - for PalletAuthorize +impl + AttestationAccessControl, Ctype, ClaimHash> for PalletAuthorize where - DelegationAc: AttestationAccessControl, + DelegationAc: AttestationAccessControl, { - fn can_attest(&self, who: &AttesterId) -> Result { + fn can_attest( + &self, + who: &AttesterId, + ctype: &Ctype, + claim: &ClaimHash, + ) -> Result { match self { - PalletAuthorize::Delegation(ac) => ac.can_attest(who), + PalletAuthorize::Delegation(ac) => ac.can_attest(who, ctype, claim), } } fn can_revoke( &self, who: &AttesterId, + ctype: &Ctype, + claim: &ClaimHash, auth_id: &AuthorizationId, ) -> Result { match (self, auth_id) { - (PalletAuthorize::Delegation(ac), AuthorizationId::Delegation(auth_id)) => ac.can_revoke(who, auth_id), - // _ => Err(DispatchError::Other("unauthorized")), + (PalletAuthorize::Delegation(ac), AuthorizationId::Delegation(auth_id)) => { + ac.can_revoke(who, ctype, claim, auth_id) + } // _ => Err(DispatchError::Other("unauthorized")), } } fn can_remove( &self, who: &AttesterId, + ctype: &Ctype, + claim: &ClaimHash, auth_id: &AuthorizationId, ) -> Result { match (self, auth_id) { - (PalletAuthorize::Delegation(ac), AuthorizationId::Delegation(auth_id)) => ac.can_remove(who, auth_id), - // _ => Err(DispatchError::Other("unauthorized")), + (PalletAuthorize::Delegation(ac), AuthorizationId::Delegation(auth_id)) => { + ac.can_remove(who, ctype, claim, auth_id) + } // _ => Err(DispatchError::Other("unauthorized")), } } From 0d690146ec03fbc825c344357c30263b24f0b224 Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 31 Jan 2022 08:58:00 +0100 Subject: [PATCH 31/60] refactor: tuple struct to struct, split weight --- pallets/attestation/src/access_control.rs | 34 +++++++-- pallets/attestation/src/lib.rs | 6 +- pallets/attestation/src/mock.rs | 20 +++-- pallets/delegation/src/access_control.rs | 78 ++++++++++++-------- pallets/delegation/src/benchmarking.rs | 17 ++++- pallets/delegation/src/default_weights.rs | 13 +--- runtimes/common/src/authorization.rs | 14 +++- runtimes/peregrine/src/weights/delegation.rs | 5 +- runtimes/spiritnet/src/weights/delegation.rs | 4 +- 9 files changed, 121 insertions(+), 70 deletions(-) diff --git a/pallets/attestation/src/access_control.rs b/pallets/attestation/src/access_control.rs index 58de9eae51..1cdb266d71 100644 --- a/pallets/attestation/src/access_control.rs +++ b/pallets/attestation/src/access_control.rs @@ -54,8 +54,20 @@ pub trait AttestationAccessControl AuthorizationId; - /// The maximum weight of `can_attest`, `can_revoke` and `can_remove`. - fn weight(&self) -> Weight; + /// The worst-case weight of `can_attest`. + fn can_attest_weight(&self) -> Weight { + 0 + } + + /// The worst-case weight of `can_revoke`. + fn can_revoke_weight(&self) -> Weight { + 0 + } + + /// The worst-case weight of `can_remove`. + fn can_remove_weight(&self) -> Weight { + 0 + } } impl @@ -63,14 +75,14 @@ impl where AuthorizationId: Default, { - fn can_attest(&self, _who: &AttesterId, ctype: &Ctype, claim: &ClaimHash) -> Result { + fn can_attest(&self, _who: &AttesterId, _ctype: &Ctype, _claim: &ClaimHash) -> Result { Err(DispatchError::Other("Unimplemented")) } fn can_revoke( &self, _who: &AttesterId, - ctype: &Ctype, - claim: &ClaimHash, + _ctype: &Ctype, + _claim: &ClaimHash, _authorization_id: &AuthorizationId, ) -> Result { Err(DispatchError::Other("Unimplemented")) @@ -78,8 +90,8 @@ where fn can_remove( &self, _who: &AttesterId, - ctype: &Ctype, - claim: &ClaimHash, + _ctype: &Ctype, + _claim: &ClaimHash, _authorization_id: &AuthorizationId, ) -> Result { Err(DispatchError::Other("Unimplemented")) @@ -87,7 +99,13 @@ where fn authorization_id(&self) -> AuthorizationId { Default::default() } - fn weight(&self) -> Weight { + fn can_attest_weight(&self) -> Weight { + 0 + } + fn can_revoke_weight(&self) -> Weight { + 0 + } + fn can_remove_weight(&self) -> Weight { 0 } } diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index a85aac221d..292cd4220e 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -242,7 +242,7 @@ pub mod pallet { /// # #[pallet::weight( ::WeightInfo::add() - .saturating_add(authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0)) + .saturating_add(authorization.as_ref().map(|ac| ac.can_attest_weight()).unwrap_or(0)) )] pub fn add( origin: OriginFor, @@ -316,7 +316,7 @@ pub mod pallet { /// # #[pallet::weight( ::WeightInfo::revoke() - .saturating_add(authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0)) + .saturating_add(authorization.as_ref().map(|ac| ac.can_revoke_weight()).unwrap_or(0)) )] pub fn revoke( origin: OriginFor, @@ -375,7 +375,7 @@ pub mod pallet { /// # #[pallet::weight( ::WeightInfo::remove() - .saturating_add(authorization.as_ref().map(|ac| ac.weight()).unwrap_or(0)) + .saturating_add(authorization.as_ref().map(|ac| ac.can_remove_weight()).unwrap_or(0)) )] pub fn remove( origin: OriginFor, diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index b6b4914000..82b0e8b817 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -86,8 +86,8 @@ where fn can_attest( &self, who: &T::AttesterId, - ctype: &CtypeHashOf, - claim: &ClaimHashOf, + _ctype: &CtypeHashOf, + _claim: &ClaimHashOf, ) -> Result { if who == &self.0 { Ok(0) @@ -99,8 +99,8 @@ where fn can_revoke( &self, who: &T::AttesterId, - ctype: &CtypeHashOf, - claim: &ClaimHashOf, + _ctype: &CtypeHashOf, + _claim: &ClaimHashOf, authorization_id: &T::AuthorizationId, ) -> Result { if authorization_id == who { @@ -113,8 +113,8 @@ where fn can_remove( &self, who: &T::AttesterId, - ctype: &CtypeHashOf, - claim: &ClaimHashOf, + _ctype: &CtypeHashOf, + _claim: &ClaimHashOf, authorization_id: &T::AuthorizationId, ) -> Result { if authorization_id == who { @@ -128,7 +128,13 @@ where self.0.clone() } - fn weight(&self) -> Weight { + fn can_attest_weight(&self) -> Weight { + 0 + } + fn can_revoke_weight(&self) -> Weight { + 0 + } + fn can_remove_weight(&self) -> Weight { 0 } } diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index c524d90090..f94d22e400 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -24,26 +24,29 @@ use sp_runtime::DispatchError; use attestation::ClaimHashOf; use ctype::CtypeHashOf; -use crate::{default_weights::WeightInfo, Config, DelegationNodeIdOf, DelegatorIdOf, Error, Pallet}; +use crate::{ + default_weights::WeightInfo, Config, DelegationHierarchies, DelegationNodeIdOf, DelegationNodes, DelegatorIdOf, + Error, Pallet, Permissions, +}; /// Controls the access to attestations. /// -/// Can attest if all conditions are fulfilled +/// Can attest if /// * delegation node of sender is not revoked /// * delegation node of sender has ATTEST permission /// * the CType of the delegation root matches the CType of the attestation /// -/// Can revoke if all conditions are fulfilled -/// * sender delegation node is not revoked +/// Can revoke attestations if +/// * delegation node of sender is not revoked /// * sender delegation node is equal to OR parent of the delegation node /// stored in the attestation /// -/// Can revoke if all conditions are fulfilled -/// * sender delegation node is not revoked -/// * sender delegation node is equal to OR parent of the delegation node -/// stored in the attestation +/// Can remove attestations if #[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] -pub struct DelegationAc(pub(crate) DelegationNodeIdOf, pub(crate) u32); +pub struct DelegationAc { + pub(crate) sender_node_id: DelegationNodeIdOf, + pub(crate) max_checks: u32, +} impl attestation::AttestationAccessControl, DelegationNodeIdOf, CtypeHashOf, ClaimHashOf> @@ -53,24 +56,38 @@ impl &self, who: &DelegatorIdOf, ctype: &CtypeHashOf, - claim: &ClaimHashOf, + _claim: &ClaimHashOf, ) -> Result { - match Pallet::::is_delegating(who, &self.0, self.1)? { - (true, checks) => Ok(::WeightInfo::can_attest(checks)), - _ => Err(Error::::AccessDenied.into()), - } + let delegation_node = DelegationNodes::::get(self.sender_node_id).ok_or(Error::::DelegationNotFound)?; + let root = + DelegationHierarchies::::get(delegation_node.hierarchy_root_id).ok_or(Error::::DelegationNotFound)?; + ensure!( + // has permission + ((delegation_node.details.permissions & Permissions::ATTEST) == Permissions::ATTEST) + // not revoked + && !delegation_node.details.revoked + // is owner of delegation + && &delegation_node.details.owner == who + // delegation matches the ctype + && &root.ctype_hash == ctype, + Error::::AccessDenied + ); + + Ok(::WeightInfo::can_attest()) } fn can_revoke( &self, who: &DelegatorIdOf, - ctype: &CtypeHashOf, - claim: &ClaimHashOf, - auth_id: &DelegationNodeIdOf, + _ctype: &CtypeHashOf, + _claim: &ClaimHashOf, + attester_node_id: &DelegationNodeIdOf, ) -> Result { - ensure!(auth_id == &self.0, Error::::AccessDenied); + // `attester_node` was supplied by the attestation pallet and is stored in the + // attestation. `self.sender_node` was supplied by the user. `attester_node` and + // `self.sender_node` can be different! - match Pallet::::is_delegating(who, &self.0, self.1)? { + match Pallet::::is_delegating(who, &attester_node_id, self.max_checks)? { (true, checks) => Ok(::WeightInfo::can_revoke(checks)), _ => Err(Error::::AccessDenied.into()), } @@ -83,22 +100,23 @@ impl claim: &ClaimHashOf, auth_id: &DelegationNodeIdOf, ) -> Result { - ensure!(auth_id == &self.0, Error::::AccessDenied); - - match Pallet::::is_delegating(who, &self.0, self.1)? { - (true, checks) => Ok(::WeightInfo::can_remove(checks)), - _ => Err(Error::::AccessDenied.into()), - } + self.can_revoke(who, ctype, claim, auth_id) } fn authorization_id(&self) -> DelegationNodeIdOf { - self.0 + self.sender_node_id + } + + fn can_attest_weight(&self) -> Weight { + ::WeightInfo::can_attest() + } + + fn can_revoke_weight(&self) -> Weight { + ::WeightInfo::can_revoke(self.max_checks) } - fn weight(&self) -> Weight { - ::WeightInfo::can_attest(self.1) - .max(::WeightInfo::can_revoke(self.1)) - .max(::WeightInfo::can_remove(self.1)) + fn can_remove_weight(&self) -> Weight { + ::WeightInfo::can_remove(self.max_checks) } } diff --git a/pallets/delegation/src/benchmarking.rs b/pallets/delegation/src/benchmarking.rs index a6b12ab419..b8f3afad3d 100644 --- a/pallets/delegation/src/benchmarking.rs +++ b/pallets/delegation/src/benchmarking.rs @@ -380,7 +380,7 @@ benchmarks! { } can_attest { - let c in 1 .. T::MaxParentChecks::get(); + let c = T::MaxParentChecks::get(); let ctype = Default::default(); let claim = Default::default(); @@ -389,7 +389,10 @@ benchmarks! { let (root_acc, _, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; let root_acc: T::DelegationEntityId = root_acc.into(); - let ac = DelegationAc::(leaf_id, c); + let ac = DelegationAc::{ + sender_node_id: leaf_id, + max_checks: c + }; }: { ac.can_attest(&root_acc, &ctype, &claim).expect("Should be allowed") } verify { @@ -405,7 +408,10 @@ benchmarks! { let (root_acc, root_id, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; let root_acc: T::DelegationEntityId = root_acc.into(); - let ac = DelegationAc::(leaf_id, c); + let ac = DelegationAc::{ + sender_node_id: leaf_id, + max_checks: c + }; }: { ac.can_revoke(&root_acc, &ctype, &claim, &leaf_id).expect("Should be allowed") } verify { @@ -421,7 +427,10 @@ benchmarks! { let (root_acc, root_id, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; let root_acc: T::DelegationEntityId = root_acc.into(); - let ac = DelegationAc::(leaf_id, c); + let ac = DelegationAc::{ + sender_node_id: leaf_id, + max_checks: c + }; }: { ac.can_remove(&root_acc, &ctype, &claim, &leaf_id).expect("Should be allowed") } verify { diff --git a/pallets/delegation/src/default_weights.rs b/pallets/delegation/src/default_weights.rs index 0c984d2770..44260fd919 100644 --- a/pallets/delegation/src/default_weights.rs +++ b/pallets/delegation/src/default_weights.rs @@ -52,7 +52,7 @@ pub trait WeightInfo { fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight; fn remove_delegation(r: u32, ) -> Weight; fn reclaim_deposit(r: u32, ) -> Weight; - fn can_attest(c: u32, ) -> Weight; + fn can_attest() -> Weight; fn can_revoke(c: u32, ) -> Weight; fn can_remove(c: u32, ) -> Weight; } @@ -125,12 +125,9 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) - fn can_attest(c: u32, ) -> Weight { + fn can_attest() -> Weight { (9_002_000 as Weight) - // Standard Error: 26_000 - .saturating_add((5_910_000 as Weight).saturating_mul(c as Weight)) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { @@ -217,12 +214,10 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) - fn can_attest(c: u32, ) -> Weight { + fn can_attest( ) -> Weight { (9_002_000 as Weight) // Standard Error: 26_000 - .saturating_add((5_910_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) - .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { diff --git a/runtimes/common/src/authorization.rs b/runtimes/common/src/authorization.rs index 8e3cd780dc..5514547d29 100644 --- a/runtimes/common/src/authorization.rs +++ b/runtimes/common/src/authorization.rs @@ -82,9 +82,19 @@ where } } - fn weight(&self) -> Weight { + fn can_attest_weight(&self) -> Weight { match self { - PalletAuthorize::Delegation(ac) => ac.weight(), + PalletAuthorize::Delegation(ac) => ac.can_attest_weight(), + } + } + fn can_revoke_weight(&self) -> Weight { + match self { + PalletAuthorize::Delegation(ac) => ac.can_revoke_weight(), + } + } + fn can_remove_weight(&self) -> Weight { + match self { + PalletAuthorize::Delegation(ac) => ac.can_remove_weight(), } } } diff --git a/runtimes/peregrine/src/weights/delegation.rs b/runtimes/peregrine/src/weights/delegation.rs index e247aafe01..db872b9f1a 100644 --- a/runtimes/peregrine/src/weights/delegation.rs +++ b/runtimes/peregrine/src/weights/delegation.rs @@ -114,14 +114,11 @@ impl delegation::WeightInfo for WeightInfo { } // TODO: run benchmarks - fn can_attest(r: u32, ) -> Weight { + fn can_attest( ) -> Weight { (61_171_000_u64) // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) } fn can_revoke(r: u32, ) -> Weight { (61_171_000_u64) diff --git a/runtimes/spiritnet/src/weights/delegation.rs b/runtimes/spiritnet/src/weights/delegation.rs index 9bd8bf0de1..b10bb51a76 100644 --- a/runtimes/spiritnet/src/weights/delegation.rs +++ b/runtimes/spiritnet/src/weights/delegation.rs @@ -110,12 +110,10 @@ impl delegation::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) - fn can_attest(c: u32, ) -> Weight { + fn can_attest( ) -> Weight { (8_421_000 as Weight) // Standard Error: 18_000 - .saturating_add((5_462_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { From 3f03b7b7d53f946557ac03c5c826902736d0fde5 Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 31 Jan 2022 16:48:50 +0100 Subject: [PATCH 32/60] test: can_attest --- pallets/attestation/src/mock.rs | 8 +- pallets/delegation/src/access_control.rs | 267 ++++++++++++++++++++++- pallets/delegation/src/mock.rs | 42 +++- 3 files changed, 304 insertions(+), 13 deletions(-) diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index 82b0e8b817..1121100ad6 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -286,6 +286,10 @@ pub(crate) mod runtime { pub const CLAIM_HASH_SEED_01: u64 = 1u64; pub const CLAIM_HASH_SEED_02: u64 = 2u64; + pub fn claim_hash_from_seed(seed: u64) -> Hash { + Hash::from_low_u64_be(seed) + } + pub fn ed25519_did_from_seed(seed: &[u8; 32]) -> SubjectId { MultiSigner::from(ed25519::Pair::from_seed(seed).public()) .into_account() @@ -298,10 +302,6 @@ pub(crate) mod runtime { .into() } - pub fn claim_hash_from_seed(seed: u64) -> Hash { - Hash::from_low_u64_be(seed) - } - #[derive(Clone, Default)] pub struct ExtBuilder { /// initial ctypes & owners diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index f94d22e400..d0fba0b860 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -122,25 +122,284 @@ impl #[cfg(test)] mod tests { + use frame_support::{assert_noop, assert_ok}; + + use attestation::AttestationAccessControl; + use ctype::mock::get_ctype_hash; + use kilt_support::{deposit::Deposit, mock::mock_origin::DoubleOrigin}; + + use super::*; + use crate::{mock::*, DelegationDetails, DelegationNode}; #[test] fn test_can_attest() { - todo!() + let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); + let delegate = sr25519_did_from_seed(&BOB_SEED); + + let hierarchy_root_id = get_delegation_hierarchy_id::(true); + let hierarchy_details = generate_base_delegation_hierarchy_details(); + let ctype_hash = hierarchy_details.ctype_hash; + let parent_id = delegation_id_from_seed::(DELEGATION_ID_SEED_1); + let parent_node = DelegationNode { + details: DelegationDetails { + owner: delegate.clone(), + permissions: Permissions::DELEGATE | Permissions::ATTEST, + revoked: false, + }, + children: Default::default(), + hierarchy_root_id, + parent: Some(hierarchy_root_id), + deposit: Deposit { + owner: ACCOUNT_00, + amount: ::Deposit::get(), + }, + }; + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let authorization_id = Some(DelegationAc { + sender_node_id: parent_id, + max_checks: 1, + }); + + ExtBuilder::default() + .with_ctypes(vec![(ctype_hash, root_owner.clone())]) + .with_delegation_hierarchies(vec![( + hierarchy_root_id, + hierarchy_details, + root_owner.clone(), + ACCOUNT_00, + )]) + .with_delegations(vec![(parent_id, parent_node)]) + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .build() + .execute_with(|| { + assert_ok!(Attestation::add( + DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), + claim_hash, + ctype_hash, + authorization_id.clone() + )); + let stored_attestation = + Attestation::attestations(&claim_hash).expect("Attestation should be present on chain."); + + assert_eq!(stored_attestation.ctype_hash, ctype_hash); + assert_eq!(stored_attestation.attester, delegate); + assert_eq!( + stored_attestation.authorization_id, + authorization_id.map(|ac| ac.authorization_id()) + ); + assert!(!stored_attestation.revoked); + }); } #[test] fn test_can_attest_missing_permission() { - todo!() + let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); + let delegate = sr25519_did_from_seed(&BOB_SEED); + + let hierarchy_root_id = get_delegation_hierarchy_id::(true); + let hierarchy_details = generate_base_delegation_hierarchy_details(); + let ctype_hash = hierarchy_details.ctype_hash; + let parent_id = delegation_id_from_seed::(DELEGATION_ID_SEED_1); + let parent_node = DelegationNode { + details: DelegationDetails { + owner: delegate.clone(), + permissions: Permissions::DELEGATE, + revoked: false, + }, + children: Default::default(), + hierarchy_root_id, + parent: Some(hierarchy_root_id), + deposit: Deposit { + owner: ACCOUNT_00, + amount: ::Deposit::get(), + }, + }; + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let authorization_id = Some(DelegationAc { + sender_node_id: parent_id, + max_checks: 1, + }); + + ExtBuilder::default() + .with_ctypes(vec![(ctype_hash, root_owner.clone())]) + .with_delegation_hierarchies(vec![( + hierarchy_root_id, + hierarchy_details, + root_owner.clone(), + ACCOUNT_00, + )]) + .with_delegations(vec![(parent_id, parent_node)]) + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .build() + .execute_with(|| { + assert_noop!( + Attestation::add( + DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), + claim_hash, + ctype_hash, + authorization_id.clone() + ), + Error::::AccessDenied + ); + }); + } + + #[test] + fn test_can_attest_revoked() { + let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); + let delegate = sr25519_did_from_seed(&BOB_SEED); + + let hierarchy_root_id = get_delegation_hierarchy_id::(true); + let hierarchy_details = generate_base_delegation_hierarchy_details(); + let ctype_hash = hierarchy_details.ctype_hash; + let parent_id = delegation_id_from_seed::(DELEGATION_ID_SEED_1); + let parent_node = DelegationNode { + details: DelegationDetails { + owner: delegate.clone(), + permissions: Permissions::DELEGATE | Permissions::ATTEST, + revoked: true, + }, + children: Default::default(), + hierarchy_root_id, + parent: Some(hierarchy_root_id), + deposit: Deposit { + owner: ACCOUNT_00, + amount: ::Deposit::get(), + }, + }; + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let authorization_id = Some(DelegationAc { + sender_node_id: parent_id, + max_checks: 1, + }); + + ExtBuilder::default() + .with_ctypes(vec![(ctype_hash, root_owner.clone())]) + .with_delegation_hierarchies(vec![( + hierarchy_root_id, + hierarchy_details, + root_owner.clone(), + ACCOUNT_00, + )]) + .with_delegations(vec![(parent_id, parent_node)]) + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .build() + .execute_with(|| { + assert_noop!( + Attestation::add( + DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), + claim_hash, + ctype_hash, + authorization_id.clone() + ), + Error::::AccessDenied + ); + }); } #[test] fn test_can_attest_missing_node() { - todo!() + let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); + let delegate = sr25519_did_from_seed(&BOB_SEED); + + let hierarchy_root_id = get_delegation_hierarchy_id::(true); + let hierarchy_details = generate_base_delegation_hierarchy_details(); + let ctype_hash = hierarchy_details.ctype_hash; + let parent_id = delegation_id_from_seed::(DELEGATION_ID_SEED_1); + let parent_node = DelegationNode:: { + details: DelegationDetails { + owner: delegate.clone(), + permissions: Permissions::DELEGATE | Permissions::ATTEST, + revoked: false, + }, + children: Default::default(), + hierarchy_root_id, + parent: Some(hierarchy_root_id), + deposit: Deposit { + owner: ACCOUNT_00, + amount: ::Deposit::get(), + }, + }; + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let authorization_id = Some(DelegationAc { + sender_node_id: parent_id, + max_checks: 1, + }); + + ExtBuilder::default() + .with_ctypes(vec![(ctype_hash, root_owner.clone())]) + .with_delegation_hierarchies(vec![( + hierarchy_root_id, + hierarchy_details, + root_owner.clone(), + ACCOUNT_00, + )]) + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .build() + .execute_with(|| { + assert_noop!( + Attestation::add( + DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), + claim_hash, + ctype_hash, + authorization_id.clone() + ), + Error::::DelegationNotFound + ); + }); } #[test] fn test_can_attest_wrong_ctype() { - todo!() + let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); + let delegate = sr25519_did_from_seed(&BOB_SEED); + + let hierarchy_root_id = get_delegation_hierarchy_id::(true); + let hierarchy_details = generate_base_delegation_hierarchy_details(); + let ctype_hash = get_ctype_hash::(false); + let parent_id = delegation_id_from_seed::(DELEGATION_ID_SEED_1); + let parent_node = DelegationNode { + details: DelegationDetails { + owner: delegate.clone(), + permissions: Permissions::DELEGATE | Permissions::ATTEST, + revoked: false, + }, + children: Default::default(), + hierarchy_root_id, + parent: Some(hierarchy_root_id), + deposit: Deposit { + owner: ACCOUNT_00, + amount: ::Deposit::get(), + }, + }; + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let authorization_id = Some(DelegationAc { + sender_node_id: parent_id, + max_checks: 1, + }); + + ExtBuilder::default() + .with_ctypes(vec![(ctype_hash, root_owner.clone())]) + .with_delegation_hierarchies(vec![( + hierarchy_root_id, + hierarchy_details, + root_owner.clone(), + ACCOUNT_00, + )]) + .with_delegations(vec![(parent_id, parent_node)]) + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .build() + .execute_with(|| { + assert_noop!( + Attestation::add( + DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), + claim_hash, + ctype_hash, + authorization_id.clone() + ), + Error::::AccessDenied + ); + }); } #[test] diff --git a/pallets/delegation/src/mock.rs b/pallets/delegation/src/mock.rs index 72aa66f8a0..d66f9a8fa2 100644 --- a/pallets/delegation/src/mock.rs +++ b/pallets/delegation/src/mock.rs @@ -30,6 +30,9 @@ use crate::{ DelegatorIdOf, Permissions, }; +#[cfg(test)] +pub use self::runtime::*; + const DEFAULT_HIERARCHY_ID_SEED: u64 = 1u64; const ALTERNATIVE_HIERARCHY_ID_SEED: u64 = 2u64; @@ -166,7 +169,7 @@ where #[cfg(test)] pub mod runtime { - use crate::{migrations::DelegationStorageVersion, BalanceOf, DelegateSignatureTypeOf, DelegationNodeIdOf}; + use crate::{migrations::DelegationStorageVersion, BalanceOf, DelegateSignatureTypeOf, DelegationNodeIdOf, DelegationAc}; use super::*; @@ -198,6 +201,7 @@ pub mod runtime { pub const UNIT: Balance = 10u128.pow(15); pub const MILLI_UNIT: Balance = 10u128.pow(12); pub const DELEGATION_DEPOSIT: Balance = 10 * MILLI_UNIT; + pub const ATTESTATION_DEPOSIT: Balance = 10 * MILLI_UNIT; frame_support::construct_runtime!( pub enum Test where @@ -206,10 +210,12 @@ pub mod runtime { UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Pallet, Call, Config, Storage, Event}, - Ctype: ctype::{Pallet, Call, Storage, Event}, - Delegation: delegation::{Pallet, Call, Storage, Event}, Balances: pallet_balances::{Pallet, Call, Storage, Event}, - MockOrigin: mock_origin::{Pallet, Origin}, + + Attestation: attestation, + Ctype: ctype, + Delegation: delegation, + MockOrigin: mock_origin, } ); @@ -285,6 +291,25 @@ pub mod runtime { type FeeCollector = (); } + parameter_types! { + pub const MaxDelegatedAttestations: u32 = 1000; + pub const Deposit: Balance = ATTESTATION_DEPOSIT; + } + + impl attestation::Config for Test { + type EnsureOrigin = mock_origin::EnsureDoubleOrigin>; + type OriginSuccess = mock_origin::DoubleOrigin>; + type Event = (); + type WeightInfo = (); + + type Currency = Balances; + type Deposit = Deposit; + type MaxDelegatedAttestations = MaxDelegatedAttestations; + type AttesterId = SubjectId; + type AuthorizationId = DelegationNodeIdOf; + type AccessControl = DelegationAc; + } + parameter_types! { pub const MaxSignatureByteLength: u16 = 64; pub const MaxParentChecks: u32 = 5; @@ -321,6 +346,13 @@ pub mod runtime { pub(crate) const BOB_SEED: [u8; 32] = [1u8; 32]; pub(crate) const CHARLIE_SEED: [u8; 32] = [2u8; 32]; + pub const CLAIM_HASH_SEED_01: u64 = 1u64; + pub const CLAIM_HASH_SEED_02: u64 = 2u64; + + pub fn claim_hash_from_seed(seed: u64) -> Hash { + Hash::from_low_u64_be(seed) + } + pub fn ed25519_did_from_seed(seed: &[u8; 32]) -> SubjectId { MultiSigner::from(ed25519::Pair::from_seed(seed).public()) .into_account() @@ -333,7 +365,7 @@ pub mod runtime { .into() } - pub(crate) fn hash_to_u8(hash: T) -> Vec { + pub(crate) fn hash_to_u8(hash: Hash) -> Vec { hash.encode() } From b85f1c214a015e19c88f7a3cdf69ef1985ddb87a Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 1 Feb 2022 17:40:14 +0100 Subject: [PATCH 33/60] test: test_can_revoke_same_node --- pallets/attestation/src/mock.rs | 25 ++++--- pallets/delegation/Cargo.toml | 1 + pallets/delegation/src/access_control.rs | 88 ++++++++++++++++-------- pallets/delegation/src/mock.rs | 26 +++++-- 4 files changed, 96 insertions(+), 44 deletions(-) diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index 1121100ad6..5c9d07ab19 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -139,6 +139,19 @@ where } } +pub fn insert_attestation(claim_hash: ClaimHashOf, details: AttestationDetails) { + crate::Pallet::::reserve_deposit(details.deposit.owner.clone(), details.deposit.amount).expect("Should have balance"); + + crate::Attestations::::insert(&claim_hash, details.clone()); + if let Some(delegation_id) = details.authorization_id.as_ref() { + crate::DelegatedAttestations::::try_mutate(delegation_id, |attestations| { + let attestations = attestations.get_or_insert_with(Default::default); + attestations.try_push(claim_hash) + }) + .expect("Couldn't initialise delegated attestation"); + } +} + /// Mocks that are only used internally #[cfg(test)] pub(crate) mod runtime { @@ -343,17 +356,7 @@ pub(crate) mod runtime { } for (claim_hash, details) in self.attestations { - Pallet::::reserve_deposit(details.deposit.owner.clone(), details.deposit.amount) - .expect("Should have balance"); - - crate::Attestations::::insert(&claim_hash, details.clone()); - if let Some(delegation_id) = details.authorization_id.as_ref() { - crate::DelegatedAttestations::::try_mutate(delegation_id, |attestations| { - let attestations = attestations.get_or_insert_with(Default::default); - attestations.try_push(claim_hash) - }) - .expect("Couldn't initialise delegated attestation"); - } + insert_attestation(claim_hash, details); } }); diff --git a/pallets/delegation/Cargo.toml b/pallets/delegation/Cargo.toml index 1bf8a685d6..13d1ea974a 100644 --- a/pallets/delegation/Cargo.toml +++ b/pallets/delegation/Cargo.toml @@ -13,6 +13,7 @@ targets = ["x86_64-unknown-linux-gnu"] substrate-wasm-builder-runner = {version = "3.0.0"} [dev-dependencies] +attestation = {features = ["mock"], path = "../attestation"} ctype = {features = ["mock"], path = "../ctype"} kilt-support = {features = ["mock"], path = "../../support"} diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index d0fba0b860..626fae8543 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -124,7 +124,7 @@ impl mod tests { use frame_support::{assert_noop, assert_ok}; - use attestation::AttestationAccessControl; + use attestation::{mock::generate_base_attestation, AttestationAccessControl}; use ctype::mock::get_ctype_hash; use kilt_support::{deposit::Deposit, mock::mock_origin::DoubleOrigin}; @@ -155,7 +155,7 @@ mod tests { }, }; let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let authorization_id = Some(DelegationAc { + let ac_info = Some(DelegationAc { sender_node_id: parent_id, max_checks: 1, }); @@ -176,7 +176,7 @@ mod tests { DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), claim_hash, ctype_hash, - authorization_id.clone() + ac_info.clone() )); let stored_attestation = Attestation::attestations(&claim_hash).expect("Attestation should be present on chain."); @@ -185,7 +185,7 @@ mod tests { assert_eq!(stored_attestation.attester, delegate); assert_eq!( stored_attestation.authorization_id, - authorization_id.map(|ac| ac.authorization_id()) + ac_info.map(|ac| ac.authorization_id()) ); assert!(!stored_attestation.revoked); }); @@ -215,7 +215,7 @@ mod tests { }, }; let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let authorization_id = Some(DelegationAc { + let ac_info = Some(DelegationAc { sender_node_id: parent_id, max_checks: 1, }); @@ -237,7 +237,7 @@ mod tests { DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), claim_hash, ctype_hash, - authorization_id.clone() + ac_info.clone() ), Error::::AccessDenied ); @@ -268,7 +268,7 @@ mod tests { }, }; let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let authorization_id = Some(DelegationAc { + let ac_info = Some(DelegationAc { sender_node_id: parent_id, max_checks: 1, }); @@ -290,7 +290,7 @@ mod tests { DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), claim_hash, ctype_hash, - authorization_id.clone() + ac_info.clone() ), Error::::AccessDenied ); @@ -306,22 +306,8 @@ mod tests { let hierarchy_details = generate_base_delegation_hierarchy_details(); let ctype_hash = hierarchy_details.ctype_hash; let parent_id = delegation_id_from_seed::(DELEGATION_ID_SEED_1); - let parent_node = DelegationNode:: { - details: DelegationDetails { - owner: delegate.clone(), - permissions: Permissions::DELEGATE | Permissions::ATTEST, - revoked: false, - }, - children: Default::default(), - hierarchy_root_id, - parent: Some(hierarchy_root_id), - deposit: Deposit { - owner: ACCOUNT_00, - amount: ::Deposit::get(), - }, - }; let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let authorization_id = Some(DelegationAc { + let ac_info = Some(DelegationAc { sender_node_id: parent_id, max_checks: 1, }); @@ -342,7 +328,7 @@ mod tests { DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), claim_hash, ctype_hash, - authorization_id.clone() + ac_info.clone() ), Error::::DelegationNotFound ); @@ -373,7 +359,7 @@ mod tests { }, }; let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let authorization_id = Some(DelegationAc { + let ac_info = Some(DelegationAc { sender_node_id: parent_id, max_checks: 1, }); @@ -395,7 +381,7 @@ mod tests { DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), claim_hash, ctype_hash, - authorization_id.clone() + ac_info.clone() ), Error::::AccessDenied ); @@ -404,7 +390,55 @@ mod tests { #[test] fn test_can_revoke_same_node() { - todo!() + let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); + let delegate = sr25519_did_from_seed(&BOB_SEED); + + let hierarchy_root_id = get_delegation_hierarchy_id::(true); + let hierarchy_details = generate_base_delegation_hierarchy_details(); + let parent_id = delegation_id_from_seed::(DELEGATION_ID_SEED_1); + let parent_node = DelegationNode { + details: DelegationDetails { + owner: delegate.clone(), + permissions: Permissions::DELEGATE | Permissions::ATTEST, + revoked: false, + }, + children: Default::default(), + hierarchy_root_id, + parent: Some(hierarchy_root_id), + deposit: Deposit { + owner: ACCOUNT_00, + amount: ::Deposit::get(), + }, + }; + let ac_info = Some(DelegationAc { + sender_node_id: parent_id, + max_checks: 1, + }); + + let revoker: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let mut attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); + attestation.authorization_id = Some(parent_id.clone()); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) + .with_delegation_hierarchies(vec![( + hierarchy_root_id, + hierarchy_details, + root_owner.clone(), + ACCOUNT_00, + )]) + .with_delegations(vec![(parent_id, parent_node)]) + .with_attestations(vec![(claim_hash, attestation)]) + .build() + .execute_with(|| { + assert_ok!(Attestation::revoke( + DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), + claim_hash, + ac_info + )); + }); } #[test] diff --git a/pallets/delegation/src/mock.rs b/pallets/delegation/src/mock.rs index d66f9a8fa2..509d053be6 100644 --- a/pallets/delegation/src/mock.rs +++ b/pallets/delegation/src/mock.rs @@ -169,7 +169,9 @@ where #[cfg(test)] pub mod runtime { - use crate::{migrations::DelegationStorageVersion, BalanceOf, DelegateSignatureTypeOf, DelegationNodeIdOf, DelegationAc}; + use crate::{ + migrations::DelegationStorageVersion, BalanceOf, DelegateSignatureTypeOf, DelegationAc, DelegationNodeIdOf, + }; use super::*; @@ -184,6 +186,7 @@ pub mod runtime { }; use sp_std::sync::Arc; + use attestation::{mock::insert_attestation, AttestationDetails, ClaimHashOf}; use kilt_support::{ mock::{mock_origin, SubjectId}, signature::EqualVerify, @@ -442,8 +445,9 @@ pub mod runtime { balances: Vec<(AccountIdOf, BalanceOf)>, /// initial ctypes & owners ctypes: Vec<(CtypeHashOf, SubjectId)>, - delegation_hierarchies_stored: DelegationHierarchyInitialization, - delegations_stored: Vec<(DelegationNodeIdOf, DelegationNode)>, + delegation_hierarchies: DelegationHierarchyInitialization, + delegations: Vec<(DelegationNodeIdOf, DelegationNode)>, + attestations: Vec<(ClaimHashOf, AttestationDetails)>, storage_version: DelegationStorageVersion, } @@ -452,7 +456,7 @@ pub mod runtime { mut self, delegation_hierarchies: DelegationHierarchyInitialization, ) -> Self { - self.delegation_hierarchies_stored = delegation_hierarchies; + self.delegation_hierarchies = delegation_hierarchies; self } @@ -467,7 +471,12 @@ pub mod runtime { } pub fn with_delegations(mut self, delegations: Vec<(DelegationNodeIdOf, DelegationNode)>) -> Self { - self.delegations_stored = delegations; + self.delegations = delegations; + self + } + + pub fn with_attestations(mut self, attestations: Vec<(ClaimHashOf, AttestationDetails)>) -> Self { + self.attestations = attestations; self } @@ -491,11 +500,16 @@ pub mod runtime { ctype::Ctypes::::insert(ctype_hash, owner); } - initialize_pallet(self.delegations_stored, self.delegation_hierarchies_stored); + initialize_pallet(self.delegations, self.delegation_hierarchies); delegation::StorageVersion::::set(self.storage_version); + + for (claim_hash, details) in self.attestations { + insert_attestation(claim_hash, details) + } }); + ext } From ef81ac489c2582d5f5b7207b41afd6ec89deeb10 Mon Sep 17 00:00:00 2001 From: weichweich Date: Fri, 11 Feb 2022 16:14:49 +0100 Subject: [PATCH 34/60] test: add missing tests --- pallets/delegation/src/access_control.rs | 117 +++++++++++++++++++---- 1 file changed, 100 insertions(+), 17 deletions(-) diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index 626fae8543..bcce3c08c9 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -87,7 +87,7 @@ impl // attestation. `self.sender_node` was supplied by the user. `attester_node` and // `self.sender_node` can be different! - match Pallet::::is_delegating(who, &attester_node_id, self.max_checks)? { + match Pallet::::is_delegating(who, attester_node_id, self.max_checks)? { (true, checks) => Ok(::WeightInfo::can_revoke(checks)), _ => Err(Error::::AccessDenied.into()), } @@ -162,12 +162,7 @@ mod tests { ExtBuilder::default() .with_ctypes(vec![(ctype_hash, root_owner.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - root_owner.clone(), - ACCOUNT_00, - )]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, root_owner, ACCOUNT_00)]) .with_delegations(vec![(parent_id, parent_node)]) .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .build() @@ -415,14 +410,13 @@ mod tests { max_checks: 1, }); - let revoker: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let mut attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); + let mut attestation = generate_base_attestation::(delegate.clone(), ACCOUNT_00); attestation.authorization_id = Some(parent_id.clone()); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(attestation.ctype_hash, revoker.clone())]) + .with_ctypes(vec![(attestation.ctype_hash, delegate.clone())]) .with_delegation_hierarchies(vec![( hierarchy_root_id, hierarchy_details, @@ -434,7 +428,7 @@ mod tests { .build() .execute_with(|| { assert_ok!(Attestation::revoke( - DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), + DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), claim_hash, ac_info )); @@ -443,16 +437,105 @@ mod tests { #[test] fn test_can_revoke_parent() { - todo!() + let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); + let delegate = sr25519_did_from_seed(&BOB_SEED); + + let hierarchy_root_id = get_delegation_hierarchy_id::(true); + let hierarchy_details = generate_base_delegation_hierarchy_details(); + let parent_id = delegation_id_from_seed::(DELEGATION_ID_SEED_1); + let parent_node = DelegationNode { + details: DelegationDetails { + owner: delegate.clone(), + permissions: Permissions::DELEGATE | Permissions::ATTEST, + revoked: false, + }, + children: Default::default(), + hierarchy_root_id, + parent: Some(hierarchy_root_id), + deposit: Deposit { + owner: ACCOUNT_00, + amount: ::Deposit::get(), + }, + }; + let ac_info = Some(DelegationAc { + sender_node_id: parent_id, + max_checks: 1, + }); + + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let mut attestation = generate_base_attestation::(delegate.clone(), ACCOUNT_00); + attestation.authorization_id = Some(parent_id.clone()); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, delegate.clone())]) + .with_delegation_hierarchies(vec![( + hierarchy_root_id, + hierarchy_details, + root_owner.clone(), + ACCOUNT_00, + )]) + .with_delegations(vec![(parent_id, parent_node)]) + .with_attestations(vec![(claim_hash, attestation)]) + .build() + .execute_with(|| { + assert_ok!(Attestation::revoke( + DoubleOrigin(ACCOUNT_00, root_owner.clone()).into(), + claim_hash, + ac_info + )); + }); } #[test] fn test_can_revoke_same_node_revoked() { - todo!() - } + let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); + let delegate = sr25519_did_from_seed(&BOB_SEED); - #[test] - fn test_can_revoke_parent_revoked() { - todo!() + let hierarchy_root_id = get_delegation_hierarchy_id::(true); + let hierarchy_details = generate_base_delegation_hierarchy_details(); + let parent_id = delegation_id_from_seed::(DELEGATION_ID_SEED_1); + let parent_node = DelegationNode { + details: DelegationDetails { + owner: delegate.clone(), + permissions: Permissions::DELEGATE | Permissions::ATTEST, + revoked: true, + }, + children: Default::default(), + hierarchy_root_id, + parent: Some(hierarchy_root_id), + deposit: Deposit { + owner: ACCOUNT_00, + amount: ::Deposit::get(), + }, + }; + let ac_info = Some(DelegationAc { + sender_node_id: parent_id, + max_checks: 1, + }); + + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let mut attestation = generate_base_attestation::(delegate.clone(), ACCOUNT_00); + attestation.authorization_id = Some(parent_id.clone()); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, delegate.clone())]) + .with_delegation_hierarchies(vec![( + hierarchy_root_id, + hierarchy_details, + root_owner.clone(), + ACCOUNT_00, + )]) + .with_delegations(vec![(parent_id, parent_node)]) + .with_attestations(vec![(claim_hash, attestation)]) + .build() + .execute_with(|| { + assert_ok!(Attestation::revoke( + DoubleOrigin(ACCOUNT_00, delegate.clone()).into(), + claim_hash, + ac_info + )); + }); } } From 18595ff9dfbb859c6ec379ebb0e91c3a4427fc99 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 15 Feb 2022 13:36:14 +0100 Subject: [PATCH 35/60] fmt, rename, cleanup --- pallets/attestation/src/tests.rs | 48 ++++++++++++++++---------------- pallets/delegation/src/mock.rs | 1 - 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 9b199daffb..88bcb0e94c 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -36,7 +36,7 @@ fn test_attest_successful() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ctype_hash = get_ctype_hash::(true); - let authorization_id = None; + let authorization_info = None; ExtBuilder::default() .with_ctypes(vec![(ctype_hash, attester.clone())]) @@ -47,7 +47,7 @@ fn test_attest_successful() { DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, ctype_hash, - authorization_id.clone() + authorization_info.clone() )); let stored_attestation = Attestation::attestations(&claim_hash).expect("Attestation should be present on chain."); @@ -56,7 +56,7 @@ fn test_attest_successful() { assert_eq!(stored_attestation.attester, attester); assert_eq!( stored_attestation.authorization_id, - authorization_id.map(|ac| ac.authorization_id()) + authorization_info.map(|ac| ac.authorization_id()) ); assert!(!stored_attestation.revoked); }); @@ -67,7 +67,7 @@ fn test_attest_authorized() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ctype = get_ctype_hash::(true); - let authorization_id = Some(MockAccessControl(attester.clone())); + let authorization_info = Some(MockAccessControl(attester.clone())); ExtBuilder::default() .with_ctypes(vec![(ctype, attester.clone())]) @@ -78,7 +78,7 @@ fn test_attest_authorized() { DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, ctype, - authorization_id.clone() + authorization_info.clone() )); let stored_attestation = Attestation::attestations(&claim_hash).expect("Attestation should be present on chain."); @@ -87,7 +87,7 @@ fn test_attest_authorized() { assert_eq!(stored_attestation.attester, attester); assert_eq!( stored_attestation.authorization_id, - authorization_id.map(|ac| ac.authorization_id()) + authorization_info.map(|ac| ac.authorization_id()) ); assert!(!stored_attestation.revoked); }); @@ -99,7 +99,7 @@ fn test_attest_unauthorized() { let bob: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ctype = get_ctype_hash::(true); - let authorization_id = Some(MockAccessControl(bob)); + let authorization_info = Some(MockAccessControl(bob)); ExtBuilder::default() .with_ctypes(vec![(ctype, attester.clone())]) @@ -111,7 +111,7 @@ fn test_attest_unauthorized() { DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, ctype, - authorization_id + authorization_info ), Err(DispatchError::Other("Unauthorized")) ); @@ -205,7 +205,7 @@ fn test_authorized_revoke() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let revoker: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let authorization_id = Some(MockAccessControl(revoker.clone())); + let authorization_info = Some(MockAccessControl(revoker.clone())); let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); attestation.authorization_id = Some(revoker.clone()); @@ -218,7 +218,7 @@ fn test_authorized_revoke() { assert_ok!(Attestation::revoke( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash, - authorization_id + authorization_info )); let stored_attestation = Attestation::attestations(claim_hash).expect("Attestation should be present on chain."); @@ -235,7 +235,7 @@ fn test_unauthorized_revoke() { let evil: AttesterOf = sr25519_did_from_seed(&CHARLIE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let authorization_id = Some(MockAccessControl(revoker.clone())); + let authorization_info = Some(MockAccessControl(revoker.clone())); let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); attestation.authorization_id = Some(revoker); @@ -246,7 +246,7 @@ fn test_unauthorized_revoke() { .build() .execute_with(|| { assert_noop!( - Attestation::revoke(DoubleOrigin(ACCOUNT_00, evil).into(), claim_hash, authorization_id), + Attestation::revoke(DoubleOrigin(ACCOUNT_00, evil).into(), claim_hash, authorization_info), DispatchError::Other("Unauthorized") ); }); @@ -256,7 +256,7 @@ fn test_unauthorized_revoke() { fn test_revoke_not_found() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let authorization_id = Some(MockAccessControl(revoker.clone())); + let authorization_info = Some(MockAccessControl(revoker.clone())); let attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); ExtBuilder::default() @@ -268,7 +268,7 @@ fn test_revoke_not_found() { Attestation::revoke( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash, - authorization_id + authorization_info ), attestation::Error::::AttestationNotFound ); @@ -279,7 +279,7 @@ fn test_revoke_not_found() { fn test_already_revoked() { let revoker: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let authorization_id = Some(MockAccessControl(revoker.clone())); + let authorization_info = Some(MockAccessControl(revoker.clone())); // Attestation already revoked let mut attestation = generate_base_attestation::(revoker.clone(), ACCOUNT_00); @@ -295,7 +295,7 @@ fn test_already_revoked() { Attestation::revoke( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash, - authorization_id + authorization_info ), attestation::Error::::AlreadyRevoked ); @@ -310,7 +310,7 @@ fn test_remove() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - let authorization_id = None; + let authorization_info = None; ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) @@ -321,7 +321,7 @@ fn test_remove() { assert_ok!(Attestation::remove( DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, - authorization_id + authorization_info )); assert!(Attestation::attestations(claim_hash).is_none()); }); @@ -334,7 +334,7 @@ fn test_remove_authorized() { let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let mut attestation = generate_base_attestation::(attester, ACCOUNT_00); attestation.authorization_id = Some(revoker.clone()); - let authorization_id = Some(MockAccessControl(revoker.clone())); + let authorization_info = Some(MockAccessControl(revoker.clone())); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) @@ -345,9 +345,10 @@ fn test_remove_authorized() { assert_ok!(Attestation::remove( DoubleOrigin(ACCOUNT_00, revoker.clone()).into(), claim_hash, - authorization_id + authorization_info )); assert!(Attestation::attestations(claim_hash).is_none()); + assert!(!Attestation::external_attestations(revoker.clone(), claim_hash)); }); } @@ -357,7 +358,7 @@ fn test_remove_unauthorised() { let evil: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); - let authorization_id = Some(MockAccessControl(evil.clone())); + let authorization_info = Some(MockAccessControl(evil.clone())); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) @@ -369,7 +370,7 @@ fn test_remove_unauthorised() { Attestation::remove( DoubleOrigin(ACCOUNT_00, evil.clone()).into(), claim_hash, - authorization_id + authorization_info ), attestation::Error::::Unauthorized ); @@ -380,7 +381,6 @@ fn test_remove_unauthorised() { fn test_remove_not_found() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); - let authorization_id = Some(MockAccessControl(attester.clone())); let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); ExtBuilder::default() @@ -393,7 +393,7 @@ fn test_remove_not_found() { Attestation::remove( DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, - authorization_id + None ), attestation::Error::::AttestationNotFound ); diff --git a/pallets/delegation/src/mock.rs b/pallets/delegation/src/mock.rs index 438909a137..e2cc86a244 100644 --- a/pallets/delegation/src/mock.rs +++ b/pallets/delegation/src/mock.rs @@ -510,7 +510,6 @@ pub mod runtime { } }); - ext } From 4e4272dd83cbed919ddedc66f6bcf2a45bd560ce Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 15 Feb 2022 14:01:14 +0100 Subject: [PATCH 36/60] external attestations --- pallets/attestation/src/lib.rs | 35 +++++++++++++++++-------------- pallets/attestation/src/mock.rs | 10 +++------ pallets/attestation/src/tests.rs | 36 +++++++++++++++++++++++++++----- 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 723ba066de..23326ae3d5 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -94,9 +94,9 @@ pub use crate::{ pub mod pallet { use super::*; use frame_support::{ + dispatch::{DispatchResult, DispatchResultWithPostInfo}, pallet_prelude::*, traits::{Currency, Get, ReservableCurrency}, - BoundedVec, }; use frame_system::pallet_prelude::*; use sp_runtime::DispatchError; @@ -168,13 +168,9 @@ pub mod pallet { /// /// It maps from a delegation ID to a vector of claim hashes. #[pallet::storage] - #[pallet::getter(fn delegated_attestations)] - pub type DelegatedAttestations = StorageMap< - _, - Blake2_128Concat, - AuthorizationIdOf, - BoundedVec, ::MaxDelegatedAttestations>, - >; + #[pallet::getter(fn external_attestations)] + pub type ExternalAttestations = + StorageDoubleMap<_, Twox64Concat, AuthorizationIdOf, Blake2_128Concat, ClaimHashOf, bool, ValueQuery>; #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] @@ -278,22 +274,26 @@ pub mod pallet { log::debug!("insert Attestation"); - Self::deposit_event(Event::AttestationCreated( - who.clone(), - claim_hash, - ctype_hash, - authorization_id.clone(), - )); Attestations::::insert( &claim_hash, AttestationDetails { ctype_hash, - attester: who, - authorization_id, + attester: who.clone(), + authorization_id: authorization_id.clone(), revoked: false, deposit, }, ); + if let Some(authorization_id) = &authorization_id { + ExternalAttestations::::insert(authorization_id, claim_hash, true); + } + + Self::deposit_event(Event::AttestationCreated( + who, + claim_hash, + ctype_hash, + authorization_id, + )); Ok(()) } @@ -454,6 +454,9 @@ pub mod pallet { fn remove_attestation(attestation: AttestationDetails, claim_hash: ClaimHashOf) { kilt_support::free_deposit::, CurrencyOf>(&attestation.deposit); Attestations::::remove(&claim_hash); + if let Some(authorization_id) = &attestation.authorization_id { + ExternalAttestations::::remove(authorization_id, claim_hash); + } } } } diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index 61b4707a55..cb83308db8 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -140,15 +140,12 @@ where } pub fn insert_attestation(claim_hash: ClaimHashOf, details: AttestationDetails) { - crate::Pallet::::reserve_deposit(details.deposit.owner.clone(), details.deposit.amount).expect("Should have balance"); + crate::Pallet::::reserve_deposit(details.deposit.owner.clone(), details.deposit.amount) + .expect("Should have balance"); crate::Attestations::::insert(&claim_hash, details.clone()); if let Some(delegation_id) = details.authorization_id.as_ref() { - crate::DelegatedAttestations::::try_mutate(delegation_id, |attestations| { - let attestations = attestations.get_or_insert_with(Default::default); - attestations.try_push(claim_hash) - }) - .expect("Couldn't initialise delegated attestation"); + crate::ExternalAttestations::::insert(delegation_id, claim_hash, true) } } @@ -169,7 +166,6 @@ pub(crate) mod runtime { use kilt_support::mock::{mock_origin, SubjectId}; use super::*; - use crate::Pallet; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 88bcb0e94c..307b4bb93a 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -82,6 +82,10 @@ fn test_attest_authorized() { )); let stored_attestation = Attestation::attestations(&claim_hash).expect("Attestation should be present on chain."); + assert!(Attestation::external_attestations( + attester.clone(), + claim_hash + )); assert_eq!(stored_attestation.ctype_hash, ctype); assert_eq!(stored_attestation.attester, attester); @@ -222,6 +226,10 @@ fn test_authorized_revoke() { )); let stored_attestation = Attestation::attestations(claim_hash).expect("Attestation should be present on chain."); + assert!(Attestation::external_attestations( + revoker.clone(), + claim_hash + )); assert!(stored_attestation.revoked); assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); @@ -390,11 +398,7 @@ fn test_remove_not_found() { .execute_with(|| { assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); assert_noop!( - Attestation::remove( - DoubleOrigin(ACCOUNT_00, attester.clone()).into(), - claim_hash, - None - ), + Attestation::remove(DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, None), attestation::Error::::AttestationNotFound ); }); @@ -405,6 +409,28 @@ fn test_remove_not_found() { #[test] fn test_reclaim_deposit() { + let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); + let other_authorized: AttesterOf = sr25519_did_from_seed(&BOB_SEED); + let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); + let mut attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); + attestation.authorization_id = Some(other_authorized.clone()); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) + .with_ctypes(vec![(attestation.ctype_hash, attester)]) + .with_attestations(vec![(claim_hash, attestation)]) + .build() + .execute_with(|| { + assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); + assert_ok!(Attestation::reclaim_deposit(Origin::signed(ACCOUNT_00), claim_hash)); + assert!(!Attestation::external_attestations(other_authorized.clone(), claim_hash)); + assert!(Attestation::attestations(claim_hash).is_none()); + assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); + }); +} + +#[test] +fn test_reclaim_deposit_authorization() { let attester: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let attestation = generate_base_attestation::(attester.clone(), ACCOUNT_00); From 86856998cb7e30fd602473230445408b4bba85f6 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 15 Feb 2022 14:46:03 +0100 Subject: [PATCH 37/60] fix benchmark --- pallets/delegation/src/benchmarking.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pallets/delegation/src/benchmarking.rs b/pallets/delegation/src/benchmarking.rs index b8f3afad3d..80fbe10cc1 100644 --- a/pallets/delegation/src/benchmarking.rs +++ b/pallets/delegation/src/benchmarking.rs @@ -386,15 +386,16 @@ benchmarks! { let claim = Default::default(); let sender: T::AccountId = account("sender", 0, SEED); - let (root_acc, _, _, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE)?; + let (root_acc, _, leaf_acc, leaf_id) = setup_delegations::(c, ONE_CHILD_PER_LEVEL.expect(">0"), Permissions::DELEGATE | Permissions::ATTEST)?; let root_acc: T::DelegationEntityId = root_acc.into(); + let leaf_acc: T::DelegationEntityId = leaf_acc.into(); let ac = DelegationAc::{ sender_node_id: leaf_id, max_checks: c }; - }: { ac.can_attest(&root_acc, &ctype, &claim).expect("Should be allowed") } + }: { ac.can_attest(&leaf_acc, &ctype, &claim).expect("Should be allowed") } verify { } From bfdaf2c86b37092f000d252d90601043bbff1fc4 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 15 Feb 2022 16:01:49 +0100 Subject: [PATCH 38/60] clippy --- pallets/attestation/src/lib.rs | 7 +--- pallets/attestation/src/mock.rs | 3 ++ pallets/attestation/src/tests.rs | 15 +++---- pallets/delegation/src/access_control.rs | 50 +++++------------------- pallets/delegation/src/mock.rs | 6 +++ pallets/did/src/mock.rs | 6 +++ pallets/kilt-launch/src/mock.rs | 4 ++ pallets/pallet-did-lookup/src/mock.rs | 2 + pallets/pallet-web3-names/src/mock.rs | 3 ++ pallets/parachain-staking/src/lib.rs | 5 +-- pallets/parachain-staking/src/mock.rs | 5 +++ pallets/parachain-staking/src/tests.rs | 4 +- 12 files changed, 50 insertions(+), 60 deletions(-) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 23326ae3d5..0ff7dc391e 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -288,12 +288,7 @@ pub mod pallet { ExternalAttestations::::insert(authorization_id, claim_hash, true); } - Self::deposit_event(Event::AttestationCreated( - who, - claim_hash, - ctype_hash, - authorization_id, - )); + Self::deposit_event(Event::AttestationCreated(who, claim_hash, ctype_hash, authorization_id)); Ok(()) } diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index cb83308db8..b169fc13b0 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -322,16 +322,19 @@ pub(crate) mod runtime { } impl ExtBuilder { + #[must_use] pub fn with_ctypes(mut self, ctypes: Vec<(CtypeHashOf, CtypeCreatorOf)>) -> Self { self.ctypes = ctypes; self } + #[must_use] pub fn with_balances(mut self, balances: Vec<(AccountIdOf, BalanceOf)>) -> Self { self.balances = balances; self } + #[must_use] pub fn with_attestations(mut self, attestations: Vec<(ClaimHashOf, AttestationDetails)>) -> Self { self.attestations = attestations; self diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 307b4bb93a..53e579b344 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -82,10 +82,7 @@ fn test_attest_authorized() { )); let stored_attestation = Attestation::attestations(&claim_hash).expect("Attestation should be present on chain."); - assert!(Attestation::external_attestations( - attester.clone(), - claim_hash - )); + assert!(Attestation::external_attestations(attester.clone(), claim_hash)); assert_eq!(stored_attestation.ctype_hash, ctype); assert_eq!(stored_attestation.attester, attester); @@ -226,10 +223,7 @@ fn test_authorized_revoke() { )); let stored_attestation = Attestation::attestations(claim_hash).expect("Attestation should be present on chain."); - assert!(Attestation::external_attestations( - revoker.clone(), - claim_hash - )); + assert!(Attestation::external_attestations(revoker.clone(), claim_hash)); assert!(stored_attestation.revoked); assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); @@ -423,7 +417,10 @@ fn test_reclaim_deposit() { .execute_with(|| { assert_eq!(Balances::reserved_balance(ACCOUNT_00), ::Deposit::get()); assert_ok!(Attestation::reclaim_deposit(Origin::signed(ACCOUNT_00), claim_hash)); - assert!(!Attestation::external_attestations(other_authorized.clone(), claim_hash)); + assert!(!Attestation::external_attestations( + other_authorized.clone(), + claim_hash + )); assert!(Attestation::attestations(claim_hash).is_none()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); }); diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index bcce3c08c9..5efbbde2da 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -217,12 +217,7 @@ mod tests { ExtBuilder::default() .with_ctypes(vec![(ctype_hash, root_owner.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - root_owner.clone(), - ACCOUNT_00, - )]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, root_owner, ACCOUNT_00)]) .with_delegations(vec![(parent_id, parent_node)]) .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .build() @@ -270,12 +265,7 @@ mod tests { ExtBuilder::default() .with_ctypes(vec![(ctype_hash, root_owner.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - root_owner.clone(), - ACCOUNT_00, - )]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, root_owner, ACCOUNT_00)]) .with_delegations(vec![(parent_id, parent_node)]) .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .build() @@ -309,12 +299,7 @@ mod tests { ExtBuilder::default() .with_ctypes(vec![(ctype_hash, root_owner.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - root_owner.clone(), - ACCOUNT_00, - )]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, root_owner, ACCOUNT_00)]) .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .build() .execute_with(|| { @@ -361,12 +346,7 @@ mod tests { ExtBuilder::default() .with_ctypes(vec![(ctype_hash, root_owner.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - root_owner.clone(), - ACCOUNT_00, - )]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, root_owner, ACCOUNT_00)]) .with_delegations(vec![(parent_id, parent_node)]) .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .build() @@ -412,17 +392,12 @@ mod tests { let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let mut attestation = generate_base_attestation::(delegate.clone(), ACCOUNT_00); - attestation.authorization_id = Some(parent_id.clone()); + attestation.authorization_id = Some(parent_id); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .with_ctypes(vec![(attestation.ctype_hash, delegate.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - root_owner.clone(), - ACCOUNT_00, - )]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, root_owner, ACCOUNT_00)]) .with_delegations(vec![(parent_id, parent_node)]) .with_attestations(vec![(claim_hash, attestation)]) .build() @@ -464,11 +439,11 @@ mod tests { let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let mut attestation = generate_base_attestation::(delegate.clone(), ACCOUNT_00); - attestation.authorization_id = Some(parent_id.clone()); + attestation.authorization_id = Some(parent_id); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) - .with_ctypes(vec![(attestation.ctype_hash, delegate.clone())]) + .with_ctypes(vec![(attestation.ctype_hash, delegate)]) .with_delegation_hierarchies(vec![( hierarchy_root_id, hierarchy_details, @@ -516,17 +491,12 @@ mod tests { let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let mut attestation = generate_base_attestation::(delegate.clone(), ACCOUNT_00); - attestation.authorization_id = Some(parent_id.clone()); + attestation.authorization_id = Some(parent_id); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, ::Deposit::get() * 100)]) .with_ctypes(vec![(attestation.ctype_hash, delegate.clone())]) - .with_delegation_hierarchies(vec![( - hierarchy_root_id, - hierarchy_details, - root_owner.clone(), - ACCOUNT_00, - )]) + .with_delegation_hierarchies(vec![(hierarchy_root_id, hierarchy_details, root_owner, ACCOUNT_00)]) .with_delegations(vec![(parent_id, parent_node)]) .with_attestations(vec![(claim_hash, attestation)]) .build() diff --git a/pallets/delegation/src/mock.rs b/pallets/delegation/src/mock.rs index e2cc86a244..2bfb38053e 100644 --- a/pallets/delegation/src/mock.rs +++ b/pallets/delegation/src/mock.rs @@ -453,6 +453,7 @@ pub mod runtime { } impl ExtBuilder { + #[must_use] pub fn with_delegation_hierarchies( mut self, delegation_hierarchies: DelegationHierarchyInitialization, @@ -461,26 +462,31 @@ pub mod runtime { self } + #[must_use] pub fn with_balances(mut self, balances: Vec<(AccountIdOf, BalanceOf)>) -> Self { self.balances = balances; self } + #[must_use] pub fn with_ctypes(mut self, ctypes: Vec<(CtypeHashOf, SubjectId)>) -> Self { self.ctypes = ctypes; self } + #[must_use] pub fn with_delegations(mut self, delegations: Vec<(DelegationNodeIdOf, DelegationNode)>) -> Self { self.delegations = delegations; self } + #[must_use] pub fn with_attestations(mut self, attestations: Vec<(ClaimHashOf, AttestationDetails)>) -> Self { self.attestations = attestations; self } + #[must_use] pub fn with_storage_version(mut self, storage_version: DelegationStorageVersion) -> Self { self.storage_version = storage_version; self diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index 2e9740bdec..914e122238 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -431,31 +431,37 @@ pub struct ExtBuilder { } impl ExtBuilder { + #[must_use] pub fn with_dids(mut self, dids: Vec<(TestDidIdentifier, DidDetails)>) -> Self { self.dids_stored = dids; self } + #[must_use] pub fn with_endpoints(mut self, endpoints: Vec<(TestDidIdentifier, Vec>)>) -> Self { self.service_endpoints = endpoints; self } + #[must_use] pub(crate) fn with_balances(mut self, balances: Vec<(AccountIdOf, Balance)>) -> Self { self.balances = balances; self } + #[must_use] pub fn with_ctypes(mut self, ctypes: Vec<(TestCtypeHash, TestCtypeOwner)>) -> Self { self.ctypes_stored = ctypes; self } + #[must_use] pub fn with_deleted_dids(mut self, dids: Vec) -> Self { self.deleted_dids = dids; self } + #[must_use] pub fn with_storage_version(mut self, storage_version: DidStorageVersion) -> Self { self.storage_version = storage_version; self diff --git a/pallets/kilt-launch/src/mock.rs b/pallets/kilt-launch/src/mock.rs index b3015893bf..8782cabaef 100644 --- a/pallets/kilt-launch/src/mock.rs +++ b/pallets/kilt-launch/src/mock.rs @@ -298,11 +298,13 @@ pub fn assert_balance(who: AccountId, free: Balance, usable_for_fees: Balance, u } impl ExtBuilder { + #[must_use] pub fn vest(mut self, vesting: Vec<(AccountId, BlockNumber, Balance)>) -> Self { self.vesting = vesting; self } + #[must_use] pub fn pseudos_vest_all(self) -> Self { self.vest(vec![ (PSEUDO_1, 10, 10_000), @@ -311,6 +313,7 @@ impl ExtBuilder { ]) } + #[must_use] pub fn lock_balance(mut self, balance_locks: Vec<(AccountId, BlockNumber, Balance)>) -> Self { self.balance_locks = balance_locks; self @@ -320,6 +323,7 @@ impl ExtBuilder { self.lock_balance(vec![(PSEUDO_1, 100, 1111), (PSEUDO_2, 1337, 2222)]) } + #[must_use] pub fn pseudos_lock_all(self) -> Self { self.lock_balance(vec![ (PSEUDO_1, 100, 10_000), diff --git a/pallets/pallet-did-lookup/src/mock.rs b/pallets/pallet-did-lookup/src/mock.rs index 529f565eeb..568aec4af9 100644 --- a/pallets/pallet-did-lookup/src/mock.rs +++ b/pallets/pallet-did-lookup/src/mock.rs @@ -128,11 +128,13 @@ pub struct ExtBuilder { } impl ExtBuilder { + #[must_use] pub fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self { self.balances = balances; self } + #[must_use] pub fn with_connections(mut self, connections: Vec<(AccountId, SubjectId, AccountId)>) -> Self { self.connections = connections; self diff --git a/pallets/pallet-web3-names/src/mock.rs b/pallets/pallet-web3-names/src/mock.rs index d7bad03d90..1d1ddb4886 100644 --- a/pallets/pallet-web3-names/src/mock.rs +++ b/pallets/pallet-web3-names/src/mock.rs @@ -156,16 +156,19 @@ pub struct ExtBuilder { } impl ExtBuilder { + #[must_use] pub fn with_balances(mut self, balances: Vec<(TestWeb3NamePayer, Balance)>) -> Self { self.balances = balances; self } + #[must_use] pub fn with_web3_names(mut self, web3_names: Vec<(TestWeb3NameOwner, TestWeb3Name, TestWeb3NamePayer)>) -> Self { self.claimed_web3_names = web3_names; self } + #[must_use] pub fn with_banned_web3_names(mut self, web3_names: Vec) -> Self { self.banned_web3_names = web3_names; self diff --git a/pallets/parachain-staking/src/lib.rs b/pallets/parachain-staking/src/lib.rs index 5ec86ef8c7..312eb54836 100644 --- a/pallets/parachain-staking/src/lib.rs +++ b/pallets/parachain-staking/src/lib.rs @@ -1485,7 +1485,7 @@ pub mod pallet { ensure!(amount >= T::MinDelegatorStake::get(), Error::::NomStakeBelowMin); // cannot be a collator candidate and delegator with same AccountId - ensure!(!Self::is_active_candidate(&acc).is_some(), Error::::CandidateExists); + ensure!(Self::is_active_candidate(&acc).is_none(), Error::::CandidateExists); ensure!( Unstaking::::get(&acc).len().saturated_into::() < T::MaxUnstakeRequests::get(), Error::::CannotJoinBeforeUnlocking @@ -2139,8 +2139,7 @@ pub mod pallet { ) -> Option<(BalanceOf, BalanceOf)> { top_candidates .get(index) - .map(|stake| CandidatePool::::get(&stake.owner)) - .flatten() + .and_then(|stake| CandidatePool::::get(&stake.owner)) // SAFETY: the total is always more than the stake .map(|state| (state.stake, state.total - state.stake)) } diff --git a/pallets/parachain-staking/src/mock.rs b/pallets/parachain-staking/src/mock.rs index fcb671d90f..c3eb3960e8 100644 --- a/pallets/parachain-staking/src/mock.rs +++ b/pallets/parachain-staking/src/mock.rs @@ -246,21 +246,25 @@ impl Default for ExtBuilder { } impl ExtBuilder { + #[must_use] pub(crate) fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self { self.balances = balances; self } + #[must_use] pub(crate) fn with_collators(mut self, collators: Vec<(AccountId, Balance)>) -> Self { self.collators = collators; self } + #[must_use] pub(crate) fn with_delegators(mut self, delegators: Vec<(AccountId, AccountId, Balance)>) -> Self { self.delegators = delegators; self } + #[must_use] pub(crate) fn with_inflation( mut self, col_max: u64, @@ -280,6 +284,7 @@ impl ExtBuilder { self } + #[must_use] pub(crate) fn set_blocks_per_round(mut self, blocks_per_round: BlockNumber) -> Self { self.blocks_per_round = blocks_per_round; self diff --git a/pallets/parachain-staking/src/tests.rs b/pallets/parachain-staking/src/tests.rs index 8a1f3baefe..f9ead1d482 100644 --- a/pallets/parachain-staking/src/tests.rs +++ b/pallets/parachain-staking/src/tests.rs @@ -887,7 +887,7 @@ fn execute_leave_candidates_with_delay() { *collator )); assert!(StakePallet::candidate_pool(&collator).is_none()); - assert!(!StakePallet::is_active_candidate(collator).is_some()); + assert!(StakePallet::is_active_candidate(collator).is_none()); assert_eq!(StakePallet::unstaking(collator).len(), 1); } assert_eq!(CandidatePool::::count(), 5, "Five collators left."); @@ -906,7 +906,7 @@ fn execute_leave_candidates_with_delay() { collator )); assert!(StakePallet::candidate_pool(&collator).is_none()); - assert!(!StakePallet::is_active_candidate(&collator).is_some()); + assert!(StakePallet::is_active_candidate(&collator).is_none()); assert_eq!(StakePallet::unstaking(collator).len(), 1); } assert_eq!(CandidatePool::::count(), 2, "3 collators left."); From 897e18983d44828e7fde3037ca6700934d7aa539 Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Wed, 16 Feb 2022 07:09:06 +0100 Subject: [PATCH 39/60] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=spiritnet-dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/spiritnet/src/weights/delegation.rs --template=.maintain/runtime-weight-template.hbs --- runtimes/spiritnet/src/weights/delegation.rs | 54 ++++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/runtimes/spiritnet/src/weights/delegation.rs b/runtimes/spiritnet/src/weights/delegation.rs index b10bb51a76..0033e8fc8b 100644 --- a/runtimes/spiritnet/src/weights/delegation.rs +++ b/runtimes/spiritnet/src/weights/delegation.rs @@ -19,8 +19,8 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-01-25, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("spiritnet-dev"), DB CACHE: 128 +//! DATE: 2022-02-16, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("spiritnet-dev"), DB CACHE: 1024 // Executed Command: // target/release/kilt-parachain @@ -52,25 +52,25 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (45_498_000 as Weight) + (38_033_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (53_812_000 as Weight) + (45_250_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (20_242_000 as Weight) + (19_326_000 as Weight) // Standard Error: 52_000 - .saturating_add((17_467_000 as Weight).saturating_mul(r as Weight)) + .saturating_add((13_435_000 as Weight).saturating_mul(r as Weight)) // Standard Error: 52_000 - .saturating_add((179_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((116_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) @@ -78,9 +78,9 @@ impl delegation::WeightInfo for WeightInfo { // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(_r: u32, c: u32, ) -> Weight { - (38_384_000 as Weight) - // Standard Error: 28_000 - .saturating_add((5_370_000 as Weight).saturating_mul(c as Weight)) + (33_073_000 as Weight) + // Standard Error: 36_000 + .saturating_add((5_138_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -89,9 +89,9 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (62_215_000 as Weight) - // Standard Error: 94_000 - .saturating_add((31_974_000 as Weight).saturating_mul(r as Weight)) + (52_347_000 as Weight) + // Standard Error: 64_000 + .saturating_add((23_524_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -101,33 +101,33 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (53_556_000 as Weight) - // Standard Error: 76_000 - .saturating_add((31_906_000 as Weight).saturating_mul(r as Weight)) + (45_221_000 as Weight) + // Standard Error: 56_000 + .saturating_add((23_309_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } - // Storage: Delegation DelegationNodes (r:2 w:0) - fn can_attest( ) -> Weight { - (8_421_000 as Weight) - // Standard Error: 18_000 - .saturating_add(T::DbWeight::get().reads(1 as Weight)) + // Storage: Delegation DelegationNodes (r:1 w:0) + // Storage: Delegation DelegationHierarchies (r:1 w:0) + fn can_attest() -> Weight { + (12_477_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { - (8_605_000 as Weight) - // Standard Error: 18_000 - .saturating_add((5_452_000 as Weight).saturating_mul(c as Weight)) + (8_318_000 as Weight) + // Standard Error: 41_000 + .saturating_add((5_093_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_remove(c: u32, ) -> Weight { - (8_663_000 as Weight) - // Standard Error: 22_000 - .saturating_add((5_427_000 as Weight).saturating_mul(c as Weight)) + (7_933_000 as Weight) + // Standard Error: 30_000 + .saturating_add((5_204_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } From 70c0fe8fc2cda7fb93eee95927c3df45f0ab46f9 Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Thu, 17 Feb 2022 15:38:23 +0100 Subject: [PATCH 40/60] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=pallets/delegation/src/default_weights.rs --template=.maintain/weight-template.hbs --- pallets/delegation/src/default_weights.rs | 115 +++++++++++----------- 1 file changed, 58 insertions(+), 57 deletions(-) diff --git a/pallets/delegation/src/default_weights.rs b/pallets/delegation/src/default_weights.rs index 44260fd919..12977df98d 100644 --- a/pallets/delegation/src/default_weights.rs +++ b/pallets/delegation/src/default_weights.rs @@ -19,8 +19,8 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-01-25, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 +//! DATE: 2022-02-17, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: // target/release/kilt-parachain @@ -65,25 +65,25 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (46_899_000 as Weight) + (38_014_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (55_555_000 as Weight) + (45_899_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (22_158_000 as Weight) - // Standard Error: 56_000 - .saturating_add((17_812_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 56_000 - .saturating_add((65_000 as Weight).saturating_mul(c as Weight)) + (19_600_000 as Weight) + // Standard Error: 59_000 + .saturating_add((13_287_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 59_000 + .saturating_add((110_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) @@ -91,11 +91,11 @@ impl WeightInfo for SubstrateWeight { // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (39_004_000 as Weight) - // Standard Error: 31_000 - .saturating_add((101_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 31_000 - .saturating_add((5_945_000 as Weight).saturating_mul(c as Weight)) + (32_503_000 as Weight) + // Standard Error: 36_000 + .saturating_add((49_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 36_000 + .saturating_add((5_096_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -104,9 +104,9 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (64_475_000 as Weight) - // Standard Error: 68_000 - .saturating_add((32_285_000 as Weight).saturating_mul(r as Weight)) + (52_473_000 as Weight) + // Standard Error: 51_000 + .saturating_add((23_018_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -116,32 +116,33 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (55_017_000 as Weight) - // Standard Error: 84_000 - .saturating_add((32_384_000 as Weight).saturating_mul(r as Weight)) + (45_240_000 as Weight) + // Standard Error: 71_000 + .saturating_add((23_136_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } - // Storage: Delegation DelegationNodes (r:2 w:0) + // Storage: Delegation DelegationNodes (r:1 w:0) + // Storage: Delegation DelegationHierarchies (r:1 w:0) fn can_attest() -> Weight { - (9_002_000 as Weight) + (12_438_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { - (9_119_000 as Weight) - // Standard Error: 36_000 - .saturating_add((5_889_000 as Weight).saturating_mul(c as Weight)) + (8_298_000 as Weight) + // Standard Error: 39_000 + .saturating_add((5_061_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_remove(c: u32, ) -> Weight { - (9_343_000 as Weight) - // Standard Error: 30_000 - .saturating_add((5_832_000 as Weight).saturating_mul(c as Weight)) + (8_248_000 as Weight) + // Standard Error: 51_000 + .saturating_add((5_103_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } @@ -154,25 +155,25 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (46_899_000 as Weight) + (38_014_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (55_555_000 as Weight) + (45_899_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (22_158_000 as Weight) - // Standard Error: 56_000 - .saturating_add((17_812_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 56_000 - .saturating_add((65_000 as Weight).saturating_mul(c as Weight)) + (19_600_000 as Weight) + // Standard Error: 59_000 + .saturating_add((13_287_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 59_000 + .saturating_add((110_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) @@ -180,11 +181,11 @@ impl WeightInfo for () { // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (39_004_000 as Weight) - // Standard Error: 31_000 - .saturating_add((101_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 31_000 - .saturating_add((5_945_000 as Weight).saturating_mul(c as Weight)) + (32_503_000 as Weight) + // Standard Error: 36_000 + .saturating_add((49_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 36_000 + .saturating_add((5_096_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) @@ -193,9 +194,9 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (64_475_000 as Weight) - // Standard Error: 68_000 - .saturating_add((32_285_000 as Weight).saturating_mul(r as Weight)) + (52_473_000 as Weight) + // Standard Error: 51_000 + .saturating_add((23_018_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) @@ -205,33 +206,33 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (55_017_000 as Weight) - // Standard Error: 84_000 - .saturating_add((32_384_000 as Weight).saturating_mul(r as Weight)) + (45_240_000 as Weight) + // Standard Error: 71_000 + .saturating_add((23_136_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } - // Storage: Delegation DelegationNodes (r:2 w:0) - fn can_attest( ) -> Weight { - (9_002_000 as Weight) - // Standard Error: 26_000 - .saturating_add(RocksDbWeight::get().reads(1 as Weight)) + // Storage: Delegation DelegationNodes (r:1 w:0) + // Storage: Delegation DelegationHierarchies (r:1 w:0) + fn can_attest() -> Weight { + (12_438_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { - (9_119_000 as Weight) - // Standard Error: 36_000 - .saturating_add((5_889_000 as Weight).saturating_mul(c as Weight)) + (8_298_000 as Weight) + // Standard Error: 39_000 + .saturating_add((5_061_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_remove(c: u32, ) -> Weight { - (9_343_000 as Weight) - // Standard Error: 30_000 - .saturating_add((5_832_000 as Weight).saturating_mul(c as Weight)) + (8_248_000 as Weight) + // Standard Error: 51_000 + .saturating_add((5_103_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } From e85e85b328b8a2e02a53c1c5e3606a494a47acf8 Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Thu, 17 Feb 2022 15:41:42 +0100 Subject: [PATCH 41/60] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/peregrine/src/weights/delegation.rs --template=.maintain/runtime-weight-template.hbs --- runtimes/peregrine/src/weights/delegation.rs | 99 ++++++++++---------- 1 file changed, 47 insertions(+), 52 deletions(-) diff --git a/runtimes/peregrine/src/weights/delegation.rs b/runtimes/peregrine/src/weights/delegation.rs index db872b9f1a..ddf4448058 100644 --- a/runtimes/peregrine/src/weights/delegation.rs +++ b/runtimes/peregrine/src/weights/delegation.rs @@ -19,8 +19,8 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-11-08, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 +//! DATE: 2022-02-17, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: // target/release/kilt-parachain @@ -36,7 +36,6 @@ // --output=./runtimes/peregrine/src/weights/delegation.rs // --template=.maintain/runtime-weight-template.hbs - #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] #![allow(unused_imports)] @@ -53,25 +52,25 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (48_478_000 as Weight) + (37_835_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (59_829_000 as Weight) + (45_830_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (23_761_000 as Weight) - // Standard Error: 52_000 - .saturating_add((19_062_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 52_000 - .saturating_add((43_000 as Weight).saturating_mul(c as Weight)) + (20_121_000 as Weight) + // Standard Error: 49_000 + .saturating_add((13_621_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 49_000 + .saturating_add((11_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) @@ -79,11 +78,11 @@ impl delegation::WeightInfo for WeightInfo { // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (41_094_000 as Weight) - // Standard Error: 27_000 - .saturating_add((41_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 27_000 - .saturating_add((5_758_000 as Weight).saturating_mul(c as Weight)) + (32_825_000 as Weight) + // Standard Error: 34_000 + .saturating_add((52_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 34_000 + .saturating_add((5_203_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -92,50 +91,46 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (63_362_000 as Weight) - // Standard Error: 83_000 - .saturating_add((38_970_000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2 as Weight)) - .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) - .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) + (53_132_000 as Weight) + // Standard Error: 60_000 + .saturating_add((23_707_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (53_098_000 as Weight) - // Standard Error: 75_000 - .saturating_add((39_041_000 as Weight).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) - .saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(r as Weight))) + (45_719_000 as Weight) + // Standard Error: 55_000 + .saturating_add((23_684_000 as Weight).saturating_mul(r as Weight)) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } - - // TODO: run benchmarks - fn can_attest( ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().writes(2_u64)) + // Storage: Delegation DelegationNodes (r:1 w:0) + // Storage: Delegation DelegationHierarchies (r:1 w:0) + fn can_attest() -> Weight { + (12_752_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) } - fn can_revoke(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_revoke(c: u32, ) -> Weight { + (8_235_000 as Weight) + // Standard Error: 34_000 + .saturating_add((5_151_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } - fn can_remove(r: u32, ) -> Weight { - (61_171_000_u64) - // Standard Error: 99_000 - .saturating_add((37_949_000_u64).saturating_mul(r as Weight)) - .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(r as Weight))) - .saturating_add(T::DbWeight::get().writes(2_u64)) - .saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(r as Weight))) + // Storage: Delegation DelegationNodes (r:2 w:0) + fn can_remove(c: u32, ) -> Weight { + (8_320_000 as Weight) + // Standard Error: 26_000 + .saturating_add((5_154_000 as Weight).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } } From 1016572af6dc9a4138bdeec705153437010a2e0b Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 21 Feb 2022 09:26:23 +0100 Subject: [PATCH 42/60] remove unused and restrict visibility --- pallets/delegation/src/mock.rs | 50 ++++++++++++++++------------------ 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/pallets/delegation/src/mock.rs b/pallets/delegation/src/mock.rs index ae15f59bcb..b096f56a8f 100644 --- a/pallets/delegation/src/mock.rs +++ b/pallets/delegation/src/mock.rs @@ -168,7 +168,7 @@ where } #[cfg(test)] -pub mod runtime { +pub(crate) mod runtime { use crate::{BalanceOf, DelegateSignatureTypeOf, DelegationAc, DelegationNodeIdOf}; use super::*; @@ -190,19 +190,18 @@ pub mod runtime { signature::EqualVerify, }; - pub type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; - pub type Block = frame_system::mocking::MockBlock; + pub(crate) type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; + pub(crate) type Block = frame_system::mocking::MockBlock; - pub type Hash = sp_core::H256; - pub type Balance = u128; - pub type Signature = MultiSignature; - pub type AccountPublic = ::Signer; - pub type AccountId = ::AccountId; + pub(crate) type Hash = sp_core::H256; + pub(crate) type Balance = u128; + pub(crate) type Signature = MultiSignature; + pub(crate) type AccountPublic = ::Signer; + pub(crate) type AccountId = ::AccountId; - pub const UNIT: Balance = 10u128.pow(15); - pub const MILLI_UNIT: Balance = 10u128.pow(12); - pub const DELEGATION_DEPOSIT: Balance = 10 * MILLI_UNIT; - pub const ATTESTATION_DEPOSIT: Balance = 10 * MILLI_UNIT; + pub(crate) const MILLI_UNIT: Balance = 10u128.pow(12); + pub(crate) const DELEGATION_DEPOSIT: Balance = 10 * MILLI_UNIT; + pub(crate) const ATTESTATION_DEPOSIT: Balance = 10 * MILLI_UNIT; frame_support::construct_runtime!( pub enum Test where @@ -348,20 +347,19 @@ pub mod runtime { pub(crate) const BOB_SEED: [u8; 32] = [1u8; 32]; pub(crate) const CHARLIE_SEED: [u8; 32] = [2u8; 32]; - pub const CLAIM_HASH_SEED_01: u64 = 1u64; - pub const CLAIM_HASH_SEED_02: u64 = 2u64; + pub(crate) const CLAIM_HASH_SEED_01: u64 = 1u64; - pub fn claim_hash_from_seed(seed: u64) -> Hash { + pub(crate) fn claim_hash_from_seed(seed: u64) -> Hash { Hash::from_low_u64_be(seed) } - pub fn ed25519_did_from_seed(seed: &[u8; 32]) -> SubjectId { + pub(crate) fn ed25519_did_from_seed(seed: &[u8; 32]) -> SubjectId { MultiSigner::from(ed25519::Pair::from_seed(seed).public()) .into_account() .into() } - pub fn sr25519_did_from_seed(seed: &[u8; 32]) -> SubjectId { + pub(crate) fn sr25519_did_from_seed(seed: &[u8; 32]) -> SubjectId { MultiSigner::from(sr25519::Pair::from_seed(seed).public()) .into_account() .into() @@ -371,7 +369,7 @@ pub mod runtime { hash.encode() } - pub struct DelegationCreationOperation { + pub(crate) struct DelegationCreationOperation { pub delegation_id: DelegationNodeIdOf, pub hierarchy_id: DelegationNodeIdOf, pub parent_id: DelegationNodeIdOf, @@ -380,7 +378,7 @@ pub mod runtime { pub delegate_signature: DelegateSignatureTypeOf, } - pub fn generate_base_delegation_creation_operation( + pub(crate) fn generate_base_delegation_creation_operation( delegation_id: DelegationNodeIdOf, delegate_signature: DelegateSignatureTypeOf, delegation_node: DelegationNode, @@ -397,29 +395,29 @@ pub mod runtime { } } - pub struct DelegationHierarchyRevocationOperation { + pub(crate) struct DelegationHierarchyRevocationOperation { pub id: DelegationNodeIdOf, pub max_children: u32, } - pub fn generate_base_delegation_hierarchy_revocation_operation( + pub(crate) fn generate_base_delegation_hierarchy_revocation_operation( id: DelegationNodeIdOf, ) -> DelegationHierarchyRevocationOperation { DelegationHierarchyRevocationOperation { id, max_children: 0u32 } } - pub struct DelegationRevocationOperation { + pub(crate) struct DelegationRevocationOperation { pub delegation_id: DelegationNodeIdOf, pub max_parent_checks: u32, pub max_revocations: u32, } - pub struct DelegationDepositClaimOperation { + pub(crate) struct DelegationDepositClaimOperation { pub delegation_id: DelegationNodeIdOf, pub max_removals: u32, } - pub fn generate_base_delegation_revocation_operation( + pub(crate) fn generate_base_delegation_revocation_operation( delegation_id: DelegationNodeIdOf, ) -> DelegationRevocationOperation { DelegationRevocationOperation { @@ -429,7 +427,7 @@ pub mod runtime { } } - pub fn generate_base_delegation_deposit_claim_operation( + pub(crate) fn generate_base_delegation_deposit_claim_operation( delegation_id: DelegationNodeIdOf, ) -> DelegationDepositClaimOperation { DelegationDepositClaimOperation { @@ -439,7 +437,7 @@ pub mod runtime { } #[derive(Clone, Default)] - pub struct ExtBuilder { + pub(crate) struct ExtBuilder { /// endowed accounts with balances balances: Vec<(AccountIdOf, BalanceOf)>, /// initial ctypes & owners From 351e9e4bba294bc264d183288d2d83b7a0ab47ac Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 21 Feb 2022 09:50:57 +0100 Subject: [PATCH 43/60] remove default implementation --- pallets/attestation/src/access_control.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pallets/attestation/src/access_control.rs b/pallets/attestation/src/access_control.rs index 1cdb266d71..a94d79502e 100644 --- a/pallets/attestation/src/access_control.rs +++ b/pallets/attestation/src/access_control.rs @@ -55,19 +55,13 @@ pub trait AttestationAccessControl AuthorizationId; /// The worst-case weight of `can_attest`. - fn can_attest_weight(&self) -> Weight { - 0 - } + fn can_attest_weight(&self) -> Weight; /// The worst-case weight of `can_revoke`. - fn can_revoke_weight(&self) -> Weight { - 0 - } + fn can_revoke_weight(&self) -> Weight; /// The worst-case weight of `can_remove`. - fn can_remove_weight(&self) -> Weight { - 0 - } + fn can_remove_weight(&self) -> Weight; } impl From 4a4998752a71b36f46c643f1e041d8e5c3d57e31 Mon Sep 17 00:00:00 2001 From: Albrecht Date: Mon, 21 Feb 2022 10:48:58 +0100 Subject: [PATCH 44/60] Update pallets/attestation/src/tests.rs Co-authored-by: William Freudenberger --- pallets/attestation/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 53e579b344..37b6154841 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -355,7 +355,7 @@ fn test_remove_authorized() { } #[test] -fn test_remove_unauthorised() { +fn test_remove_unauthorized() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let evil: AttesterOf = sr25519_did_from_seed(&BOB_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); From c99455b4a7caebb96c89cc2e4774e6775d1e03c0 Mon Sep 17 00:00:00 2001 From: Albrecht Date: Mon, 21 Feb 2022 10:49:33 +0100 Subject: [PATCH 45/60] Update pallets/delegation/src/access_control.rs Co-authored-by: William Freudenberger --- pallets/delegation/src/access_control.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index 5efbbde2da..fdd6c403d8 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -83,10 +83,8 @@ impl _claim: &ClaimHashOf, attester_node_id: &DelegationNodeIdOf, ) -> Result { - // `attester_node` was supplied by the attestation pallet and is stored in the - // attestation. `self.sender_node` was supplied by the user. `attester_node` and - // `self.sender_node` can be different! - + // NOTE: The node IDs of the sender (provided by the user through `who`) and attester (provided + // by the attestation pallet through on-chain storage) can be different! match Pallet::::is_delegating(who, attester_node_id, self.max_checks)? { (true, checks) => Ok(::WeightInfo::can_revoke(checks)), _ => Err(Error::::AccessDenied.into()), From c07b421f74871d924731c5103588761c4d0fe1f2 Mon Sep 17 00:00:00 2001 From: Albrecht Date: Mon, 21 Feb 2022 10:50:43 +0100 Subject: [PATCH 46/60] Update pallets/delegation/src/access_control.rs Co-authored-by: William Freudenberger --- pallets/delegation/src/access_control.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index fdd6c403d8..504033db0c 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -185,7 +185,7 @@ mod tests { } #[test] - fn test_can_attest_missing_permission() { + fn test_cannot_attest_missing_permission() { let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); let delegate = sr25519_did_from_seed(&BOB_SEED); From 90764e1025b46b90531448fc0a097cafd81e6708 Mon Sep 17 00:00:00 2001 From: Albrecht Date: Mon, 21 Feb 2022 10:50:51 +0100 Subject: [PATCH 47/60] Update pallets/delegation/src/access_control.rs Co-authored-by: William Freudenberger --- pallets/delegation/src/access_control.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index 504033db0c..f47e551625 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -314,7 +314,7 @@ mod tests { } #[test] - fn test_can_attest_wrong_ctype() { + fn test_cannot_attest_wrong_ctype() { let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); let delegate = sr25519_did_from_seed(&BOB_SEED); From edb4b949895082d3957fcf4417e85460beae9b17 Mon Sep 17 00:00:00 2001 From: Albrecht Date: Mon, 21 Feb 2022 10:50:58 +0100 Subject: [PATCH 48/60] Update pallets/delegation/src/access_control.rs Co-authored-by: William Freudenberger --- pallets/delegation/src/access_control.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index f47e551625..8b0ed3dce6 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -281,7 +281,7 @@ mod tests { } #[test] - fn test_can_attest_missing_node() { + fn test_cannot_attest_missing_node() { let root_owner: DelegatorIdOf = sr25519_did_from_seed(&ALICE_SEED); let delegate = sr25519_did_from_seed(&BOB_SEED); From 9b16948c963ce5021437046678185cd79dbd463f Mon Sep 17 00:00:00 2001 From: Albrecht Date: Mon, 21 Feb 2022 13:47:44 +0100 Subject: [PATCH 49/60] Update pallets/attestation/src/tests.rs Co-authored-by: William Freudenberger --- pallets/attestation/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 37b6154841..15bf21a3aa 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -32,7 +32,7 @@ use crate::{ // add #[test] -fn test_attest_successful() { +fn test_attest_without_auth() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ctype_hash = get_ctype_hash::(true); From e1d37ce7c17e35d507bfbd226a13fb5476bea42d Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 21 Feb 2022 13:47:25 +0100 Subject: [PATCH 50/60] doc: add comment --- pallets/attestation/src/mock.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index b169fc13b0..51665c2b28 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -74,6 +74,7 @@ where } } +/// Authorize iff the subject of the origin and the provided attester id match. #[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq, Eq)] #[scale_info(skip_type_params(T))] pub struct MockAccessControl(pub T::AttesterId); From c5fec0c9ba40eba90e06c66746fc6e8d6722383d Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 21 Feb 2022 13:48:28 +0100 Subject: [PATCH 51/60] sender -> subject --- pallets/attestation/src/tests.rs | 2 +- pallets/delegation/src/access_control.rs | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pallets/attestation/src/tests.rs b/pallets/attestation/src/tests.rs index 15bf21a3aa..0e7794b9ea 100644 --- a/pallets/attestation/src/tests.rs +++ b/pallets/attestation/src/tests.rs @@ -32,7 +32,7 @@ use crate::{ // add #[test] -fn test_attest_without_auth() { +fn test_attest_without_authorization() { let attester: AttesterOf = sr25519_did_from_seed(&ALICE_SEED); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ctype_hash = get_ctype_hash::(true); diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index 8b0ed3dce6..7018daa9e5 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -44,7 +44,7 @@ use crate::{ /// Can remove attestations if #[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] pub struct DelegationAc { - pub(crate) sender_node_id: DelegationNodeIdOf, + pub(crate) subject_node_id: DelegationNodeIdOf, pub(crate) max_checks: u32, } @@ -58,7 +58,7 @@ impl ctype: &CtypeHashOf, _claim: &ClaimHashOf, ) -> Result { - let delegation_node = DelegationNodes::::get(self.sender_node_id).ok_or(Error::::DelegationNotFound)?; + let delegation_node = DelegationNodes::::get(self.authorization_id()).ok_or(Error::::DelegationNotFound)?; let root = DelegationHierarchies::::get(delegation_node.hierarchy_root_id).ok_or(Error::::DelegationNotFound)?; ensure!( @@ -102,7 +102,7 @@ impl } fn authorization_id(&self) -> DelegationNodeIdOf { - self.sender_node_id + self.subject_node_id } fn can_attest_weight(&self) -> Weight { @@ -154,7 +154,7 @@ mod tests { }; let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ac_info = Some(DelegationAc { - sender_node_id: parent_id, + subject_node_id: parent_id, max_checks: 1, }); @@ -209,7 +209,7 @@ mod tests { }; let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ac_info = Some(DelegationAc { - sender_node_id: parent_id, + subject_node_id: parent_id, max_checks: 1, }); @@ -257,7 +257,7 @@ mod tests { }; let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ac_info = Some(DelegationAc { - sender_node_id: parent_id, + subject_node_id: parent_id, max_checks: 1, }); @@ -291,7 +291,7 @@ mod tests { let parent_id = delegation_id_from_seed::(DELEGATION_ID_SEED_1); let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ac_info = Some(DelegationAc { - sender_node_id: parent_id, + subject_node_id: parent_id, max_checks: 1, }); @@ -338,7 +338,7 @@ mod tests { }; let claim_hash = claim_hash_from_seed(CLAIM_HASH_SEED_01); let ac_info = Some(DelegationAc { - sender_node_id: parent_id, + subject_node_id: parent_id, max_checks: 1, }); @@ -384,7 +384,7 @@ mod tests { }, }; let ac_info = Some(DelegationAc { - sender_node_id: parent_id, + subject_node_id: parent_id, max_checks: 1, }); @@ -431,7 +431,7 @@ mod tests { }, }; let ac_info = Some(DelegationAc { - sender_node_id: parent_id, + subject_node_id: parent_id, max_checks: 1, }); @@ -483,7 +483,7 @@ mod tests { }, }; let ac_info = Some(DelegationAc { - sender_node_id: parent_id, + subject_node_id: parent_id, max_checks: 1, }); From e675ecc7ef7caa82c9b9810be996997b15b86717 Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 21 Feb 2022 14:04:04 +0100 Subject: [PATCH 52/60] standalone AC --- pallets/delegation/src/access_control.rs | 8 +++++--- pallets/delegation/src/benchmarking.rs | 6 +++--- runtimes/standalone/src/lib.rs | 6 ++++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index 7018daa9e5..4f9fb0b2e2 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -58,7 +58,8 @@ impl ctype: &CtypeHashOf, _claim: &ClaimHashOf, ) -> Result { - let delegation_node = DelegationNodes::::get(self.authorization_id()).ok_or(Error::::DelegationNotFound)?; + let delegation_node = + DelegationNodes::::get(self.authorization_id()).ok_or(Error::::DelegationNotFound)?; let root = DelegationHierarchies::::get(delegation_node.hierarchy_root_id).ok_or(Error::::DelegationNotFound)?; ensure!( @@ -83,8 +84,9 @@ impl _claim: &ClaimHashOf, attester_node_id: &DelegationNodeIdOf, ) -> Result { - // NOTE: The node IDs of the sender (provided by the user through `who`) and attester (provided - // by the attestation pallet through on-chain storage) can be different! + // NOTE: The node IDs of the sender (provided by the user through `who`) and + // attester (provided by the attestation pallet through on-chain storage) can be + // different! match Pallet::::is_delegating(who, attester_node_id, self.max_checks)? { (true, checks) => Ok(::WeightInfo::can_revoke(checks)), _ => Err(Error::::AccessDenied.into()), diff --git a/pallets/delegation/src/benchmarking.rs b/pallets/delegation/src/benchmarking.rs index 80fbe10cc1..aadb2001c4 100644 --- a/pallets/delegation/src/benchmarking.rs +++ b/pallets/delegation/src/benchmarking.rs @@ -391,7 +391,7 @@ benchmarks! { let leaf_acc: T::DelegationEntityId = leaf_acc.into(); let ac = DelegationAc::{ - sender_node_id: leaf_id, + subject_node_id: leaf_id, max_checks: c }; @@ -410,7 +410,7 @@ benchmarks! { let root_acc: T::DelegationEntityId = root_acc.into(); let ac = DelegationAc::{ - sender_node_id: leaf_id, + subject_node_id: leaf_id, max_checks: c }; @@ -429,7 +429,7 @@ benchmarks! { let root_acc: T::DelegationEntityId = root_acc.into(); let ac = DelegationAc::{ - sender_node_id: leaf_id, + subject_node_id: leaf_id, max_checks: c }; diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index a2a08faa3a..d29429580e 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -28,6 +28,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); use codec::{Decode, Encode, MaxEncodedLen}; +use delegation::DelegationAc; use frame_support::traits::InstanceFilter; pub use frame_support::{ construct_runtime, parameter_types, @@ -63,6 +64,7 @@ pub use did; pub use pallet_balances::Call as BalancesCall; pub use pallet_web3_names; use runtime_common::{ + authorization::{AuthorizationId, PalletAuthorize}, constants::{self, KILT, MICRO_KILT, MILLI_KILT}, fees::ToAuthor, pallet_id, AccountId, Balance, BlockNumber, DidIdentifier, Hash, Index, Signature, SlowAdjustingFeeUpdate, @@ -321,8 +323,8 @@ impl attestation::Config for Runtime { type Deposit = AttestationDeposit; type MaxDelegatedAttestations = MaxDelegatedAttestations; type AttesterId = DidIdentifier; - type AuthorizationId = (); - type AccessControl = (); + type AuthorizationId = AuthorizationId<::DelegationNodeId>; + type AccessControl = PalletAuthorize>; } parameter_types! { From f4ba6532ba596f6c07e863e9503bcafa57cf4664 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 22 Feb 2022 10:07:46 +0100 Subject: [PATCH 53/60] MaxEncodedLen --- pallets/attestation/src/lib.rs | 4 ++-- pallets/delegation/src/access_control.rs | 4 ++-- runtimes/common/src/authorization.rs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 9178243b9b..56a5b40a60 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -144,9 +144,9 @@ pub mod pallet { #[pallet::constant] type MaxDelegatedAttestations: Get; - type AttesterId: Parameter; + type AttesterId: Parameter + MaxEncodedLen; - type AuthorizationId: Parameter; + type AuthorizationId: Parameter + MaxEncodedLen; type AccessControl: Parameter + AttestationAccessControl, ClaimHashOf>; diff --git a/pallets/delegation/src/access_control.rs b/pallets/delegation/src/access_control.rs index 4f9fb0b2e2..3702cd4c14 100644 --- a/pallets/delegation/src/access_control.rs +++ b/pallets/delegation/src/access_control.rs @@ -16,7 +16,7 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use codec::{Decode, Encode}; +use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::{dispatch::Weight, ensure}; use scale_info::TypeInfo; use sp_runtime::DispatchError; @@ -42,7 +42,7 @@ use crate::{ /// stored in the attestation /// /// Can remove attestations if -#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] +#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo, MaxEncodedLen)] pub struct DelegationAc { pub(crate) subject_node_id: DelegationNodeIdOf, pub(crate) max_checks: u32, diff --git a/runtimes/common/src/authorization.rs b/runtimes/common/src/authorization.rs index 5514547d29..50b45e1961 100644 --- a/runtimes/common/src/authorization.rs +++ b/runtimes/common/src/authorization.rs @@ -15,19 +15,19 @@ // along with this program. If not, see . // If you feel like getting in touch with us, you can do so at info@botlabs.org -use codec::{Decode, Encode}; +use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::dispatch::Weight; use scale_info::TypeInfo; use sp_runtime::DispatchError; use attestation::AttestationAccessControl; -#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] +#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo, MaxEncodedLen)] pub enum AuthorizationId { Delegation(DelegationId), } -#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] +#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo, MaxEncodedLen)] pub enum PalletAuthorize { Delegation(DelegationAc), } From b85d39301b373e4924279f1bd4ca86cd612193e8 Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Tue, 22 Feb 2022 11:35:45 +0100 Subject: [PATCH 54/60] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/peregrine/src/weights/delegation.rs --template=.maintain/runtime-weight-template.hbs --- runtimes/peregrine/src/weights/delegation.rs | 52 ++++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/runtimes/peregrine/src/weights/delegation.rs b/runtimes/peregrine/src/weights/delegation.rs index ddf4448058..aca6480d81 100644 --- a/runtimes/peregrine/src/weights/delegation.rs +++ b/runtimes/peregrine/src/weights/delegation.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-02-17, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-02-22, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: @@ -52,25 +52,25 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (37_835_000 as Weight) + (37_795_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (45_830_000 as Weight) + (45_303_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (20_121_000 as Weight) - // Standard Error: 49_000 - .saturating_add((13_621_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 49_000 - .saturating_add((11_000 as Weight).saturating_mul(c as Weight)) + (18_819_000 as Weight) + // Standard Error: 50_000 + .saturating_add((13_562_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 50_000 + .saturating_add((180_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) @@ -78,11 +78,11 @@ impl delegation::WeightInfo for WeightInfo { // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (32_825_000 as Weight) - // Standard Error: 34_000 - .saturating_add((52_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 34_000 - .saturating_add((5_203_000 as Weight).saturating_mul(c as Weight)) + (32_036_000 as Weight) + // Standard Error: 35_000 + .saturating_add((133_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 35_000 + .saturating_add((5_171_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -91,9 +91,9 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (53_132_000 as Weight) - // Standard Error: 60_000 - .saturating_add((23_707_000 as Weight).saturating_mul(r as Weight)) + (52_158_000 as Weight) + // Standard Error: 67_000 + .saturating_add((23_383_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -103,9 +103,9 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (45_719_000 as Weight) - // Standard Error: 55_000 - .saturating_add((23_684_000 as Weight).saturating_mul(r as Weight)) + (44_630_000 as Weight) + // Standard Error: 82_000 + .saturating_add((23_457_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -114,22 +114,22 @@ impl delegation::WeightInfo for WeightInfo { // Storage: Delegation DelegationNodes (r:1 w:0) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn can_attest() -> Weight { - (12_752_000 as Weight) + (12_490_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { - (8_235_000 as Weight) - // Standard Error: 34_000 - .saturating_add((5_151_000 as Weight).saturating_mul(c as Weight)) + (8_147_000 as Weight) + // Standard Error: 37_000 + .saturating_add((5_115_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_remove(c: u32, ) -> Weight { - (8_320_000 as Weight) - // Standard Error: 26_000 - .saturating_add((5_154_000 as Weight).saturating_mul(c as Weight)) + (8_136_000 as Weight) + // Standard Error: 25_000 + .saturating_add((5_056_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } From 4f551742e7d0d9cad78644014fe0f98e1be1e595 Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Tue, 22 Feb 2022 11:38:43 +0100 Subject: [PATCH 55/60] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=pallets/delegation/src/default_weights.rs --template=.maintain/weight-template.hbs --- pallets/delegation/src/default_weights.rs | 102 +++++++++++----------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/pallets/delegation/src/default_weights.rs b/pallets/delegation/src/default_weights.rs index 12977df98d..ea0ee5080e 100644 --- a/pallets/delegation/src/default_weights.rs +++ b/pallets/delegation/src/default_weights.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-02-17, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ +//! DATE: 2022-02-22, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: @@ -65,25 +65,25 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (38_014_000 as Weight) + (37_014_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (45_899_000 as Weight) + (44_986_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (19_600_000 as Weight) - // Standard Error: 59_000 - .saturating_add((13_287_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 59_000 - .saturating_add((110_000 as Weight).saturating_mul(c as Weight)) + (19_082_000 as Weight) + // Standard Error: 51_000 + .saturating_add((13_360_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 51_000 + .saturating_add((169_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) @@ -91,11 +91,11 @@ impl WeightInfo for SubstrateWeight { // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (32_503_000 as Weight) - // Standard Error: 36_000 - .saturating_add((49_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 36_000 - .saturating_add((5_096_000 as Weight).saturating_mul(c as Weight)) + (32_789_000 as Weight) + // Standard Error: 41_000 + .saturating_add((13_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 41_000 + .saturating_add((5_095_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -104,9 +104,9 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (52_473_000 as Weight) - // Standard Error: 51_000 - .saturating_add((23_018_000 as Weight).saturating_mul(r as Weight)) + (51_863_000 as Weight) + // Standard Error: 59_000 + .saturating_add((23_235_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -116,9 +116,9 @@ impl WeightInfo for SubstrateWeight { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (45_240_000 as Weight) - // Standard Error: 71_000 - .saturating_add((23_136_000 as Weight).saturating_mul(r as Weight)) + (44_669_000 as Weight) + // Standard Error: 56_000 + .saturating_add((23_133_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -127,22 +127,22 @@ impl WeightInfo for SubstrateWeight { // Storage: Delegation DelegationNodes (r:1 w:0) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn can_attest() -> Weight { - (12_438_000 as Weight) + (12_484_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { - (8_298_000 as Weight) - // Standard Error: 39_000 - .saturating_add((5_061_000 as Weight).saturating_mul(c as Weight)) + (8_127_000 as Weight) + // Standard Error: 38_000 + .saturating_add((5_164_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_remove(c: u32, ) -> Weight { - (8_248_000 as Weight) - // Standard Error: 51_000 - .saturating_add((5_103_000 as Weight).saturating_mul(c as Weight)) + (7_991_000 as Weight) + // Standard Error: 35_000 + .saturating_add((5_193_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } @@ -155,25 +155,25 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (38_014_000 as Weight) + (37_014_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (45_899_000 as Weight) + (44_986_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (19_600_000 as Weight) - // Standard Error: 59_000 - .saturating_add((13_287_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 59_000 - .saturating_add((110_000 as Weight).saturating_mul(c as Weight)) + (19_082_000 as Weight) + // Standard Error: 51_000 + .saturating_add((13_360_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 51_000 + .saturating_add((169_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) @@ -181,11 +181,11 @@ impl WeightInfo for () { // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { - (32_503_000 as Weight) - // Standard Error: 36_000 - .saturating_add((49_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 36_000 - .saturating_add((5_096_000 as Weight).saturating_mul(c as Weight)) + (32_789_000 as Weight) + // Standard Error: 41_000 + .saturating_add((13_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 41_000 + .saturating_add((5_095_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) @@ -194,9 +194,9 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (52_473_000 as Weight) - // Standard Error: 51_000 - .saturating_add((23_018_000 as Weight).saturating_mul(r as Weight)) + (51_863_000 as Weight) + // Standard Error: 59_000 + .saturating_add((23_235_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) @@ -206,9 +206,9 @@ impl WeightInfo for () { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (45_240_000 as Weight) - // Standard Error: 71_000 - .saturating_add((23_136_000 as Weight).saturating_mul(r as Weight)) + (44_669_000 as Weight) + // Standard Error: 56_000 + .saturating_add((23_133_000 as Weight).saturating_mul(r as Weight)) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) @@ -217,22 +217,22 @@ impl WeightInfo for () { // Storage: Delegation DelegationNodes (r:1 w:0) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn can_attest() -> Weight { - (12_438_000 as Weight) + (12_484_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { - (8_298_000 as Weight) - // Standard Error: 39_000 - .saturating_add((5_061_000 as Weight).saturating_mul(c as Weight)) + (8_127_000 as Weight) + // Standard Error: 38_000 + .saturating_add((5_164_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_remove(c: u32, ) -> Weight { - (8_248_000 as Weight) - // Standard Error: 51_000 - .saturating_add((5_103_000 as Weight).saturating_mul(c as Weight)) + (7_991_000 as Weight) + // Standard Error: 35_000 + .saturating_add((5_193_000 as Weight).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } From 4290c3a45f0acbc6e62cd6c6b9fadcd53de608c2 Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Tue, 22 Feb 2022 11:42:02 +0100 Subject: [PATCH 56/60] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=spiritnet-dev --steps=50 --repeat=20 --pallet=delegation --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/spiritnet/src/weights/delegation.rs --template=.maintain/runtime-weight-template.hbs --- runtimes/spiritnet/src/weights/delegation.rs | 52 ++++++++++---------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/runtimes/spiritnet/src/weights/delegation.rs b/runtimes/spiritnet/src/weights/delegation.rs index 0033e8fc8b..ba9555e94b 100644 --- a/runtimes/spiritnet/src/weights/delegation.rs +++ b/runtimes/spiritnet/src/weights/delegation.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for delegation //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-02-16, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-02-22, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("spiritnet-dev"), DB CACHE: 1024 // Executed Command: @@ -52,35 +52,37 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationNodes (r:0 w:1) fn create_hierarchy() -> Weight { - (38_033_000 as Weight) + (37_923_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:2) // Storage: System Account (r:1 w:1) fn add_delegation() -> Weight { - (45_250_000 as Weight) + (45_375_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Delegation DelegationNodes (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn revoke_delegation_root_child(r: u32, c: u32, ) -> Weight { - (19_326_000 as Weight) - // Standard Error: 52_000 - .saturating_add((13_435_000 as Weight).saturating_mul(r as Weight)) - // Standard Error: 52_000 - .saturating_add((116_000 as Weight).saturating_mul(c as Weight)) + (19_130_000 as Weight) + // Standard Error: 51_000 + .saturating_add((13_475_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 51_000 + .saturating_add((148_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(r as Weight))) } // Storage: Delegation DelegationNodes (r:6 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:0) - fn revoke_delegation_leaf(_r: u32, c: u32, ) -> Weight { - (33_073_000 as Weight) - // Standard Error: 36_000 - .saturating_add((5_138_000 as Weight).saturating_mul(c as Weight)) + fn revoke_delegation_leaf(r: u32, c: u32, ) -> Weight { + (32_418_000 as Weight) + // Standard Error: 39_000 + .saturating_add((71_000 as Weight).saturating_mul(r as Weight)) + // Standard Error: 39_000 + .saturating_add((5_175_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -89,9 +91,9 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:1 w:1) fn remove_delegation(r: u32, ) -> Weight { - (52_347_000 as Weight) - // Standard Error: 64_000 - .saturating_add((23_524_000 as Weight).saturating_mul(r as Weight)) + (52_774_000 as Weight) + // Standard Error: 83_000 + .saturating_add((22_979_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -101,9 +103,9 @@ impl delegation::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Delegation DelegationHierarchies (r:0 w:1) fn reclaim_deposit(r: u32, ) -> Weight { - (45_221_000 as Weight) - // Standard Error: 56_000 - .saturating_add((23_309_000 as Weight).saturating_mul(r as Weight)) + (44_951_000 as Weight) + // Standard Error: 78_000 + .saturating_add((23_162_000 as Weight).saturating_mul(r as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(r as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -112,22 +114,22 @@ impl delegation::WeightInfo for WeightInfo { // Storage: Delegation DelegationNodes (r:1 w:0) // Storage: Delegation DelegationHierarchies (r:1 w:0) fn can_attest() -> Weight { - (12_477_000 as Weight) + (12_360_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_revoke(c: u32, ) -> Weight { - (8_318_000 as Weight) - // Standard Error: 41_000 - .saturating_add((5_093_000 as Weight).saturating_mul(c as Weight)) + (8_163_000 as Weight) + // Standard Error: 45_000 + .saturating_add((5_153_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } // Storage: Delegation DelegationNodes (r:2 w:0) fn can_remove(c: u32, ) -> Weight { - (7_933_000 as Weight) - // Standard Error: 30_000 - .saturating_add((5_204_000 as Weight).saturating_mul(c as Weight)) + (8_543_000 as Weight) + // Standard Error: 29_000 + .saturating_add((5_026_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(c as Weight))) } From daf5803b5d3d48b8afeb270f636bde139d336186 Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 1 Mar 2022 08:43:05 +0100 Subject: [PATCH 57/60] add test to ensure no migration is needed --- pallets/attestation/src/attestations.rs | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/pallets/attestation/src/attestations.rs b/pallets/attestation/src/attestations.rs index b0be8cbe6f..677d10377f 100644 --- a/pallets/attestation/src/attestations.rs +++ b/pallets/attestation/src/attestations.rs @@ -41,3 +41,58 @@ pub struct AttestationDetails { /// storage. pub deposit: Deposit, BalanceOf>, } + +#[cfg(test)] +mod tests { + use super::*; + use crate::mock::*; + + /// Old Attestation + #[derive(Clone, Debug, Encode, Decode, PartialEq, TypeInfo, MaxEncodedLen)] + #[scale_info(skip_type_params(T))] + #[codec(mel_bound())] + pub struct OldAttestationDetails { + /// The hash of the CType used for this attestation. + pub ctype_hash: CtypeHashOf, + /// The ID of the attester. + pub attester: AttesterOf, + /// \[OPTIONAL\] The ID of the delegation node used to authorize the + /// attester. + pub delegation_id: Option<[u8; 32]>, + /// The flag indicating whether the attestation has been revoked or not. + pub revoked: bool, + /// The deposit that was taken to incentivise fair use of the on chain + /// storage. + pub deposit: Deposit, BalanceOf>, + } + + #[test] + fn test_no_need_to_migrate_if_none() { + let old = OldAttestationDetails:: { + ctype_hash: claim_hash_from_seed(CLAIM_HASH_SEED_01), + attester: sr25519_did_from_seed(&ALICE_SEED), + delegation_id: None, + revoked: true, + deposit: Deposit { + owner: ACCOUNT_00, + amount: ATTESTATION_DEPOSIT, + }, + }; + let encoded = old.encode(); + + let new = AttestationDetails::::decode(&mut &encoded[..]); + assert_eq!( + new, + Ok(AttestationDetails:: { + ctype_hash: claim_hash_from_seed(CLAIM_HASH_SEED_01), + attester: sr25519_did_from_seed(&ALICE_SEED), + authorization_id: None, + revoked: true, + deposit: Deposit { + owner: ACCOUNT_00, + amount: ATTESTATION_DEPOSIT, + }, + }) + ); + } +} From e0be5f481aa7bc4775374ffdb1e46dc19fa78f7c Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 1 Mar 2022 08:59:01 +0100 Subject: [PATCH 58/60] fix benchmarks and attestation size. Attestation got bigger because of the nested enum --- pallets/attestation/src/benchmarking.rs | 1 - runtimes/common/src/constants.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pallets/attestation/src/benchmarking.rs b/pallets/attestation/src/benchmarking.rs index e00a6d29a7..8b4c123c74 100644 --- a/pallets/attestation/src/benchmarking.rs +++ b/pallets/attestation/src/benchmarking.rs @@ -32,7 +32,6 @@ benchmarks! { where T: core::fmt::Debug, ::EnsureOrigin: GenerateBenchmarkOrigin, - T::AttesterId: Default, T: ctype::Config, } diff --git a/runtimes/common/src/constants.rs b/runtimes/common/src/constants.rs index 1796682914..fe0fadfa65 100644 --- a/runtimes/common/src/constants.rs +++ b/runtimes/common/src/constants.rs @@ -100,7 +100,7 @@ pub mod attestation { use super::*; /// The size is checked in the runtime by a test. - pub const MAX_ATTESTATION_BYTE_LENGTH: u32 = 178; + pub const MAX_ATTESTATION_BYTE_LENGTH: u32 = 179; pub const ATTESTATION_DEPOSIT: Balance = deposit(2, MAX_ATTESTATION_BYTE_LENGTH); } From 8c3f594691a6abdd7327938d3896036f650979be Mon Sep 17 00:00:00 2001 From: weichweich Date: Tue, 1 Mar 2022 09:44:53 +0100 Subject: [PATCH 59/60] storage is never used, no migration needed --- pallets/attestation/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pallets/attestation/src/lib.rs b/pallets/attestation/src/lib.rs index 56a5b40a60..707a508b0c 100644 --- a/pallets/attestation/src/lib.rs +++ b/pallets/attestation/src/lib.rs @@ -167,7 +167,6 @@ pub mod pallet { #[pallet::getter(fn attestations)] pub type Attestations = StorageMap<_, Blake2_128Concat, ClaimHashOf, AttestationDetails>; - // TODO: Migrate... Either delete or rename /// Delegated attestations stored on chain. /// /// It maps from a delegation ID to a vector of claim hashes. From fffd0e189a6f74547f361cc55776221c6e737077 Mon Sep 17 00:00:00 2001 From: weichweich Date: Thu, 17 Mar 2022 14:34:17 +0100 Subject: [PATCH 60/60] =?UTF-8?q?=F0=9F=A7=B9=F0=9F=90=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pallets/attestation/src/mock.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pallets/attestation/src/mock.rs b/pallets/attestation/src/mock.rs index 5bdfef64e3..51665c2b28 100644 --- a/pallets/attestation/src/mock.rs +++ b/pallets/attestation/src/mock.rs @@ -325,7 +325,6 @@ pub(crate) mod runtime { impl ExtBuilder { #[must_use] pub fn with_ctypes(mut self, ctypes: Vec<(CtypeHashOf, CtypeCreatorOf)>) -> Self { - self.ctypes = ctypes; self }