mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
parachain-template: Simplify it (#3801)
Also while doing this, move slot duration fetching into the AURA code.
This commit is contained in:
@@ -42,7 +42,7 @@ use sp_api::{CallApiAt, ProvideRuntimeApi};
|
||||
use sp_application_crypto::AppPublic;
|
||||
use sp_blockchain::HeaderBackend;
|
||||
use sp_consensus::SyncOracle;
|
||||
use sp_consensus_aura::{AuraApi, SlotDuration};
|
||||
use sp_consensus_aura::AuraApi;
|
||||
use sp_core::crypto::Pair;
|
||||
use sp_inherents::CreateInherentDataProviders;
|
||||
use sp_keystore::KeystorePtr;
|
||||
@@ -74,8 +74,6 @@ pub struct Params<BI, CIDP, Client, RClient, SO, Proposer, CS> {
|
||||
pub para_id: ParaId,
|
||||
/// A handle to the relay-chain client's "Overseer" or task orchestrator.
|
||||
pub overseer_handle: OverseerHandle,
|
||||
/// The length of slots in this chain.
|
||||
pub slot_duration: SlotDuration,
|
||||
/// The length of slots in the relay chain.
|
||||
pub relay_chain_slot_duration: Duration,
|
||||
/// The underlying block proposer this should call into.
|
||||
@@ -197,11 +195,16 @@ where
|
||||
Ok(Some(h)) => h,
|
||||
};
|
||||
|
||||
let slot_duration = match params.para_client.runtime_api().slot_duration(parent_hash) {
|
||||
Ok(d) => d,
|
||||
Err(e) => reject_with_error!(e),
|
||||
};
|
||||
|
||||
let claim = match collator_util::claim_slot::<_, _, P>(
|
||||
&*params.para_client,
|
||||
parent_hash,
|
||||
&relay_parent_header,
|
||||
params.slot_duration,
|
||||
slot_duration,
|
||||
params.relay_chain_slot_duration,
|
||||
¶ms.keystore,
|
||||
)
|
||||
|
||||
@@ -70,7 +70,6 @@ impl NaiveEquivocationDefender {
|
||||
struct Verifier<P, Client, Block, CIDP> {
|
||||
client: Arc<Client>,
|
||||
create_inherent_data_providers: CIDP,
|
||||
slot_duration: SlotDuration,
|
||||
defender: NaiveEquivocationDefender,
|
||||
telemetry: Option<TelemetryHandle>,
|
||||
_phantom: std::marker::PhantomData<fn() -> (Block, P)>,
|
||||
@@ -110,7 +109,13 @@ where
|
||||
format!("Could not fetch authorities at {:?}: {}", parent_hash, e)
|
||||
})?;
|
||||
|
||||
let slot_now = slot_now(self.slot_duration);
|
||||
let slot_duration = self
|
||||
.client
|
||||
.runtime_api()
|
||||
.slot_duration(parent_hash)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let slot_now = slot_now(slot_duration);
|
||||
let res = aura_internal::check_header_slot_and_seal::<Block, P>(
|
||||
slot_now,
|
||||
block_params.header,
|
||||
@@ -218,7 +223,6 @@ pub fn fully_verifying_import_queue<P, Client, Block: BlockT, I, CIDP>(
|
||||
client: Arc<Client>,
|
||||
block_import: I,
|
||||
create_inherent_data_providers: CIDP,
|
||||
slot_duration: SlotDuration,
|
||||
spawner: &impl sp_core::traits::SpawnEssentialNamed,
|
||||
registry: Option<&substrate_prometheus_endpoint::Registry>,
|
||||
telemetry: Option<TelemetryHandle>,
|
||||
@@ -240,7 +244,6 @@ where
|
||||
client,
|
||||
create_inherent_data_providers,
|
||||
defender: NaiveEquivocationDefender::default(),
|
||||
slot_duration,
|
||||
telemetry,
|
||||
_phantom: std::marker::PhantomData,
|
||||
};
|
||||
|
||||
@@ -718,8 +718,6 @@ pub async fn start_generic_aura_node<Net: NetworkBackend<Block, Hash>>(
|
||||
overseer_handle,
|
||||
announce_block,
|
||||
_backend| {
|
||||
let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
|
||||
|
||||
let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(
|
||||
task_manager.spawn_handle(),
|
||||
client.clone(),
|
||||
@@ -746,7 +744,6 @@ pub async fn start_generic_aura_node<Net: NetworkBackend<Block, Hash>>(
|
||||
collator_key,
|
||||
para_id,
|
||||
overseer_handle,
|
||||
slot_duration,
|
||||
relay_chain_slot_duration,
|
||||
proposer,
|
||||
collator_service,
|
||||
@@ -892,15 +889,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// Move to Aura consensus.
|
||||
let slot_duration = match cumulus_client_consensus_aura::slot_duration(&*client) {
|
||||
Ok(d) => d,
|
||||
Err(e) => {
|
||||
log::error!("Could not get Aura slot duration: {e}");
|
||||
return
|
||||
},
|
||||
};
|
||||
|
||||
let proposer = Proposer::new(proposer_factory);
|
||||
|
||||
let params = BasicAuraParams {
|
||||
@@ -913,7 +901,6 @@ where
|
||||
collator_key,
|
||||
para_id,
|
||||
overseer_handle,
|
||||
slot_duration,
|
||||
relay_chain_slot_duration,
|
||||
proposer,
|
||||
collator_service,
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
title: "Remove `slot_duration` from `aura::collators::basic::Params`"
|
||||
|
||||
doc:
|
||||
- audience: Node Dev
|
||||
description: |
|
||||
Removes the `slot_duration` parameter from the `aura::collators::basic::Params`.
|
||||
The `slot_duration` will now be fetched from the runtime using the `Aura` runtime api.
|
||||
|
||||
crates:
|
||||
- name: cumulus-client-consensus-aura
|
||||
@@ -10,7 +10,6 @@ use sc_cli::{
|
||||
NetworkParams, Result, SharedParams, SubstrateCli,
|
||||
};
|
||||
use sc_service::config::{BasePath, PrometheusConfig};
|
||||
use sp_runtime::traits::AccountIdConversion;
|
||||
|
||||
use crate::{
|
||||
chain_spec,
|
||||
@@ -241,17 +240,11 @@ pub fn run() -> Result<()> {
|
||||
|
||||
let id = ParaId::from(para_id);
|
||||
|
||||
let parachain_account =
|
||||
AccountIdConversion::<polkadot_primitives::AccountId>::into_account_truncating(
|
||||
&id,
|
||||
);
|
||||
|
||||
let tokio_handle = config.tokio_handle.clone();
|
||||
let polkadot_config =
|
||||
SubstrateCli::create_configuration(&polkadot_cli, &polkadot_cli, tokio_handle)
|
||||
.map_err(|err| format!("Relay chain argument error: {}", err))?;
|
||||
|
||||
info!("Parachain Account: {parachain_account}");
|
||||
info!("Is collating: {}", if config.role.is_authority() { "yes" } else { "no" });
|
||||
|
||||
crate::service::start_parachain_node(
|
||||
|
||||
@@ -27,7 +27,7 @@ use frame_benchmarking_cli::SUBSTRATE_REFERENCE_HARDWARE;
|
||||
use sc_client_api::Backend;
|
||||
use sc_consensus::ImportQueue;
|
||||
use sc_executor::{HeapAllocStrategy, WasmExecutor, DEFAULT_HEAP_ALLOC_STRATEGY};
|
||||
use sc_network::{NetworkBackend, NetworkBlock};
|
||||
use sc_network::NetworkBlock;
|
||||
use sc_network_sync::SyncingService;
|
||||
use sc_service::{Configuration, PartialComponents, TFullBackend, TFullClient, TaskManager};
|
||||
use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};
|
||||
@@ -113,7 +113,7 @@ pub fn new_partial(config: &Configuration) -> Result<Service, sc_service::Error>
|
||||
config,
|
||||
telemetry.as_ref().map(|telemetry| telemetry.handle()),
|
||||
&task_manager,
|
||||
)?;
|
||||
);
|
||||
|
||||
Ok(PartialComponents {
|
||||
backend,
|
||||
@@ -127,11 +127,100 @@ pub fn new_partial(config: &Configuration) -> Result<Service, sc_service::Error>
|
||||
})
|
||||
}
|
||||
|
||||
/// 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,
|
||||
) -> sc_consensus::DefaultImportQueue<Block> {
|
||||
cumulus_client_consensus_aura::equivocation_import_queue::fully_verifying_import_queue::<
|
||||
sp_consensus_aura::sr25519::AuthorityPair,
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
>(
|
||||
client,
|
||||
block_import,
|
||||
move |_, _| async move {
|
||||
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
|
||||
Ok(timestamp)
|
||||
},
|
||||
&task_manager.spawn_essential_handle(),
|
||||
config.prometheus_registry(),
|
||||
telemetry,
|
||||
)
|
||||
}
|
||||
|
||||
fn start_consensus(
|
||||
client: Arc<ParachainClient>,
|
||||
block_import: ParachainBlockImport,
|
||||
prometheus_registry: Option<&Registry>,
|
||||
telemetry: Option<TelemetryHandle>,
|
||||
task_manager: &TaskManager,
|
||||
relay_chain_interface: Arc<dyn RelayChainInterface>,
|
||||
transaction_pool: Arc<sc_transaction_pool::FullPool<Block, ParachainClient>>,
|
||||
sync_oracle: Arc<SyncingService<Block>>,
|
||||
keystore: KeystorePtr,
|
||||
relay_chain_slot_duration: Duration,
|
||||
para_id: ParaId,
|
||||
collator_key: CollatorPair,
|
||||
overseer_handle: OverseerHandle,
|
||||
announce_block: Arc<dyn Fn(Hash, Option<Vec<u8>>) + Send + Sync>,
|
||||
) -> Result<(), sc_service::Error> {
|
||||
use cumulus_client_consensus_aura::collators::basic::{
|
||||
self as basic_aura, Params as BasicAuraParams,
|
||||
};
|
||||
|
||||
let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(
|
||||
task_manager.spawn_handle(),
|
||||
client.clone(),
|
||||
transaction_pool,
|
||||
prometheus_registry,
|
||||
telemetry.clone(),
|
||||
);
|
||||
|
||||
let proposer = Proposer::new(proposer_factory);
|
||||
|
||||
let collator_service = CollatorService::new(
|
||||
client.clone(),
|
||||
Arc::new(task_manager.spawn_handle()),
|
||||
announce_block,
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
let params = BasicAuraParams {
|
||||
create_inherent_data_providers: move |_, ()| async move { Ok(()) },
|
||||
block_import,
|
||||
para_client: client,
|
||||
relay_client: relay_chain_interface,
|
||||
sync_oracle,
|
||||
keystore,
|
||||
collator_key,
|
||||
para_id,
|
||||
overseer_handle,
|
||||
relay_chain_slot_duration,
|
||||
proposer,
|
||||
collator_service,
|
||||
// Very limited proposal time.
|
||||
authoring_duration: Duration::from_millis(500),
|
||||
collation_request_receiver: None,
|
||||
};
|
||||
|
||||
let fut =
|
||||
basic_aura::run::<Block, sp_consensus_aura::sr25519::AuthorityPair, _, _, _, _, _, _, _>(
|
||||
params,
|
||||
);
|
||||
task_manager.spawn_essential_handle().spawn("aura", None, fut);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.
|
||||
///
|
||||
/// This is the actual implementation that is abstract over the executor and the runtime api.
|
||||
#[sc_tracing::logging::prefix_logs_with("Parachain")]
|
||||
async fn start_node_impl<N: NetworkBackend<Block, Hash>>(
|
||||
pub async fn start_parachain_node(
|
||||
parachain_config: Configuration,
|
||||
polkadot_config: Configuration,
|
||||
collator_options: CollatorOptions,
|
||||
@@ -142,8 +231,11 @@ async fn start_node_impl<N: NetworkBackend<Block, Hash>>(
|
||||
|
||||
let params = new_partial(¶chain_config)?;
|
||||
let (block_import, mut telemetry, telemetry_worker_handle) = params.other;
|
||||
let net_config =
|
||||
sc_network::config::FullNetworkConfiguration::<_, _, N>::new(¶chain_config.network);
|
||||
let net_config = sc_network::config::FullNetworkConfiguration::<
|
||||
_,
|
||||
_,
|
||||
sc_network::NetworkWorker<Block, Hash>,
|
||||
>::new(¶chain_config.network);
|
||||
|
||||
let client = params.client.clone();
|
||||
let backend = params.backend.clone();
|
||||
@@ -165,6 +257,8 @@ async fn start_node_impl<N: NetworkBackend<Block, Hash>>(
|
||||
let transaction_pool = params.transaction_pool.clone();
|
||||
let import_queue_service = params.import_queue.service();
|
||||
|
||||
// NOTE: because we use Aura here explicitly, we can use `CollatorSybilResistance::Resistant`
|
||||
// when starting the network.
|
||||
let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =
|
||||
build_network(BuildNetworkParams {
|
||||
parachain_config: ¶chain_config,
|
||||
@@ -308,133 +402,3 @@ async fn start_node_impl<N: NetworkBackend<Block, Hash>>(
|
||||
|
||||
Ok((task_manager, client))
|
||||
}
|
||||
|
||||
/// 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,
|
||||
) -> Result<sc_consensus::DefaultImportQueue<Block>, sc_service::Error> {
|
||||
let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
|
||||
|
||||
Ok(cumulus_client_consensus_aura::equivocation_import_queue::fully_verifying_import_queue::<
|
||||
sp_consensus_aura::sr25519::AuthorityPair,
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
>(
|
||||
client,
|
||||
block_import,
|
||||
move |_, _| async move {
|
||||
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
|
||||
Ok(timestamp)
|
||||
},
|
||||
slot_duration,
|
||||
&task_manager.spawn_essential_handle(),
|
||||
config.prometheus_registry(),
|
||||
telemetry,
|
||||
))
|
||||
}
|
||||
|
||||
fn start_consensus(
|
||||
client: Arc<ParachainClient>,
|
||||
block_import: ParachainBlockImport,
|
||||
prometheus_registry: Option<&Registry>,
|
||||
telemetry: Option<TelemetryHandle>,
|
||||
task_manager: &TaskManager,
|
||||
relay_chain_interface: Arc<dyn RelayChainInterface>,
|
||||
transaction_pool: Arc<sc_transaction_pool::FullPool<Block, ParachainClient>>,
|
||||
sync_oracle: Arc<SyncingService<Block>>,
|
||||
keystore: KeystorePtr,
|
||||
relay_chain_slot_duration: Duration,
|
||||
para_id: ParaId,
|
||||
collator_key: CollatorPair,
|
||||
overseer_handle: OverseerHandle,
|
||||
announce_block: Arc<dyn Fn(Hash, Option<Vec<u8>>) + Send + Sync>,
|
||||
) -> Result<(), sc_service::Error> {
|
||||
use cumulus_client_consensus_aura::collators::basic::{
|
||||
self as basic_aura, Params as BasicAuraParams,
|
||||
};
|
||||
|
||||
// NOTE: because we use Aura here explicitly, we can use `CollatorSybilResistance::Resistant`
|
||||
// when starting the network.
|
||||
|
||||
let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
|
||||
|
||||
let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(
|
||||
task_manager.spawn_handle(),
|
||||
client.clone(),
|
||||
transaction_pool,
|
||||
prometheus_registry,
|
||||
telemetry.clone(),
|
||||
);
|
||||
|
||||
let proposer = Proposer::new(proposer_factory);
|
||||
|
||||
let collator_service = CollatorService::new(
|
||||
client.clone(),
|
||||
Arc::new(task_manager.spawn_handle()),
|
||||
announce_block,
|
||||
client.clone(),
|
||||
);
|
||||
|
||||
let params = BasicAuraParams {
|
||||
create_inherent_data_providers: move |_, ()| async move { Ok(()) },
|
||||
block_import,
|
||||
para_client: client,
|
||||
relay_client: relay_chain_interface,
|
||||
sync_oracle,
|
||||
keystore,
|
||||
collator_key,
|
||||
para_id,
|
||||
overseer_handle,
|
||||
slot_duration,
|
||||
relay_chain_slot_duration,
|
||||
proposer,
|
||||
collator_service,
|
||||
// Very limited proposal time.
|
||||
authoring_duration: Duration::from_millis(500),
|
||||
collation_request_receiver: None,
|
||||
};
|
||||
|
||||
let fut =
|
||||
basic_aura::run::<Block, sp_consensus_aura::sr25519::AuthorityPair, _, _, _, _, _, _, _>(
|
||||
params,
|
||||
);
|
||||
task_manager.spawn_essential_handle().spawn("aura", None, fut);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Start a parachain node.
|
||||
pub async fn start_parachain_node(
|
||||
parachain_config: Configuration,
|
||||
polkadot_config: Configuration,
|
||||
collator_options: CollatorOptions,
|
||||
para_id: ParaId,
|
||||
hwbench: Option<sc_sysinfo::HwBench>,
|
||||
) -> sc_service::error::Result<(TaskManager, Arc<ParachainClient>)> {
|
||||
match polkadot_config.network.network_backend {
|
||||
sc_network::config::NetworkBackendType::Libp2p =>
|
||||
start_node_impl::<sc_network::NetworkWorker<_, _>>(
|
||||
parachain_config,
|
||||
polkadot_config,
|
||||
collator_options,
|
||||
para_id,
|
||||
hwbench,
|
||||
)
|
||||
.await,
|
||||
sc_network::config::NetworkBackendType::Litep2p =>
|
||||
start_node_impl::<sc_network::Litep2pNetworkBackend>(
|
||||
parachain_config,
|
||||
polkadot_config,
|
||||
collator_options,
|
||||
para_id,
|
||||
hwbench,
|
||||
)
|
||||
.await,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user