Skip to content

Commit

Permalink
Check forked node startup with retries
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Jan 2, 2025
1 parent 1294738 commit e6be341
Showing 1 changed file with 52 additions and 15 deletions.
67 changes: 52 additions & 15 deletions crates/e2e/src/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,7 @@ async fn run<F, Fut, T>(
// it but rather in the locked state.
let _lock = NODE_MUTEX.lock();

let node = match fork {
Some((fork, block_number)) => Node::forked(fork, block_number).await,
None => Node::new().await,
};

let node = Arc::new(Mutex::new(Some(node)));
let node_panic_handle = node.clone();
observe::panic_hook::prepend_panic_handler(Box::new(move |_| {
// Drop node in panic handler because `.catch_unwind()` does not catch all
// panics
let _ = node_panic_handle.lock().unwrap().take();
}));

let http = create_test_transport(NODE_HOST);
let web3 = Web3::new(http);
let (node, web3) = spawn_node_with_retries(&fork, 3).await;

services::clear_database().await;
// Hack: the closure may actually be unwind unsafe; moreover, `catch_unwind`
Expand All @@ -227,6 +213,57 @@ async fn run<F, Fut, T>(
}
}

async fn try_spawn_node_and_check(
fork: &Option<(String, Option<u64>)>,
) -> Option<(Arc<Mutex<Option<Node>>>, Web3)> {
let node = match fork {
Some((fork_url, block_number)) => Node::forked(fork_url.clone(), *block_number).await,
None => Node::new().await,
};
let node = Arc::new(Mutex::new(Some(node)));

let node_panic_handle = node.clone();
observe::panic_hook::prepend_panic_handler(Box::new(move |_| {
// Drop node in panic handler because `.catch_unwind()` does not catch all
// panics
let _ = node_panic_handle.lock().unwrap().take();
}));

let http = create_test_transport(NODE_HOST);
let web3 = Web3::new(http);

let block_check = tokio::time::timeout(Duration::from_secs(2), web3.eth().block_number()).await;
match block_check {
Ok(Ok(_)) => Some((node, web3)),
_ => {
let node = node.lock().unwrap().take();
if let Some(mut node) = node {
node.kill().await;
}
None
}
}
}

async fn spawn_node_with_retries(
fork: &Option<(String, Option<u64>)>,
max_attempts: u32,
) -> (Arc<Mutex<Option<Node>>>, Web3) {
let mut attempts = 0;

while attempts < max_attempts {
match try_spawn_node_and_check(fork).await {
Some((node, web3)) => return (node, web3),
None => attempts += 1,
}
}

panic!(
"Failed to get block number after {:?} attempts",
max_attempts
);
}

#[macro_export]
macro_rules! assert_approximately_eq {
($executed_value:expr, $expected_value:expr) => {{
Expand Down

0 comments on commit e6be341

Please sign in to comment.