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
@@ -24,6 +24,7 @@ rand_chacha = { version = "0.2", default-features = false }
[dev-dependencies]
sp-core = { version = "3.0.0", path = "../../primitives/core" }
sp-io ={ version = "3.0.0", path = "../../primitives/io" }
frame-support-test = { version = "3.0.0", path = "../support/test" }
pallet-balances = { version = "3.0.0", path = "../balances" }
[features]
+7 -3
View File
@@ -283,7 +283,7 @@ pub trait Config<I=DefaultInstance>: 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 minimum amount of a deposit required for a bid to be made.
type CandidateDeposit: Get<BalanceOf<Self, I>>;
@@ -1309,7 +1309,9 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
let mut pot = <Pot<T, I>>::get();
// we'll need a random seed here.
let seed = T::Randomness::random(phrase);
// TODO: deal with randomness freshness
// https://github.com/paritytech/substrate/issues/8312
let (seed, _) = T::Randomness::random(phrase);
// seed needs to be guaranteed to be 32 bytes.
let seed = <[u8; 32]>::decode(&mut TrailingZeroInput::new(seed.as_ref()))
.expect("input is padded with zeroes; qed");
@@ -1565,7 +1567,9 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
// Start a new defender rotation
let phrase = b"society_challenge";
// we'll need a random seed here.
let seed = T::Randomness::random(phrase);
// TODO: deal with randomness freshness
// https://github.com/paritytech/substrate/issues/8312
let (seed, _) = T::Randomness::random(phrase);
// seed needs to be guaranteed to be 32 bytes.
let seed = <[u8; 32]>::decode(&mut TrailingZeroInput::new(seed.as_ref()))
.expect("input is padded with zeroes; qed");
+3 -2
View File
@@ -22,8 +22,9 @@ use crate as pallet_society;
use frame_support::{
parameter_types, ord_parameter_types,
traits::{OnInitialize, OnFinalize, TestRandomness},
traits::{OnInitialize, OnFinalize},
};
use frame_support_test::TestRandomness;
use sp_core::H256;
use sp_runtime::{
testing::Header,
@@ -104,7 +105,7 @@ impl pallet_balances::Config for Test {
impl Config for Test {
type Event = Event;
type Currency = pallet_balances::Module<Self>;
type Randomness = TestRandomness;
type Randomness = TestRandomness<Self>;
type CandidateDeposit = CandidateDeposit;
type WrongSideDeduction = WrongSideDeduction;
type MaxStrikes = MaxStrikes;