mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 14:01:02 +00:00
Migrate pallet-randomness-collective-flip to pallet attribute macro (#9061)
* migrate pallet-randomness-collective-flip to pallet attribute macro Signed-off-by: koushiro <koushiro.cqx@gmail.com> * fix some nits Signed-off-by: koushiro <koushiro.cqx@gmail.com> * remove some spacing things Signed-off-by: koushiro <koushiro.cqx@gmail.com> * remove space Signed-off-by: koushiro <koushiro.cqx@gmail.com> * use tabs Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
@@ -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"]
|
||||
|
||||
@@ -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 safe_mix::TripletMix;
|
||||
|
||||
use codec::Encode;
|
||||
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 frame_support::traits::Randomness;
|
||||
|
||||
const RANDOM_MATERIAL_LEN: u32 = 81;
|
||||
|
||||
@@ -73,8 +81,23 @@ 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 {
|
||||
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();
|
||||
|
||||
@@ -85,21 +108,20 @@ decl_module! {
|
||||
values[index] = parent_hash;
|
||||
});
|
||||
|
||||
0
|
||||
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<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
|
||||
@@ -140,13 +162,15 @@ 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_runtime::{
|
||||
testing::Header,
|
||||
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<Test>;
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user