Dispatch on-disabled digests from consensus modules (#3055)

* on-disable primitives for engines

* dispatch on-disabled digests from SRML consensus

* bump runtime versions

* use find_map
This commit is contained in:
Robert Habermeier
2019-07-08 16:22:15 +02:00
committed by Gavin Wood
parent 3d72844710
commit 13b9e49688
11 changed files with 140 additions and 72 deletions
@@ -23,7 +23,7 @@ extern crate alloc;
#[cfg(feature = "std")]
use serde::Serialize;
use parity_codec::{Encode, Decode};
use parity_codec::{Encode, Decode, Codec};
use sr_primitives::{ConsensusEngineId, traits::{DigestFor, NumberFor}};
use client::decl_runtime_apis;
use rstd::vec::Vec;
@@ -44,16 +44,75 @@ pub const GRANDPA_ENGINE_ID: ConsensusEngineId = *b"FRNK";
/// The weight of an authority.
pub type AuthorityWeight = u64;
/// The index of an authority.
pub type AuthorityIndex = u64;
/// A scheduled change of authority set.
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
#[derive(Clone, Eq, PartialEq, Encode, Decode)]
pub struct ScheduledChange<N> {
/// The new authorities after the change, along with their respective weights.
pub next_authorities: Vec<(AuthorityId, u64)>,
pub next_authorities: Vec<(AuthorityId, AuthorityWeight)>,
/// The number of blocks to delay.
pub delay: N,
}
/// An consensus log item for GRANDPA.
#[cfg_attr(feature = "std", derive(Serialize, Debug))]
#[derive(Decode, Encode, PartialEq, Eq, Clone)]
pub enum ConsensusLog<N: Codec> {
/// Schedule an authority set change.
///
/// Precedence towards earlier or later digest items can be given
/// based on the rules of the chain.
///
/// No change should be scheduled if one is already and the delay has not
/// passed completely.
///
/// This should be a pure function: i.e. as long as the runtime can interpret
/// the digest type it should return the same result regardless of the current
/// state.
#[codec(index = "1")]
ScheduledChange(ScheduledChange<N>),
/// Force an authority set change.
///
/// Forced changes are applied after a delay of _imported_ blocks,
/// while pending changes are applied after a delay of _finalized_ blocks.
///
/// Precedence towards earlier or later digest items can be given
/// based on the rules of the chain.
///
/// No change should be scheduled if one is already and the delay has not
/// passed completely.
///
/// This should be a pure function: i.e. as long as the runtime can interpret
/// the digest type it should return the same result regardless of the current
/// state.
#[codec(index = "2")]
ForcedChange(N, ScheduledChange<N>),
/// Note that the authority with given index is disabled until the next change.
#[codec(index = "3")]
OnDisabled(AuthorityIndex),
}
impl<N: Codec> ConsensusLog<N> {
/// Try to cast the log entry as a contained signal.
pub fn try_into_change(self) -> Option<ScheduledChange<N>> {
match self {
ConsensusLog::ScheduledChange(change) => Some(change),
ConsensusLog::ForcedChange(_, _) | ConsensusLog::OnDisabled(_) => None,
}
}
/// Try to cast the log entry as a contained forced signal.
pub fn try_into_forced_change(self) -> Option<(N, ScheduledChange<N>)> {
match self {
ConsensusLog::ForcedChange(median, change) => Some((median, change)),
ConsensusLog::ScheduledChange(_) | ConsensusLog::OnDisabled(_) => None,
}
}
}
/// WASM function call to check for pending changes.
pub const PENDING_CHANGE_CALL: &str = "grandpa_pending_change";
/// WASM function call to get current GRANDPA authorities.