Skip to content

Commit

Permalink
feat: add instant-seal for dev chain
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
  • Loading branch information
gregdhill committed Apr 25, 2022
1 parent f9abdeb commit 94f2df3
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 48 deletions.
41 changes: 40 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions parachain/Cargo.toml
Expand Up @@ -20,6 +20,7 @@ log = "0.4.8"
codec = { package = "parity-scale-codec", version = "3.0.0" }
serde = { version = "1.0.130", features = ["derive"] }
hex-literal = "0.2.1"
futures = "0.3.15"

# Parachain dependencies
interlay-runtime = { package = "interlay-runtime-parachain", path = "./runtime/interlay" }
Expand All @@ -42,6 +43,7 @@ module-refund-rpc-runtime-api = { path = "../crates/refund/rpc/runtime-api" }
sc-cli = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18", features = ["wasmtime"] }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
sc-consensus-manual-seal = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
sc-executor = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18", features = ["wasmtime"] }
sc-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
Expand Down
6 changes: 6 additions & 0 deletions parachain/src/cli.rs
Expand Up @@ -115,6 +115,12 @@ pub struct Cli {
/// Relaychain arguments
#[clap(raw = true)]
pub relaychain_args: Vec<String>,

/// Instant block sealing
///
/// Can only be used with `--dev`
#[clap(long = "instant-seal", requires = "dev")]
pub instant_seal: bool,
}

#[derive(Debug)]
Expand Down
96 changes: 55 additions & 41 deletions parachain/src/command.rs
Expand Up @@ -100,6 +100,27 @@ fn load_spec(id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, St
})
}

macro_rules! with_runtime_or_err {
($chain_spec:expr, { $( $code:tt )* }) => {
if $chain_spec.is_interlay() {
#[allow(unused_imports)]
use { interlay_runtime::RuntimeApi, crate::service::InterlayRuntimeExecutor as Executor };
$( $code )*

} else if $chain_spec.is_kintsugi() {
#[allow(unused_imports)]
use { kintsugi_runtime::RuntimeApi, crate::service::KintsugiRuntimeExecutor as Executor };
$( $code )*

} else {
#[allow(unused_imports)]
use { testnet_runtime::RuntimeApi, crate::service::TestnetRuntimeExecutor as Executor };
$( $code )*

}
}
}

impl SubstrateCli for Cli {
fn impl_name() -> String {
"interBTC Parachain".into()
Expand Down Expand Up @@ -212,6 +233,7 @@ macro_rules! construct_async_run {
runner.async_run(|$config| {
let $components = new_partial::<interlay_runtime::RuntimeApi, InterlayRuntimeExecutor>(
&$config,
true,
)?;
let task_manager = $components.task_manager;
{ $( $code )* }.map(|v| (v, task_manager))
Expand All @@ -223,6 +245,7 @@ macro_rules! construct_async_run {
KintsugiRuntimeExecutor,
>(
&$config,
true,
)?;
let task_manager = $components.task_manager;
{ $( $code )* }.map(|v| (v, task_manager))
Expand All @@ -234,6 +257,7 @@ macro_rules! construct_async_run {
TestnetRuntimeExecutor,
>(
&$config,
true,
)?;
let task_manager = $components.task_manager;
{ $( $code )* }.map(|v| (v, task_manager))
Expand Down Expand Up @@ -291,15 +315,11 @@ pub fn run() -> Result<()> {
Some(Subcommand::Benchmark(cmd)) => {
if cfg!(feature = "runtime-benchmarks") {
let runner = cli.create_runner(cmd)?;
if runner.config().chain_spec.is_interlay() {
runner.sync_run(|config| cmd.run::<Block, InterlayRuntimeExecutor>(config))
} else if runner.config().chain_spec.is_kintsugi() {
runner.sync_run(|config| cmd.run::<Block, KintsugiRuntimeExecutor>(config))
} else if runner.config().chain_spec.is_testnet() {
runner.sync_run(|config| cmd.run::<Block, TestnetRuntimeExecutor>(config))
} else {
Err("Chain doesn't support benchmarking".into())
}
let chain_spec = &runner.config().chain_spec;

with_runtime_or_err!(chain_spec, {
return runner.sync_run(|config| cmd.run::<Block, Executor>(config));
})
} else {
Err("Benchmarking wasn't enabled when building the node. \
You can enable it with `--features runtime-benchmarks`."
Expand Down Expand Up @@ -349,12 +369,29 @@ pub fn run() -> Result<()> {
let runner = cli.create_runner(&cli.run.normalize())?;

runner
.run_node_until_exit(|config| async move { start_node(cli, config).await })
.run_node_until_exit(|config| async move {
if cli.instant_seal {
start_instant(config).await
} else {
start_node(cli, config).await
}
})
.map_err(Into::into)
}
}
}

async fn start_instant(config: Configuration) -> sc_service::error::Result<TaskManager> {
with_runtime_or_err!(config.chain_spec, {
{
crate::service::start_instant::<RuntimeApi, Executor>(config)
.await
.map(|r| r.0)
.map_err(Into::into)
}
})
}

async fn start_node(cli: Cli, config: Configuration) -> sc_service::error::Result<TaskManager> {
let para_id = chain_spec::Extensions::try_get(&*config.chain_spec).map(|e| e.para_id);

Expand Down Expand Up @@ -387,37 +424,14 @@ async fn start_node(cli: Cli, config: Configuration) -> sc_service::error::Resul
if config.role.is_authority() { "yes" } else { "no" }
);

if config.chain_spec.is_interlay() {
crate::service::start_node::<interlay_runtime::RuntimeApi, InterlayRuntimeExecutor>(
config,
polkadot_config,
collator_options,
id,
)
.await
.map(|r| r.0)
.map_err(Into::into)
} else if config.chain_spec.is_kintsugi() {
crate::service::start_node::<kintsugi_runtime::RuntimeApi, KintsugiRuntimeExecutor>(
config,
polkadot_config,
collator_options,
id,
)
.await
.map(|r| r.0)
.map_err(Into::into)
} else {
crate::service::start_node::<testnet_runtime::RuntimeApi, TestnetRuntimeExecutor>(
config,
polkadot_config,
collator_options,
id,
)
.await
.map(|r| r.0)
.map_err(Into::into)
}
with_runtime_or_err!(config.chain_spec, {
{
crate::service::start_node::<RuntimeApi, Executor>(config, polkadot_config, collator_options, id)
.await
.map(|r| r.0)
.map_err(Into::into)
}
})
}

impl DefaultConfigurationValues for RelayChainCli {
Expand Down

0 comments on commit 94f2df3

Please sign in to comment.