diff --git a/substrate/.gitignore b/substrate/.gitignore index 4caeb6cc53..21dee82d81 100644 --- a/substrate/.gitignore +++ b/substrate/.gitignore @@ -16,3 +16,5 @@ node/runtime/wasm/target/ polkadot.* .DS_Store .idea/ +nohup.out +rls*.log diff --git a/substrate/core/consensus/aura/src/lib.rs b/substrate/core/consensus/aura/src/lib.rs index 064738216f..e2c1091f14 100644 --- a/substrate/core/consensus/aura/src/lib.rs +++ b/substrate/core/consensus/aura/src/lib.rs @@ -26,9 +26,9 @@ //! Blocks from future steps will be either deferred or rejected depending on how //! far in the future they are. -use std::{sync::Arc, time::Duration, thread}; +use std::{sync::Arc, time::Duration, thread, marker::PhantomData, hash::Hash, fmt::Debug}; -use parity_codec::Encode; +use parity_codec::{Encode, Decode}; use consensus_common::{ Authorities, BlockImport, Environment, Proposer, ForkChoiceStrategy }; @@ -41,7 +41,7 @@ use runtime_primitives::{generic, generic::BlockId, Justification}; use runtime_primitives::traits::{ Block, Header, Digest, DigestItemFor, DigestItem, ProvideRuntimeApi }; -use primitives::{ed25519, Pair}; +use primitives::Pair; use inherents::{InherentDataProviders, InherentData, RuntimeString}; use futures::{Stream, Future, IntoFuture, future}; @@ -60,8 +60,8 @@ pub use aura_slots::SlotDuration; pub use aura_primitives::*; pub use consensus_common::SyncOracle; -type AuthorityId = ed25519::Public; -type Signature = ed25519::Signature; +type AuthorityId

=

::Public; +type Signature

=

