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

Commit

Permalink
add a manually curated grandpa module
Browse files Browse the repository at this point in the history
  • Loading branch information
rphmeier committed Feb 12, 2019
1 parent 2a5e8fc commit dd50017
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
79 changes: 79 additions & 0 deletions runtime/src/curated_grandpa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot 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.

// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.

//! A module for manually curated GRANDPA set.

use {grandpa, system};
use codec::Decode;
use sr_primitives::traits::{As, Hash as HashT, BlakeTwo256, Zero};
use rstd::prelude::*;


pub trait Trait: grandpa::Trait {}

decl_storage! {
trait Store for Module<T: Trait> as CuratedGrandpa {
/// How often to shuffle the GRANDPA sets.
pub ShufflePeriod get(shuffle_period) build(|_| T::BlockNumber::sa(1024u64)): T::BlockNumber;
}
}

decl_module! {
/// curated GRANDPA set.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// Changes the GRANDPA voter set.
fn set_voters(origin, voters: Vec<(T::SessionKey, u64)>) {
system::ensure_root(origin)?;
grandpa::Module::<T>::schedule_change(voters, T::BlockNumber::zero())?;
}

fn on_finalise(block_number: T::BlockNumber) {
// every so often shuffle the voters and issue a change.
let shuffle_period: u64 = Self::shuffle_period().as_();
if block_number.as_() % shuffle_period == 0 {
let mut seed = system::Module::<T>::random_seed().as_ref().to_vec();
seed.extend(b"grandpa_shuffling");
let mut seed = BlakeTwo256::hash(&seed);

let mut voters = grandpa::Module::<T>::grandpa_authorities();
let voter_count = voters.len();

if voter_count == 0 { return }

for i in 0..(voter_count - 1) {
// 4 bytes of entropy used per cycle, 32 bytes entropy per hash
let offset = (i * 4 % 32) as usize;

// number of roles remaining to select from.
let remaining = (voter_count - i) as usize;

// 8 32-bit ints per 256-bit seed.
let voter_index = u32::decode(&mut &seed[offset..offset + 4]).expect("using 4 bytes for a 32-bit quantity") as usize % remaining;

if offset == 28 {
// into the last 4 bytes - rehash to gather new entropy
seed = BlakeTwo256::hash(seed.as_ref());
}

// exchange last item with randomly chosen first.
voters.swap(remaining - 1, voter_index);
}

let _ = grandpa::Module::<T>::schedule_change(voters, T::BlockNumber::zero());
}
}
}
}
8 changes: 6 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ extern crate polkadot_primitives as primitives;
#[cfg(test)]
extern crate substrate_keyring as keyring;

mod curated_grandpa;
mod parachains;

use rstd::prelude::*;
Expand Down Expand Up @@ -108,7 +109,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polkadot"),
impl_name: create_runtime_str!("parity-polkadot"),
authoring_version: 1,
spec_version: 103,
spec_version: 104,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};
Expand Down Expand Up @@ -181,7 +182,7 @@ impl Convert<AccountId, SessionKey> for SessionKeyConversion {

impl session::Trait for Runtime {
type ConvertAccountIdToSessionKey = SessionKeyConversion;
type OnSessionChange = (Staking, grandpa::SyncedAuthorities<Runtime>);
type OnSessionChange = Staking;
type Event = Event;
}

Expand Down Expand Up @@ -221,6 +222,8 @@ impl grandpa::Trait for Runtime {
type Event = Event;
}

impl curated_grandpa::Trait for Runtime { }

impl parachains::Trait for Runtime {
const SET_POSITION: u32 = PARACHAINS_SET_POSITION;
}
Expand Down Expand Up @@ -251,6 +254,7 @@ construct_runtime!(
Staking: staking,
Democracy: democracy,
Grandpa: grandpa::{Module, Call, Storage, Config<T>, Log(), Event<T>},
CuratedGrandpa: curated_grandpa::{Module, Call, Storage},
Council: council::{Module, Call, Storage, Event<T>},
CouncilVoting: council_voting,
CouncilMotions: council_motions::{Module, Call, Storage, Event<T>, Origin},
Expand Down
2 changes: 2 additions & 0 deletions runtime/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.

0 comments on commit dd50017

Please sign in to comment.