diff --git a/crates/node-bindings/tests/anvil.rs b/crates/node-bindings/tests/anvil.rs deleted file mode 100644 index 544df2f060d..00000000000 --- a/crates/node-bindings/tests/anvil.rs +++ /dev/null @@ -1,42 +0,0 @@ -use alloy_node_bindings::Anvil; - -/// Run the given function only if we are in a CI environment. -fn ci_only(f: F) -where - F: FnOnce(), -{ - if ci_info::is_ci() { - f(); - } -} - -#[test] -fn can_launch_anvil() { - ci_only(|| { - let _ = Anvil::new().spawn(); - }); -} - -#[test] -fn can_launch_anvil_with_more_accounts() { - ci_only(|| { - let _ = Anvil::new().arg("--accounts").arg("20").spawn(); - }) -} - -#[test] -fn assert_chain_id() { - ci_only(|| { - let id = 99999; - let anvil = Anvil::new().chain_id(id).spawn(); - assert_eq!(anvil.chain_id(), id); - }) -} - -#[test] -fn assert_chain_id_without_rpc() { - ci_only(|| { - let anvil = Anvil::new().spawn(); - assert_eq!(anvil.chain_id(), 31337); - }); -} diff --git a/crates/node-bindings/tests/geth.rs b/crates/node-bindings/tests/geth.rs deleted file mode 100644 index 47a8f57a6b8..00000000000 --- a/crates/node-bindings/tests/geth.rs +++ /dev/null @@ -1,75 +0,0 @@ -use alloy_node_bindings::{utils::run_with_tempdir_sync, Geth}; -use k256::ecdsa::SigningKey; - -/// Run the given function only if we are in a CI environment. -fn ci_only(f: F) -where - F: FnOnce(), -{ - if ci_info::is_ci() { - f(); - } -} -#[test] -fn port_0() { - ci_only(|| { - run_with_tempdir_sync("geth-test-", |_| { - let _geth = Geth::new().disable_discovery().port(0u16).spawn(); - }); - }) -} - -#[test] -fn p2p_port() { - ci_only(|| { - run_with_tempdir_sync("geth-test-", |temp_dir_path| { - let geth = Geth::new().disable_discovery().data_dir(temp_dir_path).spawn(); - let p2p_port = geth.p2p_port(); - assert!(p2p_port.is_some()); - }); - }) -} - -#[test] -fn explicit_p2p_port() { - ci_only(|| { - run_with_tempdir_sync("geth-test-", |temp_dir_path| { - // if a p2p port is explicitly set, it should be used - let geth = Geth::new().p2p_port(1234).data_dir(temp_dir_path).spawn(); - let p2p_port = geth.p2p_port(); - assert_eq!(p2p_port, Some(1234)); - }); - }) -} - -#[test] -fn dev_mode() { - ci_only(|| { - run_with_tempdir_sync("geth-test-", |temp_dir_path| { - // dev mode should not have a p2p port, and dev should be the default - let geth = Geth::new().data_dir(temp_dir_path).spawn(); - let p2p_port = geth.p2p_port(); - assert!(p2p_port.is_none(), "{p2p_port:?}"); - }) - }) -} - -#[test] -#[ignore = "fails on geth >=1.14"] -#[allow(deprecated)] -fn clique_correctly_configured() { - ci_only(|| { - run_with_tempdir_sync("geth-test-", |temp_dir_path| { - let private_key = SigningKey::random(&mut rand::thread_rng()); - let geth = Geth::new() - .set_clique_private_key(private_key) - .chain_id(1337u64) - .data_dir(temp_dir_path) - .spawn(); - - assert!(geth.p2p_port().is_some()); - assert!(geth.clique_private_key().is_some()); - assert!(geth.genesis().is_some()); - }) - }) -} diff --git a/crates/node-bindings/tests/it/anvil.rs b/crates/node-bindings/tests/it/anvil.rs new file mode 100644 index 00000000000..81a4ec7f7ea --- /dev/null +++ b/crates/node-bindings/tests/it/anvil.rs @@ -0,0 +1,40 @@ +use alloy_node_bindings::Anvil; + +#[test] +fn can_launch_anvil() { + if !ci_info::is_ci() { + return; + } + + let _ = Anvil::new().spawn(); +} + +#[test] +fn can_launch_anvil_with_more_accounts() { + if !ci_info::is_ci() { + return; + } + + let _ = Anvil::new().arg("--accounts").arg("20").spawn(); +} + +#[test] +fn assert_chain_id() { + if !ci_info::is_ci() { + return; + } + + let id = 99999; + let anvil = Anvil::new().chain_id(id).spawn(); + assert_eq!(anvil.chain_id(), id); +} + +#[test] +fn assert_chain_id_without_rpc() { + if !ci_info::is_ci() { + return; + } + + let anvil = Anvil::new().spawn(); + assert_eq!(anvil.chain_id(), 31337); +} diff --git a/crates/node-bindings/tests/it/geth.rs b/crates/node-bindings/tests/it/geth.rs new file mode 100644 index 00000000000..82459cb3a86 --- /dev/null +++ b/crates/node-bindings/tests/it/geth.rs @@ -0,0 +1,76 @@ +use alloy_node_bindings::{utils::run_with_tempdir_sync, Geth}; +use k256::ecdsa::SigningKey; + +#[test] +fn port_0() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("geth-test-", |_| { + let _geth = Geth::new().disable_discovery().port(0u16).spawn(); + }); +} + +#[test] +fn p2p_port() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("geth-test-", |temp_dir_path| { + let geth = Geth::new().disable_discovery().data_dir(temp_dir_path).spawn(); + let p2p_port = geth.p2p_port(); + assert!(p2p_port.is_some()); + }); +} + +#[test] +fn explicit_p2p_port() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("geth-test-", |temp_dir_path| { + // if a p2p port is explicitly set, it should be used + let geth = Geth::new().p2p_port(1234).data_dir(temp_dir_path).spawn(); + let p2p_port = geth.p2p_port(); + assert_eq!(p2p_port, Some(1234)); + }); +} + +#[test] +fn dev_mode() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("geth-test-", |temp_dir_path| { + // dev mode should not have a p2p port, and dev should be the default + let geth = Geth::new().data_dir(temp_dir_path).spawn(); + let p2p_port = geth.p2p_port(); + assert!(p2p_port.is_none(), "{p2p_port:?}"); + }) +} + +#[test] +#[ignore = "fails on geth >=1.14"] +#[allow(deprecated)] +fn clique_correctly_configured() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("geth-test-", |temp_dir_path| { + let private_key = SigningKey::random(&mut rand::thread_rng()); + let geth = Geth::new() + .set_clique_private_key(private_key) + .chain_id(1337u64) + .data_dir(temp_dir_path) + .spawn(); + + assert!(geth.p2p_port().is_some()); + assert!(geth.clique_private_key().is_some()); + assert!(geth.genesis().is_some()); + }) +} diff --git a/crates/node-bindings/tests/it/main.rs b/crates/node-bindings/tests/it/main.rs new file mode 100644 index 00000000000..81aaa2a0471 --- /dev/null +++ b/crates/node-bindings/tests/it/main.rs @@ -0,0 +1,5 @@ +#![allow(missing_docs)] + +mod anvil; +mod geth; +mod reth; diff --git a/crates/node-bindings/tests/it/reth.rs b/crates/node-bindings/tests/it/reth.rs new file mode 100644 index 00000000000..dd5188f95ed --- /dev/null +++ b/crates/node-bindings/tests/it/reth.rs @@ -0,0 +1,191 @@ +// These tests should use a different datadir for each `reth` instance spawned. + +use alloy_genesis::Genesis; +use alloy_node_bindings::{utils::run_with_tempdir_sync, Reth, RethInstance}; + +/// The default HTTP port for Reth. +const DEFAULT_HTTP_PORT: u16 = 8545; + +/// The default WS port for Reth. +const DEFAULT_WS_PORT: u16 = 8546; + +/// The default auth port for Reth. +const DEFAULT_AUTH_PORT: u16 = 8551; + +/// The default P2P port for Reth. +const DEFAULT_P2P_PORT: u16 = 30303; + +#[test] +#[cfg_attr(windows, ignore)] +fn can_launch_reth() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new().data_dir(temp_dir_path).spawn(); + + assert_ports(&reth, false); + }); +} + +#[test] +#[cfg_attr(windows, ignore)] +fn can_launch_reth_sepolia() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new().chain_or_path("sepolia").data_dir(temp_dir_path).spawn(); + + assert_ports(&reth, false); + }); +} + +#[test] +#[cfg_attr(windows, ignore)] +fn can_launch_reth_dev() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new().dev().disable_discovery().data_dir(temp_dir_path).spawn(); + + assert_ports(&reth, true); + }); +} + +#[test] +#[cfg_attr(windows, ignore)] +fn can_launch_reth_dev_custom_genesis() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new() + .dev() + .disable_discovery() + .data_dir(temp_dir_path) + .genesis(Genesis::default()) + .spawn(); + + assert_ports(&reth, true); + }); +} + +#[test] +#[cfg_attr(windows, ignore)] +fn can_launch_reth_dev_custom_blocktime() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new() + .dev() + .disable_discovery() + .block_time("1sec") + .data_dir(temp_dir_path) + .spawn(); + + assert_ports(&reth, true); + }); +} + +#[test] +#[cfg_attr(windows, ignore)] +fn can_launch_reth_p2p_instances() { + if !ci_info::is_ci() { + return; + } + + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new().instance(100).data_dir(temp_dir_path).spawn(); + + assert_ports(&reth, false); + + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new().instance(101).data_dir(temp_dir_path).spawn(); + + assert_ports(&reth, false); + }); + }); +} + +// Tests that occupy the same port are combined so they are ran sequentially, to prevent +// flakiness. +#[test] +#[cfg_attr(windows, ignore)] +fn can_launch_reth_custom_ports() { + if !ci_info::is_ci() { + return; + } + + // Assert that all ports are default if no custom ports are set + // and the instance is set to 0. + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new().instance(0).data_dir(temp_dir_path).spawn(); + + assert_eq!(reth.http_port(), DEFAULT_HTTP_PORT); + assert_eq!(reth.ws_port(), DEFAULT_WS_PORT); + assert_eq!(reth.auth_port(), Some(DEFAULT_AUTH_PORT)); + assert_eq!(reth.p2p_port(), Some(DEFAULT_P2P_PORT)); + }); + + // Assert that only the HTTP port is set and the rest are default. + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new().http_port(8577).data_dir(temp_dir_path).spawn(); + + assert_eq!(reth.http_port(), 8577); + assert_eq!(reth.ws_port(), DEFAULT_WS_PORT); + assert_eq!(reth.auth_port(), Some(DEFAULT_AUTH_PORT)); + assert_eq!(reth.p2p_port(), Some(DEFAULT_P2P_PORT)); + }); + + // Assert that all ports can be set. + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new() + .http_port(8577) + .ws_port(8578) + .auth_port(8579) + .p2p_port(30307) + .data_dir(temp_dir_path) + .spawn(); + + assert_eq!(reth.http_port(), 8577); + assert_eq!(reth.ws_port(), 8578); + assert_eq!(reth.auth_port(), Some(8579)); + assert_eq!(reth.p2p_port(), Some(30307)); + }); + + // Assert that the HTTP port is picked by the OS and the rest are default. + run_with_tempdir_sync("reth-test-", |temp_dir_path| { + let reth = Reth::new().http_port(0).data_dir(temp_dir_path).spawn(); + + // Assert that a random unused port is used picked by the OS. + assert_ne!(reth.http_port(), DEFAULT_HTTP_PORT); + + assert_eq!(reth.ws_port(), DEFAULT_WS_PORT); + assert_eq!(reth.auth_port(), Some(DEFAULT_AUTH_PORT)); + assert_eq!(reth.p2p_port(), Some(DEFAULT_P2P_PORT)); + }); +} + +// Asserts that the ports are set correctly for the given Reth instance. +fn assert_ports(reth: &RethInstance, dev: bool) { + // Changes to the following port numbers for each instance: + // - `HTTP_RPC_PORT`: default - `instance` + 1 + // - `WS_RPC_PORT`: default + `instance` * 2 - 2 + // - `AUTH_PORT`: default + `instance` * 100 - 100 + // - `DISCOVERY_PORT`: default + `instance` - 1 + assert_eq!(reth.http_port(), DEFAULT_HTTP_PORT - reth.instance() + 1); + assert_eq!(reth.ws_port(), DEFAULT_WS_PORT + reth.instance() * 2 - 2); + assert_eq!(reth.auth_port(), Some(DEFAULT_AUTH_PORT + reth.instance() * 100 - 100)); + assert_eq!( + reth.p2p_port(), + if dev { None } else { Some(DEFAULT_P2P_PORT + reth.instance() - 1) } + ); +} diff --git a/crates/node-bindings/tests/reth.rs b/crates/node-bindings/tests/reth.rs deleted file mode 100644 index 6336e285392..00000000000 --- a/crates/node-bindings/tests/reth.rs +++ /dev/null @@ -1,187 +0,0 @@ -// These tests should use a different datadir for each `reth` instance spawned. - -use alloy_genesis::Genesis; -use alloy_node_bindings::{utils::run_with_tempdir_sync, Reth, RethInstance}; - -/// The default HTTP port for Reth. -const DEFAULT_HTTP_PORT: u16 = 8545; - -/// The default WS port for Reth. -const DEFAULT_WS_PORT: u16 = 8546; - -/// The default auth port for Reth. -const DEFAULT_AUTH_PORT: u16 = 8551; - -/// The default P2P port for Reth. -const DEFAULT_P2P_PORT: u16 = 30303; - -/// Run the given function only if we are in a CI environment. -fn ci_only(f: F) -where - F: FnOnce(), -{ - if ci_info::is_ci() { - f(); - } -} - -#[test] -#[cfg_attr(windows, ignore)] -fn can_launch_reth() { - ci_only(|| { - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new().data_dir(temp_dir_path).spawn(); - - assert_ports(&reth, false); - }); - }); -} - -#[test] -#[cfg_attr(windows, ignore)] -fn can_launch_reth_sepolia() { - ci_only(|| { - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new().chain_or_path("sepolia").data_dir(temp_dir_path).spawn(); - - assert_ports(&reth, false); - }); - }) -} - -#[test] -#[cfg_attr(windows, ignore)] -fn can_launch_reth_dev() { - ci_only(|| { - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new().dev().disable_discovery().data_dir(temp_dir_path).spawn(); - - assert_ports(&reth, true); - }); - }) -} - -#[test] -#[cfg_attr(windows, ignore)] -fn can_launch_reth_dev_custom_genesis() { - ci_only(|| { - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new() - .dev() - .disable_discovery() - .data_dir(temp_dir_path) - .genesis(Genesis::default()) - .spawn(); - - assert_ports(&reth, true); - }); - }) -} - -#[test] -#[cfg_attr(windows, ignore)] -fn can_launch_reth_dev_custom_blocktime() { - ci_only(|| { - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new() - .dev() - .disable_discovery() - .block_time("1sec") - .data_dir(temp_dir_path) - .spawn(); - - assert_ports(&reth, true); - }); - }) -} - -#[test] -#[cfg_attr(windows, ignore)] -fn can_launch_reth_p2p_instances() { - ci_only(|| { - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new().instance(100).data_dir(temp_dir_path).spawn(); - - assert_ports(&reth, false); - - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new().instance(101).data_dir(temp_dir_path).spawn(); - - assert_ports(&reth, false); - }); - }); - }) -} - -// Tests that occupy the same port are combined so they are ran sequentially, to prevent -// flakiness. -#[test] -#[cfg_attr(windows, ignore)] -fn can_launch_reth_custom_ports() { - ci_only(|| { - // Assert that all ports are default if no custom ports are set - // and the instance is set to 0. - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new().instance(0).data_dir(temp_dir_path).spawn(); - - assert_eq!(reth.http_port(), DEFAULT_HTTP_PORT); - assert_eq!(reth.ws_port(), DEFAULT_WS_PORT); - assert_eq!(reth.auth_port(), Some(DEFAULT_AUTH_PORT)); - assert_eq!(reth.p2p_port(), Some(DEFAULT_P2P_PORT)); - }); - - // Assert that only the HTTP port is set and the rest are default. - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new().http_port(8577).data_dir(temp_dir_path).spawn(); - - assert_eq!(reth.http_port(), 8577); - assert_eq!(reth.ws_port(), DEFAULT_WS_PORT); - assert_eq!(reth.auth_port(), Some(DEFAULT_AUTH_PORT)); - assert_eq!(reth.p2p_port(), Some(DEFAULT_P2P_PORT)); - }); - - // Assert that all ports can be set. - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new() - .http_port(8577) - .ws_port(8578) - .auth_port(8579) - .p2p_port(30307) - .data_dir(temp_dir_path) - .spawn(); - - assert_eq!(reth.http_port(), 8577); - assert_eq!(reth.ws_port(), 8578); - assert_eq!(reth.auth_port(), Some(8579)); - assert_eq!(reth.p2p_port(), Some(30307)); - }); - - // Assert that the HTTP port is picked by the OS and the rest are default. - run_with_tempdir_sync("reth-test-", |temp_dir_path| { - let reth = Reth::new().http_port(0).data_dir(temp_dir_path).spawn(); - - // Assert that a random unused port is used picked by the OS. - assert_ne!(reth.http_port(), DEFAULT_HTTP_PORT); - - assert_eq!(reth.ws_port(), DEFAULT_WS_PORT); - assert_eq!(reth.auth_port(), Some(DEFAULT_AUTH_PORT)); - assert_eq!(reth.p2p_port(), Some(DEFAULT_P2P_PORT)); - }); - }) -} - -// Asserts that the ports are set correctly for the given Reth instance. -fn assert_ports(reth: &RethInstance, dev: bool) { - // Changes to the following port numbers for each instance: - // - `HTTP_RPC_PORT`: default - `instance` + 1 - // - `WS_RPC_PORT`: default + `instance` * 2 - 2 - // - `AUTH_PORT`: default + `instance` * 100 - 100 - // - `DISCOVERY_PORT`: default + `instance` - 1 - assert_eq!(reth.http_port(), DEFAULT_HTTP_PORT - reth.instance() + 1); - assert_eq!(reth.ws_port(), DEFAULT_WS_PORT + reth.instance() * 2 - 2); - assert_eq!(reth.auth_port(), Some(DEFAULT_AUTH_PORT + reth.instance() * 100 - 100)); - assert_eq!( - reth.p2p_port(), - if dev { None } else { Some(DEFAULT_P2P_PORT + reth.instance() - 1) } - ); -}