Skip to content

Commit

Permalink
fix: client
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Apr 21, 2022
1 parent e11143d commit ea93d7a
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 57 deletions.
2 changes: 1 addition & 1 deletion nodes/parachain/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! KILT chain specification

use cumulus_primitives_core::ParaId;
use polkadot_primitives::v1::LOWEST_PUBLIC_ID;
use polkadot_primitives::v2::LOWEST_PUBLIC_ID;
use runtime_common::{AccountId, AccountPublic};
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
use sc_service::Properties;
Expand Down
5 changes: 3 additions & 2 deletions nodes/parachain/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ pub(crate) enum Subcommand {
/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),

/// The custom benchmark subcommmand benchmarking runtime pallets.
#[clap(name = "benchmark", about = "Benchmark runtime pallets.")]
/// Sub-commands concerned with benchmarking.
/// The pallet benchmarking moved to the `pallet` sub-command.
#[clap(subcommand)]
Benchmark(frame_benchmarking_cli::BenchmarkCmd),

/// Try some command against runtime state.
Expand Down
95 changes: 78 additions & 17 deletions nodes/parachain/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
use crate::{
chain_spec,
cli::{Cli, RelayChainCli, Subcommand},
service::{new_partial, PeregrineRuntimeExecutor, SpiritRuntimeExecutor},
service::{new_partial, PeregrineRuntimeExecutor, SpiritnetRuntimeExecutor},
};
use codec::Encode;
use cumulus_client_service::genesis::generate_genesis_block;
use cumulus_primitives_core::ParaId;
use frame_benchmarking_cli::BenchmarkCmd;
use log::info;
use polkadot_parachain::primitives::AccountIdConversion;
#[cfg(feature = "try-runtime")]
Expand Down Expand Up @@ -173,15 +174,16 @@ fn extract_genesis_wasm(chain_spec: &Box<dyn sc_service::ChainSpec>) -> Result<V
.ok_or_else(|| "Could not find wasm file in genesis state!".into())
}

