From b7e0db725d1ce4747024cb9bfee3c0bb71a6c8ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Mon, 26 Nov 2018 14:29:13 +0000 Subject: [PATCH] core, node: use grandpa block import for locally sealed aura blocks (#1167) * core, node: use grandpa block import for locally sealed aura blocks * core: impl DerefMut for FullComponents * node: take grandpa_import_setup from service config --- substrate/core/consensus/aura/src/lib.rs | 10 +++++++--- substrate/core/service/src/components.rs | 8 +++++++- substrate/core/service/src/lib.rs | 4 ---- substrate/node/cli/src/service.rs | 22 +++++++++++++--------- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/substrate/core/consensus/aura/src/lib.rs b/substrate/core/consensus/aura/src/lib.rs index 1869eba5dd..0313f7af3c 100644 --- a/substrate/core/consensus/aura/src/lib.rs +++ b/substrate/core/consensus/aura/src/lib.rs @@ -146,17 +146,19 @@ impl CompatibleDigestItem for generic::DigestItem( +pub fn start_aura( config: Config, client: Arc, + block_import: Arc, env: Arc, sync_oracle: SO, ) -> impl Future where B: Block, - C: Authorities + BlockImport + ChainHead, + C: Authorities + ChainHead, E: Environment, E::Proposer: Proposer, + I: BlockImport, SO: SyncOracle + Send + Clone, DigestItemFor: CompatibleDigestItem, Error: ::std::error::Error + Send + 'static + From<::consensus_common::Error>, @@ -164,6 +166,7 @@ pub fn start_aura( let make_authorship = move || { let config = config.clone(); let client = client.clone(); + let block_import = block_import.clone(); let env = env.clone(); let sync_oracle = sync_oracle.clone(); @@ -225,7 +228,7 @@ pub fn start_aura( } }; - let block_import = client.clone(); + let block_import = block_import.clone(); Either::A(proposal_work .map(move |b| { let (header, body) = b.deconstruct(); @@ -542,6 +545,7 @@ mod tests { local_key: Some(Arc::new(key.clone().into())), slot_duration: SLOT_DURATION }, + client.clone(), client, environ.clone(), DummyOracle, diff --git a/substrate/core/service/src/components.rs b/substrate/core/service/src/components.rs index b7d1781f3f..cc6771a634 100644 --- a/substrate/core/service/src/components.rs +++ b/substrate/core/service/src/components.rs @@ -16,7 +16,7 @@ //! Substrate service components. -use std::{sync::Arc, net::SocketAddr, marker::PhantomData, ops::Deref}; +use std::{sync::Arc, net::SocketAddr, marker::PhantomData, ops::Deref, ops::DerefMut}; use serde::{Serialize, de::DeserializeOwned}; use tokio::runtime::TaskExecutor; use chain_spec::{ChainSpec, Properties}; @@ -369,6 +369,12 @@ impl Deref for FullComponents { } } +impl DerefMut for FullComponents { + fn deref_mut(&mut self) -> &mut Service { + &mut self.service + } +} + impl Components for FullComponents { type Factory = Factory; type Executor = FullExecutor; diff --git a/substrate/core/service/src/lib.rs b/substrate/core/service/src/lib.rs index aebf447149..917b3d7482 100644 --- a/substrate/core/service/src/lib.rs +++ b/substrate/core/service/src/lib.rs @@ -310,10 +310,6 @@ impl Service None } } - - pub fn config(&self) -> &FactoryFullConfiguration { - &self.config - } } impl Service where Components: components::Components { diff --git a/substrate/node/cli/src/service.rs b/substrate/node/cli/src/service.rs index 1bd61bc6c4..1a4df71946 100644 --- a/substrate/node/cli/src/service.rs +++ b/substrate/node/cli/src/service.rs @@ -50,7 +50,7 @@ pub struct NodeConfig { // FIXME: rather than putting this on the config, let's have an actual intermediate setup state // https://github.com/paritytech/substrate/issues/1134 - pub grandpa_link_half: Option>, + pub grandpa_import_setup: Option<(Arc>, grandpa::LinkHalfForService)>, } impl Default for NodeConfig where F: substrate_service::ServiceFactory { @@ -58,7 +58,7 @@ impl Default for NodeConfig where F: substrate_service::ServiceFactory { NodeConfig { grandpa_authority: false, grandpa_authority_only: false, - grandpa_link_half: None + grandpa_import_setup: None, } } } @@ -79,18 +79,19 @@ construct_service_factory! { { |config: FactoryFullConfiguration, executor: TaskExecutor| FullComponents::::new(config, executor) }, AuthoritySetup = { - |service: Self::FullService, executor: TaskExecutor, key: Arc| { + |mut service: Self::FullService, executor: TaskExecutor, key: Arc| { + 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"); + if service.config.custom.grandpa_authority { info!("Running Grandpa session as Authority {}", key.public()); - let link_half = service.config().custom.grandpa_link_half.as_ref().take() - .expect("Link Half is present for Full Services or setup failed before. qed"); let grandpa_fut = grandpa::run_grandpa( grandpa::Config { gossip_duration: Duration::new(4, 0), // FIXME: make this available through chainspec? local_key: Some(key.clone()), - name: Some(service.config().name.clone()) + name: Some(service.config.name.clone()) }, - (*link_half).clone(), + link_half, grandpa::NetworkBridge::new(service.network()) )?; @@ -104,6 +105,7 @@ construct_service_factory! { slot_duration: AURA_SLOT_DURATION, }, service.client(), + block_import.clone(), service.proposer(), service.network(), )); @@ -116,14 +118,16 @@ construct_service_factory! { FullImportQueue = AuraImportQueue, NothingExtra> { |config: &mut FactoryFullConfiguration , client: Arc>| { let (block_import, link_half) = grandpa::block_import::<_, _, _, ClientWithApi, FullClient>(client.clone(), client)?; - config.custom.grandpa_link_half = Some(link_half); + let block_import = Arc::new(block_import); + + config.custom.grandpa_import_setup = Some((block_import.clone(), link_half)); Ok(import_queue( AuraConfig { local_key: None, slot_duration: 5 }, - Arc::new(block_import), + block_import, NothingExtra, )) }},