Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aurexav committed Dec 27, 2024
1 parent 64fa8f4 commit 447b568
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 92 deletions.
9 changes: 4 additions & 5 deletions node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,14 @@ impl RelayChainCli {
para_config: &sc_service::Configuration,
relay_chain_args: impl Iterator<Item = &'a String>,
) -> Self {
let polkadot_cmd = Self::polkadot_cmd();
let matches = polkadot_cmd.get_matches_from(relay_chain_args);
let base = clap::FromArgMatches::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
let chain_id = extension.map(|e| e.relay_chain.clone());
let base_path = para_config.base_path.path().join("polkadot");

Self {
base_path: Some(base_path),
chain_id,
base: clap::Parser::parse_from(relay_chain_args),
}
Self { base, chain_id, base_path: Some(base_path) }
}
}

Expand Down
182 changes: 104 additions & 78 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ use std::{env, fs, io::ErrorKind, net::SocketAddr, path::PathBuf, result::Result
// darwinia
use crate::{
chain_spec::*,
cli::{Cli, FrontierBackendType, RelayChainCli, Subcommand},
cli::{Cli, EthRpcConfig, FrontierBackendType, RelayChainCli, Subcommand},
service::{self, *},
};
use dc_primitives::{Block, Hash};
// polkadot-sdk
use cumulus_client_cli::CollatorOptions;
use cumulus_primitives_core::ParaId;
use sc_cli::{
CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams, NetworkParams,
Result, SharedParams, SubstrateCli,
};
use sc_network::{
config::NetworkBackendType, Litep2pNetworkBackend, NetworkBackend, NetworkWorker,
};
use sc_service::{
config::{BasePath, PrometheusConfig},
ChainSpec as ChainSpecT, DatabaseSource,
ChainSpec as ChainSpecT, Configuration, DatabaseSource, TaskManager,
};
use sc_storage_monitor::StorageMonitorParams;
use sp_core::crypto::{self, Ss58AddressFormatRegistry};
use sp_runtime::traits::AccountIdConversion;

Expand Down Expand Up @@ -469,11 +475,12 @@ pub fn run() -> Result<()> {
let polkadot_cli = RelayChainCli::new(runner.config(), cli.relay_chain_args.iter());

runner.run_node_until_exit(|config| async move {
let chain_spec = &config.chain_spec;

set_default_ss58_version(chain_spec);
set_default_ss58_version(&config.chain_spec);

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}"))?;
let para_id = Extensions::try_get(&*config.chain_spec)
.map(|e| e.para_id)
.ok_or("Could not find parachain ID in chain-spec.")?;
Expand All @@ -488,96 +495,115 @@ pub fn run() -> Result<()> {
log::info!("🧾 Parachain Account: {}", parachain_account);
log::info!("✍️ Is collating: {}", if config.role.is_authority() { "yes" } else { "no" });

if chain_spec.is_dev() {
#[cfg(feature = "crab-runtime")]
if chain_spec.is_crab() {
return service::start_dev_node::<CrabRuntimeApi>(
config,
id,
&eth_rpc_config,
)
.map_err(Into::into);
}

#[cfg(feature = "darwinia-runtime")]
if chain_spec.is_darwinia() {
return service::start_dev_node::<DarwiniaRuntimeApi>(
config,
id,
&eth_rpc_config,
)
.map_err(Into::into)
}

#[cfg(feature = "koi-runtime")]
if chain_spec.is_koi() {
return service::start_dev_node::<KoiRuntimeApi>(
config,
id,
&eth_rpc_config,
)
.map_err(Into::into)
}
}

let polkadot_config =
SubstrateCli::create_configuration(&polkadot_cli, &polkadot_cli, tokio_handle)
.map_err(|err| format!("Relay chain argument error: {}", err))?;

#[cfg(feature = "crab-runtime")]
if chain_spec.is_crab() {
return service::start_parachain_node::<CrabRuntimeApi>(
config,
polkadot_config,
match polkadot_config.network.network_backend {
NetworkBackendType::Libp2p => start_node::<NetworkWorker<_, _>>(
collator_options,
id,
no_hardware_benchmarks,
storage_monitor,
&eth_rpc_config,
)
.await
.map(|r| r.0)
.map_err(Into::into);
}

#[cfg(feature = "darwinia-runtime")]
if chain_spec.is_darwinia() {
return service::start_parachain_node::<DarwiniaRuntimeApi>(
config,
polkadot_config,
collator_options,
id,
no_hardware_benchmarks,
storage_monitor,
&eth_rpc_config,
)
.await
.map(|r| r.0)
.map_err(Into::into);
}

#[cfg(feature = "koi-runtime")]
if chain_spec.is_koi() {
return service::start_parachain_node::<KoiRuntimeApi>(
eth_rpc_config
).await,
NetworkBackendType::Litep2p => start_node::<Litep2pNetworkBackend>(
collator_options,
config,
polkadot_config,
collator_options,
id,
no_hardware_benchmarks,
storage_monitor,
&eth_rpc_config,
)
.await
.map(|r| r.0)
.map_err(Into::into);
eth_rpc_config,
).await
}

panic!("No feature(crab-runtime, darwinia-runtime, koi-runtime) is enabled!");
})
},
}
}

