Skip to content

Commit

Permalink
Add build features
Browse files Browse the repository at this point in the history
  • Loading branch information
darkforest0202 authored and olanod committed Jul 11, 2023
1 parent 9928a77 commit 06f1885
Show file tree
Hide file tree
Showing 8 changed files with 567 additions and 267 deletions.
14 changes: 7 additions & 7 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"

# Local
virto-runtime = { path = "../runtime/virto"}
kreivo-runtime = { path = "../runtime/kreivo"}
virto-runtime = { path = "../runtime/virto", optional = true}
kreivo-runtime = { path = "../runtime/kreivo", optional = true}
jsonrpsee = { version = "0.16.2", features = ["server"] }

# Substrate
Expand Down Expand Up @@ -97,13 +97,13 @@ wait-timeout = "0.2"
polkadot-cli = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.43" }

[features]
default = []
default = ["virto-runtime", "kreivo-runtime"]
runtime-benchmarks = [
"virto-runtime/runtime-benchmarks",
"kreivo-runtime/runtime-benchmarks",
"virto-runtime?/runtime-benchmarks",
"kreivo-runtime?/runtime-benchmarks",
"polkadot-cli/runtime-benchmarks"
]
try-runtime = [
"virto-runtime/try-runtime",
"kreivo-runtime/try-runtime"
"virto-runtime?/try-runtime",
"kreivo-runtime?/try-runtime"
]
2 changes: 2 additions & 0 deletions node/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl<RuntimeApi> RemarkBuilder<RuntimeApi> {
}
}

#[cfg(feature = "kreivo-runtime")]
impl frame_benchmarking_cli::ExtrinsicBuilder for RemarkBuilder<kreivo_runtime::RuntimeApi> {
fn pallet(&self) -> &str {
"system"
Expand Down Expand Up @@ -84,6 +85,7 @@ impl frame_benchmarking_cli::ExtrinsicBuilder for RemarkBuilder<kreivo_runtime::
}
}

