Skip to content

Commit c0ee512

Browse files
liamaharonlexnv
andauthored
test-utils: programatically spawn dev nodes (paritytech#14704)
* allow spinning up dev node in background without binary * improve comments * restore rust-toolchain * remove rust-toolchain * tweak start_node_without_binary api * Update test-utils/cli/src/lib.rs Co-authored-by: Alexandru Vasile <[email protected]> * address commends * use &str * update example * update comment * update docs * Revert "update docs" This reverts commit e18677f. * use node-cli instead of node-template * fix feature * fix feature * fix features --------- Co-authored-by: Alexandru Vasile <[email protected]>
1 parent cffd582 commit c0ee512

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

Cargo.lock

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/cli/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ try-runtime = [
184184
"pallet-balances/try-runtime",
185185
"pallet-im-online/try-runtime",
186186
"pallet-timestamp/try-runtime",
187-
"sp-runtime/try-runtime"
187+
"sp-runtime/try-runtime",
188+
"substrate-cli-test-utils/try-runtime"
188189
]
189190

190191
[[bench]]

test-utils/cli/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ nix = "0.26.2"
2020
regex = "1.7.3"
2121
tokio = { version = "1.22.0", features = ["full"] }
2222
node-primitives = { path = "../../bin/node/primitives" }
23+
node-cli = { path = "../../bin/node/cli" }
24+
sc-cli = { path = "../../client/cli" }
25+
sc-service = { path = "../../client/service" }
2326
futures = "0.3.28"
27+
28+
[features]
29+
try-runtime = ["node-cli/try-runtime"]

test-utils/cli/src/lib.rs

+32
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,38 @@ use std::{
3535
};
3636
use tokio::io::{AsyncBufReadExt, AsyncRead};
3737

38+
/// Similar to [`crate::start_node`] spawns a node, but works in environments where the substrate
39+
/// binary is not accessible with `cargo_bin("substrate-node")`, and allows customising the args
40+
/// passed in.
41+
///
42+
/// Helpful if you need a Substrate dev node running in the background of a project external to
43+
/// `substrate`.
44+
///
45+
/// The downside compared to using [`crate::start_node`] is that this method is blocking rather than
46+
/// returning a [`Child`]. Therefore, you may want to call this method inside a new thread.
47+
///
48+
/// # Example
49+
/// ```ignore
50+
/// // Spawn a dev node.
51+
/// let _ = std::thread::spawn(move || {
52+
/// match common::start_node_inline(vec!["--dev", "--rpc-port=12345"]) {
53+
/// Ok(_) => {}
54+
/// Err(e) => {
55+
/// panic!("Node exited with error: {}", e);
56+
/// }
57+
/// }
58+
/// });
59+
/// ```
60+
pub fn start_node_inline(args: Vec<&str>) -> Result<(), sc_service::error::Error> {
61+
use sc_cli::SubstrateCli;
62+
63+
// Prepend the args with some dummy value because the first arg is skipped.
64+
let cli_call = std::iter::once("node-template").chain(args);
65+
let cli = node_cli::Cli::from_iter(cli_call);
66+
let runner = cli.create_runner(&cli.run).unwrap();
67+
runner.run_node_until_exit(|config| async move { node_cli::service::new_full(config, cli) })
68+
}
69+
3870
/// Starts a new Substrate node in development mode with a temporary chain.
3971
///
4072
/// This function creates a new Substrate node using the `substrate` binary.

utils/frame/try-runtime/cli/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ tokio = "1.27.0"
5555
try-runtime = [
5656
"sp-debug-derive/force-debug",
5757
"frame-try-runtime/try-runtime",
58-
"sp-runtime/try-runtime"
58+
"sp-runtime/try-runtime",
59+
"substrate-cli-test-utils/try-runtime"
5960
]

0 commit comments

Comments
 (0)