Skip to content

Commit

Permalink
Single ParachainBlockImport instance (#1782)
Browse files Browse the repository at this point in the history
  • Loading branch information
davxy authored Oct 21, 2022
1 parent 73a53d9 commit 3d257b7
Showing 9 changed files with 154 additions and 121 deletions.
7 changes: 4 additions & 3 deletions client/consensus/aura/src/import_queue.rs
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
//! Parachain specific wrapper for the AuRa import queue.
use codec::Codec;
use cumulus_client_consensus_common::ParachainBlockImport;
use sc_client_api::{backend::AuxStore, BlockOf, UsageProvider};
use sc_consensus::{import_queue::DefaultImportQueue, BlockImport};
use sc_consensus_aura::AuraVerifier;
@@ -33,10 +34,10 @@ use sp_runtime::traits::Block as BlockT;
use std::{fmt::Debug, hash::Hash, sync::Arc};
use substrate_prometheus_endpoint::Registry;

/// Parameters of [`import_queue`].
/// Parameters for [`import_queue`].
pub struct ImportQueueParams<'a, I, C, CIDP, S> {
/// The block import to use.
pub block_import: I,
pub block_import: ParachainBlockImport<I>,
/// The client to interact with the chain.
pub client: Arc<C>,
/// The inherent data providers, to create the inherent data.
@@ -83,7 +84,7 @@ where
CIDP::InherentDataProviders: InherentDataProviderExt + Send + Sync,
{
sc_consensus_aura::import_queue::<P, _, _, _, _, _>(sc_consensus_aura::ImportQueueParams {
block_import: cumulus_client_consensus_common::ParachainBlockImport::new(block_import),
block_import,
justification_import: None,
client,
create_inherent_data_providers,
34 changes: 17 additions & 17 deletions client/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
@@ -71,6 +71,22 @@ impl<B, CIDP, W> Clone for AuraConsensus<B, CIDP, W> {
}
}

/// Parameters of [`AuraConsensus::build`].
pub struct BuildAuraConsensusParams<PF, BI, CIDP, Client, BS, SO> {
pub proposer_factory: PF,
pub create_inherent_data_providers: CIDP,
pub block_import: ParachainBlockImport<BI>,
pub para_client: Arc<Client>,
pub backoff_authoring_blocks: Option<BS>,
pub sync_oracle: SO,
pub keystore: SyncCryptoStorePtr,
pub force_authoring: bool,
pub slot_duration: SlotDuration,
pub telemetry: Option<TelemetryHandle>,
pub block_proposal_slot_portion: SlotProportion,
pub max_block_proposal_slot_portion: Option<SlotProportion>,
}

impl<B, CIDP> AuraConsensus<B, CIDP, ()>
where
B: BlockT,
@@ -117,7 +133,7 @@ where
let worker = sc_consensus_aura::build_aura_worker::<P, _, _, _, _, _, _, _, _>(
BuildAuraWorkerParams {
client: para_client,
block_import: ParachainBlockImport::new(block_import),
block_import,
justification_sync_link: (),
proposer_factory,
sync_oracle,
@@ -216,19 +232,3 @@ where
Some(ParachainCandidate { block: res.block, proof: res.storage_proof })
}
}

/// Parameters of [`AuraConsensus::build`].
pub struct BuildAuraConsensusParams<PF, BI, CIDP, Client, BS, SO> {
pub proposer_factory: PF,
pub create_inherent_data_providers: CIDP,
pub block_import: BI,
pub para_client: Arc<Client>,
pub backoff_authoring_blocks: Option<BS>,
pub sync_oracle: SO,
pub keystore: SyncCryptoStorePtr,
pub force_authoring: bool,
pub slot_duration: SlotDuration,
pub telemetry: Option<TelemetryHandle>,
pub block_proposal_slot_portion: SlotProportion,
pub max_block_proposal_slot_portion: Option<SlotProportion>,
}
6 changes: 6 additions & 0 deletions client/consensus/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -83,6 +83,12 @@ impl<I> ParachainBlockImport<I> {
}
}

impl<I: Clone> Clone for ParachainBlockImport<I> {
fn clone(&self) -> Self {
ParachainBlockImport(self.0.clone())
}
}

#[async_trait::async_trait]
impl<Block, I> BlockImport<Block> for ParachainBlockImport<I>
where
12 changes: 4 additions & 8 deletions client/consensus/relay-chain/src/import_queue.rs
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@

use std::{marker::PhantomData, sync::Arc};

use cumulus_client_consensus_common::ParachainBlockImport;

use sc_consensus::{
import_queue::{BasicQueue, Verifier as VerifierT},
BlockImport, BlockImportParams,
@@ -103,7 +105,7 @@ where
/// Start an import queue for a Cumulus collator that does not uses any special authoring logic.
pub fn import_queue<Client, Block: BlockT, I, CIDP>(
client: Arc<Client>,
block_import: I,
block_import: ParachainBlockImport<I>,
create_inherent_data_providers: CIDP,
spawner: &impl sp_core::traits::SpawnEssentialNamed,
registry: Option<&substrate_prometheus_endpoint::Registry>,
@@ -117,11 +119,5 @@ where
{
let verifier = Verifier::new(client, create_inherent_data_providers);

Ok(BasicQueue::new(
verifier,
Box::new(cumulus_client_consensus_common::ParachainBlockImport::new(block_import)),
None,
spawner,
registry,
))
Ok(BasicQueue::new(verifier, Box::new(block_import), None, spawner, registry))
}
8 changes: 3 additions & 5 deletions client/consensus/relay-chain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -90,16 +90,14 @@ where
para_id: ParaId,
proposer_factory: PF,
create_inherent_data_providers: CIDP,
block_import: BI,
block_import: ParachainBlockImport<BI>,
relay_chain_interface: RCInterface,
) -> Self {
Self {
para_id,
proposer_factory: Arc::new(Mutex::new(proposer_factory)),
create_inherent_data_providers: Arc::new(create_inherent_data_providers),
block_import: Arc::new(futures::lock::Mutex::new(ParachainBlockImport::new(
block_import,
))),
block_import: Arc::new(futures::lock::Mutex::new(block_import)),
relay_chain_interface,
_phantom: PhantomData,
}
@@ -222,7 +220,7 @@ pub struct BuildRelayChainConsensusParams<PF, BI, CIDP, RCInterface> {
pub para_id: ParaId,
pub proposer_factory: PF,
pub create_inherent_data_providers: CIDP,
pub block_import: BI,
pub block_import: ParachainBlockImport<BI>,
pub relay_chain_interface: RCInterface,
}

1 change: 0 additions & 1 deletion parachain-template/node/src/command.rs
Original file line number Diff line number Diff line change
@@ -217,7 +217,6 @@ pub fn run() -> Result<()> {
let partials = new_partial(&config)?;
let db = partials.backend.expose_db();
let storage = partials.backend.expose_storage();

cmd.run(config, partials.client.clone(), db, storage)
}),
BenchmarkCmd::Machine(cmd) =>
24 changes: 17 additions & 7 deletions parachain-template/node/src/service.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,9 @@ use parachain_template_runtime::{opaque::Block, Hash, RuntimeApi};

// Cumulus Imports
use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};
use cumulus_client_consensus_common::ParachainConsensus;
use cumulus_client_consensus_common::{
ParachainBlockImport as TParachainBlockImport, ParachainConsensus,
};
use cumulus_client_network::BlockAnnounceValidator;
use cumulus_client_service::{
prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,
@@ -51,6 +53,8 @@ type ParachainClient = TFullClient<Block, RuntimeApi, ParachainExecutor>;

type ParachainBackend = TFullBackend<Block>;

type ParachainBlockImport = TParachainBlockImport<Arc<ParachainClient>>;

/// Starts a `ServiceBuilder` for a full service.
///
/// Use this macro if you don't actually need the full service, but just the builder in order to
@@ -64,7 +68,7 @@ pub fn new_partial(
(),
sc_consensus::DefaultImportQueue<Block, ParachainClient>,
sc_transaction_pool::FullPool<Block, ParachainClient>,
(Option<Telemetry>, Option<TelemetryWorkerHandle>),
(ParachainBlockImport, Option<Telemetry>, Option<TelemetryWorkerHandle>),
>,
sc_service::Error,
> {
@@ -109,8 +113,11 @@ pub fn new_partial(
client.clone(),
);

let block_import = ParachainBlockImport::new(client.clone());

let import_queue = build_import_queue(
client.clone(),
block_import.clone(),
config,
telemetry.as_ref().map(|telemetry| telemetry.handle()),
&task_manager,
@@ -124,7 +131,7 @@ pub fn new_partial(
task_manager,
transaction_pool,
select_chain: (),
other: (telemetry, telemetry_worker_handle),
other: (block_import, telemetry, telemetry_worker_handle),
})
}

@@ -163,7 +170,7 @@ async fn start_node_impl(
let parachain_config = prepare_node_config(parachain_config);

let params = new_partial(&parachain_config)?;
let (mut telemetry, telemetry_worker_handle) = params.other;
let (block_import, mut telemetry, telemetry_worker_handle) = params.other;

let client = params.client.clone();
let backend = params.backend.clone();
@@ -255,6 +262,7 @@ async fn start_node_impl(
if validator {
let parachain_consensus = build_consensus(
client.clone(),
block_import,
prometheus_registry.as_ref(),
telemetry.as_ref().map(|t| t.handle()),
&task_manager,
@@ -304,6 +312,7 @@ async fn start_node_impl(
/// Build the import queue for the parachain runtime.
fn build_import_queue(
client: Arc<ParachainClient>,
block_import: ParachainBlockImport,
config: &Configuration,
telemetry: Option<TelemetryHandle>,
task_manager: &TaskManager,
@@ -318,8 +327,8 @@ fn build_import_queue(
_,
_,
>(cumulus_client_consensus_aura::ImportQueueParams {
block_import: client.clone(),
client: client.clone(),
block_import,
client,
create_inherent_data_providers: move |_, _| async move {
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();

@@ -340,6 +349,7 @@ fn build_import_queue(

fn build_consensus(
client: Arc<ParachainClient>,
block_import: ParachainBlockImport,
prometheus_registry: Option<&Registry>,
telemetry: Option<TelemetryHandle>,
task_manager: &TaskManager,
@@ -389,7 +399,7 @@ fn build_consensus(
Ok((slot, timestamp, parachain_inherent))
}
},
block_import: client.clone(),
block_import,
para_client: client,
backoff_authoring_blocks: Option::<()>::None,
sync_oracle,
Loading

0 comments on commit 3d257b7

Please sign in to comment.