diff --git a/substrate/.gitlab-ci.yml b/substrate/.gitlab-ci.yml index 3bca6f6be8..b803ce3d57 100644 --- a/substrate/.gitlab-ci.yml +++ b/substrate/.gitlab-ci.yml @@ -32,7 +32,7 @@ variables: when: on_success expire_in: 1 mos paths: - - target/release/polkadot + - target/release/substrate .determine_version: &determine_version | export VERSION=$(grep -m 1 "version =" Cargo.toml | awk '{print $3}' | tr -d '"' | tr -d "\n") @@ -49,6 +49,7 @@ test:rust:stable: &test script: - ./scripts/init.sh - export PATH="${CI_PROJECT_DIR}/cargo/bin/:$PATH" + - export RUST_BACKTRACE=1 - ./scripts/build.sh - time cargo test --all --release --locked tags: diff --git a/substrate/core/service/test/src/lib.rs b/substrate/core/service/test/src/lib.rs index 7593d1ad38..31ac007c4f 100644 --- a/substrate/core/service/test/src/lib.rs +++ b/substrate/core/service/test/src/lib.rs @@ -185,7 +185,7 @@ pub fn connectivity(spec: FactoryChainSpec) { { let mut network = TestNet::::new(&temp, spec.clone(), NUM_NODES, 0, vec![], 30400); info!("Checking star topology"); - let first_address = network.full_nodes[0].1.network().node_id().unwrap(); + let first_address = network.full_nodes[0].1.network().node_id().expect("No node address"); for (_, service) in network.full_nodes.iter().skip(1) { service.network().add_reserved_peer(first_address.clone()).expect("Error adding reserved peer"); } @@ -200,10 +200,10 @@ pub fn connectivity(spec: FactoryChainSpec) { { let mut network = TestNet::::new(&temp, spec, NUM_NODES as u32, 0, vec![], 30400); info!("Checking linked topology"); - let mut address = network.full_nodes[0].1.network().node_id().unwrap(); + let mut address = network.full_nodes[0].1.network().node_id().expect("No node address"); for (_, service) in network.full_nodes.iter().skip(1) { service.network().add_reserved_peer(address.clone()).expect("Error adding reserved peer"); - address = service.network().node_id().unwrap(); + address = service.network().node_id().expect("No node address"); } network.run_until_all_full(|_index, service| { service.network().status().num_peers == NUM_NODES as usize - 1 diff --git a/substrate/core/sr-primitives/src/generic/digest.rs b/substrate/core/sr-primitives/src/generic/digest.rs index 38d2262cd6..4d44ff5ce0 100644 --- a/substrate/core/sr-primitives/src/generic/digest.rs +++ b/substrate/core/sr-primitives/src/generic/digest.rs @@ -113,17 +113,11 @@ impl traits::DigestItem for D type AuthorityId = AuthorityId; fn as_authorities_change(&self) -> Option<&[Self::AuthorityId]> { - match *self { - DigestItem::AuthoritiesChange(ref authorities) => Some(authorities), - _ => None, - } + self.dref().as_authorities_change() } fn as_changes_trie_root(&self) -> Option<&Hash> { - match *self { - DigestItem::ChangesTrieRoot(ref changes_trie_root) => Some(changes_trie_root), - _ => None, - } + self.dref().as_changes_trie_root() } } @@ -150,6 +144,22 @@ impl Decode for DigestItem } } +impl<'a, Hash: Codec + Member, AuthorityId: Codec + Member> DigestItemRef<'a, Hash, AuthorityId> { + pub fn as_authorities_change(&self) -> Option<&'a [AuthorityId]> { + match *self { + DigestItemRef::AuthoritiesChange(ref authorities) => Some(authorities), + _ => None, + } + } + + pub fn as_changes_trie_root(&self) -> Option<&'a Hash> { + match *self { + DigestItemRef::ChangesTrieRoot(ref changes_trie_root) => Some(changes_trie_root), + _ => None, + } + } +} + impl<'a, Hash: Encode, AuthorityId: Encode> Encode for DigestItemRef<'a, Hash, AuthorityId> { fn encode(&self) -> Vec { let mut v = Vec::new(); diff --git a/substrate/core/sr-primitives/src/lib.rs b/substrate/core/sr-primitives/src/lib.rs index 465b1e3f29..aac96bc40f 100644 --- a/substrate/core/sr-primitives/src/lib.rs +++ b/substrate/core/sr-primitives/src/lib.rs @@ -326,7 +326,7 @@ macro_rules! impl_outer_log { ( $(#[$attr:meta])* pub enum $name:ident ($internal:ident: DigestItem<$( $genarg:ty ),*>) for $trait:ident { - $( $module:ident($( $item:ident ),*) ),* + $( $module:ident( $( $sitem:ident ),* ) ),* } ) => { /// Wrapper for all possible log entries for the `$trait` runtime. Provides binary-compatible @@ -343,7 +343,7 @@ macro_rules! impl_outer_log { #[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] $(#[$attr])* #[allow(non_camel_case_types)] - enum $internal { + pub enum InternalLog { $( $module($module::Log<$trait>), )* @@ -357,14 +357,27 @@ macro_rules! impl_outer_log { fn dref<'a>(&'a self) -> Option<$crate::generic::DigestItemRef<'a, $($genarg),*>> { match self.0 { $($( - $internal::$module($module::RawLog::$item(ref v)) => - Some($crate::generic::DigestItemRef::$item(v)), + $internal::$module($module::RawLog::$sitem(ref v)) => + Some($crate::generic::DigestItemRef::$sitem(v)), )*)* _ => 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()) + } + } + 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. @@ -375,8 +388,8 @@ macro_rules! impl_outer_log { fn from(gen: $crate::generic::DigestItem<$($genarg),*>) -> Self { match gen { $($( - $crate::generic::DigestItem::$item(value) => - $name($internal::$module($module::RawLog::$item(value))), + $crate::generic::DigestItem::$sitem(value) => + $name($internal::$module($module::RawLog::$sitem(value))), )*)* _ => gen.as_other() .and_then(|value| $crate::codec::Decode::decode(&mut &value[..])) @@ -417,10 +430,10 @@ macro_rules! impl_outer_log { } } - impl From<$module::Log<$trait>> for $internal { + impl From<$module::Log<$trait>> for InternalLog { /// Converts single module log item into `$internal`. fn from(x: $module::Log<$trait>) -> Self { - $internal::$module(x) + InternalLog::$module(x) } } )* @@ -431,6 +444,7 @@ macro_rules! impl_outer_log { mod tests { use substrate_primitives::hash::H256; use codec::{Encode as EncodeHidden, Decode as DecodeHidden}; + use traits::DigestItem; pub trait RuntimeT { type AuthorityId; @@ -442,31 +456,31 @@ mod tests { type AuthorityId = u64; } + mod a { + use super::RuntimeT; + pub type Log = RawLog<::AuthorityId>; + + #[derive(Serialize, Deserialize, Debug, Encode, Decode, PartialEq, Eq, Clone)] + pub enum RawLog { A1(AuthorityId), AuthoritiesChange(Vec), A3(AuthorityId) } + } + + mod b { + use super::RuntimeT; + pub type Log = RawLog<::AuthorityId>; + + #[derive(Serialize, Deserialize, Debug, Encode, Decode, PartialEq, Eq, Clone)] + pub enum RawLog { B1(AuthorityId), B2(AuthorityId) } + } + + // TODO try to avoid redundant brackets: a(AuthoritiesChange), b + impl_outer_log! { + pub enum Log(InternalLog: DigestItem) for Runtime { + a(AuthoritiesChange), b() + } + } + #[test] fn impl_outer_log_works() { - mod a { - use super::RuntimeT; - pub type Log = RawLog<::AuthorityId>; - - #[derive(Serialize, Deserialize, Debug, Encode, Decode, PartialEq, Eq, Clone)] - pub enum RawLog { A1(AuthorityId), AuthoritiesChange(Vec), A3(AuthorityId) } - } - - mod b { - use super::RuntimeT; - pub type Log = RawLog<::AuthorityId>; - - #[derive(Serialize, Deserialize, Debug, Encode, Decode, PartialEq, Eq, Clone)] - pub enum RawLog { B1(AuthorityId), B2(AuthorityId) } - } - - // TODO try to avoid redundant brackets: a(AuthoritiesChange), b - impl_outer_log! { - pub enum Log(InternalLog: DigestItem) for Runtime { - a(AuthoritiesChange), b() - } - } - // encode/decode regular item let b1: Log = b::RawLog::B1::(777).into(); let encoded_b1 = b1.encode(); @@ -492,5 +506,11 @@ mod tests { super::generic::DigestItem::AuthoritiesChange::(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()); } } diff --git a/substrate/core/sr-primitives/src/traits.rs b/substrate/core/sr-primitives/src/traits.rs index 2841708e6e..6c188c35f4 100644 --- a/substrate/core/sr-primitives/src/traits.rs +++ b/substrate/core/sr-primitives/src/traits.rs @@ -534,14 +534,10 @@ pub trait DigestItem: Codec + Member { type AuthorityId: Member; /// Returns Some if the entry is the `AuthoritiesChange` entry. - fn as_authorities_change(&self) -> Option<&[Self::AuthorityId]> { - None - } + fn as_authorities_change(&self) -> Option<&[Self::AuthorityId]>; /// Returns Some if the entry is the `ChangesTrieRoot` entry. - fn as_changes_trie_root(&self) -> Option<&Self::Hash> { - None - } + fn as_changes_trie_root(&self) -> Option<&Self::Hash>; } /// Something that provides an inherent for a runtime. diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs index b6dbda5c57..81a5ddea2e 100644 --- a/substrate/node/runtime/src/lib.rs +++ b/substrate/node/runtime/src/lib.rs @@ -40,7 +40,6 @@ extern crate substrate_primitives; #[macro_use] extern crate parity_codec_derive; -#[cfg_attr(not(feature = "std"), macro_use)] extern crate sr_std as rstd; extern crate srml_balances as balances; extern crate srml_consensus as consensus; @@ -67,7 +66,7 @@ use runtime_api::runtime::*; use runtime_primitives::ApplyResult; use runtime_primitives::transaction_validity::TransactionValidity; use runtime_primitives::generic; -use runtime_primitives::traits::{Convert, BlakeTwo256, DigestItem, Block as BlockT}; +use runtime_primitives::traits::{Convert, BlakeTwo256, Block as BlockT}; use version::{RuntimeVersion, ApiId}; use council::{motions as council_motions, voting as council_voting}; #[cfg(feature = "std")] @@ -192,25 +191,6 @@ impl contract::Trait for Runtime { type Event = Event; } -impl DigestItem for Log { - type Hash = Hash; - type AuthorityId = SessionKey; - - fn as_authorities_change(&self) -> Option<&[Self::AuthorityId]> { - match self.0 { - InternalLog::consensus(ref item) => item.as_authorities_change(), - _ => None, - } - } - - fn as_changes_trie_root(&self) -> Option<&Self::Hash> { - match self.0 { - InternalLog::system(ref item) => item.as_changes_trie_root(), - _ => None, - } - } -} - construct_runtime!( pub enum Runtime with Log(InternalLog: DigestItem) where Block = Block,