Skip to content

Commit 9210f83

Browse files
authored
fix: execute-block handle no explicit --at (#84)
`execute-block` currently panics during block execution if `--at` is not passed, due to trying to execute a block that doesn't match current state.
1 parent 226514e commit 9210f83

4 files changed

Lines changed: 63 additions & 5 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ resolver = "2"
44
members = ["cli", "core"]
55

66
[workspace.package]
7-
version = "0.6.1"
7+
version = "0.6.2"
88
authors = ["Parity Technologies <admin@parity.io>"]
99
description = "Substrate's programmatic testing framework."
1010
edition = "2021"

core/src/commands/execute_block.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,27 @@ where
9898
let rpc = ws_client(&block_ws_uri).await?;
9999

100100
let live_state = match command.state {
101-
State::Live(live_state) => live_state,
101+
State::Live(live_state) => {
102+
// If no --at is provided, get the latest block to replay
103+
if live_state.at.is_some() {
104+
live_state
105+
} else {
106+
let header =
107+
ChainApi::<(), Block::Hash, Block::Header, SignedBlock<Block>>::header(
108+
&rpc, None,
109+
)
110+
.await
111+
.map_err(rpc_err_handler)?
112+
.expect("header exists, block should also exist; qed");
113+
LiveState {
114+
uri: block_ws_uri,
115+
at: Some(hex::encode(header.hash().encode())),
116+
pallet: Default::default(),
117+
hashed_prefixes: Default::default(),
118+
child_tree: Default::default(),
119+
}
120+
}
121+
}
102122
_ => {
103123
unreachable!("execute block currently only supports Live state")
104124
}

core/tests/execute_block.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use tokio::process::Command;
2828
#[tokio::test]
2929
async fn execute_block_works() {
3030
let port = 45789;
31-
let ws_url = format!("ws://localhost:{}", port);
3231

3332
// Spawn a dev node.
3433
let _ = std::thread::spawn(move || {
@@ -46,7 +45,10 @@ async fn execute_block_works() {
4645
// Wait some time to ensure the node is warmed up.
4746
std::thread::sleep(Duration::from_secs(90));
4847

48+
// Test passing --at
4949
common::run_with_timeout(Duration::from_secs(60), async move {
50+
let ws_url = format!("ws://localhost:{}", port);
51+
5052
fn execute_block(ws_url: &str, at: Hash) -> tokio::process::Child {
5153
Command::new(cargo_bin("try-runtime"))
5254
.stdout(std::process::Stdio::piped())
@@ -83,5 +85,41 @@ async fn execute_block_works() {
8385
.status
8486
.success());
8587
})
88+
.await;
89+
90+
// Test not passing --at
91+
common::run_with_timeout(Duration::from_secs(60), async move {
92+
let ws_url = format!("ws://localhost:{}", port);
93+
94+
fn execute_block(ws_url: &str) -> tokio::process::Child {
95+
Command::new(cargo_bin("try-runtime"))
96+
.stdout(std::process::Stdio::piped())
97+
.stderr(std::process::Stdio::piped())
98+
.arg("--runtime=existing")
99+
.args(["execute-block"])
100+
.args(["live", format!("--uri={}", ws_url).as_str()])
101+
.kill_on_drop(true)
102+
.spawn()
103+
.unwrap()
104+
}
105+
106+
// Try to execute the block.
107+
let mut block_execution = execute_block(&ws_url);
108+
let expected_output = r".*Block #(\d+) successfully executed";
109+
let re = Regex::new(expected_output).unwrap();
110+
let matched =
111+
common::wait_for_stream_pattern_match(block_execution.stderr.take().unwrap(), re).await;
112+
113+
// Assert that the block-execution process has executed a block.
114+
assert!(matched.is_ok());
115+
116+
// Assert that the block-execution exited succesfully
117+
assert!(block_execution
118+
.wait_with_output()
119+
.await
120+
.unwrap()
121+
.status
122+
.success());
123+
})
86124
.await
87125
}

0 commit comments

Comments
 (0)