From 88a7e1c12f6b72a2d8a670e39ce44f891eda9605 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Mon, 19 Jun 2023 21:13:04 +1200 Subject: [PATCH 01/17] orml-parameters --- 1.rs | 0 Cargo.dev.toml | 3 +- parameters/Cargo.toml | 43 ++++++ parameters/README.md | 5 + parameters/src/lib.rs | 90 ++++++++++++ parameters/src/mock.rs | 130 +++++++++++++++++ parameters/src/tests.rs | 7 + traits/Cargo.toml | 1 + traits/src/lib.rs | 1 + traits/src/parameters.rs | 293 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 572 insertions(+), 1 deletion(-) create mode 100644 1.rs create mode 100644 parameters/Cargo.toml create mode 100644 parameters/README.md create mode 100644 parameters/src/lib.rs create mode 100644 parameters/src/mock.rs create mode 100644 parameters/src/tests.rs create mode 100644 traits/src/parameters.rs diff --git a/1.rs b/1.rs new file mode 100644 index 000000000..e69de29bb diff --git a/Cargo.dev.toml b/Cargo.dev.toml index bdf769718..ec7745897 100644 --- a/Cargo.dev.toml +++ b/Cargo.dev.toml @@ -26,7 +26,8 @@ members = [ "build-script-utils", "weight-gen", "weight-meter", - "payments" + "payments", + "parameters", ] exclude = ["bencher/test"] diff --git a/parameters/Cargo.toml b/parameters/Cargo.toml new file mode 100644 index 000000000..f44e0b32c --- /dev/null +++ b/parameters/Cargo.toml @@ -0,0 +1,43 @@ +[package] +name = "orml-parameters" +description = "Offer a centralized place to store and configure parameters." +repository = "https://github.com/open-web3-stack/open-runtime-module-library/tree/master/parameters" +license = "Apache-2.0" +version = "0.4.1-dev" +authors = ["Acala Developers"] +edition = "2021" + +[dependencies] +codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["max-encoded-len"] } +scale-info = { version = "2.1.2", default-features = false, features = ["derive"] } +serde = { version = "1.0.136", optional = true } + +frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" } +frame-system = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" } +sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" } +sp-std = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" } + +orml-traits = { path = "../traits", version = "0.4.1-dev", default-features = false } + +[dev-dependencies] +sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" } +sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" } + +[features] +default = ["std"] +std = [ + "serde", + + "codec/std", + "frame-support/std", + "frame-system/std", + "scale-info/std", + "sp-runtime/std", + "sp-std/std", + + "orml-traits/std", +] +try-runtime = [ + "frame-support/try-runtime", + "frame-system/try-runtime", +] diff --git a/parameters/README.md b/parameters/README.md new file mode 100644 index 000000000..3354a90cd --- /dev/null +++ b/parameters/README.md @@ -0,0 +1,5 @@ +# Parameters Store + +### Overview + +Offer a centralized place to store and configure parameters. diff --git a/parameters/src/lib.rs b/parameters/src/lib.rs new file mode 100644 index 000000000..697d8c97e --- /dev/null +++ b/parameters/src/lib.rs @@ -0,0 +1,90 @@ +//! # Parameters +//! Offer a centralized place to store and configure parameters. + +#![cfg_attr(not(feature = "std"), no_std)] +#![allow(clippy::unused_unit)] + +use frame_support::pallet_prelude::*; +use frame_system::pallet_prelude::*; + +use frame_support::traits::EnsureOriginWithArg; +use orml_traits::parameters::{AggregratedKeyValue, Key, ParameterStore}; + +mod mock; +mod tests; + +pub use module::*; + +#[frame_support::pallet] +pub mod module { + use super::*; + + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + type AggregratedKeyValue: AggregratedKeyValue; + type AdminOrigin: EnsureOriginWithArg>; + } + + type KeyOf = <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey; + type ValueOf = <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue; + + #[pallet::error] + pub enum Error {} + + #[pallet::event] + #[pallet::generate_deposit(pub(crate) fn deposit_event)] + pub enum Event { + /// Parameter is updated + Updated { key_value: T::AggregratedKeyValue }, + } + + /// Stored parameters. + /// + /// map KeyOf => Option> + #[pallet::storage] + pub type Parameters = StorageMap<_, Blake2_128Concat, KeyOf, ValueOf, OptionQuery>; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::hooks] + impl Hooks for Pallet {} + + #[pallet::call] + impl Pallet { + /// Set parameter + #[pallet::call_index(0)] + #[pallet::weight(0)] + pub fn set_parameter(origin: OriginFor, key_value: T::AggregratedKeyValue) -> DispatchResult { + let (key, value) = key_value.clone().into_parts(); + + T::AdminOrigin::ensure_origin(origin, &key)?; + + Parameters::::mutate(key, |v| *v = value); + + Self::deposit_event(Event::Updated { key_value }); + + Ok(()) + } + } +} + +impl ParameterStore for Pallet { + type AggregratedKeyValue = T::AggregratedKeyValue; + + fn get(key: K) -> Option + where + K: Key + Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, + <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue: + TryInto, + { + let key = key.into(); + let val = Parameters::::get(key); + val.and_then(|v| { + let val: K::WrappedValue = v.try_into().ok()?; + let val = val.into(); + Some(val) + }) + } +} diff --git a/parameters/src/mock.rs b/parameters/src/mock.rs new file mode 100644 index 000000000..55c22c511 --- /dev/null +++ b/parameters/src/mock.rs @@ -0,0 +1,130 @@ +//! Mocks for the gradually-update module. + +#![cfg(test)] + +use frame_support::traits::EnsureOriginWithArg; +use frame_support::{ + construct_runtime, + traits::{ConstU32, ConstU64, Everything}, +}; +use orml_traits::{define_aggregrated_parameters, define_parameters}; +use sp_core::H256; +use sp_runtime::{testing::Header, traits::IdentityLookup}; + +use super::*; + +use crate as nft; + +pub type AccountId = u128; +pub type BlockNumber = u64; + +impl frame_system::Config for Runtime { + type RuntimeOrigin = RuntimeOrigin; + type Index = u64; + type BlockNumber = BlockNumber; + type RuntimeCall = RuntimeCall; + type Hash = H256; + type Hashing = ::sp_runtime::traits::BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type Header = Header; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = ConstU64<250>; + type BlockWeights = (); + type BlockLength = (); + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type DbWeight = (); + type BaseCallFilter = Everything; + type SystemWeightInfo = (); + type SS58Prefix = (); + type OnSetCode = (); + type MaxConsumers = ConstU32<16>; +} + +mod pallet1 { + orml_traits::define_parameters! { + pub Parameters = { + Key1: u64 = 0, + Key2(u32): u32 = 1, + Key3((u8, u8)): u128 = 2, + } + } +} +mod pallet2 { + orml_traits::define_parameters! { + pub Parameters = { + Key1: u64 = 0, + Key2(u32): u32 = 2, + Key3((u8, u8)): u128 = 4, + } + } +} +define_aggregrated_parameters! { + pub RuntimeParameters = { + Pallet1: pallet1::Parameters = 0, + Pallet2: pallet2::Parameters = 3, + } +} + +pub struct EnsureOriginImpl; + +impl EnsureOriginWithArg for EnsureOriginImpl { + type Success = (); + + fn try_origin(origin: RuntimeOrigin, key: &RuntimeParametersKey) -> Result { + match key { + RuntimeParametersKey::Pallet1(_) => { + ensure_root(origin.clone()).map_err(|_| origin)?; + return Ok(()); + } + RuntimeParametersKey::Pallet2(_) => { + ensure_signed(origin.clone()).map_err(|_| origin)?; + return Ok(()); + } + } + } +} + +impl Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type AggregratedKeyValue = RuntimeParameters; + type AdminOrigin = EnsureOriginImpl; +} + +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +construct_runtime!( + pub enum Runtime where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system, + Parameters: crate, + } +); + +pub struct ExtBuilder; + +impl Default for ExtBuilder { + fn default() -> Self { + ExtBuilder + } +} + +impl ExtBuilder { + pub fn build(self) -> sp_io::TestExternalities { + let t = frame_system::GenesisConfig::default() + .build_storage::() + .unwrap(); + + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| System::set_block_number(1)); + ext + } +} diff --git a/parameters/src/tests.rs b/parameters/src/tests.rs new file mode 100644 index 000000000..ffdf441c5 --- /dev/null +++ b/parameters/src/tests.rs @@ -0,0 +1,7 @@ +//! Unit tests for the non-fungible-token module. + +#![cfg(test)] + +use super::*; +use frame_support::{assert_noop, assert_ok}; +use mock::*; diff --git a/traits/Cargo.toml b/traits/Cargo.toml index bce8152c9..294966d30 100644 --- a/traits/Cargo.toml +++ b/traits/Cargo.toml @@ -13,6 +13,7 @@ serde = { version = "1.0.136", optional = true } codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false } num-traits = { version = "0.2.14", default-features = false } impl-trait-for-tuples = "0.2.2" +paste = "1.0" sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" } sp-core = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" } diff --git a/traits/src/lib.rs b/traits/src/lib.rs index 672c58231..3f7bb010a 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -35,6 +35,7 @@ pub mod get_by_key; pub mod location; pub mod multi_asset; pub mod nft; +pub mod parameters; pub mod price; pub mod rewards; pub mod xcm_transfer; diff --git a/traits/src/parameters.rs b/traits/src/parameters.rs new file mode 100644 index 000000000..a250caa2b --- /dev/null +++ b/traits/src/parameters.rs @@ -0,0 +1,293 @@ +#[doc(hidden)] +pub use codec; +#[doc(hidden)] +pub use frame_support; +use frame_support::Parameter; +#[doc(hidden)] +pub use paste; +#[doc(hidden)] +pub use scale_info; + +pub trait ParameterStore { + type AggregratedKeyValue: AggregratedKeyValue; + + fn get(key: K) -> Option + where + K: Key + Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, + <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue: + TryInto; +} + +pub trait Key { + type Value; + type WrappedValue: Into; +} + +pub trait AggregratedKeyValue: Parameter { + type AggregratedKey: Parameter + codec::MaxEncodedLen; + type AggregratedValue: Parameter + codec::MaxEncodedLen; + + fn into_parts(self) -> (Self::AggregratedKey, Option); +} + +/// Define parameters key value types. +/// Example: +/// +/// ``` +/// define_parameters! { +/// pub Pallet = { +/// Key1: u64 = 0, +/// Key2(u32): u32 = 1, +/// Key3((u8, u8)): u128 = 2, +/// } +/// } +/// ``` +#[macro_export] +macro_rules! define_parameters { + ( + $vis:vis $name:ident = { + $( + $key_name:ident $( ($key_para: ty) )? : $value_type:ty = $index:expr + ),+ $(,)? + } + ) => { + $crate::parameters::paste::item! { + #[derive( + Clone, + PartialEq, + Eq, + $crate::parameters::codec::Encode, + $crate::parameters::codec::Decode, + $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::frame_support::RuntimeDebug, + $crate::parameters::scale_info::TypeInfo + )] + $vis enum $name { + $( + #[codec(index = $index)] + $key_name($key_name, Option<$value_type>), + )* + } + + #[derive( + Clone, + PartialEq, + Eq, + $crate::parameters::codec::Encode, + $crate::parameters::codec::Decode, + $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::frame_support::RuntimeDebug, + $crate::parameters::scale_info::TypeInfo + )] + $vis enum [<$name Key>] { + $( + #[codec(index = $index)] + $key_name($key_name), + )* + } + + #[derive( + Clone, + PartialEq, + Eq, + $crate::parameters::codec::Encode, + $crate::parameters::codec::Decode, + $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::frame_support::RuntimeDebug, + $crate::parameters::scale_info::TypeInfo + )] + $vis enum [<$name Value>] { + $( + #[codec(index = $index)] + $key_name($value_type), + )* + } + + impl $crate::parameters::AggregratedKeyValue for $name { + type AggregratedKey = [<$name Key>]; + type AggregratedValue = [<$name Value>]; + + fn into_parts(self) -> (Self::AggregratedKey, Option) { + match self { + $( + $name::$key_name(key, value) => ([<$name Key>]::$key_name(key), value.map([<$name Value>]::$key_name)), + )* + } + } + } + + $( + #[derive( + Clone, + PartialEq, + Eq, + $crate::parameters::codec::Encode, + $crate::parameters::codec::Decode, + $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::frame_support::RuntimeDebug, + $crate::parameters::scale_info::TypeInfo + )] + $vis struct $key_name( $(pub $key_para)? ); + + impl $crate::parameters::Key for $key_name { + type Value = $value_type; + type WrappedValue = [<$key_name Value>]; + } + + impl From<$key_name> for [<$name Key>] { + fn from(key: $key_name) -> Self { + [<$name Key>]::$key_name(key) + } + } + + $vis struct [<$key_name Value>](pub $value_type); + + impl From<[<$key_name Value>]> for [<$name Value>] { + fn from(value: [<$key_name Value>]) -> Self { + [<$name Value>]::$key_name(value.0) + } + } + + impl From<($key_name, $value_type)> for $name { + fn from((key, value): ($key_name, $value_type)) -> Self { + $name::$key_name(key, Some(value)) + } + } + + impl From<$key_name> for $name { + fn from(key: $key_name) -> Self { + $name::$key_name(key, None) + } + } + + impl TryFrom<[<$name Value>]> for [<$key_name Value>] { + type Error = (); + + fn try_from(value: [<$name Value>]) -> Result { + match value { + [<$name Value>]::$key_name(value) => Ok([<$key_name Value>](value)), + _ => Err(()), + } + } + } + + impl From<[<$key_name Value>]> for $value_type { + fn from(value: [<$key_name Value>]) -> Self { + value.0 + } + } + )* + } + }; +} + +/// Define aggregrated parameters types. +/// +/// Example: +/// ``` +/// mod pallet1 { +/// define_parameters! { +/// pub Pallet = { +/// Key1: u64 = 0, +/// Key2(u32): u32 = 1, +/// Key3((u8, u8)): u128 = 2, +/// } +/// } +/// } +/// +/// mod pallet2 { +/// define_parameters! { +/// pub Pallet = { +/// Key1: u64 = 0, +/// Key2(u32): u32 = 1, +/// Key3((u8, u8)): u128 = 2, +/// } +/// } +/// } +/// +/// define_aggregrated_parameters! { +/// pub AggregratedPallet = { +/// Pallet1: pallet1::Pallet = 0, +/// Pallet2: pallet2::Pallet = 1, +/// } +/// } +/// ``` +#[macro_export] +macro_rules! define_aggregrated_parameters { + ( + $vis:vis $name:ident = { + $( + $parameter_name:ident: $parameter_type:ty = $index:expr + ),+ $(,)? + } + ) => { + $crate::parameters::paste::item! { + #[derive( + Clone, + PartialEq, + Eq, + $crate::parameters::codec::Encode, + $crate::parameters::codec::Decode, + $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::frame_support::RuntimeDebug, + $crate::parameters::scale_info::TypeInfo + )] + $vis enum $name { + $( + #[codec(index = $index)] + $parameter_name($parameter_type), + )* + } + + #[derive( + Clone, + PartialEq, + Eq, + $crate::parameters::codec::Encode, + $crate::parameters::codec::Decode, + $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::frame_support::RuntimeDebug, + $crate::parameters::scale_info::TypeInfo + )] + $vis enum [<$name Key>] { + $( + #[codec(index = $index)] + $parameter_name(<$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedKey), + )* + } + + #[derive( + Clone, + PartialEq, + Eq, + $crate::parameters::codec::Encode, + $crate::parameters::codec::Decode, + $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::frame_support::RuntimeDebug, + $crate::parameters::scale_info::TypeInfo + )] + $vis enum [<$name Value>] { + $( + #[codec(index = $index)] + $parameter_name(<$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedValue), + )* + } + + impl $crate::parameters::AggregratedKeyValue for $name { + type AggregratedKey = [<$name Key>]; + type AggregratedValue = [<$name Value>]; + + fn into_parts(self) -> (Self::AggregratedKey, Option) { + match self { + $( + $name::$parameter_name(parameter) => { + let (key, value) = parameter.into_parts(); + ([<$name Key>]::$parameter_name(key), value.map([<$name Value>]::$parameter_name)) + }, + )* + } + } + } + } + }; +} From 1ed2b43ce5dce9acf67865d17484ba80531058c2 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 20 Jun 2023 10:58:16 +1200 Subject: [PATCH 02/17] fix --- 1.rs | 0 traits/src/parameters.rs | 46 ++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 23 deletions(-) delete mode 100644 1.rs diff --git a/1.rs b/1.rs deleted file mode 100644 index e69de29bb..000000000 diff --git a/traits/src/parameters.rs b/traits/src/parameters.rs index a250caa2b..5bd816987 100644 --- a/traits/src/parameters.rs +++ b/traits/src/parameters.rs @@ -35,11 +35,11 @@ pub trait AggregratedKeyValue: Parameter { /// /// ``` /// define_parameters! { -/// pub Pallet = { -/// Key1: u64 = 0, -/// Key2(u32): u32 = 1, -/// Key3((u8, u8)): u128 = 2, -/// } +/// pub Pallet = { +/// Key1: u64 = 0, +/// Key2(u32): u32 = 1, +/// Key3((u8, u8)): u128 = 2, +/// } /// } /// ``` #[macro_export] @@ -186,30 +186,30 @@ macro_rules! define_parameters { /// Example: /// ``` /// mod pallet1 { -/// define_parameters! { -/// pub Pallet = { -/// Key1: u64 = 0, -/// Key2(u32): u32 = 1, -/// Key3((u8, u8)): u128 = 2, -/// } -/// } +/// define_parameters! { +/// pub Pallet = { +/// Key1: u64 = 0, +/// Key2(u32): u32 = 1, +/// Key3((u8, u8)): u128 = 2, +/// } +/// } /// } /// /// mod pallet2 { -/// define_parameters! { -/// pub Pallet = { -/// Key1: u64 = 0, -/// Key2(u32): u32 = 1, -/// Key3((u8, u8)): u128 = 2, -/// } -/// } +/// define_parameters! { +/// pub Pallet = { +/// Key1: u64 = 0, +/// Key2(u32): u32 = 1, +/// Key3((u8, u8)): u128 = 2, +/// } +/// } /// } /// /// define_aggregrated_parameters! { -/// pub AggregratedPallet = { -/// Pallet1: pallet1::Pallet = 0, -/// Pallet2: pallet2::Pallet = 1, -/// } +/// pub AggregratedPallet = { +/// Pallet1: pallet1::Pallet = 0, +/// Pallet2: pallet2::Pallet = 1, +/// } /// } /// ``` #[macro_export] From 055d48dd4fb3370141880fdd2e87a7e00f60dab7 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 20 Jun 2023 18:59:33 +1200 Subject: [PATCH 03/17] add tests --- parameters/Cargo.toml | 2 +- parameters/README.md | 2 +- parameters/src/lib.rs | 19 +++++++----- parameters/src/mock.rs | 18 ++++------- parameters/src/tests.rs | 67 ++++++++++++++++++++++++++++++++++++++++ traits/src/parameters.rs | 29 +++++++++++++++-- 6 files changed, 113 insertions(+), 24 deletions(-) diff --git a/parameters/Cargo.toml b/parameters/Cargo.toml index f44e0b32c..86564c73d 100644 --- a/parameters/Cargo.toml +++ b/parameters/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "orml-parameters" -description = "Offer a centralized place to store and configure parameters." +description = "Offer a centra place to store and configure parameters." repository = "https://github.com/open-web3-stack/open-runtime-module-library/tree/master/parameters" license = "Apache-2.0" version = "0.4.1-dev" diff --git a/parameters/README.md b/parameters/README.md index 3354a90cd..a889bff9f 100644 --- a/parameters/README.md +++ b/parameters/README.md @@ -2,4 +2,4 @@ ### Overview -Offer a centralized place to store and configure parameters. +Offer a centra place to store and configure parameters. diff --git a/parameters/src/lib.rs b/parameters/src/lib.rs index 697d8c97e..4b19c58aa 100644 --- a/parameters/src/lib.rs +++ b/parameters/src/lib.rs @@ -1,5 +1,5 @@ //! # Parameters -//! Offer a centralized place to store and configure parameters. +//! Offer a centra place to store and configure parameters. #![cfg_attr(not(feature = "std"), no_std)] #![allow(clippy::unused_unit)] @@ -73,16 +73,21 @@ pub mod module { impl ParameterStore for Pallet { type AggregratedKeyValue = T::AggregratedKeyValue; - fn get(key: K) -> Option + fn get(key: K) -> Option where - K: Key + Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, + KV: AggregratedKeyValue, + K: Key + Into<::AggregratedKey>, + ::AggregratedKey: + Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue: - TryInto, + TryInto<::AggregratedValue>, + ::AggregratedValue: TryInto, { - let key = key.into(); - let val = Parameters::::get(key); + let key: ::AggregratedKey = key.into(); + let val = Parameters::::get(key.into()); val.and_then(|v| { - let val: K::WrappedValue = v.try_into().ok()?; + let val: ::AggregratedValue = v.try_into().ok()?; + let val: K::WrappedValue = val.try_into().ok()?; let val = val.into(); Some(val) }) diff --git a/parameters/src/mock.rs b/parameters/src/mock.rs index 55c22c511..cd4649203 100644 --- a/parameters/src/mock.rs +++ b/parameters/src/mock.rs @@ -7,13 +7,13 @@ use frame_support::{ construct_runtime, traits::{ConstU32, ConstU64, Everything}, }; -use orml_traits::{define_aggregrated_parameters, define_parameters}; +use orml_traits::define_aggregrated_parameters; use sp_core::H256; use sp_runtime::{testing::Header, traits::IdentityLookup}; use super::*; -use crate as nft; +use crate as parameters; pub type AccountId = u128; pub type BlockNumber = u64; @@ -45,7 +45,7 @@ impl frame_system::Config for Runtime { type MaxConsumers = ConstU32<16>; } -mod pallet1 { +pub mod pallet1 { orml_traits::define_parameters! { pub Parameters = { Key1: u64 = 0, @@ -54,7 +54,7 @@ mod pallet1 { } } } -mod pallet2 { +pub mod pallet2 { orml_traits::define_parameters! { pub Parameters = { Key1: u64 = 0, @@ -105,20 +105,14 @@ construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system, - Parameters: crate, + ModuleParameters: parameters, } ); pub struct ExtBuilder; -impl Default for ExtBuilder { - fn default() -> Self { - ExtBuilder - } -} - impl ExtBuilder { - pub fn build(self) -> sp_io::TestExternalities { + pub fn new() -> sp_io::TestExternalities { let t = frame_system::GenesisConfig::default() .build_storage::() .unwrap(); diff --git a/parameters/src/tests.rs b/parameters/src/tests.rs index ffdf441c5..b7178a53e 100644 --- a/parameters/src/tests.rs +++ b/parameters/src/tests.rs @@ -5,3 +5,70 @@ use super::*; use frame_support::{assert_noop, assert_ok}; use mock::*; +use orml_traits::parameters::ParameterStore; + +#[test] +fn set_parameters() { + ExtBuilder::new().execute_with(|| { + assert_eq!( + ::get::(pallet1::Key1()), + None + ); + + assert_noop!( + ModuleParameters::set_parameter( + RuntimeOrigin::signed(1), + RuntimeParameters::Pallet1(pallet1::Parameters::Key1(pallet1::Key1(), Some(123))), + ), + DispatchError::BadOrigin + ); + + assert_ok!(ModuleParameters::set_parameter( + RuntimeOrigin::root(), + RuntimeParameters::Pallet1(pallet1::Parameters::Key1(pallet1::Key1(), Some(123))), + )); + + assert_eq!( + ::get::(pallet1::Key1()), + Some(123) + ); + + assert_ok!(ModuleParameters::set_parameter( + RuntimeOrigin::root(), + RuntimeParameters::Pallet1(pallet1::Parameters::Key2(pallet1::Key2(234), Some(345))), + )); + + assert_eq!( + ::get::(pallet1::Key2(234)), + Some(345) + ); + + assert_eq!( + ::get::(pallet1::Key2(235)), + None + ); + + assert_eq!( + ::get::(pallet2::Key3((1, 2))), + None + ); + + assert_noop!( + ModuleParameters::set_parameter( + RuntimeOrigin::root(), + RuntimeParameters::Pallet2(pallet2::Parameters::Key3(pallet2::Key3((1, 2)), Some(123))), + ), + DispatchError::BadOrigin + ); + + assert_ok!(ModuleParameters::set_parameter( + RuntimeOrigin::signed(1), + RuntimeParameters::Pallet2(pallet2::Parameters::Key3(pallet2::Key3((1, 2)), Some(456))), + )); + + assert_eq!( + ::get::(pallet2::Key3((1, 2))), + Some(456) + ); + }); +} diff --git a/traits/src/parameters.rs b/traits/src/parameters.rs index 5bd816987..1e3e08744 100644 --- a/traits/src/parameters.rs +++ b/traits/src/parameters.rs @@ -11,11 +11,15 @@ pub use scale_info; pub trait ParameterStore { type AggregratedKeyValue: AggregratedKeyValue; - fn get(key: K) -> Option + fn get(key: K) -> Option where - K: Key + Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, + KV: AggregratedKeyValue, + K: Key + Into<::AggregratedKey>, + ::AggregratedKey: + Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue: - TryInto; + TryInto<::AggregratedValue>, + ::AggregratedValue: TryInto; } pub trait Key { @@ -288,6 +292,25 @@ macro_rules! define_aggregrated_parameters { } } } + + $( + impl From<<$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedKey> for [<$name Key>] { + fn from(key: <$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedKey) -> Self { + [<$name Key>]::$parameter_name(key) + } + } + + impl TryFrom<[<$name Value>]> for <$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedValue { + type Error = (); + + fn try_from(value: [<$name Value>]) -> Result { + match value { + [<$name Value>]::$parameter_name(value) => Ok(value), + _ => Err(()), + } + } + } + )* } }; } From 446d9c55e66286343fee6839d43b06f749393872 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 20 Jun 2023 22:18:50 +1200 Subject: [PATCH 04/17] whitespaces --- traits/src/parameters.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/traits/src/parameters.rs b/traits/src/parameters.rs index 1e3e08744..4572b3b34 100644 --- a/traits/src/parameters.rs +++ b/traits/src/parameters.rs @@ -62,7 +62,7 @@ macro_rules! define_parameters { Eq, $crate::parameters::codec::Encode, $crate::parameters::codec::Decode, - $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::codec::MaxEncodedLen, $crate::parameters::frame_support::RuntimeDebug, $crate::parameters::scale_info::TypeInfo )] @@ -79,7 +79,7 @@ macro_rules! define_parameters { Eq, $crate::parameters::codec::Encode, $crate::parameters::codec::Decode, - $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::codec::MaxEncodedLen, $crate::parameters::frame_support::RuntimeDebug, $crate::parameters::scale_info::TypeInfo )] @@ -96,7 +96,7 @@ macro_rules! define_parameters { Eq, $crate::parameters::codec::Encode, $crate::parameters::codec::Decode, - $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::codec::MaxEncodedLen, $crate::parameters::frame_support::RuntimeDebug, $crate::parameters::scale_info::TypeInfo )] @@ -232,7 +232,7 @@ macro_rules! define_aggregrated_parameters { Eq, $crate::parameters::codec::Encode, $crate::parameters::codec::Decode, - $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::codec::MaxEncodedLen, $crate::parameters::frame_support::RuntimeDebug, $crate::parameters::scale_info::TypeInfo )] @@ -249,7 +249,7 @@ macro_rules! define_aggregrated_parameters { Eq, $crate::parameters::codec::Encode, $crate::parameters::codec::Decode, - $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::codec::MaxEncodedLen, $crate::parameters::frame_support::RuntimeDebug, $crate::parameters::scale_info::TypeInfo )] @@ -266,7 +266,7 @@ macro_rules! define_aggregrated_parameters { Eq, $crate::parameters::codec::Encode, $crate::parameters::codec::Decode, - $crate::parameters::codec::MaxEncodedLen, + $crate::parameters::codec::MaxEncodedLen, $crate::parameters::frame_support::RuntimeDebug, $crate::parameters::scale_info::TypeInfo )] From 41918e613274fe00aa67b6c3b771a2d4c811a9a5 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Fri, 23 Jun 2023 11:47:43 +1200 Subject: [PATCH 05/17] more tests --- traits/src/parameters.rs | 147 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/traits/src/parameters.rs b/traits/src/parameters.rs index 4572b3b34..d1e4ce7f2 100644 --- a/traits/src/parameters.rs +++ b/traits/src/parameters.rs @@ -144,6 +144,23 @@ macro_rules! define_parameters { } } + impl TryFrom<[<$name Key>]> for $key_name { + type Error = (); + + fn try_from(key: [<$name Key>]) -> Result { + match key { + [<$name Key>]::$key_name(key) => Ok(key), + _ => Err(()), + } + } + } + + #[derive( + Clone, + PartialEq, + Eq, + $crate::parameters::frame_support::RuntimeDebug + )] $vis struct [<$key_name Value>](pub $value_type); impl From<[<$key_name Value>]> for [<$name Value>] { @@ -314,3 +331,133 @@ macro_rules! define_aggregrated_parameters { } }; } + +#[cfg(test)] +mod tests { + pub mod pallet1 { + define_parameters! { + pub Parameters = { + Key1: u64 = 0, + Key2(u32): u32 = 1, + Key3((u8, u8)): u128 = 2, + } + } + } + pub mod pallet2 { + define_parameters! { + pub Parameters = { + Key1: u64 = 0, + Key2(u32): u32 = 2, + Key3((u8, u8)): u128 = 4, + } + } + } + define_aggregrated_parameters! { + pub RuntimeParameters = { + Pallet1: pallet1::Parameters = 0, + Pallet2: pallet2::Parameters = 3, + } + } + + #[test] + fn test_define_parameters_key_convert() { + let key1 = pallet1::Key1(); + let parameter_key: pallet1::ParametersKey = key1.clone().into(); + let key1_2: pallet1::Key1 = parameter_key.clone().try_into().unwrap(); + + assert_eq!(key1, key1_2); + assert_eq!(parameter_key, pallet1::ParametersKey::Key1(key1)); + + let key2 = pallet1::Key2(1); + let parameter_key: pallet1::ParametersKey = key2.clone().into(); + let key2_2: pallet1::Key2 = parameter_key.clone().try_into().unwrap(); + + assert_eq!(key2, key2_2); + assert_eq!(parameter_key, pallet1::ParametersKey::Key2(key2)); + } + + #[test] + fn test_define_parameters_value_convert() { + let value1 = pallet1::Key1Value(1); + let parameter_value: pallet1::ParametersValue = value1.clone().into(); + let value1_2: pallet1::Key1Value = parameter_value.clone().try_into().unwrap(); + + assert_eq!(value1, value1_2); + assert_eq!(parameter_value, pallet1::ParametersValue::Key1(1)); + + let value2 = pallet1::Key2Value(2); + let parameter_value: pallet1::ParametersValue = value2.clone().into(); + let value2_2: pallet1::Key2Value = parameter_value.clone().try_into().unwrap(); + + assert_eq!(value2, value2_2); + assert_eq!(parameter_value, pallet1::ParametersValue::Key2(2)); + } + + #[test] + fn test_define_parameters_aggregrated_key_value() { + use crate::parameters::AggregratedKeyValue; + + let kv1 = pallet1::Parameters::Key1(pallet1::Key1(), None); + let (key1, value1) = kv1.clone().into_parts(); + + assert_eq!(key1, pallet1::ParametersKey::Key1(pallet1::Key1())); + assert_eq!(value1, None); + + let kv2 = pallet1::Parameters::Key2(pallet1::Key2(1), Some(2)); + let (key2, value2) = kv2.clone().into_parts(); + + assert_eq!(key2, pallet1::ParametersKey::Key2(pallet1::Key2(1))); + assert_eq!(value2, Some(pallet1::ParametersValue::Key2(2))); + } + + #[test] + fn test_define_aggregrated_parameters_key_convert() { + use codec::Encode; + + let key1 = pallet1::Key1(); + let parameter_key: pallet1::ParametersKey = key1.clone().into(); + let runtime_key: RuntimeParametersKey = parameter_key.clone().into(); + + assert_eq!( + runtime_key, + RuntimeParametersKey::Pallet1(pallet1::ParametersKey::Key1(key1)) + ); + assert_eq!(runtime_key.encode(), vec![0, 0]); + + let key2 = pallet2::Key2(1); + let parameter_key: pallet2::ParametersKey = key2.clone().into(); + let runtime_key: RuntimeParametersKey = parameter_key.clone().into(); + + assert_eq!( + runtime_key, + RuntimeParametersKey::Pallet2(pallet2::ParametersKey::Key2(key2)) + ); + assert_eq!(runtime_key.encode(), vec![3, 2, 1, 0, 0, 0]); + } + + #[test] + fn test_define_aggregrated_parameters_aggregrated_key_value() { + use crate::parameters::AggregratedKeyValue; + + let kv1 = RuntimeParameters::Pallet1(pallet1::Parameters::Key1(pallet1::Key1(), None)); + let (key1, value1) = kv1.clone().into_parts(); + + assert_eq!( + key1, + RuntimeParametersKey::Pallet1(pallet1::ParametersKey::Key1(pallet1::Key1())) + ); + assert_eq!(value1, None); + + let kv2 = RuntimeParameters::Pallet2(pallet2::Parameters::Key2(pallet2::Key2(1), Some(2))); + let (key2, value2) = kv2.clone().into_parts(); + + assert_eq!( + key2, + RuntimeParametersKey::Pallet2(pallet2::ParametersKey::Key2(pallet2::Key2(1))) + ); + assert_eq!( + value2, + Some(RuntimeParametersValue::Pallet2(pallet2::ParametersValue::Key2(2))) + ); + } +} From 2562ff3e35772a7eb2af57c0d6d8e34af8151cda Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Wed, 5 Jul 2023 13:41:41 +1200 Subject: [PATCH 06/17] 0.9.43 --- parameters/Cargo.toml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/parameters/Cargo.toml b/parameters/Cargo.toml index 86564c73d..9d61c0139 100644 --- a/parameters/Cargo.toml +++ b/parameters/Cargo.toml @@ -12,16 +12,16 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features = scale-info = { version = "2.1.2", default-features = false, features = ["derive"] } serde = { version = "1.0.136", optional = true } -frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" } -frame-system = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" } -sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" } -sp-std = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" } +frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.43" } +frame-system = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.43" } +sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.43" } +sp-std = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.43" } orml-traits = { path = "../traits", version = "0.4.1-dev", default-features = false } [dev-dependencies] -sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" } -sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" } +sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" } +sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" } [features] default = ["std"] From 9cd9dbc18ff57e1bbab8fbd2f268f14800004bca Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 11 Jul 2023 11:29:05 +1200 Subject: [PATCH 07/17] add weights --- parameters/src/lib.rs | 11 ++++++++++- parameters/src/weights.rs | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 parameters/src/weights.rs diff --git a/parameters/src/lib.rs b/parameters/src/lib.rs index 4b19c58aa..1ed8a5316 100644 --- a/parameters/src/lib.rs +++ b/parameters/src/lib.rs @@ -12,6 +12,7 @@ use orml_traits::parameters::{AggregratedKeyValue, Key, ParameterStore}; mod mock; mod tests; +mod weights; pub use module::*; @@ -22,8 +23,16 @@ pub mod module { #[pallet::config] pub trait Config: frame_system::Config { type RuntimeEvent: From> + IsType<::RuntimeEvent>; + + /// The key value type for parameters. Usually created by + /// orml_traits::parameters::define_aggregrated_parameters type AggregratedKeyValue: AggregratedKeyValue; + + /// The origin which may update the parameter. type AdminOrigin: EnsureOriginWithArg>; + + /// Weight information for extrinsics in this module. + type WeightInfo: WeightInfo; } type KeyOf = <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey; @@ -55,7 +64,7 @@ pub mod module { impl Pallet { /// Set parameter #[pallet::call_index(0)] - #[pallet::weight(0)] + #[pallet::weight(T::WeightInfo::set_parameter())] pub fn set_parameter(origin: OriginFor, key_value: T::AggregratedKeyValue) -> DispatchResult { let (key, value) = key_value.clone().into_parts(); diff --git a/parameters/src/weights.rs b/parameters/src/weights.rs new file mode 100644 index 000000000..23c06f6fb --- /dev/null +++ b/parameters/src/weights.rs @@ -0,0 +1,17 @@ +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(clippy::unnecessary_cast)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use sp_std::marker::PhantomData; + +pub trait WeightInfo { + fn set_parameter() -> Weight; +} + +impl WeightInfo for () { + fn set_parameter() -> Weight { + Weight::zero() + } +} From bd3f75c29cb3477d37e2d70fe95a67d78a5d50a3 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 11 Jul 2023 12:13:57 +1200 Subject: [PATCH 08/17] fix --- parameters/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/parameters/src/lib.rs b/parameters/src/lib.rs index 1ed8a5316..0273382e9 100644 --- a/parameters/src/lib.rs +++ b/parameters/src/lib.rs @@ -15,6 +15,7 @@ mod tests; mod weights; pub use module::*; +pub use weights::WeightInfo; #[frame_support::pallet] pub mod module { From 701f647df6c625b80f3e79fb208dad10969ca413 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 11 Jul 2023 12:50:25 +1200 Subject: [PATCH 09/17] fix --- parameters/src/mock.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/parameters/src/mock.rs b/parameters/src/mock.rs index cd4649203..b9cb111e5 100644 --- a/parameters/src/mock.rs +++ b/parameters/src/mock.rs @@ -87,12 +87,18 @@ impl EnsureOriginWithArg for EnsureOriginIm } } } + + #[cfg(feature = "runtime-benchmarks")] + fn try_successful_origin() -> Result { + Err(()) + } } impl Config for Runtime { type RuntimeEvent = RuntimeEvent; type AggregratedKeyValue = RuntimeParameters; type AdminOrigin = EnsureOriginImpl; + type WeightInfo = (); } type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; From 5c516e4a37576482eb5b0e9d14abe60cada0d0ff Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 11 Jul 2023 13:39:41 +1200 Subject: [PATCH 10/17] fix docs test --- parameters/Cargo.toml | 5 +++++ traits/src/parameters.rs | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/parameters/Cargo.toml b/parameters/Cargo.toml index 9d61c0139..790540e4b 100644 --- a/parameters/Cargo.toml +++ b/parameters/Cargo.toml @@ -37,6 +37,11 @@ std = [ "orml-traits/std", ] +runtime-benchmarks = [ + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", +] try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", diff --git a/traits/src/parameters.rs b/traits/src/parameters.rs index d1e4ce7f2..c3f9f5e7b 100644 --- a/traits/src/parameters.rs +++ b/traits/src/parameters.rs @@ -38,6 +38,9 @@ pub trait AggregratedKeyValue: Parameter { /// Example: /// /// ``` +/// # #[macro_use] +/// # extern crate orml_traits; +/// # fn main() {} /// define_parameters! { /// pub Pallet = { /// Key1: u64 = 0, @@ -206,6 +209,9 @@ macro_rules! define_parameters { /// /// Example: /// ``` +/// # #[macro_use] +/// # extern crate orml_traits; +/// # fn main() {} /// mod pallet1 { /// define_parameters! { /// pub Pallet = { From 7ac9410a8e61fc74b025354d88a0beff61152dca Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 11 Jul 2023 14:05:38 +1200 Subject: [PATCH 11/17] fix --- parameters/src/mock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parameters/src/mock.rs b/parameters/src/mock.rs index b9cb111e5..f32b7ff73 100644 --- a/parameters/src/mock.rs +++ b/parameters/src/mock.rs @@ -89,7 +89,7 @@ impl EnsureOriginWithArg for EnsureOriginIm } #[cfg(feature = "runtime-benchmarks")] - fn try_successful_origin() -> Result { + fn try_successful_origin(_key: &RuntimeParametersKey) -> Result { Err(()) } } From 91dd3c885baeb6bb553c75b4c098b6f11d9d0ae6 Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Wed, 16 Aug 2023 20:14:51 +1200 Subject: [PATCH 12/17] Apply suggestions from code review Co-authored-by: Nuno Alexandre --- parameters/README.md | 2 +- parameters/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/parameters/README.md b/parameters/README.md index a889bff9f..8a7fd706f 100644 --- a/parameters/README.md +++ b/parameters/README.md @@ -2,4 +2,4 @@ ### Overview -Offer a centra place to store and configure parameters. +Offer a central place to store and configure parameters. diff --git a/parameters/src/lib.rs b/parameters/src/lib.rs index 0273382e9..a4f64c665 100644 --- a/parameters/src/lib.rs +++ b/parameters/src/lib.rs @@ -1,5 +1,5 @@ //! # Parameters -//! Offer a centra place to store and configure parameters. +//! Offer a central place to store and configure parameters. #![cfg_attr(not(feature = "std"), no_std)] #![allow(clippy::unused_unit)] From 6c7a85994f454427f624c0edc79e5dda537ee7a7 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Thu, 17 Aug 2023 14:06:07 +1200 Subject: [PATCH 13/17] update --- parameters/Cargo.toml | 12 ++++++------ parameters/src/lib.rs | 3 --- parameters/src/mock.rs | 19 ++++++------------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/parameters/Cargo.toml b/parameters/Cargo.toml index 790540e4b..23190f996 100644 --- a/parameters/Cargo.toml +++ b/parameters/Cargo.toml @@ -12,16 +12,16 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features = scale-info = { version = "2.1.2", default-features = false, features = ["derive"] } serde = { version = "1.0.136", optional = true } -frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.43" } -frame-system = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.43" } -sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.43" } -sp-std = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.43" } +frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v1.0.0" } +frame-system = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v1.0.0" } +sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v1.0.0" } +sp-std = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v1.0.0" } orml-traits = { path = "../traits", version = "0.4.1-dev", default-features = false } [dev-dependencies] -sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" } -sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" } +sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" } +sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" } [features] default = ["std"] diff --git a/parameters/src/lib.rs b/parameters/src/lib.rs index a4f64c665..bc398e6f9 100644 --- a/parameters/src/lib.rs +++ b/parameters/src/lib.rs @@ -58,9 +58,6 @@ pub mod module { #[pallet::pallet] pub struct Pallet(_); - #[pallet::hooks] - impl Hooks for Pallet {} - #[pallet::call] impl Pallet { /// Set parameter diff --git a/parameters/src/mock.rs b/parameters/src/mock.rs index f32b7ff73..2b0806a0f 100644 --- a/parameters/src/mock.rs +++ b/parameters/src/mock.rs @@ -9,25 +9,23 @@ use frame_support::{ }; use orml_traits::define_aggregrated_parameters; use sp_core::H256; -use sp_runtime::{testing::Header, traits::IdentityLookup}; +use sp_runtime::{traits::IdentityLookup, BuildStorage}; use super::*; use crate as parameters; pub type AccountId = u128; -pub type BlockNumber = u64; impl frame_system::Config for Runtime { type RuntimeOrigin = RuntimeOrigin; - type Index = u64; - type BlockNumber = BlockNumber; type RuntimeCall = RuntimeCall; + type Nonce = u64; type Hash = H256; type Hashing = ::sp_runtime::traits::BlakeTwo256; type AccountId = AccountId; type Lookup = IdentityLookup; - type Header = Header; + type Block = Block; type RuntimeEvent = RuntimeEvent; type BlockHashCount = ConstU64<250>; type BlockWeights = (); @@ -101,15 +99,10 @@ impl Config for Runtime { type WeightInfo = (); } -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; construct_runtime!( - pub enum Runtime where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, - { + pub enum Runtime { System: frame_system, ModuleParameters: parameters, } @@ -119,8 +112,8 @@ pub struct ExtBuilder; impl ExtBuilder { pub fn new() -> sp_io::TestExternalities { - let t = frame_system::GenesisConfig::default() - .build_storage::() + let t = frame_system::GenesisConfig::::default() + .build_storage() .unwrap(); let mut ext = sp_io::TestExternalities::new(t); From f3d4a54bca265437fe606fcc0d3d1dbc80346591 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Thu, 17 Aug 2023 18:30:20 +1200 Subject: [PATCH 14/17] improve usage --- parameters/src/lib.rs | 8 +++---- parameters/src/tests.rs | 18 +++++++-------- traits/src/parameters.rs | 47 +++++++++++++++++++++++++++++++--------- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/parameters/src/lib.rs b/parameters/src/lib.rs index bc398e6f9..58ffc3c5d 100644 --- a/parameters/src/lib.rs +++ b/parameters/src/lib.rs @@ -8,7 +8,7 @@ use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; use frame_support::traits::EnsureOriginWithArg; -use orml_traits::parameters::{AggregratedKeyValue, Key, ParameterStore}; +use orml_traits::parameters::{AggregratedKeyValue, Key, RuntimeParameterStore}; mod mock; mod tests; @@ -77,7 +77,7 @@ pub mod module { } } -impl ParameterStore for Pallet { +impl RuntimeParameterStore for Pallet { type AggregratedKeyValue = T::AggregratedKeyValue; fn get(key: K) -> Option @@ -85,8 +85,8 @@ impl ParameterStore for Pallet { KV: AggregratedKeyValue, K: Key + Into<::AggregratedKey>, ::AggregratedKey: - Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, - <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue: + Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, + <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue: TryInto<::AggregratedValue>, ::AggregratedValue: TryInto, { diff --git a/parameters/src/tests.rs b/parameters/src/tests.rs index b7178a53e..c0dc7ec8e 100644 --- a/parameters/src/tests.rs +++ b/parameters/src/tests.rs @@ -5,31 +5,31 @@ use super::*; use frame_support::{assert_noop, assert_ok}; use mock::*; -use orml_traits::parameters::ParameterStore; +use orml_traits::parameters::RuntimeParameterStore; #[test] fn set_parameters() { ExtBuilder::new().execute_with(|| { assert_eq!( - ::get::(pallet1::Key1()), + ::get::(pallet1::Key1), None ); assert_noop!( ModuleParameters::set_parameter( RuntimeOrigin::signed(1), - RuntimeParameters::Pallet1(pallet1::Parameters::Key1(pallet1::Key1(), Some(123))), + RuntimeParameters::Pallet1(pallet1::Parameters::Key1(pallet1::Key1, Some(123))), ), DispatchError::BadOrigin ); assert_ok!(ModuleParameters::set_parameter( RuntimeOrigin::root(), - RuntimeParameters::Pallet1(pallet1::Parameters::Key1(pallet1::Key1(), Some(123))), + RuntimeParameters::Pallet1(pallet1::Parameters::Key1(pallet1::Key1, Some(123))), )); assert_eq!( - ::get::(pallet1::Key1()), + ::get::(pallet1::Key1), Some(123) ); @@ -39,17 +39,17 @@ fn set_parameters() { )); assert_eq!( - ::get::(pallet1::Key2(234)), + ::get::(pallet1::Key2(234)), Some(345) ); assert_eq!( - ::get::(pallet1::Key2(235)), + ::get::(pallet1::Key2(235)), None ); assert_eq!( - ::get::(pallet2::Key3((1, 2))), + ::get::(pallet2::Key3((1, 2))), None ); @@ -67,7 +67,7 @@ fn set_parameters() { )); assert_eq!( - ::get::(pallet2::Key3((1, 2))), + ::get::(pallet2::Key3((1, 2))), Some(456) ); }); diff --git a/traits/src/parameters.rs b/traits/src/parameters.rs index c3f9f5e7b..d468bde9e 100644 --- a/traits/src/parameters.rs +++ b/traits/src/parameters.rs @@ -8,7 +8,7 @@ pub use paste; #[doc(hidden)] pub use scale_info; -pub trait ParameterStore { +pub trait RuntimeParameterStore { type AggregratedKeyValue: AggregratedKeyValue; fn get(key: K) -> Option @@ -16,8 +16,8 @@ pub trait ParameterStore { KV: AggregratedKeyValue, K: Key + Into<::AggregratedKey>, ::AggregratedKey: - Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, - <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue: + Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, + <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue: TryInto<::AggregratedValue>, ::AggregratedValue: TryInto; } @@ -34,6 +34,33 @@ pub trait AggregratedKeyValue: Parameter { fn into_parts(self) -> (Self::AggregratedKey, Option); } +pub trait ParameterStore { + fn get(key: K) -> Option + where + K: Key + Into<::AggregratedKey>, + ::AggregratedValue: TryInto; +} + +pub struct ParameterStoreAdapter(sp_std::marker::PhantomData<(PS, KV)>); + +impl ParameterStore for ParameterStoreAdapter +where + PS: RuntimeParameterStore, + KV: AggregratedKeyValue + Into, + ::AggregratedKey: + Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, + ::AggregratedValue: + From<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue>, +{ + fn get(key: K) -> Option + where + K: Key + Into<::AggregratedKey>, + ::AggregratedValue: TryInto, + { + PS::get::(key) + } +} + /// Define parameters key value types. /// Example: /// @@ -134,7 +161,7 @@ macro_rules! define_parameters { $crate::parameters::frame_support::RuntimeDebug, $crate::parameters::scale_info::TypeInfo )] - $vis struct $key_name( $(pub $key_para)? ); + $vis struct $key_name $( (pub $key_para) )?; impl $crate::parameters::Key for $key_name { type Value = $value_type; @@ -367,7 +394,7 @@ mod tests { #[test] fn test_define_parameters_key_convert() { - let key1 = pallet1::Key1(); + let key1 = pallet1::Key1; let parameter_key: pallet1::ParametersKey = key1.clone().into(); let key1_2: pallet1::Key1 = parameter_key.clone().try_into().unwrap(); @@ -403,10 +430,10 @@ mod tests { fn test_define_parameters_aggregrated_key_value() { use crate::parameters::AggregratedKeyValue; - let kv1 = pallet1::Parameters::Key1(pallet1::Key1(), None); + let kv1 = pallet1::Parameters::Key1(pallet1::Key1, None); let (key1, value1) = kv1.clone().into_parts(); - assert_eq!(key1, pallet1::ParametersKey::Key1(pallet1::Key1())); + assert_eq!(key1, pallet1::ParametersKey::Key1(pallet1::Key1)); assert_eq!(value1, None); let kv2 = pallet1::Parameters::Key2(pallet1::Key2(1), Some(2)); @@ -420,7 +447,7 @@ mod tests { fn test_define_aggregrated_parameters_key_convert() { use codec::Encode; - let key1 = pallet1::Key1(); + let key1 = pallet1::Key1; let parameter_key: pallet1::ParametersKey = key1.clone().into(); let runtime_key: RuntimeParametersKey = parameter_key.clone().into(); @@ -445,12 +472,12 @@ mod tests { fn test_define_aggregrated_parameters_aggregrated_key_value() { use crate::parameters::AggregratedKeyValue; - let kv1 = RuntimeParameters::Pallet1(pallet1::Parameters::Key1(pallet1::Key1(), None)); + let kv1 = RuntimeParameters::Pallet1(pallet1::Parameters::Key1(pallet1::Key1, None)); let (key1, value1) = kv1.clone().into_parts(); assert_eq!( key1, - RuntimeParametersKey::Pallet1(pallet1::ParametersKey::Key1(pallet1::Key1())) + RuntimeParametersKey::Pallet1(pallet1::ParametersKey::Key1(pallet1::Key1)) ); assert_eq!(value1, None); From ad3ee01356c7541dc019651b0200a71f936f0b41 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 5 Sep 2023 13:26:30 +1200 Subject: [PATCH 15/17] fix --- parameters/src/lib.rs | 10 +++--- traits/src/parameters.rs | 71 +++++++++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/parameters/src/lib.rs b/parameters/src/lib.rs index 58ffc3c5d..8ef2b7137 100644 --- a/parameters/src/lib.rs +++ b/parameters/src/lib.rs @@ -8,7 +8,7 @@ use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; use frame_support::traits::EnsureOriginWithArg; -use orml_traits::parameters::{AggregratedKeyValue, Key, RuntimeParameterStore}; +use orml_traits::parameters::{AggregratedKeyValue, Into2, Key, RuntimeParameterStore, TryInto2}; mod mock; mod tests; @@ -85,15 +85,15 @@ impl RuntimeParameterStore for Pallet { KV: AggregratedKeyValue, K: Key + Into<::AggregratedKey>, ::AggregratedKey: - Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, + Into2<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue: - TryInto<::AggregratedValue>, + TryInto2<::AggregratedValue>, ::AggregratedValue: TryInto, { let key: ::AggregratedKey = key.into(); - let val = Parameters::::get(key.into()); + let val = Parameters::::get(key.into2()); val.and_then(|v| { - let val: ::AggregratedValue = v.try_into().ok()?; + let val: ::AggregratedValue = v.try_into2().ok()?; let val: K::WrappedValue = val.try_into().ok()?; let val = val.into(); Some(val) diff --git a/traits/src/parameters.rs b/traits/src/parameters.rs index d468bde9e..a3c66b0f1 100644 --- a/traits/src/parameters.rs +++ b/traits/src/parameters.rs @@ -16,9 +16,9 @@ pub trait RuntimeParameterStore { KV: AggregratedKeyValue, K: Key + Into<::AggregratedKey>, ::AggregratedKey: - Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, + Into2<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, <::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue: - TryInto<::AggregratedValue>, + TryInto2<::AggregratedValue>, ::AggregratedValue: TryInto; } @@ -46,11 +46,11 @@ pub struct ParameterStoreAdapter(sp_std::marker::PhantomData<(PS, KV)>); impl ParameterStore for ParameterStoreAdapter where PS: RuntimeParameterStore, - KV: AggregratedKeyValue + Into, + KV: AggregratedKeyValue, ::AggregratedKey: - Into<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, + Into2<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedKey>, ::AggregratedValue: - From<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue>, + TryFrom2<<::AggregratedKeyValue as AggregratedKeyValue>::AggregratedValue>, { fn get(key: K) -> Option where @@ -61,6 +61,54 @@ where } } +// workaround for rust bug https://github.com/rust-lang/rust/issues/51445 +mod workaround { + pub trait From2: Sized { + #[must_use] + fn from2(value: T) -> Self; + } + + pub trait Into2: Sized { + #[must_use] + fn into2(self) -> T; + } + + impl Into2 for T + where + U: From2, + { + #[inline] + fn into2(self) -> U { + U::from2(self) + } + } + + pub trait TryInto2: Sized { + type Error; + + fn try_into2(self) -> Result; + } + + pub trait TryFrom2: Sized { + type Error; + + fn try_from2(value: T) -> Result; + } + + impl TryInto2 for T + where + U: TryFrom2, + { + type Error = U::Error; + + #[inline] + fn try_into2(self) -> Result { + U::try_from2(self) + } + } +} +pub use workaround::*; + /// Define parameters key value types. /// Example: /// @@ -344,16 +392,16 @@ macro_rules! define_aggregrated_parameters { } $( - impl From<<$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedKey> for [<$name Key>] { - fn from(key: <$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedKey) -> Self { + impl $crate::parameters::From2<<$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedKey> for [<$name Key>] { + fn from2(key: <$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedKey) -> Self { [<$name Key>]::$parameter_name(key) } } - impl TryFrom<[<$name Value>]> for <$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedValue { + impl $crate::parameters::TryFrom2<[<$name Value>]> for <$parameter_type as $crate::parameters::AggregratedKeyValue>::AggregratedValue { type Error = (); - fn try_from(value: [<$name Value>]) -> Result { + fn try_from2(value: [<$name Value>]) -> Result { match value { [<$name Value>]::$parameter_name(value) => Ok(value), _ => Err(()), @@ -445,11 +493,12 @@ mod tests { #[test] fn test_define_aggregrated_parameters_key_convert() { + use crate::parameters::workaround::Into2; use codec::Encode; let key1 = pallet1::Key1; let parameter_key: pallet1::ParametersKey = key1.clone().into(); - let runtime_key: RuntimeParametersKey = parameter_key.clone().into(); + let runtime_key: RuntimeParametersKey = parameter_key.clone().into2(); assert_eq!( runtime_key, @@ -459,7 +508,7 @@ mod tests { let key2 = pallet2::Key2(1); let parameter_key: pallet2::ParametersKey = key2.clone().into(); - let runtime_key: RuntimeParametersKey = parameter_key.clone().into(); + let runtime_key: RuntimeParametersKey = parameter_key.clone().into2(); assert_eq!( runtime_key, From fd84744e4daca194aab1b11d26006e2a7a687924 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 5 Sep 2023 13:44:10 +1200 Subject: [PATCH 16/17] update comment --- parameters/src/mock.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/parameters/src/mock.rs b/parameters/src/mock.rs index 2b0806a0f..62ab382e9 100644 --- a/parameters/src/mock.rs +++ b/parameters/src/mock.rs @@ -1,5 +1,3 @@ -//! Mocks for the gradually-update module. - #![cfg(test)] use frame_support::traits::EnsureOriginWithArg; From f9654d45de0c39c9001ecad276cca75f7c4418d1 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 5 Sep 2023 13:47:52 +1200 Subject: [PATCH 17/17] set weight --- parameters/src/weights.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parameters/src/weights.rs b/parameters/src/weights.rs index 23c06f6fb..4959e4349 100644 --- a/parameters/src/weights.rs +++ b/parameters/src/weights.rs @@ -12,6 +12,6 @@ pub trait WeightInfo { impl WeightInfo for () { fn set_parameter() -> Weight { - Weight::zero() + RocksDbWeight::get().reads_writes(2, 1) } }