From 5bf70b976c9f3edf3eb98d2cf4c905700ba0d469 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 20 Oct 2021 15:33:03 +0200 Subject: [PATCH 01/45] wip: add support for service endpoints --- pallets/did/src/did_details.rs | 37 +++++++++++------------ pallets/did/src/errors.rs | 6 ++++ pallets/did/src/lib.rs | 54 ++++++++++++++++++++++++++++++---- pallets/did/src/utils.rs | 53 +++++++++++++++++++++++++++++++-- 4 files changed, 124 insertions(+), 26 deletions(-) diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index 77a9ef1cb..831ee5250 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -21,7 +21,7 @@ use frame_support::storage::{bounded_btree_map::BoundedBTreeMap, bounded_btree_s use kilt_support::deposit::Deposit; use sp_core::{ecdsa, ed25519, sr25519}; use sp_runtime::{traits::Verify, MultiSignature}; -use sp_std::{convert::TryInto, fmt}; +use sp_std::convert::TryInto; use crate::*; @@ -531,13 +531,29 @@ impl DidDetails { } } +// pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; +pub type ServiceEndpointId = Vec; +// pub type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; +pub type ServiceEndpointType = Vec; +// pub type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; +pub type ServiceEndpointUrl = Vec; + +#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq)] +pub struct DidEndpointDetails { + phantom_data: sp_std::marker::PhantomData, + pub(crate) id: ServiceEndpointId, + pub(crate) service_type: [ServiceEndpointType; 5], + pub(crate) url: Vec, +} + pub(crate) type DidNewKeyAgreementKeySet = BoundedBTreeSet::MaxNewKeyAgreementKeys>; pub(crate) type DidKeyAgreementKeySet = BoundedBTreeSet, ::MaxTotalKeyAgreementKeys>; pub(crate) type DidPublicKeyMap = BoundedBTreeMap, DidPublicKeyDetails, ::MaxPublicKeysPerDid>; +// pub(crate) type DidNewServiceEndpoints = BoundedBTreeSet, ::MaxDidServicesCount>; /// The details of a new DID to create. -#[derive(Clone, Decode, Encode, PartialEq)] +#[derive(Clone, Debug, Decode, Encode, PartialEq)] pub struct DidCreationDetails { /// The DID identifier. It has to be unique. pub did: DidIdentifierOf, @@ -549,22 +565,7 @@ pub struct DidCreationDetails { pub new_attestation_key: Option, /// \[OPTIONAL\] The new delegation key. pub new_delegation_key: Option, -} - -// required because BoundedTreeSet does not implement Debug outside of std -impl fmt::Debug for DidCreationDetails { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("DidCreationDetails") - .field("did", &self.did) - .field("submitter", &self.submitter) - .field( - "new_key_agreement_keys", - &self.new_key_agreement_keys.clone().into_inner(), - ) - .field("new_attestation_key", &self.new_attestation_key) - .field("new_delegation_key", &self.new_delegation_key) - .finish() - } + pub new_service_details: Vec>, } /// Trait for extrinsic DID-based authorization. diff --git a/pallets/did/src/errors.rs b/pallets/did/src/errors.rs index 44cc22dfb..c2a41f02b 100644 --- a/pallets/did/src/errors.rs +++ b/pallets/did/src/errors.rs @@ -92,4 +92,10 @@ pub enum InputError { /// A number of new verification keys to remove greater than the maximum /// allowed has been provided. MaxVerificationKeysToRemoveLimitExceeded, + MaxServicesCountExceeded, + MaxUrlCountExceeded, + MaxTypeCountExceeded, + MaxIdLengthExceeded, + MaxUrlLengthExceeded, + MaxTypeLengthExceeded, } diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index ae2afe98e..d20443e58 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -247,6 +247,24 @@ pub mod pallet { /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; + + #[pallet::constant] + type MaxServiceIdLength: Get; + + #[pallet::constant] + type MaxServiceTypeLength: Get; + + #[pallet::constant] + type MaxServiceUrlLength: Get; + + #[pallet::constant] + type MaxTypeCountPerService: Get; + + #[pallet::constant] + type MaxUrlCountPerService: Get; + + #[pallet::constant] + type MaxDidServicesCount: Get; } #[pallet::pallet] @@ -277,6 +295,13 @@ pub mod pallet { #[pallet::getter(fn get_did)] pub type Did = StorageMap<_, Blake2_128Concat, DidIdentifierOf, DidDetails>; + /// Service endpoints associated with DIDs. + /// + /// It maps from (DID identifier, service ID) to the service details. + #[pallet::storage] + #[pallet::getter(fn get_service_endpoints)] + pub type ServiceEndpoints = StorageDoubleMap<_, Twox64Concat, DidIdentifierOf, Blake2_128Concat, Vec, DidEndpointDetails>; + /// The set of DIDs that have been deleted and cannot therefore be created /// again for security reasons. /// @@ -346,10 +371,6 @@ pub mod pallet { /// A number of new verification keys to remove greater than the maximum /// allowed has been provided. MaxVerificationKeysToRemoveLimitExceeded, - /// A URL longer than the maximum size allowed has been provided. - MaxUrlLengthExceeded, - /// More than the maximum number of URLs have been specified. - MaxUrlsCountExceeded, /// The maximum number of public keys for this DID key identifier has /// been reached. MaxPublicKeysPerDidExceeded, @@ -362,12 +383,18 @@ pub mod pallet { TransactionExpired, /// The DID has already been previously deleted. DidAlreadyDeleted, - /// An error that is not supposed to take place, yet it happened. - InternalError, /// Only the owner of the deposit can reclaim its reserved balance. NotOwnerOfDeposit, /// The origin is unable to reserve the deposit and pay the fee. UnableToPayFees, + MaxIdLengthExceeded, + MaxUrlLengthExceeded, + MaxTypeLengthExceeded, + MaxUrlCountExceeded, + MaxTypeCountExceeded, + MaxServicesCountExceeded, + /// An error that is not supposed to take place, yet it happened. + InternalError, } impl From for Error { @@ -411,6 +438,12 @@ pub mod pallet { match error { InputError::MaxKeyAgreementKeysLimitExceeded => Self::MaxKeyAgreementKeysLimitExceeded, InputError::MaxVerificationKeysToRemoveLimitExceeded => Self::MaxVerificationKeysToRemoveLimitExceeded, + InputError::MaxIdLengthExceeded => Self::MaxIdLengthExceeded, + InputError::MaxServicesCountExceeded => Self::MaxServicesCountExceeded, + InputError::MaxTypeCountExceeded => Self::MaxTypeCountExceeded, + InputError::MaxTypeLengthExceeded => Self::MaxTypeLengthExceeded, + InputError::MaxUrlCountExceeded => Self::MaxUrlCountExceeded, + InputError::MaxUrlLengthExceeded => Self::MaxUrlLengthExceeded, } } } @@ -476,6 +509,10 @@ pub mod pallet { .verify_and_recover_signature(&details.encode(), &signature) .map_err(Error::::from)?; + // Validate all the size constraints for the service endpoints. + let input_service_endpoints = details.new_service_details.clone(); + utils::validate_service_endpoints(&input_service_endpoints).map_err(Error::::from)?; + let did_entry = DidDetails::from_creation_details(details, account_did_auth_key).map_err(Error::::from)?; @@ -494,8 +531,13 @@ pub mod pallet { log::debug!("Creating DID {:?}", &did_identifier); T::FeeCollector::on_unbalanced(imbalance); + Did::::insert(&did_identifier, did_entry); + input_service_endpoints.iter().for_each(|service| { + ServiceEndpoints::::insert(&did_identifier, &service.id, service); + }); + Self::deposit_event(Event::DidCreated(sender, did_identifier)); Ok(()) diff --git a/pallets/did/src/utils.rs b/pallets/did/src/utils.rs index ac28fdc9d..202ae2f70 100644 --- a/pallets/did/src/utils.rs +++ b/pallets/did/src/utils.rs @@ -17,12 +17,61 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org use codec::Encode; -use sp_runtime::traits::Hash; +use sp_runtime::{traits::{Hash, SaturatedConversion}}; use sp_std::vec::Vec; -use crate::{Config, DidPublicKey, KeyIdOf}; +use frame_support::{ensure, traits::Get}; + +use crate::{Config, DidPublicKey, KeyIdOf, InputError, DidEndpointDetails}; pub fn calculate_key_id(key: &DidPublicKey) -> KeyIdOf { let hashed_values: Vec = key.encode(); T::Hashing::hash(&hashed_values) } + +pub(crate) fn validate_service_endpoints(endpoints: &[DidEndpointDetails]) -> Result<(), InputError> { + // Check if the maximum number of endpoints is provided + ensure!( + endpoints.len() <= T::MaxDidServicesCount::get().saturated_into(), + InputError::MaxServicesCountExceeded + ); + + // For each service... + endpoints.iter().try_for_each(|endpoint| { + // Check that the maximum number of service types is provided. + ensure!( + endpoint.service_type.len() <= T::MaxTypeCountPerService::get().saturated_into(), + InputError::MaxTypeCountExceeded + ); + // Check that the maximum number of URLs is provided. + ensure!( + endpoint.url.len() <= T::MaxUrlCountPerService::get().saturated_into(), + InputError::MaxUrlCountExceeded + ); + // Check that the ID is the maximum allowed length. + ensure!( + endpoint.id.len() <= T::MaxServiceIdLength::get().saturated_into(), + InputError::MaxIdLengthExceeded + ); + // Check that all types are the maximum allowed length. + endpoint.service_type.iter().try_for_each(|s_type| { + ensure!( + s_type.len() <= T::MaxServiceTypeLength::get().saturated_into(), + InputError::MaxTypeLengthExceeded + ); + Ok(()) + })?; + // Check that all URLs are the maximum allowed length. + endpoint.url.iter().try_for_each(|s_url| { + ensure!( + s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), + InputError::MaxUrlLengthExceeded + ); + Ok(()) + })?; + + Ok(()) + })?; + + Ok(()) +} From 77760dbce5d0693767764f51cdd783f3f75efe85 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 20 Oct 2021 16:01:43 +0200 Subject: [PATCH 02/45] feat: add support for service endpoints in DID pallet --- pallets/did/src/did_details.rs | 12 ++++-- pallets/did/src/lib.rs | 56 +++++++++++++++++++++++----- pallets/did/src/utils.rs | 68 ++++++++++++++++++---------------- 3 files changed, 90 insertions(+), 46 deletions(-) diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index 831ee5250..f02e4d3ca 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -531,11 +531,14 @@ impl DidDetails { } } -// pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; +// pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; pub type ServiceEndpointId = Vec; -// pub type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; +// pub type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; pub type ServiceEndpointType = Vec; -// pub type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; +// pub type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; pub type ServiceEndpointUrl = Vec; #[derive(Clone, Debug, Decode, Encode, PartialEq, Eq)] @@ -550,7 +553,8 @@ pub(crate) type DidNewKeyAgreementKeySet = BoundedBTreeSet = BoundedBTreeSet, ::MaxTotalKeyAgreementKeys>; pub(crate) type DidPublicKeyMap = BoundedBTreeMap, DidPublicKeyDetails, ::MaxPublicKeysPerDid>; -// pub(crate) type DidNewServiceEndpoints = BoundedBTreeSet, ::MaxDidServicesCount>; +// pub(crate) type DidNewServiceEndpoints = +// BoundedBTreeSet, ::MaxDidServicesCount>; /// The details of a new DID to create. #[derive(Clone, Debug, Decode, Encode, PartialEq)] diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index d20443e58..677b060bd 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -300,7 +300,8 @@ pub mod pallet { /// It maps from (DID identifier, service ID) to the service details. #[pallet::storage] #[pallet::getter(fn get_service_endpoints)] - pub type ServiceEndpoints = StorageDoubleMap<_, Twox64Concat, DidIdentifierOf, Blake2_128Concat, Vec, DidEndpointDetails>; + pub type ServiceEndpoints = + StorageDoubleMap<_, Twox64Concat, DidIdentifierOf, Blake2_128Concat, Vec, DidEndpointDetails>; /// The set of DIDs that have been deleted and cannot therefore be created /// again for security reasons. @@ -352,13 +353,6 @@ pub mod pallet { VerificationKeyNotPresent, /// The DID operation nonce is not equal to the current DID nonce + 1. InvalidNonce, - /// The URL specified is not ASCII-encoded. - InvalidUrlEncoding, - /// The URL specified is not properly formatted. - InvalidUrlScheme, - /// The maximum supported value for the DID tx counter has been reached. - /// No more operations with the DID are allowed. - MaxTxCounterValue, /// The user tries to delete a verification key that is currently being /// used as an authentication, delegation, or attestation key, and this /// is not allowed. @@ -393,6 +387,8 @@ pub mod pallet { MaxUrlCountExceeded, MaxTypeCountExceeded, MaxServicesCountExceeded, + ServiceAlreadyPresent, + ServiceNotPresent, /// An error that is not supposed to take place, yet it happened. InternalError, } @@ -511,7 +507,7 @@ pub mod pallet { // Validate all the size constraints for the service endpoints. let input_service_endpoints = details.new_service_details.clone(); - utils::validate_service_endpoints(&input_service_endpoints).map_err(Error::::from)?; + utils::validate_new_service_endpoints(&input_service_endpoints).map_err(Error::::from)?; let did_entry = DidDetails::from_creation_details(details, account_did_auth_key).map_err(Error::::from)?; @@ -766,6 +762,45 @@ pub mod pallet { Ok(()) } + #[pallet::weight(100_000)] + pub fn add_service_endpoint(origin: OriginFor, service_endpoint: DidEndpointDetails) -> DispatchResult { + let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); + + utils::validate_single_service_endpoint(&service_endpoint).map_err(Error::::from)?; + + // Verify that the DID is present. + ensure!(Did::::get(&did_subject).is_some(), Error::::DidNotPresent); + + // Verify that there are less than the maximum limit of services stored. + ensure!( + ServiceEndpoints::::iter_prefix(&did_subject).count() + < T::MaxDidServicesCount::get().saturated_into(), + Error::::MaxServicesCountExceeded + ); + + // Verify that the service with the given ID does not exist. + ensure!( + ServiceEndpoints::::get(&did_subject, &service_endpoint.id).is_none(), + Error::::ServiceAlreadyPresent + ); + + ServiceEndpoints::::insert(&did_subject, service_endpoint.id.clone(), service_endpoint); + + Ok(()) + } + + #[pallet::weight(100_000)] + pub fn remove_service_endpoint(origin: OriginFor, service_id: Vec) -> DispatchResult { + let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); + + ensure!( + ServiceEndpoints::::take(&did_subject, &service_id).is_some(), + Error::::ServiceNotPresent + ); + + Ok(()) + } + /// Delete a DID from the chain and all information associated with it, /// after verifying that the delete operation has been signed by the DID /// subject using the authentication key currently stored on chain. @@ -1020,8 +1055,9 @@ impl Pallet { fn delete_did(did_subject: DidIdentifierOf) -> DispatchResult { // `take` calls `kill` internally let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; - // *** No Fail beyond this point *** + // *** No Fail beyond this point *** + ServiceEndpoints::::remove_prefix(&did_subject, Some(T::MaxDidServicesCount::get())); kilt_support::free_deposit::, CurrencyOf>(&did_entry.deposit); // Mark as deleted to prevent potential replay-attacks of re-adding a previously // deleted DID. diff --git a/pallets/did/src/utils.rs b/pallets/did/src/utils.rs index 202ae2f70..f371405ac 100644 --- a/pallets/did/src/utils.rs +++ b/pallets/did/src/utils.rs @@ -17,19 +17,19 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org use codec::Encode; -use sp_runtime::{traits::{Hash, SaturatedConversion}}; +use sp_runtime::traits::{Hash, SaturatedConversion}; use sp_std::vec::Vec; use frame_support::{ensure, traits::Get}; -use crate::{Config, DidPublicKey, KeyIdOf, InputError, DidEndpointDetails}; +use crate::{Config, DidEndpointDetails, DidPublicKey, InputError, KeyIdOf}; pub fn calculate_key_id(key: &DidPublicKey) -> KeyIdOf { let hashed_values: Vec = key.encode(); T::Hashing::hash(&hashed_values) } -pub(crate) fn validate_service_endpoints(endpoints: &[DidEndpointDetails]) -> Result<(), InputError> { +pub(crate) fn validate_new_service_endpoints(endpoints: &[DidEndpointDetails]) -> Result<(), InputError> { // Check if the maximum number of endpoints is provided ensure!( endpoints.len() <= T::MaxDidServicesCount::get().saturated_into(), @@ -37,39 +37,43 @@ pub(crate) fn validate_service_endpoints(endpoints: &[DidEndpointDeta ); // For each service... - endpoints.iter().try_for_each(|endpoint| { - // Check that the maximum number of service types is provided. - ensure!( - endpoint.service_type.len() <= T::MaxTypeCountPerService::get().saturated_into(), - InputError::MaxTypeCountExceeded - ); - // Check that the maximum number of URLs is provided. + endpoints + .iter() + .try_for_each(|endpoint| validate_single_service_endpoint(endpoint))?; + + Ok(()) +} + +pub(crate) fn validate_single_service_endpoint(endpoint: &DidEndpointDetails) -> Result<(), InputError> { + // Check that the maximum number of service types is provided. + ensure!( + endpoint.service_type.len() <= T::MaxTypeCountPerService::get().saturated_into(), + InputError::MaxTypeCountExceeded + ); + // Check that the maximum number of URLs is provided. + ensure!( + endpoint.url.len() <= T::MaxUrlCountPerService::get().saturated_into(), + InputError::MaxUrlCountExceeded + ); + // Check that the ID is the maximum allowed length. + ensure!( + endpoint.id.len() <= T::MaxServiceIdLength::get().saturated_into(), + InputError::MaxIdLengthExceeded + ); + // Check that all types are the maximum allowed length. + endpoint.service_type.iter().try_for_each(|s_type| { ensure!( - endpoint.url.len() <= T::MaxUrlCountPerService::get().saturated_into(), - InputError::MaxUrlCountExceeded + s_type.len() <= T::MaxServiceTypeLength::get().saturated_into(), + InputError::MaxTypeLengthExceeded ); - // Check that the ID is the maximum allowed length. + Ok(()) + })?; + // Check that all URLs are the maximum allowed length. + endpoint.url.iter().try_for_each(|s_url| { ensure!( - endpoint.id.len() <= T::MaxServiceIdLength::get().saturated_into(), - InputError::MaxIdLengthExceeded + s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), + InputError::MaxUrlLengthExceeded ); - // Check that all types are the maximum allowed length. - endpoint.service_type.iter().try_for_each(|s_type| { - ensure!( - s_type.len() <= T::MaxServiceTypeLength::get().saturated_into(), - InputError::MaxTypeLengthExceeded - ); - Ok(()) - })?; - // Check that all URLs are the maximum allowed length. - endpoint.url.iter().try_for_each(|s_url| { - ensure!( - s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), - InputError::MaxUrlLengthExceeded - ); - Ok(()) - })?; - Ok(()) })?; From 03339c1c9a2a9f53a3f44475f12b78552b7799eb Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 20 Oct 2021 16:30:21 +0200 Subject: [PATCH 03/45] chore: refactor --- pallets/did/src/did_details.rs | 20 ------ pallets/did/src/lib.rs | 7 +- pallets/did/src/service_endpoints.rs | 99 ++++++++++++++++++++++++++++ pallets/did/src/utils.rs | 57 +--------------- 4 files changed, 105 insertions(+), 78 deletions(-) create mode 100644 pallets/did/src/service_endpoints.rs diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index f02e4d3ca..739406b80 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -531,30 +531,10 @@ impl DidDetails { } } -// pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; -pub type ServiceEndpointId = Vec; -// pub type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; -pub type ServiceEndpointType = Vec; -// pub type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; -pub type ServiceEndpointUrl = Vec; - -#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq)] -pub struct DidEndpointDetails { - phantom_data: sp_std::marker::PhantomData, - pub(crate) id: ServiceEndpointId, - pub(crate) service_type: [ServiceEndpointType; 5], - pub(crate) url: Vec, -} - pub(crate) type DidNewKeyAgreementKeySet = BoundedBTreeSet::MaxNewKeyAgreementKeys>; pub(crate) type DidKeyAgreementKeySet = BoundedBTreeSet, ::MaxTotalKeyAgreementKeys>; pub(crate) type DidPublicKeyMap = BoundedBTreeMap, DidPublicKeyDetails, ::MaxPublicKeysPerDid>; -// pub(crate) type DidNewServiceEndpoints = -// BoundedBTreeSet, ::MaxDidServicesCount>; /// The details of a new DID to create. #[derive(Clone, Debug, Decode, Encode, PartialEq)] diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 677b060bd..1904baad7 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -104,6 +104,7 @@ pub mod default_weights; pub mod did_details; pub mod errors; pub mod migrations; +pub mod service_endpoints; pub mod origin; #[cfg(feature = "runtime-benchmarks")] @@ -121,7 +122,7 @@ mod signature; mod utils; use crate::migrations::*; -pub use crate::{default_weights::WeightInfo, did_details::*, errors::*, origin::*, pallet::*, signature::*}; +pub use crate::{default_weights::WeightInfo, did_details::*, errors::*, origin::*, service_endpoints::*, pallet::*, signature::*}; use codec::Encode; use frame_support::{ @@ -507,7 +508,7 @@ pub mod pallet { // Validate all the size constraints for the service endpoints. let input_service_endpoints = details.new_service_details.clone(); - utils::validate_new_service_endpoints(&input_service_endpoints).map_err(Error::::from)?; + service_endpoints::utils::validate_new_service_endpoints(&input_service_endpoints).map_err(Error::::from)?; let did_entry = DidDetails::from_creation_details(details, account_did_auth_key).map_err(Error::::from)?; @@ -766,7 +767,7 @@ pub mod pallet { pub fn add_service_endpoint(origin: OriginFor, service_endpoint: DidEndpointDetails) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); - utils::validate_single_service_endpoint(&service_endpoint).map_err(Error::::from)?; + service_endpoints::utils::validate_single_service_endpoint(&service_endpoint).map_err(Error::::from)?; // Verify that the DID is present. ensure!(Did::::get(&did_subject).is_some(), Error::::DidNotPresent); diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs new file mode 100644 index 000000000..b9d344e97 --- /dev/null +++ b/pallets/did/src/service_endpoints.rs @@ -0,0 +1,99 @@ +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2021 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::{Encode, Decode}; +use crate::Config; + +// pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; +pub type ServiceEndpointId = Vec; +// pub type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; +pub type ServiceEndpointType = Vec; +// pub type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; +pub type ServiceEndpointUrl = Vec; + +// pub(crate) type DidNewServiceEndpoints = +// BoundedBTreeSet, ::MaxDidServicesCount>; + +#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq)] +pub struct DidEndpointDetails { + phantom_data: sp_std::marker::PhantomData, + pub(crate) id: ServiceEndpointId, + pub(crate) service_type: [ServiceEndpointType; 5], + pub(crate) url: Vec, +} + +pub mod utils { + use super::*; + use crate::InputError; + use frame_support::{ensure, traits::Get}; + use sp_runtime::traits::SaturatedConversion; + + pub(crate) fn validate_new_service_endpoints(endpoints: &[DidEndpointDetails]) -> Result<(), InputError> { + // Check if the maximum number of endpoints is provided + ensure!( + endpoints.len() <= T::MaxDidServicesCount::get().saturated_into(), + InputError::MaxServicesCountExceeded + ); + + // For each service... + endpoints + .iter() + .try_for_each(|endpoint| validate_single_service_endpoint(endpoint))?; + + Ok(()) + } + + pub(crate) fn validate_single_service_endpoint(endpoint: &DidEndpointDetails) -> Result<(), InputError> { + // Check that the maximum number of service types is provided. + ensure!( + endpoint.service_type.len() <= T::MaxTypeCountPerService::get().saturated_into(), + InputError::MaxTypeCountExceeded + ); + // Check that the maximum number of URLs is provided. + ensure!( + endpoint.url.len() <= T::MaxUrlCountPerService::get().saturated_into(), + InputError::MaxUrlCountExceeded + ); + // Check that the ID is the maximum allowed length. + ensure!( + endpoint.id.len() <= T::MaxServiceIdLength::get().saturated_into(), + InputError::MaxIdLengthExceeded + ); + // Check that all types are the maximum allowed length. + endpoint.service_type.iter().try_for_each(|s_type| { + ensure!( + s_type.len() <= T::MaxServiceTypeLength::get().saturated_into(), + InputError::MaxTypeLengthExceeded + ); + Ok(()) + })?; + // Check that all URLs are the maximum allowed length. + endpoint.url.iter().try_for_each(|s_url| { + ensure!( + s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), + InputError::MaxUrlLengthExceeded + ); + Ok(()) + })?; + + Ok(()) + } +} diff --git a/pallets/did/src/utils.rs b/pallets/did/src/utils.rs index f371405ac..ac28fdc9d 100644 --- a/pallets/did/src/utils.rs +++ b/pallets/did/src/utils.rs @@ -17,65 +17,12 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org use codec::Encode; -use sp_runtime::traits::{Hash, SaturatedConversion}; +use sp_runtime::traits::Hash; use sp_std::vec::Vec; -use frame_support::{ensure, traits::Get}; - -use crate::{Config, DidEndpointDetails, DidPublicKey, InputError, KeyIdOf}; +use crate::{Config, DidPublicKey, KeyIdOf}; pub fn calculate_key_id(key: &DidPublicKey) -> KeyIdOf { let hashed_values: Vec = key.encode(); T::Hashing::hash(&hashed_values) } - -pub(crate) fn validate_new_service_endpoints(endpoints: &[DidEndpointDetails]) -> Result<(), InputError> { - // Check if the maximum number of endpoints is provided - ensure!( - endpoints.len() <= T::MaxDidServicesCount::get().saturated_into(), - InputError::MaxServicesCountExceeded - ); - - // For each service... - endpoints - .iter() - .try_for_each(|endpoint| validate_single_service_endpoint(endpoint))?; - - Ok(()) -} - -pub(crate) fn validate_single_service_endpoint(endpoint: &DidEndpointDetails) -> Result<(), InputError> { - // Check that the maximum number of service types is provided. - ensure!( - endpoint.service_type.len() <= T::MaxTypeCountPerService::get().saturated_into(), - InputError::MaxTypeCountExceeded - ); - // Check that the maximum number of URLs is provided. - ensure!( - endpoint.url.len() <= T::MaxUrlCountPerService::get().saturated_into(), - InputError::MaxUrlCountExceeded - ); - // Check that the ID is the maximum allowed length. - ensure!( - endpoint.id.len() <= T::MaxServiceIdLength::get().saturated_into(), - InputError::MaxIdLengthExceeded - ); - // Check that all types are the maximum allowed length. - endpoint.service_type.iter().try_for_each(|s_type| { - ensure!( - s_type.len() <= T::MaxServiceTypeLength::get().saturated_into(), - InputError::MaxTypeLengthExceeded - ); - Ok(()) - })?; - // Check that all URLs are the maximum allowed length. - endpoint.url.iter().try_for_each(|s_url| { - ensure!( - s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), - InputError::MaxUrlLengthExceeded - ); - Ok(()) - })?; - - Ok(()) -} From 46d1523e295bfe2568e2a70962af0ba90753f019 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 20 Oct 2021 17:10:19 +0200 Subject: [PATCH 04/45] wip: tests and benchmarks for new feature --- pallets/did/src/mock_utils.rs | 36 +++++++++++++++++++++++++++- pallets/did/src/service_endpoints.rs | 4 ++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pallets/did/src/mock_utils.rs b/pallets/did/src/mock_utils.rs index e1b0d0fd9..1b1e6a120 100644 --- a/pallets/did/src/mock_utils.rs +++ b/pallets/did/src/mock_utils.rs @@ -20,7 +20,7 @@ use crate::*; use did_details::*; use frame_support::storage::bounded_btree_set::BoundedBTreeSet; use kilt_support::deposit::Deposit; -use sp_runtime::traits::Zero; +use sp_runtime::{app_crypto::IsWrappedBy, traits::Zero}; use sp_std::{ collections::btree_set::BTreeSet, convert::{TryFrom, TryInto}, @@ -43,6 +43,39 @@ pub fn get_key_agreement_keys(n_keys: u32) -> DidNewKeyAgreementKeySe .expect("Failed to convert key_agreement_keys to BoundedBTreeSet") } +pub fn get_service_endpoints(count: u32, endpoint_id_length: u32, endpoint_type_count: u32, endpoint_type_length: u32, endpoint_url_count: u32, endpoint_url_length: u32) -> Vec> { + (0..count).map(|i| { + let a: Vec = vec![i.to_be_bytes(); endpoint_id_length.saturated_into()]; + let endpoint_id = Vec::from(a.as_ref().iter()); + let endpoint_types = (0..endpoint_type_count).map(|t| { + Vec::from(vec![t.to_be_bytes(); endpoint_url_length.saturated_into()].as_ref().iter()) + }).collect(); + let endpoint_urls = (0..endpoint_url_count).map(|u| { + Vec::from(vec![u.to_be_bytes(); endpoint_url_length.saturated_into()].as_ref().iter()) + }).collect(); + DidEndpointDetails { + id: endpoint_id, + service_type: endpoint_types, + url: endpoint_urls, + phantom_data: None + } + }); + // BoundedBTreeSet::try_from( + // (1..=n_keys) + // .map(|i| { + // // Converts the loop index to a 32-byte array; + // let mut seed_vec = i.to_be_bytes().to_vec(); + // seed_vec.resize(32, 0u8); + // let seed: [u8; 32] = seed_vec + // .try_into() + // .expect("Failed to create encryption key from raw seed."); + // DidEncryptionKey::X25519(seed) + // }) + // .collect::>(), + // ) + // .expect("Failed to convert key_agreement_keys to BoundedBTreeSet") +} + pub fn generate_base_did_creation_details( did: DidIdentifierOf, submitter: AccountIdOf, @@ -53,6 +86,7 @@ pub fn generate_base_did_creation_details( new_key_agreement_keys: BoundedBTreeSet::new(), new_attestation_key: None, new_delegation_key: None, + new_service_details: Vec::new(), } } diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index b9d344e97..6eea83604 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -34,9 +34,9 @@ pub type ServiceEndpointUrl = Vec; #[derive(Clone, Debug, Decode, Encode, PartialEq, Eq)] pub struct DidEndpointDetails { - phantom_data: sp_std::marker::PhantomData, + phantom_data: Option>, pub(crate) id: ServiceEndpointId, - pub(crate) service_type: [ServiceEndpointType; 5], + pub(crate) service_type: Vec, pub(crate) url: Vec, } From ea9bbfeb829bde780f5287d6ce81ffba4c7576e6 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 10:30:34 +0200 Subject: [PATCH 05/45] test: add unit test cases for new features --- pallets/did/src/lib.rs | 11 +- pallets/did/src/mock.rs | 27 +- pallets/did/src/mock_utils.rs | 67 ++-- pallets/did/src/service_endpoints.rs | 12 +- pallets/did/src/tests.rs | 569 ++++++++++++++++++++++++++- primitives/src/constants.rs | 7 + 6 files changed, 635 insertions(+), 58 deletions(-) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 1904baad7..d17c547e7 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -104,8 +104,8 @@ pub mod default_weights; pub mod did_details; pub mod errors; pub mod migrations; -pub mod service_endpoints; pub mod origin; +pub mod service_endpoints; #[cfg(feature = "runtime-benchmarks")] pub mod benchmarking; @@ -122,7 +122,9 @@ mod signature; mod utils; use crate::migrations::*; -pub use crate::{default_weights::WeightInfo, did_details::*, errors::*, origin::*, service_endpoints::*, pallet::*, signature::*}; +pub use crate::{ + default_weights::WeightInfo, did_details::*, errors::*, origin::*, pallet::*, service_endpoints::*, signature::*, +}; use codec::Encode; use frame_support::{ @@ -508,7 +510,8 @@ pub mod pallet { // Validate all the size constraints for the service endpoints. let input_service_endpoints = details.new_service_details.clone(); - service_endpoints::utils::validate_new_service_endpoints(&input_service_endpoints).map_err(Error::::from)?; + service_endpoints::utils::validate_new_service_endpoints(&input_service_endpoints) + .map_err(Error::::from)?; let did_entry = DidDetails::from_creation_details(details, account_did_auth_key).map_err(Error::::from)?; @@ -785,6 +788,8 @@ pub mod pallet { Error::::ServiceAlreadyPresent ); + // *** No Fail beyond this point *** + ServiceEndpoints::::insert(&did_subject, service_endpoint.id.clone(), service_endpoint); Ok(()) diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index 4478f9954..1fda491f8 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -105,6 +105,12 @@ parameter_types! { pub const MaxBlocksTxValidity: u64 = 300u64; pub const Deposit: Balance = 10 * MICRO_KILT; pub const DidFee: Balance = MICRO_KILT; + pub const MaxDidServicesCount: u32 = 25u32; + pub const MaxServiceIdLength: u32 = 50u32; + pub const MaxServiceTypeLength: u32 = 50u32; + pub const MaxServiceUrlLength: u32 = 100u32; + pub const MaxTypeCountPerService: u32 = 1u32; + pub const MaxUrlCountPerService: u32 = 1u32; } pub struct ToAccount(sp_std::marker::PhantomData); @@ -135,6 +141,12 @@ impl Config for Test { type MaxPublicKeysPerDid = MaxPublicKeysPerDid; type MaxBlocksTxValidity = MaxBlocksTxValidity; type WeightInfo = (); + type MaxDidServicesCount = MaxDidServicesCount; + type MaxServiceIdLength = MaxServiceIdLength; + type MaxServiceTypeLength = MaxServiceTypeLength; + type MaxServiceUrlLength = MaxServiceUrlLength; + type MaxTypeCountPerService = MaxTypeCountPerService; + type MaxUrlCountPerService = MaxUrlCountPerService; } parameter_types! { @@ -392,6 +404,7 @@ pub fn initialize_logger() { #[derive(Clone, Default)] pub struct ExtBuilder { dids_stored: Vec<(TestDidIdentifier, did::DidDetails)>, + service_endpoints: Vec<(TestDidIdentifier, Vec>)>, deleted_dids: Vec, storage_version: DidStorageVersion, ctypes_stored: Vec<(TestCtypeHash, TestCtypeOwner)>, @@ -404,6 +417,11 @@ impl ExtBuilder { self } + pub fn with_endpoints(mut self, endpoints: Vec<(TestDidIdentifier, Vec>)>) -> Self { + self.service_endpoints = endpoints; + self + } + pub(crate) fn with_balances(mut self, balances: Vec<(AccountIdOf, Balance)>) -> Self { self.balances = balances; self @@ -443,12 +461,17 @@ impl ExtBuilder { } for did in self.dids_stored.iter() { - did::Did::::insert(did.0.clone(), did.1.clone()); + did::Did::::insert(&did.0, did.1.clone()); CurrencyOf::::reserve(&did.1.deposit.owner, did.1.deposit.amount) .expect("Deposit owner should have enough balance"); } for did in self.deleted_dids.iter() { - did::DidBlacklist::::insert(did.clone(), ()); + did::DidBlacklist::::insert(&did, ()); + } + for (did, endpoints) in self.service_endpoints.iter() { + for endpoint in endpoints.iter() { + did::ServiceEndpoints::::insert(&did, &endpoint.id, endpoint) + } } did::StorageVersion::::set(self.storage_version); }); diff --git a/pallets/did/src/mock_utils.rs b/pallets/did/src/mock_utils.rs index 1b1e6a120..a156b9152 100644 --- a/pallets/did/src/mock_utils.rs +++ b/pallets/did/src/mock_utils.rs @@ -20,7 +20,7 @@ use crate::*; use did_details::*; use frame_support::storage::bounded_btree_set::BoundedBTreeSet; use kilt_support::deposit::Deposit; -use sp_runtime::{app_crypto::IsWrappedBy, traits::Zero}; +use sp_runtime::traits::Zero; use sp_std::{ collections::btree_set::BTreeSet, convert::{TryFrom, TryInto}, @@ -43,37 +43,40 @@ pub fn get_key_agreement_keys(n_keys: u32) -> DidNewKeyAgreementKeySe .expect("Failed to convert key_agreement_keys to BoundedBTreeSet") } -pub fn get_service_endpoints(count: u32, endpoint_id_length: u32, endpoint_type_count: u32, endpoint_type_length: u32, endpoint_url_count: u32, endpoint_url_length: u32) -> Vec> { - (0..count).map(|i| { - let a: Vec = vec![i.to_be_bytes(); endpoint_id_length.saturated_into()]; - let endpoint_id = Vec::from(a.as_ref().iter()); - let endpoint_types = (0..endpoint_type_count).map(|t| { - Vec::from(vec![t.to_be_bytes(); endpoint_url_length.saturated_into()].as_ref().iter()) - }).collect(); - let endpoint_urls = (0..endpoint_url_count).map(|u| { - Vec::from(vec![u.to_be_bytes(); endpoint_url_length.saturated_into()].as_ref().iter()) - }).collect(); - DidEndpointDetails { - id: endpoint_id, - service_type: endpoint_types, - url: endpoint_urls, - phantom_data: None - } - }); - // BoundedBTreeSet::try_from( - // (1..=n_keys) - // .map(|i| { - // // Converts the loop index to a 32-byte array; - // let mut seed_vec = i.to_be_bytes().to_vec(); - // seed_vec.resize(32, 0u8); - // let seed: [u8; 32] = seed_vec - // .try_into() - // .expect("Failed to create encryption key from raw seed."); - // DidEncryptionKey::X25519(seed) - // }) - // .collect::>(), - // ) - // .expect("Failed to convert key_agreement_keys to BoundedBTreeSet") +pub fn get_service_endpoints( + count: u32, + endpoint_id_length: u32, + endpoint_type_count: u32, + endpoint_type_length: u32, + endpoint_url_count: u32, + endpoint_url_length: u32, +) -> Vec> { + (0..count) + .map(|i| { + let mut endpoint_id = i.to_be_bytes().to_vec(); + endpoint_id.resize(endpoint_id_length.saturated_into(), 0u8); + let endpoint_types = (0..endpoint_type_count) + .map(|t| { + let mut endpoint_type = t.to_be_bytes().to_vec(); + endpoint_type.resize(endpoint_type_length.saturated_into(), 0u8); + endpoint_type + }) + .collect(); + let endpoint_urls = (0..endpoint_url_count) + .map(|u| { + let mut endpoint_url = u.to_be_bytes().to_vec(); + endpoint_url.resize(endpoint_url_length.saturated_into(), 0u8); + endpoint_url + }) + .collect(); + DidEndpointDetails { + id: endpoint_id, + service_type: endpoint_types, + url: endpoint_urls, + phantom_data: None, + } + }) + .collect() } pub fn generate_base_did_creation_details( diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 6eea83604..05be55d45 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -16,8 +16,8 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use codec::{Encode, Decode}; use crate::Config; +use codec::{Decode, Encode}; // pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; @@ -34,7 +34,7 @@ pub type ServiceEndpointUrl = Vec; #[derive(Clone, Debug, Decode, Encode, PartialEq, Eq)] pub struct DidEndpointDetails { - phantom_data: Option>, + pub(crate) phantom_data: Option>, pub(crate) id: ServiceEndpointId, pub(crate) service_type: Vec, pub(crate) url: Vec, @@ -46,7 +46,9 @@ pub mod utils { use frame_support::{ensure, traits::Get}; use sp_runtime::traits::SaturatedConversion; - pub(crate) fn validate_new_service_endpoints(endpoints: &[DidEndpointDetails]) -> Result<(), InputError> { + pub(crate) fn validate_new_service_endpoints( + endpoints: &[DidEndpointDetails], + ) -> Result<(), InputError> { // Check if the maximum number of endpoints is provided ensure!( endpoints.len() <= T::MaxDidServicesCount::get().saturated_into(), @@ -61,7 +63,9 @@ pub mod utils { Ok(()) } - pub(crate) fn validate_single_service_endpoint(endpoint: &DidEndpointDetails) -> Result<(), InputError> { + pub(crate) fn validate_single_service_endpoint( + endpoint: &DidEndpointDetails, + ) -> Result<(), InputError> { // Check that the maximum number of service types is provided. ensure!( endpoint.service_type.len() <= T::MaxTypeCountPerService::get().saturated_into(), diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index e7a93a1eb..ac3995248 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -24,7 +24,7 @@ use sp_runtime::{ }; use sp_std::{collections::btree_set::BTreeSet, convert::TryFrom}; -use crate::{self as did, mock::*, mock_utils::*, AccountIdOf}; +use crate::{self as did, DidEndpointDetails, mock::*, mock_utils::*}; // create @@ -39,7 +39,7 @@ fn check_successful_simple_ed25519_creation() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .build(None) @@ -81,7 +81,7 @@ fn check_successful_simple_sr25519_creation() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .build(None) @@ -123,7 +123,7 @@ fn check_successful_simple_ecdsa_creation() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .build(None) @@ -172,12 +172,19 @@ fn check_successful_complete_creation() { details.new_key_agreement_keys = enc_keys.clone(); details.new_attestation_key = Some(did::DidVerificationKey::from(att_key.public())); details.new_delegation_key = Some(did::DidVerificationKey::from(del_key.public())); - + details.new_service_details = get_service_endpoints( + ::MaxDidServicesCount::get(), + ::MaxServiceIdLength::get(), + ::MaxTypeCountPerService::get(), + ::MaxServiceTypeLength::get(), + ::MaxUrlCountPerService::get(), + ::MaxServiceUrlLength::get(), + ); let signature = auth_key.sign(details.encode().as_ref()); let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .build(None) @@ -224,6 +231,17 @@ fn check_successful_complete_creation() { assert!(stored_did .public_keys .contains_key(&generate_key_id(&details.new_delegation_key.clone().unwrap().into()))); + + // We check that the service details in the creation operation have been all stored in the storage... + details.new_service_details.iter().for_each(|new_service| { + let stored_service = Did::get_service_endpoints(&alice_did, &new_service.id).expect("Service endpoint should be stored."); + assert_eq!(stored_service.id, new_service.id); + assert_eq!(stored_service.url, new_service.url); + assert_eq!(stored_service.service_type, new_service.service_type); + }); + // ... and that the number of elements in the creation operation is the same as the number of elements stored. + assert_eq!(did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), details.new_service_details.len()); + assert_eq!( Balances::reserved_balance(ACCOUNT_00), ::Deposit::get() @@ -244,7 +262,7 @@ fn check_duplicate_did_creation() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .with_dids(vec![(alice_did, mock_did)]) @@ -270,7 +288,7 @@ fn check_unauthorised_submitter_did_creation_error() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .with_dids(vec![(alice_did, mock_did)]) @@ -310,7 +328,7 @@ fn check_did_already_deleted_creation() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .with_deleted_dids(vec![alice_did]) @@ -336,7 +354,7 @@ fn check_invalid_signature_format_did_creation() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .build(None) @@ -359,7 +377,7 @@ fn check_invalid_signature_did_creation() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .build(None) @@ -382,7 +400,7 @@ fn check_swapped_did_subject_did_creation() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .build(None) @@ -408,7 +426,7 @@ fn check_max_limit_key_agreement_keys_did_creation() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .build(None) @@ -420,6 +438,186 @@ fn check_max_limit_key_agreement_keys_did_creation() { }); } +#[test] +fn check_max_limit_service_endpoints_count_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); + details.new_service_details = get_service_endpoints( + ::MaxDidServicesCount::get() + 1, + 1, + 1, + 1, + 1, + 1, + ); + + let signature = auth_key.sign(details.encode().as_ref()); + + let balance = ::Deposit::get() + + ::Fee::get() + + <::Currency as Currency>>::minimum_balance(); + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + did::Error::::MaxServicesCountExceeded + ); + }); +} + +#[test] +fn check_max_limit_service_id_length_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); + details.new_service_details = get_service_endpoints( + 1, + ::MaxServiceIdLength::get() + 1, + 1, + 1, + 1, + 1, + ); + + let signature = auth_key.sign(details.encode().as_ref()); + + let balance = ::Deposit::get() + + ::Fee::get() + + <::Currency as Currency>>::minimum_balance(); + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + did::Error::::MaxIdLengthExceeded + ); + }); +} + +#[test] +fn check_max_limit_service_type_count_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); + details.new_service_details = get_service_endpoints( + 1, + 1, + ::MaxTypeCountPerService::get() + 1, + 1, + 1, + 1, + ); + + let signature = auth_key.sign(details.encode().as_ref()); + + let balance = ::Deposit::get() + + ::Fee::get() + + <::Currency as Currency>>::minimum_balance(); + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + did::Error::::MaxTypeCountExceeded + ); + }); +} + +#[test] +fn check_max_limit_service_type_length_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); + details.new_service_details = get_service_endpoints( + 1, + 1, + 1, + ::MaxServiceTypeLength::get() + 1, + 1, + 1, + ); + + let signature = auth_key.sign(details.encode().as_ref()); + + let balance = ::Deposit::get() + + ::Fee::get() + + <::Currency as Currency>>::minimum_balance(); + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + did::Error::::MaxTypeLengthExceeded + ); + }); +} + +#[test] +fn check_max_limit_service_url_count_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); + details.new_service_details = get_service_endpoints( + 1, + 1, + 1, + 1, + ::MaxUrlCountPerService::get() + 1, + 1, + ); + + let signature = auth_key.sign(details.encode().as_ref()); + + let balance = ::Deposit::get() + + ::Fee::get() + + <::Currency as Currency>>::minimum_balance(); + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + did::Error::::MaxUrlCountExceeded + ); + }); +} + +#[test] +fn check_max_limit_service_url_length_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); + details.new_service_details = get_service_endpoints( + 1, + 1, + 1, + 1, + 1, + ::MaxServiceUrlLength::get() + 1, + ); + + let signature = auth_key.sign(details.encode().as_ref()); + + let balance = ::Deposit::get() + + ::Fee::get() + + <::Currency as Currency>>::minimum_balance(); + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + did::Error::::MaxUrlLengthExceeded + ); + }); +} + // updates #[test] @@ -1351,25 +1549,358 @@ fn check_key_not_found_key_agreement_key_deletion_error() { }); } +#[test] +fn check_service_addition_no_prior_service_successful() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let new_service_endpoint = DidEndpointDetails { + id: b"id".to_vec(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()], + phantom_data: None, + }; + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None) + .execute_with(|| { + assert_ok!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint.clone()), + ); + let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id).expect("Service endpoint should be stored."); + assert_eq!( + stored_endpoint, new_service_endpoint + ); + }); +} + +#[test] +fn check_service_addition_one_from_full_successful() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let old_service_endpoints = get_service_endpoints( + // -1 from the max number + ::MaxDidServicesCount::get() - 1, + ::MaxServiceIdLength::get(), + ::MaxTypeCountPerService::get(), + ::MaxServiceTypeLength::get(), + ::MaxUrlCountPerService::get(), + ::MaxServiceUrlLength::get(), + ); + let new_service_endpoint = DidEndpointDetails { + id: b"id".to_vec(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()], + phantom_data: None, + }; + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .with_endpoints(vec![(alice_did.clone(), old_service_endpoints)]) + .build(None) + .execute_with(|| { + assert_ok!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint.clone()), + ); + assert_eq!( + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count().saturated_into::(), ::MaxDidServicesCount::get() + ); + let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id).expect("Service endpoint should be stored."); + assert_eq!( + stored_endpoint, new_service_endpoint + ); + }); +} + +#[test] +fn check_did_not_present_services_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let new_service_endpoint = DidEndpointDetails { + id: b"id".to_vec(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()], + phantom_data: None, + }; + + ExtBuilder::default() + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), + did::Error::::DidNotPresent + ); + }); +} + +#[test] +fn check_service_already_present_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let service_endpoint = DidEndpointDetails { + id: b"id".to_vec(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()], + phantom_data: None, + }; + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .with_endpoints(vec![(alice_did.clone(), vec![service_endpoint.clone()])]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), service_endpoint), + did::Error::::ServiceAlreadyPresent + ); + }); +} + +#[test] +fn check_max_services_count_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let old_service_endpoints = get_service_endpoints( + ::MaxDidServicesCount::get(), + ::MaxServiceIdLength::get(), + ::MaxTypeCountPerService::get(), + ::MaxServiceTypeLength::get(), + ::MaxUrlCountPerService::get(), + ::MaxServiceUrlLength::get(), + ); + let new_service_endpoint = DidEndpointDetails { + id: b"id".to_vec(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()], + phantom_data: None, + }; + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .with_endpoints(vec![(alice_did.clone(), old_service_endpoints)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), + did::Error::::MaxServicesCountExceeded + ); + }); +} + +#[test] +fn check_max_service_id_length_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let new_service_endpoint = get_service_endpoints( + 1, + ::MaxServiceIdLength::get() + 1, + ::MaxTypeCountPerService::get(), + ::MaxServiceTypeLength::get(), + ::MaxUrlCountPerService::get(), + ::MaxServiceUrlLength::get(), + )[0].clone(); + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), + did::Error::::MaxIdLengthExceeded + ); + }); +} + +#[test] +fn check_max_service_type_length_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let new_service_endpoint = get_service_endpoints( + 1, + ::MaxServiceIdLength::get(), + ::MaxTypeCountPerService::get(), + ::MaxServiceTypeLength::get() + 1, + ::MaxUrlCountPerService::get(), + ::MaxServiceUrlLength::get(), + )[0].clone(); + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), + did::Error::::MaxTypeLengthExceeded + ); + }); +} + +#[test] +fn check_max_service_type_count_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let new_service_endpoint = get_service_endpoints( + 1, + ::MaxServiceIdLength::get(), + ::MaxTypeCountPerService::get() + 1, + ::MaxServiceTypeLength::get(), + ::MaxUrlCountPerService::get(), + ::MaxServiceUrlLength::get(), + )[0].clone(); + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), + did::Error::::MaxTypeCountExceeded + ); + }); +} + +#[test] +fn check_max_service_url_length_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let new_service_endpoint = get_service_endpoints( + 1, + ::MaxServiceIdLength::get(), + ::MaxTypeCountPerService::get(), + ::MaxServiceTypeLength::get(), + ::MaxUrlCountPerService::get(), + ::MaxServiceUrlLength::get() + 1, + )[0].clone(); + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), + did::Error::::MaxUrlLengthExceeded + ); + }); +} + +#[test] +fn check_max_service_url_count_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let new_service_endpoint = get_service_endpoints( + 1, + ::MaxServiceIdLength::get(), + ::MaxTypeCountPerService::get(), + ::MaxServiceTypeLength::get(), + ::MaxUrlCountPerService::get() + 1, + ::MaxServiceUrlLength::get(), + )[0].clone(); + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), + did::Error::::MaxUrlCountExceeded + ); + }); +} + +#[test] +fn check_service_deletion_successful() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let old_service_endpoint = DidEndpointDetails { + id: b"id".to_vec(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()], + phantom_data: None, + }; + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .with_endpoints(vec![(alice_did.clone(), vec![old_service_endpoint.clone()])]) + .build(None) + .execute_with(|| { + assert_ok!( + Did::remove_service_endpoint(Origin::signed(alice_did.clone()), old_service_endpoint.id), + ); + assert_eq!( + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 0 + ); + }); +} + +#[test] +fn check_service_not_present_deletion_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let service_id = b"id".to_vec(); + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::remove_service_endpoint(Origin::signed(alice_did.clone()), service_id), + did::Error::::ServiceNotPresent + ); + }); +} + // delete #[test] fn check_successful_deletion() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let service_endpoint = DidEndpointDetails { + id: b"id".to_vec(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()], + phantom_data: None, + }; + let mut did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); did_details.deposit.owner = ACCOUNT_00; did_details.deposit.amount = ::Deposit::get(); let balance = ::Deposit::get() * 2 + ::Fee::get() * 2 - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .with_dids(vec![(alice_did.clone(), did_details)]) + .with_endpoints(vec![(alice_did.clone(), vec![service_endpoint.clone()])]) .build(None) .execute_with(|| { + assert_eq!( + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 1 + ); assert_eq!( Balances::reserved_balance(ACCOUNT_00), ::Deposit::get() @@ -1379,6 +1910,10 @@ fn check_successful_deletion() { assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); + assert_eq!( + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 0 + ); + // Re-adding the same DID identifier should fail. let details = generate_base_did_creation_details::(alice_did.clone(), ACCOUNT_00); @@ -1402,7 +1937,7 @@ fn check_did_not_present_deletion() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .build(None) @@ -1426,7 +1961,7 @@ fn check_successful_reclaiming() { let balance = ::Deposit::get() * 2 + ::Fee::get() * 2 - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) @@ -1471,7 +2006,7 @@ fn unauthorized_reclaiming() { let balance = ::Deposit::get() + ::Fee::get() - + <::Currency as Currency>>::minimum_balance(); + + <::Currency as Currency>>::minimum_balance(); ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) diff --git a/primitives/src/constants.rs b/primitives/src/constants.rs index e6edb8c1b..1b9903435 100644 --- a/primitives/src/constants.rs +++ b/primitives/src/constants.rs @@ -198,4 +198,11 @@ pub mod did { pub const MAX_TOTAL_KEY_AGREEMENT_KEYS: u32 = MAX_PUBLIC_KEYS_PER_DID - 1; pub const MAX_ENDPOINT_URLS_COUNT: u32 = 3; pub const MAX_BLOCKS_TX_VALIDITY: BlockNumber = HOURS; + + pub const MAX_SERVICE_ID_LENGTH: u32 = 50; + pub const MAX_SERVICE_TYPE_LENGTH: u32 = 50; + pub const MAX_SERVICE_URL_LENGTH: u32 = 100; + pub const MAX_SERVICE_TYPE_COUNT: u32 = 1; + pub const MAX_SERVICE_URL_COUNT: u32 = 1; + pub const MAX_DID_SERVICES_COUNT: u32 = 25; } From 24953c53d166597036ebb655772426ad29f12109 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 11:13:17 +0200 Subject: [PATCH 06/45] bench: add benchmarks for service endpoints --- pallets/did/Cargo.toml | 1 + pallets/did/src/benchmarking.rs | 142 +++++++++++++++++++++++++++++++- pallets/did/src/tests.rs | 13 +++ 3 files changed, 155 insertions(+), 1 deletion(-) diff --git a/pallets/did/Cargo.toml b/pallets/did/Cargo.toml index 255bebc7e..43747b820 100644 --- a/pallets/did/Cargo.toml +++ b/pallets/did/Cargo.toml @@ -56,6 +56,7 @@ runtime-benchmarks = [ "frame-benchmarking", "pallet-balances/runtime-benchmarks", "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", ] std = [ "codec/std", diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index daaa89ed4..926dd2b15 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -29,7 +29,7 @@ use sp_std::{convert::TryInto, vec::Vec}; use crate::{ did_details::*, - mock_utils::{generate_base_did_creation_details, generate_base_did_details, get_key_agreement_keys}, + mock_utils::{generate_base_did_creation_details, generate_base_did_details, get_key_agreement_keys, get_service_endpoints}, *, }; @@ -110,6 +110,9 @@ benchmarks! { /* create extrinsic */ create_ed25519_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); + // We only calculate weights based on how many endpoints are specified. For each endpoint, we use the max possible length and count for its components. + // This makes weight computation easier at runtime, at the cost of always having worst-case weights for any # of endpoints c. + let c in 1 .. T::MaxDidServicesCount::get(); let submitter: AccountIdOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); @@ -120,11 +123,20 @@ benchmarks! { let did_key_agreement_keys = get_key_agreement_keys::(n); let did_public_att_key = get_ed25519_public_attestation_key(); let did_public_del_key = get_ed25519_public_delegation_key(); + let service_endpoints = get_service_endpoints::( + c, + T::MaxServiceIdLength::get(), + T::MaxTypeCountPerService::get(), + T::MaxServiceTypeLength::get(), + T::MaxUrlCountPerService::get(), + T::MaxServiceUrlLength::get(), + ); let mut did_creation_details = generate_base_did_creation_details::(did_subject.clone(), submitter.clone()); did_creation_details.new_key_agreement_keys = did_key_agreement_keys; did_creation_details.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key)); did_creation_details.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key)); + did_creation_details.new_service_details = service_endpoints.clone(); let did_creation_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); }: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature)) @@ -152,11 +164,16 @@ benchmarks! { stored_did.attestation_key, Some(expected_attestation_key_id) ); + assert_eq!( + ServiceEndpoints::::iter_prefix(&did_subject).count(), + service_endpoints.len() + ); assert_eq!(stored_did.last_tx_counter, 0u64); } create_sr25519_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); + let c in 1 .. T::MaxDidServicesCount::get(); let submitter: AccountIdOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); make_free_for_did::(&submitter); @@ -166,11 +183,20 @@ benchmarks! { let did_key_agreement_keys = get_key_agreement_keys::(n); let did_public_att_key = get_sr25519_public_attestation_key(); let did_public_del_key = get_sr25519_public_delegation_key(); + let service_endpoints = get_service_endpoints::( + c, + T::MaxServiceIdLength::get(), + T::MaxTypeCountPerService::get(), + T::MaxServiceTypeLength::get(), + T::MaxUrlCountPerService::get(), + T::MaxServiceUrlLength::get(), + ); let mut did_creation_details = generate_base_did_creation_details::(did_subject.clone(), submitter.clone()); did_creation_details.new_key_agreement_keys = did_key_agreement_keys; did_creation_details.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key)); did_creation_details.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key)); + did_creation_details.new_service_details = service_endpoints.clone(); let did_creation_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature."); }: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature)) @@ -198,11 +224,16 @@ benchmarks! { stored_did.attestation_key, Some(expected_attestation_key_id) ); + assert_eq!( + ServiceEndpoints::::iter_prefix(&did_subject).count(), + service_endpoints.len() + ); assert_eq!(stored_did.last_tx_counter, 0u64); } create_ecdsa_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); + let c in 1 .. T::MaxDidServicesCount::get(); let submitter: AccountIdOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); make_free_for_did::(&submitter); @@ -212,11 +243,20 @@ benchmarks! { let did_key_agreement_keys = get_key_agreement_keys::(n); let did_public_att_key = get_ecdsa_public_attestation_key(); let did_public_del_key = get_ecdsa_public_delegation_key(); + let service_endpoints = get_service_endpoints::( + c, + T::MaxServiceIdLength::get(), + T::MaxTypeCountPerService::get(), + T::MaxServiceTypeLength::get(), + T::MaxUrlCountPerService::get(), + T::MaxServiceUrlLength::get(), + ); let mut did_creation_details = generate_base_did_creation_details::(did_subject.clone(), submitter.clone()); did_creation_details.new_key_agreement_keys = did_key_agreement_keys; did_creation_details.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key.clone())); did_creation_details.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key.clone())); + did_creation_details.new_service_details = service_endpoints.clone(); let did_creation_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature."); }: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature)) @@ -244,6 +284,10 @@ benchmarks! { stored_did.attestation_key, Some(expected_attestation_key_id) ); + assert_eq!( + ServiceEndpoints::::iter_prefix(&did_subject).count(), + service_endpoints.len() + ); assert_eq!(stored_did.last_tx_counter, 0u64); } @@ -252,12 +296,28 @@ benchmarks! { let did_subject: DidIdentifierOf = MultiSigner::from(did_public_auth_key).into_account().into(); let did_details = generate_base_did_details::(DidVerificationKey::from(did_public_auth_key)); + let service_endpoints = get_service_endpoints::( + T::MaxDidServicesCount::get(), + T::MaxServiceIdLength::get(), + T::MaxTypeCountPerService::get(), + T::MaxServiceTypeLength::get(), + T::MaxUrlCountPerService::get(), + T::MaxServiceUrlLength::get(), + ); + Did::::insert(&did_subject, did_details); + for endpoint in service_endpoints.iter() { + ServiceEndpoints::::insert(&did_subject, &endpoint.id, endpoint.clone()); + } }: _(RawOrigin::Signed(did_subject.clone())) verify { assert!( Did::::get(&did_subject).is_none() ); + assert_eq!( + ServiceEndpoints::::iter_prefix(&did_subject).count(), + 0 + ); } reclaim_deposit { @@ -265,12 +325,28 @@ benchmarks! { let did_subject: DidIdentifierOf = MultiSigner::from(did_public_auth_key).into_account().into(); let did_details = generate_base_did_details::(DidVerificationKey::from(did_public_auth_key)); + let service_endpoints = get_service_endpoints::( + T::MaxDidServicesCount::get(), + T::MaxServiceIdLength::get(), + T::MaxTypeCountPerService::get(), + T::MaxServiceTypeLength::get(), + T::MaxUrlCountPerService::get(), + T::MaxServiceUrlLength::get(), + ); + Did::::insert(&did_subject, did_details.clone()); + for endpoint in service_endpoints.iter() { + ServiceEndpoints::::insert(&did_subject, &endpoint.id, endpoint.clone()); + } }: _(RawOrigin::Signed(did_details.deposit.owner.clone()), did_subject.clone()) verify { assert!( Did::::get(&did_subject).is_none() ); + assert_eq!( + ServiceEndpoints::::iter_prefix(&did_subject).count(), + 0 + ); } /* submit_did_call extrinsic */ @@ -764,6 +840,70 @@ benchmarks! { assert!(!Did::::get(&did_subject).unwrap().key_agreement_keys.contains(&key_agreement_key_id)); } + add_service_endpoint { + let public_auth_key = get_ecdsa_public_authentication_key(); + let did_subject: DidIdentifierOf = MultiSigner::from(public_auth_key.clone()).into_account().into(); + let old_service_endpoints = get_service_endpoints::( + T::MaxDidServicesCount::get() - 1, + T::MaxServiceIdLength::get(), + T::MaxTypeCountPerService::get(), + T::MaxServiceTypeLength::get(), + T::MaxUrlCountPerService::get(), + T::MaxServiceUrlLength::get(), + ); + let new_service_endpoint = DidEndpointDetails { + id: b"id".to_vec(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()], + phantom_data: None, + }; + + let did_details = generate_base_did_details::(DidVerificationKey::from(public_auth_key)); + Did::::insert(&did_subject, did_details); + for old_endpoint in old_service_endpoints.iter() { + ServiceEndpoints::::insert(&did_subject, &old_endpoint.id, old_endpoint.clone()); + } + }: _(RawOrigin::Signed(did_subject.clone()), new_service_endpoint.clone()) + verify { + assert_eq!( + ServiceEndpoints::::get(&did_subject, &new_service_endpoint.id), + Some(new_service_endpoint) + ); + assert_eq!( + ServiceEndpoints::::iter_prefix(&did_subject).count().saturated_into::(), + T::MaxDidServicesCount::get() + ); + } + + remove_service_endpoint { + let public_auth_key = get_ecdsa_public_authentication_key(); + let did_subject: DidIdentifierOf = MultiSigner::from(public_auth_key.clone()).into_account().into(); + let old_service_endpoints = get_service_endpoints::( + T::MaxDidServicesCount::get(), + T::MaxServiceIdLength::get(), + T::MaxTypeCountPerService::get(), + T::MaxServiceTypeLength::get(), + T::MaxUrlCountPerService::get(), + T::MaxServiceUrlLength::get(), + ); + let endpoint_id = old_service_endpoints[0].id.clone(); + + let did_details = generate_base_did_details::(DidVerificationKey::from(public_auth_key)); + Did::::insert(&did_subject, did_details); + for old_endpoint in old_service_endpoints.iter() { + ServiceEndpoints::::insert(&did_subject, &old_endpoint.id, old_endpoint.clone()); + } + }: _(RawOrigin::Signed(did_subject.clone()), endpoint_id.clone()) + verify { + assert!( + ServiceEndpoints::::get(&did_subject, &endpoint_id).is_none() + ); + assert_eq!( + ServiceEndpoints::::iter_prefix(&did_subject).count().saturated_into::(), + T::MaxDidServicesCount::get() - 1 + ); + } + signature_verification_sr25519 { let l in 1 .. MAX_PAYLOAD_BYTE_LENGTH; diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index ac3995248..8dc727f31 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -1955,6 +1955,12 @@ fn check_did_not_present_deletion() { fn check_successful_reclaiming() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let old_service_endpoint = DidEndpointDetails { + id: b"id".to_vec(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()], + phantom_data: None, + }; let mut did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); did_details.deposit.owner = ACCOUNT_00; did_details.deposit.amount = ::Deposit::get(); @@ -1966,8 +1972,12 @@ fn check_successful_reclaiming() { ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .with_dids(vec![(alice_did.clone(), did_details)]) + .with_endpoints(vec![(alice_did.clone(), vec![old_service_endpoint.clone()])]) .build(None) .execute_with(|| { + assert_eq!( + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 1 + ); assert_eq!( Balances::reserved_balance(ACCOUNT_00), ::Deposit::get() @@ -1979,6 +1989,9 @@ fn check_successful_reclaiming() { assert!(Did::get_did(alice_did.clone()).is_none()); assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); + assert_eq!( + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 0 + ); // Re-adding the same DID identifier should fail. let details = generate_base_did_creation_details::(alice_did.clone(), ACCOUNT_00); From ab3df164e742342d12ef33c81f0cf6c5b9b2f682 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 11:24:59 +0200 Subject: [PATCH 07/45] chore: refactoring --- pallets/did/src/benchmarking.rs | 4 +- pallets/did/src/lib.rs | 21 +++-- pallets/did/src/tests.rs | 160 ++++++++++++++------------------ 3 files changed, 86 insertions(+), 99 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 926dd2b15..950af6993 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -29,7 +29,9 @@ use sp_std::{convert::TryInto, vec::Vec}; use crate::{ did_details::*, - mock_utils::{generate_base_did_creation_details, generate_base_did_details, get_key_agreement_keys, get_service_endpoints}, + mock_utils::{ + generate_base_did_creation_details, generate_base_did_details, get_key_agreement_keys, get_service_endpoints, + }, *, }; diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index d17c547e7..ca31f5588 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -145,6 +145,7 @@ use frame_system::RawOrigin; #[frame_support::pallet] pub mod pallet { use super::*; + use crate::service_endpoints::utils as service_endpoints_utils; use frame_support::{ pallet_prelude::*, traits::{Currency, ExistenceRequirement, Imbalance, ReservableCurrency}, @@ -510,7 +511,7 @@ pub mod pallet { // Validate all the size constraints for the service endpoints. let input_service_endpoints = details.new_service_details.clone(); - service_endpoints::utils::validate_new_service_endpoints(&input_service_endpoints) + service_endpoints_utils::validate_new_service_endpoints(&input_service_endpoints) .map_err(Error::::from)?; let did_entry = @@ -770,7 +771,7 @@ pub mod pallet { pub fn add_service_endpoint(origin: OriginFor, service_endpoint: DidEndpointDetails) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); - service_endpoints::utils::validate_single_service_endpoint(&service_endpoint).map_err(Error::::from)?; + service_endpoints_utils::validate_single_service_endpoint(&service_endpoint).map_err(Error::::from)?; // Verify that the DID is present. ensure!(Did::::get(&did_subject).is_some(), Error::::DidNotPresent); @@ -782,15 +783,17 @@ pub mod pallet { Error::::MaxServicesCountExceeded ); - // Verify that the service with the given ID does not exist. - ensure!( - ServiceEndpoints::::get(&did_subject, &service_endpoint.id).is_none(), - Error::::ServiceAlreadyPresent - ); - // *** No Fail beyond this point *** - ServiceEndpoints::::insert(&did_subject, service_endpoint.id.clone(), service_endpoint); + ServiceEndpoints::::try_mutate_exists( + &did_subject, + service_endpoint.id.clone(), + |existing_service| -> Result<(), Error> { + ensure!(&existing_service.is_none(), Error::::ServiceAlreadyPresent); + *existing_service = Some(service_endpoint); + Ok(()) + }, + )?; Ok(()) } diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 8dc727f31..85dad7576 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -24,7 +24,7 @@ use sp_runtime::{ }; use sp_std::{collections::btree_set::BTreeSet, convert::TryFrom}; -use crate::{self as did, DidEndpointDetails, mock::*, mock_utils::*}; +use crate::{self as did, mock::*, mock_utils::*, DidEndpointDetails}; // create @@ -232,15 +232,21 @@ fn check_successful_complete_creation() { .public_keys .contains_key(&generate_key_id(&details.new_delegation_key.clone().unwrap().into()))); - // We check that the service details in the creation operation have been all stored in the storage... + // We check that the service details in the creation operation have been all + // stored in the storage... details.new_service_details.iter().for_each(|new_service| { - let stored_service = Did::get_service_endpoints(&alice_did, &new_service.id).expect("Service endpoint should be stored."); + let stored_service = Did::get_service_endpoints(&alice_did, &new_service.id) + .expect("Service endpoint should be stored."); assert_eq!(stored_service.id, new_service.id); assert_eq!(stored_service.url, new_service.url); assert_eq!(stored_service.service_type, new_service.service_type); }); - // ... and that the number of elements in the creation operation is the same as the number of elements stored. - assert_eq!(did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), details.new_service_details.len()); + // ... and that the number of elements in the creation operation is the same as + // the number of elements stored. + assert_eq!( + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + details.new_service_details.len() + ); assert_eq!( Balances::reserved_balance(ACCOUNT_00), @@ -443,14 +449,8 @@ fn check_max_limit_service_endpoints_count_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); - details.new_service_details = get_service_endpoints( - ::MaxDidServicesCount::get() + 1, - 1, - 1, - 1, - 1, - 1, - ); + details.new_service_details = + get_service_endpoints(::MaxDidServicesCount::get() + 1, 1, 1, 1, 1, 1); let signature = auth_key.sign(details.encode().as_ref()); @@ -473,14 +473,8 @@ fn check_max_limit_service_id_length_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); - details.new_service_details = get_service_endpoints( - 1, - ::MaxServiceIdLength::get() + 1, - 1, - 1, - 1, - 1, - ); + details.new_service_details = + get_service_endpoints(1, ::MaxServiceIdLength::get() + 1, 1, 1, 1, 1); let signature = auth_key.sign(details.encode().as_ref()); @@ -503,14 +497,8 @@ fn check_max_limit_service_type_count_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); - details.new_service_details = get_service_endpoints( - 1, - 1, - ::MaxTypeCountPerService::get() + 1, - 1, - 1, - 1, - ); + details.new_service_details = + get_service_endpoints(1, 1, ::MaxTypeCountPerService::get() + 1, 1, 1, 1); let signature = auth_key.sign(details.encode().as_ref()); @@ -533,14 +521,8 @@ fn check_max_limit_service_type_length_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); - details.new_service_details = get_service_endpoints( - 1, - 1, - 1, - ::MaxServiceTypeLength::get() + 1, - 1, - 1, - ); + details.new_service_details = + get_service_endpoints(1, 1, 1, ::MaxServiceTypeLength::get() + 1, 1, 1); let signature = auth_key.sign(details.encode().as_ref()); @@ -563,14 +545,8 @@ fn check_max_limit_service_url_count_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); - details.new_service_details = get_service_endpoints( - 1, - 1, - 1, - 1, - ::MaxUrlCountPerService::get() + 1, - 1, - ); + details.new_service_details = + get_service_endpoints(1, 1, 1, 1, ::MaxUrlCountPerService::get() + 1, 1); let signature = auth_key.sign(details.encode().as_ref()); @@ -593,14 +569,8 @@ fn check_max_limit_service_url_length_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); - details.new_service_details = get_service_endpoints( - 1, - 1, - 1, - 1, - 1, - ::MaxServiceUrlLength::get() + 1, - ); + details.new_service_details = + get_service_endpoints(1, 1, 1, 1, 1, ::MaxServiceUrlLength::get() + 1); let signature = auth_key.sign(details.encode().as_ref()); @@ -1566,13 +1536,13 @@ fn check_service_addition_no_prior_service_successful() { .with_dids(vec![(alice_did.clone(), old_did_details)]) .build(None) .execute_with(|| { - assert_ok!( - Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint.clone()), - ); - let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id).expect("Service endpoint should be stored."); - assert_eq!( - stored_endpoint, new_service_endpoint - ); + assert_ok!(Did::add_service_endpoint( + Origin::signed(alice_did.clone()), + new_service_endpoint.clone() + ),); + let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id) + .expect("Service endpoint should be stored."); + assert_eq!(stored_endpoint, new_service_endpoint); }); } @@ -1603,16 +1573,19 @@ fn check_service_addition_one_from_full_successful() { .with_endpoints(vec![(alice_did.clone(), old_service_endpoints)]) .build(None) .execute_with(|| { - assert_ok!( - Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint.clone()), - ); - assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count().saturated_into::(), ::MaxDidServicesCount::get() - ); - let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id).expect("Service endpoint should be stored."); + assert_ok!(Did::add_service_endpoint( + Origin::signed(alice_did.clone()), + new_service_endpoint.clone() + ),); assert_eq!( - stored_endpoint, new_service_endpoint + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did) + .count() + .saturated_into::(), + ::MaxDidServicesCount::get() ); + let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id) + .expect("Service endpoint should be stored."); + assert_eq!(stored_endpoint, new_service_endpoint); }); } @@ -1627,14 +1600,12 @@ fn check_did_not_present_services_addition_error() { phantom_data: None, }; - ExtBuilder::default() - .build(None) - .execute_with(|| { - assert_noop!( - Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), - did::Error::::DidNotPresent - ); - }); + ExtBuilder::default().build(None).execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), + did::Error::::DidNotPresent + ); + }); } #[test] @@ -1706,7 +1677,8 @@ fn check_max_service_id_length_addition_error() { ::MaxServiceTypeLength::get(), ::MaxUrlCountPerService::get(), ::MaxServiceUrlLength::get(), - )[0].clone(); + )[0] + .clone(); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1732,7 +1704,8 @@ fn check_max_service_type_length_addition_error() { ::MaxServiceTypeLength::get() + 1, ::MaxUrlCountPerService::get(), ::MaxServiceUrlLength::get(), - )[0].clone(); + )[0] + .clone(); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1758,7 +1731,8 @@ fn check_max_service_type_count_addition_error() { ::MaxServiceTypeLength::get(), ::MaxUrlCountPerService::get(), ::MaxServiceUrlLength::get(), - )[0].clone(); + )[0] + .clone(); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1784,7 +1758,8 @@ fn check_max_service_url_length_addition_error() { ::MaxServiceTypeLength::get(), ::MaxUrlCountPerService::get(), ::MaxServiceUrlLength::get() + 1, - )[0].clone(); + )[0] + .clone(); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1810,7 +1785,8 @@ fn check_max_service_url_count_addition_error() { ::MaxServiceTypeLength::get(), ::MaxUrlCountPerService::get() + 1, ::MaxServiceUrlLength::get(), - )[0].clone(); + )[0] + .clone(); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1843,11 +1819,13 @@ fn check_service_deletion_successful() { .with_endpoints(vec![(alice_did.clone(), vec![old_service_endpoint.clone()])]) .build(None) .execute_with(|| { - assert_ok!( - Did::remove_service_endpoint(Origin::signed(alice_did.clone()), old_service_endpoint.id), - ); + assert_ok!(Did::remove_service_endpoint( + Origin::signed(alice_did.clone()), + old_service_endpoint.id + ),); assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 0 + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + 0 ); }); } @@ -1899,7 +1877,8 @@ fn check_successful_deletion() { .build(None) .execute_with(|| { assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 1 + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + 1 ); assert_eq!( Balances::reserved_balance(ACCOUNT_00), @@ -1911,7 +1890,8 @@ fn check_successful_deletion() { assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 0 + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + 0 ); // Re-adding the same DID identifier should fail. @@ -1976,7 +1956,8 @@ fn check_successful_reclaiming() { .build(None) .execute_with(|| { assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 1 + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + 1 ); assert_eq!( Balances::reserved_balance(ACCOUNT_00), @@ -1990,7 +1971,8 @@ fn check_successful_reclaiming() { assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 0 + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + 0 ); // Re-adding the same DID identifier should fail. From 29077a9adaf3a008ad7a78f76330dde90d7087e5 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 11:31:16 +0200 Subject: [PATCH 08/45] chore: add some more checks --- pallets/did/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index ca31f5588..71fd1019c 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -136,6 +136,7 @@ use frame_support::{ Parameter, }; use frame_system::ensure_signed; +use sp_io::KillStorageResult; use sp_runtime::{traits::Saturating, SaturatedConversion}; use sp_std::{boxed::Box, fmt::Debug, prelude::Clone}; @@ -1066,7 +1067,12 @@ impl Pallet { let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; // *** No Fail beyond this point *** - ServiceEndpoints::::remove_prefix(&did_subject, Some(T::MaxDidServicesCount::get())); + let storage_kill_result = ServiceEndpoints::::remove_prefix(&did_subject, Some(T::MaxDidServicesCount::get())); + + // If some items are remaining, it means that there were more than MaxDidServicesCount stored, and that should never happen. + if let KillStorageResult::SomeRemaining(_) = storage_kill_result { + return Err(Error::::InternalError.into()); + }; kilt_support::free_deposit::, CurrencyOf>(&did_entry.deposit); // Mark as deleted to prevent potential replay-attacks of re-adding a previously // deleted DID. From e665543c645f17e37c7e005ae8bff20bb25bb87d Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 12:19:28 +0200 Subject: [PATCH 09/45] wip: add checks for ASCII characters --- pallets/did/src/errors.rs | 1 + pallets/did/src/lib.rs | 2 ++ pallets/did/src/service_endpoints.rs | 19 ++++++++++---- pallets/did/src/utils.rs | 39 ++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/pallets/did/src/errors.rs b/pallets/did/src/errors.rs index c2a41f02b..bb2806b63 100644 --- a/pallets/did/src/errors.rs +++ b/pallets/did/src/errors.rs @@ -98,4 +98,5 @@ pub enum InputError { MaxIdLengthExceeded, MaxUrlLengthExceeded, MaxTypeLengthExceeded, + InvalidUrlEncoding, } diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 71fd1019c..3eb4b5cbb 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -394,6 +394,7 @@ pub mod pallet { MaxServicesCountExceeded, ServiceAlreadyPresent, ServiceNotPresent, + InvalidUrlEncoding, /// An error that is not supposed to take place, yet it happened. InternalError, } @@ -445,6 +446,7 @@ pub mod pallet { InputError::MaxTypeLengthExceeded => Self::MaxTypeLengthExceeded, InputError::MaxUrlCountExceeded => Self::MaxUrlCountExceeded, InputError::MaxUrlLengthExceeded => Self::MaxUrlLengthExceeded, + InputError::InvalidUrlEncoding => Self::InvalidUrlEncoding, } } } diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 05be55d45..b82255bc6 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -18,6 +18,9 @@ use crate::Config; use codec::{Decode, Encode}; +use sp_std::str; + +use crate::utils as crate_utils; // pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; @@ -43,7 +46,8 @@ pub struct DidEndpointDetails { pub mod utils { use super::*; use crate::InputError; - use frame_support::{ensure, traits::Get}; + use codec::Input; +use frame_support::{ensure, traits::Get}; use sp_runtime::traits::SaturatedConversion; pub(crate) fn validate_new_service_endpoints( @@ -76,28 +80,33 @@ pub mod utils { endpoint.url.len() <= T::MaxUrlCountPerService::get().saturated_into(), InputError::MaxUrlCountExceeded ); - // Check that the ID is the maximum allowed length. + // Check that the ID is the maximum allowed length and only contain ASCII characters. ensure!( endpoint.id.len() <= T::MaxServiceIdLength::get().saturated_into(), InputError::MaxIdLengthExceeded ); - // Check that all types are the maximum allowed length. + let str_id = str::from_utf8(&endpoint.id).map_err(|_| InputError::InvalidUrlEncoding)?; + ensure!(crate_utils::is_valid_ascii_url(str_id), InputError::InvalidUrlEncoding); + // Check that all types are the maximum allowed length and only contain ASCII characters. endpoint.service_type.iter().try_for_each(|s_type| { ensure!( s_type.len() <= T::MaxServiceTypeLength::get().saturated_into(), InputError::MaxTypeLengthExceeded ); + let str_type = str::from_utf8(s_type).map_err(|_| InputError::InvalidUrlEncoding)?; + ensure!(crate_utils::is_valid_ascii_url(str_type), InputError::InvalidUrlEncoding); Ok(()) })?; - // Check that all URLs are the maximum allowed length. + // Check that all URLs are the maximum allowed length AND only contain ASCII characters. endpoint.url.iter().try_for_each(|s_url| { ensure!( s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), InputError::MaxUrlLengthExceeded ); + let str_url = str::from_utf8(s_url).map_err(|_| InputError::InvalidUrlEncoding)?; + ensure!(crate_utils::is_valid_ascii_url(str_url), InputError::InvalidUrlEncoding); Ok(()) })?; - Ok(()) } } diff --git a/pallets/did/src/utils.rs b/pallets/did/src/utils.rs index ac28fdc9d..cce68ef88 100644 --- a/pallets/did/src/utils.rs +++ b/pallets/did/src/utils.rs @@ -26,3 +26,42 @@ pub fn calculate_key_id(key: &DidPublicKey) -> KeyIdOf { let hashed_values: Vec = key.encode(); T::Hashing::hash(&hashed_values) } + +/// Verifies that an input string contains only URL-allowed ASCII characters. +/// For more info about what those characters are, please visit the official RFC +/// 3986. +pub(crate) fn is_valid_ascii_url(input: &str) -> bool { + // Matches [0-9], [a-z], [A-Z], plus the symbols as in the RFC. + input.chars().all(|c| { + matches!(c, ':' | '/' | '?' | '#' | '[' | ']' | '@' | '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | ';' + | '=' | '-' | '.' | '_' | '~' | '%' | '0'..='9' | 'a'..='z' | 'A'..='Z') + }) +} + +#[test] +fn check_is_valid_ascii_url() { + let test_cases = [ + ("kilt.io", true), + ("super.long.domain.com:12345/path/to/directory#fragment?arg=value", true), + ("super.long.domain.com:12345/path/to/directory/file.txt", true), + ("domain.with.only.valid.characters.:/?#[]@!$&'()*+,;=-._~", true), + ("invalid.châracter.domain.org", false), + ("âinvalid.character.domain.org", false), + ("invalid.character.domain.orgâ", false), + ("", true), + ("kilt.io//invalid_ascii.com", false), + ("/invalid_ascii.com", false), + ("kilt.io//invalid_ascii.com>", false), + ("kilt.io/%3Ctag%3E/encoded_upper_case_ascii.com", true), + ("kilt.io/%3ctag%3e/encoded_lower_case_ascii.com", true), + ]; + + test_cases.iter().for_each(|(input, expected_result)| { + assert_eq!( + is_valid_ascii_url(input), + *expected_result, + "Test case for \"{}\" returned wrong result.", + input + ); + }); +} From 68db99a2a9d28a84a22ac946f30ff98feeb17655 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 13:59:47 +0200 Subject: [PATCH 10/45] feat: add input check for ASCII characters --- pallets/did/src/service_endpoints.rs | 9 +- pallets/did/src/tests.rs | 159 +++++++++++++++++++++++++++ pallets/did/src/utils.rs | 18 +-- 3 files changed, 169 insertions(+), 17 deletions(-) diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index b82255bc6..267d63ef0 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -46,8 +46,7 @@ pub struct DidEndpointDetails { pub mod utils { use super::*; use crate::InputError; - use codec::Input; -use frame_support::{ensure, traits::Get}; + use frame_support::{ensure, traits::Get}; use sp_runtime::traits::SaturatedConversion; pub(crate) fn validate_new_service_endpoints( @@ -86,7 +85,7 @@ use frame_support::{ensure, traits::Get}; InputError::MaxIdLengthExceeded ); let str_id = str::from_utf8(&endpoint.id).map_err(|_| InputError::InvalidUrlEncoding)?; - ensure!(crate_utils::is_valid_ascii_url(str_id), InputError::InvalidUrlEncoding); + ensure!(crate_utils::is_valid_ascii_string(str_id), InputError::InvalidUrlEncoding); // Check that all types are the maximum allowed length and only contain ASCII characters. endpoint.service_type.iter().try_for_each(|s_type| { ensure!( @@ -94,7 +93,7 @@ use frame_support::{ensure, traits::Get}; InputError::MaxTypeLengthExceeded ); let str_type = str::from_utf8(s_type).map_err(|_| InputError::InvalidUrlEncoding)?; - ensure!(crate_utils::is_valid_ascii_url(str_type), InputError::InvalidUrlEncoding); + ensure!(crate_utils::is_valid_ascii_string(str_type), InputError::InvalidUrlEncoding); Ok(()) })?; // Check that all URLs are the maximum allowed length AND only contain ASCII characters. @@ -104,7 +103,7 @@ use frame_support::{ensure, traits::Get}; InputError::MaxUrlLengthExceeded ); let str_url = str::from_utf8(s_url).map_err(|_| InputError::InvalidUrlEncoding)?; - ensure!(crate_utils::is_valid_ascii_url(str_url), InputError::InvalidUrlEncoding); + ensure!(crate_utils::is_valid_ascii_string(str_url), InputError::InvalidUrlEncoding); Ok(()) })?; Ok(()) diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 85dad7576..332e429c9 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -588,6 +588,93 @@ fn check_max_limit_service_url_length_did_creation() { }); } +#[test] +fn check_invalid_service_id_character_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let new_service_details = DidEndpointDetails:: { + phantom_data: None, + id: "å".bytes().collect(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()] + }; + let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); + details.new_service_details = vec![new_service_details]; + + let signature = auth_key.sign(details.encode().as_ref()); + + let balance = ::Deposit::get() + + ::Fee::get() + + <::Currency as Currency>>::minimum_balance(); + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + did::Error::::InvalidUrlEncoding + ); + }); +} + +#[test] +fn check_invalid_service_type_character_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let new_service_details = DidEndpointDetails:: { + phantom_data: None, + id: b"id".to_vec(), + service_type: vec!["å".bytes().collect()], + url: vec![b"url".to_vec()] + }; + let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); + details.new_service_details = vec![new_service_details]; + + let signature = auth_key.sign(details.encode().as_ref()); + + let balance = ::Deposit::get() + + ::Fee::get() + + <::Currency as Currency>>::minimum_balance(); + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + did::Error::::InvalidUrlEncoding + ); + }); +} + +#[test] +fn check_invalid_service_url_character_did_creation() { + let auth_key = get_sr25519_authentication_key(true); + let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); + let new_service_details = DidEndpointDetails:: { + phantom_data: None, + id: b"id".to_vec(), + service_type: vec![b"type".to_vec()], + url: vec!["å".bytes().collect()] + }; + let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); + details.new_service_details = vec![new_service_details]; + + let signature = auth_key.sign(details.encode().as_ref()); + + let balance = ::Deposit::get() + + ::Fee::get() + + <::Currency as Currency>>::minimum_balance(); + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + did::Error::::InvalidUrlEncoding + ); + }); +} + // updates #[test] @@ -1801,6 +1888,78 @@ fn check_max_service_url_count_addition_error() { }); } +#[test] +fn check_invalid_service_id_character_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let new_service_details = DidEndpointDetails:: { + phantom_data: None, + id: "å".bytes().collect(), + service_type: vec![b"type".to_vec()], + url: vec![b"url".to_vec()] + }; + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_details), + did::Error::::InvalidUrlEncoding + ); + }); +} + +#[test] +fn check_invalid_service_type_character_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let new_service_details = DidEndpointDetails:: { + phantom_data: None, + id: b"id".to_vec(), + service_type: vec!["å".bytes().collect()], + url: vec![b"url".to_vec()] + }; + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_details), + did::Error::::InvalidUrlEncoding + ); + }); +} + +#[test] +fn check_invalid_service_url_character_addition_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let new_service_details = DidEndpointDetails:: { + phantom_data: None, + id: b"id".to_vec(), + service_type: vec![b"url".to_vec()], + url: vec!["å".bytes().collect()] + }; + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_details), + did::Error::::InvalidUrlEncoding + ); + }); +} + #[test] fn check_service_deletion_successful() { let auth_key = get_ed25519_authentication_key(true); diff --git a/pallets/did/src/utils.rs b/pallets/did/src/utils.rs index cce68ef88..e09fa0dc4 100644 --- a/pallets/did/src/utils.rs +++ b/pallets/did/src/utils.rs @@ -27,19 +27,15 @@ pub fn calculate_key_id(key: &DidPublicKey) -> KeyIdOf { T::Hashing::hash(&hashed_values) } -/// Verifies that an input string contains only URL-allowed ASCII characters. -/// For more info about what those characters are, please visit the official RFC -/// 3986. -pub(crate) fn is_valid_ascii_url(input: &str) -> bool { - // Matches [0-9], [a-z], [A-Z], plus the symbols as in the RFC. +/// Verifies that an input string contains only traditional (non-extended) ASCII characters. +pub(crate) fn is_valid_ascii_string(input: &str) -> bool { input.chars().all(|c| { - matches!(c, ':' | '/' | '?' | '#' | '[' | ']' | '@' | '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | ';' - | '=' | '-' | '.' | '_' | '~' | '%' | '0'..='9' | 'a'..='z' | 'A'..='Z') + c.is_ascii() }) } #[test] -fn check_is_valid_ascii_url() { +fn check_is_valid_ascii_string() { let test_cases = [ ("kilt.io", true), ("super.long.domain.com:12345/path/to/directory#fragment?arg=value", true), @@ -49,16 +45,14 @@ fn check_is_valid_ascii_url() { ("âinvalid.character.domain.org", false), ("invalid.character.domain.orgâ", false), ("", true), - ("kilt.io//invalid_ascii.com", false), - ("/invalid_ascii.com", false), - ("kilt.io//invalid_ascii.com>", false), + ("例子.領域.cn", false), ("kilt.io/%3Ctag%3E/encoded_upper_case_ascii.com", true), ("kilt.io/%3ctag%3e/encoded_lower_case_ascii.com", true), ]; test_cases.iter().for_each(|(input, expected_result)| { assert_eq!( - is_valid_ascii_url(input), + is_valid_ascii_string(input), *expected_result, "Test case for \"{}\" returned wrong result.", input From e96be02de5ec2fcbfff2550e1b28da28ed73bed3 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 14:07:17 +0200 Subject: [PATCH 11/45] chore: unused errors --- pallets/did/src/errors.rs | 6 ------ pallets/did/src/lib.rs | 11 ----------- 2 files changed, 17 deletions(-) diff --git a/pallets/did/src/errors.rs b/pallets/did/src/errors.rs index bb2806b63..7fbb79ee4 100644 --- a/pallets/did/src/errors.rs +++ b/pallets/did/src/errors.rs @@ -55,9 +55,6 @@ pub enum StorageError { DidKeyNotPresent(DidVerificationKeyRelationship), /// At least one key referenced is not stored under the given DID. KeyNotPresent, - /// The user tries to delete a verification key that is currently being - /// used to authorize operations, and this is not allowed. - CurrentlyActiveKey, /// The maximum number of public keys for this DID key identifier has /// been reached. MaxPublicKeysPerDidExceeded, @@ -89,9 +86,6 @@ pub enum InputError { /// A number of new key agreement keys greater than the maximum allowed has /// been provided. MaxKeyAgreementKeysLimitExceeded, - /// A number of new verification keys to remove greater than the maximum - /// allowed has been provided. - MaxVerificationKeysToRemoveLimitExceeded, MaxServicesCountExceeded, MaxUrlCountExceeded, MaxTypeCountExceeded, diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 3eb4b5cbb..7ae49cc18 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -351,25 +351,16 @@ pub mod pallet { DidAlreadyPresent, /// No DID with the given identifier is present on chain. DidNotPresent, - /// The DID fragment is not present in the DID details. - DidFragmentNotPresent, /// One or more verification keys referenced are not stored in the set /// of verification keys. VerificationKeyNotPresent, /// The DID operation nonce is not equal to the current DID nonce + 1. InvalidNonce, - /// The user tries to delete a verification key that is currently being - /// used as an authentication, delegation, or attestation key, and this - /// is not allowed. - CurrentlyActiveKey, /// The called extrinsic does not support DID authorisation. UnsupportedDidAuthorizationCall, /// A number of new key agreement keys greater than the maximum allowed /// has been provided. MaxKeyAgreementKeysLimitExceeded, - /// A number of new verification keys to remove greater than the maximum - /// allowed has been provided. - MaxVerificationKeysToRemoveLimitExceeded, /// The maximum number of public keys for this DID key identifier has /// been reached. MaxPublicKeysPerDidExceeded, @@ -416,7 +407,6 @@ pub mod pallet { StorageError::DidNotPresent => Self::DidNotPresent, StorageError::DidAlreadyPresent => Self::DidAlreadyPresent, StorageError::DidKeyNotPresent(_) | StorageError::KeyNotPresent => Self::VerificationKeyNotPresent, - StorageError::CurrentlyActiveKey => Self::CurrentlyActiveKey, StorageError::MaxPublicKeysPerDidExceeded => Self::MaxPublicKeysPerDidExceeded, StorageError::MaxTotalKeyAgreementKeysExceeded => Self::MaxTotalKeyAgreementKeysExceeded, StorageError::DidAlreadyDeleted => Self::DidAlreadyDeleted, @@ -439,7 +429,6 @@ pub mod pallet { fn from(error: InputError) -> Self { match error { InputError::MaxKeyAgreementKeysLimitExceeded => Self::MaxKeyAgreementKeysLimitExceeded, - InputError::MaxVerificationKeysToRemoveLimitExceeded => Self::MaxVerificationKeysToRemoveLimitExceeded, InputError::MaxIdLengthExceeded => Self::MaxIdLengthExceeded, InputError::MaxServicesCountExceeded => Self::MaxServicesCountExceeded, InputError::MaxTypeCountExceeded => Self::MaxTypeCountExceeded, From 56fe1789046ebf63f87b8e2eba78fe9328c86efa Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 15:08:04 +0200 Subject: [PATCH 12/45] feat: migrate everything to bounded vectors --- pallets/did/src/benchmarking.rs | 7 +- pallets/did/src/lib.rs | 23 ++++-- pallets/did/src/mock_utils.rs | 7 +- pallets/did/src/service_endpoints.rs | 80 ++++++++++++------ pallets/did/src/tests.rs | 118 ++++++++------------------- pallets/did/src/utils.rs | 7 +- 6 files changed, 112 insertions(+), 130 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 950af6993..fabc4fe1b 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -854,10 +854,9 @@ benchmarks! { T::MaxServiceUrlLength::get(), ); let new_service_endpoint = DidEndpointDetails { - id: b"id".to_vec(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()], - phantom_data: None, + id: b"id".to_vec().try_into().unwrap(), + service_type: vec![b"type".to_vec().try_into().unwrap()].try_into().unwrap(), + url: vec![b"url".to_vec().try_into().unwrap()].try_into().unwrap(), }; let did_details = generate_base_did_details::(DidVerificationKey::from(public_auth_key)); diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 7ae49cc18..32be0f1c4 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -305,8 +305,14 @@ pub mod pallet { /// It maps from (DID identifier, service ID) to the service details. #[pallet::storage] #[pallet::getter(fn get_service_endpoints)] - pub type ServiceEndpoints = - StorageDoubleMap<_, Twox64Concat, DidIdentifierOf, Blake2_128Concat, Vec, DidEndpointDetails>; + pub type ServiceEndpoints = StorageDoubleMap< + _, + Twox64Concat, + DidIdentifierOf, + Blake2_128Concat, + ServiceEndpointId, + DidEndpointDetails, + >; /// The set of DIDs that have been deleted and cannot therefore be created /// again for security reasons. @@ -528,7 +534,7 @@ pub mod pallet { Did::::insert(&did_identifier, did_entry); input_service_endpoints.iter().for_each(|service| { - ServiceEndpoints::::insert(&did_identifier, &service.id, service); + ServiceEndpoints::::insert(&did_identifier, &service.id, service.clone()); }); Self::deposit_event(Event::DidCreated(sender, did_identifier)); @@ -763,7 +769,8 @@ pub mod pallet { pub fn add_service_endpoint(origin: OriginFor, service_endpoint: DidEndpointDetails) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); - service_endpoints_utils::validate_single_service_endpoint(&service_endpoint).map_err(Error::::from)?; + service_endpoints_utils::validate_single_service_endpoint_entry(&service_endpoint) + .map_err(Error::::from)?; // Verify that the DID is present. ensure!(Did::::get(&did_subject).is_some(), Error::::DidNotPresent); @@ -791,7 +798,7 @@ pub mod pallet { } #[pallet::weight(100_000)] - pub fn remove_service_endpoint(origin: OriginFor, service_id: Vec) -> DispatchResult { + pub fn remove_service_endpoint(origin: OriginFor, service_id: ServiceEndpointId) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); ensure!( @@ -1058,9 +1065,11 @@ impl Pallet { let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; // *** No Fail beyond this point *** - let storage_kill_result = ServiceEndpoints::::remove_prefix(&did_subject, Some(T::MaxDidServicesCount::get())); + let storage_kill_result = + ServiceEndpoints::::remove_prefix(&did_subject, Some(T::MaxDidServicesCount::get())); - // If some items are remaining, it means that there were more than MaxDidServicesCount stored, and that should never happen. + // If some items are remaining, it means that there were more than + // MaxDidServicesCount stored, and that should never happen. if let KillStorageResult::SomeRemaining(_) = storage_kill_result { return Err(Error::::InternalError.into()); }; diff --git a/pallets/did/src/mock_utils.rs b/pallets/did/src/mock_utils.rs index a156b9152..68ff098b7 100644 --- a/pallets/did/src/mock_utils.rs +++ b/pallets/did/src/mock_utils.rs @@ -69,12 +69,7 @@ pub fn get_service_endpoints( endpoint_url }) .collect(); - DidEndpointDetails { - id: endpoint_id, - service_type: endpoint_types, - url: endpoint_urls, - phantom_data: None, - } + DidEndpointDetails::new(endpoint_id, endpoint_types, endpoint_urls) }) .collect() } diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 267d63ef0..2c037e8ec 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -18,29 +18,51 @@ use crate::Config; use codec::{Decode, Encode}; +use frame_support::BoundedVec; use sp_std::str; +#[cfg(test)] +use sp_std::convert::TryInto; use crate::utils as crate_utils; -// pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; -pub type ServiceEndpointId = Vec; -// pub type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; -pub type ServiceEndpointType = Vec; -// pub type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; -pub type ServiceEndpointUrl = Vec; +pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; -// pub(crate) type DidNewServiceEndpoints = -// BoundedBTreeSet, ::MaxDidServicesCount>; +pub type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; +pub type ServiceEndpointTypeEntries = BoundedVec, ::MaxTypeCountPerService>; + +pub type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; +pub type ServiceEndpointUrlEntries = BoundedVec, ::MaxUrlCountPerService>; #[derive(Clone, Debug, Decode, Encode, PartialEq, Eq)] pub struct DidEndpointDetails { - pub(crate) phantom_data: Option>, - pub(crate) id: ServiceEndpointId, - pub(crate) service_type: Vec, - pub(crate) url: Vec, + pub(crate) id: ServiceEndpointId, + pub(crate) service_type: ServiceEndpointTypeEntries, + pub(crate) url: ServiceEndpointUrlEntries, +} + +#[cfg(test)] +impl DidEndpointDetails { + pub(crate) fn new(id: Vec, types: Vec>, urls: Vec>) -> Self { + let bounded_id = id.try_into().expect("Service ID too long."); + let bounded_types = types + .iter() + .map(|el| el.to_vec().try_into().expect("Service type too long.")) + .collect::>>() + .try_into() + .expect("Too many types for the given service."); + let bounded_urls = urls + .iter() + .map(|el| el.to_vec().try_into().expect("Service URL too long.")) + .collect::>>() + .try_into() + .expect("Too many URLs for the given service."); + + Self { + id: bounded_id, + service_type: bounded_types, + url: bounded_urls, + } + } } pub mod utils { @@ -61,12 +83,12 @@ pub mod utils { // For each service... endpoints .iter() - .try_for_each(|endpoint| validate_single_service_endpoint(endpoint))?; + .try_for_each(|endpoint| validate_single_service_endpoint_entry(endpoint))?; Ok(()) } - pub(crate) fn validate_single_service_endpoint( + pub(crate) fn validate_single_service_endpoint_entry( endpoint: &DidEndpointDetails, ) -> Result<(), InputError> { // Check that the maximum number of service types is provided. @@ -79,31 +101,43 @@ pub mod utils { endpoint.url.len() <= T::MaxUrlCountPerService::get().saturated_into(), InputError::MaxUrlCountExceeded ); - // Check that the ID is the maximum allowed length and only contain ASCII characters. + // Check that the ID is the maximum allowed length and only contain ASCII + // characters. ensure!( endpoint.id.len() <= T::MaxServiceIdLength::get().saturated_into(), InputError::MaxIdLengthExceeded ); let str_id = str::from_utf8(&endpoint.id).map_err(|_| InputError::InvalidUrlEncoding)?; - ensure!(crate_utils::is_valid_ascii_string(str_id), InputError::InvalidUrlEncoding); - // Check that all types are the maximum allowed length and only contain ASCII characters. + ensure!( + crate_utils::is_valid_ascii_string(str_id), + InputError::InvalidUrlEncoding + ); + // Check that all types are the maximum allowed length and only contain ASCII + // characters. endpoint.service_type.iter().try_for_each(|s_type| { ensure!( s_type.len() <= T::MaxServiceTypeLength::get().saturated_into(), InputError::MaxTypeLengthExceeded ); let str_type = str::from_utf8(s_type).map_err(|_| InputError::InvalidUrlEncoding)?; - ensure!(crate_utils::is_valid_ascii_string(str_type), InputError::InvalidUrlEncoding); + ensure!( + crate_utils::is_valid_ascii_string(str_type), + InputError::InvalidUrlEncoding + ); Ok(()) })?; - // Check that all URLs are the maximum allowed length AND only contain ASCII characters. + // Check that all URLs are the maximum allowed length AND only contain ASCII + // characters. endpoint.url.iter().try_for_each(|s_url| { ensure!( s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), InputError::MaxUrlLengthExceeded ); let str_url = str::from_utf8(s_url).map_err(|_| InputError::InvalidUrlEncoding)?; - ensure!(crate_utils::is_valid_ascii_string(str_url), InputError::InvalidUrlEncoding); + ensure!( + crate_utils::is_valid_ascii_string(str_url), + InputError::InvalidUrlEncoding + ); Ok(()) })?; Ok(()) diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 332e429c9..50c27b79f 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -22,7 +22,7 @@ use sp_runtime::{ traits::{BadOrigin, Zero}, SaturatedConversion, }; -use sp_std::{collections::btree_set::BTreeSet, convert::TryFrom}; +use sp_std::{collections::btree_set::BTreeSet, convert::{TryFrom, TryInto}}; use crate::{self as did, mock::*, mock_utils::*, DidEndpointDetails}; @@ -469,6 +469,7 @@ fn check_max_limit_service_endpoints_count_did_creation() { } #[test] +#[should_panic = "Service ID too long."] fn check_max_limit_service_id_length_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); @@ -493,6 +494,7 @@ fn check_max_limit_service_id_length_did_creation() { } #[test] +#[should_panic = "Too many types for the given service."] fn check_max_limit_service_type_count_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); @@ -517,6 +519,7 @@ fn check_max_limit_service_type_count_did_creation() { } #[test] +#[should_panic = "Service type too long."] fn check_max_limit_service_type_length_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); @@ -541,6 +544,7 @@ fn check_max_limit_service_type_length_did_creation() { } #[test] +#[should_panic = "Too many URLs for the given service."] fn check_max_limit_service_url_count_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); @@ -565,6 +569,7 @@ fn check_max_limit_service_url_count_did_creation() { } #[test] +#[should_panic = "URL too long."] fn check_max_limit_service_url_length_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); @@ -592,12 +597,8 @@ fn check_max_limit_service_url_length_did_creation() { fn check_invalid_service_id_character_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let new_service_details = DidEndpointDetails:: { - phantom_data: None, - id: "å".bytes().collect(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()] - }; + let new_service_details = + DidEndpointDetails::new("å".bytes().collect(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = vec![new_service_details]; @@ -621,12 +622,8 @@ fn check_invalid_service_id_character_did_creation() { fn check_invalid_service_type_character_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let new_service_details = DidEndpointDetails:: { - phantom_data: None, - id: b"id".to_vec(), - service_type: vec!["å".bytes().collect()], - url: vec![b"url".to_vec()] - }; + let new_service_details = + DidEndpointDetails::new(b"id".to_vec(), vec!["å".bytes().collect()], vec![b"url".to_vec()]); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = vec![new_service_details]; @@ -650,12 +647,8 @@ fn check_invalid_service_type_character_did_creation() { fn check_invalid_service_url_character_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let new_service_details = DidEndpointDetails:: { - phantom_data: None, - id: b"id".to_vec(), - service_type: vec![b"type".to_vec()], - url: vec!["å".bytes().collect()] - }; + let new_service_details = + DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec!["å".bytes().collect()]); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = vec![new_service_details]; @@ -1610,12 +1603,7 @@ fn check_key_not_found_key_agreement_key_deletion_error() { fn check_service_addition_no_prior_service_successful() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let new_service_endpoint = DidEndpointDetails { - id: b"id".to_vec(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()], - phantom_data: None, - }; + let new_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1646,12 +1634,7 @@ fn check_service_addition_one_from_full_successful() { ::MaxUrlCountPerService::get(), ::MaxServiceUrlLength::get(), ); - let new_service_endpoint = DidEndpointDetails { - id: b"id".to_vec(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()], - phantom_data: None, - }; + let new_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1680,12 +1663,7 @@ fn check_service_addition_one_from_full_successful() { fn check_did_not_present_services_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let new_service_endpoint = DidEndpointDetails { - id: b"id".to_vec(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()], - phantom_data: None, - }; + let new_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); ExtBuilder::default().build(None).execute_with(|| { assert_noop!( @@ -1699,12 +1677,7 @@ fn check_did_not_present_services_addition_error() { fn check_service_already_present_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let service_endpoint = DidEndpointDetails { - id: b"id".to_vec(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()], - phantom_data: None, - }; + let service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1732,12 +1705,7 @@ fn check_max_services_count_addition_error() { ::MaxUrlCountPerService::get(), ::MaxServiceUrlLength::get(), ); - let new_service_endpoint = DidEndpointDetails { - id: b"id".to_vec(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()], - phantom_data: None, - }; + let new_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1754,6 +1722,7 @@ fn check_max_services_count_addition_error() { } #[test] +#[should_panic = "Service ID too long."] fn check_max_service_id_length_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); @@ -1781,6 +1750,7 @@ fn check_max_service_id_length_addition_error() { } #[test] +#[should_panic = "Service type too long."] fn check_max_service_type_length_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); @@ -1808,6 +1778,7 @@ fn check_max_service_type_length_addition_error() { } #[test] +#[should_panic = "Too many types for the given service."] fn check_max_service_type_count_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); @@ -1835,6 +1806,7 @@ fn check_max_service_type_count_addition_error() { } #[test] +#[should_panic = "Service URL too long."] fn check_max_service_url_length_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); @@ -1862,6 +1834,7 @@ fn check_max_service_url_length_addition_error() { } #[test] +#[should_panic = "Too many URLs for the given service."] fn check_max_service_url_count_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); @@ -1892,12 +1865,8 @@ fn check_max_service_url_count_addition_error() { fn check_invalid_service_id_character_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let new_service_details = DidEndpointDetails:: { - phantom_data: None, - id: "å".bytes().collect(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()] - }; + let new_service_details = + DidEndpointDetails::new("å".bytes().collect(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1916,12 +1885,8 @@ fn check_invalid_service_id_character_addition_error() { fn check_invalid_service_type_character_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let new_service_details = DidEndpointDetails:: { - phantom_data: None, - id: b"id".to_vec(), - service_type: vec!["å".bytes().collect()], - url: vec![b"url".to_vec()] - }; + let new_service_details = + DidEndpointDetails::new(b"id".to_vec(), vec!["å".bytes().collect()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1940,12 +1905,8 @@ fn check_invalid_service_type_character_addition_error() { fn check_invalid_service_url_character_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let new_service_details = DidEndpointDetails:: { - phantom_data: None, - id: b"id".to_vec(), - service_type: vec![b"url".to_vec()], - url: vec!["å".bytes().collect()] - }; + let new_service_details = + DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec!["å".bytes().collect()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1964,12 +1925,7 @@ fn check_invalid_service_url_character_addition_error() { fn check_service_deletion_successful() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let old_service_endpoint = DidEndpointDetails { - id: b"id".to_vec(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()], - phantom_data: None, - }; + let old_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -2002,7 +1958,7 @@ fn check_service_not_present_deletion_error() { .build(None) .execute_with(|| { assert_noop!( - Did::remove_service_endpoint(Origin::signed(alice_did.clone()), service_id), + Did::remove_service_endpoint(Origin::signed(alice_did.clone()), service_id.try_into().expect("Service ID to delete too long")), did::Error::::ServiceNotPresent ); }); @@ -2014,12 +1970,7 @@ fn check_service_not_present_deletion_error() { fn check_successful_deletion() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let service_endpoint = DidEndpointDetails { - id: b"id".to_vec(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()], - phantom_data: None, - }; + let service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let mut did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); did_details.deposit.owner = ACCOUNT_00; @@ -2094,12 +2045,7 @@ fn check_did_not_present_deletion() { fn check_successful_reclaiming() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let old_service_endpoint = DidEndpointDetails { - id: b"id".to_vec(), - service_type: vec![b"type".to_vec()], - url: vec![b"url".to_vec()], - phantom_data: None, - }; + let old_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let mut did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); did_details.deposit.owner = ACCOUNT_00; did_details.deposit.amount = ::Deposit::get(); diff --git a/pallets/did/src/utils.rs b/pallets/did/src/utils.rs index e09fa0dc4..020845225 100644 --- a/pallets/did/src/utils.rs +++ b/pallets/did/src/utils.rs @@ -27,11 +27,10 @@ pub fn calculate_key_id(key: &DidPublicKey) -> KeyIdOf { T::Hashing::hash(&hashed_values) } -/// Verifies that an input string contains only traditional (non-extended) ASCII characters. +/// Verifies that an input string contains only traditional (non-extended) ASCII +/// characters. pub(crate) fn is_valid_ascii_string(input: &str) -> bool { - input.chars().all(|c| { - c.is_ascii() - }) + input.chars().all(|c| c.is_ascii()) } #[test] From 571c1bb8eab07a2d85ecb8fa008df5380c899e6e Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 15:32:13 +0200 Subject: [PATCH 13/45] chore: --all build working --- pallets/did/src/benchmarking.rs | 2 +- pallets/did/src/did_details.rs | 20 ++++++++++++++++++-- pallets/did/src/mock_utils.rs | 1 + pallets/did/src/service_endpoints.rs | 18 ++++++++++++++---- pallets/did/src/tests.rs | 14 ++++++++++---- runtimes/peregrine/src/lib.rs | 18 ++++++++++++++++-- runtimes/spiritnet/src/lib.rs | 18 ++++++++++++++++-- runtimes/standalone/src/lib.rs | 18 ++++++++++++++++-- 8 files changed, 92 insertions(+), 17 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index fabc4fe1b..eaecd9e3f 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -25,7 +25,7 @@ use kilt_support::signature::VerifySignature; use sp_core::{crypto::KeyTypeId, ecdsa, ed25519, sr25519}; use sp_io::crypto::{ecdsa_generate, ecdsa_sign, ed25519_generate, ed25519_sign, sr25519_generate, sr25519_sign}; use sp_runtime::{traits::IdentifyAccount, MultiSigner}; -use sp_std::{convert::TryInto, vec::Vec}; +use sp_std::{convert::TryInto, vec, vec::Vec}; use crate::{ did_details::*, diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index 739406b80..d2a89e5b0 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -21,7 +21,7 @@ use frame_support::storage::{bounded_btree_map::BoundedBTreeMap, bounded_btree_s use kilt_support::deposit::Deposit; use sp_core::{ecdsa, ed25519, sr25519}; use sp_runtime::{traits::Verify, MultiSignature}; -use sp_std::convert::TryInto; +use sp_std::{convert::TryInto, vec::Vec}; use crate::*; @@ -537,7 +537,7 @@ pub(crate) type DidPublicKeyMap = BoundedBTreeMap, DidPublicKeyDetails, ::MaxPublicKeysPerDid>; /// The details of a new DID to create. -#[derive(Clone, Debug, Decode, Encode, PartialEq)] +#[derive(Clone, Decode, Encode, PartialEq)] pub struct DidCreationDetails { /// The DID identifier. It has to be unique. pub did: DidIdentifierOf, @@ -552,6 +552,22 @@ pub struct DidCreationDetails { pub new_service_details: Vec>, } +impl sp_std::fmt::Debug for DidCreationDetails { + fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result { + f.debug_struct("DidCreationDetails") + .field("did", &self.did) + .field("submitter", &self.submitter) + .field( + "new_key_agreement_keys", + &self.new_key_agreement_keys.clone().into_inner(), + ) + .field("new_attestation_key", &self.new_attestation_key) + .field("new_delegation_key", &self.new_delegation_key) + .field("new_service_details", &self.new_service_details) + .finish() + } +} + /// Trait for extrinsic DID-based authorization. /// /// The trait allows diff --git a/pallets/did/src/mock_utils.rs b/pallets/did/src/mock_utils.rs index 68ff098b7..99b12b5ea 100644 --- a/pallets/did/src/mock_utils.rs +++ b/pallets/did/src/mock_utils.rs @@ -24,6 +24,7 @@ use sp_runtime::traits::Zero; use sp_std::{ collections::btree_set::BTreeSet, convert::{TryFrom, TryInto}, + vec::Vec, }; pub fn get_key_agreement_keys(n_keys: u32) -> DidNewKeyAgreementKeySet { diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 2c037e8ec..7bb665047 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -20,8 +20,8 @@ use crate::Config; use codec::{Decode, Encode}; use frame_support::BoundedVec; use sp_std::str; -#[cfg(test)] -use sp_std::convert::TryInto; +#[cfg(any(test, feature = "runtime-benchmarks"))] +use sp_std::{convert::TryInto, vec::Vec}; use crate::utils as crate_utils; @@ -33,14 +33,24 @@ pub type ServiceEndpointTypeEntries = BoundedVec, = BoundedVec::MaxServiceUrlLength>; pub type ServiceEndpointUrlEntries = BoundedVec, ::MaxUrlCountPerService>; -#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq)] +#[derive(Clone, Decode, Encode, PartialEq, Eq)] pub struct DidEndpointDetails { pub(crate) id: ServiceEndpointId, pub(crate) service_type: ServiceEndpointTypeEntries, pub(crate) url: ServiceEndpointUrlEntries, } -#[cfg(test)] +impl sp_std::fmt::Debug for DidEndpointDetails { + fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result { + f.debug_struct("DidEndpointDetails") + .field("id", &self.id.clone().into_inner()) + .field("service_type", &self.service_type.encode()) + .field("url", &self.url.encode()) + .finish() + } +} + +#[cfg(any(test, feature = "runtime-benchmarks"))] impl DidEndpointDetails { pub(crate) fn new(id: Vec, types: Vec>, urls: Vec>) -> Self { let bounded_id = id.try_into().expect("Service ID too long."); diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 50c27b79f..6f1550c07 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -22,7 +22,10 @@ use sp_runtime::{ traits::{BadOrigin, Zero}, SaturatedConversion, }; -use sp_std::{collections::btree_set::BTreeSet, convert::{TryFrom, TryInto}}; +use sp_std::{ + collections::btree_set::BTreeSet, + convert::{TryFrom, TryInto}, +}; use crate::{self as did, mock::*, mock_utils::*, DidEndpointDetails}; @@ -1958,7 +1961,10 @@ fn check_service_not_present_deletion_error() { .build(None) .execute_with(|| { assert_noop!( - Did::remove_service_endpoint(Origin::signed(alice_did.clone()), service_id.try_into().expect("Service ID to delete too long")), + Did::remove_service_endpoint( + Origin::signed(alice_did.clone()), + service_id.try_into().expect("Service ID to delete too long") + ), did::Error::::ServiceNotPresent ); }); @@ -1983,7 +1989,7 @@ fn check_successful_deletion() { ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .with_dids(vec![(alice_did.clone(), did_details)]) - .with_endpoints(vec![(alice_did.clone(), vec![service_endpoint.clone()])]) + .with_endpoints(vec![(alice_did.clone(), vec![service_endpoint])]) .build(None) .execute_with(|| { assert_eq!( @@ -2057,7 +2063,7 @@ fn check_successful_reclaiming() { ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, balance)]) .with_dids(vec![(alice_did.clone(), did_details)]) - .with_endpoints(vec![(alice_did.clone(), vec![old_service_endpoint.clone()])]) + .with_endpoints(vec![(alice_did.clone(), vec![old_service_endpoint])]) .build(None) .execute_with(|| { assert_eq!( diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 03c06f305..ebc6fe0ab 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -60,8 +60,10 @@ use kilt_primitives::{ MAX_SIGNATURE_BYTE_LENGTH, }, did::{ - DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_ENDPOINT_URLS_COUNT, MAX_KEY_AGREEMENT_KEYS, - MAX_PUBLIC_KEYS_PER_DID, MAX_TOTAL_KEY_AGREEMENT_KEYS, MAX_URL_LENGTH, + DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_DID_SERVICES_COUNT, MAX_ENDPOINT_URLS_COUNT, + MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_SERVICE_TYPE_COUNT, + MAX_SERVICE_TYPE_LENGTH, MAX_SERVICE_URL_COUNT, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, + MAX_URL_LENGTH, }, governance::{ COOLOFF_PERIOD, COUNCIL_MOTION_DURATION, ENACTMENT_PERIOD, FAST_TRACK_VOTING_PERIOD, LAUNCH_PERIOD, @@ -607,6 +609,12 @@ parameter_types! { pub const MaxBlocksTxValidity: BlockNumber = MAX_BLOCKS_TX_VALIDITY; pub const DidDeposit: Balance = DID_DEPOSIT; pub const DidFee: Balance = DID_FEE; + pub const MaxDidServicesCount: u32 = MAX_DID_SERVICES_COUNT; + pub const MaxServiceIdLength: u32 = MAX_SERVICE_ID_LENGTH; + pub const MaxServiceTypeLength: u32 = MAX_SERVICE_TYPE_LENGTH; + pub const MaxServiceUrlLength: u32 = MAX_SERVICE_URL_LENGTH; + pub const MaxTypeCountPerService: u32 = MAX_SERVICE_TYPE_COUNT; + pub const MaxUrlCountPerService: u32 = MAX_SERVICE_URL_COUNT; } impl did::Config for Runtime { @@ -633,6 +641,12 @@ impl did::Config for Runtime { type MaxTotalKeyAgreementKeys = MaxTotalKeyAgreementKeys; type MaxPublicKeysPerDid = MaxPublicKeysPerDid; type MaxBlocksTxValidity = MaxBlocksTxValidity; + type MaxDidServicesCount = MaxDidServicesCount; + type MaxServiceIdLength = MaxServiceIdLength; + type MaxServiceTypeLength = MaxServiceTypeLength; + type MaxServiceUrlLength = MaxServiceUrlLength; + type MaxTypeCountPerService = MaxTypeCountPerService; + type MaxUrlCountPerService = MaxUrlCountPerService; type WeightInfo = weights::did::WeightInfo; } diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index a7f4588c4..a7c810fb2 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -61,8 +61,10 @@ use kilt_primitives::{ MAX_SIGNATURE_BYTE_LENGTH, }, did::{ - DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_ENDPOINT_URLS_COUNT, MAX_KEY_AGREEMENT_KEYS, - MAX_PUBLIC_KEYS_PER_DID, MAX_TOTAL_KEY_AGREEMENT_KEYS, MAX_URL_LENGTH, + DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_DID_SERVICES_COUNT, MAX_ENDPOINT_URLS_COUNT, + MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_SERVICE_TYPE_COUNT, + MAX_SERVICE_TYPE_LENGTH, MAX_SERVICE_URL_COUNT, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, + MAX_URL_LENGTH, }, governance::{ COOLOFF_PERIOD, COUNCIL_MOTION_DURATION, ENACTMENT_PERIOD, FAST_TRACK_VOTING_PERIOD, LAUNCH_PERIOD, @@ -622,6 +624,12 @@ parameter_types! { pub const MaxBlocksTxValidity: BlockNumber = MAX_BLOCKS_TX_VALIDITY; pub const DidDeposit: Balance = DID_DEPOSIT; pub const DidFee: Balance = DID_FEE; + pub const MaxDidServicesCount: u32 = MAX_DID_SERVICES_COUNT; + pub const MaxServiceIdLength: u32 = MAX_SERVICE_ID_LENGTH; + pub const MaxServiceTypeLength: u32 = MAX_SERVICE_TYPE_LENGTH; + pub const MaxServiceUrlLength: u32 = MAX_SERVICE_URL_LENGTH; + pub const MaxTypeCountPerService: u32 = MAX_SERVICE_TYPE_COUNT; + pub const MaxUrlCountPerService: u32 = MAX_SERVICE_URL_COUNT; } impl did::Config for Runtime { @@ -648,6 +656,12 @@ impl did::Config for Runtime { type MaxTotalKeyAgreementKeys = MaxTotalKeyAgreementKeys; type MaxPublicKeysPerDid = MaxPublicKeysPerDid; type MaxBlocksTxValidity = MaxBlocksTxValidity; + type MaxDidServicesCount = MaxDidServicesCount; + type MaxServiceIdLength = MaxServiceIdLength; + type MaxServiceTypeLength = MaxServiceTypeLength; + type MaxServiceUrlLength = MaxServiceUrlLength; + type MaxTypeCountPerService = MaxTypeCountPerService; + type MaxUrlCountPerService = MaxUrlCountPerService; type WeightInfo = weights::did::WeightInfo; } diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index a2d1d2be1..83fb57d31 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -38,8 +38,10 @@ use kilt_primitives::{ MAX_SIGNATURE_BYTE_LENGTH, }, did::{ - DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_ENDPOINT_URLS_COUNT, MAX_KEY_AGREEMENT_KEYS, - MAX_PUBLIC_KEYS_PER_DID, MAX_TOTAL_KEY_AGREEMENT_KEYS, MAX_URL_LENGTH, + DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_DID_SERVICES_COUNT, MAX_ENDPOINT_URLS_COUNT, + MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_SERVICE_TYPE_COUNT, + MAX_SERVICE_TYPE_LENGTH, MAX_SERVICE_URL_COUNT, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, + MAX_URL_LENGTH, }, KILT, MILLI_KILT, MIN_VESTED_TRANSFER_AMOUNT, SLOT_DURATION, }, @@ -397,6 +399,12 @@ parameter_types! { pub const MaxBlocksTxValidity: BlockNumber = MAX_BLOCKS_TX_VALIDITY * 2; pub const DidDeposit: Balance = DID_DEPOSIT; pub const DidFee: Balance = DID_FEE; + pub const MaxDidServicesCount: u32 = MAX_DID_SERVICES_COUNT; + pub const MaxServiceIdLength: u32 = MAX_SERVICE_ID_LENGTH; + pub const MaxServiceTypeLength: u32 = MAX_SERVICE_TYPE_LENGTH; + pub const MaxServiceUrlLength: u32 = MAX_SERVICE_URL_LENGTH; + pub const MaxTypeCountPerService: u32 = MAX_SERVICE_TYPE_COUNT; + pub const MaxUrlCountPerService: u32 = MAX_SERVICE_URL_COUNT; } impl did::Config for Runtime { @@ -423,6 +431,12 @@ impl did::Config for Runtime { type MaxTotalKeyAgreementKeys = MaxTotalKeyAgreementKeys; type MaxPublicKeysPerDid = MaxPublicKeysPerDid; type MaxBlocksTxValidity = MaxBlocksTxValidity; + type MaxDidServicesCount = MaxDidServicesCount; + type MaxServiceIdLength = MaxServiceIdLength; + type MaxServiceTypeLength = MaxServiceTypeLength; + type MaxServiceUrlLength = MaxServiceUrlLength; + type MaxTypeCountPerService = MaxTypeCountPerService; + type MaxUrlCountPerService = MaxUrlCountPerService; type WeightInfo = (); } From 7099f469288b6f5cd658cc7207798b9f86d9bcd6 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 15:36:53 +0200 Subject: [PATCH 14/45] chore: build warning in delegation pallet --- pallets/delegation/src/lib.rs | 1 - pallets/delegation/src/mock.rs | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/delegation/src/lib.rs b/pallets/delegation/src/lib.rs index 7f246be71..f0437d2d3 100644 --- a/pallets/delegation/src/lib.rs +++ b/pallets/delegation/src/lib.rs @@ -96,7 +96,6 @@ use frame_support::{ pallet_prelude::Weight, traits::{Get, ReservableCurrency}, }; -use kilt_support::deposit::Deposit; use sp_runtime::{ traits::{Hash, Zero}, DispatchError, diff --git a/pallets/delegation/src/mock.rs b/pallets/delegation/src/mock.rs index e0cf39697..9dd93cafb 100644 --- a/pallets/delegation/src/mock.rs +++ b/pallets/delegation/src/mock.rs @@ -25,6 +25,7 @@ use frame_support::{ }; use frame_system::EnsureSigned; use kilt_primitives::constants::delegation::DELEGATION_DEPOSIT; +use kilt_support::deposit::Deposit; use sp_core::{ed25519, sr25519, Pair, H256}; use sp_keystore::{testing::KeyStore, KeystoreExt}; use sp_runtime::{ From 162d0eb8023c2dafbbb90ab379bcf3a8eebaee55 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 16:27:32 +0200 Subject: [PATCH 15/45] wip: benchmark weights --- pallets/did/src/default_weights.rs | 224 +++++++++++++++++------------ 1 file changed, 130 insertions(+), 94 deletions(-) diff --git a/pallets/did/src/default_weights.rs b/pallets/did/src/default_weights.rs index 901f3bf44..cb1f6016c 100644 --- a/pallets/did/src/default_weights.rs +++ b/pallets/did/src/default_weights.rs @@ -19,11 +19,11 @@ //! Autogenerated weights for did //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-15, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ +//! DATE: 2021-10-21, 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: -// ./target/release/kilt-parachain +// target/release/kilt-parachain // benchmark // --chain=dev // --steps=50 @@ -33,7 +33,7 @@ // --execution=wasm // --wasm-execution=compiled // --heap-pages=4096 -// --output=./pallets/did/src/default_weights.rs +// --output=pallets/did/src/default_weights.rs // --template=.maintain/weight-template.hbs @@ -46,9 +46,9 @@ use sp_std::marker::PhantomData; /// Weight functions needed for did. pub trait WeightInfo { - fn create_ed25519_keys(n: u32, ) -> Weight; - fn create_sr25519_keys(n: u32, ) -> Weight; - fn create_ecdsa_keys(n: u32, ) -> Weight; + fn create_ed25519_keys(n: u32, c: u32, ) -> Weight; + fn create_sr25519_keys(n: u32, c: u32, ) -> Weight; + fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight; fn delete() -> Weight; fn reclaim_deposit() -> Weight; fn submit_did_call_ed25519_key() -> Weight; @@ -75,6 +75,8 @@ pub trait WeightInfo { fn remove_ed25519_key_agreement_key() -> Weight; fn remove_sr25519_key_agreement_key() -> Weight; fn remove_ecdsa_key_agreement_key() -> Weight; + fn add_service_endpoint() -> Weight; + fn remove_service_endpoint() -> Weight; fn signature_verification_sr25519(l: u32, ) -> Weight; fn signature_verification_ed25519(l: u32, ) -> Weight; fn signature_verification_ecdsa(l: u32, ) -> Weight; @@ -83,171 +85,188 @@ pub trait WeightInfo { /// Weights for did using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { - fn create_ed25519_keys(n: u32, ) -> Weight { - (151_504_000_u64) - // Standard Error: 22_000 - .saturating_add((2_282_000_u64).saturating_mul(n as Weight)) + fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { + (108_176_000_u64) + // Standard Error: 241_000 + .saturating_add((4_481_000_u64).saturating_mul(n as Weight)) + // Standard Error: 75_000 + .saturating_add((9_351_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(n: u32, ) -> Weight { - (154_472_000_u64) - // Standard Error: 18_000 - .saturating_add((2_320_000_u64).saturating_mul(n as Weight)) + fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { + (171_118_000_u64) + // Standard Error: 76_000 + .saturating_add((9_207_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) - } - fn create_ecdsa_keys(n: u32, ) -> Weight { - (269_362_000_u64) - // Standard Error: 26_000 - .saturating_add((2_226_000_u64).saturating_mul(n as Weight)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) + } + fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { + (223_404_000_u64) + // Standard Error: 186_000 + .saturating_add((3_119_000_u64).saturating_mul(n as Weight)) + // Standard Error: 58_000 + .saturating_add((9_215_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (27_656_000_u64) + (48_642_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes(27_u64)) } fn reclaim_deposit() -> Weight { - (32_263_000_u64) + (53_300_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes(27_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (89_857_000_u64) + (73_979_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (90_367_000_u64) + (76_314_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (205_096_000_u64) + (177_954_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (50_844_000_u64) + (41_358_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (50_715_000_u64) + (41_848_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (50_917_000_u64) + (41_578_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (50_197_000_u64) + (41_107_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (50_650_000_u64) + (41_538_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (50_443_000_u64) + (41_958_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (46_539_000_u64) + (38_693_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (46_718_000_u64) + (38_402_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (46_571_000_u64) + (39_134_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (50_380_000_u64) + (41_197_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (50_575_000_u64) + (40_797_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (50_328_000_u64) + (42_069_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (46_490_000_u64) + (37_862_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (46_537_000_u64) + (38_462_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (46_435_000_u64) + (38_582_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (49_541_000_u64) + (40_215_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (49_659_000_u64) + (40_526_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (49_724_000_u64) + (40_857_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (46_880_000_u64) + (38_282_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (46_807_000_u64) + (39_494_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (46_705_000_u64) + (38_462_000_u64) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn add_service_endpoint() -> Weight { + (226_335_000_u64) + .saturating_add(T::DbWeight::get().reads(27_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn remove_service_endpoint() -> Weight { + (10_841_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (28_816_000_u64) + (0_u64) // Standard Error: 0 .saturating_add((4_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (27_142_000_u64) + (331_203_000_u64) // Standard Error: 0 .saturating_add((2_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (143_764_000_u64) + (142_753_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) @@ -256,171 +275,188 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { - fn create_ed25519_keys(n: u32, ) -> Weight { - (151_504_000_u64) - // Standard Error: 22_000 - .saturating_add((2_282_000_u64).saturating_mul(n as Weight)) + fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { + (108_176_000_u64) + // Standard Error: 241_000 + .saturating_add((4_481_000_u64).saturating_mul(n as Weight)) + // Standard Error: 75_000 + .saturating_add((9_351_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(n: u32, ) -> Weight { - (154_472_000_u64) - // Standard Error: 18_000 - .saturating_add((2_320_000_u64).saturating_mul(n as Weight)) + fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { + (171_118_000_u64) + // Standard Error: 76_000 + .saturating_add((9_207_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) - } - fn create_ecdsa_keys(n: u32, ) -> Weight { - (269_362_000_u64) - // Standard Error: 26_000 - .saturating_add((2_226_000_u64).saturating_mul(n as Weight)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) + } + fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { + (223_404_000_u64) + // Standard Error: 186_000 + .saturating_add((3_119_000_u64).saturating_mul(n as Weight)) + // Standard Error: 58_000 + .saturating_add((9_215_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (27_656_000_u64) + (48_642_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) - .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes(27_u64)) } fn reclaim_deposit() -> Weight { - (32_263_000_u64) + (53_300_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) - .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes(27_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (89_857_000_u64) + (73_979_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (90_367_000_u64) + (76_314_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (205_096_000_u64) + (177_954_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (50_844_000_u64) + (41_358_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (50_715_000_u64) + (41_848_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (50_917_000_u64) + (41_578_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (50_197_000_u64) + (41_107_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (50_650_000_u64) + (41_538_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (50_443_000_u64) + (41_958_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (46_539_000_u64) + (38_693_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (46_718_000_u64) + (38_402_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (46_571_000_u64) + (39_134_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (50_380_000_u64) + (41_197_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (50_575_000_u64) + (40_797_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (50_328_000_u64) + (42_069_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (46_490_000_u64) + (37_862_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (46_537_000_u64) + (38_462_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (46_435_000_u64) + (38_582_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (49_541_000_u64) + (40_215_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (49_659_000_u64) + (40_526_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (49_724_000_u64) + (40_857_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (46_880_000_u64) + (38_282_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (46_807_000_u64) + (39_494_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (46_705_000_u64) + (38_462_000_u64) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + fn add_service_endpoint() -> Weight { + (226_335_000_u64) + .saturating_add(RocksDbWeight::get().reads(27_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + fn remove_service_endpoint() -> Weight { + (10_841_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (28_816_000_u64) + (0_u64) // Standard Error: 0 .saturating_add((4_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (27_142_000_u64) + (331_203_000_u64) // Standard Error: 0 .saturating_add((2_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (143_764_000_u64) + (142_753_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) From 851fa90c0ab3ee676b67dc9821c06f85b3e23040 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 21 Oct 2021 16:31:07 +0200 Subject: [PATCH 16/45] wip: using the new weights --- pallets/did/src/lib.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 32be0f1c4..9fd8854f8 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -476,7 +476,26 @@ pub mod pallet { /// - Reads: [Origin Account], Did /// - Writes: Did (with K new key agreement keys) /// # - #[pallet::weight(1)] + #[pallet::weight({ + let new_key_agreement_keys = details.new_key_agreement_keys.len().saturated_into::(); + // We only consider the number of new endpoints. + let new_services_count = details.new_service_details.len(); + + let ed25519_weight = ::WeightInfo::create_ed25519_keys( + new_key_agreement_keys, + new_services_count + ); + let sr25519_weight = ::WeightInfo::create_sr25519_keys( + new_key_agreement_keys, + new_services_count + ); + let ecdsa_weight = ::WeightInfo::create_ecdsa_keys( + new_key_agreement_keys, + new_services_count + ); + + ed25519_weight.max(sr25519_weight).max(ecdsa_weight) + })] pub fn create(origin: OriginFor, details: DidCreationDetails, signature: DidSignature) -> DispatchResult { let sender = ensure_signed(origin)?; From 260939244d4b7784d730b058484c030c274e36d2 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 08:28:01 +0200 Subject: [PATCH 17/45] chore: rename DID pallet configuration parameters --- pallets/did/src/benchmarking.rs | 46 ++++++++-------- pallets/did/src/lib.rs | 56 +++++++++---------- pallets/did/src/mock.rs | 12 ++-- pallets/did/src/service_endpoints.rs | 10 ++-- pallets/did/src/tests.rs | 82 ++++++++++++++-------------- primitives/src/constants.rs | 6 +- runtimes/peregrine/src/lib.rs | 16 +++--- runtimes/standalone/src/lib.rs | 18 +++--- 8 files changed, 124 insertions(+), 122 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index eaecd9e3f..969beca43 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -114,7 +114,7 @@ benchmarks! { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); // We only calculate weights based on how many endpoints are specified. For each endpoint, we use the max possible length and count for its components. // This makes weight computation easier at runtime, at the cost of always having worst-case weights for any # of endpoints c. - let c in 1 .. T::MaxDidServicesCount::get(); + let c in 1 .. T::MaxNumberOfServicesPerDid::get(); let submitter: AccountIdOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); @@ -128,9 +128,9 @@ benchmarks! { let service_endpoints = get_service_endpoints::( c, T::MaxServiceIdLength::get(), - T::MaxTypeCountPerService::get(), + T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), - T::MaxUrlCountPerService::get(), + T::MaxNumberOfUrlsPerService::get(), T::MaxServiceUrlLength::get(), ); @@ -175,7 +175,7 @@ benchmarks! { create_sr25519_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); - let c in 1 .. T::MaxDidServicesCount::get(); + let c in 1 .. T::MaxNumberOfServicesPerDid::get(); let submitter: AccountIdOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); make_free_for_did::(&submitter); @@ -188,9 +188,9 @@ benchmarks! { let service_endpoints = get_service_endpoints::( c, T::MaxServiceIdLength::get(), - T::MaxTypeCountPerService::get(), + T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), - T::MaxUrlCountPerService::get(), + T::MaxNumberOfUrlsPerService::get(), T::MaxServiceUrlLength::get(), ); @@ -235,7 +235,7 @@ benchmarks! { create_ecdsa_keys { let n in 1 .. T::MaxNewKeyAgreementKeys::get(); - let c in 1 .. T::MaxDidServicesCount::get(); + let c in 1 .. T::MaxNumberOfServicesPerDid::get(); let submitter: AccountIdOf = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED); make_free_for_did::(&submitter); @@ -248,9 +248,9 @@ benchmarks! { let service_endpoints = get_service_endpoints::( c, T::MaxServiceIdLength::get(), - T::MaxTypeCountPerService::get(), + T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), - T::MaxUrlCountPerService::get(), + T::MaxNumberOfUrlsPerService::get(), T::MaxServiceUrlLength::get(), ); @@ -299,11 +299,11 @@ benchmarks! { let did_details = generate_base_did_details::(DidVerificationKey::from(did_public_auth_key)); let service_endpoints = get_service_endpoints::( - T::MaxDidServicesCount::get(), + T::MaxNumberOfServicesPerDid::get(), T::MaxServiceIdLength::get(), - T::MaxTypeCountPerService::get(), + T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), - T::MaxUrlCountPerService::get(), + T::MaxNumberOfUrlsPerService::get(), T::MaxServiceUrlLength::get(), ); @@ -328,11 +328,11 @@ benchmarks! { let did_details = generate_base_did_details::(DidVerificationKey::from(did_public_auth_key)); let service_endpoints = get_service_endpoints::( - T::MaxDidServicesCount::get(), + T::MaxNumberOfServicesPerDid::get(), T::MaxServiceIdLength::get(), - T::MaxTypeCountPerService::get(), + T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), - T::MaxUrlCountPerService::get(), + T::MaxNumberOfUrlsPerService::get(), T::MaxServiceUrlLength::get(), ); @@ -846,11 +846,11 @@ benchmarks! { let public_auth_key = get_ecdsa_public_authentication_key(); let did_subject: DidIdentifierOf = MultiSigner::from(public_auth_key.clone()).into_account().into(); let old_service_endpoints = get_service_endpoints::( - T::MaxDidServicesCount::get() - 1, + T::MaxNumberOfServicesPerDid::get() - 1, T::MaxServiceIdLength::get(), - T::MaxTypeCountPerService::get(), + T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), - T::MaxUrlCountPerService::get(), + T::MaxNumberOfUrlsPerService::get(), T::MaxServiceUrlLength::get(), ); let new_service_endpoint = DidEndpointDetails { @@ -872,7 +872,7 @@ benchmarks! { ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count().saturated_into::(), - T::MaxDidServicesCount::get() + T::MaxNumberOfServicesPerDid::get() ); } @@ -880,11 +880,11 @@ benchmarks! { let public_auth_key = get_ecdsa_public_authentication_key(); let did_subject: DidIdentifierOf = MultiSigner::from(public_auth_key.clone()).into_account().into(); let old_service_endpoints = get_service_endpoints::( - T::MaxDidServicesCount::get(), + T::MaxNumberOfServicesPerDid::get(), T::MaxServiceIdLength::get(), - T::MaxTypeCountPerService::get(), + T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), - T::MaxUrlCountPerService::get(), + T::MaxNumberOfUrlsPerService::get(), T::MaxServiceUrlLength::get(), ); let endpoint_id = old_service_endpoints[0].id.clone(); @@ -901,7 +901,7 @@ benchmarks! { ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count().saturated_into::(), - T::MaxDidServicesCount::get() - 1 + T::MaxNumberOfServicesPerDid::get() - 1 ); } diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 9fd8854f8..a8e3dacbc 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -254,22 +254,22 @@ pub mod pallet { type WeightInfo: WeightInfo; #[pallet::constant] - type MaxServiceIdLength: Get; + type MaxNumberOfServicesPerDid: Get; #[pallet::constant] - type MaxServiceTypeLength: Get; + type MaxServiceIdLength: Get; #[pallet::constant] - type MaxServiceUrlLength: Get; + type MaxServiceTypeLength: Get; #[pallet::constant] - type MaxTypeCountPerService: Get; + type MaxNumberOfTypesPerService: Get; #[pallet::constant] - type MaxUrlCountPerService: Get; + type MaxServiceUrlLength: Get; #[pallet::constant] - type MaxDidServicesCount: Get; + type MaxNumberOfUrlsPerService: Get; } #[pallet::pallet] @@ -383,15 +383,15 @@ pub mod pallet { NotOwnerOfDeposit, /// The origin is unable to reserve the deposit and pay the fee. UnableToPayFees, - MaxIdLengthExceeded, - MaxUrlLengthExceeded, - MaxTypeLengthExceeded, - MaxUrlCountExceeded, - MaxTypeCountExceeded, - MaxServicesCountExceeded, + MaxNumberOfServicesPerDidExceeded, + MaxServiceIdLengthExceeded, + MaxServiceTypeLengthExceeded, + MaxNumberOfTypesPerServiceExceeded, + MaxServiceUrlLengthExceeded, + MaxNumberOfUrlsPerServiceExceeded, ServiceAlreadyPresent, ServiceNotPresent, - InvalidUrlEncoding, + InvalidServiceEncoding, /// An error that is not supposed to take place, yet it happened. InternalError, } @@ -435,13 +435,13 @@ pub mod pallet { fn from(error: InputError) -> Self { match error { InputError::MaxKeyAgreementKeysLimitExceeded => Self::MaxKeyAgreementKeysLimitExceeded, - InputError::MaxIdLengthExceeded => Self::MaxIdLengthExceeded, - InputError::MaxServicesCountExceeded => Self::MaxServicesCountExceeded, - InputError::MaxTypeCountExceeded => Self::MaxTypeCountExceeded, - InputError::MaxTypeLengthExceeded => Self::MaxTypeLengthExceeded, - InputError::MaxUrlCountExceeded => Self::MaxUrlCountExceeded, - InputError::MaxUrlLengthExceeded => Self::MaxUrlLengthExceeded, - InputError::InvalidUrlEncoding => Self::InvalidUrlEncoding, + InputError::MaxIdLengthExceeded => Self::MaxServiceIdLengthExceeded, + InputError::MaxServicesCountExceeded => Self::MaxNumberOfServicesPerDidExceeded, + InputError::MaxTypeCountExceeded => Self::MaxNumberOfTypesPerServiceExceeded, + InputError::MaxTypeLengthExceeded => Self::MaxServiceTypeLengthExceeded, + InputError::MaxUrlCountExceeded => Self::MaxNumberOfUrlsPerServiceExceeded, + InputError::MaxUrlLengthExceeded => Self::MaxServiceUrlLengthExceeded, + InputError::InvalidUrlEncoding => Self::InvalidServiceEncoding, } } } @@ -479,19 +479,19 @@ pub mod pallet { #[pallet::weight({ let new_key_agreement_keys = details.new_key_agreement_keys.len().saturated_into::(); // We only consider the number of new endpoints. - let new_services_count = details.new_service_details.len(); + let new_services_count = details.new_service_details.len().saturated_into::(); let ed25519_weight = ::WeightInfo::create_ed25519_keys( new_key_agreement_keys, - new_services_count + new_services_count, ); let sr25519_weight = ::WeightInfo::create_sr25519_keys( new_key_agreement_keys, - new_services_count + new_services_count, ); let ecdsa_weight = ::WeightInfo::create_ecdsa_keys( new_key_agreement_keys, - new_services_count + new_services_count, ); ed25519_weight.max(sr25519_weight).max(ecdsa_weight) @@ -797,8 +797,8 @@ pub mod pallet { // Verify that there are less than the maximum limit of services stored. ensure!( ServiceEndpoints::::iter_prefix(&did_subject).count() - < T::MaxDidServicesCount::get().saturated_into(), - Error::::MaxServicesCountExceeded + < T::MaxNumberOfServicesPerDid::get().saturated_into(), + Error::::MaxNumberOfServicesPerDidExceeded ); // *** No Fail beyond this point *** @@ -1085,10 +1085,10 @@ impl Pallet { // *** No Fail beyond this point *** let storage_kill_result = - ServiceEndpoints::::remove_prefix(&did_subject, Some(T::MaxDidServicesCount::get())); + ServiceEndpoints::::remove_prefix(&did_subject, Some(T::MaxNumberOfServicesPerDid::get())); // If some items are remaining, it means that there were more than - // MaxDidServicesCount stored, and that should never happen. + // MaxNumberOfServicesPerDid stored, and that should never happen. if let KillStorageResult::SomeRemaining(_) = storage_kill_result { return Err(Error::::InternalError.into()); }; diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index 1fda491f8..d8c038e6c 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -105,12 +105,12 @@ parameter_types! { pub const MaxBlocksTxValidity: u64 = 300u64; pub const Deposit: Balance = 10 * MICRO_KILT; pub const DidFee: Balance = MICRO_KILT; - pub const MaxDidServicesCount: u32 = 25u32; + pub const MaxNumberOfServicesPerDid: u32 = 25u32; pub const MaxServiceIdLength: u32 = 50u32; pub const MaxServiceTypeLength: u32 = 50u32; pub const MaxServiceUrlLength: u32 = 100u32; - pub const MaxTypeCountPerService: u32 = 1u32; - pub const MaxUrlCountPerService: u32 = 1u32; + pub const MaxNumberOfTypesPerService: u32 = 1u32; + pub const MaxNumberOfUrlsPerService: u32 = 1u32; } pub struct ToAccount(sp_std::marker::PhantomData); @@ -141,12 +141,12 @@ impl Config for Test { type MaxPublicKeysPerDid = MaxPublicKeysPerDid; type MaxBlocksTxValidity = MaxBlocksTxValidity; type WeightInfo = (); - type MaxDidServicesCount = MaxDidServicesCount; + type MaxNumberOfServicesPerDid = MaxNumberOfServicesPerDid; type MaxServiceIdLength = MaxServiceIdLength; type MaxServiceTypeLength = MaxServiceTypeLength; type MaxServiceUrlLength = MaxServiceUrlLength; - type MaxTypeCountPerService = MaxTypeCountPerService; - type MaxUrlCountPerService = MaxUrlCountPerService; + type MaxNumberOfTypesPerService = MaxNumberOfTypesPerService; + type MaxNumberOfUrlsPerService = MaxNumberOfUrlsPerService; } parameter_types! { diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 7bb665047..274ebe125 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -28,10 +28,10 @@ use crate::utils as crate_utils; pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; pub type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; -pub type ServiceEndpointTypeEntries = BoundedVec, ::MaxTypeCountPerService>; +pub type ServiceEndpointTypeEntries = BoundedVec, ::MaxNumberOfTypesPerService>; pub type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; -pub type ServiceEndpointUrlEntries = BoundedVec, ::MaxUrlCountPerService>; +pub type ServiceEndpointUrlEntries = BoundedVec, ::MaxNumberOfUrlsPerService>; #[derive(Clone, Decode, Encode, PartialEq, Eq)] pub struct DidEndpointDetails { @@ -86,7 +86,7 @@ pub mod utils { ) -> Result<(), InputError> { // Check if the maximum number of endpoints is provided ensure!( - endpoints.len() <= T::MaxDidServicesCount::get().saturated_into(), + endpoints.len() <= T::MaxNumberOfServicesPerDid::get().saturated_into(), InputError::MaxServicesCountExceeded ); @@ -103,12 +103,12 @@ pub mod utils { ) -> Result<(), InputError> { // Check that the maximum number of service types is provided. ensure!( - endpoint.service_type.len() <= T::MaxTypeCountPerService::get().saturated_into(), + endpoint.service_type.len() <= T::MaxNumberOfTypesPerService::get().saturated_into(), InputError::MaxTypeCountExceeded ); // Check that the maximum number of URLs is provided. ensure!( - endpoint.url.len() <= T::MaxUrlCountPerService::get().saturated_into(), + endpoint.url.len() <= T::MaxNumberOfUrlsPerService::get().saturated_into(), InputError::MaxUrlCountExceeded ); // Check that the ID is the maximum allowed length and only contain ASCII diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 6f1550c07..54187887f 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -176,11 +176,11 @@ fn check_successful_complete_creation() { details.new_attestation_key = Some(did::DidVerificationKey::from(att_key.public())); details.new_delegation_key = Some(did::DidVerificationKey::from(del_key.public())); details.new_service_details = get_service_endpoints( - ::MaxDidServicesCount::get(), + ::MaxNumberOfServicesPerDid::get(), ::MaxServiceIdLength::get(), - ::MaxTypeCountPerService::get(), + ::MaxNumberOfTypesPerService::get(), ::MaxServiceTypeLength::get(), - ::MaxUrlCountPerService::get(), + ::MaxNumberOfUrlsPerService::get(), ::MaxServiceUrlLength::get(), ); let signature = auth_key.sign(details.encode().as_ref()); @@ -453,7 +453,7 @@ fn check_max_limit_service_endpoints_count_did_creation() { let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = - get_service_endpoints(::MaxDidServicesCount::get() + 1, 1, 1, 1, 1, 1); + get_service_endpoints(::MaxNumberOfServicesPerDid::get() + 1, 1, 1, 1, 1, 1); let signature = auth_key.sign(details.encode().as_ref()); @@ -466,7 +466,7 @@ fn check_max_limit_service_endpoints_count_did_creation() { .execute_with(|| { assert_noop!( Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), - did::Error::::MaxServicesCountExceeded + did::Error::::MaxNumberOfServicesPerDidExceeded ); }); } @@ -491,7 +491,7 @@ fn check_max_limit_service_id_length_did_creation() { .execute_with(|| { assert_noop!( Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), - did::Error::::MaxIdLengthExceeded + did::Error::::MaxServiceIdLengthExceeded ); }); } @@ -503,7 +503,7 @@ fn check_max_limit_service_type_count_did_creation() { let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = - get_service_endpoints(1, 1, ::MaxTypeCountPerService::get() + 1, 1, 1, 1); + get_service_endpoints(1, 1, ::MaxNumberOfTypesPerService::get() + 1, 1, 1, 1); let signature = auth_key.sign(details.encode().as_ref()); @@ -516,7 +516,7 @@ fn check_max_limit_service_type_count_did_creation() { .execute_with(|| { assert_noop!( Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), - did::Error::::MaxTypeCountExceeded + did::Error::::MaxNumberOfTypesPerServiceExceeded ); }); } @@ -541,7 +541,7 @@ fn check_max_limit_service_type_length_did_creation() { .execute_with(|| { assert_noop!( Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), - did::Error::::MaxTypeLengthExceeded + did::Error::::MaxServiceTypeLengthExceeded ); }); } @@ -553,7 +553,7 @@ fn check_max_limit_service_url_count_did_creation() { let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = - get_service_endpoints(1, 1, 1, 1, ::MaxUrlCountPerService::get() + 1, 1); + get_service_endpoints(1, 1, 1, 1, ::MaxNumberOfUrlsPerService::get() + 1, 1); let signature = auth_key.sign(details.encode().as_ref()); @@ -566,7 +566,7 @@ fn check_max_limit_service_url_count_did_creation() { .execute_with(|| { assert_noop!( Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), - did::Error::::MaxUrlCountExceeded + did::Error::::MaxNumberOfUrlsPerServiceExceeded ); }); } @@ -591,7 +591,7 @@ fn check_max_limit_service_url_length_did_creation() { .execute_with(|| { assert_noop!( Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), - did::Error::::MaxUrlLengthExceeded + did::Error::::MaxServiceUrlLengthExceeded ); }); } @@ -616,7 +616,7 @@ fn check_invalid_service_id_character_did_creation() { .execute_with(|| { assert_noop!( Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), - did::Error::::InvalidUrlEncoding + did::Error::::InvalidServiceEncoding ); }); } @@ -641,7 +641,7 @@ fn check_invalid_service_type_character_did_creation() { .execute_with(|| { assert_noop!( Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), - did::Error::::InvalidUrlEncoding + did::Error::::InvalidServiceEncoding ); }); } @@ -666,7 +666,7 @@ fn check_invalid_service_url_character_did_creation() { .execute_with(|| { assert_noop!( Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), - did::Error::::InvalidUrlEncoding + did::Error::::InvalidServiceEncoding ); }); } @@ -1630,11 +1630,11 @@ fn check_service_addition_one_from_full_successful() { let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); let old_service_endpoints = get_service_endpoints( // -1 from the max number - ::MaxDidServicesCount::get() - 1, + ::MaxNumberOfServicesPerDid::get() - 1, ::MaxServiceIdLength::get(), - ::MaxTypeCountPerService::get(), + ::MaxNumberOfTypesPerService::get(), ::MaxServiceTypeLength::get(), - ::MaxUrlCountPerService::get(), + ::MaxNumberOfUrlsPerService::get(), ::MaxServiceUrlLength::get(), ); let new_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); @@ -1654,7 +1654,7 @@ fn check_service_addition_one_from_full_successful() { did::pallet::ServiceEndpoints::::iter_prefix(&alice_did) .count() .saturated_into::(), - ::MaxDidServicesCount::get() + ::MaxNumberOfServicesPerDid::get() ); let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id) .expect("Service endpoint should be stored."); @@ -1701,11 +1701,11 @@ fn check_max_services_count_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); let old_service_endpoints = get_service_endpoints( - ::MaxDidServicesCount::get(), + ::MaxNumberOfServicesPerDid::get(), ::MaxServiceIdLength::get(), - ::MaxTypeCountPerService::get(), + ::MaxNumberOfTypesPerService::get(), ::MaxServiceTypeLength::get(), - ::MaxUrlCountPerService::get(), + ::MaxNumberOfUrlsPerService::get(), ::MaxServiceUrlLength::get(), ); let new_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); @@ -1719,7 +1719,7 @@ fn check_max_services_count_addition_error() { .execute_with(|| { assert_noop!( Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), - did::Error::::MaxServicesCountExceeded + did::Error::::MaxNumberOfServicesPerDidExceeded ); }); } @@ -1732,9 +1732,9 @@ fn check_max_service_id_length_addition_error() { let new_service_endpoint = get_service_endpoints( 1, ::MaxServiceIdLength::get() + 1, - ::MaxTypeCountPerService::get(), + ::MaxNumberOfTypesPerService::get(), ::MaxServiceTypeLength::get(), - ::MaxUrlCountPerService::get(), + ::MaxNumberOfUrlsPerService::get(), ::MaxServiceUrlLength::get(), )[0] .clone(); @@ -1747,7 +1747,7 @@ fn check_max_service_id_length_addition_error() { .execute_with(|| { assert_noop!( Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), - did::Error::::MaxIdLengthExceeded + did::Error::::MaxServiceIdLengthExceeded ); }); } @@ -1760,9 +1760,9 @@ fn check_max_service_type_length_addition_error() { let new_service_endpoint = get_service_endpoints( 1, ::MaxServiceIdLength::get(), - ::MaxTypeCountPerService::get(), + ::MaxNumberOfTypesPerService::get(), ::MaxServiceTypeLength::get() + 1, - ::MaxUrlCountPerService::get(), + ::MaxNumberOfUrlsPerService::get(), ::MaxServiceUrlLength::get(), )[0] .clone(); @@ -1775,7 +1775,7 @@ fn check_max_service_type_length_addition_error() { .execute_with(|| { assert_noop!( Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), - did::Error::::MaxTypeLengthExceeded + did::Error::::MaxServiceTypeLengthExceeded ); }); } @@ -1788,9 +1788,9 @@ fn check_max_service_type_count_addition_error() { let new_service_endpoint = get_service_endpoints( 1, ::MaxServiceIdLength::get(), - ::MaxTypeCountPerService::get() + 1, + ::MaxNumberOfTypesPerService::get() + 1, ::MaxServiceTypeLength::get(), - ::MaxUrlCountPerService::get(), + ::MaxNumberOfUrlsPerService::get(), ::MaxServiceUrlLength::get(), )[0] .clone(); @@ -1803,7 +1803,7 @@ fn check_max_service_type_count_addition_error() { .execute_with(|| { assert_noop!( Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), - did::Error::::MaxTypeCountExceeded + did::Error::::MaxNumberOfTypesPerServiceExceeded ); }); } @@ -1816,9 +1816,9 @@ fn check_max_service_url_length_addition_error() { let new_service_endpoint = get_service_endpoints( 1, ::MaxServiceIdLength::get(), - ::MaxTypeCountPerService::get(), + ::MaxNumberOfTypesPerService::get(), ::MaxServiceTypeLength::get(), - ::MaxUrlCountPerService::get(), + ::MaxNumberOfUrlsPerService::get(), ::MaxServiceUrlLength::get() + 1, )[0] .clone(); @@ -1831,7 +1831,7 @@ fn check_max_service_url_length_addition_error() { .execute_with(|| { assert_noop!( Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), - did::Error::::MaxUrlLengthExceeded + did::Error::::MaxServiceUrlLengthExceeded ); }); } @@ -1844,9 +1844,9 @@ fn check_max_service_url_count_addition_error() { let new_service_endpoint = get_service_endpoints( 1, ::MaxServiceIdLength::get(), - ::MaxTypeCountPerService::get(), + ::MaxNumberOfTypesPerService::get(), ::MaxServiceTypeLength::get(), - ::MaxUrlCountPerService::get() + 1, + ::MaxNumberOfUrlsPerService::get() + 1, ::MaxServiceUrlLength::get(), )[0] .clone(); @@ -1859,7 +1859,7 @@ fn check_max_service_url_count_addition_error() { .execute_with(|| { assert_noop!( Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_endpoint), - did::Error::::MaxUrlCountExceeded + did::Error::::MaxNumberOfUrlsPerServiceExceeded ); }); } @@ -1879,7 +1879,7 @@ fn check_invalid_service_id_character_addition_error() { .execute_with(|| { assert_noop!( Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_details), - did::Error::::InvalidUrlEncoding + did::Error::::InvalidServiceEncoding ); }); } @@ -1899,7 +1899,7 @@ fn check_invalid_service_type_character_addition_error() { .execute_with(|| { assert_noop!( Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_details), - did::Error::::InvalidUrlEncoding + did::Error::::InvalidServiceEncoding ); }); } @@ -1919,7 +1919,7 @@ fn check_invalid_service_url_character_addition_error() { .execute_with(|| { assert_noop!( Did::add_service_endpoint(Origin::signed(alice_did.clone()), new_service_details), - did::Error::::InvalidUrlEncoding + did::Error::::InvalidServiceEncoding ); }); } diff --git a/primitives/src/constants.rs b/primitives/src/constants.rs index 1b9903435..8624a9d3f 100644 --- a/primitives/src/constants.rs +++ b/primitives/src/constants.rs @@ -199,10 +199,10 @@ pub mod did { pub const MAX_ENDPOINT_URLS_COUNT: u32 = 3; pub const MAX_BLOCKS_TX_VALIDITY: BlockNumber = HOURS; + pub const MAX_NUMBER_OF_SERVICES_PER_DID: u32 = 25; pub const MAX_SERVICE_ID_LENGTH: u32 = 50; pub const MAX_SERVICE_TYPE_LENGTH: u32 = 50; + pub const MAX_NUMBER_OF_TYPES_PER_SERVICE: u32 = 1; pub const MAX_SERVICE_URL_LENGTH: u32 = 100; - pub const MAX_SERVICE_TYPE_COUNT: u32 = 1; - pub const MAX_SERVICE_URL_COUNT: u32 = 1; - pub const MAX_DID_SERVICES_COUNT: u32 = 25; + pub const MAX_NUMBER_OF_URLS_PER_SERVICE: u32 = 1; } diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index ebc6fe0ab..712a15a98 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -60,9 +60,9 @@ use kilt_primitives::{ MAX_SIGNATURE_BYTE_LENGTH, }, did::{ - DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_DID_SERVICES_COUNT, MAX_ENDPOINT_URLS_COUNT, - MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_SERVICE_TYPE_COUNT, - MAX_SERVICE_TYPE_LENGTH, MAX_SERVICE_URL_COUNT, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, + DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_NUMBER_OF_SERVICES_PER_DID, MAX_ENDPOINT_URLS_COUNT, + MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_NUMBER_OF_TYPES_PER_SERVICE, + MAX_SERVICE_TYPE_LENGTH, MAX_NUMBER_OF_URLS_PER_SERVICE, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, MAX_URL_LENGTH, }, governance::{ @@ -609,12 +609,12 @@ parameter_types! { pub const MaxBlocksTxValidity: BlockNumber = MAX_BLOCKS_TX_VALIDITY; pub const DidDeposit: Balance = DID_DEPOSIT; pub const DidFee: Balance = DID_FEE; - pub const MaxDidServicesCount: u32 = MAX_DID_SERVICES_COUNT; + pub const MaxNumberOfServicesPerDid: u32 = MAX_NUMBER_OF_SERVICES_PER_DID; pub const MaxServiceIdLength: u32 = MAX_SERVICE_ID_LENGTH; pub const MaxServiceTypeLength: u32 = MAX_SERVICE_TYPE_LENGTH; pub const MaxServiceUrlLength: u32 = MAX_SERVICE_URL_LENGTH; - pub const MaxTypeCountPerService: u32 = MAX_SERVICE_TYPE_COUNT; - pub const MaxUrlCountPerService: u32 = MAX_SERVICE_URL_COUNT; + pub const MaxNumberOfTypesPerService: u32 = MAX_NUMBER_OF_TYPES_PER_SERVICE; + pub const MaxNumberOfUrlsPerService: u32 = MAX_NUMBER_OF_URLS_PER_SERVICE; } impl did::Config for Runtime { @@ -641,10 +641,12 @@ impl did::Config for Runtime { type MaxTotalKeyAgreementKeys = MaxTotalKeyAgreementKeys; type MaxPublicKeysPerDid = MaxPublicKeysPerDid; type MaxBlocksTxValidity = MaxBlocksTxValidity; - type MaxDidServicesCount = MaxDidServicesCount; + type MaxNumberOfServicesPerDid = MaxNumberOfServicesPerDid; type MaxServiceIdLength = MaxServiceIdLength; type MaxServiceTypeLength = MaxServiceTypeLength; type MaxServiceUrlLength = MaxServiceUrlLength; + type MaxNumberOfTypesPerService = MaxNumberOfTypesPerService; + type MaxNumberOfUrlsPerService = MaxNumberOfUrlsPerService; type MaxTypeCountPerService = MaxTypeCountPerService; type MaxUrlCountPerService = MaxUrlCountPerService; type WeightInfo = weights::did::WeightInfo; diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index 83fb57d31..46ed64b68 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -38,9 +38,9 @@ use kilt_primitives::{ MAX_SIGNATURE_BYTE_LENGTH, }, did::{ - DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_DID_SERVICES_COUNT, MAX_ENDPOINT_URLS_COUNT, - MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_SERVICE_TYPE_COUNT, - MAX_SERVICE_TYPE_LENGTH, MAX_SERVICE_URL_COUNT, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, + DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_NUMBER_OF_SERVICES_PER_DID, MAX_ENDPOINT_URLS_COUNT, + MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_NUMBER_OF_TYPES_PER_SERVICE, + MAX_SERVICE_TYPE_LENGTH, MAX_NUMBER_OF_URLS_PER_SERVICE, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, MAX_URL_LENGTH, }, KILT, MILLI_KILT, MIN_VESTED_TRANSFER_AMOUNT, SLOT_DURATION, @@ -399,12 +399,12 @@ parameter_types! { pub const MaxBlocksTxValidity: BlockNumber = MAX_BLOCKS_TX_VALIDITY * 2; pub const DidDeposit: Balance = DID_DEPOSIT; pub const DidFee: Balance = DID_FEE; - pub const MaxDidServicesCount: u32 = MAX_DID_SERVICES_COUNT; + pub const MaxNumberOfServicesPerDid: u32 = MAX_NUMBER_OF_SERVICES_PER_DID; pub const MaxServiceIdLength: u32 = MAX_SERVICE_ID_LENGTH; pub const MaxServiceTypeLength: u32 = MAX_SERVICE_TYPE_LENGTH; pub const MaxServiceUrlLength: u32 = MAX_SERVICE_URL_LENGTH; - pub const MaxTypeCountPerService: u32 = MAX_SERVICE_TYPE_COUNT; - pub const MaxUrlCountPerService: u32 = MAX_SERVICE_URL_COUNT; + pub const MaxNumberOfTypesPerService: u32 = MAX_NUMBER_OF_TYPES_PER_SERVICE; + pub const MaxNumberOfUrlsPerService: u32 = MAX_NUMBER_OF_URLS_PER_SERVICE; } impl did::Config for Runtime { @@ -431,12 +431,12 @@ impl did::Config for Runtime { type MaxTotalKeyAgreementKeys = MaxTotalKeyAgreementKeys; type MaxPublicKeysPerDid = MaxPublicKeysPerDid; type MaxBlocksTxValidity = MaxBlocksTxValidity; - type MaxDidServicesCount = MaxDidServicesCount; + type MaxNumberOfServicesPerDid = MaxNumberOfServicesPerDid; type MaxServiceIdLength = MaxServiceIdLength; type MaxServiceTypeLength = MaxServiceTypeLength; type MaxServiceUrlLength = MaxServiceUrlLength; - type MaxTypeCountPerService = MaxTypeCountPerService; - type MaxUrlCountPerService = MaxUrlCountPerService; + type MaxNumberOfTypesPerService = MaxNumberOfTypesPerService; + type MaxNumberOfUrlsPerService = MaxNumberOfUrlsPerService; type WeightInfo = (); } From b3b2a5cfc1b4992f5d4db459edb50c8751bfd61a Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 08:28:19 +0200 Subject: [PATCH 18/45] bench: temporary remove benchmarks from runtime --- runtimes/peregrine/src/lib.rs | 4 +--- runtimes/peregrine/src/weights/mod.rs | 2 +- runtimes/spiritnet/src/lib.rs | 20 ++++++++++---------- runtimes/spiritnet/src/weights/mod.rs | 2 +- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 712a15a98..832de4c08 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -647,9 +647,7 @@ impl did::Config for Runtime { type MaxServiceUrlLength = MaxServiceUrlLength; type MaxNumberOfTypesPerService = MaxNumberOfTypesPerService; type MaxNumberOfUrlsPerService = MaxNumberOfUrlsPerService; - type MaxTypeCountPerService = MaxTypeCountPerService; - type MaxUrlCountPerService = MaxUrlCountPerService; - type WeightInfo = weights::did::WeightInfo; + type WeightInfo = (); } parameter_types! { diff --git a/runtimes/peregrine/src/weights/mod.rs b/runtimes/peregrine/src/weights/mod.rs index 66fa4afcd..72819c9c7 100644 --- a/runtimes/peregrine/src/weights/mod.rs +++ b/runtimes/peregrine/src/weights/mod.rs @@ -19,7 +19,7 @@ pub mod attestation; pub mod ctype; pub mod delegation; -pub mod did; +// pub mod did; pub mod frame_system; pub mod kilt_launch; pub mod pallet_balances; diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index a7c810fb2..ad9f29da9 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -61,9 +61,9 @@ use kilt_primitives::{ MAX_SIGNATURE_BYTE_LENGTH, }, did::{ - DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_DID_SERVICES_COUNT, MAX_ENDPOINT_URLS_COUNT, - MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_SERVICE_TYPE_COUNT, - MAX_SERVICE_TYPE_LENGTH, MAX_SERVICE_URL_COUNT, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, + DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_NUMBER_OF_SERVICES_PER_DID, MAX_ENDPOINT_URLS_COUNT, + MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_NUMBER_OF_TYPES_PER_SERVICE, + MAX_SERVICE_TYPE_LENGTH, MAX_NUMBER_OF_URLS_PER_SERVICE, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, MAX_URL_LENGTH, }, governance::{ @@ -624,12 +624,12 @@ parameter_types! { pub const MaxBlocksTxValidity: BlockNumber = MAX_BLOCKS_TX_VALIDITY; pub const DidDeposit: Balance = DID_DEPOSIT; pub const DidFee: Balance = DID_FEE; - pub const MaxDidServicesCount: u32 = MAX_DID_SERVICES_COUNT; + pub const MaxNumberOfServicesPerDid: u32 = MAX_NUMBER_OF_SERVICES_PER_DID; pub const MaxServiceIdLength: u32 = MAX_SERVICE_ID_LENGTH; pub const MaxServiceTypeLength: u32 = MAX_SERVICE_TYPE_LENGTH; pub const MaxServiceUrlLength: u32 = MAX_SERVICE_URL_LENGTH; - pub const MaxTypeCountPerService: u32 = MAX_SERVICE_TYPE_COUNT; - pub const MaxUrlCountPerService: u32 = MAX_SERVICE_URL_COUNT; + pub const MaxNumberOfTypesPerService: u32 = MAX_NUMBER_OF_TYPES_PER_SERVICE; + pub const MaxNumberOfUrlsPerService: u32 = MAX_NUMBER_OF_URLS_PER_SERVICE; } impl did::Config for Runtime { @@ -656,13 +656,13 @@ impl did::Config for Runtime { type MaxTotalKeyAgreementKeys = MaxTotalKeyAgreementKeys; type MaxPublicKeysPerDid = MaxPublicKeysPerDid; type MaxBlocksTxValidity = MaxBlocksTxValidity; - type MaxDidServicesCount = MaxDidServicesCount; + type MaxNumberOfServicesPerDid = MaxNumberOfServicesPerDid; type MaxServiceIdLength = MaxServiceIdLength; type MaxServiceTypeLength = MaxServiceTypeLength; type MaxServiceUrlLength = MaxServiceUrlLength; - type MaxTypeCountPerService = MaxTypeCountPerService; - type MaxUrlCountPerService = MaxUrlCountPerService; - type WeightInfo = weights::did::WeightInfo; + type MaxNumberOfTypesPerService = MaxNumberOfTypesPerService; + type MaxNumberOfUrlsPerService = MaxNumberOfUrlsPerService; + type WeightInfo = (); } parameter_types! { diff --git a/runtimes/spiritnet/src/weights/mod.rs b/runtimes/spiritnet/src/weights/mod.rs index 66fa4afcd..72819c9c7 100644 --- a/runtimes/spiritnet/src/weights/mod.rs +++ b/runtimes/spiritnet/src/weights/mod.rs @@ -19,7 +19,7 @@ pub mod attestation; pub mod ctype; pub mod delegation; -pub mod did; +// pub mod did; pub mod frame_system; pub mod kilt_launch; pub mod pallet_balances; From bc027d9054d6928344eb605bda510989c6e13ea2 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 08:44:01 +0200 Subject: [PATCH 19/45] bench: re-run benchmarks with step=1 --- pallets/did/src/default_weights.rs | 192 +++++++++++++------------- runtimes/peregrine/src/lib.rs | 2 +- runtimes/peregrine/src/weights/did.rs | 107 ++++++++------ runtimes/peregrine/src/weights/mod.rs | 2 +- runtimes/spiritnet/src/lib.rs | 2 +- runtimes/spiritnet/src/weights/did.rs | 107 ++++++++------ runtimes/spiritnet/src/weights/mod.rs | 2 +- 7 files changed, 226 insertions(+), 188 deletions(-) diff --git a/pallets/did/src/default_weights.rs b/pallets/did/src/default_weights.rs index cb1f6016c..7b932469b 100644 --- a/pallets/did/src/default_weights.rs +++ b/pallets/did/src/default_weights.rs @@ -19,14 +19,14 @@ //! Autogenerated weights for did //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-21, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ +//! DATE: 2021-10-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: 128 // Executed Command: // target/release/kilt-parachain // benchmark // --chain=dev -// --steps=50 +// --steps=1 // --repeat=20 // --pallet=did // --extrinsic=* @@ -86,187 +86,189 @@ pub trait WeightInfo { pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (108_176_000_u64) - // Standard Error: 241_000 - .saturating_add((4_481_000_u64).saturating_mul(n as Weight)) - // Standard Error: 75_000 - .saturating_add((9_351_000_u64).saturating_mul(c as Weight)) + (33_965_000_u64) + // Standard Error: 1_036_000 + .saturating_add((13_461_000_u64).saturating_mul(n as Weight)) + // Standard Error: 388_000 + .saturating_add((22_520_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { - (171_118_000_u64) - // Standard Error: 76_000 - .saturating_add((9_207_000_u64).saturating_mul(c as Weight)) + fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { + (87_757_000_u64) + // Standard Error: 2_150_000 + .saturating_add((10_087_000_u64).saturating_mul(n as Weight)) + // Standard Error: 806_000 + .saturating_add((12_920_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (223_404_000_u64) - // Standard Error: 186_000 - .saturating_add((3_119_000_u64).saturating_mul(n as Weight)) - // Standard Error: 58_000 - .saturating_add((9_215_000_u64).saturating_mul(c as Weight)) + (11_241_000_u64) + // Standard Error: 700_000 + .saturating_add((49_317_000_u64).saturating_mul(n as Weight)) + // Standard Error: 262_000 + .saturating_add((19_091_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (48_642_000_u64) + (107_392_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(27_u64)) } fn reclaim_deposit() -> Weight { - (53_300_000_u64) + (118_222_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(27_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (73_979_000_u64) + (161_424_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (76_314_000_u64) + (161_975_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (177_954_000_u64) + (315_704_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (41_358_000_u64) + (106_891_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (41_848_000_u64) + (111_430_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (41_578_000_u64) + (122_421_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (41_107_000_u64) + (106_030_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (41_538_000_u64) + (91_392_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (41_958_000_u64) + (111_650_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (38_693_000_u64) + (81_884_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (38_402_000_u64) + (93_416_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (39_134_000_u64) + (86_853_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (41_197_000_u64) + (111_119_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (40_797_000_u64) + (65_884_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (42_069_000_u64) + (124_104_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (37_862_000_u64) + (61_025_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (38_462_000_u64) + (49_654_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (38_582_000_u64) + (96_932_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (40_215_000_u64) + (53_732_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (40_526_000_u64) + (104_266_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (40_857_000_u64) + (111_751_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (38_282_000_u64) + (95_078_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (39_494_000_u64) + (96_742_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (38_462_000_u64) + (98_255_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (226_335_000_u64) + (434_818_000_u64) .saturating_add(T::DbWeight::get().reads(27_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_service_endpoint() -> Weight { - (10_841_000_u64) + (24_686_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (0_u64) + (144_359_000_u64) // Standard Error: 0 - .saturating_add((4_000_u64).saturating_mul(l as Weight)) + .saturating_add((5_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (331_203_000_u64) + (136_050_000_u64) // Standard Error: 0 .saturating_add((2_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (142_753_000_u64) + (180_185_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) @@ -276,187 +278,189 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (108_176_000_u64) - // Standard Error: 241_000 - .saturating_add((4_481_000_u64).saturating_mul(n as Weight)) - // Standard Error: 75_000 - .saturating_add((9_351_000_u64).saturating_mul(c as Weight)) + (33_965_000_u64) + // Standard Error: 1_036_000 + .saturating_add((13_461_000_u64).saturating_mul(n as Weight)) + // Standard Error: 388_000 + .saturating_add((22_520_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { - (171_118_000_u64) - // Standard Error: 76_000 - .saturating_add((9_207_000_u64).saturating_mul(c as Weight)) + fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { + (87_757_000_u64) + // Standard Error: 2_150_000 + .saturating_add((10_087_000_u64).saturating_mul(n as Weight)) + // Standard Error: 806_000 + .saturating_add((12_920_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (223_404_000_u64) - // Standard Error: 186_000 - .saturating_add((3_119_000_u64).saturating_mul(n as Weight)) - // Standard Error: 58_000 - .saturating_add((9_215_000_u64).saturating_mul(c as Weight)) + (11_241_000_u64) + // Standard Error: 700_000 + .saturating_add((49_317_000_u64).saturating_mul(n as Weight)) + // Standard Error: 262_000 + .saturating_add((19_091_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (48_642_000_u64) + (107_392_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(27_u64)) } fn reclaim_deposit() -> Weight { - (53_300_000_u64) + (118_222_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(27_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (73_979_000_u64) + (161_424_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (76_314_000_u64) + (161_975_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (177_954_000_u64) + (315_704_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (41_358_000_u64) + (106_891_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (41_848_000_u64) + (111_430_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (41_578_000_u64) + (122_421_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (41_107_000_u64) + (106_030_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (41_538_000_u64) + (91_392_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (41_958_000_u64) + (111_650_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (38_693_000_u64) + (81_884_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (38_402_000_u64) + (93_416_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (39_134_000_u64) + (86_853_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (41_197_000_u64) + (111_119_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (40_797_000_u64) + (65_884_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (42_069_000_u64) + (124_104_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (37_862_000_u64) + (61_025_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (38_462_000_u64) + (49_654_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (38_582_000_u64) + (96_932_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (40_215_000_u64) + (53_732_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (40_526_000_u64) + (104_266_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (40_857_000_u64) + (111_751_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (38_282_000_u64) + (95_078_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (39_494_000_u64) + (96_742_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (38_462_000_u64) + (98_255_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (226_335_000_u64) + (434_818_000_u64) .saturating_add(RocksDbWeight::get().reads(27_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_service_endpoint() -> Weight { - (10_841_000_u64) + (24_686_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (0_u64) + (144_359_000_u64) // Standard Error: 0 - .saturating_add((4_000_u64).saturating_mul(l as Weight)) + .saturating_add((5_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (331_203_000_u64) + (136_050_000_u64) // Standard Error: 0 .saturating_add((2_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (142_753_000_u64) + (180_185_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 832de4c08..165b42fd5 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -647,7 +647,7 @@ impl did::Config for Runtime { type MaxServiceUrlLength = MaxServiceUrlLength; type MaxNumberOfTypesPerService = MaxNumberOfTypesPerService; type MaxNumberOfUrlsPerService = MaxNumberOfUrlsPerService; - type WeightInfo = (); + type WeightInfo = weights::did::WeightInfo; } parameter_types! { diff --git a/runtimes/peregrine/src/weights/did.rs b/runtimes/peregrine/src/weights/did.rs index c6f1bd9e4..37c94f29b 100644 --- a/runtimes/peregrine/src/weights/did.rs +++ b/runtimes/peregrine/src/weights/did.rs @@ -47,173 +47,190 @@ use sp_std::marker::PhantomData; /// Weights for did using the recommended hardware. pub struct WeightInfo(PhantomData); impl did::WeightInfo for WeightInfo { - fn create_ed25519_keys(n: u32, ) -> Weight { - (152_990_000_u64) - // Standard Error: 19_000 - .saturating_add((2_263_000_u64).saturating_mul(n as Weight)) + fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { + (108_176_000_u64) + // Standard Error: 241_000 + .saturating_add((4_481_000_u64).saturating_mul(n as Weight)) + // Standard Error: 75_000 + .saturating_add((9_351_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(n: u32, ) -> Weight { - (156_426_000_u64) - // Standard Error: 17_000 - .saturating_add((2_308_000_u64).saturating_mul(n as Weight)) + fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { + (171_118_000_u64) + // Standard Error: 76_000 + .saturating_add((9_207_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) - } - fn create_ecdsa_keys(n: u32, ) -> Weight { - (271_602_000_u64) - // Standard Error: 27_000 - .saturating_add((2_249_000_u64).saturating_mul(n as Weight)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) + } + fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { + (223_404_000_u64) + // Standard Error: 186_000 + .saturating_add((3_119_000_u64).saturating_mul(n as Weight)) + // Standard Error: 58_000 + .saturating_add((9_215_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (27_753_000_u64) + (48_642_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes(27_u64)) } fn reclaim_deposit() -> Weight { - (32_617_000_u64) + (53_300_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes(27_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (87_622_000_u64) + (73_979_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (90_660_000_u64) + (76_314_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (205_636_000_u64) + (177_954_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (51_002_000_u64) + (41_358_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (51_170_000_u64) + (41_848_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (50_767_000_u64) + (41_578_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (50_809_000_u64) + (41_107_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (51_006_000_u64) + (41_538_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (50_786_000_u64) + (41_958_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (46_817_000_u64) + (38_693_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (46_894_000_u64) + (38_402_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (47_139_000_u64) + (39_134_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (50_902_000_u64) + (41_197_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (50_685_000_u64) + (40_797_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (50_658_000_u64) + (42_069_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (46_883_000_u64) + (37_862_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (46_979_000_u64) + (38_462_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (46_874_000_u64) + (38_582_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (50_126_000_u64) + (40_215_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (50_087_000_u64) + (40_526_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (50_163_000_u64) + (40_857_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (47_274_000_u64) + (38_282_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (47_150_000_u64) + (39_494_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (47_219_000_u64) + (38_462_000_u64) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn add_service_endpoint() -> Weight { + (226_335_000_u64) + .saturating_add(T::DbWeight::get().reads(27_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn remove_service_endpoint() -> Weight { + (10_841_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (32_076_000_u64) + (0_u64) // Standard Error: 0 .saturating_add((4_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (29_466_000_u64) + (331_203_000_u64) // Standard Error: 0 .saturating_add((2_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (149_067_000_u64) + (142_753_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } -} \ No newline at end of file +} diff --git a/runtimes/peregrine/src/weights/mod.rs b/runtimes/peregrine/src/weights/mod.rs index 72819c9c7..66fa4afcd 100644 --- a/runtimes/peregrine/src/weights/mod.rs +++ b/runtimes/peregrine/src/weights/mod.rs @@ -19,7 +19,7 @@ pub mod attestation; pub mod ctype; pub mod delegation; -// pub mod did; +pub mod did; pub mod frame_system; pub mod kilt_launch; pub mod pallet_balances; diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index ad9f29da9..b8042cc0f 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -662,7 +662,7 @@ impl did::Config for Runtime { type MaxServiceUrlLength = MaxServiceUrlLength; type MaxNumberOfTypesPerService = MaxNumberOfTypesPerService; type MaxNumberOfUrlsPerService = MaxNumberOfUrlsPerService; - type WeightInfo = (); + type WeightInfo = weights::did::WeightInfo; } parameter_types! { diff --git a/runtimes/spiritnet/src/weights/did.rs b/runtimes/spiritnet/src/weights/did.rs index 3e211988c..8f6fb298d 100644 --- a/runtimes/spiritnet/src/weights/did.rs +++ b/runtimes/spiritnet/src/weights/did.rs @@ -47,173 +47,190 @@ use sp_std::marker::PhantomData; /// Weights for did using the recommended hardware. pub struct WeightInfo(PhantomData); impl did::WeightInfo for WeightInfo { - fn create_ed25519_keys(n: u32, ) -> Weight { - (154_795_000_u64) - // Standard Error: 26_000 - .saturating_add((2_260_000_u64).saturating_mul(n as Weight)) + fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { + (108_176_000_u64) + // Standard Error: 241_000 + .saturating_add((4_481_000_u64).saturating_mul(n as Weight)) + // Standard Error: 75_000 + .saturating_add((9_351_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(n: u32, ) -> Weight { - (157_404_000_u64) - // Standard Error: 19_000 - .saturating_add((2_397_000_u64).saturating_mul(n as Weight)) + fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { + (171_118_000_u64) + // Standard Error: 76_000 + .saturating_add((9_207_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) - } - fn create_ecdsa_keys(n: u32, ) -> Weight { - (274_173_000_u64) - // Standard Error: 37_000 - .saturating_add((2_137_000_u64).saturating_mul(n as Weight)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) + } + fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { + (223_404_000_u64) + // Standard Error: 186_000 + .saturating_add((3_119_000_u64).saturating_mul(n as Weight)) + // Standard Error: 58_000 + .saturating_add((9_215_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (28_034_000_u64) + (48_642_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes(27_u64)) } fn reclaim_deposit() -> Weight { - (32_719_000_u64) + (53_300_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes(27_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (87_871_000_u64) + (73_979_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (90_623_000_u64) + (76_314_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (206_460_000_u64) + (177_954_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (51_201_000_u64) + (41_358_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (51_538_000_u64) + (41_848_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (51_660_000_u64) + (41_578_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (50_884_000_u64) + (41_107_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (50_713_000_u64) + (41_538_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (50_749_000_u64) + (41_958_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (46_931_000_u64) + (38_693_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (47_051_000_u64) + (38_402_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (47_052_000_u64) + (39_134_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (50_688_000_u64) + (41_197_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (51_242_000_u64) + (40_797_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (50_993_000_u64) + (42_069_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (47_161_000_u64) + (37_862_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (47_215_000_u64) + (38_462_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (47_032_000_u64) + (38_582_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (50_407_000_u64) + (40_215_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (50_143_000_u64) + (40_526_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (50_221_000_u64) + (40_857_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (47_462_000_u64) + (38_282_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (47_390_000_u64) + (39_494_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (47_502_000_u64) + (38_462_000_u64) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn add_service_endpoint() -> Weight { + (226_335_000_u64) + .saturating_add(T::DbWeight::get().reads(27_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn remove_service_endpoint() -> Weight { + (10_841_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (30_672_000_u64) + (0_u64) // Standard Error: 0 .saturating_add((4_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (28_096_000_u64) + (331_203_000_u64) // Standard Error: 0 .saturating_add((2_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (146_144_000_u64) + (142_753_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } -} \ No newline at end of file +} diff --git a/runtimes/spiritnet/src/weights/mod.rs b/runtimes/spiritnet/src/weights/mod.rs index 72819c9c7..66fa4afcd 100644 --- a/runtimes/spiritnet/src/weights/mod.rs +++ b/runtimes/spiritnet/src/weights/mod.rs @@ -19,7 +19,7 @@ pub mod attestation; pub mod ctype; pub mod delegation; -// pub mod did; +pub mod did; pub mod frame_system; pub mod kilt_launch; pub mod pallet_balances; From ab4a925620119e6009e4d3710416f758250d535d Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 14:15:16 +0200 Subject: [PATCH 20/45] feat: new storage design --- pallets/did/src/default_weights.rs | 228 ++++++++++++++--------------- pallets/did/src/lib.rs | 27 +++- pallets/did/src/mock.rs | 1 + pallets/did/src/tests.rs | 28 ++-- 4 files changed, 154 insertions(+), 130 deletions(-) diff --git a/pallets/did/src/default_weights.rs b/pallets/did/src/default_weights.rs index 7b932469b..eee5920ba 100644 --- a/pallets/did/src/default_weights.rs +++ b/pallets/did/src/default_weights.rs @@ -86,189 +86,189 @@ pub trait WeightInfo { pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (33_965_000_u64) - // Standard Error: 1_036_000 - .saturating_add((13_461_000_u64).saturating_mul(n as Weight)) - // Standard Error: 388_000 - .saturating_add((22_520_000_u64).saturating_mul(c as Weight)) + (138_480_000_u64) + // Standard Error: 79_000 + .saturating_add((951_000_u64).saturating_mul(n as Weight)) + // Standard Error: 29_000 + .saturating_add((7_831_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { - (87_757_000_u64) - // Standard Error: 2_150_000 - .saturating_add((10_087_000_u64).saturating_mul(n as Weight)) - // Standard Error: 806_000 - .saturating_add((12_920_000_u64).saturating_mul(c as Weight)) + (131_034_000_u64) + // Standard Error: 75_000 + .saturating_add((1_636_000_u64).saturating_mul(n as Weight)) + // Standard Error: 28_000 + .saturating_add((8_400_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (11_241_000_u64) - // Standard Error: 700_000 - .saturating_add((49_317_000_u64).saturating_mul(n as Weight)) - // Standard Error: 262_000 - .saturating_add((19_091_000_u64).saturating_mul(c as Weight)) + (227_356_000_u64) + // Standard Error: 61_000 + .saturating_add((1_927_000_u64).saturating_mul(n as Weight)) + // Standard Error: 23_000 + .saturating_add((7_862_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (107_392_000_u64) + (46_747_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(27_u64)) + .saturating_add(T::DbWeight::get().writes(28_u64)) } fn reclaim_deposit() -> Weight { - (118_222_000_u64) + (50_725_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(27_u64)) + .saturating_add(T::DbWeight::get().writes(28_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (161_424_000_u64) + (70_222_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (161_975_000_u64) + (72_757_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (315_704_000_u64) + (172_554_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (106_891_000_u64) + (38_232_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (111_430_000_u64) + (38_582_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (122_421_000_u64) + (38_823_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (106_030_000_u64) + (38_122_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (91_392_000_u64) + (38_221_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (111_650_000_u64) + (38_271_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (81_884_000_u64) + (35_085_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (93_416_000_u64) + (36_699_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (86_853_000_u64) + (35_147_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (111_119_000_u64) + (38_062_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (65_884_000_u64) + (37_891_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (124_104_000_u64) + (38_131_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (61_025_000_u64) + (35_076_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (49_654_000_u64) + (35_016_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (96_932_000_u64) + (34_735_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (53_732_000_u64) + (37_239_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (104_266_000_u64) + (37_450_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (111_751_000_u64) + (37_671_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (95_078_000_u64) + (34_835_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (96_742_000_u64) + (35_096_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (98_255_000_u64) + (35_086_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (434_818_000_u64) - .saturating_add(T::DbWeight::get().reads(27_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) + (19_967_000_u64) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) } fn remove_service_endpoint() -> Weight { - (24_686_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) + (13_816_000_u64) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (144_359_000_u64) + (60_289_000_u64) // Standard Error: 0 - .saturating_add((5_000_u64).saturating_mul(l as Weight)) + .saturating_add((4_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (136_050_000_u64) + (58_172_000_u64) // Standard Error: 0 - .saturating_add((2_000_u64).saturating_mul(l as Weight)) + .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (180_185_000_u64) + (159_729_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) @@ -278,189 +278,189 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (33_965_000_u64) - // Standard Error: 1_036_000 - .saturating_add((13_461_000_u64).saturating_mul(n as Weight)) - // Standard Error: 388_000 - .saturating_add((22_520_000_u64).saturating_mul(c as Weight)) + (138_480_000_u64) + // Standard Error: 79_000 + .saturating_add((951_000_u64).saturating_mul(n as Weight)) + // Standard Error: 29_000 + .saturating_add((7_831_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) - .saturating_add(RocksDbWeight::get().writes(3_u64)) + .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { - (87_757_000_u64) - // Standard Error: 2_150_000 - .saturating_add((10_087_000_u64).saturating_mul(n as Weight)) - // Standard Error: 806_000 - .saturating_add((12_920_000_u64).saturating_mul(c as Weight)) + (131_034_000_u64) + // Standard Error: 75_000 + .saturating_add((1_636_000_u64).saturating_mul(n as Weight)) + // Standard Error: 28_000 + .saturating_add((8_400_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) - .saturating_add(RocksDbWeight::get().writes(3_u64)) + .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (11_241_000_u64) - // Standard Error: 700_000 - .saturating_add((49_317_000_u64).saturating_mul(n as Weight)) - // Standard Error: 262_000 - .saturating_add((19_091_000_u64).saturating_mul(c as Weight)) + (227_356_000_u64) + // Standard Error: 61_000 + .saturating_add((1_927_000_u64).saturating_mul(n as Weight)) + // Standard Error: 23_000 + .saturating_add((7_862_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) - .saturating_add(RocksDbWeight::get().writes(3_u64)) + .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (107_392_000_u64) + (46_747_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) - .saturating_add(RocksDbWeight::get().writes(27_u64)) + .saturating_add(RocksDbWeight::get().writes(28_u64)) } fn reclaim_deposit() -> Weight { - (118_222_000_u64) + (50_725_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) - .saturating_add(RocksDbWeight::get().writes(27_u64)) + .saturating_add(RocksDbWeight::get().writes(28_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (161_424_000_u64) + (70_222_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (161_975_000_u64) + (72_757_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (315_704_000_u64) + (172_554_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (106_891_000_u64) + (38_232_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (111_430_000_u64) + (38_582_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (122_421_000_u64) + (38_823_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (106_030_000_u64) + (38_122_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (91_392_000_u64) + (38_221_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (111_650_000_u64) + (38_271_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (81_884_000_u64) + (35_085_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (93_416_000_u64) + (36_699_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (86_853_000_u64) + (35_147_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (111_119_000_u64) + (38_062_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (65_884_000_u64) + (37_891_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (124_104_000_u64) + (38_131_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (61_025_000_u64) + (35_076_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (49_654_000_u64) + (35_016_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (96_932_000_u64) + (34_735_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (53_732_000_u64) + (37_239_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (104_266_000_u64) + (37_450_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (111_751_000_u64) + (37_671_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (95_078_000_u64) + (34_835_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (96_742_000_u64) + (35_096_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (98_255_000_u64) + (35_086_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (434_818_000_u64) - .saturating_add(RocksDbWeight::get().reads(27_u64)) - .saturating_add(RocksDbWeight::get().writes(1_u64)) + (19_967_000_u64) + .saturating_add(RocksDbWeight::get().reads(3_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn remove_service_endpoint() -> Weight { - (24_686_000_u64) - .saturating_add(RocksDbWeight::get().reads(1_u64)) - .saturating_add(RocksDbWeight::get().writes(1_u64)) + (13_816_000_u64) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (144_359_000_u64) + (60_289_000_u64) // Standard Error: 0 - .saturating_add((5_000_u64).saturating_mul(l as Weight)) + .saturating_add((4_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (136_050_000_u64) + (58_172_000_u64) // Standard Error: 0 - .saturating_add((2_000_u64).saturating_mul(l as Weight)) + .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (180_185_000_u64) + (159_729_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index a8e3dacbc..5c96d7498 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -314,6 +314,9 @@ pub mod pallet { DidEndpointDetails, >; + #[pallet::storage] + pub(crate) type DidEndpointsCount = StorageMap<_, Blake2_128Concat, DidIdentifierOf, u32>; + /// The set of DIDs that have been deleted and cannot therefore be created /// again for security reasons. /// @@ -556,6 +559,8 @@ pub mod pallet { ServiceEndpoints::::insert(&did_identifier, &service.id, service.clone()); }); + DidEndpointsCount::::insert(&did_identifier, input_service_endpoints.len().saturated_into::()); + Self::deposit_event(Event::DidCreated(sender, did_identifier)); Ok(()) @@ -784,7 +789,7 @@ pub mod pallet { Ok(()) } - #[pallet::weight(100_000)] + #[pallet::weight(::WeightInfo::add_service_endpoint())] pub fn add_service_endpoint(origin: OriginFor, service_endpoint: DidEndpointDetails) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); @@ -794,10 +799,11 @@ pub mod pallet { // Verify that the DID is present. ensure!(Did::::get(&did_subject).is_some(), Error::::DidNotPresent); + let currently_stored_endpoints_count = DidEndpointsCount::::get(&did_subject).unwrap_or_default(); + // Verify that there are less than the maximum limit of services stored. ensure!( - ServiceEndpoints::::iter_prefix(&did_subject).count() - < T::MaxNumberOfServicesPerDid::get().saturated_into(), + currently_stored_endpoints_count < T::MaxNumberOfServicesPerDid::get(), Error::::MaxNumberOfServicesPerDidExceeded ); @@ -813,10 +819,12 @@ pub mod pallet { }, )?; + DidEndpointsCount::::insert(&did_subject, currently_stored_endpoints_count.saturating_add(1)); + Ok(()) } - #[pallet::weight(100_000)] + #[pallet::weight(::WeightInfo::remove_service_endpoint())] pub fn remove_service_endpoint(origin: OriginFor, service_id: ServiceEndpointId) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); @@ -825,6 +833,16 @@ pub mod pallet { Error::::ServiceNotPresent ); + // Decrease the endpoints counter or delete the entry if it reaches 0 + DidEndpointsCount::::mutate_exists(&did_subject, |existing_endpoint_count| { + let new_value = existing_endpoint_count.unwrap_or_default().saturating_sub(1); + if new_value == 0u32 { + *existing_endpoint_count = None; + } else { + *existing_endpoint_count = Some(new_value); + } + }); + Ok(()) } @@ -1092,6 +1110,7 @@ impl Pallet { if let KillStorageResult::SomeRemaining(_) = storage_kill_result { return Err(Error::::InternalError.into()); }; + DidEndpointsCount::::remove(&did_subject); kilt_support::free_deposit::, CurrencyOf>(&did_entry.deposit); // Mark as deleted to prevent potential replay-attacks of re-adding a previously // deleted DID. diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index d8c038e6c..71eb2a516 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -472,6 +472,7 @@ impl ExtBuilder { for endpoint in endpoints.iter() { did::ServiceEndpoints::::insert(&did, &endpoint.id, endpoint) } + did::DidEndpointsCount::::insert(&did, endpoints.len().saturated_into::()); } did::StorageVersion::::set(self.storage_version); }); diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 54187887f..8047f680b 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -247,8 +247,8 @@ fn check_successful_complete_creation() { // ... and that the number of elements in the creation operation is the same as // the number of elements stored. assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), - details.new_service_details.len() + did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), + details.new_service_details.len().saturated_into::() ); assert_eq!( @@ -1602,6 +1602,8 @@ fn check_key_not_found_key_agreement_key_deletion_error() { }); } +// add_service_endpoint + #[test] fn check_service_addition_no_prior_service_successful() { let auth_key = get_ed25519_authentication_key(true); @@ -1621,6 +1623,7 @@ fn check_service_addition_no_prior_service_successful() { let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id) .expect("Service endpoint should be stored."); assert_eq!(stored_endpoint, new_service_endpoint); + assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), 1); }); } @@ -1651,9 +1654,7 @@ fn check_service_addition_one_from_full_successful() { new_service_endpoint.clone() ),); assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did) - .count() - .saturated_into::(), + did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), ::MaxNumberOfServicesPerDid::get() ); let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id) @@ -1924,8 +1925,11 @@ fn check_invalid_service_url_character_addition_error() { }); } +// remove_service_endpoint + #[test] fn check_service_deletion_successful() { + initialize_logger(); let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); let old_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); @@ -1941,9 +1945,9 @@ fn check_service_deletion_successful() { Origin::signed(alice_did.clone()), old_service_endpoint.id ),); - assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), - 0 + // Counter should be deleted from the storage. + assert!( + did::pallet::DidEndpointsCount::::get(&alice_did).is_none() ); }); } @@ -1993,7 +1997,7 @@ fn check_successful_deletion() { .build(None) .execute_with(|| { assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), 1 ); assert_eq!( @@ -2006,7 +2010,7 @@ fn check_successful_deletion() { assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), 0 ); @@ -2067,7 +2071,7 @@ fn check_successful_reclaiming() { .build(None) .execute_with(|| { assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), 1 ); assert_eq!( @@ -2082,7 +2086,7 @@ fn check_successful_reclaiming() { assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); assert_eq!( - did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), 0 ); From a37b0fa96d77b6b440e913fe34954fcd5dd5fca9 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 14:46:37 +0200 Subject: [PATCH 21/45] chore: minor improvements --- pallets/did/src/benchmarking.rs | 8 +- pallets/did/src/default_weights.rs | 184 ++++++++++++++--------------- pallets/did/src/lib.rs | 10 +- pallets/did/src/tests.rs | 114 +++++++++++++++--- runtimes/peregrine/src/lib.rs | 8 +- runtimes/spiritnet/src/lib.rs | 8 +- runtimes/standalone/src/lib.rs | 8 +- 7 files changed, 207 insertions(+), 133 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 969beca43..c21e5427b 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -871,8 +871,8 @@ benchmarks! { Some(new_service_endpoint) ); assert_eq!( - ServiceEndpoints::::iter_prefix(&did_subject).count().saturated_into::(), - T::MaxNumberOfServicesPerDid::get() + ServiceEndpoints::::iter_prefix(&did_subject).count(), + T::MaxNumberOfServicesPerDid::get().saturated_into::() ); } @@ -900,8 +900,8 @@ benchmarks! { ServiceEndpoints::::get(&did_subject, &endpoint_id).is_none() ); assert_eq!( - ServiceEndpoints::::iter_prefix(&did_subject).count().saturated_into::(), - T::MaxNumberOfServicesPerDid::get() - 1 + ServiceEndpoints::::iter_prefix(&did_subject).count(), + T::MaxNumberOfServicesPerDid::get().saturated_into::() - 1 ); } diff --git a/pallets/did/src/default_weights.rs b/pallets/did/src/default_weights.rs index eee5920ba..b01c73906 100644 --- a/pallets/did/src/default_weights.rs +++ b/pallets/did/src/default_weights.rs @@ -85,190 +85,188 @@ pub trait WeightInfo { /// Weights for did using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { - fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (138_480_000_u64) - // Standard Error: 79_000 - .saturating_add((951_000_u64).saturating_mul(n as Weight)) - // Standard Error: 29_000 - .saturating_add((7_831_000_u64).saturating_mul(c as Weight)) + fn create_ed25519_keys(_n: u32, c: u32, ) -> Weight { + (145_699_000_u64) + // Standard Error: 55_000 + .saturating_add((8_183_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { - (131_034_000_u64) - // Standard Error: 75_000 - .saturating_add((1_636_000_u64).saturating_mul(n as Weight)) - // Standard Error: 28_000 - .saturating_add((8_400_000_u64).saturating_mul(c as Weight)) + (132_311_000_u64) + // Standard Error: 109_000 + .saturating_add((1_618_000_u64).saturating_mul(n as Weight)) + // Standard Error: 40_000 + .saturating_add((8_640_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (227_356_000_u64) - // Standard Error: 61_000 - .saturating_add((1_927_000_u64).saturating_mul(n as Weight)) - // Standard Error: 23_000 - .saturating_add((7_862_000_u64).saturating_mul(c as Weight)) + (228_832_000_u64) + // Standard Error: 90_000 + .saturating_add((1_894_000_u64).saturating_mul(n as Weight)) + // Standard Error: 33_000 + .saturating_add((8_065_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (46_747_000_u64) + (46_988_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(28_u64)) } fn reclaim_deposit() -> Weight { - (50_725_000_u64) + (50_956_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(28_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (70_222_000_u64) + (71_013_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (72_757_000_u64) + (73_658_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (172_554_000_u64) + (174_668_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (38_232_000_u64) + (39_094_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (38_582_000_u64) + (39_103_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (38_823_000_u64) + (39_113_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (38_122_000_u64) + (38_643_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (38_221_000_u64) + (38_653_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (38_271_000_u64) + (39_033_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (35_085_000_u64) + (35_677_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (36_699_000_u64) + (35_627_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (35_147_000_u64) + (35_828_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (38_062_000_u64) + (38_943_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (37_891_000_u64) + (38_562_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (38_131_000_u64) + (39_264_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (35_076_000_u64) + (35_847_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (35_016_000_u64) + (35_808_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (34_735_000_u64) + (35_997_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (37_239_000_u64) + (38_363_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (37_450_000_u64) + (38_312_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (37_671_000_u64) + (38_613_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (34_835_000_u64) + (36_048_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (35_096_000_u64) + (36_398_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (35_086_000_u64) + (36_719_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (19_967_000_u64) + (20_929_000_u64) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn remove_service_endpoint() -> Weight { - (13_816_000_u64) + (13_756_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (60_289_000_u64) + (61_127_000_u64) // Standard Error: 0 .saturating_add((4_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (58_172_000_u64) + (58_737_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (159_729_000_u64) + (160_746_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) @@ -277,190 +275,188 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { - fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (138_480_000_u64) - // Standard Error: 79_000 - .saturating_add((951_000_u64).saturating_mul(n as Weight)) - // Standard Error: 29_000 - .saturating_add((7_831_000_u64).saturating_mul(c as Weight)) + fn create_ed25519_keys(_n: u32, c: u32, ) -> Weight { + (145_699_000_u64) + // Standard Error: 55_000 + .saturating_add((8_183_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { - (131_034_000_u64) - // Standard Error: 75_000 - .saturating_add((1_636_000_u64).saturating_mul(n as Weight)) - // Standard Error: 28_000 - .saturating_add((8_400_000_u64).saturating_mul(c as Weight)) + (132_311_000_u64) + // Standard Error: 109_000 + .saturating_add((1_618_000_u64).saturating_mul(n as Weight)) + // Standard Error: 40_000 + .saturating_add((8_640_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (227_356_000_u64) - // Standard Error: 61_000 - .saturating_add((1_927_000_u64).saturating_mul(n as Weight)) - // Standard Error: 23_000 - .saturating_add((7_862_000_u64).saturating_mul(c as Weight)) + (228_832_000_u64) + // Standard Error: 90_000 + .saturating_add((1_894_000_u64).saturating_mul(n as Weight)) + // Standard Error: 33_000 + .saturating_add((8_065_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (46_747_000_u64) + (46_988_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(28_u64)) } fn reclaim_deposit() -> Weight { - (50_725_000_u64) + (50_956_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(28_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (70_222_000_u64) + (71_013_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (72_757_000_u64) + (73_658_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (172_554_000_u64) + (174_668_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (38_232_000_u64) + (39_094_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (38_582_000_u64) + (39_103_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (38_823_000_u64) + (39_113_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (38_122_000_u64) + (38_643_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (38_221_000_u64) + (38_653_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (38_271_000_u64) + (39_033_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (35_085_000_u64) + (35_677_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (36_699_000_u64) + (35_627_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (35_147_000_u64) + (35_828_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (38_062_000_u64) + (38_943_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (37_891_000_u64) + (38_562_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (38_131_000_u64) + (39_264_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (35_076_000_u64) + (35_847_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (35_016_000_u64) + (35_808_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (34_735_000_u64) + (35_997_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (37_239_000_u64) + (38_363_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (37_450_000_u64) + (38_312_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (37_671_000_u64) + (38_613_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (34_835_000_u64) + (36_048_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (35_096_000_u64) + (36_398_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (35_086_000_u64) + (36_719_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (19_967_000_u64) + (20_929_000_u64) .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn remove_service_endpoint() -> Weight { - (13_816_000_u64) + (13_756_000_u64) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (60_289_000_u64) + (61_127_000_u64) // Standard Error: 0 .saturating_add((4_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (58_172_000_u64) + (58_737_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (159_729_000_u64) + (160_746_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 5c96d7498..ad31310e8 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -809,7 +809,7 @@ pub mod pallet { // *** No Fail beyond this point *** - ServiceEndpoints::::try_mutate_exists( + ServiceEndpoints::::try_mutate( &did_subject, service_endpoint.id.clone(), |existing_service| -> Result<(), Error> { @@ -1102,15 +1102,13 @@ impl Pallet { let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; // *** No Fail beyond this point *** - let storage_kill_result = - ServiceEndpoints::::remove_prefix(&did_subject, Some(T::MaxNumberOfServicesPerDid::get())); - + let services_count = DidEndpointsCount::::take(&did_subject).unwrap_or_default(); + let storage_kill_result = ServiceEndpoints::::remove_prefix(&did_subject, Some(services_count)); // If some items are remaining, it means that there were more than - // MaxNumberOfServicesPerDid stored, and that should never happen. + // the counter stored in `DidEndpointsCount`, and that should never happen. if let KillStorageResult::SomeRemaining(_) = storage_kill_result { return Err(Error::::InternalError.into()); }; - DidEndpointsCount::::remove(&did_subject); kilt_support::free_deposit::, CurrencyOf>(&did_entry.deposit); // Mark as deleted to prevent potential replay-attacks of re-adding a previously // deleted DID. diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 8047f680b..d2f32b05d 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -245,10 +245,16 @@ fn check_successful_complete_creation() { assert_eq!(stored_service.service_type, new_service.service_type); }); // ... and that the number of elements in the creation operation is the same as - // the number of elements stored. + // the number of elements stored in `ServiceEndpoints` and `DidEndpointsCount`. assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), - details.new_service_details.len().saturated_into::() + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + details.new_service_details.len() + ); + assert_eq!( + did::pallet::DidEndpointsCount::::get(&alice_did) + .unwrap_or_default() + .saturated_into::(), + details.new_service_details.len() ); assert_eq!( @@ -452,8 +458,14 @@ fn check_max_limit_service_endpoints_count_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); - details.new_service_details = - get_service_endpoints(::MaxNumberOfServicesPerDid::get() + 1, 1, 1, 1, 1, 1); + details.new_service_details = get_service_endpoints( + ::MaxNumberOfServicesPerDid::get() + 1, + 1, + 1, + 1, + 1, + 1, + ); let signature = auth_key.sign(details.encode().as_ref()); @@ -502,8 +514,14 @@ fn check_max_limit_service_type_count_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); - details.new_service_details = - get_service_endpoints(1, 1, ::MaxNumberOfTypesPerService::get() + 1, 1, 1, 1); + details.new_service_details = get_service_endpoints( + 1, + 1, + ::MaxNumberOfTypesPerService::get() + 1, + 1, + 1, + 1, + ); let signature = auth_key.sign(details.encode().as_ref()); @@ -552,8 +570,14 @@ fn check_max_limit_service_url_count_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); - details.new_service_details = - get_service_endpoints(1, 1, 1, 1, ::MaxNumberOfUrlsPerService::get() + 1, 1); + details.new_service_details = get_service_endpoints( + 1, + 1, + 1, + 1, + ::MaxNumberOfUrlsPerService::get() + 1, + 1, + ); let signature = auth_key.sign(details.encode().as_ref()); @@ -1623,7 +1647,14 @@ fn check_service_addition_no_prior_service_successful() { let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id) .expect("Service endpoint should be stored."); assert_eq!(stored_endpoint, new_service_endpoint); - assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), 1); + assert_eq!( + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + 1 + ); + assert_eq!( + did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), + 1 + ); }); } @@ -1657,6 +1688,10 @@ fn check_service_addition_one_from_full_successful() { did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), ::MaxNumberOfServicesPerDid::get() ); + assert_eq!( + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + ::MaxNumberOfServicesPerDid::get().saturated_into::() + ); let stored_endpoint = did::pallet::ServiceEndpoints::::get(&alice_did, &new_service_endpoint.id) .expect("Service endpoint should be stored."); assert_eq!(stored_endpoint, new_service_endpoint); @@ -1946,8 +1981,10 @@ fn check_service_deletion_successful() { old_service_endpoint.id ),); // Counter should be deleted from the storage. - assert!( - did::pallet::DidEndpointsCount::::get(&alice_did).is_none() + assert!(did::pallet::DidEndpointsCount::::get(&alice_did).is_none()); + assert_eq!( + did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), + 0 ); }); } @@ -1977,7 +2014,53 @@ fn check_service_not_present_deletion_error() { // delete #[test] -fn check_successful_deletion() { +fn check_successful_deletion_no_endpoints() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + + let mut did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + did_details.deposit.owner = ACCOUNT_00; + did_details.deposit.amount = ::Deposit::get(); + + let balance = ::Deposit::get() * 2 + + ::Fee::get() * 2 + + <::Currency as Currency>>::minimum_balance(); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .with_dids(vec![(alice_did.clone(), did_details)]) + .build(None) + .execute_with(|| { + assert!(did::pallet::DidEndpointsCount::::get(&alice_did).is_none()); + assert_eq!( + Balances::reserved_balance(ACCOUNT_00), + ::Deposit::get() + ); + assert_ok!(Did::delete(Origin::signed(alice_did.clone()))); + assert!(Did::get_did(alice_did.clone()).is_none()); + assert!(Did::get_deleted_did(alice_did.clone()).is_some()); + assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); + + assert!(did::pallet::DidEndpointsCount::::get(&alice_did).is_none()); + + // Re-adding the same DID identifier should fail. + let details = generate_base_did_creation_details::(alice_did.clone(), ACCOUNT_00); + + let signature = auth_key.sign(details.encode().as_ref()); + + assert_noop!( + Did::create( + Origin::signed(ACCOUNT_00.clone()), + details, + did::DidSignature::from(signature), + ), + did::Error::::DidAlreadyDeleted + ); + }); +} + +#[test] +fn check_successful_deletion_with_endpoints() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); let service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); @@ -2009,10 +2092,7 @@ fn check_successful_deletion() { assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); - assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), - 0 - ); + assert!(did::pallet::DidEndpointsCount::::get(&alice_did).is_none(),); // Re-adding the same DID identifier should fail. let details = generate_base_did_creation_details::(alice_did.clone(), ACCOUNT_00); diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 165b42fd5..9d1afe4ff 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -60,10 +60,10 @@ use kilt_primitives::{ MAX_SIGNATURE_BYTE_LENGTH, }, did::{ - DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_NUMBER_OF_SERVICES_PER_DID, MAX_ENDPOINT_URLS_COUNT, - MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_NUMBER_OF_TYPES_PER_SERVICE, - MAX_SERVICE_TYPE_LENGTH, MAX_NUMBER_OF_URLS_PER_SERVICE, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, - MAX_URL_LENGTH, + DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_ENDPOINT_URLS_COUNT, MAX_KEY_AGREEMENT_KEYS, + MAX_NUMBER_OF_SERVICES_PER_DID, MAX_NUMBER_OF_TYPES_PER_SERVICE, MAX_NUMBER_OF_URLS_PER_SERVICE, + MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_SERVICE_TYPE_LENGTH, MAX_SERVICE_URL_LENGTH, + MAX_TOTAL_KEY_AGREEMENT_KEYS, MAX_URL_LENGTH, }, governance::{ COOLOFF_PERIOD, COUNCIL_MOTION_DURATION, ENACTMENT_PERIOD, FAST_TRACK_VOTING_PERIOD, LAUNCH_PERIOD, diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index b8042cc0f..44854015c 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -61,10 +61,10 @@ use kilt_primitives::{ MAX_SIGNATURE_BYTE_LENGTH, }, did::{ - DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_NUMBER_OF_SERVICES_PER_DID, MAX_ENDPOINT_URLS_COUNT, - MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_NUMBER_OF_TYPES_PER_SERVICE, - MAX_SERVICE_TYPE_LENGTH, MAX_NUMBER_OF_URLS_PER_SERVICE, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, - MAX_URL_LENGTH, + DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_ENDPOINT_URLS_COUNT, MAX_KEY_AGREEMENT_KEYS, + MAX_NUMBER_OF_SERVICES_PER_DID, MAX_NUMBER_OF_TYPES_PER_SERVICE, MAX_NUMBER_OF_URLS_PER_SERVICE, + MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_SERVICE_TYPE_LENGTH, MAX_SERVICE_URL_LENGTH, + MAX_TOTAL_KEY_AGREEMENT_KEYS, MAX_URL_LENGTH, }, governance::{ COOLOFF_PERIOD, COUNCIL_MOTION_DURATION, ENACTMENT_PERIOD, FAST_TRACK_VOTING_PERIOD, LAUNCH_PERIOD, diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index 46ed64b68..c46cb86ba 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -38,10 +38,10 @@ use kilt_primitives::{ MAX_SIGNATURE_BYTE_LENGTH, }, did::{ - DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_NUMBER_OF_SERVICES_PER_DID, MAX_ENDPOINT_URLS_COUNT, - MAX_KEY_AGREEMENT_KEYS, MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_NUMBER_OF_TYPES_PER_SERVICE, - MAX_SERVICE_TYPE_LENGTH, MAX_NUMBER_OF_URLS_PER_SERVICE, MAX_SERVICE_URL_LENGTH, MAX_TOTAL_KEY_AGREEMENT_KEYS, - MAX_URL_LENGTH, + DID_DEPOSIT, DID_FEE, MAX_BLOCKS_TX_VALIDITY, MAX_ENDPOINT_URLS_COUNT, MAX_KEY_AGREEMENT_KEYS, + MAX_NUMBER_OF_SERVICES_PER_DID, MAX_NUMBER_OF_TYPES_PER_SERVICE, MAX_NUMBER_OF_URLS_PER_SERVICE, + MAX_PUBLIC_KEYS_PER_DID, MAX_SERVICE_ID_LENGTH, MAX_SERVICE_TYPE_LENGTH, MAX_SERVICE_URL_LENGTH, + MAX_TOTAL_KEY_AGREEMENT_KEYS, MAX_URL_LENGTH, }, KILT, MILLI_KILT, MIN_VESTED_TRANSFER_AMOUNT, SLOT_DURATION, }, From 359f9436945e66090f89c225caad93bd1d7357b7 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 15:14:28 +0200 Subject: [PATCH 22/45] chore: refactoring --- pallets/delegation/src/mock.rs | 1 - pallets/did/src/benchmarking.rs | 44 ++++++++++++++++++++++++---- pallets/did/src/lib.rs | 12 ++++---- pallets/did/src/service_endpoints.rs | 16 +++++----- 4 files changed, 54 insertions(+), 19 deletions(-) diff --git a/pallets/delegation/src/mock.rs b/pallets/delegation/src/mock.rs index b8c96d972..599bdae8f 100644 --- a/pallets/delegation/src/mock.rs +++ b/pallets/delegation/src/mock.rs @@ -25,7 +25,6 @@ use frame_support::{ }; use frame_system::EnsureSigned; use kilt_primitives::constants::delegation::DELEGATION_DEPOSIT; -use kilt_support::deposit::Deposit; use sp_core::{ed25519, sr25519, Pair, H256}; use sp_keystore::{testing::KeyStore, KeystoreExt}; use sp_runtime::{ diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index c21e5427b..1534bf80f 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -25,7 +25,7 @@ use kilt_support::signature::VerifySignature; use sp_core::{crypto::KeyTypeId, ecdsa, ed25519, sr25519}; use sp_io::crypto::{ecdsa_generate, ecdsa_sign, ed25519_generate, ed25519_sign, sr25519_generate, sr25519_sign}; use sp_runtime::{traits::IdentifyAccount, MultiSigner}; -use sp_std::{convert::TryInto, vec, vec::Vec}; +use sp_std::{convert::TryInto, vec::Vec}; use crate::{ did_details::*, @@ -166,6 +166,10 @@ benchmarks! { stored_did.attestation_key, Some(expected_attestation_key_id) ); + assert_eq!( + DidEndpointsCount::::get(&did_subject).unwrap_or_default().saturated_into::(), + service_endpoints.len() + ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), service_endpoints.len() @@ -226,6 +230,10 @@ benchmarks! { stored_did.attestation_key, Some(expected_attestation_key_id) ); + assert_eq!( + DidEndpointsCount::::get(&did_subject).unwrap_or_default().saturated_into::(), + service_endpoints.len() + ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), service_endpoints.len() @@ -286,6 +294,10 @@ benchmarks! { stored_did.attestation_key, Some(expected_attestation_key_id) ); + assert_eq!( + DidEndpointsCount::::get(&did_subject).unwrap_or_default().saturated_into::(), + service_endpoints.len() + ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), service_endpoints.len() @@ -316,6 +328,9 @@ benchmarks! { assert!( Did::::get(&did_subject).is_none() ); + assert!( + DidEndpointsCount::::get(&did_subject).is_none() + ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), 0 @@ -345,6 +360,9 @@ benchmarks! { assert!( Did::::get(&did_subject).is_none() ); + assert!( + DidEndpointsCount::::get(&did_subject).is_none() + ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), 0 @@ -845,6 +863,7 @@ benchmarks! { add_service_endpoint { let public_auth_key = get_ecdsa_public_authentication_key(); let did_subject: DidIdentifierOf = MultiSigner::from(public_auth_key.clone()).into_account().into(); + // Max allowed - 1. let old_service_endpoints = get_service_endpoints::( T::MaxNumberOfServicesPerDid::get() - 1, T::MaxServiceIdLength::get(), @@ -853,11 +872,15 @@ benchmarks! { T::MaxNumberOfUrlsPerService::get(), T::MaxServiceUrlLength::get(), ); - let new_service_endpoint = DidEndpointDetails { - id: b"id".to_vec().try_into().unwrap(), - service_type: vec![b"type".to_vec().try_into().unwrap()].try_into().unwrap(), - url: vec![b"url".to_vec().try_into().unwrap()].try_into().unwrap(), - }; + // New endpoint with max length and count for all the properties. + let new_service_endpoint = get_service_endpoints::( + 1, + T::MaxServiceIdLength::get(), + T::MaxNumberOfTypesPerService::get(), + T::MaxServiceTypeLength::get(), + T::MaxNumberOfUrlsPerService::get(), + T::MaxServiceUrlLength::get(), + )[0].clone(); let did_details = generate_base_did_details::(DidVerificationKey::from(public_auth_key)); Did::::insert(&did_subject, did_details); @@ -870,6 +893,10 @@ benchmarks! { ServiceEndpoints::::get(&did_subject, &new_service_endpoint.id), Some(new_service_endpoint) ); + assert_eq!( + DidEndpointsCount::::get(&did_subject).unwrap_or_default(), + T::MaxNumberOfServicesPerDid::get() + ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), T::MaxNumberOfServicesPerDid::get().saturated_into::() @@ -879,6 +906,7 @@ benchmarks! { remove_service_endpoint { let public_auth_key = get_ecdsa_public_authentication_key(); let did_subject: DidIdentifierOf = MultiSigner::from(public_auth_key.clone()).into_account().into(); + // All set to max. let old_service_endpoints = get_service_endpoints::( T::MaxNumberOfServicesPerDid::get(), T::MaxServiceIdLength::get(), @@ -899,6 +927,10 @@ benchmarks! { assert!( ServiceEndpoints::::get(&did_subject, &endpoint_id).is_none() ); + assert_eq!( + DidEndpointsCount::::get(&did_subject).unwrap_or_default(), + T::MaxNumberOfServicesPerDid::get() - 1 + ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), T::MaxNumberOfServicesPerDid::get().saturated_into::() - 1 diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index ad31310e8..1fbcca26a 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -137,7 +137,7 @@ use frame_support::{ }; use frame_system::ensure_signed; use sp_io::KillStorageResult; -use sp_runtime::{traits::Saturating, SaturatedConversion}; +use sp_runtime::{traits::{Saturating, Zero}, SaturatedConversion}; use sp_std::{boxed::Box, fmt::Debug, prelude::Clone}; #[cfg(feature = "runtime-benchmarks")] @@ -813,12 +813,11 @@ pub mod pallet { &did_subject, service_endpoint.id.clone(), |existing_service| -> Result<(), Error> { - ensure!(&existing_service.is_none(), Error::::ServiceAlreadyPresent); + ensure!(existing_service.is_none(), Error::::ServiceAlreadyPresent); *existing_service = Some(service_endpoint); Ok(()) }, )?; - DidEndpointsCount::::insert(&did_subject, currently_stored_endpoints_count.saturating_add(1)); Ok(()) @@ -833,10 +832,12 @@ pub mod pallet { Error::::ServiceNotPresent ); - // Decrease the endpoints counter or delete the entry if it reaches 0 + // *** No Fail beyond this point *** + + // Decrease the endpoints counter or delete the entry if it reaches 0. DidEndpointsCount::::mutate_exists(&did_subject, |existing_endpoint_count| { let new_value = existing_endpoint_count.unwrap_or_default().saturating_sub(1); - if new_value == 0u32 { + if new_value == u32::zero() { *existing_endpoint_count = None; } else { *existing_endpoint_count = Some(new_value); @@ -1102,6 +1103,7 @@ impl Pallet { let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; // *** No Fail beyond this point *** + let services_count = DidEndpointsCount::::take(&did_subject).unwrap_or_default(); let storage_kill_result = ServiceEndpoints::::remove_prefix(&did_subject, Some(services_count)); // If some items are remaining, it means that there were more than diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 274ebe125..ebe38055c 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -19,6 +19,7 @@ use crate::Config; use codec::{Decode, Encode}; use frame_support::BoundedVec; +use scale_info::TypeInfo; use sp_std::str; #[cfg(any(test, feature = "runtime-benchmarks"))] use sp_std::{convert::TryInto, vec::Vec}; @@ -27,13 +28,14 @@ use crate::utils as crate_utils; pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; -pub type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; -pub type ServiceEndpointTypeEntries = BoundedVec, ::MaxNumberOfTypesPerService>; +pub(crate) type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; +pub(crate) type ServiceEndpointTypeEntries = BoundedVec, ::MaxNumberOfTypesPerService>; -pub type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; -pub type ServiceEndpointUrlEntries = BoundedVec, ::MaxNumberOfUrlsPerService>; +pub(crate) type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; +pub(crate) type ServiceEndpointUrlEntries = BoundedVec, ::MaxNumberOfUrlsPerService>; -#[derive(Clone, Decode, Encode, PartialEq, Eq)] +#[derive(Clone, Decode, Encode, PartialEq, Eq, TypeInfo)] +#[scale_info(skip_type_params(T))] pub struct DidEndpointDetails { pub(crate) id: ServiceEndpointId, pub(crate) service_type: ServiceEndpointTypeEntries, @@ -84,13 +86,13 @@ pub mod utils { pub(crate) fn validate_new_service_endpoints( endpoints: &[DidEndpointDetails], ) -> Result<(), InputError> { - // Check if the maximum number of endpoints is provided + // Check if up the maximum number of endpoints is provided. ensure!( endpoints.len() <= T::MaxNumberOfServicesPerDid::get().saturated_into(), InputError::MaxServicesCountExceeded ); - // For each service... + // Then validate each service. endpoints .iter() .try_for_each(|endpoint| validate_single_service_endpoint_entry(endpoint))?; From 6f5196816e2cf3ed4cfc1e378f634e804865ee5d Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 16:42:41 +0200 Subject: [PATCH 23/45] chore: improving benchmarks --- pallets/did/src/benchmarking.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 1534bf80f..ee36b525f 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -102,6 +102,7 @@ fn generate_base_did_call_operation( } } + } benchmarks! { where_clause { where @@ -320,9 +321,7 @@ benchmarks! { ); Did::::insert(&did_subject, did_details); - for endpoint in service_endpoints.iter() { - ServiceEndpoints::::insert(&did_subject, &endpoint.id, endpoint.clone()); - } + save_service_endpoints(&did_subject, &service_endpoints); }: _(RawOrigin::Signed(did_subject.clone())) verify { assert!( @@ -352,9 +351,7 @@ benchmarks! { ); Did::::insert(&did_subject, did_details.clone()); - for endpoint in service_endpoints.iter() { - ServiceEndpoints::::insert(&did_subject, &endpoint.id, endpoint.clone()); - } + save_service_endpoints(&did_subject, &service_endpoints); }: _(RawOrigin::Signed(did_details.deposit.owner.clone()), did_subject.clone()) verify { assert!( @@ -873,7 +870,7 @@ benchmarks! { T::MaxServiceUrlLength::get(), ); // New endpoint with max length and count for all the properties. - let new_service_endpoint = get_service_endpoints::( + let mut new_service_endpoint = get_service_endpoints::( 1, T::MaxServiceIdLength::get(), T::MaxNumberOfTypesPerService::get(), @@ -881,12 +878,12 @@ benchmarks! { T::MaxNumberOfUrlsPerService::get(), T::MaxServiceUrlLength::get(), )[0].clone(); + // Changing from the default ID otherwise it would be the same as the one first one in `old_service_endpoints`. + new_service_endpoint.id = b"new_id".to_vec().try_into().unwrap(); let did_details = generate_base_did_details::(DidVerificationKey::from(public_auth_key)); Did::::insert(&did_subject, did_details); - for old_endpoint in old_service_endpoints.iter() { - ServiceEndpoints::::insert(&did_subject, &old_endpoint.id, old_endpoint.clone()); - } + save_service_endpoints(&did_subject, &old_service_endpoints); }: _(RawOrigin::Signed(did_subject.clone()), new_service_endpoint.clone()) verify { assert_eq!( @@ -919,9 +916,7 @@ benchmarks! { let did_details = generate_base_did_details::(DidVerificationKey::from(public_auth_key)); Did::::insert(&did_subject, did_details); - for old_endpoint in old_service_endpoints.iter() { - ServiceEndpoints::::insert(&did_subject, &old_endpoint.id, old_endpoint.clone()); - } + save_service_endpoints(&did_subject, &old_service_endpoints); }: _(RawOrigin::Signed(did_subject.clone()), endpoint_id.clone()) verify { assert!( From 6fdfba1f1b3f3c277b2545f664407fdcb0ad6d33 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 16:43:06 +0200 Subject: [PATCH 24/45] chore: improving benchmarks pt. 2 --- pallets/did/src/benchmarking.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index ee36b525f..6cf0d9349 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -102,7 +102,13 @@ fn generate_base_did_call_operation( } } +fn save_service_endpoints(did_subject: &DidIdentifierOf, endpoints: &[DidEndpointDetails]) { + for endpoint in endpoints.iter() { + ServiceEndpoints::::insert(&did_subject, &endpoint.id, endpoint.clone()); } + DidEndpointsCount::::insert(&did_subject, endpoints.len().saturated_into::()); +} + benchmarks! { where_clause { where From c00a3e944046fd6e41abe1ffc646c0e1afe96813 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 16:43:52 +0200 Subject: [PATCH 25/45] chore: improving benchmarks pt. 3 --- pallets/did/src/lib.rs | 3 +- pallets/did/src/service_endpoints.rs | 122 +++++++++++++-------------- 2 files changed, 62 insertions(+), 63 deletions(-) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 1fbcca26a..732b7e1dd 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -793,8 +793,7 @@ pub mod pallet { pub fn add_service_endpoint(origin: OriginFor, service_endpoint: DidEndpointDetails) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); - service_endpoints_utils::validate_single_service_endpoint_entry(&service_endpoint) - .map_err(Error::::from)?; + service_endpoint.validate_against_constraints().map_err(Error::::from)?; // Verify that the DID is present. ensure!(Did::::get(&did_subject).is_some(), Error::::DidNotPresent); diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index ebe38055c..8883011e4 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -18,11 +18,14 @@ use crate::Config; use codec::{Decode, Encode}; -use frame_support::BoundedVec; +use frame_support::{BoundedVec, ensure}; use scale_info::TypeInfo; use sp_std::str; #[cfg(any(test, feature = "runtime-benchmarks"))] use sp_std::{convert::TryInto, vec::Vec}; +use sp_runtime::traits::SaturatedConversion; +use crate::InputError; +use frame_support::traits::Get; use crate::utils as crate_utils; @@ -38,95 +41,46 @@ pub(crate) type ServiceEndpointUrlEntries = BoundedVec, #[scale_info(skip_type_params(T))] pub struct DidEndpointDetails { pub(crate) id: ServiceEndpointId, - pub(crate) service_type: ServiceEndpointTypeEntries, - pub(crate) url: ServiceEndpointUrlEntries, + pub(crate) service_types: ServiceEndpointTypeEntries, + pub(crate) urls: ServiceEndpointUrlEntries, } impl sp_std::fmt::Debug for DidEndpointDetails { fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result { f.debug_struct("DidEndpointDetails") .field("id", &self.id.clone().into_inner()) - .field("service_type", &self.service_type.encode()) - .field("url", &self.url.encode()) + .field("service_types", &self.service_types.encode()) + .field("urls", &self.urls.encode()) .finish() } } -#[cfg(any(test, feature = "runtime-benchmarks"))] impl DidEndpointDetails { - pub(crate) fn new(id: Vec, types: Vec>, urls: Vec>) -> Self { - let bounded_id = id.try_into().expect("Service ID too long."); - let bounded_types = types - .iter() - .map(|el| el.to_vec().try_into().expect("Service type too long.")) - .collect::>>() - .try_into() - .expect("Too many types for the given service."); - let bounded_urls = urls - .iter() - .map(|el| el.to_vec().try_into().expect("Service URL too long.")) - .collect::>>() - .try_into() - .expect("Too many URLs for the given service."); - - Self { - id: bounded_id, - service_type: bounded_types, - url: bounded_urls, - } - } -} - -pub mod utils { - use super::*; - use crate::InputError; - use frame_support::{ensure, traits::Get}; - use sp_runtime::traits::SaturatedConversion; - - pub(crate) fn validate_new_service_endpoints( - endpoints: &[DidEndpointDetails], - ) -> Result<(), InputError> { - // Check if up the maximum number of endpoints is provided. - ensure!( - endpoints.len() <= T::MaxNumberOfServicesPerDid::get().saturated_into(), - InputError::MaxServicesCountExceeded - ); - - // Then validate each service. - endpoints - .iter() - .try_for_each(|endpoint| validate_single_service_endpoint_entry(endpoint))?; - - Ok(()) - } - - pub(crate) fn validate_single_service_endpoint_entry( - endpoint: &DidEndpointDetails, - ) -> Result<(), InputError> { + pub(crate) fn validate_against_constraints(&self) -> Result<(), InputError> { // Check that the maximum number of service types is provided. ensure!( - endpoint.service_type.len() <= T::MaxNumberOfTypesPerService::get().saturated_into(), + self.service_types.len() <= T::MaxNumberOfTypesPerService::get().saturated_into(), InputError::MaxTypeCountExceeded ); // Check that the maximum number of URLs is provided. ensure!( - endpoint.url.len() <= T::MaxNumberOfUrlsPerService::get().saturated_into(), + self.urls.len() <= T::MaxNumberOfUrlsPerService::get().saturated_into(), InputError::MaxUrlCountExceeded ); // Check that the ID is the maximum allowed length and only contain ASCII // characters. ensure!( - endpoint.id.len() <= T::MaxServiceIdLength::get().saturated_into(), + self.id.len() <= T::MaxServiceIdLength::get().saturated_into(), InputError::MaxIdLengthExceeded ); - let str_id = str::from_utf8(&endpoint.id).map_err(|_| InputError::InvalidUrlEncoding)?; + let str_id = str::from_utf8(&self.id).map_err(|_| InputError::InvalidUrlEncoding)?; ensure!( crate_utils::is_valid_ascii_string(str_id), InputError::InvalidUrlEncoding ); // Check that all types are the maximum allowed length and only contain ASCII // characters. - endpoint.service_type.iter().try_for_each(|s_type| { + self.service_types.iter().try_for_each(|s_type| { ensure!( s_type.len() <= T::MaxServiceTypeLength::get().saturated_into(), InputError::MaxTypeLengthExceeded @@ -140,7 +94,7 @@ pub mod utils { })?; // Check that all URLs are the maximum allowed length AND only contain ASCII // characters. - endpoint.url.iter().try_for_each(|s_url| { + self.urls.iter().try_for_each(|s_url| { ensure!( s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), InputError::MaxUrlLengthExceeded @@ -155,3 +109,49 @@ pub mod utils { Ok(()) } } + +#[cfg(any(test, feature = "runtime-benchmarks"))] +impl DidEndpointDetails { + pub(crate) fn new(id: Vec, types: Vec>, urls: Vec>) -> Self { + let bounded_id = id.try_into().expect("Service ID too long."); + let bounded_types = types + .iter() + .map(|el| el.to_vec().try_into().expect("Service type too long.")) + .collect::>>() + .try_into() + .expect("Too many types for the given service."); + let bounded_urls = urls + .iter() + .map(|el| el.to_vec().try_into().expect("Service URL too long.")) + .collect::>>() + .try_into() + .expect("Too many URLs for the given service."); + + Self { + id: bounded_id, + service_types: bounded_types, + urls: bounded_urls, + } + } +} + +pub mod utils { + use super::*; + + pub(crate) fn validate_new_service_endpoints( + endpoints: &[DidEndpointDetails], + ) -> Result<(), InputError> { + // Check if up the maximum number of endpoints is provided. + ensure!( + endpoints.len() <= T::MaxNumberOfServicesPerDid::get().saturated_into(), + InputError::MaxServicesCountExceeded + ); + + // Then validate each service. + endpoints + .iter() + .try_for_each(|endpoint| endpoint.validate_against_constraints())?; + + Ok(()) + } +} From 0bbe2215deb9828cf6f272766093705ca8df8563 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 16:51:41 +0200 Subject: [PATCH 26/45] fix: Box DidCreationDetails to stay within call limits --- pallets/did/src/benchmarking.rs | 6 ++-- pallets/did/src/lib.rs | 4 +-- pallets/did/src/tests.rs | 52 ++++++++++++++++----------------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 6cf0d9349..f015d9f62 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -148,7 +148,7 @@ benchmarks! { did_creation_details.new_service_details = service_endpoints.clone(); let did_creation_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature."); - }: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature)) + }: create(RawOrigin::Signed(submitter), Box::new(did_creation_details.clone()), DidSignature::from(did_creation_signature)) verify { let stored_did = Did::::get(&did_subject).expect("New DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.key_agreement_keys; @@ -212,7 +212,7 @@ benchmarks! { did_creation_details.new_service_details = service_endpoints.clone(); let did_creation_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature."); - }: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature)) + }: create(RawOrigin::Signed(submitter), Box::new(did_creation_details.clone()), DidSignature::from(did_creation_signature)) verify { let stored_did = Did::::get(&did_subject).expect("New DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.key_agreement_keys; @@ -276,7 +276,7 @@ benchmarks! { did_creation_details.new_service_details = service_endpoints.clone(); let did_creation_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature."); - }: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature)) + }: create(RawOrigin::Signed(submitter), Box::new(did_creation_details.clone()), DidSignature::from(did_creation_signature)) verify { let stored_did = Did::::get(&did_subject).expect("New DID should be stored on chain."); let stored_key_agreement_keys_ids = stored_did.key_agreement_keys; diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 732b7e1dd..84389931d 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -499,7 +499,7 @@ pub mod pallet { ed25519_weight.max(sr25519_weight).max(ecdsa_weight) })] - pub fn create(origin: OriginFor, details: DidCreationDetails, signature: DidSignature) -> DispatchResult { + pub fn create(origin: OriginFor, details: Box>, signature: DidSignature) -> DispatchResult { let sender = ensure_signed(origin)?; ensure!(sender == details.submitter, BadOrigin); @@ -535,7 +535,7 @@ pub mod pallet { .map_err(Error::::from)?; let did_entry = - DidDetails::from_creation_details(details, account_did_auth_key).map_err(Error::::from)?; + DidDetails::from_creation_details(*details, account_did_auth_key).map_err(Error::::from)?; // *** No Fail beyond this call *** CurrencyOf::::reserve(&did_entry.deposit.owner, did_entry.deposit.amount)?; diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index d2f32b05d..9eaff341b 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -49,7 +49,7 @@ fn check_successful_simple_ed25519_creation() { .execute_with(|| { assert_ok!(Did::create( Origin::signed(ACCOUNT_00), - details, + Box::new(details), did::DidSignature::from(signature), )); let stored_did = Did::get_did(&alice_did).expect("ALICE_DID should be present on chain."); @@ -91,7 +91,7 @@ fn check_successful_simple_sr25519_creation() { .execute_with(|| { assert_ok!(Did::create( Origin::signed(ACCOUNT_00), - details, + Box::new(details), did::DidSignature::from(signature), )); let stored_did = Did::get_did(&alice_did).expect("ALICE_DID should be present on chain."); @@ -133,7 +133,7 @@ fn check_successful_simple_ecdsa_creation() { .execute_with(|| { assert_ok!(Did::create( Origin::signed(ACCOUNT_00), - details, + Box::new(details), did::DidSignature::from(signature), )); let stored_did = Did::get_did(&alice_did).expect("ALICE_DID should be present on chain."); @@ -194,7 +194,7 @@ fn check_successful_complete_creation() { .execute_with(|| { assert_ok!(Did::create( Origin::signed(ACCOUNT_00), - details.clone(), + Box::new(details.clone()), did::DidSignature::from(signature), )); @@ -241,8 +241,8 @@ fn check_successful_complete_creation() { let stored_service = Did::get_service_endpoints(&alice_did, &new_service.id) .expect("Service endpoint should be stored."); assert_eq!(stored_service.id, new_service.id); - assert_eq!(stored_service.url, new_service.url); - assert_eq!(stored_service.service_type, new_service.service_type); + assert_eq!(stored_service.urls, new_service.urls); + assert_eq!(stored_service.service_types, new_service.service_types); }); // ... and that the number of elements in the creation operation is the same as // the number of elements stored in `ServiceEndpoints` and `DidEndpointsCount`. @@ -284,7 +284,7 @@ fn check_duplicate_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::DidAlreadyPresent ); }); @@ -311,7 +311,7 @@ fn check_unauthorised_submitter_did_creation_error() { .execute_with(|| { assert_noop!( // Use ACCOUNT_00 to submit the transaction - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), BadOrigin ); }); @@ -327,7 +327,7 @@ fn create_fail_insufficient_balance() { ExtBuilder::default().build(None).execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::UnableToPayFees ); }); @@ -350,7 +350,7 @@ fn check_did_already_deleted_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::DidAlreadyDeleted ); }); @@ -375,7 +375,7 @@ fn check_invalid_signature_format_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::InvalidSignature ); }); @@ -398,7 +398,7 @@ fn check_invalid_signature_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::InvalidSignature ); }); @@ -421,7 +421,7 @@ fn check_swapped_did_subject_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::InvalidSignature ); }); @@ -447,7 +447,7 @@ fn check_max_limit_key_agreement_keys_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::MaxKeyAgreementKeysLimitExceeded ); }); @@ -477,7 +477,7 @@ fn check_max_limit_service_endpoints_count_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::MaxNumberOfServicesPerDidExceeded ); }); @@ -502,7 +502,7 @@ fn check_max_limit_service_id_length_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::MaxServiceIdLengthExceeded ); }); @@ -533,7 +533,7 @@ fn check_max_limit_service_type_count_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::MaxNumberOfTypesPerServiceExceeded ); }); @@ -558,7 +558,7 @@ fn check_max_limit_service_type_length_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::MaxServiceTypeLengthExceeded ); }); @@ -589,7 +589,7 @@ fn check_max_limit_service_url_count_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::MaxNumberOfUrlsPerServiceExceeded ); }); @@ -614,7 +614,7 @@ fn check_max_limit_service_url_length_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::MaxServiceUrlLengthExceeded ); }); @@ -639,7 +639,7 @@ fn check_invalid_service_id_character_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::InvalidServiceEncoding ); }); @@ -664,7 +664,7 @@ fn check_invalid_service_type_character_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::InvalidServiceEncoding ); }); @@ -689,7 +689,7 @@ fn check_invalid_service_url_character_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), details, did::DidSignature::from(signature)), + Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), did::Error::::InvalidServiceEncoding ); }); @@ -2051,7 +2051,7 @@ fn check_successful_deletion_no_endpoints() { assert_noop!( Did::create( Origin::signed(ACCOUNT_00.clone()), - details, + Box::new(details), did::DidSignature::from(signature), ), did::Error::::DidAlreadyDeleted @@ -2102,7 +2102,7 @@ fn check_successful_deletion_with_endpoints() { assert_noop!( Did::create( Origin::signed(ACCOUNT_00.clone()), - details, + Box::new(details), did::DidSignature::from(signature), ), did::Error::::DidAlreadyDeleted @@ -2178,7 +2178,7 @@ fn check_successful_reclaiming() { assert_noop!( Did::create( Origin::signed(ACCOUNT_00.clone()), - details, + Box::new(details), did::DidSignature::from(signature), ), did::Error::::DidAlreadyDeleted From 5d74ee0bc2df697cb42731c0fda3ab5f45af70a2 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 17:02:10 +0200 Subject: [PATCH 27/45] bench: update default benchmarks --- pallets/did/src/default_weights.rs | 192 ++++++++++++++--------------- 1 file changed, 94 insertions(+), 98 deletions(-) diff --git a/pallets/did/src/default_weights.rs b/pallets/did/src/default_weights.rs index b01c73906..373963952 100644 --- a/pallets/did/src/default_weights.rs +++ b/pallets/did/src/default_weights.rs @@ -86,187 +86,185 @@ pub trait WeightInfo { pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { fn create_ed25519_keys(_n: u32, c: u32, ) -> Weight { - (145_699_000_u64) - // Standard Error: 55_000 - .saturating_add((8_183_000_u64).saturating_mul(c as Weight)) + (244_709_000_u64) + // Standard Error: 1_158_000 + .saturating_add((17_875_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { - (132_311_000_u64) - // Standard Error: 109_000 - .saturating_add((1_618_000_u64).saturating_mul(n as Weight)) - // Standard Error: 40_000 - .saturating_add((8_640_000_u64).saturating_mul(c as Weight)) + fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { + (283_812_000_u64) + // Standard Error: 823_000 + .saturating_add((11_361_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (228_832_000_u64) - // Standard Error: 90_000 - .saturating_add((1_894_000_u64).saturating_mul(n as Weight)) - // Standard Error: 33_000 - .saturating_add((8_065_000_u64).saturating_mul(c as Weight)) + (99_494_000_u64) + // Standard Error: 2_670_000 + .saturating_add((26_003_000_u64).saturating_mul(n as Weight)) + // Standard Error: 1_001_000 + .saturating_add((17_883_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (46_988_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) + (109_997_000_u64) + .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(28_u64)) } fn reclaim_deposit() -> Weight { - (50_956_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) + (124_754_000_u64) + .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(28_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (71_013_000_u64) + (144_912_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (73_658_000_u64) + (153_609_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (174_668_000_u64) + (315_673_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (39_094_000_u64) + (103_966_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (39_103_000_u64) + (107_131_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (39_113_000_u64) + (85_911_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (38_643_000_u64) + (52_219_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (38_653_000_u64) + (99_697_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (39_033_000_u64) + (93_135_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (35_677_000_u64) + (44_574_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (35_627_000_u64) + (50_204_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (35_828_000_u64) + (72_196_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (38_943_000_u64) + (63_349_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (38_562_000_u64) + (85_310_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (39_264_000_u64) + (102_342_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (35_847_000_u64) + (66_665_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (35_808_000_u64) + (80_421_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (35_997_000_u64) + (107_252_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (38_363_000_u64) + (101_591_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (38_312_000_u64) + (85_590_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (38_613_000_u64) + (47_349_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (36_048_000_u64) + (57_137_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (36_398_000_u64) + (61_966_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (36_719_000_u64) + (62_547_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (20_929_000_u64) + (40_877_000_u64) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn remove_service_endpoint() -> Weight { - (13_756_000_u64) + (28_754_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (61_127_000_u64) + (135_911_000_u64) // Standard Error: 0 - .saturating_add((4_000_u64).saturating_mul(l as Weight)) + .saturating_add((5_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (58_737_000_u64) + (133_337_000_u64) // Standard Error: 0 - .saturating_add((1_000_u64).saturating_mul(l as Weight)) + .saturating_add((2_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (160_746_000_u64) + (278_287_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) @@ -276,187 +274,185 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { fn create_ed25519_keys(_n: u32, c: u32, ) -> Weight { - (145_699_000_u64) - // Standard Error: 55_000 - .saturating_add((8_183_000_u64).saturating_mul(c as Weight)) + (244_709_000_u64) + // Standard Error: 1_158_000 + .saturating_add((17_875_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { - (132_311_000_u64) - // Standard Error: 109_000 - .saturating_add((1_618_000_u64).saturating_mul(n as Weight)) - // Standard Error: 40_000 - .saturating_add((8_640_000_u64).saturating_mul(c as Weight)) + fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { + (283_812_000_u64) + // Standard Error: 823_000 + .saturating_add((11_361_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (228_832_000_u64) - // Standard Error: 90_000 - .saturating_add((1_894_000_u64).saturating_mul(n as Weight)) - // Standard Error: 33_000 - .saturating_add((8_065_000_u64).saturating_mul(c as Weight)) + (99_494_000_u64) + // Standard Error: 2_670_000 + .saturating_add((26_003_000_u64).saturating_mul(n as Weight)) + // Standard Error: 1_001_000 + .saturating_add((17_883_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete() -> Weight { - (46_988_000_u64) - .saturating_add(RocksDbWeight::get().reads(1_u64)) + (109_997_000_u64) + .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(28_u64)) } fn reclaim_deposit() -> Weight { - (50_956_000_u64) - .saturating_add(RocksDbWeight::get().reads(1_u64)) + (124_754_000_u64) + .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(28_u64)) } fn submit_did_call_ed25519_key() -> Weight { - (71_013_000_u64) + (144_912_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (73_658_000_u64) + (153_609_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (174_668_000_u64) + (315_673_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (39_094_000_u64) + (103_966_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (39_103_000_u64) + (107_131_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (39_113_000_u64) + (85_911_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (38_643_000_u64) + (52_219_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (38_653_000_u64) + (99_697_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (39_033_000_u64) + (93_135_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (35_677_000_u64) + (44_574_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (35_627_000_u64) + (50_204_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (35_828_000_u64) + (72_196_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (38_943_000_u64) + (63_349_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (38_562_000_u64) + (85_310_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (39_264_000_u64) + (102_342_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (35_847_000_u64) + (66_665_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (35_808_000_u64) + (80_421_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (35_997_000_u64) + (107_252_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (38_363_000_u64) + (101_591_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (38_312_000_u64) + (85_590_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (38_613_000_u64) + (47_349_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (36_048_000_u64) + (57_137_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (36_398_000_u64) + (61_966_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (36_719_000_u64) + (62_547_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (20_929_000_u64) + (40_877_000_u64) .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn remove_service_endpoint() -> Weight { - (13_756_000_u64) + (28_754_000_u64) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (61_127_000_u64) + (135_911_000_u64) // Standard Error: 0 - .saturating_add((4_000_u64).saturating_mul(l as Weight)) + .saturating_add((5_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (58_737_000_u64) + (133_337_000_u64) // Standard Error: 0 - .saturating_add((1_000_u64).saturating_mul(l as Weight)) + .saturating_add((2_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (160_746_000_u64) + (278_287_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) From 9a81ca24e358a65dbd01fca8e0e25f674f928ea2 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 22 Oct 2021 17:20:41 +0200 Subject: [PATCH 28/45] chore: comments --- pallets/did/src/did_details.rs | 1 + pallets/did/src/errors.rs | 9 +- pallets/did/src/lib.rs | 120 ++++++++++++++++++--------- pallets/did/src/service_endpoints.rs | 44 +++++----- pallets/did/src/tests.rs | 102 +++++++++++++++++++---- 5 files changed, 199 insertions(+), 77 deletions(-) diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index a6da032c9..bffbb8ce6 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -556,6 +556,7 @@ pub struct DidCreationDetails { pub new_attestation_key: Option, /// \[OPTIONAL\] The new delegation key. pub new_delegation_key: Option, + /// The service endpoints details. pub new_service_details: Vec>, } diff --git a/pallets/did/src/errors.rs b/pallets/did/src/errors.rs index 856b086a4..f429be940 100644 --- a/pallets/did/src/errors.rs +++ b/pallets/did/src/errors.rs @@ -87,11 +87,18 @@ pub enum InputError { /// A number of new key agreement keys greater than the maximum allowed has /// been provided. MaxKeyAgreementKeysLimitExceeded, + /// The maximum number of service endpoints for a DID has been exceeded. MaxServicesCountExceeded, + /// The maximum number of URLs for a service endpoint has been exceeded. MaxUrlCountExceeded, + /// The maximum number of types for a service endpoint has been exceeded. MaxTypeCountExceeded, + /// The service endpoint ID exceeded the maximum allowed length. MaxIdLengthExceeded, + /// One of the service endpoint URLs exceeded the maximum allowed length. MaxUrlLengthExceeded, + /// One of the service endpoint types exceeded the maximum allowed length. MaxTypeLengthExceeded, - InvalidUrlEncoding, + /// One of the service endpoint details contains non-ASCII characters. + InvalidEncoding, } diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 84389931d..906b6857a 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -60,34 +60,14 @@ //! to any past attestation key that has been rotated but not entirely //! revoked. //! +//! - A set of **service endpoints**: pointing to the description of the +//! services the DID subject exposes. For more information, check the W3C DID +//! Core specification. +//! //! - A **transaction counter**: acts as a nonce to avoid replay or signature //! forgery attacks. Each time a DID-signed transaction is executed, the //! counter is incremented. //! -//! ## Interface -//! -//! ### Dispatchable Functions -//! -//! - `create` - Register a new DID on the KILT blockchain under the given DID -//! identifier. -//! - `set_authentication_key` - Replace the authentication key of a DID. -//! - `set_delegation_key` - Set the delegation key of a DID. -//! - `remove_delegation_key` - Remove the delegation key of a DID, if present. -//! - `set_attestation_key` - Set the attestation key of a DID. -//! - `remove_attestation_key` - Remove the attestation key of a DID, if -//! present. -//! - `add_key_agreement_key` - Add a new key agreement key to the set of keys -//! for the DID. -//! - `remove_key_agreement_key` - Remove the key with the given ID from the set -//! of key agreement keys, if present. -//! - `delete` - Delete the specified DID and all related keys from the KILT -//! blockchain. A DID with the same identifier cannot be created anymore after -//! it is deleted. -//! - `submit_did_call` - Proxy a dispatchable function for an extrinsic that -//! expects a DID origin. The DID pallet verifies the signature and the nonce -//! of the wrapping operation and then dispatches the underlying extrinsic -//! upon successful verification. -//! //! ## Assumptions //! //! - The maximum number of new key agreement keys that can be specified in a @@ -137,7 +117,10 @@ use frame_support::{ }; use frame_system::ensure_signed; use sp_io::KillStorageResult; -use sp_runtime::{traits::{Saturating, Zero}, SaturatedConversion}; +use sp_runtime::{ + traits::{Saturating, Zero}, + SaturatedConversion, +}; use sp_std::{boxed::Box, fmt::Debug, prelude::Clone}; #[cfg(feature = "runtime-benchmarks")] @@ -250,26 +233,32 @@ pub mod pallet { #[pallet::constant] type MaxBlocksTxValidity: Get>; - /// Weight information for extrinsics in this pallet. - type WeightInfo: WeightInfo; - + /// The maximum number of services that can be stored under a DID. #[pallet::constant] type MaxNumberOfServicesPerDid: Get; + /// The maximum length of a service ID. #[pallet::constant] type MaxServiceIdLength: Get; + /// The maximum length of a service type description. #[pallet::constant] type MaxServiceTypeLength: Get; + /// The maximum number of a types description for a service endpoint. #[pallet::constant] type MaxNumberOfTypesPerService: Get; + /// The maximum length of a service URL. #[pallet::constant] type MaxServiceUrlLength: Get; + /// The maximum number of a URLs for a service endpoint. #[pallet::constant] type MaxNumberOfUrlsPerService: Get; + + /// Weight information for extrinsics in this pallet. + type WeightInfo: WeightInfo; } #[pallet::pallet] @@ -314,6 +303,9 @@ pub mod pallet { DidEndpointDetails, >; + /// Counter of service endpoints for each DID. + /// + /// It maps from (DID identifier) to a 32-bit counter. #[pallet::storage] pub(crate) type DidEndpointsCount = StorageMap<_, Blake2_128Concat, DidIdentifierOf, u32>; @@ -386,14 +378,26 @@ pub mod pallet { NotOwnerOfDeposit, /// The origin is unable to reserve the deposit and pay the fee. UnableToPayFees, + /// The maximum number of service endpoints for a DID has been exceeded. MaxNumberOfServicesPerDidExceeded, + /// The service endpoint ID exceeded the maximum allowed length. MaxServiceIdLengthExceeded, + /// One of the service endpoint types exceeded the maximum allowed + /// length. MaxServiceTypeLengthExceeded, + /// The maximum number of types for a service endpoint has been + /// exceeded. MaxNumberOfTypesPerServiceExceeded, + /// One of the service endpoint URLs exceeded the maximum allowed + /// length. MaxServiceUrlLengthExceeded, + /// The maximum number of URLs for a service endpoint has been exceeded. MaxNumberOfUrlsPerServiceExceeded, + /// A service with the provided ID is already present for the given DID. ServiceAlreadyPresent, + /// A service with the provided ID is not present under the given DID. ServiceNotPresent, + /// One of the service endpoint details contains non-ASCII characters. InvalidServiceEncoding, /// An error that is not supposed to take place, yet it happened. InternalError, @@ -444,7 +448,7 @@ pub mod pallet { InputError::MaxTypeLengthExceeded => Self::MaxServiceTypeLengthExceeded, InputError::MaxUrlCountExceeded => Self::MaxNumberOfUrlsPerServiceExceeded, InputError::MaxUrlLengthExceeded => Self::MaxServiceUrlLengthExceeded, - InputError::InvalidUrlEncoding => Self::InvalidServiceEncoding, + InputError::InvalidEncoding => Self::InvalidServiceEncoding, } } } @@ -472,12 +476,15 @@ pub mod pallet { /// /// # /// - The transaction's complexity is mainly dependent on the number of - /// new key agreement keys included in the operation. + /// new key agreement keys and the number of new service endpoints + /// included in the operation. /// --------- - /// Weight: O(K) where K is the number of new key agreement - /// keys bounded by `MaxNewKeyAgreementKeys`. - /// - Reads: [Origin Account], Did - /// - Writes: Did (with K new key agreement keys) + /// Weight: O(K) + O(N) where K is the number of new key agreement + /// keys bounded by `MaxNewKeyAgreementKeys`, while N is the number of + /// new service endpoints bounded by `MaxNumberOfServicesPerDid`. + /// - Reads: [Origin Account], Did, DidBlacklist + /// - Writes: Did (with K new key agreement keys), ServiceEndpoints + /// (with N new service endpoints), DidEndpointsCount /// # #[pallet::weight({ let new_key_agreement_keys = details.new_key_agreement_keys.len().saturated_into::(); @@ -499,7 +506,11 @@ pub mod pallet { ed25519_weight.max(sr25519_weight).max(ecdsa_weight) })] - pub fn create(origin: OriginFor, details: Box>, signature: DidSignature) -> DispatchResult { + pub fn create( + origin: OriginFor, + details: Box>, + signature: DidSignature, + ) -> DispatchResult { let sender = ensure_signed(origin)?; ensure!(sender == details.submitter, BadOrigin); @@ -554,11 +565,9 @@ pub mod pallet { T::FeeCollector::on_unbalanced(imbalance); Did::::insert(&did_identifier, did_entry); - input_service_endpoints.iter().for_each(|service| { ServiceEndpoints::::insert(&did_identifier, &service.id, service.clone()); }); - DidEndpointsCount::::insert(&did_identifier, input_service_endpoints.len().saturated_into::()); Self::deposit_event(Event::DidCreated(sender, did_identifier)); @@ -789,11 +798,25 @@ pub mod pallet { Ok(()) } + /// Add a new service endpoint under the given DID. + /// + /// The dispatch origin must be a DID origin proxied via the + /// `submit_did_call` extrinsic. + /// + /// Emits `DidUpdated`. + /// + /// # + /// Weight: O(1) + /// - Reads: [Origin Account], Did, ServiceEndpoints, DidEndpointsCount + /// - Writes: Did, ServiceEndpoints, DidEndpointsCount + /// # #[pallet::weight(::WeightInfo::add_service_endpoint())] pub fn add_service_endpoint(origin: OriginFor, service_endpoint: DidEndpointDetails) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); - service_endpoint.validate_against_constraints().map_err(Error::::from)?; + service_endpoint + .validate_against_constraints() + .map_err(Error::::from)?; // Verify that the DID is present. ensure!(Did::::get(&did_subject).is_some(), Error::::DidNotPresent); @@ -819,9 +842,23 @@ pub mod pallet { )?; DidEndpointsCount::::insert(&did_subject, currently_stored_endpoints_count.saturating_add(1)); + Self::deposit_event(Event::DidUpdated(did_subject)); + Ok(()) } + /// Remove the service with the provided ID from the DID. + /// + /// The dispatch origin must be a DID origin proxied via the + /// `submit_did_call` extrinsic. + /// + /// Emits `DidUpdated`. + /// + /// # + /// Weight: O(1) + /// - Reads: [Origin Account], ServiceEndpoints, DidEndpointsCount + /// - Writes: Did, ServiceEndpoints, DidEndpointsCount + /// # #[pallet::weight(::WeightInfo::remove_service_endpoint())] pub fn remove_service_endpoint(origin: OriginFor, service_id: ServiceEndpointId) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); @@ -843,6 +880,8 @@ pub mod pallet { } }); + Self::deposit_event(Event::DidUpdated(did_subject)); + Ok(()) } @@ -1095,8 +1134,9 @@ impl Pallet { .map_err(DidError::SignatureError) } - /// Deletes DID details from storage, adds the identifier to the blacklisted - /// DIDs and frees the deposit. + /// Deletes DID details from storage, including its linked service + /// endpoints, adds the identifier to the blacklisted DIDs and frees the + /// deposit. fn delete_did(did_subject: DidIdentifierOf) -> DispatchResult { // `take` calls `kill` internally let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 8883011e4..63e9bf946 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -16,32 +16,42 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use crate::Config; +use crate::{Config, InputError}; use codec::{Decode, Encode}; -use frame_support::{BoundedVec, ensure}; +use frame_support::{ensure, traits::Get, BoundedVec}; use scale_info::TypeInfo; +use sp_runtime::traits::SaturatedConversion; use sp_std::str; #[cfg(any(test, feature = "runtime-benchmarks"))] use sp_std::{convert::TryInto, vec::Vec}; -use sp_runtime::traits::SaturatedConversion; -use crate::InputError; -use frame_support::traits::Get; use crate::utils as crate_utils; +/// A bounded vector of bytes for a service endpoint ID. pub type ServiceEndpointId = BoundedVec::MaxServiceIdLength>; +/// A bounded vectors of bytes for a service endpoint type. pub(crate) type ServiceEndpointType = BoundedVec::MaxServiceTypeLength>; -pub(crate) type ServiceEndpointTypeEntries = BoundedVec, ::MaxNumberOfTypesPerService>; +/// A bounded vector of [ServiceEndpointType]s. +pub(crate) type ServiceEndpointTypeEntries = + BoundedVec, ::MaxNumberOfTypesPerService>; +/// A bounded vectors of bytes for a service endpoint URL. pub(crate) type ServiceEndpointUrl = BoundedVec::MaxServiceUrlLength>; -pub(crate) type ServiceEndpointUrlEntries = BoundedVec, ::MaxNumberOfUrlsPerService>; +/// A bounded vector of [ServiceEndpointUrl]s. +pub(crate) type ServiceEndpointUrlEntries = + BoundedVec, ::MaxNumberOfUrlsPerService>; +/// A single service endpoint description. #[derive(Clone, Decode, Encode, PartialEq, Eq, TypeInfo)] #[scale_info(skip_type_params(T))] pub struct DidEndpointDetails { + /// The ID of the service endpoint. Allows the endpoint to be queried and + /// resolved directly. pub(crate) id: ServiceEndpointId, + /// A vector of types description for the service. pub(crate) service_types: ServiceEndpointTypeEntries, + /// A vector of URLs the service points to. pub(crate) urls: ServiceEndpointUrlEntries, } @@ -56,6 +66,8 @@ impl sp_std::fmt::Debug for DidEndpointDetails { } impl DidEndpointDetails { + /// Validates a given [DidEndpointDetails] instance against the constraint + /// set in the pallet's [Config]. pub(crate) fn validate_against_constraints(&self) -> Result<(), InputError> { // Check that the maximum number of service types is provided. ensure!( @@ -73,11 +85,8 @@ impl DidEndpointDetails { self.id.len() <= T::MaxServiceIdLength::get().saturated_into(), InputError::MaxIdLengthExceeded ); - let str_id = str::from_utf8(&self.id).map_err(|_| InputError::InvalidUrlEncoding)?; - ensure!( - crate_utils::is_valid_ascii_string(str_id), - InputError::InvalidUrlEncoding - ); + let str_id = str::from_utf8(&self.id).map_err(|_| InputError::InvalidEncoding)?; + ensure!(crate_utils::is_valid_ascii_string(str_id), InputError::InvalidEncoding); // Check that all types are the maximum allowed length and only contain ASCII // characters. self.service_types.iter().try_for_each(|s_type| { @@ -85,10 +94,10 @@ impl DidEndpointDetails { s_type.len() <= T::MaxServiceTypeLength::get().saturated_into(), InputError::MaxTypeLengthExceeded ); - let str_type = str::from_utf8(s_type).map_err(|_| InputError::InvalidUrlEncoding)?; + let str_type = str::from_utf8(s_type).map_err(|_| InputError::InvalidEncoding)?; ensure!( crate_utils::is_valid_ascii_string(str_type), - InputError::InvalidUrlEncoding + InputError::InvalidEncoding ); Ok(()) })?; @@ -99,11 +108,8 @@ impl DidEndpointDetails { s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), InputError::MaxUrlLengthExceeded ); - let str_url = str::from_utf8(s_url).map_err(|_| InputError::InvalidUrlEncoding)?; - ensure!( - crate_utils::is_valid_ascii_string(str_url), - InputError::InvalidUrlEncoding - ); + let str_url = str::from_utf8(s_url).map_err(|_| InputError::InvalidEncoding)?; + ensure!(crate_utils::is_valid_ascii_string(str_url), InputError::InvalidEncoding); Ok(()) })?; Ok(()) diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 9eaff341b..cc545dac3 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -284,7 +284,11 @@ fn check_duplicate_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::DidAlreadyPresent ); }); @@ -311,7 +315,11 @@ fn check_unauthorised_submitter_did_creation_error() { .execute_with(|| { assert_noop!( // Use ACCOUNT_00 to submit the transaction - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), BadOrigin ); }); @@ -327,7 +335,11 @@ fn create_fail_insufficient_balance() { ExtBuilder::default().build(None).execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::UnableToPayFees ); }); @@ -350,7 +362,11 @@ fn check_did_already_deleted_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::DidAlreadyDeleted ); }); @@ -375,7 +391,11 @@ fn check_invalid_signature_format_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::InvalidSignature ); }); @@ -398,7 +418,11 @@ fn check_invalid_signature_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::InvalidSignature ); }); @@ -421,7 +445,11 @@ fn check_swapped_did_subject_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::InvalidSignature ); }); @@ -447,7 +475,11 @@ fn check_max_limit_key_agreement_keys_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::MaxKeyAgreementKeysLimitExceeded ); }); @@ -477,7 +509,11 @@ fn check_max_limit_service_endpoints_count_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::MaxNumberOfServicesPerDidExceeded ); }); @@ -502,7 +538,11 @@ fn check_max_limit_service_id_length_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::MaxServiceIdLengthExceeded ); }); @@ -533,7 +573,11 @@ fn check_max_limit_service_type_count_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::MaxNumberOfTypesPerServiceExceeded ); }); @@ -558,7 +602,11 @@ fn check_max_limit_service_type_length_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::MaxServiceTypeLengthExceeded ); }); @@ -589,7 +637,11 @@ fn check_max_limit_service_url_count_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::MaxNumberOfUrlsPerServiceExceeded ); }); @@ -614,7 +666,11 @@ fn check_max_limit_service_url_length_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::MaxServiceUrlLengthExceeded ); }); @@ -639,7 +695,11 @@ fn check_invalid_service_id_character_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::InvalidServiceEncoding ); }); @@ -664,7 +724,11 @@ fn check_invalid_service_type_character_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::InvalidServiceEncoding ); }); @@ -689,7 +753,11 @@ fn check_invalid_service_url_character_did_creation() { .build(None) .execute_with(|| { assert_noop!( - Did::create(Origin::signed(ACCOUNT_00), Box::new(details), did::DidSignature::from(signature)), + Did::create( + Origin::signed(ACCOUNT_00), + Box::new(details), + did::DidSignature::from(signature) + ), did::Error::::InvalidServiceEncoding ); }); From 6f244924fc7451a28c13fd99ae3d53bac4a8b29f Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Mon, 25 Oct 2021 10:12:44 +0200 Subject: [PATCH 29/45] chore: reword the failure comment --- pallets/did/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 906b6857a..20ea15bfd 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -829,7 +829,7 @@ pub mod pallet { Error::::MaxNumberOfServicesPerDidExceeded ); - // *** No Fail beyond this point *** + // *** No Fail after the following storage write *** ServiceEndpoints::::try_mutate( &did_subject, From c6301e5af2ca1a1edf9608b02e05c9668c920276 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Mon, 25 Oct 2021 10:16:36 +0200 Subject: [PATCH 30/45] chore: make DidEndpointDetails fields public --- pallets/did/src/service_endpoints.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 63e9bf946..9549c195d 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -48,11 +48,11 @@ pub(crate) type ServiceEndpointUrlEntries = pub struct DidEndpointDetails { /// The ID of the service endpoint. Allows the endpoint to be queried and /// resolved directly. - pub(crate) id: ServiceEndpointId, + pub id: ServiceEndpointId, /// A vector of types description for the service. - pub(crate) service_types: ServiceEndpointTypeEntries, + pub service_types: ServiceEndpointTypeEntries, /// A vector of URLs the service points to. - pub(crate) urls: ServiceEndpointUrlEntries, + pub urls: ServiceEndpointUrlEntries, } impl sp_std::fmt::Debug for DidEndpointDetails { From 9323dab6ecf208dc42a2ccb870b36316e7d1cc29 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Mon, 25 Oct 2021 10:19:34 +0200 Subject: [PATCH 31/45] chore: rename DidEndpointDetails to DidEndpoint --- pallets/did/src/benchmarking.rs | 2 +- pallets/did/src/did_details.rs | 2 +- pallets/did/src/lib.rs | 4 ++-- pallets/did/src/mock.rs | 4 ++-- pallets/did/src/mock_utils.rs | 4 ++-- pallets/did/src/service_endpoints.rs | 14 ++++++------- pallets/did/src/tests.rs | 30 ++++++++++++++-------------- 7 files changed, 30 insertions(+), 30 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index f015d9f62..42b163e39 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -102,7 +102,7 @@ fn generate_base_did_call_operation( } } -fn save_service_endpoints(did_subject: &DidIdentifierOf, endpoints: &[DidEndpointDetails]) { +fn save_service_endpoints(did_subject: &DidIdentifierOf, endpoints: &[DidEndpoint]) { for endpoint in endpoints.iter() { ServiceEndpoints::::insert(&did_subject, &endpoint.id, endpoint.clone()); } diff --git a/pallets/did/src/did_details.rs b/pallets/did/src/did_details.rs index bffbb8ce6..e3eef984b 100644 --- a/pallets/did/src/did_details.rs +++ b/pallets/did/src/did_details.rs @@ -557,7 +557,7 @@ pub struct DidCreationDetails { /// \[OPTIONAL\] The new delegation key. pub new_delegation_key: Option, /// The service endpoints details. - pub new_service_details: Vec>, + pub new_service_details: Vec>, } impl sp_std::fmt::Debug for DidCreationDetails { diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 20ea15bfd..3f778341e 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -300,7 +300,7 @@ pub mod pallet { DidIdentifierOf, Blake2_128Concat, ServiceEndpointId, - DidEndpointDetails, + DidEndpoint, >; /// Counter of service endpoints for each DID. @@ -811,7 +811,7 @@ pub mod pallet { /// - Writes: Did, ServiceEndpoints, DidEndpointsCount /// # #[pallet::weight(::WeightInfo::add_service_endpoint())] - pub fn add_service_endpoint(origin: OriginFor, service_endpoint: DidEndpointDetails) -> DispatchResult { + pub fn add_service_endpoint(origin: OriginFor, service_endpoint: DidEndpoint) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); service_endpoint diff --git a/pallets/did/src/mock.rs b/pallets/did/src/mock.rs index 9a22542c3..485072717 100644 --- a/pallets/did/src/mock.rs +++ b/pallets/did/src/mock.rs @@ -413,7 +413,7 @@ pub fn initialize_logger() { #[derive(Clone, Default)] pub struct ExtBuilder { dids_stored: Vec<(TestDidIdentifier, did::DidDetails)>, - service_endpoints: Vec<(TestDidIdentifier, Vec>)>, + service_endpoints: Vec<(TestDidIdentifier, Vec>)>, deleted_dids: Vec, storage_version: DidStorageVersion, ctypes_stored: Vec<(TestCtypeHash, TestCtypeOwner)>, @@ -426,7 +426,7 @@ impl ExtBuilder { self } - pub fn with_endpoints(mut self, endpoints: Vec<(TestDidIdentifier, Vec>)>) -> Self { + pub fn with_endpoints(mut self, endpoints: Vec<(TestDidIdentifier, Vec>)>) -> Self { self.service_endpoints = endpoints; self } diff --git a/pallets/did/src/mock_utils.rs b/pallets/did/src/mock_utils.rs index 99b12b5ea..6ebbf970c 100644 --- a/pallets/did/src/mock_utils.rs +++ b/pallets/did/src/mock_utils.rs @@ -51,7 +51,7 @@ pub fn get_service_endpoints( endpoint_type_length: u32, endpoint_url_count: u32, endpoint_url_length: u32, -) -> Vec> { +) -> Vec> { (0..count) .map(|i| { let mut endpoint_id = i.to_be_bytes().to_vec(); @@ -70,7 +70,7 @@ pub fn get_service_endpoints( endpoint_url }) .collect(); - DidEndpointDetails::new(endpoint_id, endpoint_types, endpoint_urls) + DidEndpoint::new(endpoint_id, endpoint_types, endpoint_urls) }) .collect() } diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 9549c195d..71bae649b 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -45,7 +45,7 @@ pub(crate) type ServiceEndpointUrlEntries = /// A single service endpoint description. #[derive(Clone, Decode, Encode, PartialEq, Eq, TypeInfo)] #[scale_info(skip_type_params(T))] -pub struct DidEndpointDetails { +pub struct DidEndpoint { /// The ID of the service endpoint. Allows the endpoint to be queried and /// resolved directly. pub id: ServiceEndpointId, @@ -55,9 +55,9 @@ pub struct DidEndpointDetails { pub urls: ServiceEndpointUrlEntries, } -impl sp_std::fmt::Debug for DidEndpointDetails { +impl sp_std::fmt::Debug for DidEndpoint { fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result { - f.debug_struct("DidEndpointDetails") + f.debug_struct("DidEndpoint") .field("id", &self.id.clone().into_inner()) .field("service_types", &self.service_types.encode()) .field("urls", &self.urls.encode()) @@ -65,8 +65,8 @@ impl sp_std::fmt::Debug for DidEndpointDetails { } } -impl DidEndpointDetails { - /// Validates a given [DidEndpointDetails] instance against the constraint +impl DidEndpoint { + /// Validates a given [DidEndpoint] instance against the constraint /// set in the pallet's [Config]. pub(crate) fn validate_against_constraints(&self) -> Result<(), InputError> { // Check that the maximum number of service types is provided. @@ -117,7 +117,7 @@ impl DidEndpointDetails { } #[cfg(any(test, feature = "runtime-benchmarks"))] -impl DidEndpointDetails { +impl DidEndpoint { pub(crate) fn new(id: Vec, types: Vec>, urls: Vec>) -> Self { let bounded_id = id.try_into().expect("Service ID too long."); let bounded_types = types @@ -145,7 +145,7 @@ pub mod utils { use super::*; pub(crate) fn validate_new_service_endpoints( - endpoints: &[DidEndpointDetails], + endpoints: &[DidEndpoint], ) -> Result<(), InputError> { // Check if up the maximum number of endpoints is provided. ensure!( diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index cc545dac3..c51f126ab 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -27,7 +27,7 @@ use sp_std::{ convert::{TryFrom, TryInto}, }; -use crate::{self as did, mock::*, mock_utils::*, DidEndpointDetails}; +use crate::{self as did, mock::*, mock_utils::*, DidEndpoint}; // create @@ -681,7 +681,7 @@ fn check_invalid_service_id_character_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let new_service_details = - DidEndpointDetails::new("å".bytes().collect(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + DidEndpoint::new("å".bytes().collect(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = vec![new_service_details]; @@ -710,7 +710,7 @@ fn check_invalid_service_type_character_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let new_service_details = - DidEndpointDetails::new(b"id".to_vec(), vec!["å".bytes().collect()], vec![b"url".to_vec()]); + DidEndpoint::new(b"id".to_vec(), vec!["å".bytes().collect()], vec![b"url".to_vec()]); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = vec![new_service_details]; @@ -739,7 +739,7 @@ fn check_invalid_service_url_character_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); let new_service_details = - DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec!["å".bytes().collect()]); + DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec!["å".bytes().collect()]); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = vec![new_service_details]; @@ -1700,7 +1700,7 @@ fn check_key_not_found_key_agreement_key_deletion_error() { fn check_service_addition_no_prior_service_successful() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let new_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + let new_service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1739,7 +1739,7 @@ fn check_service_addition_one_from_full_successful() { ::MaxNumberOfUrlsPerService::get(), ::MaxServiceUrlLength::get(), ); - let new_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + let new_service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1770,7 +1770,7 @@ fn check_service_addition_one_from_full_successful() { fn check_did_not_present_services_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let new_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + let new_service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); ExtBuilder::default().build(None).execute_with(|| { assert_noop!( @@ -1784,7 +1784,7 @@ fn check_did_not_present_services_addition_error() { fn check_service_already_present_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + let service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1812,7 +1812,7 @@ fn check_max_services_count_addition_error() { ::MaxNumberOfUrlsPerService::get(), ::MaxServiceUrlLength::get(), ); - let new_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + let new_service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1973,7 +1973,7 @@ fn check_invalid_service_id_character_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); let new_service_details = - DidEndpointDetails::new("å".bytes().collect(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + DidEndpoint::new("å".bytes().collect(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1993,7 +1993,7 @@ fn check_invalid_service_type_character_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); let new_service_details = - DidEndpointDetails::new(b"id".to_vec(), vec!["å".bytes().collect()], vec![b"url".to_vec()]); + DidEndpoint::new(b"id".to_vec(), vec!["å".bytes().collect()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -2013,7 +2013,7 @@ fn check_invalid_service_url_character_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); let new_service_details = - DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec!["å".bytes().collect()]); + DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec!["å".bytes().collect()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -2035,7 +2035,7 @@ fn check_service_deletion_successful() { initialize_logger(); let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let old_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + let old_service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -2131,7 +2131,7 @@ fn check_successful_deletion_no_endpoints() { fn check_successful_deletion_with_endpoints() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + let service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let mut did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); did_details.deposit.owner = ACCOUNT_00; @@ -2203,7 +2203,7 @@ fn check_did_not_present_deletion() { fn check_successful_reclaiming() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let old_service_endpoint = DidEndpointDetails::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + let old_service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let mut did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); did_details.deposit.owner = ACCOUNT_00; did_details.deposit.amount = ::Deposit::get(); From 14dc9079f10066d3cb395ad6afb262fe8af99b55 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Mon, 25 Oct 2021 10:57:02 +0200 Subject: [PATCH 32/45] feat: add parameter for benchmarks --- pallets/did/src/benchmarking.rs | 22 ++- pallets/did/src/default_weights.rs | 236 ++++++++++++++------------ pallets/did/src/lib.rs | 50 +++--- pallets/did/src/tests.rs | 17 +- runtimes/peregrine/src/weights/did.rs | 132 +++++++------- runtimes/spiritnet/src/weights/did.rs | 132 +++++++------- 6 files changed, 327 insertions(+), 262 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 42b163e39..3e5ba75b6 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -313,12 +313,14 @@ benchmarks! { } delete { + let c in 1 .. T::MaxNumberOfServicesPerDid::get(); + let did_public_auth_key = get_ed25519_public_authentication_key(); let did_subject: DidIdentifierOf = MultiSigner::from(did_public_auth_key).into_account().into(); let did_details = generate_base_did_details::(DidVerificationKey::from(did_public_auth_key)); let service_endpoints = get_service_endpoints::( - T::MaxNumberOfServicesPerDid::get(), + c, T::MaxServiceIdLength::get(), T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), @@ -328,7 +330,7 @@ benchmarks! { Did::::insert(&did_subject, did_details); save_service_endpoints(&did_subject, &service_endpoints); - }: _(RawOrigin::Signed(did_subject.clone())) + }: _(RawOrigin::Signed(did_subject.clone()), c) verify { assert!( Did::::get(&did_subject).is_none() @@ -343,12 +345,14 @@ benchmarks! { } reclaim_deposit { + let c in 1 .. T::MaxNumberOfServicesPerDid::get(); + let did_public_auth_key = get_ed25519_public_authentication_key(); let did_subject: DidIdentifierOf = MultiSigner::from(did_public_auth_key).into_account().into(); let did_details = generate_base_did_details::(DidVerificationKey::from(did_public_auth_key)); let service_endpoints = get_service_endpoints::( - T::MaxNumberOfServicesPerDid::get(), + c, T::MaxServiceIdLength::get(), T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), @@ -358,7 +362,7 @@ benchmarks! { Did::::insert(&did_subject, did_details.clone()); save_service_endpoints(&did_subject, &service_endpoints); - }: _(RawOrigin::Signed(did_details.deposit.owner.clone()), did_subject.clone()) + }: _(RawOrigin::Signed(did_details.deposit.owner.clone()), did_subject.clone(), c) verify { assert!( Did::::get(&did_subject).is_none() @@ -907,11 +911,13 @@ benchmarks! { } remove_service_endpoint { + let c in 1 .. T::MaxNumberOfServicesPerDid::get(); + let public_auth_key = get_ecdsa_public_authentication_key(); let did_subject: DidIdentifierOf = MultiSigner::from(public_auth_key.clone()).into_account().into(); // All set to max. let old_service_endpoints = get_service_endpoints::( - T::MaxNumberOfServicesPerDid::get(), + c, T::MaxServiceIdLength::get(), T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), @@ -923,18 +929,18 @@ benchmarks! { let did_details = generate_base_did_details::(DidVerificationKey::from(public_auth_key)); Did::::insert(&did_subject, did_details); save_service_endpoints(&did_subject, &old_service_endpoints); - }: _(RawOrigin::Signed(did_subject.clone()), endpoint_id.clone()) + }: _(RawOrigin::Signed(did_subject.clone()), endpoint_id.clone(), c) verify { assert!( ServiceEndpoints::::get(&did_subject, &endpoint_id).is_none() ); assert_eq!( DidEndpointsCount::::get(&did_subject).unwrap_or_default(), - T::MaxNumberOfServicesPerDid::get() - 1 + c - 1 ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), - T::MaxNumberOfServicesPerDid::get().saturated_into::() - 1 + c.saturated_into::() - 1 ); } diff --git a/pallets/did/src/default_weights.rs b/pallets/did/src/default_weights.rs index 373963952..5829b7bc4 100644 --- a/pallets/did/src/default_weights.rs +++ b/pallets/did/src/default_weights.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for did //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-22, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ +//! DATE: 2021-10-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: @@ -49,8 +49,8 @@ pub trait WeightInfo { fn create_ed25519_keys(n: u32, c: u32, ) -> Weight; fn create_sr25519_keys(n: u32, c: u32, ) -> Weight; fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight; - fn delete() -> Weight; - fn reclaim_deposit() -> Weight; + fn delete(c: u32, ) -> Weight; + fn reclaim_deposit(c: u32, ) -> Weight; fn submit_did_call_ed25519_key() -> Weight; fn submit_did_call_sr25519_key() -> Weight; fn submit_did_call_ecdsa_key() -> Weight; @@ -76,7 +76,7 @@ pub trait WeightInfo { fn remove_sr25519_key_agreement_key() -> Weight; fn remove_ecdsa_key_agreement_key() -> Weight; fn add_service_endpoint() -> Weight; - fn remove_service_endpoint() -> Weight; + fn remove_service_endpoint(c: u32, ) -> Weight; fn signature_verification_sr25519(l: u32, ) -> Weight; fn signature_verification_ed25519(l: u32, ) -> Weight; fn signature_verification_ecdsa(l: u32, ) -> Weight; @@ -85,186 +85,198 @@ pub trait WeightInfo { /// Weights for did using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { - fn create_ed25519_keys(_n: u32, c: u32, ) -> Weight { - (244_709_000_u64) - // Standard Error: 1_158_000 - .saturating_add((17_875_000_u64).saturating_mul(c as Weight)) + fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { + (121_133_000_u64) + // Standard Error: 89_000 + .saturating_add((2_124_000_u64).saturating_mul(n as Weight)) + // Standard Error: 33_000 + .saturating_add((8_147_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { - (283_812_000_u64) - // Standard Error: 823_000 - .saturating_add((11_361_000_u64).saturating_mul(c as Weight)) + fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { + (124_207_000_u64) + // Standard Error: 117_000 + .saturating_add((2_129_000_u64).saturating_mul(n as Weight)) + // Standard Error: 44_000 + .saturating_add((8_584_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (99_494_000_u64) - // Standard Error: 2_670_000 - .saturating_add((26_003_000_u64).saturating_mul(n as Weight)) - // Standard Error: 1_001_000 - .saturating_add((17_883_000_u64).saturating_mul(c as Weight)) + (225_138_000_u64) + // Standard Error: 105_000 + .saturating_add((1_798_000_u64).saturating_mul(n as Weight)) + // Standard Error: 39_000 + .saturating_add((8_024_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn delete() -> Weight { - (109_997_000_u64) + fn delete(c: u32, ) -> Weight { + (28_882_000_u64) + // Standard Error: 9_000 + .saturating_add((773_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().writes(28_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn reclaim_deposit() -> Weight { - (124_754_000_u64) + fn reclaim_deposit(c: u32, ) -> Weight { + (32_879_000_u64) + // Standard Error: 14_000 + .saturating_add((765_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) - .saturating_add(T::DbWeight::get().writes(28_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn submit_did_call_ed25519_key() -> Weight { - (144_912_000_u64) + (68_188_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (153_609_000_u64) + (70_542_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (315_673_000_u64) + (168_296_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (103_966_000_u64) + (37_110_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (107_131_000_u64) + (37_851_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (85_911_000_u64) + (42_199_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (52_219_000_u64) + (37_160_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (99_697_000_u64) + (35_938_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (93_135_000_u64) + (36_037_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (44_574_000_u64) + (33_833_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (50_204_000_u64) + (34_484_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (72_196_000_u64) + (34_224_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (63_349_000_u64) + (36_518_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (85_310_000_u64) + (36_799_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (102_342_000_u64) + (37_430_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (66_665_000_u64) + (32_892_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (80_421_000_u64) + (33_683_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (107_252_000_u64) + (33_292_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (101_591_000_u64) + (34_916_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (85_590_000_u64) + (35_928_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (47_349_000_u64) + (35_737_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (57_137_000_u64) + (33_353_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (61_966_000_u64) + (33_603_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (62_547_000_u64) + (33_492_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (40_877_000_u64) + (31_278_000_u64) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } - fn remove_service_endpoint() -> Weight { - (28_754_000_u64) + fn remove_service_endpoint(c: u32, ) -> Weight { + (22_774_000_u64) + // Standard Error: 5_000 + .saturating_add((74_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (135_911_000_u64) + (58_482_000_u64) // Standard Error: 0 - .saturating_add((5_000_u64).saturating_mul(l as Weight)) + .saturating_add((3_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (133_337_000_u64) + (56_187_000_u64) // Standard Error: 0 - .saturating_add((2_000_u64).saturating_mul(l as Weight)) + .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (278_287_000_u64) + (155_859_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) @@ -273,186 +285,198 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { - fn create_ed25519_keys(_n: u32, c: u32, ) -> Weight { - (244_709_000_u64) - // Standard Error: 1_158_000 - .saturating_add((17_875_000_u64).saturating_mul(c as Weight)) + fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { + (121_133_000_u64) + // Standard Error: 89_000 + .saturating_add((2_124_000_u64).saturating_mul(n as Weight)) + // Standard Error: 33_000 + .saturating_add((8_147_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { - (283_812_000_u64) - // Standard Error: 823_000 - .saturating_add((11_361_000_u64).saturating_mul(c as Weight)) + fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { + (124_207_000_u64) + // Standard Error: 117_000 + .saturating_add((2_129_000_u64).saturating_mul(n as Weight)) + // Standard Error: 44_000 + .saturating_add((8_584_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (99_494_000_u64) - // Standard Error: 2_670_000 - .saturating_add((26_003_000_u64).saturating_mul(n as Weight)) - // Standard Error: 1_001_000 - .saturating_add((17_883_000_u64).saturating_mul(c as Weight)) + (225_138_000_u64) + // Standard Error: 105_000 + .saturating_add((1_798_000_u64).saturating_mul(n as Weight)) + // Standard Error: 39_000 + .saturating_add((8_024_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn delete() -> Weight { - (109_997_000_u64) + fn delete(c: u32, ) -> Weight { + (28_882_000_u64) + // Standard Error: 9_000 + .saturating_add((773_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().writes(28_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn reclaim_deposit() -> Weight { - (124_754_000_u64) + fn reclaim_deposit(c: u32, ) -> Weight { + (32_879_000_u64) + // Standard Error: 14_000 + .saturating_add((765_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(2_u64)) - .saturating_add(RocksDbWeight::get().writes(28_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn submit_did_call_ed25519_key() -> Weight { - (144_912_000_u64) + (68_188_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (153_609_000_u64) + (70_542_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (315_673_000_u64) + (168_296_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (103_966_000_u64) + (37_110_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (107_131_000_u64) + (37_851_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (85_911_000_u64) + (42_199_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (52_219_000_u64) + (37_160_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (99_697_000_u64) + (35_938_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (93_135_000_u64) + (36_037_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (44_574_000_u64) + (33_833_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (50_204_000_u64) + (34_484_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (72_196_000_u64) + (34_224_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (63_349_000_u64) + (36_518_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (85_310_000_u64) + (36_799_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (102_342_000_u64) + (37_430_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (66_665_000_u64) + (32_892_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (80_421_000_u64) + (33_683_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (107_252_000_u64) + (33_292_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (101_591_000_u64) + (34_916_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (85_590_000_u64) + (35_928_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (47_349_000_u64) + (35_737_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (57_137_000_u64) + (33_353_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (61_966_000_u64) + (33_603_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (62_547_000_u64) + (33_492_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (40_877_000_u64) + (31_278_000_u64) .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } - fn remove_service_endpoint() -> Weight { - (28_754_000_u64) + fn remove_service_endpoint(c: u32, ) -> Weight { + (22_774_000_u64) + // Standard Error: 5_000 + .saturating_add((74_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (135_911_000_u64) + (58_482_000_u64) // Standard Error: 0 - .saturating_add((5_000_u64).saturating_mul(l as Weight)) + .saturating_add((3_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (133_337_000_u64) + (56_187_000_u64) // Standard Error: 0 - .saturating_add((2_000_u64).saturating_mul(l as Weight)) + .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (278_287_000_u64) + (155_859_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 3f778341e..7f92fe642 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -399,6 +399,8 @@ pub mod pallet { ServiceNotPresent, /// One of the service endpoint details contains non-ASCII characters. InvalidServiceEncoding, + /// The number of service endpoints stored under the DID is larger than the number of endpoints to delete. + StoredEndpointsCountTooLarge, /// An error that is not supposed to take place, yet it happened. InternalError, } @@ -859,8 +861,8 @@ pub mod pallet { /// - Reads: [Origin Account], ServiceEndpoints, DidEndpointsCount /// - Writes: Did, ServiceEndpoints, DidEndpointsCount /// # - #[pallet::weight(::WeightInfo::remove_service_endpoint())] - pub fn remove_service_endpoint(origin: OriginFor, service_id: ServiceEndpointId) -> DispatchResult { + #[pallet::weight(::WeightInfo::remove_service_endpoint(*endpoints_to_remove))] + pub fn remove_service_endpoint(origin: OriginFor, service_id: ServiceEndpointId, endpoints_to_remove: u32) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); ensure!( @@ -868,17 +870,21 @@ pub mod pallet { Error::::ServiceNotPresent ); + let current_endpoints_count = DidEndpointsCount::::get(&did_subject).unwrap_or_default(); + ensure!( + current_endpoints_count <= endpoints_to_remove, + Error::::StoredEndpointsCountTooLarge + ); + // *** No Fail beyond this point *** // Decrease the endpoints counter or delete the entry if it reaches 0. - DidEndpointsCount::::mutate_exists(&did_subject, |existing_endpoint_count| { - let new_value = existing_endpoint_count.unwrap_or_default().saturating_sub(1); - if new_value == u32::zero() { - *existing_endpoint_count = None; - } else { - *existing_endpoint_count = Some(new_value); - } - }); + let new_endpoints_count = current_endpoints_count.saturating_sub(1); + if new_endpoints_count > u32::zero() { + DidEndpointsCount::::insert(&did_subject, new_endpoints_count) + } else { + DidEndpointsCount::::remove(&did_subject); + }; Self::deposit_event(Event::DidUpdated(did_subject)); @@ -909,12 +915,12 @@ pub mod pallet { /// - Reads: [Origin Account], Did /// - Kills: Did entry associated to the DID identifier /// # - #[pallet::weight(::WeightInfo::delete())] - pub fn delete(origin: OriginFor) -> DispatchResult { + #[pallet::weight(::WeightInfo::delete(*endpoints_to_remove))] + pub fn delete(origin: OriginFor, endpoints_to_remove: u32) -> DispatchResult { let source = T::EnsureOrigin::ensure_origin(origin)?; let did_subject = source.subject(); - Pallet::::delete_did(did_subject) + Pallet::::delete_did(did_subject, endpoints_to_remove) } /// Reclaim a deposit for a DID. This will delete the DID and all @@ -938,15 +944,15 @@ pub mod pallet { /// - Reads: [Origin Account], Did /// - Kills: Did entry associated to the DID identifier /// # - #[pallet::weight(::WeightInfo::reclaim_deposit())] - pub fn reclaim_deposit(origin: OriginFor, did_subject: DidIdentifierOf) -> DispatchResult { + #[pallet::weight(::WeightInfo::reclaim_deposit(*endpoints_to_remove))] + pub fn reclaim_deposit(origin: OriginFor, did_subject: DidIdentifierOf, endpoints_to_remove: u32) -> DispatchResult { let source = ensure_signed(origin)?; let did_entry = Did::::get(&did_subject).ok_or(Error::::DidNotPresent)?; ensure!(did_entry.deposit.owner == source, Error::::NotOwnerOfDeposit); - Pallet::::delete_did(did_subject) + Pallet::::delete_did(did_subject, endpoints_to_remove) } /// Proxy a dispatchable call of another runtime extrinsic that @@ -1137,14 +1143,20 @@ impl Pallet { /// Deletes DID details from storage, including its linked service /// endpoints, adds the identifier to the blacklisted DIDs and frees the /// deposit. - fn delete_did(did_subject: DidIdentifierOf) -> DispatchResult { + fn delete_did(did_subject: DidIdentifierOf, endpoints_to_remove: u32) -> DispatchResult { // `take` calls `kill` internally let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; + let current_endpoints_count = DidEndpointsCount::::get(&did_subject).unwrap_or_default(); + ensure!( + current_endpoints_count <= endpoints_to_remove, + Error::::StoredEndpointsCountTooLarge + ); + // *** No Fail beyond this point *** - let services_count = DidEndpointsCount::::take(&did_subject).unwrap_or_default(); - let storage_kill_result = ServiceEndpoints::::remove_prefix(&did_subject, Some(services_count)); + DidEndpointsCount::::remove(&did_subject); + let storage_kill_result = ServiceEndpoints::::remove_prefix(&did_subject, Some(current_endpoints_count)); // If some items are remaining, it means that there were more than // the counter stored in `DidEndpointsCount`, and that should never happen. if let KillStorageResult::SomeRemaining(_) = storage_kill_result { diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index c51f126ab..d7fe249c1 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -2046,7 +2046,8 @@ fn check_service_deletion_successful() { .execute_with(|| { assert_ok!(Did::remove_service_endpoint( Origin::signed(alice_did.clone()), - old_service_endpoint.id + old_service_endpoint.id, + 1 ),); // Counter should be deleted from the storage. assert!(did::pallet::DidEndpointsCount::::get(&alice_did).is_none()); @@ -2072,7 +2073,8 @@ fn check_service_not_present_deletion_error() { assert_noop!( Did::remove_service_endpoint( Origin::signed(alice_did.clone()), - service_id.try_into().expect("Service ID to delete too long") + service_id.try_into().expect("Service ID to delete too long"), + 0 ), did::Error::::ServiceNotPresent ); @@ -2104,7 +2106,7 @@ fn check_successful_deletion_no_endpoints() { Balances::reserved_balance(ACCOUNT_00), ::Deposit::get() ); - assert_ok!(Did::delete(Origin::signed(alice_did.clone()))); + assert_ok!(Did::delete(Origin::signed(alice_did.clone()), 0)); assert!(Did::get_did(alice_did.clone()).is_none()); assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); @@ -2155,7 +2157,7 @@ fn check_successful_deletion_with_endpoints() { Balances::reserved_balance(ACCOUNT_00), ::Deposit::get() ); - assert_ok!(Did::delete(Origin::signed(alice_did.clone()))); + assert_ok!(Did::delete(Origin::signed(alice_did.clone()), 1)); assert!(Did::get_did(alice_did.clone()).is_none()); assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); @@ -2191,7 +2193,7 @@ fn check_did_not_present_deletion() { .build(None) .execute_with(|| { assert_noop!( - Did::delete(Origin::signed(alice_did)), + Did::delete(Origin::signed(alice_did), 0), did::Error::::DidNotPresent ); }); @@ -2228,7 +2230,8 @@ fn check_successful_reclaiming() { ); assert_ok!(Did::reclaim_deposit( Origin::signed(ACCOUNT_00.clone()), - alice_did.clone() + alice_did.clone(), + 1 )); assert!(Did::get_did(alice_did.clone()).is_none()); assert!(Did::get_deleted_did(alice_did.clone()).is_some()); @@ -2276,7 +2279,7 @@ fn unauthorized_reclaiming() { ::Deposit::get() ); assert_noop!( - Did::reclaim_deposit(Origin::signed(ACCOUNT_01.clone()), alice_did.clone()), + Did::reclaim_deposit(Origin::signed(ACCOUNT_01.clone()), alice_did.clone(), 0), did::Error::::NotOwnerOfDeposit ); }); diff --git a/runtimes/peregrine/src/weights/did.rs b/runtimes/peregrine/src/weights/did.rs index 37c94f29b..f7a8703b4 100644 --- a/runtimes/peregrine/src/weights/did.rs +++ b/runtimes/peregrine/src/weights/did.rs @@ -48,187 +48,197 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl did::WeightInfo for WeightInfo { fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (108_176_000_u64) - // Standard Error: 241_000 - .saturating_add((4_481_000_u64).saturating_mul(n as Weight)) - // Standard Error: 75_000 - .saturating_add((9_351_000_u64).saturating_mul(c as Weight)) + (121_133_000_u64) + // Standard Error: 89_000 + .saturating_add((2_124_000_u64).saturating_mul(n as Weight)) + // Standard Error: 33_000 + .saturating_add((8_147_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { - (171_118_000_u64) - // Standard Error: 76_000 - .saturating_add((9_207_000_u64).saturating_mul(c as Weight)) + fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { + (124_207_000_u64) + // Standard Error: 117_000 + .saturating_add((2_129_000_u64).saturating_mul(n as Weight)) + // Standard Error: 44_000 + .saturating_add((8_584_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (223_404_000_u64) - // Standard Error: 186_000 - .saturating_add((3_119_000_u64).saturating_mul(n as Weight)) - // Standard Error: 58_000 - .saturating_add((9_215_000_u64).saturating_mul(c as Weight)) + (225_138_000_u64) + // Standard Error: 105_000 + .saturating_add((1_798_000_u64).saturating_mul(n as Weight)) + // Standard Error: 39_000 + .saturating_add((8_024_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn delete() -> Weight { - (48_642_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(27_u64)) + fn delete(c: u32, ) -> Weight { + (28_882_000_u64) + // Standard Error: 9_000 + .saturating_add((773_000_u64).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn reclaim_deposit() -> Weight { - (53_300_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(27_u64)) + fn reclaim_deposit(c: u32, ) -> Weight { + (32_879_000_u64) + // Standard Error: 14_000 + .saturating_add((765_000_u64).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn submit_did_call_ed25519_key() -> Weight { - (73_979_000_u64) + (68_188_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (76_314_000_u64) + (70_542_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (177_954_000_u64) + (168_296_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (41_358_000_u64) + (37_110_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (41_848_000_u64) + (37_851_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (41_578_000_u64) + (42_199_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (41_107_000_u64) + (37_160_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (41_538_000_u64) + (35_938_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (41_958_000_u64) + (36_037_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (38_693_000_u64) + (33_833_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (38_402_000_u64) + (34_484_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (39_134_000_u64) + (34_224_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (41_197_000_u64) + (36_518_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (40_797_000_u64) + (36_799_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (42_069_000_u64) + (37_430_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (37_862_000_u64) + (32_892_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (38_462_000_u64) + (33_683_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (38_582_000_u64) + (33_292_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (40_215_000_u64) + (34_916_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (40_526_000_u64) + (35_928_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (40_857_000_u64) + (35_737_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (38_282_000_u64) + (33_353_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (39_494_000_u64) + (33_603_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (38_462_000_u64) + (33_492_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (226_335_000_u64) - .saturating_add(T::DbWeight::get().reads(27_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) + (31_278_000_u64) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) } - fn remove_service_endpoint() -> Weight { - (10_841_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) + fn remove_service_endpoint(c: u32, ) -> Weight { + (22_774_000_u64) + // Standard Error: 5_000 + .saturating_add((74_000_u64).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (0_u64) + (58_482_000_u64) // Standard Error: 0 - .saturating_add((4_000_u64).saturating_mul(l as Weight)) + .saturating_add((3_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (331_203_000_u64) + (56_187_000_u64) // Standard Error: 0 - .saturating_add((2_000_u64).saturating_mul(l as Weight)) + .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (142_753_000_u64) + (155_859_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) diff --git a/runtimes/spiritnet/src/weights/did.rs b/runtimes/spiritnet/src/weights/did.rs index 8f6fb298d..504dd3930 100644 --- a/runtimes/spiritnet/src/weights/did.rs +++ b/runtimes/spiritnet/src/weights/did.rs @@ -48,187 +48,197 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl did::WeightInfo for WeightInfo { fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (108_176_000_u64) - // Standard Error: 241_000 - .saturating_add((4_481_000_u64).saturating_mul(n as Weight)) - // Standard Error: 75_000 - .saturating_add((9_351_000_u64).saturating_mul(c as Weight)) + (121_133_000_u64) + // Standard Error: 89_000 + .saturating_add((2_124_000_u64).saturating_mul(n as Weight)) + // Standard Error: 33_000 + .saturating_add((8_147_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_sr25519_keys(_n: u32, c: u32, ) -> Weight { - (171_118_000_u64) - // Standard Error: 76_000 - .saturating_add((9_207_000_u64).saturating_mul(c as Weight)) + fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { + (124_207_000_u64) + // Standard Error: 117_000 + .saturating_add((2_129_000_u64).saturating_mul(n as Weight)) + // Standard Error: 44_000 + .saturating_add((8_584_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (223_404_000_u64) - // Standard Error: 186_000 - .saturating_add((3_119_000_u64).saturating_mul(n as Weight)) - // Standard Error: 58_000 - .saturating_add((9_215_000_u64).saturating_mul(c as Weight)) + (225_138_000_u64) + // Standard Error: 105_000 + .saturating_add((1_798_000_u64).saturating_mul(n as Weight)) + // Standard Error: 39_000 + .saturating_add((8_024_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) - .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn delete() -> Weight { - (48_642_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(27_u64)) + fn delete(c: u32, ) -> Weight { + (28_882_000_u64) + // Standard Error: 9_000 + .saturating_add((773_000_u64).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn reclaim_deposit() -> Weight { - (53_300_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(27_u64)) + fn reclaim_deposit(c: u32, ) -> Weight { + (32_879_000_u64) + // Standard Error: 14_000 + .saturating_add((765_000_u64).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn submit_did_call_ed25519_key() -> Weight { - (73_979_000_u64) + (68_188_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (76_314_000_u64) + (70_542_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (177_954_000_u64) + (168_296_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (41_358_000_u64) + (37_110_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (41_848_000_u64) + (37_851_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (41_578_000_u64) + (42_199_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (41_107_000_u64) + (37_160_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (41_538_000_u64) + (35_938_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (41_958_000_u64) + (36_037_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (38_693_000_u64) + (33_833_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (38_402_000_u64) + (34_484_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (39_134_000_u64) + (34_224_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (41_197_000_u64) + (36_518_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (40_797_000_u64) + (36_799_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (42_069_000_u64) + (37_430_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (37_862_000_u64) + (32_892_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (38_462_000_u64) + (33_683_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (38_582_000_u64) + (33_292_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (40_215_000_u64) + (34_916_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (40_526_000_u64) + (35_928_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (40_857_000_u64) + (35_737_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (38_282_000_u64) + (33_353_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (39_494_000_u64) + (33_603_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (38_462_000_u64) + (33_492_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (226_335_000_u64) - .saturating_add(T::DbWeight::get().reads(27_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) + (31_278_000_u64) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) } - fn remove_service_endpoint() -> Weight { - (10_841_000_u64) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) + fn remove_service_endpoint(c: u32, ) -> Weight { + (22_774_000_u64) + // Standard Error: 5_000 + .saturating_add((74_000_u64).saturating_mul(c as Weight)) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (0_u64) + (58_482_000_u64) // Standard Error: 0 - .saturating_add((4_000_u64).saturating_mul(l as Weight)) + .saturating_add((3_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (331_203_000_u64) + (56_187_000_u64) // Standard Error: 0 - .saturating_add((2_000_u64).saturating_mul(l as Weight)) + .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (142_753_000_u64) + (155_859_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) From 37f45dbd2d61a7fa469525370fc714c8293dbeea Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Mon, 25 Oct 2021 11:02:38 +0200 Subject: [PATCH 33/45] chore: replace OptionQuery with ValueQuery for DidServicesCount storage entry --- pallets/did/src/benchmarking.rs | 18 +++++++++--------- pallets/did/src/lib.rs | 8 ++++---- pallets/did/src/tests.rs | 19 +++++++++---------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 3e5ba75b6..04bd693f3 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -174,7 +174,7 @@ benchmarks! { Some(expected_attestation_key_id) ); assert_eq!( - DidEndpointsCount::::get(&did_subject).unwrap_or_default().saturated_into::(), + DidEndpointsCount::::get(&did_subject).saturated_into::(), service_endpoints.len() ); assert_eq!( @@ -238,7 +238,7 @@ benchmarks! { Some(expected_attestation_key_id) ); assert_eq!( - DidEndpointsCount::::get(&did_subject).unwrap_or_default().saturated_into::(), + DidEndpointsCount::::get(&did_subject).saturated_into::(), service_endpoints.len() ); assert_eq!( @@ -302,7 +302,7 @@ benchmarks! { Some(expected_attestation_key_id) ); assert_eq!( - DidEndpointsCount::::get(&did_subject).unwrap_or_default().saturated_into::(), + DidEndpointsCount::::get(&did_subject).saturated_into::(), service_endpoints.len() ); assert_eq!( @@ -335,8 +335,8 @@ benchmarks! { assert!( Did::::get(&did_subject).is_none() ); - assert!( - DidEndpointsCount::::get(&did_subject).is_none() + assert_eq!( + DidEndpointsCount::::get(&did_subject), 0 ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), @@ -367,8 +367,8 @@ benchmarks! { assert!( Did::::get(&did_subject).is_none() ); - assert!( - DidEndpointsCount::::get(&did_subject).is_none() + assert_eq!( + DidEndpointsCount::::get(&did_subject), 0 ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), @@ -901,7 +901,7 @@ benchmarks! { Some(new_service_endpoint) ); assert_eq!( - DidEndpointsCount::::get(&did_subject).unwrap_or_default(), + DidEndpointsCount::::get(&did_subject), T::MaxNumberOfServicesPerDid::get() ); assert_eq!( @@ -935,7 +935,7 @@ benchmarks! { ServiceEndpoints::::get(&did_subject, &endpoint_id).is_none() ); assert_eq!( - DidEndpointsCount::::get(&did_subject).unwrap_or_default(), + DidEndpointsCount::::get(&did_subject), c - 1 ); assert_eq!( diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 7f92fe642..453e1496f 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -307,7 +307,7 @@ pub mod pallet { /// /// It maps from (DID identifier) to a 32-bit counter. #[pallet::storage] - pub(crate) type DidEndpointsCount = StorageMap<_, Blake2_128Concat, DidIdentifierOf, u32>; + pub(crate) type DidEndpointsCount = StorageMap<_, Blake2_128Concat, DidIdentifierOf, u32, ValueQuery>; /// The set of DIDs that have been deleted and cannot therefore be created /// again for security reasons. @@ -823,7 +823,7 @@ pub mod pallet { // Verify that the DID is present. ensure!(Did::::get(&did_subject).is_some(), Error::::DidNotPresent); - let currently_stored_endpoints_count = DidEndpointsCount::::get(&did_subject).unwrap_or_default(); + let currently_stored_endpoints_count = DidEndpointsCount::::get(&did_subject); // Verify that there are less than the maximum limit of services stored. ensure!( @@ -870,7 +870,7 @@ pub mod pallet { Error::::ServiceNotPresent ); - let current_endpoints_count = DidEndpointsCount::::get(&did_subject).unwrap_or_default(); + let current_endpoints_count = DidEndpointsCount::::get(&did_subject); ensure!( current_endpoints_count <= endpoints_to_remove, Error::::StoredEndpointsCountTooLarge @@ -1147,7 +1147,7 @@ impl Pallet { // `take` calls `kill` internally let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; - let current_endpoints_count = DidEndpointsCount::::get(&did_subject).unwrap_or_default(); + let current_endpoints_count = DidEndpointsCount::::get(&did_subject); ensure!( current_endpoints_count <= endpoints_to_remove, Error::::StoredEndpointsCountTooLarge diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index d7fe249c1..10e5a998a 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -252,7 +252,6 @@ fn check_successful_complete_creation() { ); assert_eq!( did::pallet::DidEndpointsCount::::get(&alice_did) - .unwrap_or_default() .saturated_into::(), details.new_service_details.len() ); @@ -1720,7 +1719,7 @@ fn check_service_addition_no_prior_service_successful() { 1 ); assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), + did::pallet::DidEndpointsCount::::get(&alice_did), 1 ); }); @@ -1753,7 +1752,7 @@ fn check_service_addition_one_from_full_successful() { new_service_endpoint.clone() ),); assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), + did::pallet::DidEndpointsCount::::get(&alice_did), ::MaxNumberOfServicesPerDid::get() ); assert_eq!( @@ -2050,7 +2049,7 @@ fn check_service_deletion_successful() { 1 ),); // Counter should be deleted from the storage. - assert!(did::pallet::DidEndpointsCount::::get(&alice_did).is_none()); + assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did), 0); assert_eq!( did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 0 @@ -2101,7 +2100,7 @@ fn check_successful_deletion_no_endpoints() { .with_dids(vec![(alice_did.clone(), did_details)]) .build(None) .execute_with(|| { - assert!(did::pallet::DidEndpointsCount::::get(&alice_did).is_none()); + assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did), 0); assert_eq!( Balances::reserved_balance(ACCOUNT_00), ::Deposit::get() @@ -2111,7 +2110,7 @@ fn check_successful_deletion_no_endpoints() { assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); - assert!(did::pallet::DidEndpointsCount::::get(&alice_did).is_none()); + assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did), 0); // Re-adding the same DID identifier should fail. let details = generate_base_did_creation_details::(alice_did.clone(), ACCOUNT_00); @@ -2150,7 +2149,7 @@ fn check_successful_deletion_with_endpoints() { .build(None) .execute_with(|| { assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), + did::pallet::DidEndpointsCount::::get(&alice_did), 1 ); assert_eq!( @@ -2162,7 +2161,7 @@ fn check_successful_deletion_with_endpoints() { assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); - assert!(did::pallet::DidEndpointsCount::::get(&alice_did).is_none(),); + assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did), 0); // Re-adding the same DID identifier should fail. let details = generate_base_did_creation_details::(alice_did.clone(), ACCOUNT_00); @@ -2221,7 +2220,7 @@ fn check_successful_reclaiming() { .build(None) .execute_with(|| { assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), + did::pallet::DidEndpointsCount::::get(&alice_did), 1 ); assert_eq!( @@ -2237,7 +2236,7 @@ fn check_successful_reclaiming() { assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did).unwrap_or_default(), + did::pallet::DidEndpointsCount::::get(&alice_did), 0 ); From 847e4eaf7fd3dd40d8639451d11ab030dcf887e8 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Mon, 25 Oct 2021 11:08:55 +0200 Subject: [PATCH 34/45] chore: use .is_zero() --- pallets/did/src/lib.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 453e1496f..03c93e295 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -880,10 +880,10 @@ pub mod pallet { // Decrease the endpoints counter or delete the entry if it reaches 0. let new_endpoints_count = current_endpoints_count.saturating_sub(1); - if new_endpoints_count > u32::zero() { - DidEndpointsCount::::insert(&did_subject, new_endpoints_count) - } else { + if new_endpoints_count.is_zero() { DidEndpointsCount::::remove(&did_subject); + } else { + DidEndpointsCount::::insert(&did_subject, new_endpoints_count) }; Self::deposit_event(Event::DidUpdated(did_subject)); @@ -1153,15 +1153,18 @@ impl Pallet { Error::::StoredEndpointsCountTooLarge ); - // *** No Fail beyond this point *** + // This one can fail, albeit this should **never** be the case as we check for the preconditions above. - DidEndpointsCount::::remove(&did_subject); let storage_kill_result = ServiceEndpoints::::remove_prefix(&did_subject, Some(current_endpoints_count)); // If some items are remaining, it means that there were more than // the counter stored in `DidEndpointsCount`, and that should never happen. if let KillStorageResult::SomeRemaining(_) = storage_kill_result { return Err(Error::::InternalError.into()); }; + + // *** No Fail beyond this point *** + + DidEndpointsCount::::remove(&did_subject); kilt_support::free_deposit::, CurrencyOf>(&did_entry.deposit); // Mark as deleted to prevent potential replay-attacks of re-adding a previously // deleted DID. From b387043bd16d7c37d82f0c265a0eca59bc590910 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Mon, 25 Oct 2021 11:09:17 +0200 Subject: [PATCH 35/45] chore: fmt --- pallets/did/src/lib.rs | 28 +++++++++++-------- pallets/did/src/service_endpoints.rs | 4 +-- pallets/did/src/tests.rs | 41 ++++++++-------------------- 3 files changed, 28 insertions(+), 45 deletions(-) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 03c93e295..1e60029ae 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -294,14 +294,8 @@ pub mod pallet { /// It maps from (DID identifier, service ID) to the service details. #[pallet::storage] #[pallet::getter(fn get_service_endpoints)] - pub type ServiceEndpoints = StorageDoubleMap< - _, - Twox64Concat, - DidIdentifierOf, - Blake2_128Concat, - ServiceEndpointId, - DidEndpoint, - >; + pub type ServiceEndpoints = + StorageDoubleMap<_, Twox64Concat, DidIdentifierOf, Blake2_128Concat, ServiceEndpointId, DidEndpoint>; /// Counter of service endpoints for each DID. /// @@ -399,7 +393,8 @@ pub mod pallet { ServiceNotPresent, /// One of the service endpoint details contains non-ASCII characters. InvalidServiceEncoding, - /// The number of service endpoints stored under the DID is larger than the number of endpoints to delete. + /// The number of service endpoints stored under the DID is larger than + /// the number of endpoints to delete. StoredEndpointsCountTooLarge, /// An error that is not supposed to take place, yet it happened. InternalError, @@ -862,7 +857,11 @@ pub mod pallet { /// - Writes: Did, ServiceEndpoints, DidEndpointsCount /// # #[pallet::weight(::WeightInfo::remove_service_endpoint(*endpoints_to_remove))] - pub fn remove_service_endpoint(origin: OriginFor, service_id: ServiceEndpointId, endpoints_to_remove: u32) -> DispatchResult { + pub fn remove_service_endpoint( + origin: OriginFor, + service_id: ServiceEndpointId, + endpoints_to_remove: u32, + ) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); ensure!( @@ -945,7 +944,11 @@ pub mod pallet { /// - Kills: Did entry associated to the DID identifier /// # #[pallet::weight(::WeightInfo::reclaim_deposit(*endpoints_to_remove))] - pub fn reclaim_deposit(origin: OriginFor, did_subject: DidIdentifierOf, endpoints_to_remove: u32) -> DispatchResult { + pub fn reclaim_deposit( + origin: OriginFor, + did_subject: DidIdentifierOf, + endpoints_to_remove: u32, + ) -> DispatchResult { let source = ensure_signed(origin)?; let did_entry = Did::::get(&did_subject).ok_or(Error::::DidNotPresent)?; @@ -1153,7 +1156,8 @@ impl Pallet { Error::::StoredEndpointsCountTooLarge ); - // This one can fail, albeit this should **never** be the case as we check for the preconditions above. + // This one can fail, albeit this should **never** be the case as we check for + // the preconditions above. let storage_kill_result = ServiceEndpoints::::remove_prefix(&did_subject, Some(current_endpoints_count)); // If some items are remaining, it means that there were more than diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 71bae649b..14cd31bbc 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -144,9 +144,7 @@ impl DidEndpoint { pub mod utils { use super::*; - pub(crate) fn validate_new_service_endpoints( - endpoints: &[DidEndpoint], - ) -> Result<(), InputError> { + pub(crate) fn validate_new_service_endpoints(endpoints: &[DidEndpoint]) -> Result<(), InputError> { // Check if up the maximum number of endpoints is provided. ensure!( endpoints.len() <= T::MaxNumberOfServicesPerDid::get().saturated_into(), diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 10e5a998a..468cc65df 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -251,8 +251,7 @@ fn check_successful_complete_creation() { details.new_service_details.len() ); assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did) - .saturated_into::(), + did::pallet::DidEndpointsCount::::get(&alice_did).saturated_into::(), details.new_service_details.len() ); @@ -679,8 +678,7 @@ fn check_max_limit_service_url_length_did_creation() { fn check_invalid_service_id_character_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let new_service_details = - DidEndpoint::new("å".bytes().collect(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + let new_service_details = DidEndpoint::new("å".bytes().collect(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = vec![new_service_details]; @@ -708,8 +706,7 @@ fn check_invalid_service_id_character_did_creation() { fn check_invalid_service_type_character_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let new_service_details = - DidEndpoint::new(b"id".to_vec(), vec!["å".bytes().collect()], vec![b"url".to_vec()]); + let new_service_details = DidEndpoint::new(b"id".to_vec(), vec!["å".bytes().collect()], vec![b"url".to_vec()]); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = vec![new_service_details]; @@ -737,8 +734,7 @@ fn check_invalid_service_type_character_did_creation() { fn check_invalid_service_url_character_did_creation() { let auth_key = get_sr25519_authentication_key(true); let alice_did = get_did_identifier_from_sr25519_key(auth_key.public()); - let new_service_details = - DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec!["å".bytes().collect()]); + let new_service_details = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec!["å".bytes().collect()]); let mut details = generate_base_did_creation_details::(alice_did, ACCOUNT_00); details.new_service_details = vec![new_service_details]; @@ -1718,10 +1714,7 @@ fn check_service_addition_no_prior_service_successful() { did::pallet::ServiceEndpoints::::iter_prefix(&alice_did).count(), 1 ); - assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did), - 1 - ); + assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did), 1); }); } @@ -1971,8 +1964,7 @@ fn check_max_service_url_count_addition_error() { fn check_invalid_service_id_character_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let new_service_details = - DidEndpoint::new("å".bytes().collect(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + let new_service_details = DidEndpoint::new("å".bytes().collect(), vec![b"type".to_vec()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -1991,8 +1983,7 @@ fn check_invalid_service_id_character_addition_error() { fn check_invalid_service_type_character_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let new_service_details = - DidEndpoint::new(b"id".to_vec(), vec!["å".bytes().collect()], vec![b"url".to_vec()]); + let new_service_details = DidEndpoint::new(b"id".to_vec(), vec!["å".bytes().collect()], vec![b"url".to_vec()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -2011,8 +2002,7 @@ fn check_invalid_service_type_character_addition_error() { fn check_invalid_service_url_character_addition_error() { let auth_key = get_ed25519_authentication_key(true); let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let new_service_details = - DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec!["å".bytes().collect()]); + let new_service_details = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec!["å".bytes().collect()]); let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); @@ -2148,10 +2138,7 @@ fn check_successful_deletion_with_endpoints() { .with_endpoints(vec![(alice_did.clone(), vec![service_endpoint])]) .build(None) .execute_with(|| { - assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did), - 1 - ); + assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did), 1); assert_eq!( Balances::reserved_balance(ACCOUNT_00), ::Deposit::get() @@ -2219,10 +2206,7 @@ fn check_successful_reclaiming() { .with_endpoints(vec![(alice_did.clone(), vec![old_service_endpoint])]) .build(None) .execute_with(|| { - assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did), - 1 - ); + assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did), 1); assert_eq!( Balances::reserved_balance(ACCOUNT_00), ::Deposit::get() @@ -2235,10 +2219,7 @@ fn check_successful_reclaiming() { assert!(Did::get_did(alice_did.clone()).is_none()); assert!(Did::get_deleted_did(alice_did.clone()).is_some()); assert!(Balances::reserved_balance(ACCOUNT_00).is_zero()); - assert_eq!( - did::pallet::DidEndpointsCount::::get(&alice_did), - 0 - ); + assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did), 0); // Re-adding the same DID identifier should fail. let details = generate_base_did_creation_details::(alice_did.clone(), ACCOUNT_00); From 96c0e177d13636bf6567bde153af59098b533764 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Mon, 25 Oct 2021 11:17:15 +0200 Subject: [PATCH 36/45] chore: mark all safe sections --- pallets/did/src/lib.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 1e60029ae..74ee4250a 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -546,6 +546,7 @@ pub mod pallet { DidDetails::from_creation_details(*details, account_did_auth_key).map_err(Error::::from)?; // *** No Fail beyond this call *** + CurrencyOf::::reserve(&did_entry.deposit.owner, did_entry.deposit.amount)?; // Withdraw the fee. We made sure that enough balance is available. But if this @@ -603,6 +604,8 @@ pub mod pallet { .update_authentication_key(new_key, frame_system::Pallet::::block_number()) .map_err(Error::::from)?; + // *** No Fail beyond this call *** + Did::::insert(&did_subject, did_details); log::debug!("Authentication key set"); @@ -636,6 +639,8 @@ pub mod pallet { .update_delegation_key(new_key, frame_system::Pallet::::block_number()) .map_err(Error::::from)?; + // *** No Fail beyond this call *** + Did::::insert(&did_subject, did_details); log::debug!("Delegation key set"); @@ -666,6 +671,8 @@ pub mod pallet { log::debug!("Removing delegation key for DID {:?}", &did_subject); did_details.remove_delegation_key().map_err(Error::::from)?; + // *** No Fail beyond this call *** + Did::::insert(&did_subject, did_details); log::debug!("Delegation key removed"); @@ -699,6 +706,8 @@ pub mod pallet { .update_attestation_key(new_key, frame_system::Pallet::::block_number()) .map_err(Error::::from)?; + // *** No Fail beyond this call *** + Did::::insert(&did_subject, did_details); log::debug!("Attestation key set"); @@ -729,6 +738,8 @@ pub mod pallet { log::debug!("Removing attestation key for DID {:?}", &did_subject); did_details.remove_attestation_key().map_err(Error::::from)?; + // *** No Fail beyond this call *** + Did::::insert(&did_subject, did_details); log::debug!("Attestation key removed"); @@ -760,6 +771,8 @@ pub mod pallet { .add_key_agreement_key(new_key, frame_system::Pallet::::block_number()) .map_err(Error::::from)?; + // *** No Fail beyond this call *** + Did::::insert(&did_subject, did_details); log::debug!("Key agreement key set"); @@ -788,6 +801,8 @@ pub mod pallet { log::debug!("Removing key agreement key for DID {:?}", &did_subject); did_details.remove_key_agreement_key(key_id).map_err(Error::::from)?; + // *** No Fail beyond this call *** + Did::::insert(&did_subject, did_details); log::debug!("Key agreement key removed"); @@ -864,11 +879,6 @@ pub mod pallet { ) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); - ensure!( - ServiceEndpoints::::take(&did_subject, &service_id).is_some(), - Error::::ServiceNotPresent - ); - let current_endpoints_count = DidEndpointsCount::::get(&did_subject); ensure!( current_endpoints_count <= endpoints_to_remove, @@ -877,6 +887,11 @@ pub mod pallet { // *** No Fail beyond this point *** + ensure!( + ServiceEndpoints::::take(&did_subject, &service_id).is_some(), + Error::::ServiceNotPresent + ); + // Decrease the endpoints counter or delete the entry if it reaches 0. let new_endpoints_count = current_endpoints_count.saturating_sub(1); if new_endpoints_count.is_zero() { @@ -950,7 +965,6 @@ pub mod pallet { endpoints_to_remove: u32, ) -> DispatchResult { let source = ensure_signed(origin)?; - let did_entry = Did::::get(&did_subject).ok_or(Error::::DidNotPresent)?; ensure!(did_entry.deposit.owner == source, Error::::NotOwnerOfDeposit); @@ -1033,6 +1047,8 @@ pub mod pallet { // Dispatch the referenced [Call] instance and return its result let DidAuthorizedCallOperation { did, call, .. } = wrapped_operation.operation; + // *** No Fail beyond this point *** + #[cfg(not(feature = "runtime-benchmarks"))] let result = call.dispatch( DidRawOrigin { @@ -1147,18 +1163,19 @@ impl Pallet { /// endpoints, adds the identifier to the blacklisted DIDs and frees the /// deposit. fn delete_did(did_subject: DidIdentifierOf, endpoints_to_remove: u32) -> DispatchResult { - // `take` calls `kill` internally - let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; - let current_endpoints_count = DidEndpointsCount::::get(&did_subject); ensure!( current_endpoints_count <= endpoints_to_remove, Error::::StoredEndpointsCountTooLarge ); + // *** No Fail beyond this point *** + + // `take` calls `kill` internally + let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; + // This one can fail, albeit this should **never** be the case as we check for // the preconditions above. - let storage_kill_result = ServiceEndpoints::::remove_prefix(&did_subject, Some(current_endpoints_count)); // If some items are remaining, it means that there were more than // the counter stored in `DidEndpointsCount`, and that should never happen. @@ -1166,8 +1183,6 @@ impl Pallet { return Err(Error::::InternalError.into()); }; - // *** No Fail beyond this point *** - DidEndpointsCount::::remove(&did_subject); kilt_support::free_deposit::, CurrencyOf>(&did_entry.deposit); // Mark as deleted to prevent potential replay-attacks of re-adding a previously From 36c92e357babbbd6f22f37946ca4904a248f27a2 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Mon, 25 Oct 2021 11:54:31 +0200 Subject: [PATCH 37/45] test: add tests + move DID removal after storage prefix deletion --- pallets/did/src/lib.rs | 6 ++-- pallets/did/src/tests.rs | 74 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index 74ee4250a..a6a465074 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -1171,9 +1171,6 @@ impl Pallet { // *** No Fail beyond this point *** - // `take` calls `kill` internally - let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; - // This one can fail, albeit this should **never** be the case as we check for // the preconditions above. let storage_kill_result = ServiceEndpoints::::remove_prefix(&did_subject, Some(current_endpoints_count)); @@ -1183,6 +1180,9 @@ impl Pallet { return Err(Error::::InternalError.into()); }; + // `take` calls `kill` internally + let did_entry = Did::::take(&did_subject).ok_or(Error::::DidNotPresent)?; + DidEndpointsCount::::remove(&did_subject); kilt_support::free_deposit::, CurrencyOf>(&did_entry.deposit); // Mark as deleted to prevent potential replay-attacks of re-adding a previously diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 468cc65df..2122a7040 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -2070,6 +2070,26 @@ fn check_service_not_present_deletion_error() { }); } +#[test] +fn check_too_small_service_count_deletion_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let old_service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + + let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + + ExtBuilder::default() + .with_dids(vec![(alice_did.clone(), old_did_details)]) + .with_endpoints(vec![(alice_did.clone(), vec![old_service_endpoint.clone()])]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::remove_service_endpoint(Origin::signed(alice_did.clone()), old_service_endpoint.id, 0), + did::Error::::StoredEndpointsCountTooLarge + ); + }); +} + // delete #[test] @@ -2185,6 +2205,33 @@ fn check_did_not_present_deletion() { }); } +#[test] +fn check_service_count_too_small_deletion_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + + let mut did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + did_details.deposit.owner = ACCOUNT_00; + did_details.deposit.amount = ::Deposit::get(); + + let balance = ::Deposit::get() * 2 + + ::Fee::get() * 2 + + <::Currency as Currency>>::minimum_balance(); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .with_dids(vec![(alice_did.clone(), did_details)]) + .with_endpoints(vec![(alice_did.clone(), vec![service_endpoint])]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::delete(Origin::signed(alice_did.clone()), 0), + did::Error::::StoredEndpointsCountTooLarge + ); + }); +} + // reclaim_deposit #[test] @@ -2265,6 +2312,33 @@ fn unauthorized_reclaiming() { }); } +#[test] +fn check_service_count_too_small_reclaim_error() { + let auth_key = get_ed25519_authentication_key(true); + let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); + let service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); + + let mut did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); + did_details.deposit.owner = ACCOUNT_00; + did_details.deposit.amount = ::Deposit::get(); + + let balance = ::Deposit::get() * 2 + + ::Fee::get() * 2 + + <::Currency as Currency>>::minimum_balance(); + + ExtBuilder::default() + .with_balances(vec![(ACCOUNT_00, balance)]) + .with_dids(vec![(alice_did.clone(), did_details)]) + .with_endpoints(vec![(alice_did.clone(), vec![service_endpoint])]) + .build(None) + .execute_with(|| { + assert_noop!( + Did::reclaim_deposit(Origin::signed(ACCOUNT_00.clone()), alice_did.clone(), 0), + did::Error::::StoredEndpointsCountTooLarge + ); + }); +} + // submit_did_call #[test] From fccda30a6f09c874d5e5c313c39fb51b555690f4 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 27 Oct 2021 09:06:52 +0200 Subject: [PATCH 38/45] replace function in try_for_each --- pallets/did/src/service_endpoints.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 14cd31bbc..659562be4 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -154,7 +154,7 @@ pub mod utils { // Then validate each service. endpoints .iter() - .try_for_each(|endpoint| endpoint.validate_against_constraints())?; + .try_for_each(DidEndpoint::::validate_against_constraints)?; Ok(()) } From 29c7e3387f6b360c19f16c0c4fb61008383df29b Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 27 Oct 2021 09:12:07 +0200 Subject: [PATCH 39/45] =?UTF-8?q?chore:=20replace=20try=5Ffor=5Feach=20to?= =?UTF-8?q?=20make=20it=20easier=20to=20read=20for=20some=20=F0=9F=91=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pallets/did/src/service_endpoints.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pallets/did/src/service_endpoints.rs b/pallets/did/src/service_endpoints.rs index 659562be4..fc5e7cc13 100644 --- a/pallets/did/src/service_endpoints.rs +++ b/pallets/did/src/service_endpoints.rs @@ -103,15 +103,14 @@ impl DidEndpoint { })?; // Check that all URLs are the maximum allowed length AND only contain ASCII // characters. - self.urls.iter().try_for_each(|s_url| { + for s_url in self.urls.iter() { ensure!( s_url.len() <= T::MaxServiceUrlLength::get().saturated_into(), InputError::MaxUrlLengthExceeded ); let str_url = str::from_utf8(s_url).map_err(|_| InputError::InvalidEncoding)?; ensure!(crate_utils::is_valid_ascii_string(str_url), InputError::InvalidEncoding); - Ok(()) - })?; + } Ok(()) } } From a7ddb29ce70df59b692702ea14e682eca0f6614d Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 27 Oct 2021 09:30:08 +0200 Subject: [PATCH 40/45] fix: remove endpoint count parameter from service endpoint removal extrinsic --- pallets/did/src/benchmarking.rs | 10 ++++------ pallets/did/src/lib.rs | 29 +++++++++++++---------------- pallets/did/src/tests.rs | 26 ++------------------------ 3 files changed, 19 insertions(+), 46 deletions(-) diff --git a/pallets/did/src/benchmarking.rs b/pallets/did/src/benchmarking.rs index 04bd693f3..c3140e33c 100644 --- a/pallets/did/src/benchmarking.rs +++ b/pallets/did/src/benchmarking.rs @@ -911,13 +911,11 @@ benchmarks! { } remove_service_endpoint { - let c in 1 .. T::MaxNumberOfServicesPerDid::get(); - let public_auth_key = get_ecdsa_public_authentication_key(); let did_subject: DidIdentifierOf = MultiSigner::from(public_auth_key.clone()).into_account().into(); // All set to max. let old_service_endpoints = get_service_endpoints::( - c, + T::MaxNumberOfServicesPerDid::get(), T::MaxServiceIdLength::get(), T::MaxNumberOfTypesPerService::get(), T::MaxServiceTypeLength::get(), @@ -929,18 +927,18 @@ benchmarks! { let did_details = generate_base_did_details::(DidVerificationKey::from(public_auth_key)); Did::::insert(&did_subject, did_details); save_service_endpoints(&did_subject, &old_service_endpoints); - }: _(RawOrigin::Signed(did_subject.clone()), endpoint_id.clone(), c) + }: _(RawOrigin::Signed(did_subject.clone()), endpoint_id.clone()) verify { assert!( ServiceEndpoints::::get(&did_subject, &endpoint_id).is_none() ); assert_eq!( DidEndpointsCount::::get(&did_subject), - c - 1 + T::MaxNumberOfServicesPerDid::get() - 1 ); assert_eq!( ServiceEndpoints::::iter_prefix(&did_subject).count(), - c.saturated_into::() - 1 + T::MaxNumberOfServicesPerDid::get().saturated_into::() - 1 ); } diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index a6a465074..eeef0f382 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -871,21 +871,16 @@ pub mod pallet { /// - Reads: [Origin Account], ServiceEndpoints, DidEndpointsCount /// - Writes: Did, ServiceEndpoints, DidEndpointsCount /// # - #[pallet::weight(::WeightInfo::remove_service_endpoint(*endpoints_to_remove))] + // #[pallet::weight(::WeightInfo::remove_service_endpoint())] + // TODO: replace + #[pallet::weight(1)] pub fn remove_service_endpoint( origin: OriginFor, - service_id: ServiceEndpointId, - endpoints_to_remove: u32, + service_id: ServiceEndpointId ) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); - let current_endpoints_count = DidEndpointsCount::::get(&did_subject); - ensure!( - current_endpoints_count <= endpoints_to_remove, - Error::::StoredEndpointsCountTooLarge - ); - - // *** No Fail beyond this point *** + // *** No Fail after the next call succeeds *** ensure!( ServiceEndpoints::::take(&did_subject, &service_id).is_some(), @@ -893,12 +888,14 @@ pub mod pallet { ); // Decrease the endpoints counter or delete the entry if it reaches 0. - let new_endpoints_count = current_endpoints_count.saturating_sub(1); - if new_endpoints_count.is_zero() { - DidEndpointsCount::::remove(&did_subject); - } else { - DidEndpointsCount::::insert(&did_subject, new_endpoints_count) - }; + DidEndpointsCount::::mutate_exists(&did_subject, |existing_endpoint_count| { + let new_value = existing_endpoint_count.unwrap_or_default().saturating_sub(1); + if new_value.is_zero() { + *existing_endpoint_count = None; + } else { + *existing_endpoint_count = Some(new_value); + } + }); Self::deposit_event(Event::DidUpdated(did_subject)); diff --git a/pallets/did/src/tests.rs b/pallets/did/src/tests.rs index 2122a7040..f4112b876 100644 --- a/pallets/did/src/tests.rs +++ b/pallets/did/src/tests.rs @@ -2035,8 +2035,7 @@ fn check_service_deletion_successful() { .execute_with(|| { assert_ok!(Did::remove_service_endpoint( Origin::signed(alice_did.clone()), - old_service_endpoint.id, - 1 + old_service_endpoint.id ),); // Counter should be deleted from the storage. assert_eq!(did::pallet::DidEndpointsCount::::get(&alice_did), 0); @@ -2062,34 +2061,13 @@ fn check_service_not_present_deletion_error() { assert_noop!( Did::remove_service_endpoint( Origin::signed(alice_did.clone()), - service_id.try_into().expect("Service ID to delete too long"), - 0 + service_id.try_into().expect("Service ID to delete too long") ), did::Error::::ServiceNotPresent ); }); } -#[test] -fn check_too_small_service_count_deletion_error() { - let auth_key = get_ed25519_authentication_key(true); - let alice_did = get_did_identifier_from_ed25519_key(auth_key.public()); - let old_service_endpoint = DidEndpoint::new(b"id".to_vec(), vec![b"type".to_vec()], vec![b"url".to_vec()]); - - let old_did_details = generate_base_did_details::(did::DidVerificationKey::from(auth_key.public())); - - ExtBuilder::default() - .with_dids(vec![(alice_did.clone(), old_did_details)]) - .with_endpoints(vec![(alice_did.clone(), vec![old_service_endpoint.clone()])]) - .build(None) - .execute_with(|| { - assert_noop!( - Did::remove_service_endpoint(Origin::signed(alice_did.clone()), old_service_endpoint.id, 0), - did::Error::::StoredEndpointsCountTooLarge - ); - }); -} - // delete #[test] From a1f1e1a74f9e4d18fa050960ad1ff84dfb2abdc0 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 27 Oct 2021 09:43:14 +0200 Subject: [PATCH 41/45] bench: re-run benchmarks without parameter for remove_service_endpoint extrinsic --- pallets/did/src/default_weights.rs | 220 ++++++++++++-------------- pallets/did/src/lib.rs | 9 +- runtimes/peregrine/src/weights/did.rs | 6 +- runtimes/spiritnet/src/weights/did.rs | 6 +- 4 files changed, 110 insertions(+), 131 deletions(-) diff --git a/pallets/did/src/default_weights.rs b/pallets/did/src/default_weights.rs index 5829b7bc4..7018f86fb 100644 --- a/pallets/did/src/default_weights.rs +++ b/pallets/did/src/default_weights.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for did //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-25, STEPS: {{cmd.steps}}\, REPEAT: {{cmd.repeat}}\, LOW RANGE: {{cmd.lowest_range_values}}\, HIGH RANGE: {{cmd.highest_range_values}}\ +//! DATE: 2021-10-27, 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: @@ -76,7 +76,7 @@ pub trait WeightInfo { fn remove_sr25519_key_agreement_key() -> Weight; fn remove_ecdsa_key_agreement_key() -> Weight; fn add_service_endpoint() -> Weight; - fn remove_service_endpoint(c: u32, ) -> Weight; + fn remove_service_endpoint() -> Weight; fn signature_verification_sr25519(l: u32, ) -> Weight; fn signature_verification_ed25519(l: u32, ) -> Weight; fn signature_verification_ecdsa(l: u32, ) -> Weight; @@ -85,198 +85,192 @@ pub trait WeightInfo { /// Weights for did using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { - fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (121_133_000_u64) - // Standard Error: 89_000 - .saturating_add((2_124_000_u64).saturating_mul(n as Weight)) - // Standard Error: 33_000 - .saturating_add((8_147_000_u64).saturating_mul(c as Weight)) + fn create_ed25519_keys(_n: u32, c: u32, ) -> Weight { + (436_583_000_u64) + // Standard Error: 1_760_000 + .saturating_add((9_755_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { - (124_207_000_u64) - // Standard Error: 117_000 - .saturating_add((2_129_000_u64).saturating_mul(n as Weight)) - // Standard Error: 44_000 - .saturating_add((8_584_000_u64).saturating_mul(c as Weight)) + (354_663_000_u64) + // Standard Error: 649_000 + .saturating_add((3_972_000_u64).saturating_mul(n as Weight)) + // Standard Error: 243_000 + .saturating_add((19_126_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (225_138_000_u64) - // Standard Error: 105_000 - .saturating_add((1_798_000_u64).saturating_mul(n as Weight)) - // Standard Error: 39_000 - .saturating_add((8_024_000_u64).saturating_mul(c as Weight)) + fn create_ecdsa_keys(_n: u32, c: u32, ) -> Weight { + (706_144_000_u64) + // Standard Error: 434_000 + .saturating_add((10_592_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete(c: u32, ) -> Weight { - (28_882_000_u64) - // Standard Error: 9_000 - .saturating_add((773_000_u64).saturating_mul(c as Weight)) + (37_058_000_u64) + // Standard Error: 22_000 + .saturating_add((2_646_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn reclaim_deposit(c: u32, ) -> Weight { - (32_879_000_u64) - // Standard Error: 14_000 - .saturating_add((765_000_u64).saturating_mul(c as Weight)) + (75_498_000_u64) + // Standard Error: 90_000 + .saturating_add((1_372_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn submit_did_call_ed25519_key() -> Weight { - (68_188_000_u64) + (94_698_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (70_542_000_u64) + (88_897_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (168_296_000_u64) + (250_190_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (37_110_000_u64) + (86_282_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (37_851_000_u64) + (124_704_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (42_199_000_u64) + (121_388_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (37_160_000_u64) + (113_934_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (35_938_000_u64) + (99_958_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (36_037_000_u64) + (121_038_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (33_833_000_u64) + (106_690_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (34_484_000_u64) + (110_408_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (34_224_000_u64) + (103_364_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (36_518_000_u64) + (114_004_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (36_799_000_u64) + (102_743_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (37_430_000_u64) + (99_226_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (32_892_000_u64) + (106_430_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (33_683_000_u64) + (101_260_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (33_292_000_u64) + (102_934_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (34_916_000_u64) + (106_259_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (35_928_000_u64) + (109_486_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (35_737_000_u64) + (100_199_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (33_353_000_u64) + (99_136_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (33_603_000_u64) + (114_495_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (33_492_000_u64) + (74_631_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (31_278_000_u64) + (52_909_000_u64) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } - fn remove_service_endpoint(c: u32, ) -> Weight { - (22_774_000_u64) - // Standard Error: 5_000 - .saturating_add((74_000_u64).saturating_mul(c as Weight)) + fn remove_service_endpoint() -> Weight { + (61_756_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (58_482_000_u64) + (123_502_000_u64) // Standard Error: 0 - .saturating_add((3_000_u64).saturating_mul(l as Weight)) + .saturating_add((5_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (56_187_000_u64) + (130_465_000_u64) // Standard Error: 0 - .saturating_add((1_000_u64).saturating_mul(l as Weight)) + .saturating_add((3_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (155_859_000_u64) + (286_956_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) @@ -285,198 +279,192 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { - fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (121_133_000_u64) - // Standard Error: 89_000 - .saturating_add((2_124_000_u64).saturating_mul(n as Weight)) - // Standard Error: 33_000 - .saturating_add((8_147_000_u64).saturating_mul(c as Weight)) + fn create_ed25519_keys(_n: u32, c: u32, ) -> Weight { + (436_583_000_u64) + // Standard Error: 1_760_000 + .saturating_add((9_755_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { - (124_207_000_u64) - // Standard Error: 117_000 - .saturating_add((2_129_000_u64).saturating_mul(n as Weight)) - // Standard Error: 44_000 - .saturating_add((8_584_000_u64).saturating_mul(c as Weight)) + (354_663_000_u64) + // Standard Error: 649_000 + .saturating_add((3_972_000_u64).saturating_mul(n as Weight)) + // Standard Error: 243_000 + .saturating_add((19_126_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } - fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (225_138_000_u64) - // Standard Error: 105_000 - .saturating_add((1_798_000_u64).saturating_mul(n as Weight)) - // Standard Error: 39_000 - .saturating_add((8_024_000_u64).saturating_mul(c as Weight)) + fn create_ecdsa_keys(_n: u32, c: u32, ) -> Weight { + (706_144_000_u64) + // Standard Error: 434_000 + .saturating_add((10_592_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete(c: u32, ) -> Weight { - (28_882_000_u64) - // Standard Error: 9_000 - .saturating_add((773_000_u64).saturating_mul(c as Weight)) + (37_058_000_u64) + // Standard Error: 22_000 + .saturating_add((2_646_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn reclaim_deposit(c: u32, ) -> Weight { - (32_879_000_u64) - // Standard Error: 14_000 - .saturating_add((765_000_u64).saturating_mul(c as Weight)) + (75_498_000_u64) + // Standard Error: 90_000 + .saturating_add((1_372_000_u64).saturating_mul(c as Weight)) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn submit_did_call_ed25519_key() -> Weight { - (68_188_000_u64) + (94_698_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (70_542_000_u64) + (88_897_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (168_296_000_u64) + (250_190_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (37_110_000_u64) + (86_282_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (37_851_000_u64) + (124_704_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (42_199_000_u64) + (121_388_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (37_160_000_u64) + (113_934_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (35_938_000_u64) + (99_958_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (36_037_000_u64) + (121_038_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (33_833_000_u64) + (106_690_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (34_484_000_u64) + (110_408_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (34_224_000_u64) + (103_364_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (36_518_000_u64) + (114_004_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (36_799_000_u64) + (102_743_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (37_430_000_u64) + (99_226_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (32_892_000_u64) + (106_430_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (33_683_000_u64) + (101_260_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (33_292_000_u64) + (102_934_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (34_916_000_u64) + (106_259_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (35_928_000_u64) + (109_486_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (35_737_000_u64) + (100_199_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (33_353_000_u64) + (99_136_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (33_603_000_u64) + (114_495_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (33_492_000_u64) + (74_631_000_u64) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (31_278_000_u64) + (52_909_000_u64) .saturating_add(RocksDbWeight::get().reads(3_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } - fn remove_service_endpoint(c: u32, ) -> Weight { - (22_774_000_u64) - // Standard Error: 5_000 - .saturating_add((74_000_u64).saturating_mul(c as Weight)) + fn remove_service_endpoint() -> Weight { + (61_756_000_u64) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (58_482_000_u64) + (123_502_000_u64) // Standard Error: 0 - .saturating_add((3_000_u64).saturating_mul(l as Weight)) + .saturating_add((5_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (56_187_000_u64) + (130_465_000_u64) // Standard Error: 0 - .saturating_add((1_000_u64).saturating_mul(l as Weight)) + .saturating_add((3_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (155_859_000_u64) + (286_956_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(RocksDbWeight::get().reads(1_u64)) diff --git a/pallets/did/src/lib.rs b/pallets/did/src/lib.rs index eeef0f382..1529ea2d0 100644 --- a/pallets/did/src/lib.rs +++ b/pallets/did/src/lib.rs @@ -871,13 +871,8 @@ pub mod pallet { /// - Reads: [Origin Account], ServiceEndpoints, DidEndpointsCount /// - Writes: Did, ServiceEndpoints, DidEndpointsCount /// # - // #[pallet::weight(::WeightInfo::remove_service_endpoint())] - // TODO: replace - #[pallet::weight(1)] - pub fn remove_service_endpoint( - origin: OriginFor, - service_id: ServiceEndpointId - ) -> DispatchResult { + #[pallet::weight(::WeightInfo::remove_service_endpoint())] + pub fn remove_service_endpoint(origin: OriginFor, service_id: ServiceEndpointId) -> DispatchResult { let did_subject = T::EnsureOrigin::ensure_origin(origin)?.subject(); // *** No Fail after the next call succeeds *** diff --git a/runtimes/peregrine/src/weights/did.rs b/runtimes/peregrine/src/weights/did.rs index f7a8703b4..044c0f631 100644 --- a/runtimes/peregrine/src/weights/did.rs +++ b/runtimes/peregrine/src/weights/did.rs @@ -218,10 +218,8 @@ impl did::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } - fn remove_service_endpoint(c: u32, ) -> Weight { - (22_774_000_u64) - // Standard Error: 5_000 - .saturating_add((74_000_u64).saturating_mul(c as Weight)) + fn remove_service_endpoint() -> Weight { + (61_756_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } diff --git a/runtimes/spiritnet/src/weights/did.rs b/runtimes/spiritnet/src/weights/did.rs index 504dd3930..aa4d838df 100644 --- a/runtimes/spiritnet/src/weights/did.rs +++ b/runtimes/spiritnet/src/weights/did.rs @@ -218,10 +218,8 @@ impl did::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } - fn remove_service_endpoint(c: u32, ) -> Weight { - (22_774_000_u64) - // Standard Error: 5_000 - .saturating_add((74_000_u64).saturating_mul(c as Weight)) + fn remove_service_endpoint() -> Weight { + (61_756_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } From 68b11f5cab1d57de87f9b590c0c07c0fd534378e Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Wed, 27 Oct 2021 16:06:28 +0200 Subject: [PATCH 42/45] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=spiritnet-dev --steps=50 --repeat=20 --pallet=did --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/spiritnet/src/weights/did.rs --template=.maintain/runtime-weight-template.hbs --- runtimes/spiritnet/src/weights/did.rs | 110 +++++++++++++------------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/runtimes/spiritnet/src/weights/did.rs b/runtimes/spiritnet/src/weights/did.rs index aa4d838df..5e95118c8 100644 --- a/runtimes/spiritnet/src/weights/did.rs +++ b/runtimes/spiritnet/src/weights/did.rs @@ -19,11 +19,11 @@ //! Autogenerated weights for did //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-17, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("spiritnet-dev"), DB CACHE: 128 // Executed Command: -// ./target/release/kilt-parachain +// target/release/kilt-parachain // benchmark // --chain=spiritnet-dev // --steps=50 @@ -48,197 +48,197 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl did::WeightInfo for WeightInfo { fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (121_133_000_u64) - // Standard Error: 89_000 - .saturating_add((2_124_000_u64).saturating_mul(n as Weight)) - // Standard Error: 33_000 - .saturating_add((8_147_000_u64).saturating_mul(c as Weight)) + (155_200_000_u64) + // Standard Error: 29_000 + .saturating_add((2_379_000_u64).saturating_mul(n as Weight)) + // Standard Error: 9_000 + .saturating_add((10_503_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { - (124_207_000_u64) - // Standard Error: 117_000 - .saturating_add((2_129_000_u64).saturating_mul(n as Weight)) - // Standard Error: 44_000 - .saturating_add((8_584_000_u64).saturating_mul(c as Weight)) + (158_638_000_u64) + // Standard Error: 31_000 + .saturating_add((2_385_000_u64).saturating_mul(n as Weight)) + // Standard Error: 9_000 + .saturating_add((10_847_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (225_138_000_u64) - // Standard Error: 105_000 - .saturating_add((1_798_000_u64).saturating_mul(n as Weight)) - // Standard Error: 39_000 - .saturating_add((8_024_000_u64).saturating_mul(c as Weight)) + (273_495_000_u64) + // Standard Error: 35_000 + .saturating_add((2_403_000_u64).saturating_mul(n as Weight)) + // Standard Error: 11_000 + .saturating_add((10_325_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete(c: u32, ) -> Weight { - (28_882_000_u64) - // Standard Error: 9_000 - .saturating_add((773_000_u64).saturating_mul(c as Weight)) + (41_042_000_u64) + // Standard Error: 4_000 + .saturating_add((1_095_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn reclaim_deposit(c: u32, ) -> Weight { - (32_879_000_u64) - // Standard Error: 14_000 - .saturating_add((765_000_u64).saturating_mul(c as Weight)) + (45_858_000_u64) + // Standard Error: 5_000 + .saturating_add((1_099_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn submit_did_call_ed25519_key() -> Weight { - (68_188_000_u64) + (86_443_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (70_542_000_u64) + (89_533_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (168_296_000_u64) + (204_492_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (37_110_000_u64) + (48_157_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (37_851_000_u64) + (48_387_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (42_199_000_u64) + (48_544_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (37_160_000_u64) + (47_631_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (35_938_000_u64) + (48_186_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (36_037_000_u64) + (48_042_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (33_833_000_u64) + (44_720_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (34_484_000_u64) + (44_742_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (34_224_000_u64) + (44_476_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (36_518_000_u64) + (48_241_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (36_799_000_u64) + (48_137_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (37_430_000_u64) + (48_281_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (32_892_000_u64) + (44_762_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (33_683_000_u64) + (44_854_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (33_292_000_u64) + (44_720_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (34_916_000_u64) + (47_004_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (35_928_000_u64) + (47_082_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (35_737_000_u64) + (47_217_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (33_353_000_u64) + (45_138_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (33_603_000_u64) + (44_988_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (33_492_000_u64) + (44_926_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (31_278_000_u64) + (43_394_000_u64) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn remove_service_endpoint() -> Weight { - (61_756_000_u64) + (34_936_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (58_482_000_u64) + (25_599_000_u64) // Standard Error: 0 - .saturating_add((3_000_u64).saturating_mul(l as Weight)) + .saturating_add((4_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (56_187_000_u64) + (21_307_000_u64) // Standard Error: 0 - .saturating_add((1_000_u64).saturating_mul(l as Weight)) + .saturating_add((2_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (155_859_000_u64) + (138_685_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } -} +} \ No newline at end of file From 0761ad29029a7975e7b580610be208e6a355b1f4 Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Wed, 27 Oct 2021 16:09:44 +0200 Subject: [PATCH 43/45] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=crowdloan --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/peregrine/src/weights/crowdloan.rs --template=.maintain/runtime-weight-template.hbs --- runtimes/peregrine/src/weights/crowdloan.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/runtimes/peregrine/src/weights/crowdloan.rs b/runtimes/peregrine/src/weights/crowdloan.rs index 8e02bb2ac..bc7877313 100644 --- a/runtimes/peregrine/src/weights/crowdloan.rs +++ b/runtimes/peregrine/src/weights/crowdloan.rs @@ -19,13 +19,13 @@ //! Autogenerated weights for crowdloan //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-20, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("spiritnet-dev"), DB CACHE: 128 +//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: // target/release/kilt-parachain // benchmark -// --chain=spiritnet-dev +// --chain=dev // --steps=50 // --repeat=20 // --pallet=crowdloan @@ -33,7 +33,7 @@ // --execution=wasm // --wasm-execution=compiled // --heap-pages=4096 -// --output=./runtimes/spiritnet/src/weights/crowdloan.rs +// --output=./runtimes/peregrine/src/weights/crowdloan.rs // --template=.maintain/runtime-weight-template.hbs @@ -48,18 +48,18 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl crowdloan::WeightInfo for WeightInfo { fn set_registrar_account() -> Weight { - (21_643_000_u64) + (20_512_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_contribution() -> Weight { - (24_614_000_u64) + (23_131_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_contribution() -> Weight { - (25_340_000_u64) + (24_773_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } -} +} \ No newline at end of file From 889784beee874b8a84b8a6d35deee0939836cf8b Mon Sep 17 00:00:00 2001 From: kiltbot <> Date: Wed, 27 Oct 2021 16:16:13 +0200 Subject: [PATCH 44/45] cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=did --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/peregrine/src/weights/did.rs --template=.maintain/runtime-weight-template.hbs --- runtimes/peregrine/src/weights/did.rs | 108 +++++++++++++------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/runtimes/peregrine/src/weights/did.rs b/runtimes/peregrine/src/weights/did.rs index 044c0f631..00f7b74a0 100644 --- a/runtimes/peregrine/src/weights/did.rs +++ b/runtimes/peregrine/src/weights/did.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for did //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-15, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -48,197 +48,197 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl did::WeightInfo for WeightInfo { fn create_ed25519_keys(n: u32, c: u32, ) -> Weight { - (121_133_000_u64) - // Standard Error: 89_000 - .saturating_add((2_124_000_u64).saturating_mul(n as Weight)) - // Standard Error: 33_000 - .saturating_add((8_147_000_u64).saturating_mul(c as Weight)) + (155_554_000_u64) + // Standard Error: 40_000 + .saturating_add((2_340_000_u64).saturating_mul(n as Weight)) + // Standard Error: 12_000 + .saturating_add((10_183_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_sr25519_keys(n: u32, c: u32, ) -> Weight { - (124_207_000_u64) - // Standard Error: 117_000 - .saturating_add((2_129_000_u64).saturating_mul(n as Weight)) - // Standard Error: 44_000 - .saturating_add((8_584_000_u64).saturating_mul(c as Weight)) + (157_851_000_u64) + // Standard Error: 27_000 + .saturating_add((2_477_000_u64).saturating_mul(n as Weight)) + // Standard Error: 8_000 + .saturating_add((10_523_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn create_ecdsa_keys(n: u32, c: u32, ) -> Weight { - (225_138_000_u64) - // Standard Error: 105_000 - .saturating_add((1_798_000_u64).saturating_mul(n as Weight)) - // Standard Error: 39_000 - .saturating_add((8_024_000_u64).saturating_mul(c as Weight)) + (275_184_000_u64) + // Standard Error: 62_000 + .saturating_add((2_307_000_u64).saturating_mul(n as Weight)) + // Standard Error: 19_000 + .saturating_add((9_953_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn delete(c: u32, ) -> Weight { - (28_882_000_u64) - // Standard Error: 9_000 - .saturating_add((773_000_u64).saturating_mul(c as Weight)) + (40_970_000_u64) + // Standard Error: 4_000 + .saturating_add((1_039_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn reclaim_deposit(c: u32, ) -> Weight { - (32_879_000_u64) - // Standard Error: 14_000 - .saturating_add((765_000_u64).saturating_mul(c as Weight)) + (45_659_000_u64) + // Standard Error: 5_000 + .saturating_add((1_037_000_u64).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c as Weight))) } fn submit_did_call_ed25519_key() -> Weight { - (68_188_000_u64) + (85_657_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_sr25519_key() -> Weight { - (70_542_000_u64) + (88_121_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn submit_did_call_ecdsa_key() -> Weight { - (168_296_000_u64) + (203_208_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_authentication_key() -> Weight { - (37_110_000_u64) + (47_523_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_authentication_key() -> Weight { - (37_851_000_u64) + (47_139_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_authentication_key() -> Weight { - (42_199_000_u64) + (47_445_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_delegation_key() -> Weight { - (37_160_000_u64) + (47_109_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_delegation_key() -> Weight { - (35_938_000_u64) + (47_153_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_delegation_key() -> Weight { - (36_037_000_u64) + (47_180_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_delegation_key() -> Weight { - (33_833_000_u64) + (43_349_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_delegation_key() -> Weight { - (34_484_000_u64) + (43_782_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_delegation_key() -> Weight { - (34_224_000_u64) + (43_469_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ed25519_attestation_key() -> Weight { - (36_518_000_u64) + (47_008_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_sr25519_attestation_key() -> Weight { - (36_799_000_u64) + (46_791_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_ecdsa_attestation_key() -> Weight { - (37_430_000_u64) + (46_654_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_attestation_key() -> Weight { - (32_892_000_u64) + (43_206_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_attestation_key() -> Weight { - (33_683_000_u64) + (43_339_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_attestation_key() -> Weight { - (33_292_000_u64) + (43_217_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ed25519_key_agreement_key() -> Weight { - (34_916_000_u64) + (45_677_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_sr25519_key_agreement_key() -> Weight { - (35_928_000_u64) + (45_880_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_ecdsa_key_agreement_key() -> Weight { - (35_737_000_u64) + (45_968_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ed25519_key_agreement_key() -> Weight { - (33_353_000_u64) + (43_709_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_sr25519_key_agreement_key() -> Weight { - (33_603_000_u64) + (43_659_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_ecdsa_key_agreement_key() -> Weight { - (33_492_000_u64) + (43_747_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn add_service_endpoint() -> Weight { - (31_278_000_u64) + (43_232_000_u64) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn remove_service_endpoint() -> Weight { - (61_756_000_u64) + (34_532_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn signature_verification_sr25519(l: u32, ) -> Weight { - (58_482_000_u64) + (25_823_000_u64) // Standard Error: 0 - .saturating_add((3_000_u64).saturating_mul(l as Weight)) + .saturating_add((4_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ed25519(l: u32, ) -> Weight { - (56_187_000_u64) + (23_103_000_u64) // Standard Error: 0 - .saturating_add((1_000_u64).saturating_mul(l as Weight)) + .saturating_add((2_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn signature_verification_ecdsa(l: u32, ) -> Weight { - (155_859_000_u64) + (141_265_000_u64) // Standard Error: 0 .saturating_add((1_000_u64).saturating_mul(l as Weight)) .saturating_add(T::DbWeight::get().reads(1_u64)) } -} +} \ No newline at end of file From 01d44fdc98b9dab9714df735073c08ea5692ca36 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 27 Oct 2021 16:13:12 +0200 Subject: [PATCH 45/45] Revert "cargo run --quiet --release -p kilt-parachain --features=runtime-benchmarks -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=crowdloan --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./runtimes/peregrine/src/weights/crowdloan.rs --template=.maintain/runtime-weight-template.hbs --- runtimes/peregrine/src/weights/crowdloan.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/runtimes/peregrine/src/weights/crowdloan.rs b/runtimes/peregrine/src/weights/crowdloan.rs index bc7877313..8e02bb2ac 100644 --- a/runtimes/peregrine/src/weights/crowdloan.rs +++ b/runtimes/peregrine/src/weights/crowdloan.rs @@ -19,13 +19,13 @@ //! Autogenerated weights for crowdloan //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 +//! DATE: 2021-10-20, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("spiritnet-dev"), DB CACHE: 128 // Executed Command: // target/release/kilt-parachain // benchmark -// --chain=dev +// --chain=spiritnet-dev // --steps=50 // --repeat=20 // --pallet=crowdloan @@ -33,7 +33,7 @@ // --execution=wasm // --wasm-execution=compiled // --heap-pages=4096 -// --output=./runtimes/peregrine/src/weights/crowdloan.rs +// --output=./runtimes/spiritnet/src/weights/crowdloan.rs // --template=.maintain/runtime-weight-template.hbs @@ -48,18 +48,18 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl crowdloan::WeightInfo for WeightInfo { fn set_registrar_account() -> Weight { - (20_512_000_u64) + (21_643_000_u64) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn set_contribution() -> Weight { - (23_131_000_u64) + (24_614_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } fn remove_contribution() -> Weight { - (24_773_000_u64) + (25_340_000_u64) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } -} \ No newline at end of file +}