// TODO: Make use of the macro in Benchmark cmd
macro_rules! construct_async_run {
(|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{
let runner = $cli.create_runner($cmd)?;
match $cli.runtime.as_str() {
"spiritnet" => {
runner.async_run(|$config| {
let $components = new_partial::<spiritnet_runtime::RuntimeApi, SpiritRuntimeExecutor, _>(
let $components = new_partial::<spiritnet_runtime::RuntimeApi, SpiritnetRuntimeExecutor, _>(
&$config,
crate::service::build_import_queue::<SpiritRuntimeExecutor, spiritnet_runtime::RuntimeApi>,
crate::service::build_import_queue::<SpiritnetRuntimeExecutor, spiritnet_runtime::RuntimeApi>,
)?;
let task_manager = $components.task_manager;
{ $( $code )* }.map(|v| (v, task_manager))
Expand Down Expand Up @@ -259,20 +261,76 @@ pub fn run() -> Result<()> {
})
}
Some(Subcommand::Revert(cmd)) => {
construct_async_run!(|components, cli, cmd, config| Ok(cmd.run(components.client, components.backend)))
construct_async_run!(|components, cli, cmd, config| Ok(cmd.run(
components.client,
components.backend,
None
)))
}
Some(Subcommand::Benchmark(cmd)) => {
if cfg!(feature = "runtime-benchmarks") {
let runner = cli.create_runner(cmd)?;
match cli.runtime.as_str() {
"peregrine" => runner.sync_run(|config| cmd.run::<Block, PeregrineRuntimeExecutor>(config)),
"spiritnet" => runner.sync_run(|config| cmd.run::<Block, SpiritRuntimeExecutor>(config)),
_ => Err("Unknown runtime".into()),
}
} else {
Err("Benchmarking wasn't enabled when building the node. \
let runner = cli.create_runner(cmd)?;

// TODO: Improve case handling throughout Benchmark Subcommand
let is_spiritnet = match cli.runtime.as_str() {
"peregrine" => Ok(true),
"spiritnet" => Ok(false),
_ => Err("Unknown runtime"),
}?;

// Switch on the concrete benchmark sub-command
match cmd {
BenchmarkCmd::Pallet(cmd) => {
if cfg!(feature = "runtime-benchmarks") {
if is_spiritnet {
runner.sync_run(|config| cmd.run::<Block, PeregrineRuntimeExecutor>(config))
} else {
runner.sync_run(|config| cmd.run::<Block, SpiritnetRuntimeExecutor>(config))
}
} else {
Err("Benchmarking wasn't enabled when building the node. \
You can enable it with `--features runtime-benchmarks`."
.into())
.into())
}
}
BenchmarkCmd::Block(cmd) => runner.sync_run(|config| {
if is_spiritnet {
let partials = new_partial::<spiritnet_runtime::RuntimeApi, SpiritnetRuntimeExecutor, _>(
&config,
crate::service::build_import_queue,
)?;
cmd.run(partials.client)
} else {
let partials = new_partial::<peregrine_runtime::RuntimeApi, PeregrineRuntimeExecutor, _>(
&config,
crate::service::build_import_queue,
)?;
cmd.run(partials.client)
}
}),
BenchmarkCmd::Storage(cmd) => runner.sync_run(|config| {
if is_spiritnet {
let partials = new_partial::<spiritnet_runtime::RuntimeApi, SpiritnetRuntimeExecutor, _>(
&config,
crate::service::build_import_queue,
)?;

let db = partials.backend.expose_db();
let storage = partials.backend.expose_storage();

cmd.run(config, partials.client.clone(), db, storage)
} else {
let partials = new_partial::<peregrine_runtime::RuntimeApi, PeregrineRuntimeExecutor, _>(
&config,
crate::service::build_import_queue,
)?;

let db = partials.backend.expose_db();
let storage = partials.backend.expose_storage();

cmd.run(config, partials.client.clone(), db, storage)
}
}),
BenchmarkCmd::Overhead(_) => Err("Unsupported benchmarking command".into()),
}
}
Some(Subcommand::ExportGenesisState(params)) => {
Expand Down Expand Up @@ -328,7 +386,7 @@ pub fn run() -> Result<()> {
if runner.config().chain_spec.is_peregrine() {
runner.async_run(|config| Ok((cmd.run::<Block, PeregrineRuntimeExecutor>(config), task_manager)))
} else if runner.config().chain_spec.is_spiritnet() {
runner.async_run(|config| Ok((cmd.run::<Block, SpiritRuntimeExecutor>(config), task_manager)))
runner.async_run(|config| Ok((cmd.run::<Block, SpiritnetRuntimeExecutor>(config), task_manager)))
} else {
Err("Chain doesn't support try-runtime".into())
}
Expand All @@ -339,6 +397,7 @@ pub fn run() -> Result<()> {
.into()),
None => {
let runner = cli.create_runner(&cli.run.normalize())?;
let collator_options = cli.run.collator_options();

runner.run_node_until_exit(|config| async move {
let para_id = chain_spec::Extensions::try_get(&*config.chain_spec)
Expand All @@ -354,7 +413,7 @@ pub fn run() -> Result<()> {

let id = ParaId::from(para_id);

let parachain_account = AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&id);
let parachain_account = AccountIdConversion::<polkadot_primitives::v2::AccountId>::into_account(&id);

let state_version = RelayChainCli::native_runtime_version(&config.chain_spec).state_version();
let block: Block =
Expand All @@ -377,15 +436,17 @@ pub fn run() -> Result<()> {
crate::service::start_node::<PeregrineRuntimeExecutor, peregrine_runtime::RuntimeApi>(
config,
polkadot_config,
collator_options,
id,
)
.await
.map(|r| r.0)
.map_err(Into::into)
} else if config.chain_spec.is_spiritnet() {
crate::service::start_node::<SpiritRuntimeExecutor, spiritnet_runtime::RuntimeApi>(
crate::service::start_node::<SpiritnetRuntimeExecutor, spiritnet_runtime::RuntimeApi>(
config,
polkadot_config,
collator_options,
id,
)
.await
Expand Down
62 changes: 44 additions & 18 deletions nodes/parachain/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@

// If you feel like getting in touch with us, you can do so at info@botlabs.org

use cumulus_client_cli::CollatorOptions;
use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};
use cumulus_client_consensus_common::ParachainConsensus;
use cumulus_client_network::BlockAnnounceValidator;
use cumulus_client_service::{
prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,
};
use cumulus_primitives_core::ParaId;
use cumulus_relay_chain_interface::RelayChainInterface;
use cumulus_relay_chain_local::build_relay_chain_interface;
use polkadot_service::NativeExecutionDispatch;
use cumulus_relay_chain_inprocess_interface::build_inprocess_relay_chain;
use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface, RelayChainResult};
use cumulus_relay_chain_rpc_interface::RelayChainRPCInterface;
use polkadot_service::{CollatorPair, NativeExecutionDispatch};
use sc_client_api::ExecutorProvider;
use sc_executor::NativeElseWasmExecutor;
use sc_network::NetworkService;
use sc_service::{Configuration, Role, RpcExtensionBuilder, TFullBackend, TFullClient, TaskManager};
use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};
use sp_api::ConstructRuntimeApi;
use sp_consensus::SlotData;
use sp_keystore::SyncCryptoStorePtr;
use sp_runtime::traits::BlakeTwo256;
use std::{sync::Arc, time::Duration};
Expand All @@ -55,9 +56,9 @@ type PartialComponents<Block, RuntimeApi, Executor, Telemetry, TelemetryWorkerHa
>;

/// Native Spiritnet executor instance.
pub struct SpiritRuntimeExecutor;
pub struct SpiritnetRuntimeExecutor;

impl sc_executor::NativeExecutionDispatch for SpiritRuntimeExecutor {
impl sc_executor::NativeExecutionDispatch for SpiritnetRuntimeExecutor {
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;

fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
Expand Down Expand Up @@ -177,6 +178,22 @@ where
Ok(params)
}

async fn build_relay_chain_interface(
polkadot_config: Configuration,
parachain_config: &Configuration,
telemetry_worker_handle: Option<TelemetryWorkerHandle>,
task_manager: &mut TaskManager,
collator_options: CollatorOptions,
) -> RelayChainResult<(Arc<(dyn RelayChainInterface + 'static)>, Option<CollatorPair>)> {
match collator_options.relay_chain_rpc_url {
Some(relay_chain_url) => Ok((
Arc::new(RelayChainRPCInterface::new(relay_chain_url).await?) as Arc<_>,
None,
)),
None => build_inprocess_relay_chain(polkadot_config, parachain_config, telemetry_worker_handle, task_manager),
}
}

/// Start a node with the given parachain `Configuration` and relay chain
/// `Configuration`.
///
Expand All @@ -186,6 +203,7 @@ where
async fn start_node_impl<RuntimeApi, Executor, RB, BIQ, BIC>(
parachain_config: Configuration,
polkadot_config: Configuration,
collator_options: CollatorOptions,
id: ParaId,
rpc_ext_builder: RB,
build_import_queue: BIQ,
Expand Down Expand Up @@ -248,13 +266,18 @@ where
let backend = params.backend.clone();
let mut task_manager = params.task_manager;

let (relay_chain_interface, collator_key) =
build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager).map_err(
|e| match e {
polkadot_service::Error::Sub(x) => x,
s => format!("{}", s).into(),
},
)?;
let (relay_chain_interface, collator_key) = build_relay_chain_interface(
polkadot_config,
&parachain_config,
telemetry_worker_handle,
&mut task_manager,
collator_options.clone(),
)
.await
.map_err(|e| match e {
RelayChainError::ServiceError(polkadot_service::Error::Sub(x)) => x,
s => s.to_string().into(),
})?;

let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);

Expand Down Expand Up @@ -318,7 +341,7 @@ where
task_manager: &mut task_manager,
parachain_consensus,
import_queue,
collator_key,
collator_key: collator_key.expect("Command line arguments do not allow this. qed"),
relay_chain_slot_duration,
};

Expand All @@ -332,6 +355,7 @@ where
relay_chain_interface,
relay_chain_slot_duration,
import_queue,
collator_options,
};

start_full_node(params)?;
Expand Down Expand Up @@ -377,9 +401,9 @@ where
create_inherent_data_providers: move |_, _| async move {
let time = sp_timestamp::InherentDataProvider::from_system_time();

let slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(
let slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
*time,
slot_duration.slot_duration(),
slot_duration,
);

Ok((time, slot))
Expand All @@ -397,6 +421,7 @@ where
pub async fn start_node<RE, API>(
parachain_config: Configuration,
polkadot_config: Configuration,
collator_options: CollatorOptions,
id: ParaId,
) -> sc_service::error::Result<(TaskManager, Arc<TFullClient<Block, API, NativeElseWasmExecutor<RE>>>)>
where
Expand Down Expand Up @@ -432,6 +457,7 @@ where
start_node_impl::<API, RE, _, _, _>(
parachain_config,
polkadot_config,
collator_options,
id,
rpc_extensions_builder,
build_import_queue::<RE, API>,
Expand Down Expand Up @@ -477,9 +503,9 @@ where
.await;
let time = sp_timestamp::InherentDataProvider::from_system_time();

let slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(
let slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
*time,
slot_duration.slot_duration(),
slot_duration,
);

let parachain_inherent = parachain_inherent.ok_or_else(|| {
Expand Down
4 changes: 2 additions & 2 deletions nodes/standalone/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub enum Subcommand {
/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),

/// The custom benchmark subcommmand benchmarking runtime pallets.
#[clap(name = "benchmark", about = "Benchmark runtime pallets.")]
/// Sub-commands concerned with benchmarking.
#[clap(subcommand)]
Benchmark(frame_benchmarking_cli::BenchmarkCmd),

/// Try some command against runtime state.
Expand Down
Loading

0 comments on commit ea93d7a

Please sign in to comment.