Skip to content
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 12 commits into from
Mar 23, 2020
6,500 changes: 3,187 additions & 3,313 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ members = [
"pallets/default-instance",
"pallets/double-map",
"pallets/execution-schedule",
# "pallets/gen-random",
"pallets/generic-event",
"pallets/hello-substrate",
"pallets/last-caller",
"pallets/linked-map",
"pallets/lockable-currency",
"pallets/offchain-demo",
"pallets/randomness",
"pallets/reservable-currency",
"pallets/simple-event",
"pallets/simple-map",
Expand Down
5 changes: 4 additions & 1 deletion book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ theme = "src/theme"

[output.linkcheck]
follow-web-links = true
exclude = [ "www4\\.comp\\.polyu\\.edu\\.hk/~csxluo/" ]
exclude = [
"https://github.com/substrate-developer-hub/recipes/",
Copy link
Owner Author

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.

"www4\\.comp\\.polyu\\.edu\\.hk/~csxluo/"
]
57 changes: 0 additions & 57 deletions pallets/gen-random/Cargo.toml

This file was deleted.

59 changes: 0 additions & 59 deletions pallets/gen-random/src/lib.rs

This file was deleted.

32 changes: 32 additions & 0 deletions pallets/randomness/Cargo.toml
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',
]
91 changes: 91 additions & 0 deletions pallets/randomness/src/lib.rs
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()
}
}
Loading