#[cfg(feature = "virto-runtime")]
impl frame_benchmarking_cli::ExtrinsicBuilder for RemarkBuilder<virto_runtime::RuntimeApi> {
fn pallet(&self) -> &str {
"system"
Expand Down
3 changes: 3 additions & 0 deletions node/src/chain_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use serde::{Deserialize, Serialize};
use sp_core::{Pair, Public};
use sp_runtime::traits::{IdentifyAccount, Verify};

#[cfg(feature = "kreivo-runtime")]
pub mod kreivo;

#[cfg(feature = "virto-runtime")]
pub mod virto;

/// The default XCM version to set in genesis config.
Expand Down
105 changes: 58 additions & 47 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

use crate::service::{new_partial, Block};
use crate::{
benchmarking::{inherent_benchmark_data, RemarkBuilder},
chain_spec,
cli::{Cli, RelayChainCli, Subcommand},
service::{new_partial, Block, RuntimeExecutor},
};

use codec::Encode;
Expand All @@ -41,12 +41,14 @@ use std::{net::SocketAddr, path::PathBuf};
macro_rules! dispatch_runtime {
($runtime:expr, |$alias: ident| $code:expr) => {
match $runtime {
#[cfg(feature = "kreivo-runtime")]
Runtime::Kreivo => {
#[allow(unused_imports)]
use kreivo_runtime as $alias;

$code
}
#[cfg(feature = "virto-runtime")]
Runtime::Virto => {
#[allow(unused_imports)]
use virto_runtime as $alias;
Expand All @@ -55,54 +57,82 @@ macro_rules! dispatch_runtime {
}
}
};
($runtime:expr, $code:expr) => {
dispatch_runtime!($runtime, |rt| $code)
};
}

/// Generates boilerplate code for constructing partial node for the runtimes
/// that are supported by the benchmarks.
macro_rules! construct_partial {
($config:expr, |$partial:ident| $code:expr) => {
dispatch_runtime!($config.chain_spec.runtime(), |rt| {
($config:expr, |$partial:ident, $runtime:ident| $code:expr) => {
dispatch_runtime!($config.chain_spec.runtime(), |$runtime| {
let $partial =
new_partial::<rt::RuntimeApi, _>(&$config, crate::service::aura_build_import_queue::<_, AuraId>)?;
new_partial::<$runtime::RuntimeApi, _>(&$config, crate::service::aura_build_import_queue::<_, AuraId>)?;

$code
})
};
($config:expr, |$partial:ident| $code:expr) => {
construct_partial!($config, |$partial, rt| $code)
};
}

/// Generates boilerplate code for async run on partial node.
macro_rules! construct_async_run {
(|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{
(|$components:ident, $cli:ident, $cmd:ident, $config:ident, $runtime:ident| $( $code:tt )* ) => {{
let runner = $cli.create_runner($cmd)?;
construct_partial!(runner.config(), |$components| {
construct_partial!(runner.config(), |$components, $runtime| {
runner.async_run(|$config| {
let task_manager = $components.task_manager;
{ $( $code )* }.map(|v| (v, task_manager))
})
})
}};
(|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{
construct_async_run!(|$components, $cli, $cmd, $config, rt| { $( $code )* })
}};
}

#[cfg(feature = "kreivo-runtime")]
impl Default for Runtime {
fn default() -> Self {
Runtime::Kreivo
}
}

#[cfg(all(feature = "virto-runtime", not(feature = "kreivo-runtime")))]
impl Default for Runtime {
fn default() -> Self {
Runtime::Virto
}
}

/// Helper enum that is used for better distinction of different
/// parachain/runtime configuration (it is based/calculated on ChainSpec's ID
/// attribute)
#[derive(Debug, PartialEq, Default)]
#[derive(Debug, PartialEq)]
enum Runtime {
#[default]
#[cfg(feature = "kreivo-runtime")]
Kreivo,
#[cfg(feature = "virto-runtime")]
Virto,
}

impl From<&str> for Runtime {
fn from(value: &str) -> Self {
#[cfg(feature = "kreivo-runtime")]
if value.starts_with("kreivo") {
Runtime::Kreivo
} else if value.starts_with("virto") {
Runtime::Virto
} else {
log::warn!("No specific runtime was recognized for ChainSpec's id: '{value}', so `Kreivo` will be used as default.");
Runtime::default()
return Runtime::Kreivo;
}
#[cfg(feature = "virto-runtime")]
if value.starts_with("virto") {
return Runtime::Virto;
}

let fallback = Runtime::default();
log::warn!("No specific runtime was recognized for ChainSpec's id: '{value}', so `{fallback:?}` will be used as default.");
fallback
}
}

Expand Down Expand Up @@ -136,22 +166,23 @@ impl RuntimeResolver for PathBuf {

fn load_spec(id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
Ok(match id {
// - Defaul-like
"kreivo" => Box::new(chain_spec::kreivo::kreivo_kusama_chain_spec()),
#[cfg(feature = "kreivo-runtime")]
"" | "kreivo" => Box::new(chain_spec::kreivo::kreivo_kusama_chain_spec()),
#[cfg(feature = "kreivo-runtime")]
"kreivo-local" => Box::new(chain_spec::kreivo::kreivo_kusama_chain_spec_local()),
#[cfg(feature = "kreivo-runtime")]
"kreivo-rococo-local" => Box::new(chain_spec::kreivo::kreivo_rococo_chain_spec_local()),
#[cfg(feature = "virto-runtime")]
"virto" => Box::new(chain_spec::virto::virto_polkadot_chain_spec()),
#[cfg(feature = "virto-runtime")]
"virto-local" => Box::new(chain_spec::virto::virto_polkadot_chain_spec_local()),
// -- Fallback (generic chainspec)
"" => {
log::warn!("No ChainSpec.id specified, so using default one, based on rococo-parachain runtime");
Box::new(chain_spec::kreivo::kreivo_kusama_chain_spec_local())
}
// -- Loading a specific spec from disk
path => {
let path: PathBuf = path.into();
match path.runtime() {
#[cfg(feature = "kreivo-runtime")]
Runtime::Kreivo => Box::new(chain_spec::kreivo::ChainSpec::from_json_file(path)?),
#[cfg(feature = "virto-runtime")]
Runtime::Virto => Box::new(chain_spec::virto::ChainSpec::from_json_file(path)?),
}
}
Expand Down Expand Up @@ -313,10 +344,9 @@ pub fn run() -> Result<()> {
.into());
}

match config.chain_spec.runtime() {
Runtime::Kreivo => cmd.run::<Block, crate::service::KreivoRuntimeExecutor>(config),
Runtime::Virto => cmd.run::<Block, crate::service::VirtoRuntimeExecutor>(config),
}
dispatch_runtime!(config.chain_spec.runtime(), |runtime| {
cmd.run::<Block, RuntimeExecutor<runtime::Runtime>>(config)
})
}
BenchmarkCmd::Block(cmd) => {
construct_partial!(config, |partial| cmd.run(partial.client))
Expand Down Expand Up @@ -361,34 +391,15 @@ pub fn run() -> Result<()> {
#[cfg(feature = "try-runtime")]
Some(Subcommand::TryRuntime(cmd)) => {
use sc_executor::{sp_wasm_interface::ExtendedHostFunctions, NativeExecutionDispatch};
use try_runtime_cli::block_building_info::timestamp_with_aura_info;

// grab the task manager.
let runner = cli.create_runner(cmd)?;
let registry = &runner.config().prometheus_config.as_ref().map(|cfg| &cfg.registry);
let task_manager = sc_service::TaskManager::new(runner.config().tokio_handle.clone(), *registry)
.map_err(|e| format!("Error: {:?}", e))?;
type HostFunctionsOf<E> = ExtendedHostFunctions<
sp_io::SubstrateHostFunctions,
<E as NativeExecutionDispatch>::ExtendHostFunctions,
>;

let info_provider = timestamp_with_aura_info(6000);

match runner.config().chain_spec.runtime() {
Runtime::Kreivo => runner.async_run(|_| {
Ok((
cmd.run::<Block, HostFunctionsOf<KreivoRuntimeExecutor>, _>(Some(info_provider)),
task_manager,
))
}),
Runtime::Virto => runner.async_run(|_| {
Ok((
cmd.run::<Block, HostFunctionsOf<VirtoRuntimeExecutor>, _>(Some(info_provider)),
task_manager,
))
}),
}
construct_async_run!(|components, cli, cmd, _config, runtime| {
Ok(cmd.run::<Block, HostFunctionsOf<RuntimeExecutor<runtime::Runtime>>>())
})
}
#[cfg(not(feature = "try-runtime"))]
Some(Subcommand::TryRuntime) => Err("Try-runtime was not enabled when building the node. \
Expand Down
25 changes: 8 additions & 17 deletions node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use cumulus_primitives_core::{
ParaId,
};
use cumulus_relay_chain_interface::RelayChainInterface;

use sp_core::Pair;

use jsonrpsee::RpcModule;
Expand Down Expand Up @@ -72,15 +73,12 @@ type ParachainBackend = TFullBackend<Block>;

type ParachainBlockImport<RuntimeApi> =
TParachainBlockImport<Block, Arc<ParachainClient<RuntimeApi>>, ParachainBackend>;
pub struct KreivoRuntimeExecutor;
impl sc_executor::NativeExecutionDispatch for KreivoRuntimeExecutor {
/// Only enable the benchmarking host functions when we actually want to
/// benchmark.
#[cfg(feature = "runtime-benchmarks")]

pub struct RuntimeExecutor<Runtime>(PhantomData<Runtime>);

#[cfg(feature = "kreivo-runtime")]
impl sc_executor::NativeExecutionDispatch for RuntimeExecutor<kreivo_runtime::Runtime> {
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;
/// Otherwise we only use the default Substrate host functions.
#[cfg(not(feature = "runtime-benchmarks"))]
type ExtendHostFunctions = ();

fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
kreivo_runtime::api::dispatch(method, data)
Expand All @@ -91,16 +89,9 @@ impl sc_executor::NativeExecutionDispatch for KreivoRuntimeExecutor {
}
}

/// Native executor instance.
pub struct VirtoRuntimeExecutor;
impl sc_executor::NativeExecutionDispatch for VirtoRuntimeExecutor {
/// Only enable the benchmarking host functions when we actually want to
/// benchmark.
#[cfg(feature = "runtime-benchmarks")]
#[cfg(feature = "virto-runtime")]
impl sc_executor::NativeExecutionDispatch for RuntimeExecutor<virto_runtime::Runtime> {
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;
/// Otherwise we only use the default Substrate host functions.
#[cfg(not(feature = "runtime-benchmarks"))]
type ExtendHostFunctions = ();

fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
virto_runtime::api::dispatch(method, data)
Expand Down
Loading

0 comments on commit 06f1885

Please sign in to comment.