New sessions, kill consensus module (#2802)

* Draft of new sessions

* Reintroduce tuple impls

* Move staking module to new session API

* More work on staking and grandpa.

* Use iterator to avoid cloning and tuple macro

* Make runtime build again

* Polish the OpaqueKeys devex

* Move consensus logic into system & aura.

* Fix up system module

* Get build mostly going. Stuck at service.rs

* Building again

* Update srml/staking/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Refactoring out Consensus module, AuthorityIdOf, &c.

* Refactored out DigestItem::AuthoritiesChanged. Building.

* Remove tentative code

* Remove invalid comment

* Make Seal opaque and introduce nice methods for handling opaque items.

* Start to use proper digest for Aura authorities tracking.

* Fix up grandpa, remove system::Raw/Log

* Refactor Grandpa to use new logging infrastructure.

Also make authorityid/sessionkey static. Switch over to storing
authorities in a straight Vec.

* Building again

* Tidy up some AuthorityIds

* Expunge most of the rest of the AuthorityKey confusion.

Also, de-generify Babe and re-generify Aura.

* Remove cruft

* Untangle last of the `AuthorityId`s.

* Sort out finality_tracker

* Refactor median getting

* Apply suggestions from code review

Co-Authored-By: Robert Habermeier <rphmeier@gmail.com>

* Session tests works

* Update core/sr-primitives/src/generic/digest.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Session tests works

* Fix for staking from @dvc94ch

* log an error

* fix test runtime build

* Some test fixes

* Staking mock update to new session api.

* Fix build.

* Move OpaqueKeys to primitives.

* Use on_initialize instead of check_rotate_session.

* Update tests to new staking api.

* fixup mock

* Fix bond_extra_and_withdraw_unbonded_works.

* Fix bond_with_little_staked_value_bounded_by_slot_stake.

* Fix bond_with_no_staked_value.

* Fix change_controller_works.

* Fix less_than_needed_candidates_works.

* Fix multi_era_reward_should_work.

* Fix nominating_and_rewards_should_work.

* Fix nominators_also_get_slashed.

* Fix phragmen_large_scale_test.

* Fix phragmen_poc_works.

* Fix phragmen_score_should_be_accurate_on_large_stakes.

* Fix phragmen_should_not_overflow.

* Fix reward_destination_works.

* Fix rewards_should_work.

* Fix sessions_and_eras_should_work.

* Fix slot_stake_is_least_staked_validator.

* Fix too_many_unbond_calls_should_not_work.

* Fix wrong_vote_is_null.

* Fix runtime.

* Fix wasm runtime build.

* Update Cargo.lock

* Fix warnings.

* Fix grandpa tests.

* Fix test-runtime build.

* Fix template node build.

* Fix stuff.

* Update Cargo.lock to fix CI

* Re-add missing AuRa logs

Runtimes are required to know about every digest they receive ― they
panic otherwise.  This re-adds support for AuRa pre-runtime digests.

* Update core/consensus/babe/src/digest.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Kill log trait and all that jazz.

* Refactor staking tests.

* Fix ci runtime wasm check.

* Line length 120.

* Make tests build again

* Remove trailing commas in function declarations

The `extern_functions!` macro doesn’t like them, perhaps due to a bug in
rustc.

* Fix type error

* Fix compilation errors

* Fix a test

* Another couple of fixes

* Fix another test

* More test fixes

* Another test fix

* Bump runtime.

* Wrap long line

* Fix build, remove redundant code.

* Issue to track TODO

* Leave the benchmark code alone.

* Fix missing `std::time::{Instant, Duration}`

* Indentation

* Aura ConsensusLog as enum
This commit is contained in:
Gavin Wood
2019-06-14 16:34:34 +02:00
committed by GitHub
parent 0f44a28ce3
commit bda8641892
128 changed files with 2646 additions and 3671 deletions
+3 -283
View File
@@ -45,6 +45,9 @@ use traits::{SaturatedConversion, UniqueSaturatedInto};
pub mod generic;
pub mod transaction_validity;
/// Re-export these since they're only "kind of" generic.
pub use generic::{DigestItem, Digest};
/// A message indicating an invalid signature in extrinsic.
pub const BAD_SIGNATURE: &str = "bad signature in extrinsic";
@@ -614,216 +617,6 @@ macro_rules! impl_outer_config {
}
}
// NOTE [`PreRuntime` and `Consensus` are special]
//
// We MUST treat `PreRuntime` and `Consensus` variants specially, as they:
//
// * have more parameters (both in `generic::DigestItem` and in runtimes)
// * have a `PhantomData` parameter in the runtime, but not in `generic::DigestItem`
#[macro_export]
#[doc(hidden)]
macro_rules! __parse_pattern_2 {
(PreRuntime $module:ident $internal:ident $v1:ident $v2:ident) => {
$internal::$module($module::RawLog::PreRuntime(ref $v1, ref $v2, $crate::rstd::marker::PhantomData))
};
(Consensus $module:ident $internal:ident $v1:ident $v2:ident) => {
$internal::$module($module::RawLog::Consensus(ref $v1, ref $v2, $crate::rstd::marker::PhantomData))
};
($name:ident $module:ident $internal:ident $v1:ident $v2:ident) => {
$internal::$module($module::RawLog::$name(ref $v1))
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __parse_pattern {
(PreRuntime $engine_id:pat, $binder:pat) => {
$crate::generic::DigestItem::PreRuntime($engine_id, $binder)
};
(Consensus $engine_id:pat, $binder:pat) => {
$crate::generic::DigestItem::Consensus($engine_id, $binder)
};
($name:ident $engine_id:pat, $binder:pat) => {
$crate::generic::DigestItem::$name($binder)
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __parse_expr {
(PreRuntime $engine_id:expr, $module:ident $internal:ident $binder:expr) => {
$internal::$module($module::RawLog::PreRuntime($engine_id, $binder, Default::default()))
};
(Consensus $engine_id:expr, $module:ident $internal:ident $binder:expr) => {
$internal::$module($module::RawLog::Consensus($engine_id, $binder, Default::default()))
};
($name:ident $engine_id:expr, $module:ident $internal:ident $binder:expr) => {
$internal::$module($module::RawLog::$name($binder))
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __parse_expr_2 {
(PreRuntime $module:ident $internal:ident $v1:ident $v2:ident) => {
$crate::generic::DigestItemRef::PreRuntime($v1, $v2)
};
(Consensus $module:ident $internal:ident $v1:ident $v2:ident) => {
$crate::generic::DigestItemRef::Consensus($v1, $v2)
};
($name:ident $module:ident $internal:ident $v1:ident $v2:ident) => {
$crate::generic::DigestItemRef::$name($v1)
};
}
/// Generates enum that contains all possible log entries for the runtime.
/// Every individual module of the runtime that is mentioned, must
/// expose a `Log` and `RawLog` enums.
///
/// Generated enum is binary-compatible with and could be interpreted
/// as `generic::DigestItem`.
///
/// Runtime requirements:
/// 1) binary representation of all supported 'system' log items should stay
/// the same. Otherwise, the native code will be unable to read log items
/// generated by previous runtime versions
/// 2) the support of 'system' log items should never be dropped by runtime.
/// Otherwise, native code will lost its ability to read items of this type
/// even if they were generated by the versions which have supported these
/// items.
#[macro_export]
macro_rules! impl_outer_log {
(
$(#[$attr:meta])*
pub enum $name:ident ($internal:ident: DigestItem<$( $genarg:ty ),*>) for $trait:ident {
$( $module:ident $(<$instance:path>)? ( $( $sitem:tt ),* ) ),*
}
) => {
/// Wrapper for all possible log entries for the `$trait` runtime. Provides binary-compatible
/// `Encode`/`Decode` implementations with the corresponding `generic::DigestItem`.
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, $crate::serde::Serialize))]
$(#[$attr])*
#[allow(non_camel_case_types)]
pub struct $name($internal);
/// All possible log entries for the `$trait` runtime. `Encode`/`Decode` implementations
/// are auto-generated => it is not binary-compatible with `generic::DigestItem`.
#[derive(Clone, PartialEq, Eq, $crate::codec::Encode, $crate::codec::Decode)]
#[cfg_attr(feature = "std", derive(Debug, $crate::serde::Serialize))]
$(#[$attr])*
#[allow(non_camel_case_types)]
pub enum InternalLog {
$(
$module($module::Log <$trait $(, $instance)?>),
)*
}
impl $name {
/// Try to convert `$name` into `generic::DigestItemRef`. Returns Some when
/// `self` is a 'system' log && it has been marked as 'system' in macro call.
/// Otherwise, None is returned.
#[allow(unreachable_patterns)]
fn dref<'a>(&'a self) -> Option<$crate::generic::DigestItemRef<'a, $($genarg),*>> {
match self.0 {
$($(
$crate::__parse_pattern_2!($sitem $module $internal a b) =>
Some($crate::__parse_expr_2!($sitem $module $internal a b)),
)*)*
_ => None,
}
}
}
impl $crate::traits::DigestItem for $name {
type Hash = <$crate::generic::DigestItem<$($genarg),*> as $crate::traits::DigestItem>::Hash;
type AuthorityId = <$crate::generic::DigestItem<$($genarg),*> as $crate::traits::DigestItem>::AuthorityId;
fn as_authorities_change(&self) -> Option<&[Self::AuthorityId]> {
self.dref().and_then(|dref| dref.as_authorities_change())
}
fn as_changes_trie_root(&self) -> Option<&Self::Hash> {
self.dref().and_then(|dref| dref.as_changes_trie_root())
}
fn as_pre_runtime(&self) -> Option<($crate::ConsensusEngineId, &[u8])> {
self.dref().and_then(|dref| dref.as_pre_runtime())
}
}
impl From<$crate::generic::DigestItem<$($genarg),*>> for $name {
/// Converts `generic::DigestItem` into `$name`. If
/// `generic::DigestItem` represents a system item which is
/// supported by the runtime, it is returned. Otherwise we expect a
/// `Other`, `PreDigest`, or `Consensus` log item. Trying to convert
/// from anything else will lead to panic at runtime, since the
/// runtime does not supports this 'system' log item.
#[allow(unreachable_patterns)]
fn from(gen: $crate::generic::DigestItem<$($genarg),*>) -> Self {
match gen {
$($(
$crate::__parse_pattern!($sitem b, a) =>
$name($crate::__parse_expr!($sitem b, $module $internal a)),
)*)*
_ => {
if let Some(s) = gen.as_other()
.and_then(|value| $crate::codec::Decode::decode(&mut &value[..]))
.map($name)
{
s
} else {
panic!("we only reach here if the runtime did not handle a digest; \
runtimes are required to handle all digests they receive; qed"
)
}
}
}
}
}
impl $crate::codec::Decode for $name {
/// `generic::DigestItem` binary compatible decode.
fn decode<I: $crate::codec::Input>(input: &mut I) -> Option<Self> {
let gen: $crate::generic::DigestItem<$($genarg),*> =
$crate::codec::Decode::decode(input)?;
Some($name::from(gen))
}
}
impl $crate::codec::Encode for $name {
/// `generic::DigestItem` binary compatible encode.
fn encode(&self) -> Vec<u8> {
match self.dref() {
Some(dref) => dref.encode(),
None => {
let gen: $crate::generic::DigestItem<$($genarg),*> =
$crate::generic::DigestItem::Other(self.0.encode());
gen.encode()
},
}
}
}
$(
impl From<$module::Log<$trait $(, $instance)?>> for $name {
/// Converts single module log item into `$name`.
fn from(x: $module::Log<$trait $(, $instance)? >) -> Self {
$name(x.into())
}
}
impl From<$module::Log<$trait $(, $instance)?>> for InternalLog {
/// Converts single module log item into `$internal`.
fn from(x: $module::Log<$trait $(, $instance)?>) -> Self {
InternalLog::$module(x)
}
}
)*
};
}
/// Simple blob to hold an extrinsic without committing to its format and ensure it is serialized
/// correctly.
#[derive(PartialEq, Eq, Clone, Default, Encode, Decode)]
@@ -851,45 +644,7 @@ impl traits::Extrinsic for OpaqueExtrinsic {
#[cfg(test)]
mod tests {
use substrate_primitives::hash::{H256, H512};
use crate::codec::{Encode, Decode};
use crate::traits::DigestItem;
pub trait RuntimeT {
type AuthorityId;
}
pub struct Runtime;
impl RuntimeT for Runtime {
type AuthorityId = u64;
}
mod a {
use super::RuntimeT;
use crate::codec::{Encode, Decode};
use serde::Serialize;
pub type Log<R> = RawLog<<R as RuntimeT>::AuthorityId>;
#[derive(Serialize, Debug, Encode, Decode, PartialEq, Eq, Clone)]
pub enum RawLog<AuthorityId> { A1(AuthorityId), AuthoritiesChange(Vec<AuthorityId>), A3(AuthorityId) }
}
mod b {
use super::RuntimeT;
use crate::codec::{Encode, Decode};
use serde::Serialize;
pub type Log<R> = RawLog<<R as RuntimeT>::AuthorityId>;
#[derive(Serialize, Debug, Encode, Decode, PartialEq, Eq, Clone)]
pub enum RawLog<AuthorityId> { B1(AuthorityId), B2(AuthorityId) }
}
impl_outer_log! {
pub enum Log(InternalLog: DigestItem<H256, u64, H512>) for Runtime {
a(AuthoritiesChange), b()
}
}
macro_rules! per_thing_mul_upper_test {
($num_type:tt, $per:tt) => {
@@ -909,41 +664,6 @@ mod tests {
}
}
#[test]
fn impl_outer_log_works() {
// encode/decode regular item
let b1: Log = b::RawLog::B1::<u64>(777).into();
let encoded_b1 = b1.encode();
let decoded_b1: Log = Decode::decode(&mut &encoded_b1[..]).unwrap();
assert_eq!(b1, decoded_b1);
// encode/decode system item
let auth_change: Log = a::RawLog::AuthoritiesChange::<u64>(vec![100, 200, 300]).into();
let encoded_auth_change = auth_change.encode();
let decoded_auth_change: Log = Decode::decode(&mut &encoded_auth_change[..]).unwrap();
assert_eq!(auth_change, decoded_auth_change);
// interpret regular item using `generic::DigestItem`
let generic_b1: super::generic::DigestItem<H256, u64, H512> = Decode::decode(&mut &encoded_b1[..]).unwrap();
match generic_b1 {
super::generic::DigestItem::Other(_) => (),
_ => panic!("unexpected generic_b1: {:?}", generic_b1),
}
// interpret system item using `generic::DigestItem`
let generic_auth_change: super::generic::DigestItem<H256, u64, H512> = Decode::decode(&mut &encoded_auth_change[..]).unwrap();
match generic_auth_change {
super::generic::DigestItem::AuthoritiesChange::<H256, u64, H512>(authorities) => assert_eq!(authorities, vec![100, 200, 300]),
_ => panic!("unexpected generic_auth_change: {:?}", generic_auth_change),
}
// check that as-style methods are working with system items
assert!(auth_change.as_authorities_change().is_some());
// check that as-style methods are not working with regular items
assert!(b1.as_authorities_change().is_none());
}
#[test]
fn opaque_extrinsic_serialization() {
let ex = super::OpaqueExtrinsic(vec![1, 2, 3, 4]);