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
+18
View File
@@ -53,3 +53,21 @@ impl frame_support::traits::PalletInfo for PanicPalletInfo {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");
}
}
/// Provides an implementation of [`Randomness`] that should only be used in tests!
pub struct TestRandomness<T>(sp_std::marker::PhantomData<T>);
impl<Output: codec::Decode + Default, T> frame_support::traits::Randomness<Output, T::BlockNumber>
for TestRandomness<T>
where
T: frame_system::Config,
{
fn random(subject: &[u8]) -> (Output, T::BlockNumber) {
use sp_runtime::traits::TrailingZeroInput;
(
Output::decode(&mut TrailingZeroInput::new(subject)).unwrap_or_default(),
frame_system::Module::<T>::block_number(),
)
}
}