Skip to content

Commit

Permalink
fix: execute-block handle no explicit --at (#84)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
liamaharon authored Apr 30, 2024
1 parent 226514e commit 9210f83
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"
members = ["cli", "core"]

[workspace.package]
version = "0.6.1"
version = "0.6.2"
authors = ["Parity Technologies <[email protected]>"]
description = "Substrate's programmatic testing framework."
edition = "2021"
Expand Down
22 changes: 21 additions & 1 deletion core/src/commands/execute_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,27 @@ where
let rpc = ws_client(&block_ws_uri).await?;

let live_state = match command.state {
State::Live(live_state) => live_state,
State::Live(live_state) => {
// If no --at is provided, get the latest block to replay
if live_state.at.is_some() {
live_state
} else {
let header =
ChainApi::<(), Block::Hash, Block::Header, SignedBlock<Block>>::header(
&rpc, None,
)
.await
.map_err(rpc_err_handler)?
.expect("header exists, block should also exist; qed");
LiveState {
uri: block_ws_uri,
at: Some(hex::encode(header.hash().encode())),
pallet: Default::default(),
hashed_prefixes: Default::default(),
child_tree: Default::default(),
}
}
}
_ => {
unreachable!("execute block currently only supports Live state")
}
Expand Down
40 changes: 39 additions & 1 deletion core/tests/execute_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use tokio::process::Command;
#[tokio::test]
async fn execute_block_works() {
let port = 45789;
let ws_url = format!("ws://localhost:{}", port);

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

// Test passing --at
common::run_with_timeout(Duration::from_secs(60), async move {
let ws_url = format!("ws://localhost:{}", port);

fn execute_block(ws_url: &str, at: Hash) -> tokio::process::Child {
Command::new(cargo_bin("try-runtime"))
.stdout(std::process::Stdio::piped())
Expand Down Expand Up @@ -83,5 +85,41 @@ async fn execute_block_works() {
.status
.success());
})
.await;

// Test not passing --at
common::run_with_timeout(Duration::from_secs(60), async move {
let ws_url = format!("ws://localhost:{}", port);

fn execute_block(ws_url: &str) -> tokio::process::Child {
Command::new(cargo_bin("try-runtime"))
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.arg("--runtime=existing")
.args(["execute-block"])
.args(["live", format!("--uri={}", ws_url).as_str()])
.kill_on_drop(true)
.spawn()
.unwrap()
}

// Try to execute the block.
let mut block_execution = execute_block(&ws_url);
let expected_output = r".*Block #(\d+) successfully executed";
let re = Regex::new(expected_output).unwrap();
let matched =
common::wait_for_stream_pattern_match(block_execution.stderr.take().unwrap(), re).await;

// Assert that the block-execution process has executed a block.
assert!(matched.is_ok());

// Assert that the block-execution exited succesfully
assert!(block_execution
.wait_with_output()
.await
.unwrap()
.status
.success());
})
.await
}

0 comments on commit 9210f83

Please sign in to comment.