more clear randomness API for BABE (#8180)

* more clear randomness API for BABE

* babe: move randomness utilities to its own file

* node: use babe::RandomnessFromOneEpochAgo in random_seed implementation

* frame-support: annotate randomness trait with block number

* pallet-randomness-collective-flip: fix for new randomness trait

* pallet-society: fix randomness usage

* pallet-lottery: fix randomness usage

* pallet-contracts: fix randomness usage

* pallet-babe: fix randomness usage

we need to track when the current and previous epoch started so that we
know the block number by each existing on-chain was known

* node: fix random_seed

* node-template: fix random_seed

* frame-support: extend docs

* babe: add test for epoch starting block number tracking

* babe: fix epoch randomness docs

* frame: add todos for dealing with randomness api changes

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Robert Habermeier
2021-03-10 10:31:49 -06:00
committed by GitHub
parent f3d4355a20
commit e2960c383e
18 changed files with 286 additions and 78 deletions
+1
View File
@@ -22,6 +22,7 @@ frame-system = { version = "3.0.0", default-features = false, path = "../system"
frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true }
[dev-dependencies]
frame-support-test = { version = "3.0.0", path = "../support/test" }
pallet-balances = { version = "3.0.0", path = "../balances" }
sp-core = { version = "3.0.0", path = "../../primitives/core" }
sp-io = { version = "3.0.0", path = "../../primitives/io" }
+4 -2
View File
@@ -85,7 +85,7 @@ pub trait Config: frame_system::Config {
type Currency: ReservableCurrency<Self::AccountId>;
/// Something that provides randomness in the runtime.
type Randomness: Randomness<Self::Hash>;
type Randomness: Randomness<Self::Hash, Self::BlockNumber>;
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
@@ -443,8 +443,10 @@ impl<T: Config> Module<T> {
// Note that there is potential bias introduced by using modulus operator.
// You should call this function with different seed values until the random
// number lies within `u32::MAX - u32::MAX % n`.
// TODO: deal with randomness freshness
// https://github.com/paritytech/substrate/issues/8311
fn generate_random_number(seed: u32) -> u32 {
let random_seed = T::Randomness::random(&(T::ModuleId::get(), seed).encode());
let (random_seed, _) = T::Randomness::random(&(T::ModuleId::get(), seed).encode());
let random_number = <u32>::decode(&mut random_seed.as_ref())
.expect("secure hashes should always be bigger than u32; qed");
random_number
+3 -2
View File
@@ -22,8 +22,9 @@ use crate as pallet_lottery;
use frame_support::{
parameter_types,
traits::{OnInitialize, OnFinalize, TestRandomness},
traits::{OnFinalize, OnInitialize},
};
use frame_support_test::TestRandomness;
use sp_core::H256;
use sp_runtime::{
Perbill,
@@ -103,7 +104,7 @@ impl Config for Test {
type ModuleId = LotteryModuleId;
type Call = Call;
type Currency = Balances;
type Randomness = TestRandomness;
type Randomness = TestRandomness<Self>;
type Event = Event;
type ManagerOrigin = EnsureRoot<u64>;
type MaxCalls = MaxCalls;