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

more clear randomness API for BABE #8180

Merged
16 commits merged into from
Mar 10, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
pallet-randomness-collective-flip: fix for new randomness trait
  • Loading branch information
andresilva committed Mar 5, 2021
commit 71499993de32cc204e9bdc62e91f9617a15e84b9
21 changes: 14 additions & 7 deletions frame/randomness-collective-flip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use sp_std::{prelude::*, convert::TryInto};
use sp_runtime::traits::Hash;
use sp_runtime::traits::{Hash, Saturating};
use frame_support::{
decl_module, decl_storage, traits::Randomness,
weights::Weight
Expand Down Expand Up @@ -99,7 +99,7 @@ decl_storage! {
}
}

impl<T: Config> Randomness<T::Hash> for Module<T> {
impl<T: Config> Randomness<T::Hash, T::BlockNumber> for Module<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 All @@ -110,14 +110,15 @@ impl<T: Config> Randomness<T::Hash> for Module<T> {
/// WARNING: Hashing the result of this function will remove any low-influence properties it has
/// and mean that all bits of the resulting value are entirely manipulatable by the author of
/// the parent block, who can determine the value of `parent_hash`.
fn random(subject: &[u8]) -> T::Hash {
fn random(subject: &[u8]) -> (T::Hash, T::BlockNumber) {
let block_number = <frame_system::Module<T>>::block_number();
let index = block_number_to_index::<T>(block_number);

let hash_series = <RandomMaterial<T>>::get();
if !hash_series.is_empty() {
let seed = if !hash_series.is_empty() {
// Always the case after block 1 is initialized.
hash_series.iter()
hash_series
.iter()
.cycle()
.skip(index)
.take(RANDOM_MATERIAL_LEN as usize)
Expand All @@ -126,7 +127,12 @@ impl<T: Config> Randomness<T::Hash> for Module<T> {
.triplet_mix()
} else {
T::Hash::default()
}
};

(
seed,
block_number.saturating_sub(RANDOM_MATERIAL_LEN.into()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit tricky but I think it's the safest thing we can ensure here.

)
}
}

Expand Down Expand Up @@ -272,8 +278,9 @@ mod tests {
assert_eq!(CollectiveFlip::random_seed(), CollectiveFlip::random_seed());
assert_ne!(CollectiveFlip::random(b"random_1"), CollectiveFlip::random(b"random_2"));

let random = CollectiveFlip::random_seed();
let (random, known_since) = CollectiveFlip::random_seed();

assert_eq!(known_since, 162 - RANDOM_MATERIAL_LEN as u64);
assert_ne!(random, H256::zero());
assert!(!CollectiveFlip::random_material().contains(&random));
});
Expand Down