Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 8da9ad8

Browse files
Integrate try-runtime into substrate node template (#10909)
* [10892-integrate-try-runtime-into-node-template] - Integrated try-runtime into node template * [10892-integrate-try-runtime-into-node-template] Added match arms for try-runtime in command.rs * [10892-integrate-try-runtime-into-node-template] Added match arms for try-runtime in command.rs * Added feature flag for try-runtime in node-template/node and enabled try-runtime for node-template/runtime * Added missing type annotations for try-runtime SubCommand in node-template * Added missing type annotations for try-runtime SubCommand in node-template * Implemented frame_try_runtime::TryRuntime<Block> for the node-template Runtime
1 parent bfc6fb4 commit 8da9ad8

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node-template/node/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ frame-benchmarking-cli = { version = "4.0.0-dev", path = "../../../utils/frame/b
5555
# Local Dependencies
5656
node-template-runtime = { version = "4.0.0-dev", path = "../runtime" }
5757

58+
# CLI-specific dependencies
59+
try-runtime-cli = { version = "0.10.0-dev", optional = true, path = "../../../utils/frame/try-runtime/cli" }
60+
5861
[build-dependencies]
5962
substrate-build-script-utils = { version = "3.0.0", path = "../../../utils/build-script-utils" }
6063

6164
[features]
6265
default = []
6366
runtime-benchmarks = ["node-template-runtime/runtime-benchmarks"]
67+
# Enable features that allow the runtime to be tried and debugged. Name might be subject to change
68+
# in the near future.
69+
try-runtime = ["node-template-runtime/try-runtime", "try-runtime-cli"]

bin/node-template/node/src/cli.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ pub enum Subcommand {
3939
/// The custom benchmark subcommand benchmarking runtime pallets.
4040
#[clap(name = "benchmark", about = "Benchmark runtime pallets.")]
4141
Benchmark(frame_benchmarking_cli::BenchmarkCmd),
42+
43+
/// Try some command against runtime state.
44+
#[cfg(feature = "try-runtime")]
45+
TryRuntime(try_runtime_cli::TryRuntimeCmd),
46+
47+
/// Try some command against runtime state. Note: `try-runtime` feature must be enabled.
48+
#[cfg(not(feature = "try-runtime"))]
49+
TryRuntime,
4250
}

bin/node-template/node/src/command.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ pub fn run() -> sc_cli::Result<()> {
108108
`--features runtime-benchmarks`."
109109
.into())
110110
},
111+
#[cfg(feature = "try-runtime")]
112+
Some(Subcommand::TryRuntime(cmd)) => {
113+
let runner = cli.create_runner(cmd)?;
114+
runner.async_run(|config| {
115+
// we don't need any of the components of new_partial, just a runtime, or a task
116+
// manager to do `async_run`.
117+
let registry = config.prometheus_config.as_ref().map(|cfg| &cfg.registry);
118+
let task_manager =
119+
sc_service::TaskManager::new(config.tokio_handle.clone(), registry)
120+
.map_err(|e| sc_cli::Error::Service(sc_service::Error::Prometheus(e)))?;
121+
Ok((cmd.run::<Block, service::ExecutorDispatch>(config), task_manager))
122+
})
123+
},
124+
#[cfg(not(feature = "try-runtime"))]
125+
Some(Subcommand::TryRuntime) => Err("TryRuntime wasn't enabled when building the node. \
126+
You can enable it with `--features try-runtime`."
127+
.into()),
111128
None => {
112129
let runner = cli.create_runner(&cli.run)?;
113130
runner.run_node_until_exit(|config| async move {

bin/node-template/runtime/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pallet-grandpa = { version = "4.0.0-dev", default-features = false, path = "../.
2323
pallet-randomness-collective-flip = { version = "4.0.0-dev", default-features = false, path = "../../../frame/randomness-collective-flip" }
2424
pallet-sudo = { version = "4.0.0-dev", default-features = false, path = "../../../frame/sudo" }
2525
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../../frame/system" }
26+
frame-try-runtime = { version = "0.10.0-dev", default-features = false, path = "../../../frame/try-runtime", optional = true }
2627
pallet-timestamp = { version = "4.0.0-dev", default-features = false, path = "../../../frame/timestamp" }
2728
pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, path = "../../../frame/transaction-payment" }
2829
frame-executive = { version = "4.0.0-dev", default-features = false, path = "../../../frame/executive" }
@@ -94,3 +95,14 @@ runtime-benchmarks = [
9495
"pallet-timestamp/runtime-benchmarks",
9596
"sp-runtime/runtime-benchmarks",
9697
]
98+
try-runtime = [
99+
"frame-executive/try-runtime",
100+
"frame-try-runtime",
101+
"frame-system/try-runtime",
102+
"pallet-balances/try-runtime",
103+
"pallet-grandpa/try-runtime",
104+
"pallet-randomness-collective-flip/try-runtime",
105+
"pallet-sudo/try-runtime",
106+
"pallet-timestamp/try-runtime",
107+
"pallet-transaction-payment/try-runtime",
108+
]

bin/node-template/runtime/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,19 @@ impl_runtime_apis! {
515515
Ok(batches)
516516
}
517517
}
518+
519+
#[cfg(feature = "try-runtime")]
520+
impl frame_try_runtime::TryRuntime<Block> for Runtime {
521+
fn on_runtime_upgrade() -> (Weight, Weight) {
522+
// NOTE: intentional unwrap: we don't want to propagate the error backwards, and want to
523+
// have a backtrace here. If any of the pre/post migration checks fail, we shall stop
524+
// right here and right now.
525+
let weight = Executive::try_runtime_upgrade().unwrap();
526+
(weight, BlockWeights::get().max_block)
527+
}
528+
529+
fn execute_block_no_check(block: Block) -> Weight {
530+
Executive::execute_block_no_check(block)
531+
}
532+
}
518533
}

0 commit comments

Comments
 (0)