From e82705c3076282b62706f798e066454e92c6d588 Mon Sep 17 00:00:00 2001 From: koushiro Date: Wed, 9 Jun 2021 22:42:16 +0800 Subject: [PATCH 1/5] migrate pallet-randomness-collective-flip to pallet attribute macro Signed-off-by: koushiro --- Cargo.lock | 1 - bin/node-template/runtime/src/lib.rs | 2 + bin/node/runtime/src/lib.rs | 2 + frame/contracts/src/tests.rs | 1 + frame/randomness-collective-flip/Cargo.toml | 8 +- frame/randomness-collective-flip/src/lib.rs | 92 +++++++++++++-------- 6 files changed, 68 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cc8557daad2f4..67c35e4cc980a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5380,7 +5380,6 @@ dependencies = [ "frame-system", "parity-scale-codec", "safe-mix", - "serde", "sp-core", "sp-io", "sp-runtime", diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index e51a190ae9a0d..f98517b91d24c 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -202,6 +202,8 @@ impl frame_system::Config for Runtime { type OnSetCode = (); } +impl pallet_randomness_collective_flip::Config for Runtime {} + impl pallet_aura::Config for Runtime { type AuthorityId = AuraId; } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 14bf16d19778e..3ef68801bf999 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -217,6 +217,8 @@ impl frame_system::Config for Runtime { type OnSetCode = (); } +impl pallet_randomness_collective_flip::Config for Runtime {} + impl pallet_utility::Config for Runtime { type Event = Event; type Call = Call; diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index e066a369af0be..3e687643cdc8a 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -235,6 +235,7 @@ impl frame_system::Config for Test { type SS58Prefix = (); type OnSetCode = (); } +impl pallet_randomness_collective_flip::Config for Test {} impl pallet_balances::Config for Test { type MaxLocks = (); type MaxReserves = (); diff --git a/frame/randomness-collective-flip/Cargo.toml b/frame/randomness-collective-flip/Cargo.toml index ad9bcb97837db..5ae350ffcac11 100644 --- a/frame/randomness-collective-flip/Cargo.toml +++ b/frame/randomness-collective-flip/Cargo.toml @@ -16,23 +16,23 @@ targets = ["x86_64-unknown-linux-gnu"] safe-mix = { version = "1.0", default-features = false } codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } + frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } -sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-io = { version = "3.0.0", path = "../../primitives/io" } -serde = { version = "1.0.101" } [features] default = ["std"] std = [ "safe-mix/std", - "frame-system/std", "codec/std", - "frame-support/std", "sp-runtime/std", "sp-std/std", + "frame-system/std", + "frame-support/std", ] try-runtime = ["frame-support/try-runtime"] diff --git a/frame/randomness-collective-flip/src/lib.rs b/frame/randomness-collective-flip/src/lib.rs index 724605c6238b6..f38648a23e3d9 100644 --- a/frame/randomness-collective-flip/src/lib.rs +++ b/frame/randomness-collective-flip/src/lib.rs @@ -37,33 +37,41 @@ //! ### Example - Get random seed for the current block //! //! ``` -//! use frame_support::{decl_module, dispatch, traits::Randomness}; +//! use frame_support::traits::Randomness; //! -//! pub trait Config: frame_system::Config {} +//! #[frame_support::pallet] +//! pub mod pallet { +//! use frame_support::pallet_prelude::*; +//! use frame_system::pallet_prelude::*; +//! use super::*; //! -//! decl_module! { -//! pub struct Module for enum Call where origin: T::Origin { -//! #[weight = 0] -//! pub fn random_module_example(origin) -> dispatch::DispatchResult { -//! let _random_value = >::random(&b"my context"[..]); -//! Ok(()) -//! } -//! } +//! #[pallet::pallet] +//! #[pallet::generate_store(pub(super) trait Store)] +//! pub struct Pallet(_); +//! +//! #[pallet::config] +//! pub trait Config: frame_system::Config + pallet_randomness_collective_flip::Config {} +//! +//! #[pallet::call] +//! impl Pallet { +//! #[pallet::weight(0)] +//! pub fn random_module_example(origin: OriginFor) -> DispatchResult { +//! let _random_value = >::random(&b"my context"[..]); +//! Ok(()) +//! } +//! } //! } //! # fn main() { } //! ``` #![cfg_attr(not(feature = "std"), no_std)] -use sp_std::{prelude::*, convert::TryInto}; -use sp_runtime::traits::{Hash, Saturating}; -use frame_support::{ - decl_module, decl_storage, traits::Randomness, - weights::Weight -}; use safe_mix::TripletMix; + use codec::Encode; -use frame_system::Config; +use sp_std::{prelude::*, convert::TryInto}; +use sp_runtime::traits::{Hash, Saturating}; +use frame_support::traits::Randomness; const RANDOM_MATERIAL_LEN: u32 = 81; @@ -73,9 +81,24 @@ fn block_number_to_index(block_number: T::BlockNumber) -> usize { index.try_into().ok().expect("Something % 81 is always smaller than usize; qed") } -decl_module! { - pub struct Module for enum Call where origin: T::Origin { - fn on_initialize(block_number: T::BlockNumber) -> Weight { +pub use pallet::*; + +#[frame_support::pallet] +pub mod pallet { + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use super::*; + + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config {} + + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_initialize(block_number: T::BlockNumber) -> Weight { let parent_hash = >::parent_hash(); >::mutate(|ref mut values| if values.len() < RANDOM_MATERIAL_LEN as usize { @@ -86,20 +109,19 @@ decl_module! { }); 0 - } - } + } + } + + /// Series of block headers from the last 81 blocks that acts as random seed material. This + /// is arranged as a ring buffer with `block_number % 81` being the index into the `Vec` of + /// the oldest hash. + #[pallet::storage] + #[pallet::getter(fn random_material)] + pub(super) type RandomMaterial = + StorageValue<_, Vec, ValueQuery>; } -decl_storage! { - trait Store for Module as RandomnessCollectiveFlip { - /// Series of block headers from the last 81 blocks that acts as random seed material. This - /// is arranged as a ring buffer with `block_number % 81` being the index into the `Vec` of - /// the oldest hash. - RandomMaterial get(fn random_material): Vec; - } -} - -impl Randomness for Module { +impl Randomness for Pallet { /// This randomness uses a low-influence function, drawing upon the block hashes from the /// previous 81 blocks. Its result for any given subject will be known far in advance by anyone /// observing the chain. Any block producer has significant influence over their block hashes @@ -140,11 +162,13 @@ impl Randomness for Module { mod tests { use crate as pallet_randomness_collective_flip; use super::*; - use sp_core::H256; + + use sp_core::H256; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, Header as _, IdentityLookup}, }; + use frame_system::limits; use frame_support::{parameter_types, traits::{Randomness, OnInitialize}}; @@ -196,6 +220,8 @@ mod tests { type OnSetCode = (); } + impl pallet_randomness_collective_flip::Config for Test {} + fn new_test_ext() -> sp_io::TestExternalities { let t = frame_system::GenesisConfig::default().build_storage::().unwrap(); t.into() From f99fe2ac51daf248b42438cce09c6a29e429e5c3 Mon Sep 17 00:00:00 2001 From: koushiro Date: Thu, 10 Jun 2021 10:16:22 +0800 Subject: [PATCH 2/5] fix some nits Signed-off-by: koushiro --- frame/randomness-collective-flip/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/randomness-collective-flip/src/lib.rs b/frame/randomness-collective-flip/src/lib.rs index f38648a23e3d9..dd70f6abb4070 100644 --- a/frame/randomness-collective-flip/src/lib.rs +++ b/frame/randomness-collective-flip/src/lib.rs @@ -108,7 +108,7 @@ pub mod pallet { values[index] = parent_hash; }); - 0 + T::DbWeight::get().reads_writes(1, 1) } } From 01d7001ea6015d633998a46fb47deed71d9265c1 Mon Sep 17 00:00:00 2001 From: koushiro Date: Thu, 10 Jun 2021 10:19:46 +0800 Subject: [PATCH 3/5] remove some spacing things Signed-off-by: koushiro --- frame/randomness-collective-flip/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/randomness-collective-flip/src/lib.rs b/frame/randomness-collective-flip/src/lib.rs index dd70f6abb4070..4a413710c7742 100644 --- a/frame/randomness-collective-flip/src/lib.rs +++ b/frame/randomness-collective-flip/src/lib.rs @@ -117,7 +117,7 @@ pub mod pallet { /// the oldest hash. #[pallet::storage] #[pallet::getter(fn random_material)] - pub(super) type RandomMaterial = + pub(super) type RandomMaterial = StorageValue<_, Vec, ValueQuery>; } @@ -163,7 +163,7 @@ mod tests { use crate as pallet_randomness_collective_flip; use super::*; - use sp_core::H256; + use sp_core::H256; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, Header as _, IdentityLookup}, @@ -220,7 +220,7 @@ mod tests { type OnSetCode = (); } - impl pallet_randomness_collective_flip::Config for Test {} + impl pallet_randomness_collective_flip::Config for Test {} fn new_test_ext() -> sp_io::TestExternalities { let t = frame_system::GenesisConfig::default().build_storage::().unwrap(); From 7044e73efb312c3034d1f37666de71f790017c73 Mon Sep 17 00:00:00 2001 From: koushiro Date: Thu, 10 Jun 2021 10:25:48 +0800 Subject: [PATCH 4/5] remove space Signed-off-by: koushiro --- frame/randomness-collective-flip/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/randomness-collective-flip/src/lib.rs b/frame/randomness-collective-flip/src/lib.rs index 4a413710c7742..cf3f7f3bf9be5 100644 --- a/frame/randomness-collective-flip/src/lib.rs +++ b/frame/randomness-collective-flip/src/lib.rs @@ -220,7 +220,7 @@ mod tests { type OnSetCode = (); } - impl pallet_randomness_collective_flip::Config for Test {} + impl pallet_randomness_collective_flip::Config for Test {} fn new_test_ext() -> sp_io::TestExternalities { let t = frame_system::GenesisConfig::default().build_storage::().unwrap(); From 95585fb8ddcaa8f7cf61cb653f72c990e5739bb0 Mon Sep 17 00:00:00 2001 From: koushiro Date: Thu, 10 Jun 2021 15:33:47 +0800 Subject: [PATCH 5/5] use tabs Signed-off-by: koushiro --- frame/randomness-collective-flip/src/lib.rs | 36 ++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/frame/randomness-collective-flip/src/lib.rs b/frame/randomness-collective-flip/src/lib.rs index cf3f7f3bf9be5..3285addc5bf48 100644 --- a/frame/randomness-collective-flip/src/lib.rs +++ b/frame/randomness-collective-flip/src/lib.rs @@ -85,20 +85,20 @@ pub use pallet::*; #[frame_support::pallet] pub mod pallet { - use frame_support::pallet_prelude::*; - use frame_system::pallet_prelude::*; - use super::*; + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use super::*; - #[pallet::pallet] - #[pallet::generate_store(pub(super) trait Store)] - pub struct Pallet(_); + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); - #[pallet::config] - pub trait Config: frame_system::Config {} + #[pallet::config] + pub trait Config: frame_system::Config {} - #[pallet::hooks] - impl Hooks> for Pallet { - fn on_initialize(block_number: T::BlockNumber) -> Weight { + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_initialize(block_number: T::BlockNumber) -> Weight { let parent_hash = >::parent_hash(); >::mutate(|ref mut values| if values.len() < RANDOM_MATERIAL_LEN as usize { @@ -109,16 +109,16 @@ pub mod pallet { }); T::DbWeight::get().reads_writes(1, 1) - } - } + } + } /// Series of block headers from the last 81 blocks that acts as random seed material. This /// is arranged as a ring buffer with `block_number % 81` being the index into the `Vec` of /// the oldest hash. - #[pallet::storage] - #[pallet::getter(fn random_material)] - pub(super) type RandomMaterial = - StorageValue<_, Vec, ValueQuery>; + #[pallet::storage] + #[pallet::getter(fn random_material)] + pub(super) type RandomMaterial = + StorageValue<_, Vec, ValueQuery>; } impl Randomness for Pallet { @@ -169,8 +169,8 @@ mod tests { traits::{BlakeTwo256, Header as _, IdentityLookup}, }; - use frame_system::limits; use frame_support::{parameter_types, traits::{Randomness, OnInitialize}}; + use frame_system::limits; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock;