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

Decouple randomness-collective-flip #3792

Merged
merged 6 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub use sr_primitives::BuildStorage;
pub use timestamp::Call as TimestampCall;
pub use balances::Call as BalancesCall;
pub use sr_primitives::{Permill, Perbill};
pub use support::{StorageValue, construct_runtime, parameter_types};
pub use support::{StorageValue, construct_runtime, parameter_types, traits::Randomness};
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need the trait here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Otherwise have this error

error[E0599]: no function or associated item named `random_seed` found for type `srml_randomness_collective_flip::Module<Runtime>` in the current scope
   --> node/runtime/src/lib.rs:594:30
    |
594 |             RandomnessCollectiveFlip::random_seed()
    |                                       ^^^^^^^^^^^ function or associated item not found in `srml_randomness_collective_flip::Module<Runtime>`
    |
    = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope, perhaps add a `use` for it:

Copy link
Member

Choose a reason for hiding this comment

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

Ahh it is used there. Okay :)


/// An index to a block.
pub type BlockNumber = u32;
Expand Down
5 changes: 3 additions & 2 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use rstd::prelude::*;
use support::{
construct_runtime, parameter_types, traits::{SplitTwoWays, Currency}
construct_runtime, parameter_types, traits::{SplitTwoWays, Currency, Randomness}
Copy link
Member

Choose a reason for hiding this comment

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

Same.

};
use primitives::u32_trait::{_1, _2, _3, _4};
use node_primitives::{
Expand Down Expand Up @@ -85,7 +85,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 174,
impl_version: 174,
impl_version: 175,
apis: RUNTIME_API_VERSIONS,
};

