This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Split off System random functions into a new Randomness module #3699
Merged
Merged
Changes from 5 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
80f98b1
split off system randomness functions into a new module
b6dbab6
bump spec and impl version
5c59f76
Move randomness to bottom of construct_runtime calls, move initializa…
81d082f
Update srml/randomness/Cargo.toml
expenses fb41b38
Update srml/randomness/src/lib.rs
expenses 6fafe55
Update srml/randomness/src/lib.rs
expenses 8755b97
Update srml/randomness/Cargo.toml
expenses 12158e3
Improve system example
expenses 2bbc03a
Merge branch 'master' into randomness-module
expenses 71bf01c
Merge branch 'randomness-module' of https://github.com/expenses/subst…
expenses 394ad49
Update Cargo.lock
expenses 664fc3c
Fix randomness example
expenses e3761d2
Get rid of the stored index
expenses 907a29a
Add tests
expenses d98c7db
Add a random test
expenses 64e3c22
Merge branch 'master' into randomness-module
expenses 908d00f
Improve docs
expenses 15011d1
Merge branch 'randomness-module' of https://github.com/expenses/subst…
expenses fb96026
Fix executive test :^)
expenses bc15acc
Add a utility function to tests
expenses f7e4d4d
Update srml/randomness/Cargo.toml
expenses 8c8f70b
Update srml/randomness/src/lib.rs
expenses b71e627
Update srml/randomness/src/lib.rs
expenses 44a3cc1
Merge branch 'master' into randomness-module
expenses d491ac8
Change interpretation of block numbers
expenses 23b29af
Merge branch 'master' into randomness-module
expenses bb84094
rename crate
expenses 3a28382
Merge branch 'randomness-module' of https://github.com/expenses/subst…
expenses 742b3db
refactor randomess module usage
expenses 8388726
Merge branch 'master' of https://github.com/paritytech/substrate into…
expenses b5cd993
change random material len to a const
expenses 0db8a8b
Update srml/randomness-collective-flip/src/lib.rs
expenses 6a88d62
Update srml/randomness-collective-flip/src/lib.rs
expenses File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| [package] | ||
| name = "srml-randomness" | ||
| version = "2.0.0" | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2018" | ||
|
|
||
| [dependencies] | ||
| safe-mix = { version = "1.0", default-features = false} | ||
| codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] } | ||
| sr-primitives = { path = "../../core/sr-primitives", default-features = false } | ||
| support = { package = "srml-support", path = "../support", default-features = false } | ||
| system = { package = "srml-system", path = "../system", default-features = false } | ||
| rstd = { package = "sr-std", path = "../../core/sr-std", default-features = false } | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "safe-mix/std", | ||
| "system/std", | ||
| "codec/std", | ||
| "support/std", | ||
| "sr-primitives/std", | ||
| "rstd/std", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Copyright 2019 Parity Technologies (UK) Ltd. | ||
| // This file is part of Substrate. | ||
|
|
||
| // Substrate is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Substrate is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! # Randomness Module | ||
| //! | ||
| //! The Randomness module provides a [`random`](./struct.Module.html#method.random) function that | ||
| //! generates low-influence random values based on the block hashes from the previous 81 blocks. | ||
expenses marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! | ||
| //! ## Public Functions | ||
| //! | ||
| //! See the [`Module`](./struct.Module.html) struct for details of publicly available functions. | ||
| //! | ||
| //! ## Usage | ||
| //! | ||
| //! ### Prerequisites | ||
| //! | ||
| //! Import the Randomness module and derive your module's configuration trait from the system trait. | ||
| //! | ||
| //! ### Example - Get random seed for the current block | ||
| //! | ||
| //! ``` | ||
| //! use support::{decl_module, dispatch::Result}; | ||
| //! | ||
| //! trait Trait: system::Trait {} | ||
| //! | ||
| //! decl_module! { | ||
| //! pub struct Module<T: Trait> for enum Call where origin: T::Origin { | ||
| //! pub fn random_module_example(origin) -> Result { | ||
| //! let _random_seed = <srml_random::Module<T>>::random_seed(); | ||
| //! Ok(()) | ||
| //! } | ||
| //! } | ||
| //! } | ||
| //! # fn main() { } | ||
| //! ``` | ||
|
|
||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| use rstd::prelude::*; | ||
| use sr_primitives::traits::Hash; | ||
| use support::{decl_module, decl_storage}; | ||
| use safe_mix::TripletMix; | ||
| use codec::Encode; | ||
| use system::Trait; | ||
|
|
||
| decl_module! { | ||
| pub struct Module<T: Trait> for enum Call where origin: T::Origin { | ||
bkchr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fn on_initialize() { | ||
| let parent_hash = <system::Module<T>>::parent_hash(); | ||
|
|
||
| <RandomMaterial<T>>::mutate(|&mut(ref mut index, ref mut values)| if values.len() < 81 { | ||
| values.push(parent_hash) | ||
| } else { | ||
| values[*index as usize] = parent_hash; | ||
| *index = (*index + 1) % 81; | ||
expenses marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| decl_storage! { | ||
| trait Store for Module<T: Trait> as System { | ||
| /// Series of block headers from the last 81 blocks that acts as random seed material. This is arranged as a | ||
| /// ring buffer with the `i8` prefix being the index into the `Vec` of the oldest hash. | ||
| RandomMaterial get(random_material): (i8, Vec<T::Hash>); | ||
| } | ||
| } | ||
|
|
||
| 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(&[][..]) | ||
| } | ||
|
|
||
| /// Get a low-influence "random" value. | ||
| /// | ||
| /// Being a deterministic block chain, 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 different result to other | ||
| /// callers of this function; use it like `random(&b"my context"[..])`. | ||
| /// | ||
| /// This is initially implemented through a low-influence "triplet mix" | ||
| /// convolution of previous block hash values. In the future it will be | ||
| /// generated from a secure verifiable random function (VRF). | ||
| /// | ||
| /// ### Security Notes | ||
| /// | ||
| /// 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 in advance by the block producer of this block (and, | ||
| /// indeed, anyone who knows the block's `parent_hash`). However, it is | ||
| /// mostly impossible for the producer of this block *alone* to influence | ||
| /// the value of this hash. A sizable minority of dishonest and coordinating | ||
| /// block producers would be required in order to affect this value. If that | ||
| /// is an insufficient security guarantee then two things can be used to | ||
| /// improve this randomness: | ||
expenses marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// - Name, in advance, the block number whose random value will be used; | ||
| /// ensure your module retains a buffer of previous random values for its | ||
| /// subject and then index into these in order to obviate the ability of | ||
| /// your user to look up the parent hash and choose when to transact based | ||
| /// upon it. | ||
| /// - Require your user to first commit to an additional value by first | ||
| /// posting its hash. Require them to reveal the value to determine the | ||
| /// final result, hashing it with the output of this random function. This | ||
| /// reduces the ability of a cabal of block producers from conspiring | ||
| /// against individuals. | ||
| /// | ||
| /// 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 { | ||
| let (index, hash_series) = <RandomMaterial<T>>::get(); | ||
| if hash_series.len() > 0 { | ||
expenses marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Always the case after block 1 is initialised. | ||
| hash_series.iter() | ||
| .cycle() | ||
| .skip(index as usize) | ||
| .take(81) | ||
| .enumerate() | ||
| .map(|(i, h)| (i as i8, subject, h).using_encoded(T::Hashing::hash)) | ||
| .triplet_mix() | ||
| } else { | ||
| T::Hash::default() | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.