::Signature; /// A handle to the network. This is generally implemented by providing some /// handle to a gossip service or similar. @@ -76,7 +76,9 @@ pub trait Network: Clone { } /// Get slot author for given block along with authorities. -fn slot_author(slot_num: u64, authorities: &[AuthorityId]) -> Option { +fn slot_author(slot_num: u64, authorities: &[AuthorityId

]) -> Option> + where P::Public: Clone, +{ if authorities.is_empty() { return None } let idx = slot_num % (authorities.len() as u64); @@ -88,7 +90,7 @@ fn slot_author(slot_num: u64, authorities: &[AuthorityId]) -> Option Option { @@ -110,25 +112,27 @@ fn inherent_to_common_error(err: RuntimeString) -> consensus_common::Error { } /// A digest item which is usable with aura consensus. -pub trait CompatibleDigestItem: Sized { +pub trait CompatibleDigestItem: Sized { /// Construct a digest item which is a slot number and a signature on the /// hash. - fn aura_seal(slot_number: u64, signature: Signature) -> Self; + fn aura_seal(slot_number: u64, signature: Signature) -> Self; /// If this item is an Aura seal, return the slot number and signature. - fn as_aura_seal(&self) -> Option<(u64, Signature)>; + fn as_aura_seal(&self) -> Option<(u64, Signature)>; } -impl CompatibleDigestItem for generic::DigestItem { +impl CompatibleDigestItem for generic::DigestItem> + where T::Signature: Clone +{ /// Construct a digest item which is a slot number and a signature on the /// hash. - fn aura_seal(slot_number: u64, signature: Signature) -> Self { + fn aura_seal(slot_number: u64, signature: Signature) -> Self { generic::DigestItem::Seal(slot_number, signature) } /// If this item is an Aura seal, return the slot number and signature. - fn as_aura_seal(&self) -> Option<(u64, Signature)> { + fn as_aura_seal(&self) -> Option<(u64, Signature)> { match self { - generic::DigestItem::Seal(slot, ref sig) => Some((*slot, sig.clone().into())), + generic::DigestItem::Seal(slot, ref sig) => Some((*slot, (*sig).clone())), _ => None } } @@ -147,9 +151,9 @@ impl SlotCompatible for AuraSlotCompatible { } /// Start the aura worker in a separate thread. -pub fn start_aura_thread( +pub fn start_aura_thread( slot_duration: SlotDuration, - local_key: Arc, + local_key: Arc

, client: Arc, block_import: Arc, env: Arc, @@ -164,9 +168,11 @@ pub fn start_aura_thread( <>::Create as IntoFuture>::Future: Send + 'static, I: BlockImport + Send + Sync + 'static, Error: From + From + 'static, + P: Pair + Send + Sync + 'static, + P::Public: Encode + Decode + Eq + Clone + Debug + Hash + Send + Sync + 'static, SO: SyncOracle + Send + Sync + Clone + 'static, OnExit: Future + Send + 'static, - DigestItemFor: CompatibleDigestItem + DigestItem + 'static, + DigestItemFor: CompatibleDigestItem

+ DigestItem> + 'static, Error: ::std::error::Error + Send + From<::consensus_common::Error> + 'static, { let worker = AuraWorker { @@ -184,9 +190,9 @@ pub fn start_aura_thread( } /// Start the aura worker. The returned future should be run in a tokio runtime. -pub fn start_aura( +pub fn start_aura( slot_duration: SlotDuration, - local_key: Arc, + local_key: Arc

, client: Arc, block_import: Arc, env: Arc, @@ -201,8 +207,10 @@ pub fn start_aura( <>::Create as IntoFuture>::Future: Send + 'static, I: BlockImport + Send + Sync + 'static, Error: From + From, + P: Pair + Send + Sync + 'static, + P::Public: Hash + Eq + Send + Sync + Clone + Debug + Encode + Decode + 'static, SO: SyncOracle + Send + Sync + Clone, - DigestItemFor: CompatibleDigestItem + DigestItem, + DigestItemFor: CompatibleDigestItem

+ DigestItem>, Error: ::std::error::Error + Send + 'static + From<::consensus_common::Error>, OnExit: Future, { @@ -219,24 +227,26 @@ pub fn start_aura( ) } -struct AuraWorker { +struct AuraWorker { client: Arc, block_import: Arc, env: Arc, - local_key: Arc, + local_key: Arc

, sync_oracle: SO, inherent_data_providers: InherentDataProviders, } -impl SlotWorker for AuraWorker where +impl SlotWorker for AuraWorker where C: Authorities, E: Environment, E::Proposer: Proposer, <>::Create as IntoFuture>::Future: Send + 'static, I: BlockImport + Send + Sync + 'static, + P: Pair + Send + Sync + 'static, + P::Public: Hash + Eq + Send + Sync + Clone + Debug + Encode + Decode + 'static, Error: From + From, SO: SyncOracle + Send + Clone, - DigestItemFor: CompatibleDigestItem + DigestItem, + DigestItemFor: CompatibleDigestItem

+ DigestItem>, Error: ::std::error::Error + Send + 'static + From<::consensus_common::Error>, { type OnSlot = Box + Send>; @@ -284,10 +294,10 @@ impl SlotWorker for AuraWorker whe ); return Box::new(future::ok(())); } - - let proposal_work = match slot_author(slot_num, &authorities) { + let maybe_author = slot_author::

(slot_num, &authorities); + let proposal_work = match maybe_author { None => return Box::new(future::ok(())), - Some(author) => if author.0 == public_key.0 { + Some(author) => if author == public_key { debug!( target: "aura", "Starting authorship at slot {}; timestamp = {}", slot_num, @@ -347,7 +357,7 @@ impl SlotWorker for AuraWorker whe // add it to a digest item. let to_sign = (slot_num, pre_hash).encode(); let signature = pair.sign(&to_sign[..]); - let item = as CompatibleDigestItem>::aura_seal( + let item = as CompatibleDigestItem

>::aura_seal( slot_num, signature, ); @@ -391,9 +401,10 @@ impl SlotWorker for AuraWorker whe /// if it's successful, returns the pre-header, the slot number, and the signat. // // FIXME #1018 needs misbehavior types -fn check_header(slot_now: u64, mut header: B::Header, hash: B::Hash, authorities: &[AuthorityId]) - -> Result, String> - where DigestItemFor: CompatibleDigestItem +fn check_header(slot_now: u64, mut header: B::Header, hash: B::Hash, authorities: &[AuthorityId

]) + -> Result, String> + where DigestItemFor: CompatibleDigestItem

, + P::Public: Clone + AsRef, { let digest_item = match header.digest_mut().pop() { Some(x) => x, @@ -411,16 +422,16 @@ fn check_header(slot_now: u64, mut header: B::Header, hash: B::Hash, a // check the signature is valid under the expected authority and // chain state. - let expected_author = match slot_author(slot_num, &authorities) { + let expected_author = match slot_author::

(slot_num, &authorities) { None => return Err("Slot Author not found".to_string()), Some(author) => author }; let pre_hash = header.hash(); let to_sign = (slot_num, pre_hash).encode(); - let public = ed25519::Public(expected_author.0); + let public = expected_author; - if ed25519::Pair::verify(&sig, &to_sign[..], public) { + if P::verify(&sig, &to_sign[..], public) { Ok(CheckedHeader::Checked(header, slot_num, sig)) } else { Err(format!("Bad signature on {:?}", hash)) @@ -442,13 +453,15 @@ pub trait ExtraVerification: Send + Sync { } /// A verifier for Aura blocks. -pub struct AuraVerifier { +pub struct AuraVerifier { client: Arc, extra: E, + phantom: PhantomData

, inherent_data_providers: inherents::InherentDataProviders, } -impl AuraVerifier +impl AuraVerifier + where P: Send + Sync + 'static { fn check_inherents( &self, @@ -511,11 +524,13 @@ impl ExtraVerification for NothingExtra { } } -impl Verifier for AuraVerifier where +impl Verifier for AuraVerifier where C: Authorities + ProvideRuntimeApi + Send + Sync, C::Api: BlockBuilderApi, - DigestItemFor: CompatibleDigestItem + DigestItem, + DigestItemFor: CompatibleDigestItem

+ DigestItem>, E: ExtraVerification, + P: Pair + Send + Sync + 'static, + P::Public: Send + Sync + Hash + Eq + Clone + Decode + Encode + Debug + AsRef + 'static, { fn verify( &self, @@ -523,7 +538,7 @@ impl Verifier for AuraVerifier where header: B::Header, justification: Option, mut body: Option>, - ) -> Result<(ImportBlock, Option>), String> { + ) -> Result<(ImportBlock, Option>>), String> { let mut inherent_data = self.inherent_data_providers.create_inherent_data().map_err(String::from)?; let (timestamp_now, slot_now) = AuraSlotCompatible::extract_timestamp_and_slot(&inherent_data) .map_err(|e| format!("Could not extract timestamp and slot: {:?}", e))?; @@ -539,7 +554,7 @@ impl Verifier for AuraVerifier where // we add one to allow for some small drift. // FIXME #1019 in the future, alter this queue to allow deferring of headers - let checked_header = check_header::(slot_now + 1, header, hash, &authorities[..])?; + let checked_header = check_header::(slot_now + 1, header, hash, &authorities[..])?; match checked_header { CheckedHeader::Checked(pre_header, slot_num, sig) => { let item = >::aura_seal(slot_num, sig); @@ -617,7 +632,7 @@ fn register_aura_inherent_data_provider( } /// Start an import queue for the Aura consensus algorithm. -pub fn import_queue( +pub fn import_queue( slot_duration: SlotDuration, block_import: SharedBlockImport, justification_import: Option>, @@ -628,13 +643,20 @@ pub fn import_queue( B: Block, C: 'static + Authorities + ProvideRuntimeApi + Send + Sync, C::Api: BlockBuilderApi, - DigestItemFor: CompatibleDigestItem + DigestItem, + DigestItemFor: CompatibleDigestItem

+ DigestItem>, E: 'static + ExtraVerification, + P: Pair + Send + Sync + 'static, + P::Public: Clone + Eq + Send + Sync + Hash + Debug + Encode + Decode + AsRef, { register_aura_inherent_data_provider(&inherent_data_providers, slot_duration.get())?; let verifier = Arc::new( - AuraVerifier { client: client.clone(), extra, inherent_data_providers } + AuraVerifier { + client: client.clone(), + extra, + inherent_data_providers, + phantom: PhantomData, + } ); Ok(BasicQueue::new(verifier, block_import, justification_import)) } @@ -650,6 +672,7 @@ mod tests { use parking_lot::Mutex; use tokio::runtime::current_thread; use keyring::ed25519::Keyring; + use primitives::ed25519; use client::BlockchainEvents; use test_client; @@ -664,7 +687,7 @@ mod tests { type Proposer = DummyProposer; type Error = Error; - fn init(&self, parent_header: &::Header, _authorities: &[AuthorityId]) + fn init(&self, parent_header: &::Header, _authorities: &[AuthorityId]) -> Result { Ok(DummyProposer(parent_header.number + 1, self.0.clone())) @@ -690,7 +713,7 @@ mod tests { impl TestNetFactory for AuraTestNet { type Specialization = DummySpecialization; - type Verifier = AuraVerifier; + type Verifier = AuraVerifier; type PeerData = (); /// Create new test network with peers and given config. @@ -717,6 +740,7 @@ mod tests { client, extra: NothingExtra, inherent_data_providers, + phantom: Default::default(), }) } @@ -775,7 +799,7 @@ mod tests { &inherent_data_providers, slot_duration.get() ).expect("Registers aura inherent data provider"); - let aura = start_aura( + let aura = start_aura::<_, _, _, _, ed25519::Pair, _, _, _>( slot_duration, Arc::new(key.clone().into()), client.clone(), diff --git a/substrate/core/primitives/src/sr25519.rs b/substrate/core/primitives/src/sr25519.rs index ee1727b79a..a60ee94b7f 100644 --- a/substrate/core/primitives/src/sr25519.rs +++ b/substrate/core/primitives/src/sr25519.rs @@ -501,7 +501,7 @@ impl Pair { #[cfg(test)] mod test { use super::*; - use crate::{Pair as PairT, crypto::{Ss58Codec, DEV_PHRASE, DEV_ADDRESS}}; + use crate::crypto::{Ss58Codec, DEV_PHRASE, DEV_ADDRESS}; use hex_literal::{hex, hex_impl}; #[test] diff --git a/substrate/core/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm b/substrate/core/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm index c4df3f06b5..4f75adc74d 100644 Binary files a/substrate/core/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm and b/substrate/core/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm differ diff --git a/substrate/node-template/src/service.rs b/substrate/node-template/src/service.rs index aa1d25963b..ae98d01de4 100644 --- a/substrate/node-template/src/service.rs +++ b/substrate/node-template/src/service.rs @@ -85,7 +85,7 @@ construct_service_factory! { Self::Block, > { |config: &mut FactoryFullConfiguration , client: Arc>| - import_queue( + import_queue::<_, _, _, Pair>( SlotDuration::get_or_compute(&*client)?, client.clone(), None, @@ -98,7 +98,7 @@ construct_service_factory! { Self::Block, > { |config: &mut FactoryFullConfiguration, client: Arc>| - import_queue( + import_queue::<_, _, _, Pair>( SlotDuration::get_or_compute(&*client)?, client.clone(), None, diff --git a/substrate/node/cli/src/service.rs b/substrate/node/cli/src/service.rs index e2c1c67236..64f191dc4b 100644 --- a/substrate/node/cli/src/service.rs +++ b/substrate/node/cli/src/service.rs @@ -25,7 +25,7 @@ use client; use consensus::{import_queue, start_aura, AuraImportQueue, SlotDuration, NothingExtra}; use grandpa; use node_executor; -use primitives::{Pair as PairT, ed25519::Pair}; +use primitives::{Pair as PairT, ed25519}; use node_primitives::Block; use node_runtime::{GenesisConfig, RuntimeApi}; use substrate_service::{ @@ -76,7 +76,7 @@ construct_service_factory! { { |config: FactoryFullConfiguration, executor: TaskExecutor| FullComponents::::new(config, executor) }, AuthoritySetup = { - |mut service: Self::FullService, executor: TaskExecutor, local_key: Option>| { + |mut service: Self::FullService, executor: TaskExecutor, local_key: Option>| { let (block_import, link_half) = service.config.custom.grandpa_import_setup.take() .expect("Link Half and Block Import are present for Full Services or setup failed before. qed"); @@ -133,7 +133,7 @@ construct_service_factory! { config.custom.grandpa_import_setup = Some((block_import.clone(), link_half)); - import_queue( + import_queue::<_, _, _, ed25519::Pair>( slot_duration, block_import, Some(justification_import), @@ -144,16 +144,16 @@ construct_service_factory! { }}, LightImportQueue = AuraImportQueue { |config: &FactoryFullConfiguration, client: Arc>| { - import_queue( - SlotDuration::get_or_compute(&*client)?, - client.clone(), - None, - client, - NothingExtra, - config.custom.inherent_data_providers.clone(), - ).map_err(Into::into) - } - }, + import_queue::<_, _, _, ed25519::Pair>( + SlotDuration::get_or_compute(&*client)?, + client.clone(), + None, + client, + NothingExtra, + config.custom.inherent_data_providers.clone(), + ).map_err(Into::into) + } + }, } } diff --git a/substrate/node/runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm b/substrate/node/runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm index 446e9eaad9..5076de93a3 100644 Binary files a/substrate/node/runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm and b/substrate/node/runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm differ