Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Migrate pallet-randomness-collective-flip to pallet attribute macro #9061

Merged
7 commits merged into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down
8 changes: 4 additions & 4 deletions frame/randomness-collective-flip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
92 changes: 59 additions & 33 deletions frame/randomness-collective-flip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Config> for enum Call where origin: T::Origin {
//! #[weight = 0]
//! pub fn random_module_example(origin) -> dispatch::DispatchResult {
//! let _random_value = <pallet_randomness_collective_flip::Module<T>>::random(&b"my context"[..]);
//! Ok(())
//! }
//! }
//! #[pallet::pallet]
//! #[pallet::generate_store(pub(super) trait Store)]
//! pub struct Pallet<T>(_);
//!
//! #[pallet::config]
//! pub trait Config: frame_system::Config + pallet_randomness_collective_flip::Config {}
//!
//! #[pallet::call]
//! impl<T: Config> Pallet<T> {
//! #[pallet::weight(0)]
//! pub fn random_module_example(origin: OriginFor<T>) -> DispatchResult {
//! let _random_value = <pallet_randomness_collective_flip::Pallet<T>>::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;

Expand All @@ -73,9 +81,24 @@ fn block_number_to_index<T: Config>(block_number: T::BlockNumber) -> usize {
index.try_into().ok().expect("Something % 81 is always smaller than usize; qed")
}

decl_module! {
pub struct Module<T: Config> 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<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config {}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(block_number: T::BlockNumber) -> Weight {
let parent_hash = <frame_system::Pallet<T>>::parent_hash();

<RandomMaterial<T>>::mutate(|ref mut values| if values.len() < RANDOM_MATERIAL_LEN as usize {
Expand All @@ -86,20 +109,19 @@ decl_module! {
});

0
koushiro marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}

/// 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<T: Config> =
StorageValue<_, Vec<T::Hash>, ValueQuery>;
}

decl_storage! {
trait Store for Module<T: Config> 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<T::Hash>;
}
}

impl<T: Config> Randomness<T::Hash, T::BlockNumber> for Module<T> {
impl<T: Config> Randomness<T::Hash, T::BlockNumber> for Pallet<T> {
/// 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
Expand Down Expand Up @@ -140,11 +162,13 @@ impl<T: Config> Randomness<T::Hash, T::BlockNumber> for Module<T> {
mod tests {
use crate as pallet_randomness_collective_flip;
use super::*;
use sp_core::H256;

use sp_core::H256;
koushiro marked this conversation as resolved.
Show resolved Hide resolved
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, Header as _, IdentityLookup},
};

use frame_system::limits;
use frame_support::{parameter_types, traits::{Randomness, OnInitialize}};

Expand Down Expand Up @@ -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::<Test>().unwrap();
t.into()
Expand Down