Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions core/sr-primitives/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::codec::{Codec, Encode, Decode};
use crate::traits::{self, Checkable, Applyable, BlakeTwo256, Convert};
use crate::generic::DigestItem as GenDigestItem;
pub use substrate_primitives::H256;
use substrate_primitives::U256;
use substrate_primitives::sr25519::{Public as AuthorityId, Signature as AuthoritySignature};

/// Authority Id
Expand All @@ -31,7 +30,7 @@ use substrate_primitives::sr25519::{Public as AuthorityId, Signature as Authorit
pub struct UintAuthorityId(pub u64);
impl Into<AuthorityId> for UintAuthorityId {
fn into(self) -> AuthorityId {
let bytes: [u8; 32] = U256::from(self.0).into();
let bytes: [u8; 32] = H256::from_low_u64_be(self.0).into();
AuthorityId(bytes)
}
}
Expand Down
8 changes: 4 additions & 4 deletions node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
GenesisConfig {
consensus: Some(ConsensusConfig {
code: include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm").to_vec(), // FIXME change once we have #1252
authorities: initial_authorities.iter().map(|x| x.2.clone()).collect(),
authorities: initial_authorities.iter().map(|x| (x.1.clone(), x.2.clone())).collect(),
}),
system: None,
balances: Some(BalancesConfig {
Expand All @@ -104,7 +104,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
session: Some(SessionConfig {
validators: initial_authorities.iter().map(|x| x.1.clone()).collect(),
session_length: 5 * MINUTES,
keys: initial_authorities.iter().map(|x| (x.1.clone(), x.2.clone())).collect::<Vec<_>>(),
keys: initial_authorities.iter().map(|x| (x.1.clone(), (x.1.clone(), x.2.clone()))).collect::<Vec<_>>(),
}),
staking: Some(StakingConfig {
current_era: 0,
Expand Down Expand Up @@ -270,7 +270,7 @@ pub fn testnet_genesis(
GenesisConfig {
consensus: Some(ConsensusConfig {
code: include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm").to_vec(),
authorities: initial_authorities.iter().map(|x| x.2.clone()).collect(),
authorities: initial_authorities.iter().map(|x| (x.1.clone(), x.2.clone())).collect(),
}),
system: None,
indices: Some(IndicesConfig {
Expand All @@ -288,7 +288,7 @@ pub fn testnet_genesis(
session: Some(SessionConfig {
validators: initial_authorities.iter().map(|x| x.1.clone()).collect(),
session_length: 10,
keys: initial_authorities.iter().map(|x| (x.1.clone(), x.2.clone())).collect::<Vec<_>>(),
keys: initial_authorities.iter().map(|x| (x.1.clone(), (x.1.clone(), x.2.clone()))).collect::<Vec<_>>(),
}),
staking: Some(StakingConfig {
current_era: 0,
Expand Down
16 changes: 13 additions & 3 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ impl Convert<u128, u128> for CurrencyToVoteHandler {
fn convert(x: u128) -> u128 { x * Self::factor() }
}

pub type SessionKey = (AccountId, AuthorityId);

impl system::Trait for Runtime {
type Origin = Origin;
type Index = Index;
Expand Down Expand Up @@ -123,13 +125,20 @@ impl balances::Trait for Runtime {

impl consensus::Trait for Runtime {
type Log = Log;
type SessionKey = AuthorityId;
type SessionKey = SessionKey;

// The Aura module handles offline-reports internally
// rather than using an explicit report system.
type InherentOfflineReport = ();
}

pub struct ConvertSessionKeyToAuthorityId;
impl Convert<SessionKey, AuthorityId> for ConvertSessionKeyToAuthorityId {
fn convert(session_key: SessionKey) -> AuthorityId {
session_key.1
}
}

impl timestamp::Trait for Runtime {
type Moment = u64;
type OnTimestampSet = Aura;
Expand Down Expand Up @@ -198,7 +207,8 @@ impl sudo::Trait for Runtime {
}

impl grandpa::Trait for Runtime {
type SessionKey = AuthorityId;
type LocalSessionKey = AuthorityId;
type ConvertLocalSessionKey = ConvertSessionKeyToAuthorityId;
type Log = Log;
type Event = Event;
}
Expand All @@ -208,7 +218,7 @@ impl finality_tracker::Trait for Runtime {
}

construct_runtime!(
pub enum Runtime with Log(InternalLog: DigestItem<Hash, AuthorityId, AuthoritySignature>) where
pub enum Runtime with Log(InternalLog: DigestItem<Hash, SessionKey, AuthoritySignature>) where
Block = Block,
NodeBlock = node_primitives::Block,
UncheckedExtrinsic = UncheckedExtrinsic
Expand Down
1 change: 1 addition & 0 deletions srml/grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ finality-tracker = { package = "srml-finality-tracker", path = "../finality-trac

[dev-dependencies]
runtime_io = { package = "sr-io", path = "../../core/sr-io" }
timestamp = { package = "srml-timestamp", path = "../timestamp", default-features = false }

[features]
default = ["std"]
Expand Down
71 changes: 35 additions & 36 deletions srml/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use srml_support::storage::unhashed::StorageVec;
use primitives::traits::CurrentHeight;
use substrate_primitives::ed25519;
use system::ensure_signed;
use primitives::traits::MaybeSerializeDebug;
use primitives::traits::{Convert, MaybeSerializeDebug};
use ed25519::Public as AuthorityId;

mod mock;
Expand All @@ -58,7 +58,7 @@ impl<S: codec::Codec + Default> StorageVec for AuthorityStorageVec<S> {
/// The log type of this crate, projected from module trait type.
pub type Log<T> = RawLog<
<T as system::Trait>::BlockNumber,
<T as Trait>::SessionKey,
<T as Trait>::LocalSessionKey,
>;

/// Logs which can be scanned by GRANDPA for authorities change events.
Expand All @@ -72,36 +72,36 @@ pub trait GrandpaChangeSignal<N> {
/// A logs in this module.
#[cfg_attr(feature = "std", derive(Serialize, Debug))]
#[derive(Encode, Decode, PartialEq, Eq, Clone)]
pub enum RawLog<N, SessionKey> {
pub enum RawLog<N, LocalSessionKey> {
/// Authorities set change has been signaled. Contains the new set of authorities
/// and the delay in blocks _to finalize_ before applying.
AuthoritiesChangeSignal(N, Vec<(SessionKey, u64)>),
AuthoritiesChangeSignal(N, Vec<(LocalSessionKey, u64)>),
/// 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.
ForcedAuthoritiesChangeSignal(N, N, Vec<(SessionKey, u64)>),
ForcedAuthoritiesChangeSignal(N, N, Vec<(LocalSessionKey, u64)>),
}

impl<N: Clone, SessionKey> RawLog<N, SessionKey> {
impl<N: Clone, LocalSessionKey> RawLog<N, LocalSessionKey> {
/// Try to cast the log entry as a contained signal.
pub fn as_signal(&self) -> Option<(N, &[(SessionKey, u64)])> {
pub fn as_signal(&self) -> Option<(N, &[(LocalSessionKey, u64)])> {
match *self {
RawLog::AuthoritiesChangeSignal(ref delay, ref signal) => Some((delay.clone(), signal)),
RawLog::ForcedAuthoritiesChangeSignal(_, _, _) => None,
}
}

/// Try to cast the log entry as a contained forced signal.
pub fn as_forced_signal(&self) -> Option<(N, N, &[(SessionKey, u64)])> {
pub fn as_forced_signal(&self) -> Option<(N, N, &[(LocalSessionKey, u64)])> {
match *self {
RawLog::ForcedAuthoritiesChangeSignal(ref median, ref delay, ref signal) => Some((median.clone(), delay.clone(), signal)),
RawLog::AuthoritiesChangeSignal(_, _) => None,
}
}
}

impl<N, SessionKey> GrandpaChangeSignal<N> for RawLog<N, SessionKey>
where N: Clone, SessionKey: Clone + Into<AuthorityId>,
impl<N, LocalSessionKey> GrandpaChangeSignal<N> for RawLog<N, LocalSessionKey>
where N: Clone, LocalSessionKey: Clone + Into<AuthorityId>,
{
fn as_signal(&self) -> Option<ScheduledChange<N>> {
RawLog::as_signal(self).map(|(delay, next_authorities)| ScheduledChange {
Expand All @@ -124,12 +124,15 @@ impl<N, SessionKey> GrandpaChangeSignal<N> for RawLog<N, SessionKey>
}
}

pub trait Trait: system::Trait {
pub trait Trait: session::Trait + system::Trait {
/// Type for all log entries of this module.
type Log: From<Log<Self>> + Into<system::DigestItemOf<Self>>;

/// The session key type used by authorities.
type SessionKey: Parameter + Default + MaybeSerializeDebug;
type LocalSessionKey: Parameter + Default + MaybeSerializeDebug;

// Convert to LocalSessionKey.
type ConvertLocalSessionKey: Convert<Self::SessionKey, Self::LocalSessionKey>;

/// The event type of this module.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
Expand All @@ -139,30 +142,30 @@ pub trait Trait: system::Trait {
// TODO: remove shim
// https://github.com/paritytech/substrate/issues/1614
#[derive(Encode, Decode)]
pub struct OldStoredPendingChange<N, SessionKey> {
pub struct OldStoredPendingChange<N, LocalSessionKey> {
/// The block number this was scheduled at.
pub scheduled_at: N,
/// The delay in blocks until it will be applied.
pub delay: N,
/// The next authority set.
pub next_authorities: Vec<(SessionKey, u64)>,
pub next_authorities: Vec<(LocalSessionKey, u64)>,
}

/// A stored pending change.
#[derive(Encode)]
pub struct StoredPendingChange<N, SessionKey> {
pub struct StoredPendingChange<N, LocalSessionKey> {
/// The block number this was scheduled at.
pub scheduled_at: N,
/// The delay in blocks until it will be applied.
pub delay: N,
/// The next authority set.
pub next_authorities: Vec<(SessionKey, u64)>,
pub next_authorities: Vec<(LocalSessionKey, u64)>,
/// If defined it means the change was forced and the given block number
/// indicates the median last finalized block when the change was signaled.
pub forced: Option<N>,
}

impl<N: Decode, SessionKey: Decode> Decode for StoredPendingChange<N, SessionKey> {
impl<N: Decode, LocalSessionKey: Decode> Decode for StoredPendingChange<N, LocalSessionKey> {
fn decode<I: codec::Input>(value: &mut I) -> Option<Self> {
let old = OldStoredPendingChange::decode(value)?;
let forced = <Option<N>>::decode(value).unwrap_or(None);
Expand All @@ -177,21 +180,21 @@ impl<N: Decode, SessionKey: Decode> Decode for StoredPendingChange<N, SessionKey
}

decl_event!(
pub enum Event<T> where <T as Trait>::SessionKey {
pub enum Event<T> where <T as Trait>::LocalSessionKey {
/// New authority set has been applied.
NewAuthorities(Vec<(SessionKey, u64)>),
NewAuthorities(Vec<(LocalSessionKey, u64)>),
}
);

decl_storage! {
trait Store for Module<T: Trait> as GrandpaFinality {
// Pending change: (signaled at, scheduled change).
PendingChange get(pending_change): Option<StoredPendingChange<T::BlockNumber, T::SessionKey>>;
PendingChange get(pending_change): Option<StoredPendingChange<T::BlockNumber, T::LocalSessionKey>>;
// next block number where we can force a change.
NextForced get(next_forced): Option<T::BlockNumber>;
}
add_extra_genesis {
config(authorities): Vec<(T::SessionKey, u64)>;
config(authorities): Vec<(T::LocalSessionKey, u64)>;

build(|storage: &mut primitives::StorageOverlay, _: &mut primitives::ChildrenStorageOverlay, config: &GenesisConfig<T>| {
use codec::{Encode, KeyedVec};
Expand Down Expand Up @@ -242,7 +245,7 @@ decl_module! {
Self::deposit_event(
RawEvent::NewAuthorities(pending_change.next_authorities.clone())
);
<AuthorityStorageVec<T::SessionKey>>::set_items(pending_change.next_authorities);
<AuthorityStorageVec<T::LocalSessionKey>>::set_items(pending_change.next_authorities);
<PendingChange<T>>::kill();
}
}
Expand All @@ -252,8 +255,8 @@ decl_module! {

impl<T: Trait> Module<T> {
/// Get the current set of authorities, along with their respective weights.
pub fn grandpa_authorities() -> Vec<(T::SessionKey, u64)> {
<AuthorityStorageVec<T::SessionKey>>::items()
pub fn grandpa_authorities() -> Vec<(T::LocalSessionKey, u64)> {
<AuthorityStorageVec<T::LocalSessionKey>>::items()
}

/// Schedule a change in the authorities.
Expand All @@ -271,7 +274,7 @@ impl<T: Trait> Module<T> {
/// No change should be signaled while any change is pending. Returns
/// an error if a change is already pending.
pub fn schedule_change(
next_authorities: Vec<(T::SessionKey, u64)>,
next_authorities: Vec<(T::LocalSessionKey, u64)>,
in_blocks: T::BlockNumber,
forced: Option<T::BlockNumber>,
) -> Result {
Expand Down Expand Up @@ -307,7 +310,7 @@ impl<T: Trait> Module<T> {
}
}

impl<T: Trait> Module<T> where AuthorityId: core::convert::From<<T as Trait>::SessionKey> {
impl<T: Trait> Module<T> where AuthorityId: core::convert::From<<T as Trait>::LocalSessionKey> {
/// See if the digest contains any standard scheduled change.
pub fn scrape_digest_change(log: &Log<T>)
-> Option<ScheduledChange<T::BlockNumber>>
Expand Down Expand Up @@ -337,17 +340,15 @@ impl<T> Default for SyncedAuthorities<T> {
}
}

impl<X, T> session::OnSessionChange<X> for SyncedAuthorities<T> where
T: Trait + consensus::Trait<SessionKey=<T as Trait>::SessionKey>,
<T as consensus::Trait>::Log: From<consensus::RawLog<<T as Trait>::SessionKey>>
impl<X, T: Trait> session::OnSessionChange<X> for SyncedAuthorities<T> where
{
fn on_session_change(_: X, _: bool) {
use primitives::traits::Zero;

let next_authorities = <consensus::Module<T>>::authorities()
.into_iter()
.map(|key| (key, 1)) // evenly-weighted.
.collect::<Vec<(<T as Trait>::SessionKey, u64)>>();
.map(|key| (T::ConvertLocalSessionKey::convert(key), 1)) // evenly-weighted.
.collect::<Vec<(<T as Trait>::LocalSessionKey, u64)>>();

// instant changes
let last_authorities = <Module<T>>::grandpa_authorities();
Expand All @@ -357,9 +358,7 @@ impl<X, T> session::OnSessionChange<X> for SyncedAuthorities<T> where
}
}

impl<T> finality_tracker::OnFinalizationStalled<T::BlockNumber> for SyncedAuthorities<T> where
T: Trait + consensus::Trait<SessionKey=<T as Trait>::SessionKey>,
<T as consensus::Trait>::Log: From<consensus::RawLog<<T as Trait>::SessionKey>>,
impl<T: Trait> finality_tracker::OnFinalizationStalled<T::BlockNumber> for SyncedAuthorities<T> where
T: finality_tracker::Trait,
{
fn on_stalled(further_wait: T::BlockNumber) {
Expand All @@ -369,8 +368,8 @@ impl<T> finality_tracker::OnFinalizationStalled<T::BlockNumber> for SyncedAuthor

let next_authorities = <consensus::Module<T>>::authorities()
.into_iter()
.map(|key| (key, 1)) // evenly-weighted.
.collect::<Vec<(<T as Trait>::SessionKey, u64)>>();
.map(|key| (T::ConvertLocalSessionKey::convert(key), 1)) // evenly-weighted.
.collect::<Vec<(<T as Trait>::LocalSessionKey, u64)>>();

let median = <finality_tracker::Module<T>>::median();

Expand Down
20 changes: 19 additions & 1 deletion srml/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,26 @@ impl From<RawLog<u64, u64>> for DigestItem {
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug, Decode, Encode)]
pub struct Test;
impl timestamp::Trait for Test {
type Moment = u64;
type OnTimestampSet = ();
}
impl consensus::Trait for Test {
type Log = DigestItem;
type SessionKey = substrate_primitives::sr25519::Public;
type InherentOfflineReport = ();
}
impl session::Trait for Test {
type ConvertAccountIdToSessionKey = ();
type OnSessionChange = ();
type OnDisable = ();
type CheckRotateSession = session::AuraCheckRotateSession<Self>;
type Event = TestEvent;
}
impl Trait for Test {
type Log = DigestItem;
type SessionKey = u64;
type LocalSessionKey = u64;
type ConvertLocalSessionKey = ();
type Event = TestEvent;
}
impl system::Trait for Test {
Expand All @@ -65,6 +82,7 @@ mod grandpa {
impl_outer_event!{
pub enum TestEvent for Test {
grandpa<T>,
session<T>,
}
}

Expand Down
Loading