async fn start_node<Net>(
collator_options: CollatorOptions,
config: Configuration,
polkadot_config: Configuration,
id: ParaId,
no_hardware_benchmarks: bool,
storage_monitor: StorageMonitorParams,
eth_rpc_config: EthRpcConfig,
) -> Result<TaskManager>
where
Net: NetworkBackend<Block, Hash>,
{
let chain_spec = &config.chain_spec;

if chain_spec.is_dev() {
#[cfg(feature = "crab-runtime")]
if chain_spec.is_crab() {
return service::start_dev_node::<Net, CrabRuntimeApi>(config, id, &eth_rpc_config)
.map_err(Into::into);
}

#[cfg(feature = "darwinia-runtime")]
if chain_spec.is_darwinia() {
return service::start_dev_node::<Net, DarwiniaRuntimeApi>(config, id, &eth_rpc_config)
.map_err(Into::into);
}

#[cfg(feature = "koi-runtime")]
if chain_spec.is_koi() {
return service::start_dev_node::<Net, KoiRuntimeApi>(config, id, &eth_rpc_config)
.map_err(Into::into);
}
}
#[cfg(feature = "crab-runtime")]
if chain_spec.is_crab() {
return service::start_parachain_node::<Net, CrabRuntimeApi>(
config,
polkadot_config,
collator_options,
id,
no_hardware_benchmarks,
storage_monitor,
&eth_rpc_config,
)
.await
.map(|r| r.0)
.map_err(Into::into);
}

#[cfg(feature = "darwinia-runtime")]
if chain_spec.is_darwinia() {
return service::start_parachain_node::<Net, DarwiniaRuntimeApi>(
config,
polkadot_config,
collator_options,
id,
no_hardware_benchmarks,
storage_monitor,
&eth_rpc_config,
)
.await
.map(|r| r.0)
.map_err(Into::into);
}

#[cfg(feature = "koi-runtime")]
if chain_spec.is_koi() {
return service::start_parachain_node::<Net, KoiRuntimeApi>(
config,
polkadot_config,
collator_options,
id,
no_hardware_benchmarks,
storage_monitor,
&eth_rpc_config,
)
.await
.map(|r| r.0)
.map_err(Into::into);
}

panic!("No feature(crab-runtime, darwinia-runtime, koi-runtime) is enabled!");
}