Expand Down Expand Up @@ -398,6 +398,7 @@ parameter_types! {
impl contracts::Trait for Runtime {
type Currency = Balances;
type Time = Timestamp;
type Randomness = RandomnessCollectiveFlip;
type Call = Call;
type Event = Event;
type DetermineContractAddress = contracts::SimpleAddressDeterminator<Runtime>;
Expand Down
4 changes: 1 addition & 3 deletions srml/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ rstd = { package = "sr-std", path = "../../core/sr-std", default-features = fals
sandbox = { package = "sr-sandbox", path = "../../core/sr-sandbox", default-features = false }
support = { package = "srml-support", path = "../support", default-features = false }
system = { package = "srml-system", path = "../system", default-features = false }
randomness-collective-flip = { package = "srml-randomness-collective-flip", path = "../randomness-collective-flip", default-features = false }
timestamp = { package = "srml-timestamp", path = "../timestamp", default-features = false }

[dev-dependencies]
wabt = "0.9.2"
Expand All @@ -27,6 +25,7 @@ hex-literal = "0.2.1"
balances = { package = "srml-balances", path = "../balances" }
hex = "0.3.2"
timestamp = { package = "srml-timestamp", path = "../timestamp" }
randomness-collective-flip = { package = "srml-randomness-collective-flip", path = "../randomness-collective-flip" }

[features]
default = ["std"]
Expand All @@ -35,7 +34,6 @@ std = [
"codec/std",
"primitives/std",
"sr-primitives/std",
"randomness-collective-flip/std",
"runtime-io/std",
"rstd/std",
"sandbox/std",
Expand Down
4 changes: 2 additions & 2 deletions srml/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::rent;

use rstd::prelude::*;
use sr_primitives::traits::{Bounded, CheckedAdd, CheckedSub, Zero};
use support::traits::{WithdrawReason, Currency, Time};
use support::traits::{WithdrawReason, Currency, Time, Randomness};

pub type AccountIdOf<T> = <T as system::Trait>::AccountId;
pub type CallOf<T> = <T as Trait>::Call;
Expand Down Expand Up @@ -753,7 +753,7 @@ where
}

fn random(&self, subject: &[u8]) -> SeedOf<T> {
randomness_collective_flip::Module::<T>::random(subject)
<T::Randomness as Randomness<T::Hash>>::random(subject)
xlc marked this conversation as resolved.
Show resolved Hide resolved
}

fn now(&self) -> &MomentOf<T> {
Expand Down
5 changes: 3 additions & 2 deletions srml/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ use sr_primitives::{
use support::dispatch::{Result, Dispatchable};
use support::{
Parameter, decl_module, decl_event, decl_storage, storage::child,
parameter_types,
parameter_types, IsSubType
};
use support::{traits::{OnFreeBalanceZero, OnUnbalanced, Currency, Get, Time}, IsSubType};
use support::traits::{OnFreeBalanceZero, OnUnbalanced, Currency, Get, Time, Randomness};
use system::{ensure_signed, RawOrigin, ensure_root};
use primitives::storage::well_known_keys::CHILD_STORAGE_KEY_PREFIX;

Expand Down Expand Up @@ -335,6 +335,7 @@ parameter_types! {
pub trait Trait: system::Trait {
type Currency: Currency<Self::AccountId>;
type Time: Time;
type Randomness: Randomness<Self::Hash>;

/// The outer call dispatch type.
type Call: Parameter + Dispatchable<Origin=<Self as system::Trait>::Origin> + IsSubType<Module<Self>, Self>;
Expand Down
2 changes: 2 additions & 0 deletions srml/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ parameter_types! {
impl Trait for Test {
type Currency = Balances;
type Time = Timestamp;
type Randomness = Randomness;
type Call = Call;
type DetermineContractAddress = DummyContractAddressFor;
type Event = MetaEvent;
Expand Down Expand Up @@ -187,6 +188,7 @@ type Balances = balances::Module<Test>;
type Timestamp = timestamp::Module<Test>;
type Contract = Module<Test>;
type System = system::Module<Test>;
type Randomness = randomness_collective_flip::Module<Test>;

pub struct DummyContractAddressFor;
impl ContractAddressFor<H256, u64> for DummyContractAddressFor {
Expand Down
37 changes: 14 additions & 23 deletions srml/randomness-collective-flip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
//! ### Example - Get random seed for the current block
//!
//! ```
//! use support::{decl_module, dispatch::Result};
//! use support::{decl_module, dispatch::Result, traits::Randomness};
//!
//! pub trait Trait: system::Trait {}
//!
Expand All @@ -54,7 +54,7 @@

use rstd::{prelude::*, convert::TryInto};
use sr_primitives::traits::Hash;
use support::{decl_module, decl_storage};
use support::{decl_module, decl_storage, traits::Randomness};
use safe_mix::TripletMix;
use codec::Encode;
use system::Trait;
Expand Down Expand Up @@ -91,16 +91,7 @@ decl_storage! {
}
}

impl<T: Trait> Module<T> {
/// Get the basic random seed.
///
/// In general you won't want to use this, but rather `Self::random` which allows you to give a
/// subject for the random result and whose value will be independently low-influence random
/// from any other such seeds.
pub fn random_seed() -> T::Hash {
Self::random(&[][..])
}

impl<T: Trait> Randomness<T::Hash> for Module<T> {
/// Get a low-influence "random" value.
///
/// Being a deterministic block chain, real randomness is difficult to come by. This gives you
Expand Down Expand Up @@ -138,7 +129,7 @@ impl<T: Trait> 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`.
pub fn random(subject: &[u8]) -> T::Hash {
fn random(subject: &[u8]) -> T::Hash {
let block_number = <system::Module<T>>::block_number();
let index = block_number_to_index::<T>(block_number);

Expand Down Expand Up @@ -166,7 +157,7 @@ mod tests {
Perbill, traits::{BlakeTwo256, OnInitialize, Header as _, IdentityLookup}, testing::Header,
set_and_run_with_externalities,
};
use support::{impl_outer_origin, parameter_types};
use support::{impl_outer_origin, parameter_types, traits::Randomness};

#[derive(Clone, PartialEq, Eq)]
pub struct Test;
Expand Down Expand Up @@ -202,7 +193,7 @@ mod tests {
}

type System = system::Module<Test>;
type Randomness = Module<Test>;
type RandomnessModule = Module<Test>;
xlc marked this conversation as resolved.
Show resolved Hide resolved

fn new_test_ext() -> runtime_io::TestExternalities {
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
Expand All @@ -221,7 +212,7 @@ mod tests {

for i in 1 .. (blocks + 1) {
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
Randomness::on_initialize(i);
RandomnessModule::on_initialize(i);

let header = System::finalize();
parent_hash = header.hash();
Expand All @@ -236,7 +227,7 @@ mod tests {

setup_blocks(38);

let random_material = Randomness::random_material();
let random_material = RandomnessModule::random_material();

assert_eq!(random_material.len(), 38);
assert_eq!(random_material[0], genesis_hash);
Expand All @@ -250,7 +241,7 @@ mod tests {

setup_blocks(81);

let random_material = Randomness::random_material();
let random_material = RandomnessModule::random_material();

assert_eq!(random_material.len(), 81);
assert_ne!(random_material[0], random_material[1]);
Expand All @@ -265,7 +256,7 @@ mod tests {

setup_blocks(162);

let random_material = Randomness::random_material();
let random_material = RandomnessModule::random_material();

assert_eq!(random_material.len(), 81);
assert_ne!(random_material[0], random_material[1]);
Expand All @@ -279,13 +270,13 @@ mod tests {
setup_blocks(162);

assert_eq!(System::block_number(), 162);
assert_eq!(Randomness::random_seed(), Randomness::random_seed());
assert_ne!(Randomness::random(b"random_1"), Randomness::random(b"random_2"));
assert_eq!(RandomnessModule::random_seed(), RandomnessModule::random_seed());
assert_ne!(RandomnessModule::random(b"random_1"), RandomnessModule::random(b"random_2"));

let random = Randomness::random_seed();
let random = RandomnessModule::random_seed();

assert_ne!(random, H256::zero());
assert!(!Randomness::random_material().contains(&random));
assert!(!RandomnessModule::random_material().contains(&random));
});
}
}
20 changes: 20 additions & 0 deletions srml/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,23 @@ pub trait InitializeMembers<AccountId> {
impl<T> InitializeMembers<T> for () {
fn initialize_members(_: &[T]) {}
}

// A trait that is able to provide randomness.
pub trait Randomness<Output> {
/// Get a "random" value
///
/// Being a deterministic block chain, real randomness is difficult to come by. This gives you
xlc marked this conversation as resolved.
Show resolved Hide resolved
/// something that approximates it. `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;

/// Get the basic random seed.
///
/// In general you won't want to use this, but rather `Self::random` which allows you to give a
/// subject for the random result and whose value will be independently low-influence random
/// from any other such seeds.
fn random_seed() -> Output {
Self::random(&[][..])
}
}