-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bring Randomness Recipe up to snuff #182
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
10c74b3
Fixes strait-forward compile errors, but doesn't update randomness api
JoshOrndorff b60ff60
Merge branch 'master' into start-updating-random
JoshOrndorff e7309f9
Pallet that compiles and probably partially works
JoshOrndorff 6981dd4
Code working
JoshOrndorff 4053bdf
typo
JoshOrndorff 2a51322
draft writeup
JoshOrndorff e85b144
add citations and rabbit hole section
JoshOrndorff e57c267
Address some feedback from review.
JoshOrndorff ac48c26
Review comments to code.
JoshOrndorff f560046
Some basic tests.
JoshOrndorff 149a23a
update links in the sidebar and content at the bottom
jimmychu0807 7a5a482
updated book.toml
jimmychu0807 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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,32 @@ | ||
[package] | ||
name = "randomness" | ||
version = "0.1.0" | ||
authors = ["4meta5"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
# external dependencies | ||
parity-scale-codec = { default-features = false, features = ['derive'], version = '1.1.0' } | ||
|
||
# Substrate pallet/frame dependencies | ||
frame-support = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-alpha.3', default_features = false } | ||
frame-system = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-alpha.3', default_features = false } | ||
sp-runtime = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-alpha.3', default_features = false } | ||
sp-std = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-alpha.3', default_features = false } | ||
sp-core = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-alpha.3', default_features = false } | ||
|
||
[dev-dependencies] | ||
sp-io = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-alpha.3' } | ||
pallet-randomness-collective-flip = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-alpha.3' } | ||
pallet-babe = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-alpha.3' } | ||
pallet-timestamp = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-alpha.3' } | ||
|
||
[features] | ||
default = ['std'] | ||
std = [ | ||
'parity-scale-codec/std', | ||
'frame-support/std', | ||
'frame-system/std', | ||
'sp-runtime/std', | ||
'sp-core/std', | ||
] |
This file contains 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,91 @@ | ||
//! Generating (insecure) randomness | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use sp_core::H256; | ||
use frame_support::{decl_event, decl_module, decl_storage, dispatch::DispatchResult, traits::Randomness}; | ||
use frame_system::{self as system, ensure_signed}; | ||
use parity_scale_codec::Encode; | ||
use sp_std::vec::Vec; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
|
||
/// The pallet's configuration trait. | ||
/// This trait includes two randomness sources. In production you will only ever need one. This pallet | ||
/// includes both merely to demonstrate both. | ||
pub trait Trait: system::Trait { | ||
type Event: From<Event> + Into<<Self as system::Trait>::Event>; | ||
|
||
/// Connection to Collective Flip pallet. Typically the type would be called something like | ||
/// `RandomnessSource` but because we are using two sources in this pallet, we will name | ||
/// them explicitly | ||
type CollectiveFlipRandomnessSource: Randomness<H256>; | ||
|
||
/// Connection to Babe pallet. Typically the type would be called something like | ||
/// `RandomnessSource` but because we are using two sources in this pallet, we will name | ||
/// them explicitly | ||
type BabeRandomnessSource: Randomness<H256>; | ||
} | ||
|
||
decl_storage! { | ||
trait Store for Module<T: Trait> as RandomnessPallet { | ||
/// A nonce to use as a subject when drawing randomness | ||
Nonce get(fn nonce): u32; | ||
} | ||
} | ||
|
||
decl_module! { | ||
pub struct Module<T: Trait> for enum Call where origin: T::Origin { | ||
fn deposit_event() = default; | ||
|
||
/// Grab a random seed and random value from the randomness collective flip pallet | ||
fn call_collective_flip(origin) -> DispatchResult { | ||
let _ = ensure_signed(origin)?; | ||
|
||
// Using a subject is recommended to prevent accidental re-use of the seed | ||
// (This does not add security or entropy) | ||
let subject = Self::encode_and_update_nonce(); | ||
|
||
let random_seed = T::CollectiveFlipRandomnessSource::random_seed(); | ||
let random_result = T::CollectiveFlipRandomnessSource::random(&subject); | ||
|
||
Self::deposit_event(Event::CollectiveFlip(random_seed, random_result)); | ||
Ok(()) | ||
} | ||
|
||
/// Grab a random seed and random value from the babe pallet | ||
fn call_babe_vrf(origin) -> DispatchResult { | ||
let _ = ensure_signed(origin)?; | ||
|
||
// Using a subject is recommended to prevent accidental re-use of the seed | ||
// (This does not add security or entropy) | ||
let subject = Self::encode_and_update_nonce(); | ||
|
||
let random_seed = T::BabeRandomnessSource::random_seed(); | ||
let random_result = T::BabeRandomnessSource::random(&subject); | ||
|
||
Self::deposit_event(Event::BabeVRF(random_seed, random_result)); | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
decl_event!( | ||
pub enum Event { | ||
/// Randomness taken from Collective Flip. First element is raw seed, second is using nonce. | ||
CollectiveFlip(H256, H256), | ||
/// Randomness taken from Babe VRF Outputs. First element is raw seed, second is using nonce. | ||
BabeVRF(H256, H256), | ||
} | ||
); | ||
|
||
impl<T: Trait> Module<T> { | ||
/// Reads the nonce from storage, increments the stored nonce, and returns | ||
/// the encoded nonce to the caller. | ||
fn encode_and_update_nonce() -> Vec<u8> { | ||
let nonce = Nonce::get(); | ||
Nonce::put(nonce.wrapping_add(1)); | ||
nonce.encode() | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made almost the same change in the PoW recipe. I wish we didn't have to do it this way, but I don't have any better idea.