runtime: use babe as randomness source (#2504)

* runtime: use babe as randomness source

* runtime: fix randomness api

* bridges: fix random_seed

* parachains: use mock TestRandomness from frame_support_test

* parachains: use mock TestRandomness from frame_support_test

* runtime: update randomness source in test-runtime

* runtime: remove unused import

* parachains: add todo to audit usage of randomness api

* "Update Substrate"

Co-authored-by: parity-processbot <>
This commit is contained in:
André Silva
2021-03-10 17:22:59 +00:00
committed by GitHub
parent 9331e06eda
commit b360360544
14 changed files with 201 additions and 179 deletions
+173 -144
View File
File diff suppressed because it is too large Load Diff
@@ -458,7 +458,7 @@ impl_runtime_apis! {
} }
fn random_seed() -> <Block as BlockT>::Hash { fn random_seed() -> <Block as BlockT>::Hash {
RandomnessCollectiveFlip::random_seed() RandomnessCollectiveFlip::random_seed().0
} }
} }
@@ -569,7 +569,7 @@ impl_runtime_apis! {
} }
fn random_seed() -> <Block as BlockT>::Hash { fn random_seed() -> <Block as BlockT>::Hash {
RandomnessCollectiveFlip::random_seed() RandomnessCollectiveFlip::random_seed().0
} }
} }
+1
View File
@@ -48,6 +48,7 @@ sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-babe = { git = "https://github.com/paritytech/substrate", branch = "master" } pallet-babe = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-election-providers = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-election-providers = { git = "https://github.com/paritytech/substrate", branch = "master" }
frame-support-test = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-randomness-collective-flip = { git = "https://github.com/paritytech/substrate", branch = "master" } pallet-randomness-collective-flip = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-staking-reward-curve = { git = "https://github.com/paritytech/substrate", branch = "master" } pallet-staking-reward-curve = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-treasury = { git = "https://github.com/paritytech/substrate", branch = "master" } pallet-treasury = { git = "https://github.com/paritytech/substrate", branch = "master" }
+3 -10
View File
@@ -262,9 +262,10 @@ mod tests {
}; };
use frame_system::limits; use frame_system::limits;
use frame_support::{ use frame_support::{
traits::{Randomness, OnInitialize, OnFinalize}, traits::{OnInitialize, OnFinalize},
assert_ok, assert_noop, parameter_types, assert_ok, assert_noop, parameter_types,
}; };
use frame_support_test::TestRandomness;
use keyring::Sr25519Keyring; use keyring::Sr25519Keyring;
use runtime_parachains::{ use runtime_parachains::{
initializer, configuration, inclusion, session_info, scheduler, dmp, ump, hrmp, shared, initializer, configuration, inclusion, session_info, scheduler, dmp, ump, hrmp, shared,
@@ -511,16 +512,8 @@ mod tests {
impl session_info::Config for Test { } impl session_info::Config for Test { }
pub struct TestRandomness;
impl Randomness<H256> for TestRandomness {
fn random(_subject: &[u8]) -> H256 {
Default::default()
}
}
impl initializer::Config for Test { impl initializer::Config for Test {
type Randomness = TestRandomness; type Randomness = TestRandomness<Self>;
} }
impl scheduler::Config for Test { } impl scheduler::Config for Test { }
+3 -2
View File
@@ -54,7 +54,7 @@ pub trait Config: frame_system::Config {
type LeasePeriod: Get<Self::BlockNumber>; type LeasePeriod: Get<Self::BlockNumber>;
/// Something that provides randomness in the runtime. /// Something that provides randomness in the runtime.
type Randomness: Randomness<Self::Hash>; type Randomness: Randomness<Self::Hash, Self::BlockNumber>;
} }
/// Parachain registration API. /// Parachain registration API.
@@ -582,7 +582,8 @@ impl<T: Config> Module<T> {
let ending_period = T::EndingPeriod::get(); let ending_period = T::EndingPeriod::get();
if early_end + ending_period == now { if early_end + ending_period == now {
// Just ended! // Just ended!
let offset = T::BlockNumber::decode(&mut T::Randomness::random_seed().as_ref()) let (seed, _) = T::Randomness::random_seed();
let offset = T::BlockNumber::decode(&mut seed.as_ref())
.expect("secure hashes always bigger than block numbers; qed") % ending_period; .expect("secure hashes always bigger than block numbers; qed") % ending_period;
let res = <Winning<T>>::get(offset).unwrap_or_default(); let res = <Winning<T>>::get(offset).unwrap_or_default();
let mut i = T::BlockNumber::zero(); let mut i = T::BlockNumber::zero();
+2 -2
View File
@@ -818,7 +818,7 @@ parameter_types! {
impl pallet_society::Config for Runtime { impl pallet_society::Config for Runtime {
type Event = Event; type Event = Event;
type Currency = Balances; type Currency = Balances;
type Randomness = RandomnessCollectiveFlip; type Randomness = pallet_babe::RandomnessFromOneEpochAgo<Runtime>;
type CandidateDeposit = CandidateDeposit; type CandidateDeposit = CandidateDeposit;
type WrongSideDeduction = WrongSideDeduction; type WrongSideDeduction = WrongSideDeduction;
type MaxStrikes = MaxStrikes; type MaxStrikes = MaxStrikes;
@@ -1140,7 +1140,7 @@ sp_api::impl_runtime_apis! {
} }
fn random_seed() -> <Block as BlockT>::Hash { fn random_seed() -> <Block as BlockT>::Hash {
RandomnessCollectiveFlip::random_seed() pallet_babe::RandomnessFromOneEpochAgo::<Runtime>::random_seed().0
} }
} }
+1
View File
@@ -49,6 +49,7 @@ keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substra
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-babe = { git = "https://github.com/paritytech/substrate", branch = "master" } pallet-babe = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "master" }
frame-support-test = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-randomness-collective-flip = { git = "https://github.com/paritytech/substrate", branch = "master" } pallet-randomness-collective-flip = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-staking-reward-curve = { git = "https://github.com/paritytech/substrate", branch = "master" } pallet-staking-reward-curve = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-treasury = { git = "https://github.com/paritytech/substrate", branch = "master" } pallet-treasury = { git = "https://github.com/paritytech/substrate", branch = "master" }
@@ -81,7 +81,7 @@ pub trait Config:
+ hrmp::Config + hrmp::Config
{ {
/// A randomness beacon. /// A randomness beacon.
type Randomness: Randomness<Self::Hash>; type Randomness: Randomness<Self::Hash, Self::BlockNumber>;
} }
decl_storage! { decl_storage! {
@@ -181,7 +181,9 @@ impl<T: Config> Module<T> {
let random_seed = { let random_seed = {
let mut buf = [0u8; 32]; let mut buf = [0u8; 32];
let random_hash = T::Randomness::random(&b"paras"[..]); // TODO: audit usage of randomness API
// https://github.com/paritytech/polkadot/issues/2601
let (random_hash, _) = T::Randomness::random(&b"paras"[..]);
let len = sp_std::cmp::min(32, random_hash.as_ref().len()); let len = sp_std::cmp::min(32, random_hash.as_ref().len());
buf[..len].copy_from_slice(&random_hash.as_ref()[..len]); buf[..len].copy_from_slice(&random_hash.as_ref()[..len]);
buf buf
+3 -10
View File
@@ -22,7 +22,8 @@ use sp_runtime::traits::{
BlakeTwo256, IdentityLookup, BlakeTwo256, IdentityLookup,
}; };
use primitives::v1::{AuthorityDiscoveryId, Balance, BlockNumber, Header, ValidatorIndex}; use primitives::v1::{AuthorityDiscoveryId, Balance, BlockNumber, Header, ValidatorIndex};
use frame_support::{parameter_types, traits::Randomness as RandomnessT}; use frame_support::parameter_types;
use frame_support_test::TestRandomness;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use crate::{ use crate::{
@@ -54,14 +55,6 @@ frame_support::construct_runtime!(
} }
); );
pub struct TestRandomness;
impl RandomnessT<H256> for TestRandomness {
fn random(_subject: &[u8]) -> H256 {
Default::default()
}
}
parameter_types! { parameter_types! {
pub const BlockHashCount: u32 = 250; pub const BlockHashCount: u32 = 250;
pub BlockWeights: frame_system::limits::BlockWeights = pub BlockWeights: frame_system::limits::BlockWeights =
@@ -108,7 +101,7 @@ impl pallet_balances::Config for Test {
} }
impl crate::initializer::Config for Test { impl crate::initializer::Config for Test {
type Randomness = TestRandomness; type Randomness = TestRandomness<Self>;
} }
impl crate::configuration::Config for Test { } impl crate::configuration::Config for Test { }
+1 -1
View File
@@ -1133,7 +1133,7 @@ sp_api::impl_runtime_apis! {
} }
fn random_seed() -> <Block as BlockT>::Hash { fn random_seed() -> <Block as BlockT>::Hash {
RandomnessCollectiveFlip::random_seed() pallet_babe::RandomnessFromOneEpochAgo::<Runtime>::random_seed().0
} }
} }
+5 -3
View File
@@ -39,7 +39,9 @@ use runtime_parachains::{
runtime_api_impl::v1 as runtime_api_impl, runtime_api_impl::v1 as runtime_api_impl,
}; };
use frame_support::{ use frame_support::{
parameter_types, construct_runtime, traits::{KeyOwnerProofSystem, Filter, EnsureOrigin}, weights::Weight, construct_runtime, parameter_types,
traits::{EnsureOrigin, Filter, KeyOwnerProofSystem, Randomness},
weights::Weight,
}; };
use sp_runtime::{ use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys, create_runtime_str, generic, impl_opaque_keys,
@@ -587,7 +589,7 @@ impl parachains_inclusion_inherent::Config for Runtime {}
impl parachains_scheduler::Config for Runtime {} impl parachains_scheduler::Config for Runtime {}
impl parachains_initializer::Config for Runtime { impl parachains_initializer::Config for Runtime {
type Randomness = Babe; type Randomness = pallet_babe::RandomnessFromOneEpochAgo<Runtime>;
} }
impl paras_sudo_wrapper::Config for Runtime {} impl paras_sudo_wrapper::Config for Runtime {}
@@ -681,7 +683,7 @@ sp_api::impl_runtime_apis! {
} }
fn random_seed() -> <Block as BlockT>::Hash { fn random_seed() -> <Block as BlockT>::Hash {
Babe::randomness().into() pallet_babe::RandomnessFromOneEpochAgo::<Runtime>::random_seed().0
} }
} }
+2 -2
View File
@@ -470,7 +470,7 @@ impl parachains_inclusion::Config for Runtime {
impl parachains_inclusion_inherent::Config for Runtime {} impl parachains_inclusion_inherent::Config for Runtime {}
impl parachains_initializer::Config for Runtime { impl parachains_initializer::Config for Runtime {
type Randomness = Babe; type Randomness = pallet_babe::RandomnessFromOneEpochAgo<Runtime>;
} }
impl parachains_session_info::Config for Runtime {} impl parachains_session_info::Config for Runtime {}
@@ -617,7 +617,7 @@ sp_api::impl_runtime_apis! {
} }
fn random_seed() -> <Block as BlockT>::Hash { fn random_seed() -> <Block as BlockT>::Hash {
RandomnessCollectiveFlip::random_seed() RandomnessCollectiveFlip::random_seed().0
} }
} }
+1 -1
View File
@@ -851,7 +851,7 @@ sp_api::impl_runtime_apis! {
} }
fn random_seed() -> <Block as BlockT>::Hash { fn random_seed() -> <Block as BlockT>::Hash {
RandomnessCollectiveFlip::random_seed() pallet_babe::RandomnessFromOneEpochAgo::<Runtime>::random_seed().0
} }
} }