Refactor DigestItem (#2108)

* Add `start_aura2`.

* .gitignore patch conflict files

and remove one that accidentally got committed

* Fix build

The tests still don’t work.

* Fix compilation errors

* Fix compile errors (again)

* Try (and fail) to fix tests

* Properly deserialize data

Previously, `DigestItem::Consensus` had no separate `DigestItemType`,
so it did not get properly serialized and deserialized.

* Add extra debug logging.  Always allow old seals.

A `RUST_LOG=substrate_aura_consensus cargo test --all -- --nocapture \
tests::authoring_blocks` revealed that old seals were being and
rejected, causing the test to hang.  As a temporary debug measure, allow
old seals unconditionally, so that CI can test if this fixes the
problem.

* Forcibly disable rejection of old seals

* Use old trait, but newer serialization

The old trait for `CompatibleDigestItem` actually worked.  By changing
its implementation, one can ensure that all *new* seals have the modern
form, but *legacy* seals are still decoded correctly.

* Bump impl version

* Squash spurious deprecation warning

`rustc` should not be emitting a deprecation warning in deprecated
code, but it does, so silence it.

* Rip out unused Cargo feature

* Move AURA to aura_primitives

* Respond to code review

* Wrap overly-long line

* Reduce logging verbosity and add target

* Add dependency on `sr-primitives` to `aura_primitives`

* Fix build

It failed with a message about Cargo.lock being out of date.

* core: aura: rename aura engine id const

* core: aura: remove superfluous logging

* core: primitives: add removed semicolons

* core: aura: remove unused import

* core: network: style fix

* runtime: update wasm blobs

* runtime: bump impl_version

* core: primitives: tag all DigestItemType variants explicitly
This commit is contained in:
DemiMarie-parity
2019-03-29 08:15:23 -04:00
committed by André Silva
parent 665a0ac26a
commit a10e86ba5a
25 changed files with 129 additions and 43 deletions
@@ -21,6 +21,7 @@ use serde_derive::Serialize;
use rstd::prelude::*;
use crate::ConsensusEngineId;
use crate::codec::{Decode, Encode, Codec, Input};
use crate::traits::{self, Member, DigestItem as DigestItemT, MaybeHash};
@@ -61,6 +62,7 @@ impl<Item> traits::Digest for Digest<Item> where
/// provide opaque access to other items.
#[derive(PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Debug))]
#[allow(deprecated)]
pub enum DigestItem<Hash, AuthorityId, SealSignature> {
/// System digest item announcing that authorities set has been changed
/// in the block. Contains the new set of authorities.
@@ -69,8 +71,11 @@ pub enum DigestItem<Hash, AuthorityId, SealSignature> {
/// block. It is created for every block iff runtime supports changes
/// trie creation.
ChangesTrieRoot(Hash),
/// Put a Seal on it
/// The old way to put a Seal on it. Deprecated.
#[deprecated]
Seal(u64, SealSignature),
/// Put a Seal on it
Consensus(ConsensusEngineId, Vec<u8>),
/// Any 'non-system' digest item, opaque to the native code.
Other(Vec<u8>),
}
@@ -89,16 +94,20 @@ impl<Hash: Encode, AuthorityId: Encode, SealSignature: Encode> ::serde::Serializ
/// final runtime implementations for encoding/decoding its log items.
#[derive(PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Debug))]
#[allow(deprecated)]
pub enum DigestItemRef<'a, Hash: 'a, AuthorityId: 'a, SealSignature: 'a> {
/// Reference to `DigestItem::AuthoritiesChange`.
AuthoritiesChange(&'a [AuthorityId]),
/// Reference to `DigestItem::ChangesTrieRoot`.
ChangesTrieRoot(&'a Hash),
/// A sealed signature for testing
/// A deprecated sealed signature for testing
#[deprecated]
Seal(&'a u64, &'a SealSignature),
/// A sealed signature for testing
Consensus(&'a ConsensusEngineId, &'a [u8]),
/// Any 'non-system' digest item, opaque to the native code.
/// Reference to `DigestItem::Other`.
Other(&'a Vec<u8>),
Other(&'a [u8]),
}
/// Type of the digest item. Used to gain explicit control over `DigestItem` encoding
@@ -109,9 +118,10 @@ pub enum DigestItemRef<'a, Hash: 'a, AuthorityId: 'a, SealSignature: 'a> {
#[derive(Encode, Decode)]
enum DigestItemType {
Other = 0,
AuthoritiesChange,
ChangesTrieRoot,
Seal,
AuthoritiesChange = 1,
ChangesTrieRoot = 2,
Seal = 3,
Consensus = 4,
}
impl<Hash, AuthorityId, SealSignature> DigestItem<Hash, AuthorityId, SealSignature> {
@@ -124,11 +134,13 @@ impl<Hash, AuthorityId, SealSignature> DigestItem<Hash, AuthorityId, SealSignatu
}
/// Returns a 'referencing view' for this digest item.
#[allow(deprecated)]
fn dref<'a>(&'a self) -> DigestItemRef<'a, Hash, AuthorityId, SealSignature> {
match *self {
DigestItem::AuthoritiesChange(ref v) => DigestItemRef::AuthoritiesChange(v),
DigestItem::ChangesTrieRoot(ref v) => DigestItemRef::ChangesTrieRoot(v),
DigestItem::Seal(ref v, ref s) => DigestItemRef::Seal(v, s),
DigestItem::Consensus(ref v, ref s) => DigestItemRef::Consensus(v, s),
DigestItem::Other(ref v) => DigestItemRef::Other(v),
}
}
@@ -158,6 +170,7 @@ impl<Hash: Encode, AuthorityId: Encode, SealSignature: Encode> Encode for Digest
}
impl<Hash: Decode, AuthorityId: Decode, SealSignature: Decode> Decode for DigestItem<Hash, AuthorityId, SealSignature> {
#[allow(deprecated)]
fn decode<I: Input>(input: &mut I) -> Option<Self> {
let item_type: DigestItemType = Decode::decode(input)?;
match item_type {
@@ -171,6 +184,10 @@ impl<Hash: Decode, AuthorityId: Decode, SealSignature: Decode> Decode for Digest
let vals: (u64, SealSignature) = Decode::decode(input)?;
Some(DigestItem::Seal(vals.0, vals.1))
},
DigestItemType::Consensus => {
let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
Some(DigestItem::Consensus(vals.0, vals.1))
}
DigestItemType::Other => Some(DigestItem::Other(
Decode::decode(input)?,
)),
@@ -196,6 +213,7 @@ impl<'a, Hash: Codec + Member, AuthorityId: Codec + Member, SealSignature: Codec
}
}
#[allow(deprecated)]
impl<'a, Hash: Encode, AuthorityId: Encode, SealSignature: Encode> Encode for DigestItemRef<'a, Hash, AuthorityId, SealSignature> {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
@@ -213,6 +231,10 @@ impl<'a, Hash: Encode, AuthorityId: Encode, SealSignature: Encode> Encode for Di
DigestItemType::Seal.encode_to(&mut v);
(val, sig).encode_to(&mut v);
},
DigestItemRef::Consensus(val, sig) => {
DigestItemType::Consensus.encode_to(&mut v);
(val, sig).encode_to(&mut v);
},
DigestItemRef::Other(val) => {
DigestItemType::Other.encode_to(&mut v);
val.encode_to(&mut v);
@@ -229,6 +251,7 @@ mod tests {
use substrate_primitives::hash::H512 as Signature;
#[test]
#[allow(deprecated)]
fn should_serialize_digest() {
let digest = Digest {
logs: vec![