diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index c2d53ccd9c..52b47621e5 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -11427,8 +11427,11 @@ dependencies = [ "assert_cmd", "futures", "nix 0.26.2", + "node-cli", "node-primitives", "regex", + "sc-cli", + "sc-service", "sp-rpc", "substrate-rpc-client", "tokio", diff --git a/substrate/bin/node/cli/Cargo.toml b/substrate/bin/node/cli/Cargo.toml index dd8c1c8e77..37f03fbb0d 100644 --- a/substrate/bin/node/cli/Cargo.toml +++ b/substrate/bin/node/cli/Cargo.toml @@ -184,7 +184,8 @@ try-runtime = [ "pallet-balances/try-runtime", "pallet-im-online/try-runtime", "pallet-timestamp/try-runtime", - "sp-runtime/try-runtime" + "sp-runtime/try-runtime", + "substrate-cli-test-utils/try-runtime" ] [[bench]] diff --git a/substrate/test-utils/cli/Cargo.toml b/substrate/test-utils/cli/Cargo.toml index ac8c519a65..314fe7ad56 100644 --- a/substrate/test-utils/cli/Cargo.toml +++ b/substrate/test-utils/cli/Cargo.toml @@ -20,4 +20,10 @@ nix = "0.26.2" regex = "1.7.3" tokio = { version = "1.22.0", features = ["full"] } node-primitives = { path = "../../bin/node/primitives" } +node-cli = { path = "../../bin/node/cli" } +sc-cli = { path = "../../client/cli" } +sc-service = { path = "../../client/service" } futures = "0.3.28" + +[features] +try-runtime = ["node-cli/try-runtime"] diff --git a/substrate/test-utils/cli/src/lib.rs b/substrate/test-utils/cli/src/lib.rs index 7e704b70f8..99119a44d2 100644 --- a/substrate/test-utils/cli/src/lib.rs +++ b/substrate/test-utils/cli/src/lib.rs @@ -35,6 +35,38 @@ use std::{ }; use tokio::io::{AsyncBufReadExt, AsyncRead}; +/// Similar to [`crate::start_node`] spawns a node, but works in environments where the substrate +/// binary is not accessible with `cargo_bin("substrate-node")`, and allows customising the args +/// passed in. +/// +/// Helpful if you need a Substrate dev node running in the background of a project external to +/// `substrate`. +/// +/// The downside compared to using [`crate::start_node`] is that this method is blocking rather than +/// returning a [`Child`]. Therefore, you may want to call this method inside a new thread. +/// +/// # Example +/// ```ignore +/// // Spawn a dev node. +/// let _ = std::thread::spawn(move || { +/// match common::start_node_inline(vec!["--dev", "--rpc-port=12345"]) { +/// Ok(_) => {} +/// Err(e) => { +/// panic!("Node exited with error: {}", e); +/// } +/// } +/// }); +/// ``` +pub fn start_node_inline(args: Vec<&str>) -> Result<(), sc_service::error::Error> { + use sc_cli::SubstrateCli; + + // Prepend the args with some dummy value because the first arg is skipped. + let cli_call = std::iter::once("node-template").chain(args); + let cli = node_cli::Cli::from_iter(cli_call); + let runner = cli.create_runner(&cli.run).unwrap(); + runner.run_node_until_exit(|config| async move { node_cli::service::new_full(config, cli) }) +} + /// Starts a new Substrate node in development mode with a temporary chain. /// /// This function creates a new Substrate node using the `substrate` binary. diff --git a/substrate/utils/frame/try-runtime/cli/Cargo.toml b/substrate/utils/frame/try-runtime/cli/Cargo.toml index 61c2fcc2ec..84b9460d13 100644 --- a/substrate/utils/frame/try-runtime/cli/Cargo.toml +++ b/substrate/utils/frame/try-runtime/cli/Cargo.toml @@ -55,5 +55,6 @@ tokio = "1.27.0" try-runtime = [ "sp-debug-derive/force-debug", "frame-try-runtime/try-runtime", - "sp-runtime/try-runtime" + "sp-runtime/try-runtime", + "substrate-cli-test-utils/try-runtime" ]