Skip to content

fix: use random port for Anvil instead of hardcoded 61611 - #1

Merged
jacderida merged 2 commits into
mainfrom
fix/anvil-random-port
Mar 27, 2026
Merged

jacderida merged 2 commits into
mainfrom
fix/anvil-random-port

Conversation

@grumbach

@grumbach grumbach commented Mar 27, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Remove hardcoded ANVIL_DEFAULT_PORT = 61611, use OS-assigned random port by default
  • Replace all .expect() in testnet.rs with proper Result propagation via TestnetError
  • Replace all direct keys()[N] indexing with .first()/.get(N) safe access
  • Make start_node(), Testnet::new(), default_wallet_private_key(), and all deploy_* functions return Result
  • Fix test_run_multiple_testnets_in_parallel to use tokio::join! for actual concurrent testing
  • Use inline format variables and top-level imports

Problem

  1. Hardcoded port 61611 causes Address already in use (os error 48) failures in parallel/sequential tests
  2. .expect() calls in non-test production code violate project rules and panic on setup failures
  3. Direct keys()[N] indexing panics on index-out-of-bounds

Breaking changes

  • start_node() now returns Result<(AnvilInstance, Url), TestnetError>
  • Testnet::new() now returns Result<Self, TestnetError>
  • default_wallet_private_key() now returns Result<String, TestnetError>
  • deploy_network_token_contract(), deploy_data_payments_contract(), deploy_merkle_payments_contract() now return Result

Downstream callers (ant-node, ant-client) need to add .unwrap() in test code or propagate errors in production code.

Test results (with this fix)

All tests run with --test-threads=4 (parallel):

Suite Tests Result
ant-node: attack tests (security + merkle) 10 all pass
ant-node: chunk tests 11 all pass
ant-client: e2e_chunk 9 all pass
ant-client: e2e_payment 8 all pass
ant-client: e2e_merkle (35-node testnet) 2 all pass

Remove ANVIL_DEFAULT_PORT constant and let the OS assign a random
available port by default. This fixes port collisions in sequential
and parallel tests where the previous fixed port 61611 would remain
in TIME_WAIT for ~60s after each test, causing 'Address already in
use' errors.

The ANVIL_PORT env var is still respected for callers that need a
specific port.
- Remove hardcoded ANVIL_DEFAULT_PORT, use OS-assigned random port
- Replace all .expect() in start_node() with proper Result propagation
- Replace direct keys()[N] indexing with .first()/.get(N) safe access
- Add TestnetError enum for structured error handling
- Make start_node(), Testnet::new(), default_wallet_private_key(),
  and all deploy_* functions return Result
- Fix test_run_multiple_testnets_in_parallel to use tokio::join!
  for actual concurrent testing
- Use inline format variables: format!("http://{host}:{port}")
- Import std::env at top level instead of inline std::env::var
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants