From 91b30bc5417fd2b1fa50ce93fe1a21c0c8ca7d40 Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Mon, 16 Dec 2024 13:24:14 +1300 Subject: [PATCH 01/20] create cargo package for new sylo pallet --- Cargo.lock | 23 ++++++++++++ Cargo.toml | 1 + pallet/sylo/Cargo.toml | 48 ++++++++++++++++++++++++ pallet/sylo/src/lib.rs | 81 ++++++++++++++++++++++++++++++++++++++++ pallet/sylo/src/mock.rs | 59 +++++++++++++++++++++++++++++ pallet/sylo/src/tests.rs | 27 ++++++++++++++ pallet/sylo/src/types.rs | 37 ++++++++++++++++++ 7 files changed, 276 insertions(+) create mode 100644 pallet/sylo/Cargo.toml create mode 100644 pallet/sylo/src/lib.rs create mode 100644 pallet/sylo/src/mock.rs create mode 100644 pallet/sylo/src/tests.rs create mode 100644 pallet/sylo/src/types.rs diff --git a/Cargo.lock b/Cargo.lock index 282588f08..a94ff6878 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6340,6 +6340,29 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-sylo" +version = "0.0.1" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "hex", + "pallet-assets", + "pallet-assets-ext", + "pallet-balances", + "parity-scale-codec", + "scale-info", + "seed-pallet-common", + "seed-primitives", + "serde", + "sp-arithmetic", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", +] + [[package]] name = "pallet-timestamp" version = "4.0.0-dev" diff --git a/Cargo.toml b/Cargo.toml index a4711f8f2..a805cb475 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -132,6 +132,7 @@ pallet-nft-peg = { path = "pallet/nft-peg", default-features = false } pallet-sft = { path = "pallet/sft", default-features = false } pallet-sft-rpc = { path = "pallet/sft/rpc", default-features = false } pallet-sft-rpc-runtime-api = { path = "pallet/sft/rpc/runtime-api", default-features = false } +pallet-sylo = { path = "pallet/sylo", default-features = false } pallet-token-approvals = { path = "pallet/token-approvals", default-features = false } pallet-tx-fee-pot = { path = "pallet/tx-fee-pot", default-features = false } pallet-vortex-distribution = { path = "pallet/vortex-distribution", default-features = false } diff --git a/pallet/sylo/Cargo.toml b/pallet/sylo/Cargo.toml new file mode 100644 index 000000000..5c1ff54f5 --- /dev/null +++ b/pallet/sylo/Cargo.toml @@ -0,0 +1,48 @@ +[package] +name = "pallet-sylo" +version = "0.0.1" +description = "Root Network Sylo Pallet" +authors.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true + +[dependencies] +hex = { workspace = true } +serde = { workspace = true } +scale-info = { workspace = true } +codec = { workspace = true } + +frame-support = { workspace = true } +frame-system = { workspace = true } +frame-benchmarking = { workspace = true, optional = true } +sp-core = { workspace = true } +sp-io = { workspace = true } +sp-runtime = { workspace = true } +sp-arithmetic = { workspace = true } +sp-std = { workspace = true } + +seed-primitives = { workspace = true } +seed-pallet-common = { workspace = true } + +[dev-dependencies] +sp-io = { workspace = true } +pallet-assets = { workspace = true } +pallet-balances = { workspace = true } +pallet-assets-ext = { workspace = true, default-features = true } + +[features] +default = ["std"] +std = [ + "codec/std", + "sp-runtime/std", + "frame-support/std", + "frame-system/std", + "sp-std/std", + "seed-primitives/std", + "seed-pallet-common/std", + "sp-io/std", + "frame-benchmarking?/std" +] +runtime-benchmarks = ["frame-benchmarking"] +try-runtime = ["frame-support/try-runtime"] diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs new file mode 100644 index 000000000..b247d9649 --- /dev/null +++ b/pallet/sylo/src/lib.rs @@ -0,0 +1,81 @@ +// Copyright 2022-2023 Futureverse Corporation Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// You may obtain a copy of the License at the root of this project source code +#![cfg_attr(not(feature = "std"), no_std)] + +pub use pallet::*; + +use frame_support::{ + pallet_prelude::*, + traits::{ + fungibles::{self, metadata::Inspect as MetadataInspect, Inspect, Mutate}, + tokens::{Fortitude, Precision, Preservation}, + }, + transactional, PalletId, +}; +use frame_system::pallet_prelude::*; +use scale_info::TypeInfo; +use seed_pallet_common::CreateExt; +use seed_primitives::{AssetId, Balance}; +use serde::{Deserialize, Serialize}; +use sp_core::{H160, U256}; +use sp_runtime::{ + traits::{AccountIdConversion, Zero}, + ArithmeticError, DispatchError, FixedU128, RuntimeDebug, SaturatedConversion, +}; +use sp_std::{cmp::min, convert::TryInto, prelude::*, vec}; + +#[cfg(test)] +mod mock; +#[cfg(test)] +mod tests; +pub mod types; + +#[frame_support::pallet] +pub mod pallet { + use super::*; + + /// The current storage version. + const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + + #[pallet::config] + pub trait Config: frame_system::Config + where + ::AccountId: From, + { + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + } + + #[pallet::error] + pub enum Error {} + + #[pallet::event] + #[pallet::generate_deposit(pub(crate) fn deposit_event)] + pub enum Event + where + ::AccountId: From, {} + + #[pallet::pallet] + #[pallet::storage_version(STORAGE_VERSION)] + pub struct Pallet(_); + + #[pallet::hooks] + impl Hooks> for Pallet where + ::AccountId: From + { + } + + #[pallet::call] + impl Pallet where ::AccountId: From {} +} diff --git a/pallet/sylo/src/mock.rs b/pallet/sylo/src/mock.rs new file mode 100644 index 000000000..b0592287c --- /dev/null +++ b/pallet/sylo/src/mock.rs @@ -0,0 +1,59 @@ +// Copyright 2022-2023 Futureverse Corporation Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// You may obtain a copy of the License at the root of this project source code + +use super::*; +use crate::{self as pallet_dex}; +use seed_pallet_common::test_prelude::*; + +construct_runtime!( + pub enum Test { + System: frame_system, + Balances: pallet_balances, + Assets: pallet_assets, + AssetsExt: pallet_assets_ext, + Dex: pallet_dex, + } +); + +impl_frame_system_config!(Test); +impl_pallet_balance_config!(Test); +impl_pallet_assets_config!(Test); +impl_pallet_assets_ext_config!(Test); + +parameter_types! { + pub const GetExchangeFee: (u32, u32) = (3, 1000); // 0.3% fee + pub const TradingPathLimit: u32 = 3; + pub const DEXBurnPalletId: PalletId = PalletId(*b"burnaddr"); + pub const LPTokenDecimals: u8 = 6; + pub const TxFeePotId: PalletId = PalletId(*b"txfeepot"); + pub const DefaultFeeTo: Option = Some(TxFeePotId::get()); +} +impl Config for Test { + type RuntimeEvent = RuntimeEvent; +} + +#[derive(Default)] +pub struct TestExt; + +impl TestExt { + pub fn build(self) -> sp_io::TestExternalities { + let storage = frame_system::GenesisConfig::::default().build_storage().unwrap(); + + let mut ext: sp_io::TestExternalities = storage.into(); + ext.execute_with(|| System::initialize(&1, &[0u8; 32].into(), &Default::default())); + ext.execute_with(|| pallet_assets_ext::GenesisConfig::::default().build()); + ext + } +} diff --git a/pallet/sylo/src/tests.rs b/pallet/sylo/src/tests.rs new file mode 100644 index 000000000..a5d3a8919 --- /dev/null +++ b/pallet/sylo/src/tests.rs @@ -0,0 +1,27 @@ +// Copyright 2022-2023 Futureverse Corporation Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// You may obtain a copy of the License at the root of this project source code + +use super::*; +use crate::mock::AssetsExt; +use hex::encode; +use mock::{Dex, RuntimeEvent as MockEvent, RuntimeOrigin, System, Test, TestExt}; +use seed_pallet_common::test_prelude::*; +use sp_arithmetic::helpers_128bit::sqrt; +use std::str::FromStr; + +#[test] +fn test_run() { + TestExt.build().execute_with(|| assert_eq!(1, 1)); +} diff --git a/pallet/sylo/src/types.rs b/pallet/sylo/src/types.rs new file mode 100644 index 000000000..a9fe19b98 --- /dev/null +++ b/pallet/sylo/src/types.rs @@ -0,0 +1,37 @@ +// Copyright 2022-2023 Futureverse Corporation Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// You may obtain a copy of the License at the root of this project source code + +use codec::{Decode, Encode, MaxEncodedLen}; +use frame_support::{traits::Get, BoundedVec, CloneNoBound, PartialEqNoBound, RuntimeDebug}; +use scale_info::TypeInfo; +use seed_primitives::{AssetId, Balance}; +use sp_core::{H160, H256}; +use sp_std::default::Default; + +pub type ResolverId = BoundedVec; + +#[derive(Clone, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] +pub struct ValidationRecord +where + MaxResolvers: Get, + MaxTags: Get, + StringLimit: Get, +{ + pub author: AccountId, + pub resolvers: BoundedVec, MaxResolvers>, + pub data_type: BoundedVec, + pub algorithm: BoundedVec, + pub tags: BoundedVec, MaxTags>, +} From 5a7b6754bc0c69ef19a596ff075d5d56c3ec2f1e Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Mon, 16 Dec 2024 17:56:28 +1300 Subject: [PATCH 02/20] implement pallet functionality for registering resolvers --- pallet/sylo/src/lib.rs | 156 ++++++++++++++++++++++++++++++++++----- pallet/sylo/src/mock.rs | 16 ++-- pallet/sylo/src/tests.rs | 1 - pallet/sylo/src/types.rs | 32 +++++++- 4 files changed, 177 insertions(+), 28 deletions(-) diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index b247d9649..c2495683a 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -42,40 +42,162 @@ mod mock; mod tests; pub mod types; +pub use types::*; + #[frame_support::pallet] pub mod pallet { + use serde::ser; + use super::*; /// The current storage version. const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + #[pallet::pallet] + #[pallet::storage_version(STORAGE_VERSION)] + pub struct Pallet(_); + #[pallet::config] - pub trait Config: frame_system::Config - where - ::AccountId: From, - { + pub trait Config: frame_system::Config { type RuntimeEvent: From> + IsType<::RuntimeEvent>; + + #[pallet::constant] + type MaxServiceEndpoints: Get; + + #[pallet::constant] + type StringLimit: Get; + + #[pallet::constant] + type ResolverMethod: Get<[u8; 9]>; } + #[pallet::storage] + pub type Resolvers = StorageMap< + _, + Twox64Concat, + ResolverId, + Resolver, + >; + #[pallet::error] - pub enum Error {} + pub enum Error { + /// The Resolver identifier is already in use + ResolverAlreadyRegistered, + /// The Resolver has not been registered + ResolverNotRegistered, + /// Account is not controller of resolver + NotController, + } #[pallet::event] #[pallet::generate_deposit(pub(crate) fn deposit_event)] - pub enum Event - where - ::AccountId: From, {} + pub enum Event { + ResolverRegistered { + id: Vec, + controller: T::AccountId, + service_endpoints: BoundedVec, T::MaxServiceEndpoints>, + }, + ResolverUpdated { + id: Vec, + controller: T::AccountId, + service_endpoints: BoundedVec, T::MaxServiceEndpoints>, + }, + ResolverUnregistered { + id: Vec, + }, + } - #[pallet::pallet] - #[pallet::storage_version(STORAGE_VERSION)] - pub struct Pallet(_); + #[pallet::call] + impl Pallet { + #[pallet::call_index(0)] + #[pallet::weight(1_000)] + pub fn register_resolver( + origin: OriginFor, + identifier: BoundedVec, + service_endpoints: BoundedVec, T::MaxServiceEndpoints>, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + let resolver_id = Self::get_reserved_resolver_id(identifier.clone()); + + ensure!( + >::get(resolver_id.clone()).is_none(), + Error::::ResolverAlreadyRegistered + ); + + let resolver = + Resolver { controller: who.clone(), service_endpoints: service_endpoints.clone() }; - #[pallet::hooks] - impl Hooks> for Pallet where - ::AccountId: From - { + >::insert(resolver_id, resolver); + + Self::deposit_event(Event::ResolverRegistered { + id: identifier.to_vec(), + controller: who, + service_endpoints, + }); + + Ok(()) + } + + #[pallet::call_index(1)] + #[pallet::weight(1_000)] + pub fn update_resolver( + origin: OriginFor, + identifier: BoundedVec, + service_endpoints: BoundedVec, T::MaxServiceEndpoints>, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + let resolver_id = Self::get_reserved_resolver_id(identifier.clone()); + let mut resolver = >::get(resolver_id.clone()) + .ok_or(Error::::ResolverNotRegistered)?; + + ensure!(who == resolver.controller, Error::::NotController); + + resolver.service_endpoints = service_endpoints.clone(); + + >::insert(resolver_id, resolver); + + Self::deposit_event(Event::ResolverUpdated { + id: identifier.to_vec(), + controller: who, + service_endpoints, + }); + + Ok(()) + } + + #[pallet::call_index(2)] + #[pallet::weight(1_000)] + pub fn unregister_resolver( + origin: OriginFor, + identifier: BoundedVec, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + let resolver_id = Self::get_reserved_resolver_id(identifier.clone()); + let mut resolver = >::get(resolver_id.clone()) + .ok_or(Error::::ResolverNotRegistered)?; + + ensure!(who == resolver.controller, Error::::NotController); + + >::remove(resolver_id); + + Self::deposit_event(Event::ResolverUnregistered { id: identifier.to_vec() }); + + Ok(()) + } } - #[pallet::call] - impl Pallet where ::AccountId: From {} + impl Pallet { + pub fn get_reserved_resolver_id( + identifier: BoundedVec, + ) -> ResolverId { + let method: BoundedVec = + BoundedVec::try_from(::ResolverMethod::get().to_vec()) + .expect("Failed to convert invalid resolver method config"); + + ResolverId { method, identifier } + } + } } diff --git a/pallet/sylo/src/mock.rs b/pallet/sylo/src/mock.rs index b0592287c..a65d3b71f 100644 --- a/pallet/sylo/src/mock.rs +++ b/pallet/sylo/src/mock.rs @@ -14,7 +14,7 @@ // You may obtain a copy of the License at the root of this project source code use super::*; -use crate::{self as pallet_dex}; +use crate::{self as pallet_sylo}; use seed_pallet_common::test_prelude::*; construct_runtime!( @@ -23,7 +23,7 @@ construct_runtime!( Balances: pallet_balances, Assets: pallet_assets, AssetsExt: pallet_assets_ext, - Dex: pallet_dex, + Dex: pallet_sylo, } ); @@ -33,15 +33,15 @@ impl_pallet_assets_config!(Test); impl_pallet_assets_ext_config!(Test); parameter_types! { - pub const GetExchangeFee: (u32, u32) = (3, 1000); // 0.3% fee - pub const TradingPathLimit: u32 = 3; - pub const DEXBurnPalletId: PalletId = PalletId(*b"burnaddr"); - pub const LPTokenDecimals: u8 = 6; - pub const TxFeePotId: PalletId = PalletId(*b"txfeepot"); - pub const DefaultFeeTo: Option = Some(TxFeePotId::get()); + pub const MaxServiceEndpoints: u32 = 10; + pub const StringLimit: u32 = 100; + pub const ResolverMethod: [u8; 9] = *b"sylo-data"; } impl Config for Test { type RuntimeEvent = RuntimeEvent; + type MaxServiceEndpoints = MaxServiceEndpoints; + type StringLimit = StringLimit; + type ResolverMethod = ResolverMethod; } #[derive(Default)] diff --git a/pallet/sylo/src/tests.rs b/pallet/sylo/src/tests.rs index a5d3a8919..1cd5c00db 100644 --- a/pallet/sylo/src/tests.rs +++ b/pallet/sylo/src/tests.rs @@ -14,7 +14,6 @@ // You may obtain a copy of the License at the root of this project source code use super::*; -use crate::mock::AssetsExt; use hex::encode; use mock::{Dex, RuntimeEvent as MockEvent, RuntimeOrigin, System, Test, TestExt}; use seed_pallet_common::test_prelude::*; diff --git a/pallet/sylo/src/types.rs b/pallet/sylo/src/types.rs index a9fe19b98..2dda15ea0 100644 --- a/pallet/sylo/src/types.rs +++ b/pallet/sylo/src/types.rs @@ -14,13 +14,41 @@ // You may obtain a copy of the License at the root of this project source code use codec::{Decode, Encode, MaxEncodedLen}; -use frame_support::{traits::Get, BoundedVec, CloneNoBound, PartialEqNoBound, RuntimeDebug}; +use frame_support::{ + parameter_types, traits::Get, BoundedVec, CloneNoBound, PartialEqNoBound, RuntimeDebug, +}; use scale_info::TypeInfo; use seed_primitives::{AssetId, Balance}; use sp_core::{H160, H256}; use sp_std::default::Default; -pub type ResolverId = BoundedVec; +parameter_types! { + pub const MethodLimit: u32 = 32; + pub const IdentifierLimit: u32 = 64; +} + +#[derive(CloneNoBound, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] +#[scale_info(skip_type_params(StringLimit))] +pub struct ResolverId +where + StringLimit: Get, +{ + pub method: BoundedVec, + pub identifier: BoundedVec, +} + +pub type ServiceEndpoint> = BoundedVec; + +#[derive(Clone, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] +#[scale_info(skip_type_params(MaxServiceEndpoints, StringLimit))] +pub struct Resolver +where + MaxServiceEndpoints: Get, + StringLimit: Get, +{ + pub controller: AccountId, + pub service_endpoints: BoundedVec, MaxServiceEndpoints>, +} #[derive(Clone, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] pub struct ValidationRecord From c1c16a70ca48f1390b6025728cd1ce3ff4beae84 Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Mon, 16 Dec 2024 18:48:55 +1300 Subject: [PATCH 03/20] implement tests for registering resolvers --- pallet/sylo/src/lib.rs | 22 ++-- pallet/sylo/src/mock.rs | 2 +- pallet/sylo/src/tests.rs | 219 ++++++++++++++++++++++++++++++++++++++- pallet/sylo/src/types.rs | 8 +- 4 files changed, 230 insertions(+), 21 deletions(-) diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index c2495683a..6d43a1141 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -75,7 +75,7 @@ pub mod pallet { pub type Resolvers = StorageMap< _, Twox64Concat, - ResolverId, + BoundedVec, Resolver, >; @@ -118,17 +118,15 @@ pub mod pallet { ) -> DispatchResult { let who = ensure_signed(origin)?; - let resolver_id = Self::get_reserved_resolver_id(identifier.clone()); - ensure!( - >::get(resolver_id.clone()).is_none(), + >::get(identifier.clone()).is_none(), Error::::ResolverAlreadyRegistered ); let resolver = Resolver { controller: who.clone(), service_endpoints: service_endpoints.clone() }; - >::insert(resolver_id, resolver); + >::insert(identifier.clone(), resolver); Self::deposit_event(Event::ResolverRegistered { id: identifier.to_vec(), @@ -148,15 +146,14 @@ pub mod pallet { ) -> DispatchResult { let who = ensure_signed(origin)?; - let resolver_id = Self::get_reserved_resolver_id(identifier.clone()); - let mut resolver = >::get(resolver_id.clone()) - .ok_or(Error::::ResolverNotRegistered)?; + let mut resolver = + >::get(identifier.clone()).ok_or(Error::::ResolverNotRegistered)?; ensure!(who == resolver.controller, Error::::NotController); resolver.service_endpoints = service_endpoints.clone(); - >::insert(resolver_id, resolver); + >::insert(identifier.clone(), resolver); Self::deposit_event(Event::ResolverUpdated { id: identifier.to_vec(), @@ -175,13 +172,12 @@ pub mod pallet { ) -> DispatchResult { let who = ensure_signed(origin)?; - let resolver_id = Self::get_reserved_resolver_id(identifier.clone()); - let mut resolver = >::get(resolver_id.clone()) - .ok_or(Error::::ResolverNotRegistered)?; + let resolver = + >::get(identifier.clone()).ok_or(Error::::ResolverNotRegistered)?; ensure!(who == resolver.controller, Error::::NotController); - >::remove(resolver_id); + >::remove(identifier.clone()); Self::deposit_event(Event::ResolverUnregistered { id: identifier.to_vec() }); diff --git a/pallet/sylo/src/mock.rs b/pallet/sylo/src/mock.rs index a65d3b71f..6ca50561f 100644 --- a/pallet/sylo/src/mock.rs +++ b/pallet/sylo/src/mock.rs @@ -23,7 +23,7 @@ construct_runtime!( Balances: pallet_balances, Assets: pallet_assets, AssetsExt: pallet_assets_ext, - Dex: pallet_sylo, + Sylo: pallet_sylo, } ); diff --git a/pallet/sylo/src/tests.rs b/pallet/sylo/src/tests.rs index 1cd5c00db..0d1ea8b58 100644 --- a/pallet/sylo/src/tests.rs +++ b/pallet/sylo/src/tests.rs @@ -15,12 +15,221 @@ use super::*; use hex::encode; -use mock::{Dex, RuntimeEvent as MockEvent, RuntimeOrigin, System, Test, TestExt}; +use mock::{RuntimeEvent as MockEvent, RuntimeOrigin, Sylo, System, Test, TestExt}; use seed_pallet_common::test_prelude::*; use sp_arithmetic::helpers_128bit::sqrt; -use std::str::FromStr; -#[test] -fn test_run() { - TestExt.build().execute_with(|| assert_eq!(1, 1)); +mod resolvers { + use super::*; + + #[test] + fn resolver_registration_works() { + TestExt.build().execute_with(|| { + let (controller, identifier, service_endpoints) = create_and_register_resolver( + bounded_string("test-resolver"), + vec![ + bounded_string("https://endpoint.one"), + bounded_string("https://endpoint.two"), + ], + ); + + System::assert_last_event(MockEvent::Sylo(Event::::ResolverRegistered { + id: identifier.to_vec(), + controller: controller.clone(), + service_endpoints: service_endpoints.clone(), + })); + + assert_eq!( + Resolvers::::get(identifier).unwrap(), + Resolver { controller, service_endpoints } + ) + }); + } + + #[test] + fn resolver_update_works() { + TestExt.build().execute_with(|| { + let (controller, identifier, mut service_endpoints) = create_and_register_resolver( + bounded_string("test-resolver"), + vec![ + bounded_string("https://endpoint.one"), + bounded_string("https://endpoint.two"), + ], + ); + + service_endpoints.force_push(bounded_string("https://endpoint.three")); + + assert_ok!(Sylo::update_resolver( + RawOrigin::Signed(controller.clone()).into(), + identifier.clone(), + service_endpoints.clone(), + )); + + System::assert_last_event(MockEvent::Sylo(Event::::ResolverUpdated { + id: identifier.to_vec(), + controller: controller.clone(), + service_endpoints: service_endpoints.clone(), + })); + + assert_eq!( + Resolvers::::get(identifier).unwrap(), + Resolver { controller, service_endpoints } + ) + }); + } + + #[test] + fn resolver_unregistration_works() { + TestExt.build().execute_with(|| { + let (controller, identifier, mut service_endpoints) = create_and_register_resolver( + bounded_string("test-resolver"), + vec![ + bounded_string("https://endpoint.one"), + bounded_string("https://endpoint.two"), + ], + ); + + assert_ok!(Sylo::unregister_resolver( + RawOrigin::Signed(controller.clone()).into(), + identifier.clone(), + )); + + System::assert_last_event(MockEvent::Sylo(Event::::ResolverUnregistered { + id: identifier.to_vec(), + })); + + assert!(Resolvers::::get(identifier).is_none()); + }); + } + + #[test] + fn resolver_register_existing_fails() { + TestExt.build().execute_with(|| { + let (controller, identifier, mut service_endpoints) = create_and_register_resolver( + bounded_string("test-resolver"), + vec![ + bounded_string("https://endpoint.one"), + bounded_string("https://endpoint.two"), + ], + ); + + assert_noop!( + Sylo::register_resolver( + RawOrigin::Signed(controller).into(), + identifier, + service_endpoints, + ), + Error::::ResolverAlreadyRegistered, + ); + }); + } + + #[test] + fn resolver_update_not_existing_fails() { + TestExt.build().execute_with(|| { + let controller: AccountId = create_account(1); + + let identifier = bounded_string("test-resolver"); + + let service_endpoints = + BoundedVec::<_, ::MaxServiceEndpoints>::try_from(vec![]).unwrap(); + + assert_noop!( + Sylo::update_resolver( + RawOrigin::Signed(controller).into(), + identifier, + service_endpoints, + ), + Error::::ResolverNotRegistered, + ); + }); + } + + #[test] + fn resolver_update_not_controller_fails() { + TestExt.build().execute_with(|| { + let (controller, identifier, service_endpoints) = create_and_register_resolver( + bounded_string("test-resolver"), + vec![ + bounded_string("https://endpoint.one"), + bounded_string("https://endpoint.two"), + ], + ); + + let not_controller: AccountId = create_account(2); + + assert_noop!( + Sylo::update_resolver( + RawOrigin::Signed(not_controller).into(), + identifier, + service_endpoints, + ), + Error::::NotController, + ); + }); + } + + #[test] + fn resolver_unregister_not_existing_fails() { + TestExt.build().execute_with(|| { + let controller: AccountId = create_account(1); + + let identifier = bounded_string("test-resolver"); + + assert_noop!( + Sylo::unregister_resolver(RawOrigin::Signed(controller).into(), identifier,), + Error::::ResolverNotRegistered, + ); + }); + } + + #[test] + fn resolver_unregister_not_controller_fails() { + TestExt.build().execute_with(|| { + let (controller, identifier, service_endpoints) = create_and_register_resolver( + bounded_string("test-resolver"), + vec![ + bounded_string("https://endpoint.one"), + bounded_string("https://endpoint.two"), + ], + ); + + let not_controller: AccountId = create_account(2); + + assert_noop!( + Sylo::unregister_resolver(RawOrigin::Signed(not_controller).into(), identifier,), + Error::::NotController, + ); + }); + } +} + +fn create_and_register_resolver( + identifier: BoundedVec::StringLimit>, + service_endpoints: Vec::StringLimit>>, +) -> ( + AccountId, + BoundedVec::StringLimit>, + BoundedVec< + BoundedVec::StringLimit>, + ::MaxServiceEndpoints, + >, +) { + let controller: AccountId = create_account(1); + + let service_endpoints = + BoundedVec::<_, ::MaxServiceEndpoints>::try_from(service_endpoints) + .unwrap(); + + assert_ok!(Sylo::register_resolver( + RawOrigin::Signed(controller.clone()).into(), + identifier.clone(), + service_endpoints.clone(), + )); + + (controller, identifier, service_endpoints) +} + +fn bounded_string(name: &str) -> BoundedVec::StringLimit> { + BoundedVec::truncate_from(name.as_bytes().to_vec()) } diff --git a/pallet/sylo/src/types.rs b/pallet/sylo/src/types.rs index 2dda15ea0..421e4e631 100644 --- a/pallet/sylo/src/types.rs +++ b/pallet/sylo/src/types.rs @@ -16,11 +16,12 @@ use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::{ parameter_types, traits::Get, BoundedVec, CloneNoBound, PartialEqNoBound, RuntimeDebug, + RuntimeDebugNoBound, }; use scale_info::TypeInfo; use seed_primitives::{AssetId, Balance}; use sp_core::{H160, H256}; -use sp_std::default::Default; +use sp_std::{fmt::Debug, prelude::*}; parameter_types! { pub const MethodLimit: u32 = 32; @@ -39,10 +40,13 @@ where pub type ServiceEndpoint> = BoundedVec; -#[derive(Clone, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] +#[derive( + Clone, Encode, Decode, RuntimeDebugNoBound, PartialEqNoBound, Eq, TypeInfo, MaxEncodedLen, +)] #[scale_info(skip_type_params(MaxServiceEndpoints, StringLimit))] pub struct Resolver where + AccountId: Debug + PartialEq + Clone, MaxServiceEndpoints: Get, StringLimit: Get, { From d1ef366b1ddf9597f3c7da38801085d154837335 Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Tue, 17 Dec 2024 13:18:34 +1300 Subject: [PATCH 04/20] implement functionality for record validation --- pallet/sylo/src/lib.rs | 196 +++++++++++++++++++++++++++++++++++++++ pallet/sylo/src/mock.rs | 6 ++ pallet/sylo/src/types.rs | 43 +++++++-- 3 files changed, 238 insertions(+), 7 deletions(-) diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 6d43a1141..43f62388e 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -47,6 +47,7 @@ pub use types::*; #[frame_support::pallet] pub mod pallet { use serde::ser; + use sp_core::H256; use super::*; @@ -61,6 +62,15 @@ pub mod pallet { pub trait Config: frame_system::Config { type RuntimeEvent: From> + IsType<::RuntimeEvent>; + #[pallet::constant] + type MaxResolvers: Get; + + #[pallet::constant] + type MaxTags: Get; + + #[pallet::constant] + type MaxEntries: Get; + #[pallet::constant] type MaxServiceEndpoints: Get; @@ -79,6 +89,23 @@ pub mod pallet { Resolver, >; + #[pallet::storage] + pub type ValidationRecords = StorageDoubleMap< + _, + Twox64Concat, + T::AccountId, + Twox64Concat, + BoundedVec, + ValidationRecord< + T::AccountId, + BlockNumberFor, + T::MaxResolvers, + T::MaxTags, + T::MaxEntries, + T::StringLimit, + >, + >; + #[pallet::error] pub enum Error { /// The Resolver identifier is already in use @@ -87,6 +114,10 @@ pub mod pallet { ResolverNotRegistered, /// Account is not controller of resolver NotController, + /// A validation record with the given data id has already been created + RecordAlreadyCreated, + /// The validation record to be updated has not been created + RecordNotCreated, } #[pallet::event] @@ -105,6 +136,26 @@ pub mod pallet { ResolverUnregistered { id: Vec, }, + ValidationRecordCreated { + author: T::AccountId, + id: Vec, + }, + ValidationEntryAdded { + author: T::AccountId, + id: Vec, + checksum: H256, + }, + ValidationRecordUpdated { + author: T::AccountId, + id: Vec, + resolvers: Option>>, + data_type: Option>, + tags: Option>>, + }, + ValidationRecordDeleted { + author: T::AccountId, + id: Vec, + }, } #[pallet::call] @@ -183,6 +234,151 @@ pub mod pallet { Ok(()) } + + #[pallet::call_index(3)] + #[pallet::weight(1_000)] + pub fn create_validation_record( + origin: OriginFor, + data_id: BoundedVec, + resolvers: BoundedVec, T::MaxResolvers>, + data_type: BoundedVec, + tags: BoundedVec, T::MaxTags>, + checksum: H256, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + ensure!( + >::get(who.clone(), data_id.clone()).is_none(), + Error::::RecordAlreadyCreated + ); + + let reserved_method: BoundedVec = + BoundedVec::try_from(::ResolverMethod::get().to_vec()) + .expect("Failed to convert invalid resolver method config"); + + // Ensure any sylo data resolvers are already registered + for resolver in resolvers.clone() { + if resolver.method == reserved_method { + ensure!( + >::get(resolver.identifier).is_some(), + Error::::ResolverNotRegistered + ); + } + } + + let current_block = >::block_number(); + + let record = ValidationRecord { + author: who.clone(), + resolvers, + data_type, + tags, + entries: BoundedVec::truncate_from(vec![ValidationEntry { + checksum, + block: current_block, + }]), + }; + + >::insert(who.clone(), data_id.clone(), record); + + Self::deposit_event(Event::ValidationRecordCreated { + author: who, + id: data_id.to_vec(), + }); + + Ok(()) + } + + #[pallet::call_index(4)] + #[pallet::weight(1_000)] + pub fn add_validation_record_entry( + origin: OriginFor, + data_id: BoundedVec, + checksum: H256, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + let mut record = >::get(who.clone(), data_id.clone()) + .ok_or(Error::::RecordNotCreated)?; + + record.entries.force_push(ValidationEntry { + checksum: checksum.clone(), + block: >::block_number(), + }); + + >::insert(who.clone(), data_id.clone(), record); + + Self::deposit_event(Event::ValidationEntryAdded { + author: who, + id: data_id.to_vec(), + checksum, + }); + + Ok(()) + } + + #[pallet::call_index(5)] + #[pallet::weight(1_000)] + pub fn update_validation_record( + origin: OriginFor, + data_id: BoundedVec, + resolvers: Option, T::MaxResolvers>>, + data_type: Option>, + tags: Option, T::MaxTags>>, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + let mut record = >::get(who.clone(), data_id.clone()) + .ok_or(Error::::RecordNotCreated)?; + + if let Some(resolvers) = resolvers.clone() { + record.resolvers = resolvers; + } + + if let Some(data_type) = data_type.clone() { + record.data_type = data_type; + } + + if let Some(tags) = tags.clone() { + record.tags = tags; + } + + >::insert(who.clone(), data_id.clone(), record); + + Self::deposit_event(Event::ValidationRecordUpdated { + author: who, + id: data_id.to_vec(), + resolvers: resolvers + .map(|resolvers| resolvers.iter().map(|resolver| resolver.to_did()).collect()), + data_type: data_type.map(|data_type| data_type.to_vec()), + tags: tags.map(|tags| tags.iter().map(|tag| tag.to_vec()).collect()), + }); + + Ok(()) + } + + #[pallet::call_index(6)] + #[pallet::weight(1_000)] + pub fn delete_validation_record( + origin: OriginFor, + data_id: BoundedVec, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + ensure!( + >::get(who.clone(), data_id.clone()).is_some(), + Error::::RecordNotCreated + ); + + >::remove(who.clone(), data_id.clone()); + + Self::deposit_event(Event::ValidationRecordDeleted { + author: who, + id: data_id.to_vec(), + }); + + Ok(()) + } } impl Pallet { diff --git a/pallet/sylo/src/mock.rs b/pallet/sylo/src/mock.rs index 6ca50561f..6f8b9baca 100644 --- a/pallet/sylo/src/mock.rs +++ b/pallet/sylo/src/mock.rs @@ -33,12 +33,18 @@ impl_pallet_assets_config!(Test); impl_pallet_assets_ext_config!(Test); parameter_types! { + pub const MaxResolvers: u32 = 10; + pub const MaxTags: u32 = 10; + pub const MaxEntries: u32 = 100; pub const MaxServiceEndpoints: u32 = 10; pub const StringLimit: u32 = 100; pub const ResolverMethod: [u8; 9] = *b"sylo-data"; } impl Config for Test { type RuntimeEvent = RuntimeEvent; + type MaxResolvers = MaxResolvers; + type MaxTags = MaxTags; + type MaxEntries = MaxEntries; type MaxServiceEndpoints = MaxServiceEndpoints; type StringLimit = StringLimit; type ResolverMethod = ResolverMethod; diff --git a/pallet/sylo/src/types.rs b/pallet/sylo/src/types.rs index 421e4e631..95813bbb3 100644 --- a/pallet/sylo/src/types.rs +++ b/pallet/sylo/src/types.rs @@ -15,12 +15,12 @@ use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::{ - parameter_types, traits::Get, BoundedVec, CloneNoBound, PartialEqNoBound, RuntimeDebug, - RuntimeDebugNoBound, + parameter_types, traits::Get, BoundedVec, CloneNoBound, EqNoBound, PartialEqNoBound, + RuntimeDebug, RuntimeDebugNoBound, }; use scale_info::TypeInfo; use seed_primitives::{AssetId, Balance}; -use sp_core::{H160, H256}; +use sp_core::{hexdisplay::AsBytesRef, H160, H256}; use sp_std::{fmt::Debug, prelude::*}; parameter_types! { @@ -28,7 +28,16 @@ parameter_types! { pub const IdentifierLimit: u32 = 64; } -#[derive(CloneNoBound, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] +#[derive( + CloneNoBound, + RuntimeDebugNoBound, + Encode, + Decode, + PartialEqNoBound, + EqNoBound, + TypeInfo, + MaxEncodedLen, +)] #[scale_info(skip_type_params(StringLimit))] pub struct ResolverId where @@ -38,7 +47,19 @@ where pub identifier: BoundedVec, } -pub type ServiceEndpoint> = BoundedVec; +impl> ResolverId { + pub fn to_did(&self) -> Vec { + let method = self.method.to_vec(); + let method = String::from_utf8_lossy(method.as_bytes_ref()); + + let identifier = self.identifier.to_vec(); + let identifier = String::from_utf8_lossy(identifier.as_bytes_ref()); + + format!("did:{method}:{identifier}").as_bytes().to_vec() + } +} + +pub type ServiceEndpoint = BoundedVec; #[derive( Clone, Encode, Decode, RuntimeDebugNoBound, PartialEqNoBound, Eq, TypeInfo, MaxEncodedLen, @@ -55,15 +76,23 @@ where } #[derive(Clone, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] -pub struct ValidationRecord +pub struct ValidationEntry { + pub checksum: H256, + pub block: BlockNumber, +} + +#[derive(Clone, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] +#[scale_info(skip_type_params(MaxResolvers, MaxTags, MaxEntries, StringLimit))] +pub struct ValidationRecord where MaxResolvers: Get, MaxTags: Get, + MaxEntries: Get, StringLimit: Get, { pub author: AccountId, pub resolvers: BoundedVec, MaxResolvers>, pub data_type: BoundedVec, - pub algorithm: BoundedVec, pub tags: BoundedVec, MaxTags>, + pub entries: BoundedVec, MaxEntries>, } From 9e781efa04404e2b8a1d480419ec7ce34c37451b Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Wed, 18 Dec 2024 15:27:52 +1300 Subject: [PATCH 05/20] implement tests for creating validation records --- pallet/sylo/src/lib.rs | 39 ++- pallet/sylo/src/mock.rs | 4 +- pallet/sylo/src/tests.rs | 676 ++++++++++++++++++++++++++++++++++++++- pallet/sylo/src/types.rs | 22 +- 4 files changed, 707 insertions(+), 34 deletions(-) diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 43f62388e..1c4f5cc61 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -78,7 +78,7 @@ pub mod pallet { type StringLimit: Get; #[pallet::constant] - type ResolverMethod: Get<[u8; 9]>; + type SyloResolverMethod: Get<[u8; 9]>; } #[pallet::storage] @@ -252,19 +252,7 @@ pub mod pallet { Error::::RecordAlreadyCreated ); - let reserved_method: BoundedVec = - BoundedVec::try_from(::ResolverMethod::get().to_vec()) - .expect("Failed to convert invalid resolver method config"); - - // Ensure any sylo data resolvers are already registered - for resolver in resolvers.clone() { - if resolver.method == reserved_method { - ensure!( - >::get(resolver.identifier).is_some(), - Error::::ResolverNotRegistered - ); - } - } + Self::validate_sylo_resolvers(resolvers.clone())?; let current_block = >::block_number(); @@ -332,6 +320,7 @@ pub mod pallet { .ok_or(Error::::RecordNotCreated)?; if let Some(resolvers) = resolvers.clone() { + Self::validate_sylo_resolvers(resolvers.clone())?; record.resolvers = resolvers; } @@ -382,14 +371,24 @@ pub mod pallet { } impl Pallet { - pub fn get_reserved_resolver_id( - identifier: BoundedVec, - ) -> ResolverId { - let method: BoundedVec = - BoundedVec::try_from(::ResolverMethod::get().to_vec()) + pub fn validate_sylo_resolvers( + resolvers: BoundedVec, T::MaxResolvers>, + ) -> DispatchResult { + let reserved_method: BoundedVec = + BoundedVec::try_from(::SyloResolverMethod::get().to_vec()) .expect("Failed to convert invalid resolver method config"); - ResolverId { method, identifier } + // Ensure any sylo data resolvers are already registered + for resolver in resolvers { + if resolver.method == reserved_method { + ensure!( + >::get(resolver.identifier).is_some(), + Error::::ResolverNotRegistered + ); + } + } + + Ok(()) } } } diff --git a/pallet/sylo/src/mock.rs b/pallet/sylo/src/mock.rs index 6f8b9baca..d514650da 100644 --- a/pallet/sylo/src/mock.rs +++ b/pallet/sylo/src/mock.rs @@ -38,7 +38,7 @@ parameter_types! { pub const MaxEntries: u32 = 100; pub const MaxServiceEndpoints: u32 = 10; pub const StringLimit: u32 = 100; - pub const ResolverMethod: [u8; 9] = *b"sylo-data"; + pub const SyloResolverMethod: [u8; 9] = *b"sylo-data"; } impl Config for Test { type RuntimeEvent = RuntimeEvent; @@ -47,7 +47,7 @@ impl Config for Test { type MaxEntries = MaxEntries; type MaxServiceEndpoints = MaxServiceEndpoints; type StringLimit = StringLimit; - type ResolverMethod = ResolverMethod; + type SyloResolverMethod = SyloResolverMethod; } #[derive(Default)] diff --git a/pallet/sylo/src/tests.rs b/pallet/sylo/src/tests.rs index 0d1ea8b58..0e35c0bfa 100644 --- a/pallet/sylo/src/tests.rs +++ b/pallet/sylo/src/tests.rs @@ -17,7 +17,6 @@ use super::*; use hex::encode; use mock::{RuntimeEvent as MockEvent, RuntimeOrigin, Sylo, System, Test, TestExt}; use seed_pallet_common::test_prelude::*; -use sp_arithmetic::helpers_128bit::sqrt; mod resolvers { use super::*; @@ -204,6 +203,624 @@ mod resolvers { } } +mod validation_records { + use core::str; + + use mock::SyloResolverMethod; + use sp_core::hexdisplay::AsBytesRef; + + use super::*; + + #[test] + fn create_validation_records_works() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + System::assert_last_event(MockEvent::Sylo(Event::::ValidationRecordCreated { + author: alice.clone(), + id: data_id.clone().to_vec(), + })); + + assert_eq!( + ValidationRecords::::get(alice.clone(), data_id.clone()).unwrap(), + record + ); + }); + } + + #[test] + fn create_existing_validation_record_fails() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + assert_noop!( + Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + ), + Error::::RecordAlreadyCreated + ); + }); + } + + #[test] + fn create_validation_records_with_sylo_resolvers_works() { + TestExt.build().execute_with(|| { + // Ensure sylo resolver is registered + let (_, identifier, _) = create_and_register_resolver( + bounded_string("test-resolver"), + vec![bounded_string("https://endpoint.one")], + ); + + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![( + str::from_utf8(SyloResolverMethod::get().as_bytes_ref()).unwrap(), + str::from_utf8(identifier.to_vec().as_bytes_ref()).unwrap(), + )], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + System::assert_last_event(MockEvent::Sylo(Event::::ValidationRecordCreated { + author: alice.clone(), + id: data_id.clone().to_vec(), + })); + + assert_eq!( + ValidationRecords::::get(alice.clone(), data_id.clone()).unwrap(), + record + ); + }); + } + + #[test] + fn create_validation_record_with_unregistered_sylo_resolver_fails() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![( + str::from_utf8(SyloResolverMethod::get().as_bytes_ref()).unwrap(), + // identifier references a non-existent resolver + "unregistered-resolver", + )], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_noop!( + Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + ), + Error::::ResolverNotRegistered + ); + }); + } + + #[test] + fn create_multiple_validation_records_with_same_author_works() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + for i in 1..5 { + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + format!("data_id_{i}").as_str(), + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + System::assert_last_event(MockEvent::Sylo( + Event::::ValidationRecordCreated { + author: alice.clone(), + id: data_id.clone().to_vec(), + }, + )); + + assert_eq!( + ValidationRecords::::get(alice.clone(), data_id.clone()).unwrap(), + record + ); + } + }); + + #[test] + fn create_validation_records_with_different_author_works() { + TestExt.build().execute_with(|| { + for i in 2..5 { + let author: AccountId = create_account(i); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + author, + // use the same data id for each author's validation record + format!("data_id").as_str(), + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(author.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + System::assert_last_event(MockEvent::Sylo( + Event::::ValidationRecordCreated { + author: author.clone(), + id: data_id.clone().to_vec(), + }, + )); + + assert_eq!( + ValidationRecords::::get(author.clone(), data_id.clone()).unwrap(), + record + ); + } + }); + } + } + + #[test] + fn add_validation_entry_works() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + for i in 2..5 { + let checksum = H256::from_low_u64_be(i); + + assert_ok!(Sylo::add_validation_record_entry( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + checksum.clone() + )); + + System::assert_last_event(MockEvent::Sylo(Event::::ValidationEntryAdded { + author: alice.clone(), + id: data_id.clone().to_vec(), + checksum, + })); + + let record = + ValidationRecords::::get(alice.clone(), data_id.clone()).unwrap(); + + assert!(record.entries.len() as u64 == i); + assert!(record.entries.last().unwrap().checksum == checksum); + } + }); + } + + #[test] + fn add_not_existing_validation_entry_fails() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, _, _, _, checksum, _) = + create_initial_validation_record(alice, "data_id", vec![], "data_type", vec![]); + + assert_noop!( + Sylo::add_validation_record_entry( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + checksum.clone() + ), + Error::::RecordNotCreated + ); + }); + } + + #[test] + fn only_author_can_add_validation_entry() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + let bob: AccountId = create_account(3); + + assert_noop!( + Sylo::add_validation_record_entry( + RawOrigin::Signed(bob.clone()).into(), + data_id, + checksum + ), + Error::::RecordNotCreated + ); + }); + } + + #[test] + fn update_validation_record_works() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + let (_, new_resolvers, new_data_type, new_tags, _, _) = + create_initial_validation_record( + alice, + "data_id", + // add anotehr resolver + vec![("method-1", "resolver-1"), ("method-2", "resolver-2")], + // modify data type + "data_type_2", + // add more tags + vec!["tag-1", "tag-2", "tag-3"], + ); + + // Update the list of resolvers + assert_ok!(Sylo::update_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + Some(new_resolvers.clone()), + None, + None + )); + + assert_eq!( + ValidationRecords::::get(alice.clone(), data_id.clone()).unwrap(), + ValidationRecord { + author: alice.clone(), + resolvers: new_resolvers.clone(), + data_type: data_type.clone(), + tags: tags.clone(), + entries: record.entries.clone(), + } + ); + + System::assert_last_event(MockEvent::Sylo(Event::::ValidationRecordUpdated { + author: alice.clone(), + id: data_id.clone().to_vec(), + resolvers: Some( + new_resolvers.clone().iter().map(|resolver| resolver.to_did()).collect(), + ), + data_type: None, + tags: None, + })); + + // Update the data type + assert_ok!(Sylo::update_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + None, + Some(new_data_type.clone()), + None + )); + + assert_eq!( + ValidationRecords::::get(alice.clone(), data_id.clone()).unwrap(), + ValidationRecord { + author: alice.clone(), + resolvers: new_resolvers.clone(), + data_type: new_data_type.clone(), + tags: tags.clone(), + entries: record.entries.clone(), + } + ); + + System::assert_last_event(MockEvent::Sylo(Event::::ValidationRecordUpdated { + author: alice.clone(), + id: data_id.clone().to_vec(), + resolvers: None, + data_type: Some(new_data_type.clone().to_vec()), + tags: None, + })); + + // Update the list of tags + assert_ok!(Sylo::update_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + None, + None, + Some(new_tags.clone()), + )); + + assert_eq!( + ValidationRecords::::get(alice.clone(), data_id.clone()).unwrap(), + ValidationRecord { + author: alice.clone(), + resolvers: new_resolvers.clone(), + data_type: new_data_type.clone(), + tags: new_tags.clone(), + entries: record.entries.clone(), + } + ); + + System::assert_last_event(MockEvent::Sylo(Event::::ValidationRecordUpdated { + author: alice.clone(), + id: data_id.clone().to_vec(), + resolvers: None, + data_type: None, + tags: Some(new_tags.iter().map(|tag| tag.to_vec()).collect()), + })); + }); + } + + #[test] + fn update_not_existing_validation_record_fails() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_noop!( + Sylo::update_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + Some(resolvers.clone()), + Some(data_type.clone()), + Some(tags.clone()), + ), + Error::::RecordNotCreated + ); + }); + } + + #[test] + fn only_author_can_update_validation_record() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + let bob: AccountId = create_account(3); + + assert_noop!( + Sylo::update_validation_record( + RawOrigin::Signed(bob.clone()).into(), + data_id.clone(), + Some(resolvers.clone()), + Some(data_type.clone()), + Some(tags.clone()), + ), + Error::::RecordNotCreated + ); + }); + } + + #[test] + fn delete_validation_record_works() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + assert_ok!(Sylo::delete_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + )); + }); + } + + #[test] + fn delete_not_existing_validation_record_fails() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_noop!( + Sylo::update_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + Some(resolvers.clone()), + Some(data_type.clone()), + Some(tags.clone()), + ), + Error::::RecordNotCreated + ); + }); + } + + #[test] + fn only_author_can_delete_validation_record() { + TestExt.build().execute_with(|| { + let alice: AccountId = create_account(2); + + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(alice.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + let bob: AccountId = create_account(3); + + assert_noop!( + Sylo::delete_validation_record( + RawOrigin::Signed(bob.clone()).into(), + data_id.clone(), + ), + Error::::RecordNotCreated + ); + }); + } +} + fn create_and_register_resolver( identifier: BoundedVec::StringLimit>, service_endpoints: Vec::StringLimit>>, @@ -230,6 +847,59 @@ fn create_and_register_resolver( (controller, identifier, service_endpoints) } -fn bounded_string(name: &str) -> BoundedVec::StringLimit> { - BoundedVec::truncate_from(name.as_bytes().to_vec()) +fn create_initial_validation_record( + author: ::AccountId, + data_id: &str, + resolvers: Vec<(&str, &str)>, + data_type: &str, + tags: Vec<&str>, +) -> ( + BoundedVec, + BoundedVec, mock::MaxResolvers>, + BoundedVec, + BoundedVec, mock::MaxTags>, + H256, + ValidationRecord< + ::AccountId, + BlockNumberFor, + mock::MaxResolvers, + mock::MaxTags, + mock::MaxEntries, + mock::StringLimit, + >, +) { + let data_id = bounded_string(data_id); + let resolvers = BoundedVec::truncate_from( + resolvers + .iter() + .map(|(method, identifier)| create_resolver_id(method, identifier)) + .collect(), + ); + let data_type = bounded_string(data_type); + let tags = BoundedVec::truncate_from(tags.iter().map(|tag| bounded_string(tag)).collect()); + let checksum = H256::from_low_u64_be(123); + + let record = ValidationRecord { + author, + resolvers: resolvers.clone(), + data_type: data_type.clone(), + tags: tags.clone(), + entries: BoundedVec::truncate_from(vec![ValidationEntry { + checksum, + block: System::block_number(), + }]), + }; + + return (data_id, resolvers, data_type, tags, checksum, record); +} + +fn create_resolver_id(method: &str, identifier: &str) -> ResolverId<::StringLimit> { + ResolverId { + method: BoundedVec::truncate_from(method.as_bytes().to_vec()), + identifier: BoundedVec::truncate_from(identifier.as_bytes().to_vec()), + } +} + +fn bounded_string(str: &str) -> BoundedVec::StringLimit> { + BoundedVec::truncate_from(str.as_bytes().to_vec()) } diff --git a/pallet/sylo/src/types.rs b/pallet/sylo/src/types.rs index 95813bbb3..bc70e1166 100644 --- a/pallet/sylo/src/types.rs +++ b/pallet/sylo/src/types.rs @@ -19,15 +19,10 @@ use frame_support::{ RuntimeDebug, RuntimeDebugNoBound, }; use scale_info::TypeInfo; -use seed_primitives::{AssetId, Balance}; +use seed_primitives::{AssetId, Balance, Block}; use sp_core::{hexdisplay::AsBytesRef, H160, H256}; use sp_std::{fmt::Debug, prelude::*}; -parameter_types! { - pub const MethodLimit: u32 = 32; - pub const IdentifierLimit: u32 = 64; -} - #[derive( CloneNoBound, RuntimeDebugNoBound, @@ -75,16 +70,25 @@ where pub service_endpoints: BoundedVec, MaxServiceEndpoints>, } -#[derive(Clone, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] -pub struct ValidationEntry { +#[derive( + Clone, Encode, Decode, RuntimeDebugNoBound, PartialEqNoBound, Eq, TypeInfo, MaxEncodedLen, +)] +pub struct ValidationEntry +where + BlockNumber: Debug + PartialEq + Clone, +{ pub checksum: H256, pub block: BlockNumber, } -#[derive(Clone, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] +#[derive( + Clone, Encode, Decode, RuntimeDebugNoBound, PartialEqNoBound, Eq, TypeInfo, MaxEncodedLen, +)] #[scale_info(skip_type_params(MaxResolvers, MaxTags, MaxEntries, StringLimit))] pub struct ValidationRecord where + AccountId: Debug + PartialEq + Clone, + BlockNumber: Debug + PartialEq + Clone, MaxResolvers: Get, MaxTags: Get, MaxEntries: Get, From 2b5855546633eb89d8a65f61e065b57456b5713c Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Wed, 18 Dec 2024 18:25:17 +1300 Subject: [PATCH 06/20] setup benchmarking for sylo pallet --- Cargo.lock | 1 + pallet/sylo/src/benchmarking.rs | 68 +++++++++++++++++++++++ pallet/sylo/src/lib.rs | 17 ++++-- pallet/sylo/src/mock.rs | 1 + pallet/sylo/src/tests.rs | 78 +++++++++++++-------------- pallet/sylo/src/types.rs | 11 ++-- pallet/sylo/src/weights.rs | 87 ++++++++++++++++++++++++++++++ runtime/Cargo.toml | 4 ++ runtime/src/lib.rs | 22 ++++++++ runtime/src/weights/pallet_sylo.rs | 53 ++++++++++++++++++ 10 files changed, 293 insertions(+), 49 deletions(-) create mode 100644 pallet/sylo/src/benchmarking.rs create mode 100644 pallet/sylo/src/weights.rs create mode 100644 runtime/src/weights/pallet_sylo.rs diff --git a/Cargo.lock b/Cargo.lock index a94ff6878..095062b56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9145,6 +9145,7 @@ dependencies = [ "pallet-sft-rpc-runtime-api", "pallet-staking", "pallet-sudo", + "pallet-sylo", "pallet-timestamp", "pallet-token-approvals", "pallet-transaction-payment", diff --git a/pallet/sylo/src/benchmarking.rs b/pallet/sylo/src/benchmarking.rs new file mode 100644 index 000000000..ab8c57f66 --- /dev/null +++ b/pallet/sylo/src/benchmarking.rs @@ -0,0 +1,68 @@ +// Copyright 2022-2023 Futureverse Corporation Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// You may obtain a copy of the License at the root of this project source code + +#![cfg(feature = "runtime-benchmarks")] + +use super::*; + +use crate::Pallet as Sylo; + +use frame_benchmarking::{account as bench_account, benchmarks, impl_benchmark_test_suite}; +use frame_support::{assert_ok, BoundedVec}; +use frame_system::RawOrigin; + +const MAX_SERVICE_ENDPOINTS: u32 = 20; +const STRING_LIMIT: u32 = 1000; + +/// This is a helper function to get an account. +pub fn account(name: &'static str) -> T::AccountId +where + T::AccountId: From, +{ + bench_account(name, 0, 0) +} + +pub fn origin(acc: &T::AccountId) -> RawOrigin { + RawOrigin::Signed(acc.clone()) +} + +benchmarks! { + where_clause { where ::AccountId: From + Into } + + register_resolver { + let p in 1 .. STRING_LIMIT; + let q in 1 .. MAX_SERVICE_ENDPOINTS; + + let alice = account::("Alice"); + + let mut identifier = BoundedVec::new(); + for _ in 1..p { + identifier.force_push(b'a'); + } + + let mut service_endpoints = BoundedVec::new(); + for _ in 1..q { + let endpoint = BoundedVec::truncate_from("https://service-endpoint.one.two.three".encode()); + service_endpoints.force_push(endpoint); + } + + }: _(origin::(&account::("Alice")), identifier, service_endpoints) +} + +impl_benchmark_test_suite!( + Sylo, + seed_primitives::test_utils::TestExt::::default().build(), + crate::mock::Test +); diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 1c4f5cc61..ca6e01c18 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -29,13 +29,15 @@ use scale_info::TypeInfo; use seed_pallet_common::CreateExt; use seed_primitives::{AssetId, Balance}; use serde::{Deserialize, Serialize}; -use sp_core::{H160, U256}; +use sp_core::{H160, H256, U256}; use sp_runtime::{ traits::{AccountIdConversion, Zero}, ArithmeticError, DispatchError, FixedU128, RuntimeDebug, SaturatedConversion, }; use sp_std::{cmp::min, convert::TryInto, prelude::*, vec}; +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; #[cfg(test)] mod mock; #[cfg(test)] @@ -44,11 +46,11 @@ pub mod types; pub use types::*; +pub mod weights; +pub use weights::WeightInfo; + #[frame_support::pallet] pub mod pallet { - use serde::ser; - use sp_core::H256; - use super::*; /// The current storage version. @@ -62,6 +64,9 @@ pub mod pallet { pub trait Config: frame_system::Config { type RuntimeEvent: From> + IsType<::RuntimeEvent>; + /// Interface to access weight values + type WeightInfo: WeightInfo; + #[pallet::constant] type MaxResolvers: Get; @@ -161,7 +166,9 @@ pub mod pallet { #[pallet::call] impl Pallet { #[pallet::call_index(0)] - #[pallet::weight(1_000)] + #[pallet::weight({ + T::WeightInfo::register_resolver(::get(), ::get()) + })] pub fn register_resolver( origin: OriginFor, identifier: BoundedVec, diff --git a/pallet/sylo/src/mock.rs b/pallet/sylo/src/mock.rs index d514650da..665aa46af 100644 --- a/pallet/sylo/src/mock.rs +++ b/pallet/sylo/src/mock.rs @@ -48,6 +48,7 @@ impl Config for Test { type MaxServiceEndpoints = MaxServiceEndpoints; type StringLimit = StringLimit; type SyloResolverMethod = SyloResolverMethod; + type WeightInfo = (); } #[derive(Default)] diff --git a/pallet/sylo/src/tests.rs b/pallet/sylo/src/tests.rs index 0e35c0bfa..a4323d45a 100644 --- a/pallet/sylo/src/tests.rs +++ b/pallet/sylo/src/tests.rs @@ -396,46 +396,46 @@ mod validation_records { ); } }); + } + + #[test] + fn create_validation_records_with_different_author_works() { + TestExt.build().execute_with(|| { + for i in 2..5 { + let author: AccountId = create_account(i); - #[test] - fn create_validation_records_with_different_author_works() { - TestExt.build().execute_with(|| { - for i in 2..5 { - let author: AccountId = create_account(i); - - let (data_id, resolvers, data_type, tags, checksum, record) = - create_initial_validation_record( - author, - // use the same data id for each author's validation record - format!("data_id").as_str(), - vec![("method-1", "resolver-1")], - "data_type", - vec!["tag-1", "tag-2"], - ); - - assert_ok!(Sylo::create_validation_record( - RawOrigin::Signed(author.clone()).into(), - data_id.clone(), - resolvers.clone(), - data_type.clone(), - tags.clone(), - checksum.clone() - )); - - System::assert_last_event(MockEvent::Sylo( - Event::::ValidationRecordCreated { - author: author.clone(), - id: data_id.clone().to_vec(), - }, - )); - - assert_eq!( - ValidationRecords::::get(author.clone(), data_id.clone()).unwrap(), - record + let (data_id, resolvers, data_type, tags, checksum, record) = + create_initial_validation_record( + author, + // use the same data id for each author's validation record + format!("data_id").as_str(), + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], ); - } - }); - } + + assert_ok!(Sylo::create_validation_record( + RawOrigin::Signed(author.clone()).into(), + data_id.clone(), + resolvers.clone(), + data_type.clone(), + tags.clone(), + checksum.clone() + )); + + System::assert_last_event(MockEvent::Sylo( + Event::::ValidationRecordCreated { + author: author.clone(), + id: data_id.clone().to_vec(), + }, + )); + + assert_eq!( + ValidationRecords::::get(author.clone(), data_id.clone()).unwrap(), + record + ); + } + }); } #[test] @@ -763,7 +763,7 @@ mod validation_records { TestExt.build().execute_with(|| { let alice: AccountId = create_account(2); - let (data_id, resolvers, data_type, tags, checksum, record) = + let (data_id, resolvers, data_type, tags, checksum, _) = create_initial_validation_record( alice, "data_id", diff --git a/pallet/sylo/src/types.rs b/pallet/sylo/src/types.rs index bc70e1166..3a460fb08 100644 --- a/pallet/sylo/src/types.rs +++ b/pallet/sylo/src/types.rs @@ -44,13 +44,14 @@ where impl> ResolverId { pub fn to_did(&self) -> Vec { - let method = self.method.to_vec(); - let method = String::from_utf8_lossy(method.as_bytes_ref()); + return Vec::new(); + // let method = self.method.to_vec(); + // let method = String::from_utf8_lossy(method.as_bytes_ref()); - let identifier = self.identifier.to_vec(); - let identifier = String::from_utf8_lossy(identifier.as_bytes_ref()); + // let identifier = self.identifier.to_vec(); + // let identifier = String::from_utf8_lossy(identifier.as_bytes_ref()); - format!("did:{method}:{identifier}").as_bytes().to_vec() + // format!("did:{method}:{identifier}").as_bytes().to_vec() } } diff --git a/pallet/sylo/src/weights.rs b/pallet/sylo/src/weights.rs new file mode 100644 index 000000000..d38253a1f --- /dev/null +++ b/pallet/sylo/src/weights.rs @@ -0,0 +1,87 @@ +// This file is part of Substrate. + +// Copyright (C) 2022 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Autogenerated weights for pallet_sylo +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2024-12-18, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` +//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// ./target/release/seed +// benchmark +// pallet +// --chain=dev +// --steps=50 +// --repeat=20 +// --pallet=pallet-sylo +// --extrinsic=* +// --wasm-execution=compiled +// --heap-pages=4096 +// --output +// ./pallet/sylo/src/weights.rs +// --template +// ./scripts/pallet_template.hbs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use sp_std::marker::PhantomData; + +/// Weight functions needed for pallet_sylo. +pub trait WeightInfo { + fn register_resolver(p: u32, q: u32, ) -> Weight; +} + +/// Weights for pallet_sylo using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + // Storage: `Sylo::Resolvers` (r:1 w:1) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(1151), added: 3626, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 100000]`. + /// The range of component `q` is `[1, 20]`. + fn register_resolver(p: u32, q: u32, ) -> Weight { + Weight::from_all(14_210_965) + // Standard Error: 0 + .saturating_add(Weight::from_all(4_u64).saturating_mul(p as u64)) + // Standard Error: 4_071 + .saturating_add(Weight::from_all(163_407_u64).saturating_mul(q as u64)) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } +} + +// For backwards compatibility and tests +impl WeightInfo for () { + // Storage: `Sylo::Resolvers` (r:1 w:1) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(1151), added: 3626, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 100000]`. + /// The range of component `q` is `[1, 20]`. + fn register_resolver(p: u32, q: u32, ) -> Weight { + Weight::from_all(14_210_965) + // Standard Error: 0 + .saturating_add(Weight::from_all(4_u64).saturating_mul(p as u64)) + // Standard Error: 4_071 + .saturating_add(Weight::from_all(163_407_u64).saturating_mul(q as u64)) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) + } +} + diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index d33f6b2bb..1e8da5dc9 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -89,6 +89,7 @@ pallet-nfi = { workspace = true } pallet-nft = { workspace = true } pallet-nft-rpc-runtime-api = { workspace = true } pallet-sft = { workspace = true } +pallet-sylo = { workspace = true } pallet-sft-rpc-runtime-api = { workspace = true } pallet-token-approvals = { workspace = true } pallet-tx-fee-pot = { workspace = true } @@ -198,6 +199,7 @@ std = [ "pallet-nft-rpc-runtime-api/std", "pallet-sft/std", "pallet-sft-rpc-runtime-api/std", + "pallet-sylo/std", "pallet-xrpl-bridge/std", "pallet-xrpl/std", "pallet-tx-fee-pot/std", @@ -282,6 +284,7 @@ try-runtime = [ "pallet-nfi/try-runtime", "pallet-nft/try-runtime", "pallet-sft/try-runtime", + "pallet-sylo/try-runtime", "pallet-xrpl-bridge/try-runtime", "pallet-xrpl/try-runtime", "pallet-token-approvals/try-runtime", @@ -335,6 +338,7 @@ runtime-benchmarks = [ "pallet-nfi/runtime-benchmarks", "pallet-nft/runtime-benchmarks", "pallet-sft/runtime-benchmarks", + "pallet-sylo/runtime-benchmarks", "pallet-xrpl-bridge/runtime-benchmarks", "pallet-xrpl/runtime-benchmarks", "pallet-dex/runtime-benchmarks", diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index e7631babb..da776bd0c 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -596,6 +596,26 @@ impl pallet_preimage::Config for Runtime { type ByteDeposit = PreimageByteDeposit; } +parameter_types! { + pub const MaxResolvers: u32 = 10; + pub const MaxTags: u32 = 10; + pub const MaxEntries: u32 = 100; + pub const MaxServiceEndpoints: u32 = 10; + pub const SyloStringLimit: u32 = 100; + pub const SyloResolverMethod: [u8; 9] = *b"sylo-data"; +} + +impl pallet_sylo::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type MaxResolvers = MaxResolvers; + type MaxTags = MaxTags; + type MaxEntries = MaxEntries; + type MaxServiceEndpoints = MaxServiceEndpoints; + type StringLimit = SyloStringLimit; + type SyloResolverMethod = SyloResolverMethod; + type WeightInfo = weights::pallet_sylo::WeightInfo; +} + impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; @@ -1434,6 +1454,7 @@ construct_runtime!( Crowdsale: pallet_crowdsale = 49, Nfi: pallet_nfi = 50, Migration: pallet_migration = 51, + Sylo: pallet_sylo = 52, // Election pallet. Only works with staking ElectionProviderMultiPhase: pallet_election_provider_multi_phase = 22, @@ -2357,5 +2378,6 @@ mod benches { [pallet_crowdsale, Crowdsale] [pallet_evm, EVM] [pallet_migration, Migration] + [pallet_sylo, Sylo] ); } diff --git a/runtime/src/weights/pallet_sylo.rs b/runtime/src/weights/pallet_sylo.rs new file mode 100644 index 000000000..25e0e3261 --- /dev/null +++ b/runtime/src/weights/pallet_sylo.rs @@ -0,0 +1,53 @@ + +//! Autogenerated weights for `pallet_sylo` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2024-12-18, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` +//! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024 + +// Executed Command: +// ./target/release/seed +// benchmark +// pallet +// --chain=dev +// --steps=50 +// --repeat=20 +// --pallet=pallet-sylo +// --extrinsic=* +// --wasm-execution=compiled +// --heap-pages=4096 +// --output +// ./runtime/src/weights/pallet_sylo.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::Weight}; +use core::marker::PhantomData; + +/// Weight functions for `pallet_sylo`. +pub struct WeightInfo(PhantomData); +impl pallet_sylo::WeightInfo for WeightInfo { + /// Storage: `Sylo::Resolvers` (r:1 w:1) + /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(1151), added: 3626, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 100000]`. + /// The range of component `q` is `[1, 20]`. + fn register_resolver(p: u32, q: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `175` + // Estimated: `4616` + // Minimum execution time: 13_000_000 picoseconds. + Weight::from_parts(14_224_220, 0) + .saturating_add(Weight::from_parts(0, 4616)) + // Standard Error: 0 + .saturating_add(Weight::from_parts(3, 0).saturating_mul(p.into())) + // Standard Error: 4_067 + .saturating_add(Weight::from_parts(147_703, 0).saturating_mul(q.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } +} From c65bf9d6f2bc36fb590e00932dd2c56f2fd2ffe3 Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Mon, 6 Jan 2025 14:13:58 +1300 Subject: [PATCH 07/20] implement remaning sylo extrinsic benchmarks --- pallet/sylo/src/benchmarking.rs | 144 ++++++++++++++++++++++++++- pallet/sylo/src/lib.rs | 27 +++-- pallet/sylo/src/weights.rs | 154 +++++++++++++++++++++++++---- runtime/src/lib.rs | 2 +- runtime/src/weights/mod.rs | 1 + runtime/src/weights/pallet_sylo.rs | 109 +++++++++++++++++--- 6 files changed, 396 insertions(+), 41 deletions(-) diff --git a/pallet/sylo/src/benchmarking.rs b/pallet/sylo/src/benchmarking.rs index ab8c57f66..b68088845 100644 --- a/pallet/sylo/src/benchmarking.rs +++ b/pallet/sylo/src/benchmarking.rs @@ -19,12 +19,15 @@ use super::*; use crate::Pallet as Sylo; +use alloc::string::{String, ToString}; use frame_benchmarking::{account as bench_account, benchmarks, impl_benchmark_test_suite}; use frame_support::{assert_ok, BoundedVec}; use frame_system::RawOrigin; -const MAX_SERVICE_ENDPOINTS: u32 = 20; -const STRING_LIMIT: u32 = 1000; +const MAX_SERVICE_ENDPOINTS: u32 = 10; +const STRING_LIMIT: u32 = 250; +const MAX_RESOLVERS: u32 = 10; +const MAX_TAGS: u32 = 10; /// This is a helper function to get an account. pub fn account(name: &'static str) -> T::AccountId @@ -38,6 +41,48 @@ pub fn origin(acc: &T::AccountId) -> RawOrigin { RawOrigin::Signed(acc.clone()) } +pub fn bounded_string(name: &str) -> BoundedVec::StringLimit> { + BoundedVec::truncate_from(name.as_bytes().to_vec()) +} + +pub fn setup_resolver( + caller: T::AccountId, + identifier: BoundedVec::StringLimit>, +) -> BoundedVec::StringLimit> { + let service_endpoints = BoundedVec::truncate_from(vec![bounded_string::( + "https://service-endpoint.one.two.three", + )]); + + assert_ok!(Sylo::::register_resolver( + RawOrigin::Signed(caller).into(), + identifier.clone(), + service_endpoints, + )); + + return identifier; +} + +pub fn setup_validation_record( + caller: T::AccountId, +) -> BoundedVec::StringLimit> { + let data_id = bounded_string::("data-id"); + let resolvers = BoundedVec::new(); + let data_type = bounded_string::("data-type"); + let tags = BoundedVec::new(); + let checksum = H256::from_low_u64_be(123); + + assert_ok!(Sylo::::create_validation_record( + RawOrigin::Signed(caller).into(), + data_id.clone(), + resolvers, + data_type, + tags, + checksum, + )); + + return data_id; +} + benchmarks! { where_clause { where ::AccountId: From + Into } @@ -54,11 +99,100 @@ benchmarks! { let mut service_endpoints = BoundedVec::new(); for _ in 1..q { - let endpoint = BoundedVec::truncate_from("https://service-endpoint.one.two.three".encode()); - service_endpoints.force_push(endpoint); + service_endpoints.force_push(bounded_string::("https://service-endpoint.one.two.three")); } + }: _(origin::(&alice), identifier, service_endpoints) + + update_resolver { + let q in 1 .. MAX_SERVICE_ENDPOINTS; + + let alice = account::("Alice"); + + let identifier = setup_resolver::(alice.clone(), bounded_string::("sylo-data-resolver")); + + let mut service_endpoints = BoundedVec::new(); + for _ in 1..q { + service_endpoints.force_push(bounded_string::("https://service-endpoint.one.two.three")); + } + }: _(origin::(&alice), identifier, service_endpoints) + + unregister_resolver { + let alice = account::("Alice"); + + let identifier = setup_resolver::(alice.clone(), bounded_string::("sylo-data-resolver")); + }: _(origin::(&alice), identifier) + + create_validation_record { + let p in 1 .. MAX_RESOLVERS; + let q in 1 .. MAX_TAGS; + + let alice = account::("Alice"); + + let data_id = bounded_string::("data-id"); + + let mut resolvers = BoundedVec::new(); + for i in 1 .. p { + let mut resolver_id = String::from("sylo-resolver"); + resolver_id.push_str(i.to_string().as_str()); + + let resolver_id = setup_resolver::(alice.clone(), bounded_string::(resolver_id.as_str())); + resolvers.force_push(ResolverId { + method: bounded_string::("sylo_resolver"), + identifier: resolver_id, + }); + } + + let data_type = bounded_string::("data-type"); + + let mut tags = BoundedVec::new(); + for _ in 1 .. q { + tags.force_push(bounded_string::("tag")); + } + + let checksum = H256::from_low_u64_be(123); + }: _(origin::(&alice), data_id, resolvers, data_type, tags, checksum) + + add_validation_record_entry { + let alice = account::("Alice"); + + let data_id = setup_validation_record::(alice.clone()); + + let checksum = H256::from_low_u64_be(125); + }: _(origin::(&alice), data_id, checksum) + + update_validation_record { + let p in 1 .. MAX_RESOLVERS; + let q in 1 .. MAX_TAGS; + + let alice = account::("Alice"); + + let data_id = setup_validation_record::(alice.clone()); + + let mut resolvers = BoundedVec::new(); + for i in 1 .. p { + let mut resolver_id = String::from("sylo-resolver"); + resolver_id.push_str(i.to_string().as_str()); + + let resolver_id = setup_resolver::(alice.clone(), bounded_string::(resolver_id.as_str())); + resolvers.force_push(ResolverId { + method: bounded_string::("sylo_resolver"), + identifier: resolver_id, + }); + } + + let data_type = bounded_string::("data-type"); + + let mut tags = BoundedVec::new(); + for _ in 1 .. q { + tags.force_push(bounded_string::("tag")); + } + }: _(origin::(&alice), data_id, Some(resolvers), Some(data_type), Some(tags)) + + delete_validation_record { + let alice = account::("Alice"); - }: _(origin::(&account::("Alice")), identifier, service_endpoints) + let data_id = setup_validation_record::(alice.clone()); + }: _(origin::(&alice), data_id) } impl_benchmark_test_suite!( diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index ca6e01c18..9e8838bfd 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -13,6 +13,7 @@ // limitations under the License. // You may obtain a copy of the License at the root of this project source code #![cfg_attr(not(feature = "std"), no_std)] +extern crate alloc; pub use pallet::*; @@ -167,7 +168,7 @@ pub mod pallet { impl Pallet { #[pallet::call_index(0)] #[pallet::weight({ - T::WeightInfo::register_resolver(::get(), ::get()) + T::WeightInfo::register_resolver(::get(), ::get()) })] pub fn register_resolver( origin: OriginFor, @@ -196,7 +197,9 @@ pub mod pallet { } #[pallet::call_index(1)] - #[pallet::weight(1_000)] + #[pallet::weight({ + T::WeightInfo::update_resolver(::get()) + })] pub fn update_resolver( origin: OriginFor, identifier: BoundedVec, @@ -223,7 +226,9 @@ pub mod pallet { } #[pallet::call_index(2)] - #[pallet::weight(1_000)] + #[pallet::weight({ + T::WeightInfo::unregister_resolver() + })] pub fn unregister_resolver( origin: OriginFor, identifier: BoundedVec, @@ -243,7 +248,9 @@ pub mod pallet { } #[pallet::call_index(3)] - #[pallet::weight(1_000)] + #[pallet::weight({ + T::WeightInfo::create_validation_record(::get(), ::get()) + })] pub fn create_validation_record( origin: OriginFor, data_id: BoundedVec, @@ -285,7 +292,9 @@ pub mod pallet { } #[pallet::call_index(4)] - #[pallet::weight(1_000)] + #[pallet::weight({ + T::WeightInfo::add_validation_record_entry() + })] pub fn add_validation_record_entry( origin: OriginFor, data_id: BoundedVec, @@ -313,7 +322,9 @@ pub mod pallet { } #[pallet::call_index(5)] - #[pallet::weight(1_000)] + #[pallet::weight({ + T::WeightInfo::update_validation_record(::get(), ::get()) + })] pub fn update_validation_record( origin: OriginFor, data_id: BoundedVec, @@ -354,7 +365,9 @@ pub mod pallet { } #[pallet::call_index(6)] - #[pallet::weight(1_000)] + #[pallet::weight({ + T::WeightInfo::delete_validation_record() + })] pub fn delete_validation_record( origin: OriginFor, data_id: BoundedVec, diff --git a/pallet/sylo/src/weights.rs b/pallet/sylo/src/weights.rs index d38253a1f..75e0e98da 100644 --- a/pallet/sylo/src/weights.rs +++ b/pallet/sylo/src/weights.rs @@ -18,7 +18,7 @@ //! Autogenerated weights for pallet_sylo //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2024-12-18, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2025-01-06, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` //! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 @@ -48,21 +48,84 @@ use sp_std::marker::PhantomData; /// Weight functions needed for pallet_sylo. pub trait WeightInfo { fn register_resolver(p: u32, q: u32, ) -> Weight; + fn update_resolver(q: u32, ) -> Weight; + fn unregister_resolver() -> Weight; + fn create_validation_record(p: u32, q: u32, ) -> Weight; + fn add_validation_record_entry() -> Weight; + fn update_validation_record(p: u32, q: u32, ) -> Weight; + fn delete_validation_record() -> Weight; } /// Weights for pallet_sylo using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { // Storage: `Sylo::Resolvers` (r:1 w:1) - // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(1151), added: 3626, mode: `MaxEncodedLen`) - /// The range of component `p` is `[1, 100000]`. - /// The range of component `q` is `[1, 20]`. + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 250]`. + /// The range of component `q` is `[1, 10]`. fn register_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(14_210_965) - // Standard Error: 0 - .saturating_add(Weight::from_all(4_u64).saturating_mul(p as u64)) - // Standard Error: 4_071 - .saturating_add(Weight::from_all(163_407_u64).saturating_mul(q as u64)) + Weight::from_all(13_647_734) + // Standard Error: 237 + .saturating_add(Weight::from_all(2_614_u64).saturating_mul(p as u64)) + // Standard Error: 6_184 + .saturating_add(Weight::from_all(250_475_u64).saturating_mul(q as u64)) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + // Storage: `Sylo::Resolvers` (r:1 w:1) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + /// The range of component `q` is `[1, 10]`. + fn update_resolver(q: u32, ) -> Weight { + Weight::from_all(15_835_394) + // Standard Error: 6_400 + .saturating_add(Weight::from_all(354_702_u64).saturating_mul(q as u64)) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + // Storage: `Sylo::Resolvers` (r:1 w:1) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + fn unregister_resolver() -> Weight { + Weight::from_all(15_000_000) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + // Storage: `Sylo::ValidationRecords` (r:1 w:1) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 10]`. + /// The range of component `q` is `[1, 10]`. + fn create_validation_record(p: u32, q: u32, ) -> Weight { + Weight::from_all(14_090_414) + // Standard Error: 5_662 + .saturating_add(Weight::from_all(672_999_u64).saturating_mul(p as u64)) + // Standard Error: 5_662 + .saturating_add(Weight::from_all(227_877_u64).saturating_mul(q as u64)) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + // Storage: `Sylo::ValidationRecords` (r:1 w:1) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + fn add_validation_record_entry() -> Weight { + Weight::from_all(18_000_000) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + // Storage: `Sylo::ValidationRecords` (r:1 w:1) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 10]`. + /// The range of component `q` is `[1, 10]`. + fn update_validation_record(p: u32, q: u32, ) -> Weight { + Weight::from_all(18_203_859) + // Standard Error: 7_399 + .saturating_add(Weight::from_all(754_695_u64).saturating_mul(p as u64)) + // Standard Error: 7_399 + .saturating_add(Weight::from_all(390_063_u64).saturating_mul(q as u64)) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + // Storage: `Sylo::ValidationRecords` (r:1 w:1) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + fn delete_validation_record() -> Weight { + Weight::from_all(16_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -71,15 +134,72 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { // Storage: `Sylo::Resolvers` (r:1 w:1) - // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(1151), added: 3626, mode: `MaxEncodedLen`) - /// The range of component `p` is `[1, 100000]`. - /// The range of component `q` is `[1, 20]`. + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 250]`. + /// The range of component `q` is `[1, 10]`. fn register_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(14_210_965) - // Standard Error: 0 - .saturating_add(Weight::from_all(4_u64).saturating_mul(p as u64)) - // Standard Error: 4_071 - .saturating_add(Weight::from_all(163_407_u64).saturating_mul(q as u64)) + Weight::from_all(13_647_734) + // Standard Error: 237 + .saturating_add(Weight::from_all(2_614_u64).saturating_mul(p as u64)) + // Standard Error: 6_184 + .saturating_add(Weight::from_all(250_475_u64).saturating_mul(q as u64)) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + // Storage: `Sylo::Resolvers` (r:1 w:1) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + /// The range of component `q` is `[1, 10]`. + fn update_resolver(q: u32, ) -> Weight { + Weight::from_all(15_835_394) + // Standard Error: 6_400 + .saturating_add(Weight::from_all(354_702_u64).saturating_mul(q as u64)) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + // Storage: `Sylo::Resolvers` (r:1 w:1) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + fn unregister_resolver() -> Weight { + Weight::from_all(15_000_000) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + // Storage: `Sylo::ValidationRecords` (r:1 w:1) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 10]`. + /// The range of component `q` is `[1, 10]`. + fn create_validation_record(p: u32, q: u32, ) -> Weight { + Weight::from_all(14_090_414) + // Standard Error: 5_662 + .saturating_add(Weight::from_all(672_999_u64).saturating_mul(p as u64)) + // Standard Error: 5_662 + .saturating_add(Weight::from_all(227_877_u64).saturating_mul(q as u64)) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + // Storage: `Sylo::ValidationRecords` (r:1 w:1) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + fn add_validation_record_entry() -> Weight { + Weight::from_all(18_000_000) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + // Storage: `Sylo::ValidationRecords` (r:1 w:1) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 10]`. + /// The range of component `q` is `[1, 10]`. + fn update_validation_record(p: u32, q: u32, ) -> Weight { + Weight::from_all(18_203_859) + // Standard Error: 7_399 + .saturating_add(Weight::from_all(754_695_u64).saturating_mul(p as u64)) + // Standard Error: 7_399 + .saturating_add(Weight::from_all(390_063_u64).saturating_mul(q as u64)) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + // Storage: `Sylo::ValidationRecords` (r:1 w:1) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + fn delete_validation_record() -> Weight { + Weight::from_all(16_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index da776bd0c..26d9b2aab 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -601,7 +601,7 @@ parameter_types! { pub const MaxTags: u32 = 10; pub const MaxEntries: u32 = 100; pub const MaxServiceEndpoints: u32 = 10; - pub const SyloStringLimit: u32 = 100; + pub const SyloStringLimit: u32 = 250; pub const SyloResolverMethod: [u8; 9] = *b"sylo-data"; } diff --git a/runtime/src/weights/mod.rs b/runtime/src/weights/mod.rs index ce39f6879..312330850 100644 --- a/runtime/src/weights/mod.rs +++ b/runtime/src/weights/mod.rs @@ -28,6 +28,7 @@ pub mod pallet_session; pub mod pallet_sft; pub mod pallet_staking; pub mod pallet_sudo; +pub mod pallet_sylo; pub mod pallet_timestamp; pub mod pallet_token_approvals; pub mod pallet_utility; diff --git a/runtime/src/weights/pallet_sylo.rs b/runtime/src/weights/pallet_sylo.rs index 25e0e3261..ebd3f5ba5 100644 --- a/runtime/src/weights/pallet_sylo.rs +++ b/runtime/src/weights/pallet_sylo.rs @@ -2,7 +2,7 @@ //! Autogenerated weights for `pallet_sylo` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2024-12-18, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2025-01-06, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` //! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024 @@ -33,20 +33,107 @@ use core::marker::PhantomData; pub struct WeightInfo(PhantomData); impl pallet_sylo::WeightInfo for WeightInfo { /// Storage: `Sylo::Resolvers` (r:1 w:1) - /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(1151), added: 3626, mode: `MaxEncodedLen`) - /// The range of component `p` is `[1, 100000]`. - /// The range of component `q` is `[1, 20]`. + /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 250]`. + /// The range of component `q` is `[1, 10]`. fn register_resolver(p: u32, q: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `175` - // Estimated: `4616` + // Estimated: `6266` // Minimum execution time: 13_000_000 picoseconds. - Weight::from_parts(14_224_220, 0) - .saturating_add(Weight::from_parts(0, 4616)) - // Standard Error: 0 - .saturating_add(Weight::from_parts(3, 0).saturating_mul(p.into())) - // Standard Error: 4_067 - .saturating_add(Weight::from_parts(147_703, 0).saturating_mul(q.into())) + Weight::from_parts(12_416_241, 0) + .saturating_add(Weight::from_parts(0, 6266)) + // Standard Error: 224 + .saturating_add(Weight::from_parts(3_202, 0).saturating_mul(p.into())) + // Standard Error: 5_847 + .saturating_add(Weight::from_parts(375_131, 0).saturating_mul(q.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + /// Storage: `Sylo::Resolvers` (r:1 w:1) + /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + /// The range of component `q` is `[1, 10]`. + fn update_resolver(q: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `289` + // Estimated: `6266` + // Minimum execution time: 15_000_000 picoseconds. + Weight::from_parts(16_038_523, 0) + .saturating_add(Weight::from_parts(0, 6266)) + // Standard Error: 7_792 + .saturating_add(Weight::from_parts(337_744, 0).saturating_mul(q.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + /// Storage: `Sylo::Resolvers` (r:1 w:1) + /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + fn unregister_resolver() -> Weight { + // Proof Size summary in bytes: + // Measured: `289` + // Estimated: `6266` + // Minimum execution time: 15_000_000 picoseconds. + Weight::from_parts(16_000_000, 0) + .saturating_add(Weight::from_parts(0, 6266)) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + /// Storage: `Sylo::ValidationRecords` (r:1 w:1) + /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 10]`. + /// The range of component `q` is `[1, 10]`. + fn create_validation_record(p: u32, q: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `213` + // Estimated: `15189` + // Minimum execution time: 16_000_000 picoseconds. + Weight::from_parts(15_824_136, 0) + .saturating_add(Weight::from_parts(0, 15189)) + // Standard Error: 5_825 + .saturating_add(Weight::from_parts(579_912, 0).saturating_mul(p.into())) + // Standard Error: 5_825 + .saturating_add(Weight::from_parts(88_911, 0).saturating_mul(q.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + /// Storage: `Sylo::ValidationRecords` (r:1 w:1) + /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + fn add_validation_record_entry() -> Weight { + // Proof Size summary in bytes: + // Measured: `317` + // Estimated: `15189` + // Minimum execution time: 18_000_000 picoseconds. + Weight::from_parts(19_000_000, 0) + .saturating_add(Weight::from_parts(0, 15189)) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + /// Storage: `Sylo::ValidationRecords` (r:1 w:1) + /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 10]`. + /// The range of component `q` is `[1, 10]`. + fn update_validation_record(p: u32, q: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `351` + // Estimated: `15189` + // Minimum execution time: 21_000_000 picoseconds. + Weight::from_parts(17_059_519, 0) + .saturating_add(Weight::from_parts(0, 15189)) + // Standard Error: 6_932 + .saturating_add(Weight::from_parts(866_848, 0).saturating_mul(p.into())) + // Standard Error: 6_932 + .saturating_add(Weight::from_parts(432_549, 0).saturating_mul(q.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + /// Storage: `Sylo::ValidationRecords` (r:1 w:1) + /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + fn delete_validation_record() -> Weight { + // Proof Size summary in bytes: + // Measured: `317` + // Estimated: `15189` + // Minimum execution time: 16_000_000 picoseconds. + Weight::from_parts(17_000_000, 0) + .saturating_add(Weight::from_parts(0, 15189)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } From 24cf65be1a4ea0a265a59298e85dfb6102d58d9e Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Wed, 8 Jan 2025 09:25:02 +1300 Subject: [PATCH 08/20] modify on charge tx to force sylo fee swap for sylo pallet extrinsics --- Cargo.lock | 1 + pallet/fee-proxy/Cargo.toml | 1 + pallet/fee-proxy/src/impls.rs | 84 ++++++++++++++++++------- pallet/sylo/src/benchmarking.rs | 2 + pallet/sylo/src/lib.rs | 57 ++++++++++------- pallet/sylo/src/mock.rs | 1 + pallet/sylo/src/tests.rs | 55 ++++++++--------- pallet/sylo/src/types.rs | 18 +++--- pallet/sylo/src/weights.rs | 99 ++++++++++++++++-------------- runtime/src/lib.rs | 1 + runtime/src/weights/pallet_sylo.rs | 55 ++++++++++------- 11 files changed, 224 insertions(+), 150 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 095062b56..14b929d4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5854,6 +5854,7 @@ dependencies = [ "pallet-evm", "pallet-fee-control", "pallet-futurepass", + "pallet-sylo", "pallet-timestamp", "pallet-transaction-payment", "parity-scale-codec", diff --git a/pallet/fee-proxy/Cargo.toml b/pallet/fee-proxy/Cargo.toml index d2cc3e043..ef966e31c 100644 --- a/pallet/fee-proxy/Cargo.toml +++ b/pallet/fee-proxy/Cargo.toml @@ -23,6 +23,7 @@ pallet-evm = { workspace = true } pallet-assets-ext = { workspace = true } pallet-dex = { workspace = true } pallet-futurepass = { workspace = true } +pallet-sylo = { workspace = true } pallet-transaction-payment = { workspace = true } precompile-utils = { workspace = true } diff --git a/pallet/fee-proxy/src/impls.rs b/pallet/fee-proxy/src/impls.rs index 9ac9dae9a..abfb81cb0 100644 --- a/pallet/fee-proxy/src/impls.rs +++ b/pallet/fee-proxy/src/impls.rs @@ -30,11 +30,14 @@ where + pallet_dex::Config + pallet_evm::Config + pallet_assets_ext::Config - + pallet_futurepass::Config, + + pallet_futurepass::Config + + pallet_sylo::Config, ::RuntimeCall: IsSubType>, ::RuntimeCall: IsSubType>, + ::RuntimeCall: IsSubType>, ::RuntimeCall: IsSubType>, ::RuntimeCall: IsSubType>, + ::RuntimeCall: IsSubType>, ::RuntimeCall: IsSubType>, ::OnChargeTransaction: OnChargeTransaction, ::ErcIdConversion: ErcIdConversion, @@ -65,12 +68,67 @@ where } } + let do_fee_swap = |payment_asset: AssetId, + mut total_fee: Balance, + max_payment: Balance| + -> Result<(), TransactionValidityError> { + let native_asset = ::FeeAssetId::get(); + + // If the account has less balance than the minimum_deposit, we need to add + // the minimum deposit onto the total_fee. + // This is due to the preservation rules of the withdraw call made within + // <::OnChargeTransaction as OnChargeTransaction>::withdraw_fee + let account_balance = pallet_assets_ext::Pallet::::balance(native_asset, who); + // Minium balance is hardcoded to 1 + // pallet_assets_ext::Pallet::::minimum_balance(native_asset); + let minimum_balance = pallet_assets_ext::Pallet::::minimum_balance(native_asset); + if account_balance < minimum_balance { + total_fee = total_fee.saturating_add(minimum_balance); + } + let path: &[AssetId] = &[payment_asset, native_asset]; + pallet_dex::Pallet::::do_swap_with_exact_target( + who, + total_fee, + max_payment, + path, + *who, + None, + ) + .map_err(|_| InvalidTransaction::Payment)?; + + Ok(()) + }; + + let is_sylo_call = match call.is_sub_type() { + Some(pallet_sylo::Call::register_resolver { .. }) => true, + Some(pallet_sylo::Call::update_resolver { .. }) => true, + Some(pallet_sylo::Call::unregister_resolver { .. }) => true, + Some(pallet_sylo::Call::create_validation_record { .. }) => true, + Some(pallet_sylo::Call::add_validation_record_entry { .. }) => true, + Some(pallet_sylo::Call::update_validation_record { .. }) => true, + Some(pallet_sylo::Call::delete_validation_record { .. }) => true, + _ => false, + }; + + // if the call is a sylo pallet call, then we always force a fee swap with the + // sylo token + if is_sylo_call { + let payment_asset = + pallet_sylo::Pallet::::payment_asset().ok_or(InvalidTransaction::Payment)?; + + do_fee_swap(payment_asset, Balance::from(fee), u128::MAX)?; + } + // Check whether this call has specified fee preferences if let Some(call_with_fee_preferences { payment_asset, max_payment, call }) = call.is_sub_type() { + // prevent using the fee proxy if the call is a sylo call + if is_sylo_call { + Err(InvalidTransaction::Payment)?; + } + let mut total_fee: Balance = Balance::from(fee); - let native_asset = ::FeeAssetId::get(); let mut add_evm_gas_cost = |gas_limit: &u64, @@ -127,27 +185,7 @@ where add_evm_gas_cost(gas_limit, max_fee_per_gas, max_priority_fee_per_gas); } - // If the account has less balance than the minimum_deposit, we need to add - // the minimum deposit onto the total_fee. - // This is due to the preservation rules of the withdraw call made within - // <::OnChargeTransaction as OnChargeTransaction>::withdraw_fee - let account_balance = pallet_assets_ext::Pallet::::balance(native_asset, who); - // Minium balance is hardcoded to 1 - // pallet_assets_ext::Pallet::::minimum_balance(native_asset); - let minimum_balance = pallet_assets_ext::Pallet::::minimum_balance(native_asset); - if account_balance < minimum_balance { - total_fee = total_fee.saturating_add(minimum_balance); - } - let path: &[AssetId] = &[*payment_asset, native_asset]; - pallet_dex::Pallet::::do_swap_with_exact_target( - who, - total_fee, - *max_payment, - path, - *who, - None, - ) - .map_err(|_| InvalidTransaction::Payment)?; + do_fee_swap(*payment_asset, total_fee, *max_payment)?; }; <::OnChargeTransaction as OnChargeTransaction>::withdraw_fee( diff --git a/pallet/sylo/src/benchmarking.rs b/pallet/sylo/src/benchmarking.rs index b68088845..18078904f 100644 --- a/pallet/sylo/src/benchmarking.rs +++ b/pallet/sylo/src/benchmarking.rs @@ -86,6 +86,8 @@ pub fn setup_validation_record( benchmarks! { where_clause { where ::AccountId: From + Into } + set_payment_asset {}: _(RawOrigin::Root, 24) + register_resolver { let p in 1 .. STRING_LIMIT; let q in 1 .. MAX_SERVICE_ENDPOINTS; diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 9e8838bfd..66e081c10 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -18,24 +18,15 @@ extern crate alloc; pub use pallet::*; use frame_support::{ + dispatch::{Dispatchable, GetDispatchInfo, PostDispatchInfo}, pallet_prelude::*, - traits::{ - fungibles::{self, metadata::Inspect as MetadataInspect, Inspect, Mutate}, - tokens::{Fortitude, Precision, Preservation}, - }, - transactional, PalletId, + traits::IsSubType, }; use frame_system::pallet_prelude::*; -use scale_info::TypeInfo; -use seed_pallet_common::CreateExt; -use seed_primitives::{AssetId, Balance}; -use serde::{Deserialize, Serialize}; -use sp_core::{H160, H256, U256}; -use sp_runtime::{ - traits::{AccountIdConversion, Zero}, - ArithmeticError, DispatchError, FixedU128, RuntimeDebug, SaturatedConversion, -}; -use sp_std::{cmp::min, convert::TryInto, prelude::*, vec}; +use seed_primitives::AssetId; +use sp_core::{H160, H256}; +use sp_std::prelude::*; +use sp_std::{convert::TryInto, vec}; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; @@ -63,6 +54,13 @@ pub mod pallet { #[pallet::config] pub trait Config: frame_system::Config { + /// The overarching call type. + type RuntimeCall: Parameter + + Dispatchable + + GetDispatchInfo + + From> + + IsSubType>; + /// The system event type type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Interface to access weight values @@ -87,6 +85,9 @@ pub mod pallet { type SyloResolverMethod: Get<[u8; 9]>; } + #[pallet::storage] + pub type SyloAssetId = StorageValue<_, AssetId, OptionQuery>; + #[pallet::storage] pub type Resolvers = StorageMap< _, @@ -167,6 +168,16 @@ pub mod pallet { #[pallet::call] impl Pallet { #[pallet::call_index(0)] + #[pallet::weight({ + T::WeightInfo::set_payment_asset() + })] + pub fn set_payment_asset(origin: OriginFor, payment_asset: AssetId) -> DispatchResult { + ensure_root(origin)?; + >::put(payment_asset); + Ok(()) + } + + #[pallet::call_index(1)] #[pallet::weight({ T::WeightInfo::register_resolver(::get(), ::get()) })] @@ -196,7 +207,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(1)] + #[pallet::call_index(2)] #[pallet::weight({ T::WeightInfo::update_resolver(::get()) })] @@ -225,7 +236,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(2)] + #[pallet::call_index(3)] #[pallet::weight({ T::WeightInfo::unregister_resolver() })] @@ -247,7 +258,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(3)] + #[pallet::call_index(4)] #[pallet::weight({ T::WeightInfo::create_validation_record(::get(), ::get()) })] @@ -291,7 +302,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(4)] + #[pallet::call_index(5)] #[pallet::weight({ T::WeightInfo::add_validation_record_entry() })] @@ -321,7 +332,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(5)] + #[pallet::call_index(6)] #[pallet::weight({ T::WeightInfo::update_validation_record(::get(), ::get()) })] @@ -364,7 +375,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(6)] + #[pallet::call_index(7)] #[pallet::weight({ T::WeightInfo::delete_validation_record() })] @@ -410,5 +421,9 @@ pub mod pallet { Ok(()) } + + pub fn payment_asset() -> Option { + >::get() + } } } diff --git a/pallet/sylo/src/mock.rs b/pallet/sylo/src/mock.rs index 665aa46af..4cbde904c 100644 --- a/pallet/sylo/src/mock.rs +++ b/pallet/sylo/src/mock.rs @@ -41,6 +41,7 @@ parameter_types! { pub const SyloResolverMethod: [u8; 9] = *b"sylo-data"; } impl Config for Test { + type RuntimeCall = RuntimeCall; type RuntimeEvent = RuntimeEvent; type MaxResolvers = MaxResolvers; type MaxTags = MaxTags; diff --git a/pallet/sylo/src/tests.rs b/pallet/sylo/src/tests.rs index a4323d45a..fc6f57b8e 100644 --- a/pallet/sylo/src/tests.rs +++ b/pallet/sylo/src/tests.rs @@ -14,8 +14,7 @@ // You may obtain a copy of the License at the root of this project source code use super::*; -use hex::encode; -use mock::{RuntimeEvent as MockEvent, RuntimeOrigin, Sylo, System, Test, TestExt}; +use mock::{RuntimeEvent as MockEvent, Sylo, System, Test, TestExt}; use seed_pallet_common::test_prelude::*; mod resolvers { @@ -80,7 +79,7 @@ mod resolvers { #[test] fn resolver_unregistration_works() { TestExt.build().execute_with(|| { - let (controller, identifier, mut service_endpoints) = create_and_register_resolver( + let (controller, identifier, _) = create_and_register_resolver( bounded_string("test-resolver"), vec![ bounded_string("https://endpoint.one"), @@ -104,7 +103,7 @@ mod resolvers { #[test] fn resolver_register_existing_fails() { TestExt.build().execute_with(|| { - let (controller, identifier, mut service_endpoints) = create_and_register_resolver( + let (controller, identifier, service_endpoints) = create_and_register_resolver( bounded_string("test-resolver"), vec![ bounded_string("https://endpoint.one"), @@ -147,7 +146,7 @@ mod resolvers { #[test] fn resolver_update_not_controller_fails() { TestExt.build().execute_with(|| { - let (controller, identifier, service_endpoints) = create_and_register_resolver( + let (_, identifier, service_endpoints) = create_and_register_resolver( bounded_string("test-resolver"), vec![ bounded_string("https://endpoint.one"), @@ -185,7 +184,7 @@ mod resolvers { #[test] fn resolver_unregister_not_controller_fails() { TestExt.build().execute_with(|| { - let (controller, identifier, service_endpoints) = create_and_register_resolver( + let (_, identifier, _) = create_and_register_resolver( bounded_string("test-resolver"), vec![ bounded_string("https://endpoint.one"), @@ -251,7 +250,7 @@ mod validation_records { TestExt.build().execute_with(|| { let alice: AccountId = create_account(2); - let (data_id, resolvers, data_type, tags, checksum, record) = + let (data_id, resolvers, data_type, tags, checksum, _) = create_initial_validation_record( alice, "data_id", @@ -332,7 +331,7 @@ mod validation_records { TestExt.build().execute_with(|| { let alice: AccountId = create_account(2); - let (data_id, resolvers, data_type, tags, checksum, record) = + let (data_id, resolvers, data_type, tags, checksum, _) = create_initial_validation_record( alice, "data_id", @@ -443,7 +442,7 @@ mod validation_records { TestExt.build().execute_with(|| { let alice: AccountId = create_account(2); - let (data_id, resolvers, data_type, tags, checksum, record) = + let (data_id, resolvers, data_type, tags, checksum, _) = create_initial_validation_record( alice, "data_id", @@ -509,7 +508,7 @@ mod validation_records { TestExt.build().execute_with(|| { let alice: AccountId = create_account(2); - let (data_id, resolvers, data_type, tags, checksum, record) = + let (data_id, resolvers, data_type, tags, checksum, _) = create_initial_validation_record( alice, "data_id", @@ -668,14 +667,13 @@ mod validation_records { TestExt.build().execute_with(|| { let alice: AccountId = create_account(2); - let (data_id, resolvers, data_type, tags, checksum, record) = - create_initial_validation_record( - alice, - "data_id", - vec![("method-1", "resolver-1")], - "data_type", - vec!["tag-1", "tag-2"], - ); + let (data_id, resolvers, data_type, tags, _, _) = create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); assert_noop!( Sylo::update_validation_record( @@ -695,7 +693,7 @@ mod validation_records { TestExt.build().execute_with(|| { let alice: AccountId = create_account(2); - let (data_id, resolvers, data_type, tags, checksum, record) = + let (data_id, resolvers, data_type, tags, checksum, _) = create_initial_validation_record( alice, "data_id", @@ -733,7 +731,7 @@ mod validation_records { TestExt.build().execute_with(|| { let alice: AccountId = create_account(2); - let (data_id, resolvers, data_type, tags, checksum, record) = + let (data_id, resolvers, data_type, tags, checksum, _) = create_initial_validation_record( alice, "data_id", @@ -763,14 +761,13 @@ mod validation_records { TestExt.build().execute_with(|| { let alice: AccountId = create_account(2); - let (data_id, resolvers, data_type, tags, checksum, _) = - create_initial_validation_record( - alice, - "data_id", - vec![("method-1", "resolver-1")], - "data_type", - vec!["tag-1", "tag-2"], - ); + let (data_id, resolvers, data_type, tags, _, _) = create_initial_validation_record( + alice, + "data_id", + vec![("method-1", "resolver-1")], + "data_type", + vec!["tag-1", "tag-2"], + ); assert_noop!( Sylo::update_validation_record( @@ -790,7 +787,7 @@ mod validation_records { TestExt.build().execute_with(|| { let alice: AccountId = create_account(2); - let (data_id, resolvers, data_type, tags, checksum, record) = + let (data_id, resolvers, data_type, tags, checksum, _) = create_initial_validation_record( alice, "data_id", diff --git a/pallet/sylo/src/types.rs b/pallet/sylo/src/types.rs index 3a460fb08..9f7b723ea 100644 --- a/pallet/sylo/src/types.rs +++ b/pallet/sylo/src/types.rs @@ -13,14 +13,13 @@ // limitations under the License. // You may obtain a copy of the License at the root of this project source code +use alloc::{format, string::String, vec::Vec}; use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::{ - parameter_types, traits::Get, BoundedVec, CloneNoBound, EqNoBound, PartialEqNoBound, - RuntimeDebug, RuntimeDebugNoBound, + traits::Get, BoundedVec, CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound, }; use scale_info::TypeInfo; -use seed_primitives::{AssetId, Balance, Block}; -use sp_core::{hexdisplay::AsBytesRef, H160, H256}; +use sp_core::H256; use sp_std::{fmt::Debug, prelude::*}; #[derive( @@ -44,14 +43,13 @@ where impl> ResolverId { pub fn to_did(&self) -> Vec { - return Vec::new(); - // let method = self.method.to_vec(); - // let method = String::from_utf8_lossy(method.as_bytes_ref()); + let method = self.method.to_vec(); + let method = String::from_utf8_lossy(method.as_slice()); - // let identifier = self.identifier.to_vec(); - // let identifier = String::from_utf8_lossy(identifier.as_bytes_ref()); + let identifier = self.identifier.to_vec(); + let identifier = String::from_utf8_lossy(identifier.as_slice()); - // format!("did:{method}:{identifier}").as_bytes().to_vec() + format!("did:{method}:{identifier}").as_bytes().to_vec() } } diff --git a/pallet/sylo/src/weights.rs b/pallet/sylo/src/weights.rs index 75e0e98da..89cedd9bf 100644 --- a/pallet/sylo/src/weights.rs +++ b/pallet/sylo/src/weights.rs @@ -18,7 +18,7 @@ //! Autogenerated weights for pallet_sylo //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2025-01-06, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2025-01-07, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` //! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 @@ -47,6 +47,7 @@ use sp_std::marker::PhantomData; /// Weight functions needed for pallet_sylo. pub trait WeightInfo { + fn set_payment_asset() -> Weight; fn register_resolver(p: u32, q: u32, ) -> Weight; fn update_resolver(q: u32, ) -> Weight; fn unregister_resolver() -> Weight; @@ -59,16 +60,20 @@ pub trait WeightInfo { /// Weights for pallet_sylo using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { + // Storage: `Sylo::SyloAssetId` (r:0 w:1) + // Proof: `Sylo::SyloAssetId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) + fn set_payment_asset() -> Weight { + Weight::from_all(4_000_000) + .saturating_add(T::DbWeight::get().writes(1)) + } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn register_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(13_647_734) - // Standard Error: 237 - .saturating_add(Weight::from_all(2_614_u64).saturating_mul(p as u64)) - // Standard Error: 6_184 - .saturating_add(Weight::from_all(250_475_u64).saturating_mul(q as u64)) + fn register_resolver(_p: u32, q: u32, ) -> Weight { + Weight::from_all(15_220_389) + // Standard Error: 19_676 + .saturating_add(Weight::from_all(296_488_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -76,16 +81,16 @@ impl WeightInfo for SubstrateWeight { // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) /// The range of component `q` is `[1, 10]`. fn update_resolver(q: u32, ) -> Weight { - Weight::from_all(15_835_394) - // Standard Error: 6_400 - .saturating_add(Weight::from_all(354_702_u64).saturating_mul(q as u64)) + Weight::from_all(16_248_317) + // Standard Error: 7_549 + .saturating_add(Weight::from_all(389_976_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) fn unregister_resolver() -> Weight { - Weight::from_all(15_000_000) + Weight::from_all(16_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -94,18 +99,18 @@ impl WeightInfo for SubstrateWeight { /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(14_090_414) - // Standard Error: 5_662 - .saturating_add(Weight::from_all(672_999_u64).saturating_mul(p as u64)) - // Standard Error: 5_662 - .saturating_add(Weight::from_all(227_877_u64).saturating_mul(q as u64)) + Weight::from_all(14_672_706) + // Standard Error: 7_561 + .saturating_add(Weight::from_all(660_188_u64).saturating_mul(p as u64)) + // Standard Error: 7_561 + .saturating_add(Weight::from_all(231_994_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { - Weight::from_all(18_000_000) + Weight::from_all(19_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -114,18 +119,18 @@ impl WeightInfo for SubstrateWeight { /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn update_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(18_203_859) - // Standard Error: 7_399 - .saturating_add(Weight::from_all(754_695_u64).saturating_mul(p as u64)) - // Standard Error: 7_399 - .saturating_add(Weight::from_all(390_063_u64).saturating_mul(q as u64)) + Weight::from_all(17_300_220) + // Standard Error: 8_614 + .saturating_add(Weight::from_all(1_049_282_u64).saturating_mul(p as u64)) + // Standard Error: 8_614 + .saturating_add(Weight::from_all(441_101_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { - Weight::from_all(16_000_000) + Weight::from_all(18_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -133,16 +138,20 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { + // Storage: `Sylo::SyloAssetId` (r:0 w:1) + // Proof: `Sylo::SyloAssetId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) + fn set_payment_asset() -> Weight { + Weight::from_all(4_000_000) + .saturating_add(RocksDbWeight::get().writes(1)) + } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn register_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(13_647_734) - // Standard Error: 237 - .saturating_add(Weight::from_all(2_614_u64).saturating_mul(p as u64)) - // Standard Error: 6_184 - .saturating_add(Weight::from_all(250_475_u64).saturating_mul(q as u64)) + fn register_resolver(_p: u32, q: u32, ) -> Weight { + Weight::from_all(15_220_389) + // Standard Error: 19_676 + .saturating_add(Weight::from_all(296_488_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -150,16 +159,16 @@ impl WeightInfo for () { // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) /// The range of component `q` is `[1, 10]`. fn update_resolver(q: u32, ) -> Weight { - Weight::from_all(15_835_394) - // Standard Error: 6_400 - .saturating_add(Weight::from_all(354_702_u64).saturating_mul(q as u64)) + Weight::from_all(16_248_317) + // Standard Error: 7_549 + .saturating_add(Weight::from_all(389_976_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) fn unregister_resolver() -> Weight { - Weight::from_all(15_000_000) + Weight::from_all(16_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -168,18 +177,18 @@ impl WeightInfo for () { /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(14_090_414) - // Standard Error: 5_662 - .saturating_add(Weight::from_all(672_999_u64).saturating_mul(p as u64)) - // Standard Error: 5_662 - .saturating_add(Weight::from_all(227_877_u64).saturating_mul(q as u64)) + Weight::from_all(14_672_706) + // Standard Error: 7_561 + .saturating_add(Weight::from_all(660_188_u64).saturating_mul(p as u64)) + // Standard Error: 7_561 + .saturating_add(Weight::from_all(231_994_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { - Weight::from_all(18_000_000) + Weight::from_all(19_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -188,18 +197,18 @@ impl WeightInfo for () { /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn update_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(18_203_859) - // Standard Error: 7_399 - .saturating_add(Weight::from_all(754_695_u64).saturating_mul(p as u64)) - // Standard Error: 7_399 - .saturating_add(Weight::from_all(390_063_u64).saturating_mul(q as u64)) + Weight::from_all(17_300_220) + // Standard Error: 8_614 + .saturating_add(Weight::from_all(1_049_282_u64).saturating_mul(p as u64)) + // Standard Error: 8_614 + .saturating_add(Weight::from_all(441_101_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { - Weight::from_all(16_000_000) + Weight::from_all(18_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 26d9b2aab..be768003c 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -606,6 +606,7 @@ parameter_types! { } impl pallet_sylo::Config for Runtime { + type RuntimeCall = RuntimeCall; type RuntimeEvent = RuntimeEvent; type MaxResolvers = MaxResolvers; type MaxTags = MaxTags; diff --git a/runtime/src/weights/pallet_sylo.rs b/runtime/src/weights/pallet_sylo.rs index ebd3f5ba5..64e7c384f 100644 --- a/runtime/src/weights/pallet_sylo.rs +++ b/runtime/src/weights/pallet_sylo.rs @@ -2,7 +2,7 @@ //! Autogenerated weights for `pallet_sylo` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2025-01-06, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2025-01-07, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` //! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024 @@ -32,6 +32,17 @@ use core::marker::PhantomData; /// Weight functions for `pallet_sylo`. pub struct WeightInfo(PhantomData); impl pallet_sylo::WeightInfo for WeightInfo { + /// Storage: `Sylo::SyloAssetId` (r:0 w:1) + /// Proof: `Sylo::SyloAssetId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) + fn set_payment_asset() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 3_000_000 picoseconds. + Weight::from_parts(4_000_000, 0) + .saturating_add(Weight::from_parts(0, 0)) + .saturating_add(T::DbWeight::get().writes(1)) + } /// Storage: `Sylo::Resolvers` (r:1 w:1) /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 250]`. @@ -41,12 +52,12 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Measured: `175` // Estimated: `6266` // Minimum execution time: 13_000_000 picoseconds. - Weight::from_parts(12_416_241, 0) + Weight::from_parts(13_760_190, 0) .saturating_add(Weight::from_parts(0, 6266)) - // Standard Error: 224 - .saturating_add(Weight::from_parts(3_202, 0).saturating_mul(p.into())) - // Standard Error: 5_847 - .saturating_add(Weight::from_parts(375_131, 0).saturating_mul(q.into())) + // Standard Error: 313 + .saturating_add(Weight::from_parts(3_660, 0).saturating_mul(p.into())) + // Standard Error: 8_176 + .saturating_add(Weight::from_parts(309_234, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -58,10 +69,10 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Measured: `289` // Estimated: `6266` // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(16_038_523, 0) + Weight::from_parts(16_249_618, 0) .saturating_add(Weight::from_parts(0, 6266)) - // Standard Error: 7_792 - .saturating_add(Weight::from_parts(337_744, 0).saturating_mul(q.into())) + // Standard Error: 8_386 + .saturating_add(Weight::from_parts(416_808, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -86,12 +97,12 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Measured: `213` // Estimated: `15189` // Minimum execution time: 16_000_000 picoseconds. - Weight::from_parts(15_824_136, 0) + Weight::from_parts(14_981_271, 0) .saturating_add(Weight::from_parts(0, 15189)) - // Standard Error: 5_825 - .saturating_add(Weight::from_parts(579_912, 0).saturating_mul(p.into())) - // Standard Error: 5_825 - .saturating_add(Weight::from_parts(88_911, 0).saturating_mul(q.into())) + // Standard Error: 7_754 + .saturating_add(Weight::from_parts(698_812, 0).saturating_mul(p.into())) + // Standard Error: 7_754 + .saturating_add(Weight::from_parts(183_708, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -101,8 +112,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `317` // Estimated: `15189` - // Minimum execution time: 18_000_000 picoseconds. - Weight::from_parts(19_000_000, 0) + // Minimum execution time: 19_000_000 picoseconds. + Weight::from_parts(26_000_000, 0) .saturating_add(Weight::from_parts(0, 15189)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) @@ -115,13 +126,13 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `351` // Estimated: `15189` - // Minimum execution time: 21_000_000 picoseconds. - Weight::from_parts(17_059_519, 0) + // Minimum execution time: 22_000_000 picoseconds. + Weight::from_parts(19_834_497, 0) .saturating_add(Weight::from_parts(0, 15189)) - // Standard Error: 6_932 - .saturating_add(Weight::from_parts(866_848, 0).saturating_mul(p.into())) - // Standard Error: 6_932 - .saturating_add(Weight::from_parts(432_549, 0).saturating_mul(q.into())) + // Standard Error: 26_349 + .saturating_add(Weight::from_parts(1_090_616, 0).saturating_mul(p.into())) + // Standard Error: 26_349 + .saturating_add(Weight::from_parts(204_524, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } From 42d44fa4b24d58df443f69cc1ed5bf9f6892ae8f Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Wed, 8 Jan 2025 09:50:43 +1300 Subject: [PATCH 09/20] use a storage value for reserved sylo resolver method --- pallet/sylo/src/benchmarking.rs | 2 + pallet/sylo/src/lib.rs | 46 +++++++--- pallet/sylo/src/mock.rs | 2 - pallet/sylo/src/weights.rs | 129 +++++++++++++++++------------ runtime/src/lib.rs | 4 +- runtime/src/weights/pallet_sylo.rs | 111 ++++++++++++++----------- 6 files changed, 175 insertions(+), 119 deletions(-) diff --git a/pallet/sylo/src/benchmarking.rs b/pallet/sylo/src/benchmarking.rs index 18078904f..82d3a8176 100644 --- a/pallet/sylo/src/benchmarking.rs +++ b/pallet/sylo/src/benchmarking.rs @@ -88,6 +88,8 @@ benchmarks! { set_payment_asset {}: _(RawOrigin::Root, 24) + set_sylo_resolver_method {}: _(RawOrigin::Root, bounded_string::("sylo-resolver-method")) + register_resolver { let p in 1 .. STRING_LIMIT; let q in 1 .. MAX_SERVICE_ENDPOINTS; diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 66e081c10..45446d3a8 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -80,13 +80,24 @@ pub mod pallet { #[pallet::constant] type StringLimit: Get; + } - #[pallet::constant] - type SyloResolverMethod: Get<[u8; 9]>; + /// The default string used as the reserved method for sylo resolvers + #[pallet::type_value] + pub fn DefaultReservedSyloResolvedMethod() -> BoundedVec { + BoundedVec::truncate_from(b"sylo-data".to_vec()) } #[pallet::storage] - pub type SyloAssetId = StorageValue<_, AssetId, OptionQuery>; + pub type SyloAssetId = StorageValue<_, AssetId, OptionQuery>; + + #[pallet::storage] + pub type SyloResolverMethod = StorageValue< + _, + BoundedVec, + ValueQuery, + DefaultReservedSyloResolvedMethod, + >; #[pallet::storage] pub type Resolvers = StorageMap< @@ -178,6 +189,19 @@ pub mod pallet { } #[pallet::call_index(1)] + #[pallet::weight({ + T::WeightInfo::set_sylo_resolver_method() + })] + pub fn set_sylo_resolver_method( + origin: OriginFor, + resolver_method: BoundedVec, + ) -> DispatchResult { + ensure_root(origin)?; + >::put(resolver_method); + Ok(()) + } + + #[pallet::call_index(2)] #[pallet::weight({ T::WeightInfo::register_resolver(::get(), ::get()) })] @@ -207,7 +231,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(2)] + #[pallet::call_index(3)] #[pallet::weight({ T::WeightInfo::update_resolver(::get()) })] @@ -236,7 +260,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(3)] + #[pallet::call_index(4)] #[pallet::weight({ T::WeightInfo::unregister_resolver() })] @@ -258,7 +282,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(4)] + #[pallet::call_index(5)] #[pallet::weight({ T::WeightInfo::create_validation_record(::get(), ::get()) })] @@ -302,7 +326,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(5)] + #[pallet::call_index(6)] #[pallet::weight({ T::WeightInfo::add_validation_record_entry() })] @@ -332,7 +356,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(6)] + #[pallet::call_index(7)] #[pallet::weight({ T::WeightInfo::update_validation_record(::get(), ::get()) })] @@ -375,7 +399,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(7)] + #[pallet::call_index(8)] #[pallet::weight({ T::WeightInfo::delete_validation_record() })] @@ -405,9 +429,7 @@ pub mod pallet { pub fn validate_sylo_resolvers( resolvers: BoundedVec, T::MaxResolvers>, ) -> DispatchResult { - let reserved_method: BoundedVec = - BoundedVec::try_from(::SyloResolverMethod::get().to_vec()) - .expect("Failed to convert invalid resolver method config"); + let reserved_method = >::get(); // Ensure any sylo data resolvers are already registered for resolver in resolvers { diff --git a/pallet/sylo/src/mock.rs b/pallet/sylo/src/mock.rs index 4cbde904c..8646649a3 100644 --- a/pallet/sylo/src/mock.rs +++ b/pallet/sylo/src/mock.rs @@ -38,7 +38,6 @@ parameter_types! { pub const MaxEntries: u32 = 100; pub const MaxServiceEndpoints: u32 = 10; pub const StringLimit: u32 = 100; - pub const SyloResolverMethod: [u8; 9] = *b"sylo-data"; } impl Config for Test { type RuntimeCall = RuntimeCall; @@ -48,7 +47,6 @@ impl Config for Test { type MaxEntries = MaxEntries; type MaxServiceEndpoints = MaxServiceEndpoints; type StringLimit = StringLimit; - type SyloResolverMethod = SyloResolverMethod; type WeightInfo = (); } diff --git a/pallet/sylo/src/weights.rs b/pallet/sylo/src/weights.rs index 89cedd9bf..46aab780a 100644 --- a/pallet/sylo/src/weights.rs +++ b/pallet/sylo/src/weights.rs @@ -48,6 +48,7 @@ use sp_std::marker::PhantomData; /// Weight functions needed for pallet_sylo. pub trait WeightInfo { fn set_payment_asset() -> Weight; + fn set_sylo_resolver_method() -> Weight; fn register_resolver(p: u32, q: u32, ) -> Weight; fn update_resolver(q: u32, ) -> Weight; fn unregister_resolver() -> Weight; @@ -66,71 +67,81 @@ impl WeightInfo for SubstrateWeight { Weight::from_all(4_000_000) .saturating_add(T::DbWeight::get().writes(1)) } + // Storage: `Sylo::SyloResolverMethod` (r:0 w:1) + // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) + fn set_sylo_resolver_method() -> Weight { + Weight::from_all(4_000_000) + .saturating_add(T::DbWeight::get().writes(1)) + } // Storage: `Sylo::Resolvers` (r:1 w:1) - // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn register_resolver(_p: u32, q: u32, ) -> Weight { - Weight::from_all(15_220_389) - // Standard Error: 19_676 - .saturating_add(Weight::from_all(296_488_u64).saturating_mul(q as u64)) + Weight::from_all(14_351_359) + // Standard Error: 7_325 + .saturating_add(Weight::from_all(296_417_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) - // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) /// The range of component `q` is `[1, 10]`. fn update_resolver(q: u32, ) -> Weight { - Weight::from_all(16_248_317) - // Standard Error: 7_549 - .saturating_add(Weight::from_all(389_976_u64).saturating_mul(q as u64)) + Weight::from_all(15_992_647) + // Standard Error: 6_497 + .saturating_add(Weight::from_all(366_404_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) - // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) fn unregister_resolver() -> Weight { - Weight::from_all(16_000_000) + Weight::from_all(15_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) - // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) + // Storage: `Sylo::SyloResolverMethod` (r:1 w:0) + // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(14_672_706) - // Standard Error: 7_561 - .saturating_add(Weight::from_all(660_188_u64).saturating_mul(p as u64)) - // Standard Error: 7_561 - .saturating_add(Weight::from_all(231_994_u64).saturating_mul(q as u64)) - .saturating_add(T::DbWeight::get().reads(1)) + Weight::from_all(16_124_714) + // Standard Error: 9_176 + .saturating_add(Weight::from_all(765_759_u64).saturating_mul(p as u64)) + // Standard Error: 9_176 + .saturating_add(Weight::from_all(231_083_u64).saturating_mul(q as u64)) + .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) - // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { Weight::from_all(19_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) - // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) + // Storage: `Sylo::SyloResolverMethod` (r:1 w:0) + // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn update_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(17_300_220) - // Standard Error: 8_614 - .saturating_add(Weight::from_all(1_049_282_u64).saturating_mul(p as u64)) - // Standard Error: 8_614 - .saturating_add(Weight::from_all(441_101_u64).saturating_mul(q as u64)) - .saturating_add(T::DbWeight::get().reads(1)) + Weight::from_all(18_475_818) + // Standard Error: 8_599 + .saturating_add(Weight::from_all(1_784_700_u64).saturating_mul(p as u64)) + // Standard Error: 8_599 + .saturating_add(Weight::from_all(364_058_u64).saturating_mul(q as u64)) + .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) - // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { - Weight::from_all(18_000_000) + Weight::from_all(17_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -144,71 +155,81 @@ impl WeightInfo for () { Weight::from_all(4_000_000) .saturating_add(RocksDbWeight::get().writes(1)) } + // Storage: `Sylo::SyloResolverMethod` (r:0 w:1) + // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) + fn set_sylo_resolver_method() -> Weight { + Weight::from_all(4_000_000) + .saturating_add(RocksDbWeight::get().writes(1)) + } // Storage: `Sylo::Resolvers` (r:1 w:1) - // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn register_resolver(_p: u32, q: u32, ) -> Weight { - Weight::from_all(15_220_389) - // Standard Error: 19_676 - .saturating_add(Weight::from_all(296_488_u64).saturating_mul(q as u64)) + Weight::from_all(14_351_359) + // Standard Error: 7_325 + .saturating_add(Weight::from_all(296_417_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) - // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) /// The range of component `q` is `[1, 10]`. fn update_resolver(q: u32, ) -> Weight { - Weight::from_all(16_248_317) - // Standard Error: 7_549 - .saturating_add(Weight::from_all(389_976_u64).saturating_mul(q as u64)) + Weight::from_all(15_992_647) + // Standard Error: 6_497 + .saturating_add(Weight::from_all(366_404_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) - // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) fn unregister_resolver() -> Weight { - Weight::from_all(16_000_000) + Weight::from_all(15_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) - // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) + // Storage: `Sylo::SyloResolverMethod` (r:1 w:0) + // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(14_672_706) - // Standard Error: 7_561 - .saturating_add(Weight::from_all(660_188_u64).saturating_mul(p as u64)) - // Standard Error: 7_561 - .saturating_add(Weight::from_all(231_994_u64).saturating_mul(q as u64)) - .saturating_add(RocksDbWeight::get().reads(1)) + Weight::from_all(16_124_714) + // Standard Error: 9_176 + .saturating_add(Weight::from_all(765_759_u64).saturating_mul(p as u64)) + // Standard Error: 9_176 + .saturating_add(Weight::from_all(231_083_u64).saturating_mul(q as u64)) + .saturating_add(RocksDbWeight::get().reads(2)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) - // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { Weight::from_all(19_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) - // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) + // Storage: `Sylo::SyloResolverMethod` (r:1 w:0) + // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn update_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(17_300_220) - // Standard Error: 8_614 - .saturating_add(Weight::from_all(1_049_282_u64).saturating_mul(p as u64)) - // Standard Error: 8_614 - .saturating_add(Weight::from_all(441_101_u64).saturating_mul(q as u64)) - .saturating_add(RocksDbWeight::get().reads(1)) + Weight::from_all(18_475_818) + // Standard Error: 8_599 + .saturating_add(Weight::from_all(1_784_700_u64).saturating_mul(p as u64)) + // Standard Error: 8_599 + .saturating_add(Weight::from_all(364_058_u64).saturating_mul(q as u64)) + .saturating_add(RocksDbWeight::get().reads(2)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) - // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { - Weight::from_all(18_000_000) + Weight::from_all(17_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index be768003c..f032d9e7a 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -601,8 +601,7 @@ parameter_types! { pub const MaxTags: u32 = 10; pub const MaxEntries: u32 = 100; pub const MaxServiceEndpoints: u32 = 10; - pub const SyloStringLimit: u32 = 250; - pub const SyloResolverMethod: [u8; 9] = *b"sylo-data"; + pub const SyloStringLimit: u32 = 500; } impl pallet_sylo::Config for Runtime { @@ -613,7 +612,6 @@ impl pallet_sylo::Config for Runtime { type MaxEntries = MaxEntries; type MaxServiceEndpoints = MaxServiceEndpoints; type StringLimit = SyloStringLimit; - type SyloResolverMethod = SyloResolverMethod; type WeightInfo = weights::pallet_sylo::WeightInfo; } diff --git a/runtime/src/weights/pallet_sylo.rs b/runtime/src/weights/pallet_sylo.rs index 64e7c384f..e76a0e8eb 100644 --- a/runtime/src/weights/pallet_sylo.rs +++ b/runtime/src/weights/pallet_sylo.rs @@ -43,108 +43,123 @@ impl pallet_sylo::WeightInfo for WeightInfo { .saturating_add(Weight::from_parts(0, 0)) .saturating_add(T::DbWeight::get().writes(1)) } + /// Storage: `Sylo::SyloResolverMethod` (r:0 w:1) + /// Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) + fn set_sylo_resolver_method() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 3_000_000 picoseconds. + Weight::from_parts(4_000_000, 0) + .saturating_add(Weight::from_parts(0, 0)) + .saturating_add(T::DbWeight::get().writes(1)) + } /// Storage: `Sylo::Resolvers` (r:1 w:1) - /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn register_resolver(p: u32, q: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `175` - // Estimated: `6266` + // Estimated: `9016` // Minimum execution time: 13_000_000 picoseconds. - Weight::from_parts(13_760_190, 0) - .saturating_add(Weight::from_parts(0, 6266)) - // Standard Error: 313 - .saturating_add(Weight::from_parts(3_660, 0).saturating_mul(p.into())) - // Standard Error: 8_176 - .saturating_add(Weight::from_parts(309_234, 0).saturating_mul(q.into())) + Weight::from_parts(12_953_454, 0) + .saturating_add(Weight::from_parts(0, 9016)) + // Standard Error: 216 + .saturating_add(Weight::from_parts(2_490, 0).saturating_mul(p.into())) + // Standard Error: 5_652 + .saturating_add(Weight::from_parts(301_265, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } /// Storage: `Sylo::Resolvers` (r:1 w:1) - /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) /// The range of component `q` is `[1, 10]`. fn update_resolver(q: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `289` - // Estimated: `6266` + // Estimated: `9016` // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(16_249_618, 0) - .saturating_add(Weight::from_parts(0, 6266)) - // Standard Error: 8_386 - .saturating_add(Weight::from_parts(416_808, 0).saturating_mul(q.into())) + Weight::from_parts(15_831_188, 0) + .saturating_add(Weight::from_parts(0, 9016)) + // Standard Error: 5_846 + .saturating_add(Weight::from_parts(350_759, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } /// Storage: `Sylo::Resolvers` (r:1 w:1) - /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(2801), added: 5276, mode: `MaxEncodedLen`) + /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) fn unregister_resolver() -> Weight { // Proof Size summary in bytes: // Measured: `289` - // Estimated: `6266` + // Estimated: `9016` // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(16_000_000, 0) - .saturating_add(Weight::from_parts(0, 6266)) + Weight::from_parts(15_000_000, 0) + .saturating_add(Weight::from_parts(0, 9016)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } /// Storage: `Sylo::ValidationRecords` (r:1 w:1) - /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) + /// Storage: `Sylo::SyloResolverMethod` (r:1 w:0) + /// Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `213` - // Estimated: `15189` - // Minimum execution time: 16_000_000 picoseconds. - Weight::from_parts(14_981_271, 0) - .saturating_add(Weight::from_parts(0, 15189)) - // Standard Error: 7_754 - .saturating_add(Weight::from_parts(698_812, 0).saturating_mul(p.into())) - // Standard Error: 7_754 - .saturating_add(Weight::from_parts(183_708, 0).saturating_mul(q.into())) - .saturating_add(T::DbWeight::get().reads(1)) + // Measured: `217 + p * (16 ±0)` + // Estimated: `23189` + // Minimum execution time: 17_000_000 picoseconds. + Weight::from_parts(17_280_488, 0) + .saturating_add(Weight::from_parts(0, 23189)) + // Standard Error: 9_099 + .saturating_add(Weight::from_parts(673_932, 0).saturating_mul(p.into())) + // Standard Error: 9_099 + .saturating_add(Weight::from_parts(133_592, 0).saturating_mul(q.into())) + .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } /// Storage: `Sylo::ValidationRecords` (r:1 w:1) - /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { // Proof Size summary in bytes: // Measured: `317` - // Estimated: `15189` - // Minimum execution time: 19_000_000 picoseconds. - Weight::from_parts(26_000_000, 0) - .saturating_add(Weight::from_parts(0, 15189)) + // Estimated: `23189` + // Minimum execution time: 18_000_000 picoseconds. + Weight::from_parts(19_000_000, 0) + .saturating_add(Weight::from_parts(0, 23189)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } /// Storage: `Sylo::ValidationRecords` (r:1 w:1) - /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) + /// Storage: `Sylo::SyloResolverMethod` (r:1 w:0) + /// Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn update_validation_record(p: u32, q: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `351` - // Estimated: `15189` + // Measured: `355 + p * (16 ±0)` + // Estimated: `23189` // Minimum execution time: 22_000_000 picoseconds. - Weight::from_parts(19_834_497, 0) - .saturating_add(Weight::from_parts(0, 15189)) - // Standard Error: 26_349 - .saturating_add(Weight::from_parts(1_090_616, 0).saturating_mul(p.into())) - // Standard Error: 26_349 - .saturating_add(Weight::from_parts(204_524, 0).saturating_mul(q.into())) - .saturating_add(T::DbWeight::get().reads(1)) + Weight::from_parts(18_370_121, 0) + .saturating_add(Weight::from_parts(0, 23189)) + // Standard Error: 6_644 + .saturating_add(Weight::from_parts(1_759_119, 0).saturating_mul(p.into())) + // Standard Error: 6_644 + .saturating_add(Weight::from_parts(374_286, 0).saturating_mul(q.into())) + .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } /// Storage: `Sylo::ValidationRecords` (r:1 w:1) - /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(11724), added: 14199, mode: `MaxEncodedLen`) + /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { // Proof Size summary in bytes: // Measured: `317` - // Estimated: `15189` - // Minimum execution time: 16_000_000 picoseconds. + // Estimated: `23189` + // Minimum execution time: 17_000_000 picoseconds. Weight::from_parts(17_000_000, 0) - .saturating_add(Weight::from_parts(0, 15189)) + .saturating_add(Weight::from_parts(0, 23189)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } From c5a30eba3b9c53ea4e704b134ed5a1a0247dfbf2 Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Wed, 8 Jan 2025 12:57:26 +1300 Subject: [PATCH 10/20] fix mock in fee-control tests --- Cargo.lock | 1 + pallet/common/src/test_utils.rs | 23 +++++++++++++++++++++++ pallet/fee-control/Cargo.toml | 1 + pallet/fee-control/src/mock.rs | 2 ++ pallet/sylo/src/benchmarking.rs | 1 + pallet/sylo/src/lib.rs | 2 +- 6 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 14b929d4b..071eae5f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5826,6 +5826,7 @@ dependencies = [ "pallet-evm", "pallet-fee-proxy", "pallet-futurepass", + "pallet-sylo", "pallet-timestamp", "pallet-transaction-payment", "parity-scale-codec", diff --git a/pallet/common/src/test_utils.rs b/pallet/common/src/test_utils.rs index ff9a67def..4b78a1d69 100644 --- a/pallet/common/src/test_utils.rs +++ b/pallet/common/src/test_utils.rs @@ -723,3 +723,26 @@ macro_rules! impl_pallet_scheduler_config { } }; } + +#[macro_export] +macro_rules! impl_pallet_sylo_config { + ($test:ident) => { + parameter_types! { + pub const MaxResolvers: u32 = 10; + pub const MaxTags: u32 = 10; + pub const MaxEntries: u32 = 100; + pub const MaxServiceEndpoints: u32 = 10; + pub const StringLimit: u32 = 100; + } + impl pallet_sylo::Config for Test { + type RuntimeCall = RuntimeCall; + type RuntimeEvent = RuntimeEvent; + type MaxResolvers = MaxResolvers; + type MaxTags = MaxTags; + type MaxEntries = MaxEntries; + type MaxServiceEndpoints = MaxServiceEndpoints; + type StringLimit = StringLimit; + type WeightInfo = (); + } + }; +} diff --git a/pallet/fee-control/Cargo.toml b/pallet/fee-control/Cargo.toml index 47c9cf9f0..7046d4d29 100644 --- a/pallet/fee-control/Cargo.toml +++ b/pallet/fee-control/Cargo.toml @@ -38,6 +38,7 @@ pallet-fee-proxy = { workspace = true, default-features = true } pallet-dex = { workspace = true } pallet-assets-ext = { workspace = true } pallet-futurepass = { workspace = true } +pallet-sylo = { workspace = true } seed-pallet-common= { workspace = true, default-features = true } [features] diff --git a/pallet/fee-control/src/mock.rs b/pallet/fee-control/src/mock.rs index 1cd08d24e..2dce1475e 100644 --- a/pallet/fee-control/src/mock.rs +++ b/pallet/fee-control/src/mock.rs @@ -39,6 +39,7 @@ construct_runtime!( Evm: pallet_evm, Timestamp: pallet_timestamp, Futurepass: pallet_futurepass, + Sylo: pallet_sylo, MockPallet: mock_pallet::pallet, FeeControl: pallet_fee_control, } @@ -54,6 +55,7 @@ impl_pallet_dex_config!(Test); impl_pallet_timestamp_config!(Test); impl_pallet_evm_config!(Test); impl_pallet_futurepass_config!(Test); +impl_pallet_sylo_config!(Test); impl_pallet_fee_control_config!(Test); impl mock_pallet::pallet::Config for Test {} diff --git a/pallet/sylo/src/benchmarking.rs b/pallet/sylo/src/benchmarking.rs index 82d3a8176..968eb8924 100644 --- a/pallet/sylo/src/benchmarking.rs +++ b/pallet/sylo/src/benchmarking.rs @@ -23,6 +23,7 @@ use alloc::string::{String, ToString}; use frame_benchmarking::{account as bench_account, benchmarks, impl_benchmark_test_suite}; use frame_support::{assert_ok, BoundedVec}; use frame_system::RawOrigin; +use sp_core::H160; const MAX_SERVICE_ENDPOINTS: u32 = 10; const STRING_LIMIT: u32 = 250; diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 45446d3a8..378cb72f2 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -24,7 +24,7 @@ use frame_support::{ }; use frame_system::pallet_prelude::*; use seed_primitives::AssetId; -use sp_core::{H160, H256}; +use sp_core::H256; use sp_std::prelude::*; use sp_std::{convert::TryInto, vec}; From cb35ca0aab997998e127adedf766593d50da47d2 Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Wed, 8 Jan 2025 13:02:00 +1300 Subject: [PATCH 11/20] add more comments to config --- pallet/sylo/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 378cb72f2..4b1444950 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -60,24 +60,32 @@ pub mod pallet { + GetDispatchInfo + From> + IsSubType>; + /// The system event type type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Interface to access weight values type WeightInfo: WeightInfo; + /// The maximim number of resolvers in a validation record. #[pallet::constant] type MaxResolvers: Get; + /// The maximum number of tags in a validation record. #[pallet::constant] type MaxTags: Get; + /// The maximum number of validation entries in a record. #[pallet::constant] type MaxEntries: Get; + /// The maximum number of service endpoints for a registered resolver. #[pallet::constant] type MaxServiceEndpoints: Get; + /// The max length of strings used within the Sylo Pallet. This limits + /// the maximum size for resolver identifiers, data identifier, service + /// endpoint strings, and tag strings. #[pallet::constant] type StringLimit: Get; } From 744a8b54a1db288fbd150717df5424af401a486b Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Wed, 8 Jan 2025 13:14:49 +1300 Subject: [PATCH 12/20] fix tests compiling --- pallet/fee-proxy/src/mock.rs | 2 ++ pallet/sylo/src/tests.rs | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pallet/fee-proxy/src/mock.rs b/pallet/fee-proxy/src/mock.rs index b3e221fed..ed651ff3c 100644 --- a/pallet/fee-proxy/src/mock.rs +++ b/pallet/fee-proxy/src/mock.rs @@ -40,6 +40,7 @@ construct_runtime!( Timestamp: pallet_timestamp, Futurepass: pallet_futurepass, FeeControl: pallet_fee_control, + Sylo: pallet_sylo, } ); @@ -53,6 +54,7 @@ impl_pallet_timestamp_config!(Test); impl_pallet_evm_config!(Test); impl_pallet_futurepass_config!(Test); impl_pallet_fee_control_config!(Test); +impl_pallet_sylo_config!(Test); // Mock ErcIdConversion for testing purposes impl ErcIdConversion for Test diff --git a/pallet/sylo/src/tests.rs b/pallet/sylo/src/tests.rs index fc6f57b8e..de1f019c0 100644 --- a/pallet/sylo/src/tests.rs +++ b/pallet/sylo/src/tests.rs @@ -205,7 +205,6 @@ mod resolvers { mod validation_records { use core::str; - use mock::SyloResolverMethod; use sp_core::hexdisplay::AsBytesRef; use super::*; @@ -298,7 +297,7 @@ mod validation_records { alice, "data_id", vec![( - str::from_utf8(SyloResolverMethod::get().as_bytes_ref()).unwrap(), + str::from_utf8(SyloResolverMethod::::get().as_bytes_ref()).unwrap(), str::from_utf8(identifier.to_vec().as_bytes_ref()).unwrap(), )], "data_type", @@ -336,7 +335,7 @@ mod validation_records { alice, "data_id", vec![( - str::from_utf8(SyloResolverMethod::get().as_bytes_ref()).unwrap(), + str::from_utf8(SyloResolverMethod::::get().as_bytes_ref()).unwrap(), // identifier references a non-existent resolver "unregistered-resolver", )], From 0abe851dd8ba62eeb5caebb4b52696b8a23badd1 Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Fri, 10 Jan 2025 11:43:31 +1300 Subject: [PATCH 13/20] implement integration tests for sylo pallet fees --- pallet/fee-proxy/src/impls.rs | 73 +++++++-- pallet/fee-proxy/src/lib.rs | 5 +- pallet/sylo/src/lib.rs | 4 + runtime/src/tests/mod.rs | 1 + runtime/src/tests/sylo_fees.rs | 290 +++++++++++++++++++++++++++++++++ 5 files changed, 353 insertions(+), 20 deletions(-) create mode 100644 runtime/src/tests/sylo_fees.rs diff --git a/pallet/fee-proxy/src/impls.rs b/pallet/fee-proxy/src/impls.rs index abfb81cb0..a9f32760b 100644 --- a/pallet/fee-proxy/src/impls.rs +++ b/pallet/fee-proxy/src/impls.rs @@ -39,6 +39,7 @@ where ::RuntimeCall: IsSubType>, ::RuntimeCall: IsSubType>, ::RuntimeCall: IsSubType>, + ::RuntimeCall: IsSubType>, ::OnChargeTransaction: OnChargeTransaction, ::ErcIdConversion: ErcIdConversion, Balance: From<<::OnChargeTransaction as OnChargeTransaction>::Balance>, @@ -48,7 +49,10 @@ where <::OnChargeTransaction as OnChargeTransaction>::LiquidityInfo; /// Intercept the withdraw fee, and swap any tokens to gas tokens if the call is - /// pallet_fee_proxy.call_with_fee_preferences() + /// pallet_fee_proxy.call_with_fee_preferences(). + /// + /// This also additionally will force the Sylo token as the gas token if the call + /// is detected as a extrinsic for the sylo pallet. fn withdraw_fee( who: &T::AccountId, call: &::RuntimeCall, @@ -68,7 +72,8 @@ where } } - let do_fee_swap = |payment_asset: AssetId, + let do_fee_swap = |who: &T::AccountId, + payment_asset: &AssetId, mut total_fee: Balance, max_payment: Balance| -> Result<(), TransactionValidityError> { @@ -80,12 +85,11 @@ where // <::OnChargeTransaction as OnChargeTransaction>::withdraw_fee let account_balance = pallet_assets_ext::Pallet::::balance(native_asset, who); // Minium balance is hardcoded to 1 - // pallet_assets_ext::Pallet::::minimum_balance(native_asset); let minimum_balance = pallet_assets_ext::Pallet::::minimum_balance(native_asset); if account_balance < minimum_balance { total_fee = total_fee.saturating_add(minimum_balance); } - let path: &[AssetId] = &[payment_asset, native_asset]; + let path: &[AssetId] = &[*payment_asset, native_asset]; pallet_dex::Pallet::::do_swap_with_exact_target( who, total_fee, @@ -94,21 +98,12 @@ where *who, None, ) - .map_err(|_| InvalidTransaction::Payment)?; + .map_err(|_| InvalidTransaction::Stale)?; Ok(()) }; - let is_sylo_call = match call.is_sub_type() { - Some(pallet_sylo::Call::register_resolver { .. }) => true, - Some(pallet_sylo::Call::update_resolver { .. }) => true, - Some(pallet_sylo::Call::unregister_resolver { .. }) => true, - Some(pallet_sylo::Call::create_validation_record { .. }) => true, - Some(pallet_sylo::Call::add_validation_record_entry { .. }) => true, - Some(pallet_sylo::Call::update_validation_record { .. }) => true, - Some(pallet_sylo::Call::delete_validation_record { .. }) => true, - _ => false, - }; + let is_sylo_call = is_sylo_call::(call); // if the call is a sylo pallet call, then we always force a fee swap with the // sylo token @@ -116,14 +111,14 @@ where let payment_asset = pallet_sylo::Pallet::::payment_asset().ok_or(InvalidTransaction::Payment)?; - do_fee_swap(payment_asset, Balance::from(fee), u128::MAX)?; + do_fee_swap(who, &payment_asset, Balance::from(fee), u128::MAX)?; } // Check whether this call has specified fee preferences if let Some(call_with_fee_preferences { payment_asset, max_payment, call }) = call.is_sub_type() { - // prevent using the fee proxy if the call is a sylo call + // prevent using the fee proxy if the inner call is a sylo call if is_sylo_call { Err(InvalidTransaction::Payment)?; } @@ -185,7 +180,7 @@ where add_evm_gas_cost(gas_limit, max_fee_per_gas, max_priority_fee_per_gas); } - do_fee_swap(*payment_asset, total_fee, *max_payment)?; + do_fee_swap(who, payment_asset, total_fee, *max_payment)?; }; <::OnChargeTransaction as OnChargeTransaction>::withdraw_fee( @@ -220,3 +215,45 @@ where ) } } + +/// Helper function to determine if a call is sylo pallet call that +/// should be paid using sylo tokens. +/// +/// Will also determine if the inner call of a futurepass or +/// fee proxy call is a sylo call as well. +fn is_sylo_call(call: &::RuntimeCall) -> bool +where + T: Config + + frame_system::Config + + pallet_futurepass::Config + + pallet_sylo::Config, + ::RuntimeCall: IsSubType>, + ::RuntimeCall: IsSubType>, + ::RuntimeCall: IsSubType>, + ::RuntimeCall: IsSubType>, + ::RuntimeCall: IsSubType>, + ::RuntimeCall: IsSubType>, +{ + if match call.is_sub_type() { + Some(pallet_sylo::Call::register_resolver { .. }) => true, + Some(pallet_sylo::Call::update_resolver { .. }) => true, + Some(pallet_sylo::Call::unregister_resolver { .. }) => true, + Some(pallet_sylo::Call::create_validation_record { .. }) => true, + Some(pallet_sylo::Call::add_validation_record_entry { .. }) => true, + Some(pallet_sylo::Call::update_validation_record { .. }) => true, + Some(pallet_sylo::Call::delete_validation_record { .. }) => true, + _ => false, + } { + return true; + } + + if let Some(pallet_futurepass::Call::proxy_extrinsic { call, .. }) = call.is_sub_type() { + return is_sylo_call::(call.as_ref().into_ref()); + } + + if let Some(call_with_fee_preferences { call, .. }) = call.is_sub_type() { + return is_sylo_call::(call.as_ref().into_ref()); + } + + false +} diff --git a/pallet/fee-proxy/src/lib.rs b/pallet/fee-proxy/src/lib.rs index e2ae3183d..4d7403711 100644 --- a/pallet/fee-proxy/src/lib.rs +++ b/pallet/fee-proxy/src/lib.rs @@ -25,7 +25,7 @@ pub use pallet::*; use frame_support::{ dispatch::{Dispatchable, GetDispatchInfo, PostDispatchInfo}, pallet_prelude::*, - traits::IsSubType, + traits::{IsSubType, IsType}, }; use frame_system::pallet_prelude::*; use seed_pallet_common::{FeeConfig, MaintenanceCheckEVM}; @@ -63,7 +63,8 @@ pub mod pallet { + Dispatchable + GetDispatchInfo + From> - + IsSubType>; + + IsSubType> + + IsType<::RuntimeCall>; /// The system event type type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The caller origin, overarching type of all pallets origins. diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 4b1444950..2ca122782 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -452,6 +452,10 @@ pub mod pallet { Ok(()) } + // pub fn is_sylo_extrinsic(call: &::RuntimeCall) -> bool { + // true + // } + pub fn payment_asset() -> Option { >::get() } diff --git a/runtime/src/tests/mod.rs b/runtime/src/tests/mod.rs index 16e9bb75f..bbd906b42 100644 --- a/runtime/src/tests/mod.rs +++ b/runtime/src/tests/mod.rs @@ -22,6 +22,7 @@ mod evm_tests; mod maintenance_mode; mod multiplier; mod staker_payouts; +mod sylo_fees; use frame_support::traits::{ fungibles::Inspect as _, diff --git a/runtime/src/tests/sylo_fees.rs b/runtime/src/tests/sylo_fees.rs new file mode 100644 index 000000000..c83a2d34e --- /dev/null +++ b/runtime/src/tests/sylo_fees.rs @@ -0,0 +1,290 @@ +// Copyright 2022-2023 Futureverse Corporation Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// You may obtain a copy of the License at the root of this project source code + +//! Integration tests for the sylo pallet. Ensures sylo extrinsics are correctly +//! charged with the set Sylo payment token. +#![cfg(test)] + +use super::{TxBuilder, BASE_TX_GAS_COST, MAX_PRIORITY_FEE_PER_GAS, MINIMUM_XRP_TX_COST}; +use crate::{ + constants::ONE_XRP, + impls::scale_wei_to_6dp, + tests::{alice, bob, charlie, ExtBuilder}, + Assets, AssetsExt, Dex, Ethereum, FeeControl, FeeProxy, Futurepass, Runtime, RuntimeOrigin, + Sylo, System, TxFeePot, XrpCurrency, EVM, +}; +use ethabi::Token; + +use frame_support::{ + assert_err, assert_noop, assert_ok, + dispatch::{GetDispatchInfo, RawOrigin}, + pallet_prelude::{InvalidTransaction, TransactionValidityError}, + traits::{ + fungible::Inspect, + fungibles::Inspect as Inspects, + tokens::{Fortitude, Preservation}, + }, +}; +use frame_system::RawOrigin::Root; +use hex_literal::hex; +use seed_pallet_common::test_prelude::create_account; + +use crate::{constants::XRP_ASSET_ID, impls::scale_6dp_to_wei}; +use pallet_transaction_payment::ChargeTransactionPayment; +use precompile_utils::{constants::ERC20_PRECOMPILE_ADDRESS_PREFIX, ErcIdConversion}; +use seed_client::chain_spec::get_account_id_from_seed; +use seed_primitives::{AccountId, AssetId, Balance}; +use sp_core::{ecdsa, H160, H256, U256}; +use sp_runtime::{traits::SignedExtension, BoundedVec, DispatchError::BadOrigin}; + +#[test] +fn sylo_extrinsic_works_with_sylo_token() { + ExtBuilder::default().build().execute_with(|| { + let new_account = create_account(2); + + let payment_asset = setup_sylo_liquidity(new_account.clone()); + + let caller_token_balance_before = AssetsExt::balance(payment_asset, &new_account); + + let calls = create_sylo_calls(); + + for call in calls.iter() { + let caller_token_balance_before = AssetsExt::balance(payment_asset, &new_account); + + let dispatch_info = call.get_dispatch_info(); + + assert_ok!( as SignedExtension>::pre_dispatch( + ChargeTransactionPayment::from(0), + &new_account, + &call, + &dispatch_info, + 1, + )); + + let caller_token_balance_after = AssetsExt::balance(payment_asset, &new_account); + + // validate caller had their sylo token balance reduced + assert!(caller_token_balance_before > caller_token_balance_after); + } + }); +} + +#[test] +fn sylo_extrinsic_works_with_futurepass_payment() { + ExtBuilder::default().build().execute_with(|| { + assert_ok!(Futurepass::create(RuntimeOrigin::signed(alice()), alice())); + let futurepass = pallet_futurepass::Holders::::get(alice()).unwrap(); + + let payment_asset = setup_sylo_liquidity(futurepass.clone()); + + let calls = create_sylo_calls(); + + for call in calls.iter() { + let caller_xrp_balance_before = XrpCurrency::balance(&alice()); + let caller_token_balance_before = AssetsExt::balance(payment_asset, &alice()); + let futurepass_token_balance_before = AssetsExt::balance(payment_asset, &futurepass); + + let fp_proxy_call = + crate::RuntimeCall::Futurepass(pallet_futurepass::Call::proxy_extrinsic { + futurepass, + call: Box::new(call.clone()), + }); + + let dispatch_info = fp_proxy_call.get_dispatch_info(); + + assert_ok!( as SignedExtension>::pre_dispatch( + ChargeTransactionPayment::from(0), + &alice(), + &fp_proxy_call, + &dispatch_info, + 1, + )); + + let caller_xrp_balance_after = XrpCurrency::balance(&alice()); + let caller_token_balance_after = AssetsExt::balance(payment_asset, &alice()); + let futurepass_token_balance_after = AssetsExt::balance(payment_asset, &futurepass); + + // validate futurepass should only have paid in tokens + assert_eq!(caller_xrp_balance_before, caller_xrp_balance_after); + assert_eq!(caller_token_balance_before, caller_token_balance_after); + + assert!(futurepass_token_balance_before > futurepass_token_balance_after); + } + }); +} + +#[test] +fn sylo_extrinsic_fails_without_sylo_funds() { + ExtBuilder::default().build().execute_with(|| { + // Test executing that calls without setting up the + // liquidity prior + let calls = create_sylo_calls(); + + for call in calls.iter() { + let dispatch_info = call.get_dispatch_info(); + + assert_err!( + as SignedExtension>::pre_dispatch( + ChargeTransactionPayment::from(0), + &alice(), + &call, + &dispatch_info, + 1, + ), + TransactionValidityError::Invalid(InvalidTransaction::Payment) + ); + } + }); +} + +#[test] +fn sylo_extrinsic_fails_without_fee_proxy() { + ExtBuilder::default().build().execute_with(|| { + let calls = create_sylo_calls(); + + for call in calls.iter() { + let dispatch_info = call.get_dispatch_info(); + + assert_err!( + as SignedExtension>::pre_dispatch( + ChargeTransactionPayment::from(0), + &alice(), + &call, + &dispatch_info, + 1, + ), + TransactionValidityError::Invalid(InvalidTransaction::Payment) + ); + } + }); +} + +#[test] +fn sylo_extrinsic_fails_using_call_with_fee_preferences() { + ExtBuilder::default().build().execute_with(|| { + let new_account = create_account(2); + + let payment_asset = setup_sylo_liquidity(new_account.clone()); + + let caller_token_balance_before = AssetsExt::balance(payment_asset, &new_account); + + let calls = create_sylo_calls(); + + for call in calls.iter() { + let max_payment: Balance = 10_000_000_000_000_000; + let fee_proxy_call = + crate::RuntimeCall::FeeProxy(pallet_fee_proxy::Call::call_with_fee_preferences { + payment_asset, + max_payment, + call: Box::new(call.clone()), + }); + + let dispatch_info = fee_proxy_call.get_dispatch_info(); + assert_err!( + as SignedExtension>::pre_dispatch( + ChargeTransactionPayment::from(0), + &new_account, + &fee_proxy_call, + &dispatch_info, + 1, + ), + TransactionValidityError::Invalid(InvalidTransaction::Payment) + ); + } + }); +} + +fn setup_sylo_liquidity(new_account: AccountId) -> u32 { + let payment_asset = AssetsExt::next_asset_uuid().unwrap(); + + assert_ok!(AssetsExt::create_asset( + RawOrigin::Signed(alice()).into(), + b"Test".to_vec(), + b"Test".to_vec(), + 6, + None, + None + )); + + assert_eq!(AssetsExt::balance(payment_asset, &bob()), 0); + + // Mint these assets into Alice and new_account + assert_ok!(Assets::mint( + RawOrigin::Signed(alice()).into(), + payment_asset, + alice(), + 10_000_000_000_000_000 + )); + assert_ok!(Assets::mint( + RawOrigin::Signed(alice()).into(), + payment_asset, + new_account, + 10_000_000_000_000_000 + )); + + // Add liquidity to the dex + assert_ok!(Dex::add_liquidity( + RawOrigin::Signed(alice()).into(), + XRP_ASSET_ID, + payment_asset, + 1_000_000_000_000, + 1_000_000_000_000, + 1, + 1, + None, + None, + )); + + assert_ok!(Sylo::set_payment_asset(RawOrigin::Root.into(), payment_asset)); + + payment_asset +} + +/// Creates a list of calls for all sylo extrinsics which should be charged in Sylo Tokens +fn create_sylo_calls() -> Vec<::RuntimeCall> { + vec![ + crate::RuntimeCall::Sylo(pallet_sylo::Call::register_resolver { + identifier: BoundedVec::new(), + service_endpoints: BoundedVec::new(), + }), + crate::RuntimeCall::Sylo(pallet_sylo::Call::update_resolver { + identifier: BoundedVec::new(), + service_endpoints: BoundedVec::new(), + }), + crate::RuntimeCall::Sylo(pallet_sylo::Call::unregister_resolver { + identifier: BoundedVec::new(), + }), + crate::RuntimeCall::Sylo(pallet_sylo::Call::create_validation_record { + data_id: BoundedVec::new(), + resolvers: BoundedVec::new(), + data_type: BoundedVec::new(), + tags: BoundedVec::new(), + checksum: H256::from_low_u64_be(123), + }), + crate::RuntimeCall::Sylo(pallet_sylo::Call::add_validation_record_entry { + data_id: BoundedVec::new(), + checksum: H256::from_low_u64_be(123), + }), + crate::RuntimeCall::Sylo(pallet_sylo::Call::update_validation_record { + data_id: BoundedVec::new(), + resolvers: None, + data_type: None, + tags: None, + }), + crate::RuntimeCall::Sylo(pallet_sylo::Call::delete_validation_record { + data_id: BoundedVec::new(), + }), + ] +} From 5b6abc1906b36e0c40c61e4aa049fe1ba8071c3a Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Fri, 10 Jan 2025 12:22:56 +1300 Subject: [PATCH 14/20] resize constants for sylo config --- runtime/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index f032d9e7a..da8f5ae69 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -597,11 +597,11 @@ impl pallet_preimage::Config for Runtime { } parameter_types! { - pub const MaxResolvers: u32 = 10; - pub const MaxTags: u32 = 10; - pub const MaxEntries: u32 = 100; - pub const MaxServiceEndpoints: u32 = 10; - pub const SyloStringLimit: u32 = 500; + pub const MaxResolvers: u8 = 10; + pub const MaxTags: u8 = 10; + pub const MaxEntries: u8 = 100; + pub const MaxServiceEndpoints: u8 = 10; + pub const SyloStringLimit: u16 = 500; } impl pallet_sylo::Config for Runtime { From 8c8ac52b1fd81712a2a643e3068779670fd7cdb6 Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Fri, 10 Jan 2025 13:31:59 +1300 Subject: [PATCH 15/20] use EnsureOrigin and emit event on state change --- pallet/common/src/test_utils.rs | 3 ++- pallet/fee-proxy/Cargo.toml | 1 + pallet/sylo/src/lib.rs | 21 ++++++++++++++------- pallet/sylo/src/mock.rs | 1 + 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pallet/common/src/test_utils.rs b/pallet/common/src/test_utils.rs index 4b78a1d69..a6ef10580 100644 --- a/pallet/common/src/test_utils.rs +++ b/pallet/common/src/test_utils.rs @@ -732,11 +732,12 @@ macro_rules! impl_pallet_sylo_config { pub const MaxTags: u32 = 10; pub const MaxEntries: u32 = 100; pub const MaxServiceEndpoints: u32 = 10; - pub const StringLimit: u32 = 100; + pub const StringLimit: u32 = 500; } impl pallet_sylo::Config for Test { type RuntimeCall = RuntimeCall; type RuntimeEvent = RuntimeEvent; + type ApproveOrigin = EnsureRoot; type MaxResolvers = MaxResolvers; type MaxTags = MaxTags; type MaxEntries = MaxEntries; diff --git a/pallet/fee-proxy/Cargo.toml b/pallet/fee-proxy/Cargo.toml index ef966e31c..4d1d6b15f 100644 --- a/pallet/fee-proxy/Cargo.toml +++ b/pallet/fee-proxy/Cargo.toml @@ -46,6 +46,7 @@ std = [ "pallet-assets-ext/std", "pallet-futurepass/std", "pallet-evm/std", + "pallet-sylo/std", "pallet-transaction-payment/std", "scale-info/std", "seed-primitives/std", diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 2ca122782..768e4ade5 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -64,6 +64,9 @@ pub mod pallet { /// The system event type type RuntimeEvent: From> + IsType<::RuntimeEvent>; + /// Allowed origins to set payment asset and reversed sylo method + type ApproveOrigin: EnsureOrigin; + /// Interface to access weight values type WeightInfo: WeightInfo; @@ -149,6 +152,12 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(crate) fn deposit_event)] pub enum Event { + PaymentAssetSet { + asset_id: AssetId, + }, + SyloResolverMethodSet { + method: Vec, + }, ResolverRegistered { id: Vec, controller: T::AccountId, @@ -191,8 +200,9 @@ pub mod pallet { T::WeightInfo::set_payment_asset() })] pub fn set_payment_asset(origin: OriginFor, payment_asset: AssetId) -> DispatchResult { - ensure_root(origin)?; + T::ApproveOrigin::ensure_origin(origin)?; >::put(payment_asset); + Self::deposit_event(Event::PaymentAssetSet { asset_id: payment_asset }); Ok(()) } @@ -204,8 +214,9 @@ pub mod pallet { origin: OriginFor, resolver_method: BoundedVec, ) -> DispatchResult { - ensure_root(origin)?; - >::put(resolver_method); + T::ApproveOrigin::ensure_origin(origin)?; + >::put(&resolver_method); + Self::deposit_event(Event::SyloResolverMethodSet { method: resolver_method.to_vec() }); Ok(()) } @@ -452,10 +463,6 @@ pub mod pallet { Ok(()) } - // pub fn is_sylo_extrinsic(call: &::RuntimeCall) -> bool { - // true - // } - pub fn payment_asset() -> Option { >::get() } diff --git a/pallet/sylo/src/mock.rs b/pallet/sylo/src/mock.rs index 8646649a3..bb383f2a8 100644 --- a/pallet/sylo/src/mock.rs +++ b/pallet/sylo/src/mock.rs @@ -42,6 +42,7 @@ parameter_types! { impl Config for Test { type RuntimeCall = RuntimeCall; type RuntimeEvent = RuntimeEvent; + type ApproveOrigin = EnsureRoot; type MaxResolvers = MaxResolvers; type MaxTags = MaxTags; type MaxEntries = MaxEntries; From d09e6be26f95f300d3a89c03daf517cd597d65c1 Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Fri, 10 Jan 2025 14:00:20 +1300 Subject: [PATCH 16/20] avoid unnecessary clones in sylo pallet --- pallet/sylo/src/lib.rs | 121 +++++++++++++++++++++-------------------- runtime/src/lib.rs | 1 + 2 files changed, 64 insertions(+), 58 deletions(-) diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 768e4ade5..bc04605ff 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -232,14 +232,14 @@ pub mod pallet { let who = ensure_signed(origin)?; ensure!( - >::get(identifier.clone()).is_none(), + >::get(&identifier).is_none(), Error::::ResolverAlreadyRegistered ); let resolver = Resolver { controller: who.clone(), service_endpoints: service_endpoints.clone() }; - >::insert(identifier.clone(), resolver); + >::insert(&identifier, resolver); Self::deposit_event(Event::ResolverRegistered { id: identifier.to_vec(), @@ -261,20 +261,21 @@ pub mod pallet { ) -> DispatchResult { let who = ensure_signed(origin)?; - let mut resolver = - >::get(identifier.clone()).ok_or(Error::::ResolverNotRegistered)?; + >::try_mutate(&identifier, |resolver| -> DispatchResult { + let resolver = resolver.as_mut().ok_or(Error::::ResolverNotRegistered)?; - ensure!(who == resolver.controller, Error::::NotController); + ensure!(who == resolver.controller, Error::::NotController); - resolver.service_endpoints = service_endpoints.clone(); + resolver.service_endpoints = service_endpoints.clone(); - >::insert(identifier.clone(), resolver); + Self::deposit_event(Event::ResolverUpdated { + id: identifier.to_vec(), + controller: who, + service_endpoints, + }); - Self::deposit_event(Event::ResolverUpdated { - id: identifier.to_vec(), - controller: who, - service_endpoints, - }); + Ok(()) + })?; Ok(()) } @@ -290,11 +291,11 @@ pub mod pallet { let who = ensure_signed(origin)?; let resolver = - >::get(identifier.clone()).ok_or(Error::::ResolverNotRegistered)?; + >::get(&identifier).ok_or(Error::::ResolverNotRegistered)?; ensure!(who == resolver.controller, Error::::NotController); - >::remove(identifier.clone()); + >::remove(&identifier); Self::deposit_event(Event::ResolverUnregistered { id: identifier.to_vec() }); @@ -316,11 +317,11 @@ pub mod pallet { let who = ensure_signed(origin)?; ensure!( - >::get(who.clone(), data_id.clone()).is_none(), + >::get(&who, &data_id).is_none(), Error::::RecordAlreadyCreated ); - Self::validate_sylo_resolvers(resolvers.clone())?; + Self::validate_sylo_resolvers(&resolvers)?; let current_block = >::block_number(); @@ -335,7 +336,7 @@ pub mod pallet { }]), }; - >::insert(who.clone(), data_id.clone(), record); + >::insert(&who, &data_id, record); Self::deposit_event(Event::ValidationRecordCreated { author: who, @@ -356,21 +357,22 @@ pub mod pallet { ) -> DispatchResult { let who = ensure_signed(origin)?; - let mut record = >::get(who.clone(), data_id.clone()) - .ok_or(Error::::RecordNotCreated)?; + >::try_mutate(&who, &data_id, |record| -> DispatchResult { + let record = record.as_mut().ok_or(Error::::RecordNotCreated)?; - record.entries.force_push(ValidationEntry { - checksum: checksum.clone(), - block: >::block_number(), - }); + record.entries.force_push(ValidationEntry { + checksum, + block: >::block_number(), + }); - >::insert(who.clone(), data_id.clone(), record); + Self::deposit_event(Event::ValidationEntryAdded { + author: who.clone(), + id: data_id.to_vec(), + checksum, + }); - Self::deposit_event(Event::ValidationEntryAdded { - author: who, - id: data_id.to_vec(), - checksum, - }); + Ok(()) + })?; Ok(()) } @@ -388,32 +390,33 @@ pub mod pallet { ) -> DispatchResult { let who = ensure_signed(origin)?; - let mut record = >::get(who.clone(), data_id.clone()) - .ok_or(Error::::RecordNotCreated)?; + >::try_mutate(&who, &data_id, |record| -> DispatchResult { + let record = record.as_mut().ok_or(Error::::RecordNotCreated)?; - if let Some(resolvers) = resolvers.clone() { - Self::validate_sylo_resolvers(resolvers.clone())?; - record.resolvers = resolvers; - } + if let Some(ref new_resolvers) = resolvers { + Self::validate_sylo_resolvers(new_resolvers)?; + record.resolvers = new_resolvers.clone(); + } - if let Some(data_type) = data_type.clone() { - record.data_type = data_type; - } + if let Some(ref new_data_type) = data_type { + record.data_type = new_data_type.clone(); + } - if let Some(tags) = tags.clone() { - record.tags = tags; - } + if let Some(ref new_tags) = tags { + record.tags = new_tags.clone(); + } - >::insert(who.clone(), data_id.clone(), record); + Self::deposit_event(Event::ValidationRecordUpdated { + author: who.clone(), + id: data_id.to_vec(), + resolvers: resolvers + .map(|r| r.iter().map(|resolver| resolver.to_did()).collect()), + data_type: data_type.map(|dt| dt.to_vec()), + tags: tags.map(|t| t.iter().map(|tag| tag.to_vec()).collect()), + }); - Self::deposit_event(Event::ValidationRecordUpdated { - author: who, - id: data_id.to_vec(), - resolvers: resolvers - .map(|resolvers| resolvers.iter().map(|resolver| resolver.to_did()).collect()), - data_type: data_type.map(|data_type| data_type.to_vec()), - tags: tags.map(|tags| tags.iter().map(|tag| tag.to_vec()).collect()), - }); + Ok(()) + })?; Ok(()) } @@ -429,11 +432,11 @@ pub mod pallet { let who = ensure_signed(origin)?; ensure!( - >::get(who.clone(), data_id.clone()).is_some(), + >::contains_key(&who, &data_id), Error::::RecordNotCreated ); - >::remove(who.clone(), data_id.clone()); + >::remove(&who, &data_id); Self::deposit_event(Event::ValidationRecordDeleted { author: who, @@ -446,19 +449,21 @@ pub mod pallet { impl Pallet { pub fn validate_sylo_resolvers( - resolvers: BoundedVec, T::MaxResolvers>, + resolvers: &BoundedVec, T::MaxResolvers>, ) -> DispatchResult { let reserved_method = >::get(); // Ensure any sylo data resolvers are already registered - for resolver in resolvers { - if resolver.method == reserved_method { + resolvers + .iter() + .filter(|resolver| resolver.method == reserved_method) + .try_for_each(|resolver| -> DispatchResult { ensure!( - >::get(resolver.identifier).is_some(), + >::contains_key(&resolver.identifier), Error::::ResolverNotRegistered ); - } - } + Ok(()) + })?; Ok(()) } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index da8f5ae69..a699af8f8 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -607,6 +607,7 @@ parameter_types! { impl pallet_sylo::Config for Runtime { type RuntimeCall = RuntimeCall; type RuntimeEvent = RuntimeEvent; + type ApproveOrigin = EnsureRoot; type MaxResolvers = MaxResolvers; type MaxTags = MaxTags; type MaxEntries = MaxEntries; From 42246f25712157498084b063e90131f0498992af Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Mon, 13 Jan 2025 11:47:35 +1300 Subject: [PATCH 17/20] ensure strings have upper bound in benchmark and add verification --- pallet/fee-proxy/src/impls.rs | 5 +- pallet/sylo/src/benchmarking.rs | 146 ++++++++++++++++++++++------- pallet/sylo/src/lib.rs | 8 +- pallet/sylo/src/weights.rs | 118 +++++++++++++---------- runtime/src/weights/pallet_sylo.rs | 80 ++++++++-------- 5 files changed, 230 insertions(+), 127 deletions(-) diff --git a/pallet/fee-proxy/src/impls.rs b/pallet/fee-proxy/src/impls.rs index a9f32760b..2feecb3f8 100644 --- a/pallet/fee-proxy/src/impls.rs +++ b/pallet/fee-proxy/src/impls.rs @@ -108,8 +108,11 @@ where // if the call is a sylo pallet call, then we always force a fee swap with the // sylo token if is_sylo_call { + // let payment_asset = + // pallet_sylo::Pallet::::payment_asset().ok_or(InvalidTransaction::Payment)?; + let payment_asset = - pallet_sylo::Pallet::::payment_asset().ok_or(InvalidTransaction::Payment)?; + pallet_sylo::SyloAssetId::::get().ok_or(InvalidTransaction::Payment)?; do_fee_swap(who, &payment_asset, Balance::from(fee), u128::MAX)?; } diff --git a/pallet/sylo/src/benchmarking.rs b/pallet/sylo/src/benchmarking.rs index 968eb8924..ef4187577 100644 --- a/pallet/sylo/src/benchmarking.rs +++ b/pallet/sylo/src/benchmarking.rs @@ -46,6 +46,14 @@ pub fn bounded_string(name: &str) -> BoundedVec::St BoundedVec::truncate_from(name.as_bytes().to_vec()) } +pub fn max_bounded_string(bound: u32) -> BoundedVec::StringLimit> { + let mut max_string = BoundedVec::new(); + for _ in 1..bound { + max_string.force_push(b'a'); + } + max_string +} + pub fn setup_resolver( caller: T::AccountId, identifier: BoundedVec::StringLimit>, @@ -97,18 +105,21 @@ benchmarks! { let alice = account::("Alice"); - let mut identifier = BoundedVec::new(); - for _ in 1..p { - identifier.force_push(b'a'); - } + let identifier = max_bounded_string::(p); let mut service_endpoints = BoundedVec::new(); for _ in 1..q { - service_endpoints.force_push(bounded_string::("https://service-endpoint.one.two.three")); + service_endpoints.force_push(max_bounded_string::(p)); } - }: _(origin::(&alice), identifier, service_endpoints) + }: _(origin::(&alice), identifier.clone(), service_endpoints.clone()) + verify { + assert_eq!(Resolvers::::get(identifier), Some(Resolver { + controller: alice, service_endpoints + })); + } update_resolver { + let p in 1 .. STRING_LIMIT; let q in 1 .. MAX_SERVICE_ENDPOINTS; let alice = account::("Alice"); @@ -117,87 +128,158 @@ benchmarks! { let mut service_endpoints = BoundedVec::new(); for _ in 1..q { - service_endpoints.force_push(bounded_string::("https://service-endpoint.one.two.three")); + service_endpoints.force_push(max_bounded_string::(p)); } - }: _(origin::(&alice), identifier, service_endpoints) + }: _(origin::(&alice), identifier.clone(), service_endpoints.clone()) + verify { + assert_eq!(Resolvers::::get(identifier), Some(Resolver { + controller: alice, service_endpoints + })); + } unregister_resolver { let alice = account::("Alice"); let identifier = setup_resolver::(alice.clone(), bounded_string::("sylo-data-resolver")); - }: _(origin::(&alice), identifier) + }: _(origin::(&alice), identifier.clone()) + verify { + assert_eq!(Resolvers::::get(identifier), None); + } create_validation_record { - let p in 1 .. MAX_RESOLVERS; - let q in 1 .. MAX_TAGS; + let p in 1 .. STRING_LIMIT; + let q in 1 .. MAX_RESOLVERS; + let r in 1 .. MAX_TAGS; let alice = account::("Alice"); let data_id = bounded_string::("data-id"); let mut resolvers = BoundedVec::new(); - for i in 1 .. p { + for i in 1 .. q { + // create a maximum sized resolver id that is unique to each + // resolver let mut resolver_id = String::from("sylo-resolver"); resolver_id.push_str(i.to_string().as_str()); - - let resolver_id = setup_resolver::(alice.clone(), bounded_string::(resolver_id.as_str())); + let mut resolver_id = bounded_string::(resolver_id.as_str()); + let id_len = >::try_into(resolver_id.len()).unwrap(); + if id_len < p { + let max_affix = max_bounded_string::(p - id_len); + resolver_id.try_append(&mut max_affix.to_vec()).unwrap(); + }; + let resolver_id = setup_resolver::(alice.clone(), resolver_id); resolvers.force_push(ResolverId { - method: bounded_string::("sylo_resolver"), + method: max_bounded_string::(p), identifier: resolver_id, }); } - let data_type = bounded_string::("data-type"); + let data_type = max_bounded_string::(p); let mut tags = BoundedVec::new(); - for _ in 1 .. q { - tags.force_push(bounded_string::("tag")); + for _ in 1 .. r { + tags.force_push(max_bounded_string::(p)); } let checksum = H256::from_low_u64_be(123); - }: _(origin::(&alice), data_id, resolvers, data_type, tags, checksum) + + let block: BlockNumberFor = 1_u32.into(); + }: _(origin::(&alice), data_id.clone(), resolvers.clone(), data_type.clone(), tags.clone(), checksum.clone()) + verify { + assert_eq!(ValidationRecords::::get(&alice, &data_id), Some(ValidationRecord { + author: alice, + resolvers: resolvers, + data_type: data_type, + tags: tags, + entries: BoundedVec::truncate_from(vec![ValidationEntry { + checksum, + block, + }]), + })); + } add_validation_record_entry { let alice = account::("Alice"); let data_id = setup_validation_record::(alice.clone()); - let checksum = H256::from_low_u64_be(125); - }: _(origin::(&alice), data_id, checksum) + let checksum = H256::from_low_u64_be(123); + }: _(origin::(&alice), data_id.clone(), checksum.clone()) + verify { + assert_eq!(ValidationRecords::::get(&alice, &data_id), Some(ValidationRecord { + author: alice, + resolvers: BoundedVec::new(), + data_type: bounded_string::("data-type"), + tags: BoundedVec::new(), + entries: BoundedVec::truncate_from(vec![ValidationEntry { + checksum, + block: 0_u32.into(), + }, ValidationEntry { + checksum, + block: 1_u32.into(), + }]), + })); + } update_validation_record { - let p in 1 .. MAX_RESOLVERS; - let q in 1 .. MAX_TAGS; + let p in 1 .. STRING_LIMIT; + let q in 1 .. MAX_RESOLVERS; + let r in 1 .. MAX_TAGS; let alice = account::("Alice"); let data_id = setup_validation_record::(alice.clone()); let mut resolvers = BoundedVec::new(); - for i in 1 .. p { + for i in 1 .. q { + // create a maximum sized resolver id that is unique to each + // resolver let mut resolver_id = String::from("sylo-resolver"); resolver_id.push_str(i.to_string().as_str()); - - let resolver_id = setup_resolver::(alice.clone(), bounded_string::(resolver_id.as_str())); + let mut resolver_id = bounded_string::(resolver_id.as_str()); + let id_len = >::try_into(resolver_id.len()).unwrap(); + if id_len < p { + let max_affix = max_bounded_string::(p - id_len); + resolver_id.try_append(&mut max_affix.to_vec()).unwrap(); + }; + + let resolver_id = setup_resolver::(alice.clone(), resolver_id); resolvers.force_push(ResolverId { - method: bounded_string::("sylo_resolver"), + method: max_bounded_string::(p), identifier: resolver_id, }); } - let data_type = bounded_string::("data-type"); + let data_type = max_bounded_string::(p); let mut tags = BoundedVec::new(); - for _ in 1 .. q { - tags.force_push(bounded_string::("tag")); + for _ in 1 .. r { + tags.force_push(max_bounded_string::(p)); } - }: _(origin::(&alice), data_id, Some(resolvers), Some(data_type), Some(tags)) + + let block: BlockNumberFor = 1_u32.into(); + }: _(origin::(&alice), data_id.clone(), Some(resolvers.clone()), Some(data_type.clone()), Some(tags.clone())) + verify { + assert_eq!(ValidationRecords::::get(&alice, &data_id), Some(ValidationRecord { + author: alice, + resolvers: resolvers, + data_type: data_type, + tags: tags, + entries: BoundedVec::truncate_from(vec![ValidationEntry { + checksum: H256::from_low_u64_be(123), + block, + }]), + })); + } delete_validation_record { let alice = account::("Alice"); let data_id = setup_validation_record::(alice.clone()); - }: _(origin::(&alice), data_id) + }: _(origin::(&alice), data_id.clone()) + verify { + assert_eq!(ValidationRecords::::get(&alice, &data_id), None); + } } impl_benchmark_test_suite!( diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index bc04605ff..69dd1b3c3 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -252,7 +252,7 @@ pub mod pallet { #[pallet::call_index(3)] #[pallet::weight({ - T::WeightInfo::update_resolver(::get()) + T::WeightInfo::update_resolver(::get(), ::get()) })] pub fn update_resolver( origin: OriginFor, @@ -304,7 +304,7 @@ pub mod pallet { #[pallet::call_index(5)] #[pallet::weight({ - T::WeightInfo::create_validation_record(::get(), ::get()) + T::WeightInfo::create_validation_record(::get(), ::get(), ::get()) })] pub fn create_validation_record( origin: OriginFor, @@ -467,9 +467,5 @@ pub mod pallet { Ok(()) } - - pub fn payment_asset() -> Option { - >::get() - } } } diff --git a/pallet/sylo/src/weights.rs b/pallet/sylo/src/weights.rs index 46aab780a..518945e87 100644 --- a/pallet/sylo/src/weights.rs +++ b/pallet/sylo/src/weights.rs @@ -18,7 +18,7 @@ //! Autogenerated weights for pallet_sylo //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2025-01-07, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2025-01-10, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` //! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 @@ -50,9 +50,9 @@ pub trait WeightInfo { fn set_payment_asset() -> Weight; fn set_sylo_resolver_method() -> Weight; fn register_resolver(p: u32, q: u32, ) -> Weight; - fn update_resolver(q: u32, ) -> Weight; + fn update_resolver(p: u32, q: u32, ) -> Weight; fn unregister_resolver() -> Weight; - fn create_validation_record(p: u32, q: u32, ) -> Weight; + fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight; fn add_validation_record_entry() -> Weight; fn update_validation_record(p: u32, q: u32, ) -> Weight; fn delete_validation_record() -> Weight; @@ -64,33 +64,38 @@ impl WeightInfo for SubstrateWeight { // Storage: `Sylo::SyloAssetId` (r:0 w:1) // Proof: `Sylo::SyloAssetId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) fn set_payment_asset() -> Weight { - Weight::from_all(4_000_000) + Weight::from_all(9_000_000) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::SyloResolverMethod` (r:0 w:1) // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) fn set_sylo_resolver_method() -> Weight { - Weight::from_all(4_000_000) + Weight::from_all(9_000_000) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn register_resolver(_p: u32, q: u32, ) -> Weight { - Weight::from_all(14_351_359) - // Standard Error: 7_325 - .saturating_add(Weight::from_all(296_417_u64).saturating_mul(q as u64)) + fn register_resolver(p: u32, q: u32, ) -> Weight { + Weight::from_all(10_338_294) + // Standard Error: 353 + .saturating_add(Weight::from_all(11_572_u64).saturating_mul(p as u64)) + // Standard Error: 9_228 + .saturating_add(Weight::from_all(601_293_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn update_resolver(q: u32, ) -> Weight { - Weight::from_all(15_992_647) - // Standard Error: 6_497 - .saturating_add(Weight::from_all(366_404_u64).saturating_mul(q as u64)) + fn update_resolver(p: u32, q: u32, ) -> Weight { + Weight::from_all(11_284_277) + // Standard Error: 391 + .saturating_add(Weight::from_all(14_845_u64).saturating_mul(p as u64)) + // Standard Error: 10_219 + .saturating_add(Weight::from_all(647_949_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -105,21 +110,24 @@ impl WeightInfo for SubstrateWeight { // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) // Storage: `Sylo::SyloResolverMethod` (r:1 w:0) // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) - /// The range of component `p` is `[1, 10]`. + /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn create_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(16_124_714) - // Standard Error: 9_176 - .saturating_add(Weight::from_all(765_759_u64).saturating_mul(p as u64)) - // Standard Error: 9_176 - .saturating_add(Weight::from_all(231_083_u64).saturating_mul(q as u64)) + /// The range of component `r` is `[1, 10]`. + fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight { + Weight::from_all(13_483_135) + // Standard Error: 363 + .saturating_add(Weight::from_all(12_239_u64).saturating_mul(p as u64)) + // Standard Error: 9_469 + .saturating_add(Weight::from_all(655_819_u64).saturating_mul(q as u64)) + // Standard Error: 9_469 + .saturating_add(Weight::from_all(317_430_u64).saturating_mul(r as u64)) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { - Weight::from_all(19_000_000) + Weight::from_all(17_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -130,18 +138,18 @@ impl WeightInfo for SubstrateWeight { /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn update_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(18_475_818) - // Standard Error: 8_599 - .saturating_add(Weight::from_all(1_784_700_u64).saturating_mul(p as u64)) - // Standard Error: 8_599 - .saturating_add(Weight::from_all(364_058_u64).saturating_mul(q as u64)) + Weight::from_all(17_282_251) + // Standard Error: 10_844 + .saturating_add(Weight::from_all(1_554_210_u64).saturating_mul(p as u64)) + // Standard Error: 10_844 + .saturating_add(Weight::from_all(402_480_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { - Weight::from_all(17_000_000) + Weight::from_all(15_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -152,33 +160,38 @@ impl WeightInfo for () { // Storage: `Sylo::SyloAssetId` (r:0 w:1) // Proof: `Sylo::SyloAssetId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) fn set_payment_asset() -> Weight { - Weight::from_all(4_000_000) + Weight::from_all(9_000_000) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::SyloResolverMethod` (r:0 w:1) // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) fn set_sylo_resolver_method() -> Weight { - Weight::from_all(4_000_000) + Weight::from_all(9_000_000) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn register_resolver(_p: u32, q: u32, ) -> Weight { - Weight::from_all(14_351_359) - // Standard Error: 7_325 - .saturating_add(Weight::from_all(296_417_u64).saturating_mul(q as u64)) + fn register_resolver(p: u32, q: u32, ) -> Weight { + Weight::from_all(10_338_294) + // Standard Error: 353 + .saturating_add(Weight::from_all(11_572_u64).saturating_mul(p as u64)) + // Standard Error: 9_228 + .saturating_add(Weight::from_all(601_293_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn update_resolver(q: u32, ) -> Weight { - Weight::from_all(15_992_647) - // Standard Error: 6_497 - .saturating_add(Weight::from_all(366_404_u64).saturating_mul(q as u64)) + fn update_resolver(p: u32, q: u32, ) -> Weight { + Weight::from_all(11_284_277) + // Standard Error: 391 + .saturating_add(Weight::from_all(14_845_u64).saturating_mul(p as u64)) + // Standard Error: 10_219 + .saturating_add(Weight::from_all(647_949_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -193,21 +206,24 @@ impl WeightInfo for () { // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) // Storage: `Sylo::SyloResolverMethod` (r:1 w:0) // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) - /// The range of component `p` is `[1, 10]`. + /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn create_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(16_124_714) - // Standard Error: 9_176 - .saturating_add(Weight::from_all(765_759_u64).saturating_mul(p as u64)) - // Standard Error: 9_176 - .saturating_add(Weight::from_all(231_083_u64).saturating_mul(q as u64)) + /// The range of component `r` is `[1, 10]`. + fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight { + Weight::from_all(13_483_135) + // Standard Error: 363 + .saturating_add(Weight::from_all(12_239_u64).saturating_mul(p as u64)) + // Standard Error: 9_469 + .saturating_add(Weight::from_all(655_819_u64).saturating_mul(q as u64)) + // Standard Error: 9_469 + .saturating_add(Weight::from_all(317_430_u64).saturating_mul(r as u64)) .saturating_add(RocksDbWeight::get().reads(2)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { - Weight::from_all(19_000_000) + Weight::from_all(17_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -218,18 +234,18 @@ impl WeightInfo for () { /// The range of component `p` is `[1, 10]`. /// The range of component `q` is `[1, 10]`. fn update_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(18_475_818) - // Standard Error: 8_599 - .saturating_add(Weight::from_all(1_784_700_u64).saturating_mul(p as u64)) - // Standard Error: 8_599 - .saturating_add(Weight::from_all(364_058_u64).saturating_mul(q as u64)) + Weight::from_all(17_282_251) + // Standard Error: 10_844 + .saturating_add(Weight::from_all(1_554_210_u64).saturating_mul(p as u64)) + // Standard Error: 10_844 + .saturating_add(Weight::from_all(402_480_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(2)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { - Weight::from_all(17_000_000) + Weight::from_all(15_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } diff --git a/runtime/src/weights/pallet_sylo.rs b/runtime/src/weights/pallet_sylo.rs index e76a0e8eb..2242197ba 100644 --- a/runtime/src/weights/pallet_sylo.rs +++ b/runtime/src/weights/pallet_sylo.rs @@ -2,7 +2,7 @@ //! Autogenerated weights for `pallet_sylo` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2025-01-07, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2025-01-10, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` //! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024 @@ -38,8 +38,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` - // Minimum execution time: 3_000_000 picoseconds. - Weight::from_parts(4_000_000, 0) + // Minimum execution time: 8_000_000 picoseconds. + Weight::from_parts(8_000_000, 0) .saturating_add(Weight::from_parts(0, 0)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -49,8 +49,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` - // Minimum execution time: 3_000_000 picoseconds. - Weight::from_parts(4_000_000, 0) + // Minimum execution time: 9_000_000 picoseconds. + Weight::from_parts(10_000_000, 0) .saturating_add(Weight::from_parts(0, 0)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -62,28 +62,31 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `175` // Estimated: `9016` - // Minimum execution time: 13_000_000 picoseconds. - Weight::from_parts(12_953_454, 0) + // Minimum execution time: 12_000_000 picoseconds. + Weight::from_parts(9_810_772, 0) .saturating_add(Weight::from_parts(0, 9016)) - // Standard Error: 216 - .saturating_add(Weight::from_parts(2_490, 0).saturating_mul(p.into())) - // Standard Error: 5_652 - .saturating_add(Weight::from_parts(301_265, 0).saturating_mul(q.into())) + // Standard Error: 396 + .saturating_add(Weight::from_parts(16_181, 0).saturating_mul(p.into())) + // Standard Error: 10_331 + .saturating_add(Weight::from_parts(580_835, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } /// Storage: `Sylo::Resolvers` (r:1 w:1) /// Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) + /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn update_resolver(q: u32, ) -> Weight { + fn update_resolver(p: u32, q: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `289` // Estimated: `9016` - // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(15_831_188, 0) + // Minimum execution time: 14_000_000 picoseconds. + Weight::from_parts(14_432_618, 0) .saturating_add(Weight::from_parts(0, 9016)) - // Standard Error: 5_846 - .saturating_add(Weight::from_parts(350_759, 0).saturating_mul(q.into())) + // Standard Error: 612 + .saturating_add(Weight::from_parts(4_979, 0).saturating_mul(p.into())) + // Standard Error: 15_971 + .saturating_add(Weight::from_parts(547_485, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -93,7 +96,7 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `289` // Estimated: `9016` - // Minimum execution time: 15_000_000 picoseconds. + // Minimum execution time: 14_000_000 picoseconds. Weight::from_parts(15_000_000, 0) .saturating_add(Weight::from_parts(0, 9016)) .saturating_add(T::DbWeight::get().reads(1)) @@ -103,19 +106,22 @@ impl pallet_sylo::WeightInfo for WeightInfo { /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) /// Storage: `Sylo::SyloResolverMethod` (r:1 w:0) /// Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) - /// The range of component `p` is `[1, 10]`. + /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn create_validation_record(p: u32, q: u32, ) -> Weight { + /// The range of component `r` is `[1, 10]`. + fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `217 + p * (16 ±0)` + // Measured: `234 + q * (16 ±0)` // Estimated: `23189` - // Minimum execution time: 17_000_000 picoseconds. - Weight::from_parts(17_280_488, 0) + // Minimum execution time: 18_000_000 picoseconds. + Weight::from_parts(16_031_373, 0) .saturating_add(Weight::from_parts(0, 23189)) - // Standard Error: 9_099 - .saturating_add(Weight::from_parts(673_932, 0).saturating_mul(p.into())) - // Standard Error: 9_099 - .saturating_add(Weight::from_parts(133_592, 0).saturating_mul(q.into())) + // Standard Error: 395 + .saturating_add(Weight::from_parts(8_002, 0).saturating_mul(p.into())) + // Standard Error: 10_281 + .saturating_add(Weight::from_parts(535_434, 0).saturating_mul(q.into())) + // Standard Error: 10_281 + .saturating_add(Weight::from_parts(221_085, 0).saturating_mul(r.into())) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -125,8 +131,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `317` // Estimated: `23189` - // Minimum execution time: 18_000_000 picoseconds. - Weight::from_parts(19_000_000, 0) + // Minimum execution time: 16_000_000 picoseconds. + Weight::from_parts(17_000_000, 0) .saturating_add(Weight::from_parts(0, 23189)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) @@ -141,13 +147,13 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `355 + p * (16 ±0)` // Estimated: `23189` - // Minimum execution time: 22_000_000 picoseconds. - Weight::from_parts(18_370_121, 0) + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(16_231_580, 0) .saturating_add(Weight::from_parts(0, 23189)) - // Standard Error: 6_644 - .saturating_add(Weight::from_parts(1_759_119, 0).saturating_mul(p.into())) - // Standard Error: 6_644 - .saturating_add(Weight::from_parts(374_286, 0).saturating_mul(q.into())) + // Standard Error: 9_032 + .saturating_add(Weight::from_parts(1_648_218, 0).saturating_mul(p.into())) + // Standard Error: 9_032 + .saturating_add(Weight::from_parts(415_619, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -155,10 +161,10 @@ impl pallet_sylo::WeightInfo for WeightInfo { /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { // Proof Size summary in bytes: - // Measured: `317` + // Measured: `276` // Estimated: `23189` - // Minimum execution time: 17_000_000 picoseconds. - Weight::from_parts(17_000_000, 0) + // Minimum execution time: 15_000_000 picoseconds. + Weight::from_parts(15_000_000, 0) .saturating_add(Weight::from_parts(0, 23189)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) From 0fbda1ffdb1990a160f0735a40a200fd4cb3c4cf Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Mon, 13 Jan 2025 11:58:45 +1300 Subject: [PATCH 18/20] refactor tests --- pallet/sylo/src/mock.rs | 22 +-- pallet/sylo/src/tests.rs | 326 +++++++++++++++++++++------------------ 2 files changed, 184 insertions(+), 164 deletions(-) diff --git a/pallet/sylo/src/mock.rs b/pallet/sylo/src/mock.rs index bb383f2a8..565a5822e 100644 --- a/pallet/sylo/src/mock.rs +++ b/pallet/sylo/src/mock.rs @@ -51,16 +51,16 @@ impl Config for Test { type WeightInfo = (); } -#[derive(Default)] -pub struct TestExt; +// #[derive(Default)] +// pub struct TestExt; -impl TestExt { - pub fn build(self) -> sp_io::TestExternalities { - let storage = frame_system::GenesisConfig::::default().build_storage().unwrap(); +// impl TestExt { +// pub fn build(self) -> sp_io::TestExternalities { +// let storage = frame_system::GenesisConfig::::default().build_storage().unwrap(); - let mut ext: sp_io::TestExternalities = storage.into(); - ext.execute_with(|| System::initialize(&1, &[0u8; 32].into(), &Default::default())); - ext.execute_with(|| pallet_assets_ext::GenesisConfig::::default().build()); - ext - } -} +// let mut ext: sp_io::TestExternalities = storage.into(); +// ext.execute_with(|| System::initialize(&1, &[0u8; 32].into(), &Default::default())); +// ext.execute_with(|| pallet_assets_ext::GenesisConfig::::default().build()); +// ext +// } +// } diff --git a/pallet/sylo/src/tests.rs b/pallet/sylo/src/tests.rs index de1f019c0..bdd2fffe7 100644 --- a/pallet/sylo/src/tests.rs +++ b/pallet/sylo/src/tests.rs @@ -14,15 +14,98 @@ // You may obtain a copy of the License at the root of this project source code use super::*; -use mock::{RuntimeEvent as MockEvent, Sylo, System, Test, TestExt}; +use mock::{RuntimeEvent as MockEvent, Sylo, System, Test}; use seed_pallet_common::test_prelude::*; -mod resolvers { +fn create_and_register_resolver( + identifier: BoundedVec::StringLimit>, + service_endpoints: Vec::StringLimit>>, +) -> ( + AccountId, + BoundedVec::StringLimit>, + BoundedVec< + BoundedVec::StringLimit>, + ::MaxServiceEndpoints, + >, +) { + let controller: AccountId = create_account(1); + + let service_endpoints = + BoundedVec::<_, ::MaxServiceEndpoints>::try_from(service_endpoints) + .unwrap(); + + assert_ok!(Sylo::register_resolver( + RawOrigin::Signed(controller.clone()).into(), + identifier.clone(), + service_endpoints.clone(), + )); + + (controller, identifier, service_endpoints) +} + +fn create_initial_validation_record( + author: ::AccountId, + data_id: &str, + resolvers: Vec<(&str, &str)>, + data_type: &str, + tags: Vec<&str>, +) -> ( + BoundedVec, + BoundedVec, mock::MaxResolvers>, + BoundedVec, + BoundedVec, mock::MaxTags>, + H256, + ValidationRecord< + ::AccountId, + BlockNumberFor, + mock::MaxResolvers, + mock::MaxTags, + mock::MaxEntries, + mock::StringLimit, + >, +) { + let data_id = bounded_string(data_id); + let resolvers = BoundedVec::truncate_from( + resolvers + .iter() + .map(|(method, identifier)| create_resolver_id(method, identifier)) + .collect(), + ); + let data_type = bounded_string(data_type); + let tags = BoundedVec::truncate_from(tags.iter().map(|tag| bounded_string(tag)).collect()); + let checksum = H256::from_low_u64_be(123); + + let record = ValidationRecord { + author, + resolvers: resolvers.clone(), + data_type: data_type.clone(), + tags: tags.clone(), + entries: BoundedVec::truncate_from(vec![ValidationEntry { + checksum, + block: System::block_number(), + }]), + }; + + return (data_id, resolvers, data_type, tags, checksum, record); +} + +fn create_resolver_id(method: &str, identifier: &str) -> ResolverId<::StringLimit> { + ResolverId { + method: BoundedVec::truncate_from(method.as_bytes().to_vec()), + identifier: BoundedVec::truncate_from(identifier.as_bytes().to_vec()), + } +} + +fn bounded_string(str: &str) -> BoundedVec::StringLimit> { + BoundedVec::truncate_from(str.as_bytes().to_vec()) +} + +mod resolver_registration { use super::*; #[test] fn resolver_registration_works() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let (controller, identifier, service_endpoints) = create_and_register_resolver( bounded_string("test-resolver"), vec![ @@ -44,9 +127,35 @@ mod resolvers { }); } + #[test] + fn resolver_register_existing_fails() { + TestExt::::default().build().execute_with(|| { + let (controller, identifier, service_endpoints) = create_and_register_resolver( + bounded_string("test-resolver"), + vec![ + bounded_string("https://endpoint.one"), + bounded_string("https://endpoint.two"), + ], + ); + + assert_noop!( + Sylo::register_resolver( + RawOrigin::Signed(controller).into(), + identifier, + service_endpoints, + ), + Error::::ResolverAlreadyRegistered, + ); + }); + } +} + +mod resolver_update { + use super::*; + #[test] fn resolver_update_works() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let (controller, identifier, mut service_endpoints) = create_and_register_resolver( bounded_string("test-resolver"), vec![ @@ -76,55 +185,9 @@ mod resolvers { }); } - #[test] - fn resolver_unregistration_works() { - TestExt.build().execute_with(|| { - let (controller, identifier, _) = create_and_register_resolver( - bounded_string("test-resolver"), - vec![ - bounded_string("https://endpoint.one"), - bounded_string("https://endpoint.two"), - ], - ); - - assert_ok!(Sylo::unregister_resolver( - RawOrigin::Signed(controller.clone()).into(), - identifier.clone(), - )); - - System::assert_last_event(MockEvent::Sylo(Event::::ResolverUnregistered { - id: identifier.to_vec(), - })); - - assert!(Resolvers::::get(identifier).is_none()); - }); - } - - #[test] - fn resolver_register_existing_fails() { - TestExt.build().execute_with(|| { - let (controller, identifier, service_endpoints) = create_and_register_resolver( - bounded_string("test-resolver"), - vec![ - bounded_string("https://endpoint.one"), - bounded_string("https://endpoint.two"), - ], - ); - - assert_noop!( - Sylo::register_resolver( - RawOrigin::Signed(controller).into(), - identifier, - service_endpoints, - ), - Error::::ResolverAlreadyRegistered, - ); - }); - } - #[test] fn resolver_update_not_existing_fails() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let controller: AccountId = create_account(1); let identifier = bounded_string("test-resolver"); @@ -145,7 +208,7 @@ mod resolvers { #[test] fn resolver_update_not_controller_fails() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let (_, identifier, service_endpoints) = create_and_register_resolver( bounded_string("test-resolver"), vec![ @@ -166,10 +229,38 @@ mod resolvers { ); }); } +} + +mod resolver_unregistration { + use super::*; + + #[test] + fn resolver_unregistration_works() { + TestExt::::default().build().execute_with(|| { + let (controller, identifier, _) = create_and_register_resolver( + bounded_string("test-resolver"), + vec![ + bounded_string("https://endpoint.one"), + bounded_string("https://endpoint.two"), + ], + ); + + assert_ok!(Sylo::unregister_resolver( + RawOrigin::Signed(controller.clone()).into(), + identifier.clone(), + )); + + System::assert_last_event(MockEvent::Sylo(Event::::ResolverUnregistered { + id: identifier.to_vec(), + })); + + assert!(Resolvers::::get(identifier).is_none()); + }); + } #[test] fn resolver_unregister_not_existing_fails() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let controller: AccountId = create_account(1); let identifier = bounded_string("test-resolver"); @@ -183,7 +274,7 @@ mod resolvers { #[test] fn resolver_unregister_not_controller_fails() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let (_, identifier, _) = create_and_register_resolver( bounded_string("test-resolver"), vec![ @@ -202,7 +293,7 @@ mod resolvers { } } -mod validation_records { +mod create_validation_record { use core::str; use sp_core::hexdisplay::AsBytesRef; @@ -211,7 +302,7 @@ mod validation_records { #[test] fn create_validation_records_works() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, checksum, record) = @@ -246,7 +337,7 @@ mod validation_records { #[test] fn create_existing_validation_record_fails() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, checksum, _) = @@ -283,7 +374,7 @@ mod validation_records { #[test] fn create_validation_records_with_sylo_resolvers_works() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { // Ensure sylo resolver is registered let (_, identifier, _) = create_and_register_resolver( bounded_string("test-resolver"), @@ -327,7 +418,7 @@ mod validation_records { #[test] fn create_validation_record_with_unregistered_sylo_resolver_fails() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, checksum, _) = @@ -359,7 +450,7 @@ mod validation_records { #[test] fn create_multiple_validation_records_with_same_author_works() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); for i in 1..5 { @@ -398,7 +489,7 @@ mod validation_records { #[test] fn create_validation_records_with_different_author_works() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { for i in 2..5 { let author: AccountId = create_account(i); @@ -435,10 +526,14 @@ mod validation_records { } }); } +} + +mod add_validation_record_entry { + use super::*; #[test] fn add_validation_entry_works() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, checksum, _) = @@ -485,7 +580,7 @@ mod validation_records { #[test] fn add_not_existing_validation_entry_fails() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, _, _, _, checksum, _) = @@ -504,7 +599,7 @@ mod validation_records { #[test] fn only_author_can_add_validation_entry() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, checksum, _) = @@ -537,10 +632,14 @@ mod validation_records { ); }); } +} + +mod update_validation_record { + use super::*; #[test] fn update_validation_record_works() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, checksum, record) = @@ -663,7 +762,7 @@ mod validation_records { #[test] fn update_not_existing_validation_record_fails() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, _, _) = create_initial_validation_record( @@ -689,7 +788,7 @@ mod validation_records { #[test] fn only_author_can_update_validation_record() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, checksum, _) = @@ -724,10 +823,14 @@ mod validation_records { ); }); } +} + +mod delete_validation_record { + use super::*; #[test] fn delete_validation_record_works() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, checksum, _) = @@ -757,7 +860,7 @@ mod validation_records { #[test] fn delete_not_existing_validation_record_fails() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, _, _) = create_initial_validation_record( @@ -783,7 +886,7 @@ mod validation_records { #[test] fn only_author_can_delete_validation_record() { - TestExt.build().execute_with(|| { + TestExt::::default().build().execute_with(|| { let alice: AccountId = create_account(2); let (data_id, resolvers, data_type, tags, checksum, _) = @@ -816,86 +919,3 @@ mod validation_records { }); } } - -fn create_and_register_resolver( - identifier: BoundedVec::StringLimit>, - service_endpoints: Vec::StringLimit>>, -) -> ( - AccountId, - BoundedVec::StringLimit>, - BoundedVec< - BoundedVec::StringLimit>, - ::MaxServiceEndpoints, - >, -) { - let controller: AccountId = create_account(1); - - let service_endpoints = - BoundedVec::<_, ::MaxServiceEndpoints>::try_from(service_endpoints) - .unwrap(); - - assert_ok!(Sylo::register_resolver( - RawOrigin::Signed(controller.clone()).into(), - identifier.clone(), - service_endpoints.clone(), - )); - - (controller, identifier, service_endpoints) -} - -fn create_initial_validation_record( - author: ::AccountId, - data_id: &str, - resolvers: Vec<(&str, &str)>, - data_type: &str, - tags: Vec<&str>, -) -> ( - BoundedVec, - BoundedVec, mock::MaxResolvers>, - BoundedVec, - BoundedVec, mock::MaxTags>, - H256, - ValidationRecord< - ::AccountId, - BlockNumberFor, - mock::MaxResolvers, - mock::MaxTags, - mock::MaxEntries, - mock::StringLimit, - >, -) { - let data_id = bounded_string(data_id); - let resolvers = BoundedVec::truncate_from( - resolvers - .iter() - .map(|(method, identifier)| create_resolver_id(method, identifier)) - .collect(), - ); - let data_type = bounded_string(data_type); - let tags = BoundedVec::truncate_from(tags.iter().map(|tag| bounded_string(tag)).collect()); - let checksum = H256::from_low_u64_be(123); - - let record = ValidationRecord { - author, - resolvers: resolvers.clone(), - data_type: data_type.clone(), - tags: tags.clone(), - entries: BoundedVec::truncate_from(vec![ValidationEntry { - checksum, - block: System::block_number(), - }]), - }; - - return (data_id, resolvers, data_type, tags, checksum, record); -} - -fn create_resolver_id(method: &str, identifier: &str) -> ResolverId<::StringLimit> { - ResolverId { - method: BoundedVec::truncate_from(method.as_bytes().to_vec()), - identifier: BoundedVec::truncate_from(identifier.as_bytes().to_vec()), - } -} - -fn bounded_string(str: &str) -> BoundedVec::StringLimit> { - BoundedVec::truncate_from(str.as_bytes().to_vec()) -} From 36b152243fcf41a1a2726e724b5023c04ef2850e Mon Sep 17 00:00:00 2001 From: John Carlo San Pedro Date: Mon, 13 Jan 2025 12:59:07 +1300 Subject: [PATCH 19/20] fix benchmark verification --- pallet/sylo/src/benchmarking.rs | 31 +++----- pallet/sylo/src/lib.rs | 2 +- pallet/sylo/src/weights.rs | 118 +++++++++++++++-------------- runtime/src/weights/pallet_sylo.rs | 81 ++++++++++---------- 4 files changed, 116 insertions(+), 116 deletions(-) diff --git a/pallet/sylo/src/benchmarking.rs b/pallet/sylo/src/benchmarking.rs index ef4187577..af852fd8b 100644 --- a/pallet/sylo/src/benchmarking.rs +++ b/pallet/sylo/src/benchmarking.rs @@ -162,11 +162,10 @@ benchmarks! { let mut resolver_id = String::from("sylo-resolver"); resolver_id.push_str(i.to_string().as_str()); let mut resolver_id = bounded_string::(resolver_id.as_str()); - let id_len = >::try_into(resolver_id.len()).unwrap(); - if id_len < p { - let max_affix = max_bounded_string::(p - id_len); - resolver_id.try_append(&mut max_affix.to_vec()).unwrap(); - }; + for _ in 1..p { + resolver_id.force_push(b'a'); + } + let resolver_id = setup_resolver::(alice.clone(), resolver_id); resolvers.force_push(ResolverId { method: max_bounded_string::(p), @@ -237,11 +236,9 @@ benchmarks! { let mut resolver_id = String::from("sylo-resolver"); resolver_id.push_str(i.to_string().as_str()); let mut resolver_id = bounded_string::(resolver_id.as_str()); - let id_len = >::try_into(resolver_id.len()).unwrap(); - if id_len < p { - let max_affix = max_bounded_string::(p - id_len); - resolver_id.try_append(&mut max_affix.to_vec()).unwrap(); - }; + for _ in 1..p { + resolver_id.force_push(b'a'); + } let resolver_id = setup_resolver::(alice.clone(), resolver_id); resolvers.force_push(ResolverId { @@ -260,16 +257,10 @@ benchmarks! { let block: BlockNumberFor = 1_u32.into(); }: _(origin::(&alice), data_id.clone(), Some(resolvers.clone()), Some(data_type.clone()), Some(tags.clone())) verify { - assert_eq!(ValidationRecords::::get(&alice, &data_id), Some(ValidationRecord { - author: alice, - resolvers: resolvers, - data_type: data_type, - tags: tags, - entries: BoundedVec::truncate_from(vec![ValidationEntry { - checksum: H256::from_low_u64_be(123), - block, - }]), - })); + let validation_record = ValidationRecords::::get(&alice, &data_id).unwrap(); + assert_eq!(validation_record.resolvers, resolvers); + assert_eq!(validation_record.data_type, data_type); + assert_eq!(validation_record.tags, tags); } delete_validation_record { diff --git a/pallet/sylo/src/lib.rs b/pallet/sylo/src/lib.rs index 69dd1b3c3..2eb9a11a7 100644 --- a/pallet/sylo/src/lib.rs +++ b/pallet/sylo/src/lib.rs @@ -379,7 +379,7 @@ pub mod pallet { #[pallet::call_index(7)] #[pallet::weight({ - T::WeightInfo::update_validation_record(::get(), ::get()) + T::WeightInfo::update_validation_record(::get(), ::get(), ::get()) })] pub fn update_validation_record( origin: OriginFor, diff --git a/pallet/sylo/src/weights.rs b/pallet/sylo/src/weights.rs index 518945e87..fff4f2020 100644 --- a/pallet/sylo/src/weights.rs +++ b/pallet/sylo/src/weights.rs @@ -18,7 +18,7 @@ //! Autogenerated weights for pallet_sylo //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2025-01-10, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2025-01-12, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` //! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 @@ -54,7 +54,7 @@ pub trait WeightInfo { fn unregister_resolver() -> Weight; fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight; fn add_validation_record_entry() -> Weight; - fn update_validation_record(p: u32, q: u32, ) -> Weight; + fn update_validation_record(p: u32, q: u32, r: u32, ) -> Weight; fn delete_validation_record() -> Weight; } @@ -78,11 +78,11 @@ impl WeightInfo for SubstrateWeight { /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn register_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(10_338_294) - // Standard Error: 353 - .saturating_add(Weight::from_all(11_572_u64).saturating_mul(p as u64)) - // Standard Error: 9_228 - .saturating_add(Weight::from_all(601_293_u64).saturating_mul(q as u64)) + Weight::from_all(10_591_051) + // Standard Error: 854 + .saturating_add(Weight::from_all(12_225_u64).saturating_mul(p as u64)) + // Standard Error: 22_283 + .saturating_add(Weight::from_all(687_699_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -91,18 +91,18 @@ impl WeightInfo for SubstrateWeight { /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn update_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(11_284_277) - // Standard Error: 391 - .saturating_add(Weight::from_all(14_845_u64).saturating_mul(p as u64)) - // Standard Error: 10_219 - .saturating_add(Weight::from_all(647_949_u64).saturating_mul(q as u64)) + Weight::from_all(12_779_040) + // Standard Error: 950 + .saturating_add(Weight::from_all(10_880_u64).saturating_mul(p as u64)) + // Standard Error: 24_784 + .saturating_add(Weight::from_all(677_602_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) fn unregister_resolver() -> Weight { - Weight::from_all(15_000_000) + Weight::from_all(16_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -114,20 +114,20 @@ impl WeightInfo for SubstrateWeight { /// The range of component `q` is `[1, 10]`. /// The range of component `r` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight { - Weight::from_all(13_483_135) - // Standard Error: 363 - .saturating_add(Weight::from_all(12_239_u64).saturating_mul(p as u64)) - // Standard Error: 9_469 - .saturating_add(Weight::from_all(655_819_u64).saturating_mul(q as u64)) - // Standard Error: 9_469 - .saturating_add(Weight::from_all(317_430_u64).saturating_mul(r as u64)) + Weight::from_all(13_259_460) + // Standard Error: 325 + .saturating_add(Weight::from_all(18_751_u64).saturating_mul(p as u64)) + // Standard Error: 8_458 + .saturating_add(Weight::from_all(815_905_u64).saturating_mul(q as u64)) + // Standard Error: 8_458 + .saturating_add(Weight::from_all(356_496_u64).saturating_mul(r as u64)) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { - Weight::from_all(17_000_000) + Weight::from_all(18_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -135,21 +135,24 @@ impl WeightInfo for SubstrateWeight { // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) // Storage: `Sylo::SyloResolverMethod` (r:1 w:0) // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) - /// The range of component `p` is `[1, 10]`. + /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn update_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(17_282_251) - // Standard Error: 10_844 - .saturating_add(Weight::from_all(1_554_210_u64).saturating_mul(p as u64)) - // Standard Error: 10_844 - .saturating_add(Weight::from_all(402_480_u64).saturating_mul(q as u64)) + /// The range of component `r` is `[1, 10]`. + fn update_validation_record(p: u32, q: u32, r: u32, ) -> Weight { + Weight::from_all(4_155_879) + // Standard Error: 408 + .saturating_add(Weight::from_all(53_149_u64).saturating_mul(p as u64)) + // Standard Error: 10_621 + .saturating_add(Weight::from_all(2_603_264_u64).saturating_mul(q as u64)) + // Standard Error: 10_621 + .saturating_add(Weight::from_all(727_556_u64).saturating_mul(r as u64)) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { - Weight::from_all(15_000_000) + Weight::from_all(16_000_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -174,11 +177,11 @@ impl WeightInfo for () { /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn register_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(10_338_294) - // Standard Error: 353 - .saturating_add(Weight::from_all(11_572_u64).saturating_mul(p as u64)) - // Standard Error: 9_228 - .saturating_add(Weight::from_all(601_293_u64).saturating_mul(q as u64)) + Weight::from_all(10_591_051) + // Standard Error: 854 + .saturating_add(Weight::from_all(12_225_u64).saturating_mul(p as u64)) + // Standard Error: 22_283 + .saturating_add(Weight::from_all(687_699_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -187,18 +190,18 @@ impl WeightInfo for () { /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn update_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(11_284_277) - // Standard Error: 391 - .saturating_add(Weight::from_all(14_845_u64).saturating_mul(p as u64)) - // Standard Error: 10_219 - .saturating_add(Weight::from_all(647_949_u64).saturating_mul(q as u64)) + Weight::from_all(12_779_040) + // Standard Error: 950 + .saturating_add(Weight::from_all(10_880_u64).saturating_mul(p as u64)) + // Standard Error: 24_784 + .saturating_add(Weight::from_all(677_602_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) fn unregister_resolver() -> Weight { - Weight::from_all(15_000_000) + Weight::from_all(16_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -210,20 +213,20 @@ impl WeightInfo for () { /// The range of component `q` is `[1, 10]`. /// The range of component `r` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight { - Weight::from_all(13_483_135) - // Standard Error: 363 - .saturating_add(Weight::from_all(12_239_u64).saturating_mul(p as u64)) - // Standard Error: 9_469 - .saturating_add(Weight::from_all(655_819_u64).saturating_mul(q as u64)) - // Standard Error: 9_469 - .saturating_add(Weight::from_all(317_430_u64).saturating_mul(r as u64)) + Weight::from_all(13_259_460) + // Standard Error: 325 + .saturating_add(Weight::from_all(18_751_u64).saturating_mul(p as u64)) + // Standard Error: 8_458 + .saturating_add(Weight::from_all(815_905_u64).saturating_mul(q as u64)) + // Standard Error: 8_458 + .saturating_add(Weight::from_all(356_496_u64).saturating_mul(r as u64)) .saturating_add(RocksDbWeight::get().reads(2)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { - Weight::from_all(17_000_000) + Weight::from_all(18_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -231,21 +234,24 @@ impl WeightInfo for () { // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) // Storage: `Sylo::SyloResolverMethod` (r:1 w:0) // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) - /// The range of component `p` is `[1, 10]`. + /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn update_validation_record(p: u32, q: u32, ) -> Weight { - Weight::from_all(17_282_251) - // Standard Error: 10_844 - .saturating_add(Weight::from_all(1_554_210_u64).saturating_mul(p as u64)) - // Standard Error: 10_844 - .saturating_add(Weight::from_all(402_480_u64).saturating_mul(q as u64)) + /// The range of component `r` is `[1, 10]`. + fn update_validation_record(p: u32, q: u32, r: u32, ) -> Weight { + Weight::from_all(4_155_879) + // Standard Error: 408 + .saturating_add(Weight::from_all(53_149_u64).saturating_mul(p as u64)) + // Standard Error: 10_621 + .saturating_add(Weight::from_all(2_603_264_u64).saturating_mul(q as u64)) + // Standard Error: 10_621 + .saturating_add(Weight::from_all(727_556_u64).saturating_mul(r as u64)) .saturating_add(RocksDbWeight::get().reads(2)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { - Weight::from_all(15_000_000) + Weight::from_all(16_000_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } diff --git a/runtime/src/weights/pallet_sylo.rs b/runtime/src/weights/pallet_sylo.rs index 2242197ba..f29255727 100644 --- a/runtime/src/weights/pallet_sylo.rs +++ b/runtime/src/weights/pallet_sylo.rs @@ -2,7 +2,7 @@ //! Autogenerated weights for `pallet_sylo` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2025-01-10, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2025-01-12, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` //! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024 @@ -38,7 +38,7 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` - // Minimum execution time: 8_000_000 picoseconds. + // Minimum execution time: 7_000_000 picoseconds. Weight::from_parts(8_000_000, 0) .saturating_add(Weight::from_parts(0, 0)) .saturating_add(T::DbWeight::get().writes(1)) @@ -49,8 +49,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` - // Minimum execution time: 9_000_000 picoseconds. - Weight::from_parts(10_000_000, 0) + // Minimum execution time: 8_000_000 picoseconds. + Weight::from_parts(9_000_000, 0) .saturating_add(Weight::from_parts(0, 0)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -62,13 +62,13 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `175` // Estimated: `9016` - // Minimum execution time: 12_000_000 picoseconds. - Weight::from_parts(9_810_772, 0) + // Minimum execution time: 13_000_000 picoseconds. + Weight::from_parts(11_986_830, 0) .saturating_add(Weight::from_parts(0, 9016)) - // Standard Error: 396 - .saturating_add(Weight::from_parts(16_181, 0).saturating_mul(p.into())) - // Standard Error: 10_331 - .saturating_add(Weight::from_parts(580_835, 0).saturating_mul(q.into())) + // Standard Error: 982 + .saturating_add(Weight::from_parts(7_471, 0).saturating_mul(p.into())) + // Standard Error: 25_620 + .saturating_add(Weight::from_parts(599_095, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -81,12 +81,12 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Measured: `289` // Estimated: `9016` // Minimum execution time: 14_000_000 picoseconds. - Weight::from_parts(14_432_618, 0) + Weight::from_parts(11_418_934, 0) .saturating_add(Weight::from_parts(0, 9016)) - // Standard Error: 612 - .saturating_add(Weight::from_parts(4_979, 0).saturating_mul(p.into())) - // Standard Error: 15_971 - .saturating_add(Weight::from_parts(547_485, 0).saturating_mul(q.into())) + // Standard Error: 332 + .saturating_add(Weight::from_parts(14_894, 0).saturating_mul(p.into())) + // Standard Error: 8_683 + .saturating_add(Weight::from_parts(720_676, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -96,8 +96,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `289` // Estimated: `9016` - // Minimum execution time: 14_000_000 picoseconds. - Weight::from_parts(15_000_000, 0) + // Minimum execution time: 16_000_000 picoseconds. + Weight::from_parts(17_000_000, 0) .saturating_add(Weight::from_parts(0, 9016)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) @@ -111,17 +111,17 @@ impl pallet_sylo::WeightInfo for WeightInfo { /// The range of component `r` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `234 + q * (16 ±0)` + // Measured: `149 + q * (28 ±0)` // Estimated: `23189` - // Minimum execution time: 18_000_000 picoseconds. - Weight::from_parts(16_031_373, 0) + // Minimum execution time: 19_000_000 picoseconds. + Weight::from_parts(14_380_692, 0) .saturating_add(Weight::from_parts(0, 23189)) - // Standard Error: 395 - .saturating_add(Weight::from_parts(8_002, 0).saturating_mul(p.into())) - // Standard Error: 10_281 - .saturating_add(Weight::from_parts(535_434, 0).saturating_mul(q.into())) - // Standard Error: 10_281 - .saturating_add(Weight::from_parts(221_085, 0).saturating_mul(r.into())) + // Standard Error: 506 + .saturating_add(Weight::from_parts(15_542, 0).saturating_mul(p.into())) + // Standard Error: 13_186 + .saturating_add(Weight::from_parts(789_626, 0).saturating_mul(q.into())) + // Standard Error: 13_186 + .saturating_add(Weight::from_parts(331_288, 0).saturating_mul(r.into())) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -131,8 +131,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `317` // Estimated: `23189` - // Minimum execution time: 16_000_000 picoseconds. - Weight::from_parts(17_000_000, 0) + // Minimum execution time: 17_000_000 picoseconds. + Weight::from_parts(18_000_000, 0) .saturating_add(Weight::from_parts(0, 23189)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) @@ -141,19 +141,22 @@ impl pallet_sylo::WeightInfo for WeightInfo { /// Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) /// Storage: `Sylo::SyloResolverMethod` (r:1 w:0) /// Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) - /// The range of component `p` is `[1, 10]`. + /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. - fn update_validation_record(p: u32, q: u32, ) -> Weight { + /// The range of component `r` is `[1, 10]`. + fn update_validation_record(p: u32, q: u32, r: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `355 + p * (16 ±0)` + // Measured: `287 + q * (28 ±0)` // Estimated: `23189` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(16_231_580, 0) + // Minimum execution time: 24_000_000 picoseconds. + Weight::from_parts(2_741_499, 0) .saturating_add(Weight::from_parts(0, 23189)) - // Standard Error: 9_032 - .saturating_add(Weight::from_parts(1_648_218, 0).saturating_mul(p.into())) - // Standard Error: 9_032 - .saturating_add(Weight::from_parts(415_619, 0).saturating_mul(q.into())) + // Standard Error: 483 + .saturating_add(Weight::from_parts(54_905, 0).saturating_mul(p.into())) + // Standard Error: 12_579 + .saturating_add(Weight::from_parts(2_729_175, 0).saturating_mul(q.into())) + // Standard Error: 12_579 + .saturating_add(Weight::from_parts(736_475, 0).saturating_mul(r.into())) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -163,8 +166,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `276` // Estimated: `23189` - // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(15_000_000, 0) + // Minimum execution time: 16_000_000 picoseconds. + Weight::from_parts(18_000_000, 0) .saturating_add(Weight::from_parts(0, 23189)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) From 3cf92d50301661f24f415c51667207d9ee9bd89b Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 13 Jan 2025 01:42:36 +0000 Subject: [PATCH 20/20] Update benchmarks for pallet-sylo on TRN-808-sylo-pallet --- pallet/sylo/src/weights.rs | 120 ++++++++++++++--------------- runtime/src/weights/pallet_sylo.rs | 84 ++++++++++---------- 2 files changed, 102 insertions(+), 102 deletions(-) diff --git a/pallet/sylo/src/weights.rs b/pallet/sylo/src/weights.rs index fff4f2020..e09229fcd 100644 --- a/pallet/sylo/src/weights.rs +++ b/pallet/sylo/src/weights.rs @@ -18,8 +18,8 @@ //! Autogenerated weights for pallet_sylo //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2025-01-12, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` +//! DATE: 2025-01-13, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `ip-172-31-102-147`, CPU: `Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz` //! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: @@ -64,13 +64,13 @@ impl WeightInfo for SubstrateWeight { // Storage: `Sylo::SyloAssetId` (r:0 w:1) // Proof: `Sylo::SyloAssetId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) fn set_payment_asset() -> Weight { - Weight::from_all(9_000_000) + Weight::from_all(24_944_000) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::SyloResolverMethod` (r:0 w:1) // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) fn set_sylo_resolver_method() -> Weight { - Weight::from_all(9_000_000) + Weight::from_all(26_848_000) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) @@ -78,11 +78,11 @@ impl WeightInfo for SubstrateWeight { /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn register_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(10_591_051) - // Standard Error: 854 - .saturating_add(Weight::from_all(12_225_u64).saturating_mul(p as u64)) - // Standard Error: 22_283 - .saturating_add(Weight::from_all(687_699_u64).saturating_mul(q as u64)) + Weight::from_all(26_910_243) + // Standard Error: 611 + .saturating_add(Weight::from_all(49_232_u64).saturating_mul(p as u64)) + // Standard Error: 15_951 + .saturating_add(Weight::from_all(1_856_252_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -91,18 +91,18 @@ impl WeightInfo for SubstrateWeight { /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn update_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(12_779_040) - // Standard Error: 950 - .saturating_add(Weight::from_all(10_880_u64).saturating_mul(p as u64)) - // Standard Error: 24_784 - .saturating_add(Weight::from_all(677_602_u64).saturating_mul(q as u64)) + Weight::from_all(30_782_545) + // Standard Error: 1_711 + .saturating_add(Weight::from_all(48_616_u64).saturating_mul(p as u64)) + // Standard Error: 44_625 + .saturating_add(Weight::from_all(2_033_888_u64).saturating_mul(q as u64)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) fn unregister_resolver() -> Weight { - Weight::from_all(16_000_000) + Weight::from_all(44_066_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -114,20 +114,20 @@ impl WeightInfo for SubstrateWeight { /// The range of component `q` is `[1, 10]`. /// The range of component `r` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight { - Weight::from_all(13_259_460) - // Standard Error: 325 - .saturating_add(Weight::from_all(18_751_u64).saturating_mul(p as u64)) - // Standard Error: 8_458 - .saturating_add(Weight::from_all(815_905_u64).saturating_mul(q as u64)) - // Standard Error: 8_458 - .saturating_add(Weight::from_all(356_496_u64).saturating_mul(r as u64)) + Weight::from_all(35_692_965) + // Standard Error: 756 + .saturating_add(Weight::from_all(56_270_u64).saturating_mul(p as u64)) + // Standard Error: 19_687 + .saturating_add(Weight::from_all(2_052_012_u64).saturating_mul(q as u64)) + // Standard Error: 19_687 + .saturating_add(Weight::from_all(992_694_u64).saturating_mul(r as u64)) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { - Weight::from_all(18_000_000) + Weight::from_all(49_435_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -139,20 +139,20 @@ impl WeightInfo for SubstrateWeight { /// The range of component `q` is `[1, 10]`. /// The range of component `r` is `[1, 10]`. fn update_validation_record(p: u32, q: u32, r: u32, ) -> Weight { - Weight::from_all(4_155_879) - // Standard Error: 408 - .saturating_add(Weight::from_all(53_149_u64).saturating_mul(p as u64)) - // Standard Error: 10_621 - .saturating_add(Weight::from_all(2_603_264_u64).saturating_mul(q as u64)) - // Standard Error: 10_621 - .saturating_add(Weight::from_all(727_556_u64).saturating_mul(r as u64)) + Weight::from_all(19_670_291) + // Standard Error: 1_097 + .saturating_add(Weight::from_all(127_148_u64).saturating_mul(p as u64)) + // Standard Error: 28_567 + .saturating_add(Weight::from_all(6_001_269_u64).saturating_mul(q as u64)) + // Standard Error: 28_567 + .saturating_add(Weight::from_all(1_869_067_u64).saturating_mul(r as u64)) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { - Weight::from_all(16_000_000) + Weight::from_all(45_087_000) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -163,13 +163,13 @@ impl WeightInfo for () { // Storage: `Sylo::SyloAssetId` (r:0 w:1) // Proof: `Sylo::SyloAssetId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`) fn set_payment_asset() -> Weight { - Weight::from_all(9_000_000) + Weight::from_all(24_944_000) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::SyloResolverMethod` (r:0 w:1) // Proof: `Sylo::SyloResolverMethod` (`max_values`: Some(1), `max_size`: Some(502), added: 997, mode: `MaxEncodedLen`) fn set_sylo_resolver_method() -> Weight { - Weight::from_all(9_000_000) + Weight::from_all(26_848_000) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) @@ -177,11 +177,11 @@ impl WeightInfo for () { /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn register_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(10_591_051) - // Standard Error: 854 - .saturating_add(Weight::from_all(12_225_u64).saturating_mul(p as u64)) - // Standard Error: 22_283 - .saturating_add(Weight::from_all(687_699_u64).saturating_mul(q as u64)) + Weight::from_all(26_910_243) + // Standard Error: 611 + .saturating_add(Weight::from_all(49_232_u64).saturating_mul(p as u64)) + // Standard Error: 15_951 + .saturating_add(Weight::from_all(1_856_252_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -190,18 +190,18 @@ impl WeightInfo for () { /// The range of component `p` is `[1, 250]`. /// The range of component `q` is `[1, 10]`. fn update_resolver(p: u32, q: u32, ) -> Weight { - Weight::from_all(12_779_040) - // Standard Error: 950 - .saturating_add(Weight::from_all(10_880_u64).saturating_mul(p as u64)) - // Standard Error: 24_784 - .saturating_add(Weight::from_all(677_602_u64).saturating_mul(q as u64)) + Weight::from_all(30_782_545) + // Standard Error: 1_711 + .saturating_add(Weight::from_all(48_616_u64).saturating_mul(p as u64)) + // Standard Error: 44_625 + .saturating_add(Weight::from_all(2_033_888_u64).saturating_mul(q as u64)) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::Resolvers` (r:1 w:1) // Proof: `Sylo::Resolvers` (`max_values`: None, `max_size`: Some(5551), added: 8026, mode: `MaxEncodedLen`) fn unregister_resolver() -> Weight { - Weight::from_all(16_000_000) + Weight::from_all(44_066_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -213,20 +213,20 @@ impl WeightInfo for () { /// The range of component `q` is `[1, 10]`. /// The range of component `r` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight { - Weight::from_all(13_259_460) - // Standard Error: 325 - .saturating_add(Weight::from_all(18_751_u64).saturating_mul(p as u64)) - // Standard Error: 8_458 - .saturating_add(Weight::from_all(815_905_u64).saturating_mul(q as u64)) - // Standard Error: 8_458 - .saturating_add(Weight::from_all(356_496_u64).saturating_mul(r as u64)) + Weight::from_all(35_692_965) + // Standard Error: 756 + .saturating_add(Weight::from_all(56_270_u64).saturating_mul(p as u64)) + // Standard Error: 19_687 + .saturating_add(Weight::from_all(2_052_012_u64).saturating_mul(q as u64)) + // Standard Error: 19_687 + .saturating_add(Weight::from_all(992_694_u64).saturating_mul(r as u64)) .saturating_add(RocksDbWeight::get().reads(2)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn add_validation_record_entry() -> Weight { - Weight::from_all(18_000_000) + Weight::from_all(49_435_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } @@ -238,20 +238,20 @@ impl WeightInfo for () { /// The range of component `q` is `[1, 10]`. /// The range of component `r` is `[1, 10]`. fn update_validation_record(p: u32, q: u32, r: u32, ) -> Weight { - Weight::from_all(4_155_879) - // Standard Error: 408 - .saturating_add(Weight::from_all(53_149_u64).saturating_mul(p as u64)) - // Standard Error: 10_621 - .saturating_add(Weight::from_all(2_603_264_u64).saturating_mul(q as u64)) - // Standard Error: 10_621 - .saturating_add(Weight::from_all(727_556_u64).saturating_mul(r as u64)) + Weight::from_all(19_670_291) + // Standard Error: 1_097 + .saturating_add(Weight::from_all(127_148_u64).saturating_mul(p as u64)) + // Standard Error: 28_567 + .saturating_add(Weight::from_all(6_001_269_u64).saturating_mul(q as u64)) + // Standard Error: 28_567 + .saturating_add(Weight::from_all(1_869_067_u64).saturating_mul(r as u64)) .saturating_add(RocksDbWeight::get().reads(2)) .saturating_add(RocksDbWeight::get().writes(1)) } // Storage: `Sylo::ValidationRecords` (r:1 w:1) // Proof: `Sylo::ValidationRecords` (`max_values`: None, `max_size`: Some(19724), added: 22199, mode: `MaxEncodedLen`) fn delete_validation_record() -> Weight { - Weight::from_all(16_000_000) + Weight::from_all(45_087_000) .saturating_add(RocksDbWeight::get().reads(1)) .saturating_add(RocksDbWeight::get().writes(1)) } diff --git a/runtime/src/weights/pallet_sylo.rs b/runtime/src/weights/pallet_sylo.rs index f29255727..c0b052d1d 100644 --- a/runtime/src/weights/pallet_sylo.rs +++ b/runtime/src/weights/pallet_sylo.rs @@ -2,9 +2,9 @@ //! Autogenerated weights for `pallet_sylo` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2025-01-12, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2025-01-13, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` -//! HOSTNAME: `Johns-Macbook-Pro.local`, CPU: `` +//! HOSTNAME: `ip-172-31-102-147`, CPU: `Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz` //! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024 // Executed Command: @@ -38,8 +38,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` - // Minimum execution time: 7_000_000 picoseconds. - Weight::from_parts(8_000_000, 0) + // Minimum execution time: 21_902_000 picoseconds. + Weight::from_parts(23_486_000, 0) .saturating_add(Weight::from_parts(0, 0)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -49,8 +49,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` - // Minimum execution time: 8_000_000 picoseconds. - Weight::from_parts(9_000_000, 0) + // Minimum execution time: 23_408_000 picoseconds. + Weight::from_parts(24_229_000, 0) .saturating_add(Weight::from_parts(0, 0)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -62,13 +62,13 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `175` // Estimated: `9016` - // Minimum execution time: 13_000_000 picoseconds. - Weight::from_parts(11_986_830, 0) + // Minimum execution time: 36_662_000 picoseconds. + Weight::from_parts(25_150_265, 0) .saturating_add(Weight::from_parts(0, 9016)) - // Standard Error: 982 - .saturating_add(Weight::from_parts(7_471, 0).saturating_mul(p.into())) - // Standard Error: 25_620 - .saturating_add(Weight::from_parts(599_095, 0).saturating_mul(q.into())) + // Standard Error: 506 + .saturating_add(Weight::from_parts(50_172, 0).saturating_mul(p.into())) + // Standard Error: 13_215 + .saturating_add(Weight::from_parts(1_832_112, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -80,13 +80,13 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `289` // Estimated: `9016` - // Minimum execution time: 14_000_000 picoseconds. - Weight::from_parts(11_418_934, 0) + // Minimum execution time: 39_574_000 picoseconds. + Weight::from_parts(30_222_407, 0) .saturating_add(Weight::from_parts(0, 9016)) - // Standard Error: 332 - .saturating_add(Weight::from_parts(14_894, 0).saturating_mul(p.into())) - // Standard Error: 8_683 - .saturating_add(Weight::from_parts(720_676, 0).saturating_mul(q.into())) + // Standard Error: 452 + .saturating_add(Weight::from_parts(41_431, 0).saturating_mul(p.into())) + // Standard Error: 11_806 + .saturating_add(Weight::from_parts(1_830_852, 0).saturating_mul(q.into())) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -96,8 +96,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `289` // Estimated: `9016` - // Minimum execution time: 16_000_000 picoseconds. - Weight::from_parts(17_000_000, 0) + // Minimum execution time: 40_473_000 picoseconds. + Weight::from_parts(41_199_000, 0) .saturating_add(Weight::from_parts(0, 9016)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) @@ -111,17 +111,17 @@ impl pallet_sylo::WeightInfo for WeightInfo { /// The range of component `r` is `[1, 10]`. fn create_validation_record(p: u32, q: u32, r: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `149 + q * (28 ±0)` + // Measured: `212 + q * (22 ±0)` // Estimated: `23189` - // Minimum execution time: 19_000_000 picoseconds. - Weight::from_parts(14_380_692, 0) + // Minimum execution time: 52_530_000 picoseconds. + Weight::from_parts(33_608_702, 0) .saturating_add(Weight::from_parts(0, 23189)) - // Standard Error: 506 - .saturating_add(Weight::from_parts(15_542, 0).saturating_mul(p.into())) - // Standard Error: 13_186 - .saturating_add(Weight::from_parts(789_626, 0).saturating_mul(q.into())) - // Standard Error: 13_186 - .saturating_add(Weight::from_parts(331_288, 0).saturating_mul(r.into())) + // Standard Error: 694 + .saturating_add(Weight::from_parts(53_732, 0).saturating_mul(p.into())) + // Standard Error: 18_066 + .saturating_add(Weight::from_parts(2_046_152, 0).saturating_mul(q.into())) + // Standard Error: 18_066 + .saturating_add(Weight::from_parts(940_420, 0).saturating_mul(r.into())) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -131,8 +131,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `317` // Estimated: `23189` - // Minimum execution time: 17_000_000 picoseconds. - Weight::from_parts(18_000_000, 0) + // Minimum execution time: 45_663_000 picoseconds. + Weight::from_parts(46_557_000, 0) .saturating_add(Weight::from_parts(0, 23189)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) @@ -146,17 +146,17 @@ impl pallet_sylo::WeightInfo for WeightInfo { /// The range of component `r` is `[1, 10]`. fn update_validation_record(p: u32, q: u32, r: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `287 + q * (28 ±0)` + // Measured: `350 + q * (22 ±0)` // Estimated: `23189` - // Minimum execution time: 24_000_000 picoseconds. - Weight::from_parts(2_741_499, 0) + // Minimum execution time: 66_160_000 picoseconds. + Weight::from_parts(15_675_696, 0) .saturating_add(Weight::from_parts(0, 23189)) - // Standard Error: 483 - .saturating_add(Weight::from_parts(54_905, 0).saturating_mul(p.into())) - // Standard Error: 12_579 - .saturating_add(Weight::from_parts(2_729_175, 0).saturating_mul(q.into())) - // Standard Error: 12_579 - .saturating_add(Weight::from_parts(736_475, 0).saturating_mul(r.into())) + // Standard Error: 1_045 + .saturating_add(Weight::from_parts(129_503, 0).saturating_mul(p.into())) + // Standard Error: 27_205 + .saturating_add(Weight::from_parts(6_070_714, 0).saturating_mul(q.into())) + // Standard Error: 27_205 + .saturating_add(Weight::from_parts(1_796_856, 0).saturating_mul(r.into())) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -166,8 +166,8 @@ impl pallet_sylo::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `276` // Estimated: `23189` - // Minimum execution time: 16_000_000 picoseconds. - Weight::from_parts(18_000_000, 0) + // Minimum execution time: 41_086_000 picoseconds. + Weight::from_parts(42_277_000, 0) .saturating_add(Weight::from_parts(0, 23189)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1))