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
+7 -2
View File
@@ -200,8 +200,13 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
}
}
}
fn on_disabled(_i: usize) {
// ignore?
fn on_disabled(i: usize) {
let log: DigestItem<T::Hash> = DigestItem::Consensus(
AURA_ENGINE_ID,
ConsensusLog::<T::AuthorityId>::OnDisabled(i as u64).encode(),
);
<system::Module<T>>::deposit_log(log.into());
}
}
+12 -7
View File
@@ -31,7 +31,7 @@ use parity_codec::{Encode, Decode};
use inherents::{RuntimeString, InherentIdentifier, InherentData, ProvideInherent, MakeFatalError};
#[cfg(feature = "std")]
use inherents::{InherentDataProviders, ProvideInherentData};
use babe_primitives::BABE_ENGINE_ID;
use babe_primitives::{BABE_ENGINE_ID, ConsensusLog};
pub use babe_primitives::{AuthorityId, VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH, PUBLIC_KEY_LENGTH};
/// The BABE inherent identifier.
@@ -128,13 +128,13 @@ decl_storage! {
/// (like everything else on-chain) it is public. For example, it can be
/// used where a number is needed that cannot have been chosen by an
/// adversary, for purposes such as public-coin zero-knowledge proofs.
EpochRandomness get(epoch_randomness): [u8; 32];
EpochRandomness get(epoch_randomness): [u8; VRF_OUTPUT_LENGTH];
/// The randomness under construction
UnderConstruction: [u8; 32];
UnderConstruction: [u8; VRF_OUTPUT_LENGTH];
/// The randomness for the next epoch
NextEpochRandomness: [u8; 32];
NextEpochRandomness: [u8; VRF_OUTPUT_LENGTH];
/// The current epoch
EpochIndex get(epoch_index): u64;
@@ -162,7 +162,7 @@ decl_module! {
}
impl<T: Trait> RandomnessBeacon for Module<T> {
fn random() -> [u8; 32] {
fn random() -> [u8; VRF_OUTPUT_LENGTH] {
Self::epoch_randomness()
}
}
@@ -245,8 +245,13 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
s[40..].copy_from_slice(&rho);
NextEpochRandomness::put(runtime_io::blake2_256(&s))
}
fn on_disabled(_i: usize) {
// ignore?
fn on_disabled(i: usize) {
let log: DigestItem<T::Hash> = DigestItem::Consensus(
BABE_ENGINE_ID,
ConsensusLog::OnDisabled(i as u64).encode(),
);
<system::Module<T>>::deposit_log(log.into());
}
}
+8 -41
View File
@@ -30,8 +30,6 @@
// re-export since this is necessary for `impl_apis` in runtime.
pub use substrate_finality_grandpa_primitives as fg_primitives;
#[cfg(feature = "std")]
use serde::Serialize;
use rstd::prelude::*;
use parity_codec::{self as codec, Encode, Decode};
use srml_support::{
@@ -40,44 +38,13 @@ use srml_support::{
use primitives::{
generic::{DigestItem, OpaqueDigestItemId}, traits::CurrentHeight
};
use fg_primitives::{ScheduledChange, GRANDPA_ENGINE_ID};
use fg_primitives::{ScheduledChange, ConsensusLog, GRANDPA_ENGINE_ID};
pub use fg_primitives::{AuthorityId, AuthorityWeight};
use system::{ensure_signed, DigestOf};
mod mock;
mod tests;
/// Consensus log type of this module.
#[cfg_attr(feature = "std", derive(Serialize, Debug))]
#[derive(Encode, Decode, PartialEq, Eq, Clone)]
pub enum Signal<N> {
/// Authorities set change has been signaled. Contains the new set of authorities
/// and the delay in blocks _to finalize_ before applying.
AuthoritiesChange(ScheduledChange<N>),
/// A forced authorities set change. Contains in this order: the median last
/// finalized block when the change was signaled, the delay in blocks _to import_
/// before applying and the new set of authorities.
ForcedAuthoritiesChange(N, ScheduledChange<N>),
}
impl<N> Signal<N> {
/// Try to cast the log entry as a contained signal.
pub fn try_into_change(self) -> Option<ScheduledChange<N>> {
match self {
Signal::AuthoritiesChange(change) => Some(change),
Signal::ForcedAuthoritiesChange(_, _) => 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 {
Signal::ForcedAuthoritiesChange(median, change) => Some((median, change)),
Signal::AuthoritiesChange(_) => None,
}
}
}
pub trait Trait: system::Trait {
/// The event type of this module.
type Event: From<Event> + Into<<Self as system::Trait>::Event>;
@@ -161,7 +128,7 @@ decl_module! {
if let Some(pending_change) = <PendingChange<T>>::get() {
if block_number == pending_change.scheduled_at {
if let Some(median) = pending_change.forced {
Self::deposit_log(Signal::ForcedAuthoritiesChange(
Self::deposit_log(ConsensusLog::ForcedChange(
median,
ScheduledChange{
delay: pending_change.delay,
@@ -169,7 +136,7 @@ decl_module! {
}
))
} else {
Self::deposit_log(Signal::AuthoritiesChange(
Self::deposit_log(ConsensusLog::ScheduledChange(
ScheduledChange{
delay: pending_change.delay,
next_authorities: pending_change.next_authorities.clone(),
@@ -242,16 +209,16 @@ impl<T: Trait> Module<T> {
}
/// Deposit one of this module's logs.
fn deposit_log(log: Signal<T::BlockNumber>) {
fn deposit_log(log: ConsensusLog<T::BlockNumber>) {
let log: DigestItem<T::Hash> = DigestItem::Consensus(GRANDPA_ENGINE_ID, log.encode());
<system::Module<T>>::deposit_log(log.into());
}
}
impl<T: Trait> Module<T> {
pub fn grandpa_log(digest: &DigestOf<T>) -> Option<Signal<T::BlockNumber>> {
pub fn grandpa_log(digest: &DigestOf<T>) -> Option<ConsensusLog<T::BlockNumber>> {
let id = OpaqueDigestItemId::Consensus(&GRANDPA_ENGINE_ID);
digest.convert_first(|l| l.try_to::<Signal<T::BlockNumber>>(id))
digest.convert_first(|l| l.try_to::<ConsensusLog<T::BlockNumber>>(id))
}
pub fn pending_change(digest: &DigestOf<T>)
@@ -287,8 +254,8 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
}
}
}
fn on_disabled(_i: usize) {
// ignore?
fn on_disabled(i: usize) {
Self::deposit_log(ConsensusLog::OnDisabled(i as u64))
}
}
+3 -5
View File
@@ -23,17 +23,15 @@ use runtime_io;
use srml_support::{impl_outer_origin, impl_outer_event};
use substrate_primitives::{H256, Blake2Hasher};
use parity_codec::{Encode, Decode};
use crate::{AuthorityId, GenesisConfig, Trait, Module, Signal};
use crate::{AuthorityId, GenesisConfig, Trait, Module, ConsensusLog};
use substrate_finality_grandpa_primitives::GRANDPA_ENGINE_ID;
impl_outer_origin!{
pub enum Origin for Test {}
}
impl From<Signal<u64>> for DigestItem<H256> {
fn from(log: Signal<u64>) -> DigestItem<H256> {
DigestItem::Consensus(GRANDPA_ENGINE_ID, log.encode())
}
pub fn grandpa_log(log: ConsensusLog<u64>) -> DigestItem<H256> {
DigestItem::Consensus(GRANDPA_ENGINE_ID, log.encode())
}
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
+4 -4
View File
@@ -39,9 +39,9 @@ fn authorities_change_logged() {
let header = System::finalize();
assert_eq!(header.digest, Digest {
logs: vec![
Signal::AuthoritiesChange(
grandpa_log(ConsensusLog::ScheduledChange(
ScheduledChange { delay: 0, next_authorities: to_authorities(vec![(4, 1), (5, 1), (6, 1)]) }
).into(),
)),
],
});
@@ -64,9 +64,9 @@ fn authorities_change_logged_after_delay() {
let header = System::finalize();
assert_eq!(header.digest, Digest {
logs: vec![
Signal::AuthoritiesChange(
grandpa_log(ConsensusLog::ScheduledChange(
ScheduledChange { delay: 1, next_authorities: to_authorities(vec![(4, 1), (5, 1), (6, 1)]) }
).into(),
)),
],
});