Companion for substrate#11113 (#5406)

* runtime: use ParentBlockRandomness instead of CurrentBlockRandomness

* update lockfile for {"substrate"}

Co-authored-by: parity-processbot <>
This commit is contained in:
André Silva
2022-05-04 18:14:17 +01:00
committed by GitHub
parent 5246fc0195
commit 82cb3c0570
2 changed files with 179 additions and 174 deletions
+172 -171
View File
File diff suppressed because it is too large Load Diff
@@ -38,7 +38,7 @@ use frame_support::{
traits::Randomness, traits::Randomness,
}; };
use frame_system::pallet_prelude::*; use frame_system::pallet_prelude::*;
use pallet_babe::{self, CurrentBlockRandomness}; use pallet_babe::{self, ParentBlockRandomness};
use primitives::v2::{ use primitives::v2::{
BackedCandidate, CandidateHash, CandidateReceipt, CheckedDisputeStatementSet, BackedCandidate, CandidateHash, CandidateReceipt, CheckedDisputeStatementSet,
CheckedMultiDisputeStatementSet, CoreIndex, DisputeStatementSet, CheckedMultiDisputeStatementSet, CoreIndex, DisputeStatementSet,
@@ -1195,14 +1195,18 @@ pub(crate) fn assure_sanity_backed_candidates<
/// a const value, while emitting a warning. /// a const value, while emitting a warning.
fn compute_entropy<T: Config>(parent_hash: T::Hash) -> [u8; 32] { fn compute_entropy<T: Config>(parent_hash: T::Hash) -> [u8; 32] {
const CANDIDATE_SEED_SUBJECT: [u8; 32] = *b"candidate-seed-selection-subject"; const CANDIDATE_SEED_SUBJECT: [u8; 32] = *b"candidate-seed-selection-subject";
let vrf_random = CurrentBlockRandomness::<T>::random(&CANDIDATE_SEED_SUBJECT[..]).0; // NOTE: this is slightly gameable since this randomness was already public
// by the previous block, while for the block author this randomness was
// known 2 epochs ago. it is marginally better than using the parent block
// hash since it's harder to influence the VRF output than the block hash.
let vrf_random = ParentBlockRandomness::<T>::random(&CANDIDATE_SEED_SUBJECT[..]).0;
let mut entropy: [u8; 32] = CANDIDATE_SEED_SUBJECT.clone(); let mut entropy: [u8; 32] = CANDIDATE_SEED_SUBJECT.clone();
if let Some(vrf_random) = vrf_random { if let Some(vrf_random) = vrf_random {
entropy.as_mut().copy_from_slice(vrf_random.as_ref()); entropy.as_mut().copy_from_slice(vrf_random.as_ref());
} else { } else {
// in case there is no VRF randomness present, we utilize the relay parent // in case there is no VRF randomness present, we utilize the relay parent
// as seed, it's better than a static value. // as seed, it's better than a static value.
log::warn!(target: LOG_TARGET, "CurrentBlockRandomness did not provide entropy"); log::warn!(target: LOG_TARGET, "ParentBlockRandomness did not provide entropy");
entropy.as_mut().copy_from_slice(parent_hash.as_ref()); entropy.as_mut().copy_from_slice(parent_hash.as_ref());
} }
entropy entropy