impl Randomness trait for Babe and remove unused RandomBeacon trait (#4886)

* impl Randomness trait for Babe and remove unused RandomBeacon trait

* bump runtime version
This commit is contained in:
Robert Habermeier
2020-02-11 11:07:14 +01:00
committed by GitHub
parent 0d45d12e3f
commit ea69238d03
4 changed files with 14 additions and 23 deletions
+9 -5
View File
@@ -23,10 +23,10 @@
pub use pallet_timestamp;
use sp_std::{result, prelude::*};
use frame_support::{decl_storage, decl_module, traits::FindAuthor, traits::Get};
use frame_support::{decl_storage, decl_module, traits::{FindAuthor, Get, Randomness as RandomnessT}};
use sp_timestamp::OnTimestampSet;
use sp_runtime::{generic::DigestItem, ConsensusEngineId, Perbill};
use sp_runtime::traits::{IsMember, SaturatedConversion, Saturating, RandomnessBeacon};
use sp_runtime::traits::{IsMember, SaturatedConversion, Saturating, Hash};
use sp_staking::{
SessionIndex,
offence::{Offence, Kind},
@@ -191,9 +191,13 @@ decl_module! {
}
}
impl<T: Trait> RandomnessBeacon for Module<T> {
fn random() -> [u8; VRF_OUTPUT_LENGTH] {
Self::randomness()
impl<T: Trait> RandomnessT<<T as frame_system::Trait>::Hash> for Module<T> {
fn random(subject: &[u8]) -> T::Hash {
let mut subject = subject.to_vec();
subject.reserve(VRF_OUTPUT_LENGTH);
subject.extend_from_slice(&Self::randomness()[..]);
<T as frame_system::Trait>::Hashing::hash(&subject[..])
}
}
+4 -1
View File
@@ -767,7 +767,10 @@ pub trait Randomness<Output> {
/// Get a "random" value
///
/// Being a deterministic blockchain, real randomness is difficult to come by. This gives you
/// something that approximates it. `subject` is a context identifier and allows you to get a
/// something that approximates it. At best, this will be randomness which was
/// hard to predict a long time ago, but that has become easy to predict recently.
///
/// `subject` is a context identifier and allows you to get a
/// different result to other callers of this function; use it like
/// `random(&b"my context"[..])`.
fn random(subject: &[u8]) -> Output;