fn load_spec(id: &str) -> StdResult<Box<dyn ChainSpecT>, String> {
let id = if id.is_empty() {
let n = get_exec_name().unwrap_or_default();
Expand Down
2 changes: 1 addition & 1 deletion node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct FullDeps<C, P, A: sc_transaction_pool::ChainApi, CIDP> {
/// The Node authority flag
pub is_authority: bool,
/// Network service
pub network: Arc<sc_network::NetworkService<Block, Hash>>,
pub network: Arc<dyn sc_network::service::traits::NetworkService>,
/// Chain syncing service
pub sync: Arc<sc_network_sync::SyncingService<Block>>,
/// EthFilterApi pool.
Expand Down
1 change: 1 addition & 0 deletions node/src/service/frontier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ pub(crate) fn db_config_dir(config: &Configuration) -> PathBuf {
}

/// Create a Frontier backend.
#[allow(clippy::type_complexity)]
pub(crate) fn backend<B, Be, C>(
client: Arc<C>,
config: &sc_service::Configuration,
Expand Down
23 changes: 15 additions & 8 deletions node/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ where
/// This is the actual implementation that is abstract over the executor and the runtime api.
#[allow(clippy::too_many_arguments)]
#[sc_tracing::logging::prefix_logs_with("Parachain")]
async fn start_node_impl<RuntimeApi, SC>(
async fn start_node_impl<Net, RuntimeApi, SC>(
parachain_config: sc_service::Configuration,
polkadot_config: sc_service::Configuration,
collator_options: cumulus_client_cli::CollatorOptions,
Expand All @@ -258,6 +258,7 @@ async fn start_node_impl<RuntimeApi, SC>(
eth_rpc_config: &crate::cli::EthRpcConfig,
) -> sc_service::error::Result<(sc_service::TaskManager, Arc<FullClient<RuntimeApi>>)>
where
Net: sc_network::NetworkBackend<Block, Hash>,
RuntimeApi: 'static + Send + Sync + sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi>>,
RuntimeApi::RuntimeApi: RuntimeApiCollection,
SC: FnOnce(
Expand Down Expand Up @@ -321,7 +322,8 @@ where
let validator = parachain_config.role.is_authority();
let prometheus_registry = parachain_config.prometheus_registry().cloned();
let import_queue_service = import_queue.service();
let net_config = sc_network::config::FullNetworkConfiguration::new(&parachain_config.network);
let net_config =
<sc_network::config::FullNetworkConfiguration<_, _, Net>>::new(&parachain_config.network);
let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =
cumulus_client_service::build_network(cumulus_client_service::BuildNetworkParams {
parachain_config: &parachain_config,
Expand Down Expand Up @@ -577,7 +579,7 @@ where
}

/// Build the import queue for the parachain runtime.
pub fn build_import_queue<RuntimeApi>(
fn build_import_queue<RuntimeApi>(
client: Arc<FullClient<RuntimeApi>>,
block_import: ParachainBlockImport<RuntimeApi>,
config: &sc_service::Configuration,
Expand Down Expand Up @@ -609,7 +611,7 @@ where
}

/// Start a parachain node.
pub async fn start_parachain_node<RuntimeApi>(
pub async fn start_parachain_node<Net, RuntimeApi>(
parachain_config: sc_service::Configuration,
polkadot_config: sc_service::Configuration,
collator_options: cumulus_client_cli::CollatorOptions,
Expand All @@ -619,12 +621,13 @@ pub async fn start_parachain_node<RuntimeApi>(
eth_rpc_config: &crate::cli::EthRpcConfig,
) -> sc_service::error::Result<(sc_service::TaskManager, Arc<FullClient<RuntimeApi>>)>
where
Net: sc_network::NetworkBackend<Block, Hash>,
RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi>> + Send + Sync + 'static,
RuntimeApi::RuntimeApi: RuntimeApiCollection,
RuntimeApi::RuntimeApi:
sp_consensus_aura::AuraApi<Block, sp_consensus_aura::sr25519::AuthorityId>,
{
start_node_impl::<RuntimeApi, _>(
start_node_impl::<Net, RuntimeApi, _>(
parachain_config,
polkadot_config,
collator_options,
Expand Down Expand Up @@ -706,12 +709,13 @@ where

/// Start a dev node which can seal instantly.
/// !!! WARNING: DO NOT USE ELSEWHERE
pub fn start_dev_node<RuntimeApi>(
pub fn start_dev_node<Net, RuntimeApi>(
mut config: sc_service::Configuration,
para_id: cumulus_primitives_core::ParaId,
eth_rpc_config: &crate::cli::EthRpcConfig,
) -> Result<sc_service::TaskManager, sc_service::error::Error>
where
Net: sc_network::NetworkBackend<Block, Hash>,
RuntimeApi: 'static + Send + Sync + sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi>>,
RuntimeApi::RuntimeApi: RuntimeApiCollection,
RuntimeApi::RuntimeApi:
Expand All @@ -737,7 +741,9 @@ where
_telemetry_worker_handle,
),
} = new_partial::<RuntimeApi>(&config, eth_rpc_config)?;
let net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);
let net_config =
<sc_network::config::FullNetworkConfiguration<_, _, Net>>::new(&config.network);
let metrics = Net::register_notification_metrics(None);
let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =
sc_service::build_network(sc_service::BuildNetworkParams {
config: &config,
Expand All @@ -749,6 +755,7 @@ where
block_announce_validator_builder: None,
warp_sync_params: None,
block_relay: None,
metrics,
})?;

if config.offchain_worker.enabled {
Expand All @@ -764,7 +771,7 @@ where
transaction_pool.clone(),
),
),
network_provider: network.clone(),
network_provider: Arc::new(network.clone()),
is_validator: config.role.is_authority(),
enable_http_requests: false,
custom_extensions: move |_| Vec::new(),
Expand Down

0 comments on commit 447b568

Please sign in to comment.