Skip to content

Commit

Permalink
fix: running script with --broadcast for a transaction sequence can…
Browse files Browse the repository at this point in the history
… error out due to nonce desync from rpc latency (foundry-rs#9096)

* fix for issue foundry-rs#9095

* changed 'if' statement into 'match'

* fmt fix

* repeat ask for provider nonce on desync

* loop break and tokio::time use instead of std::thread
  • Loading branch information
pogobounce authored and rplusq committed Nov 29, 2024
1 parent 91bbb93 commit 982f5a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tracing.workspace = true
clap = { version = "4", features = ["derive", "env", "unicode", "wrap_help"] }
semver.workspace = true
futures.workspace = true
tokio.workspace = true
async-recursion = "1.0.5"

itertools.workspace = true
Expand Down
24 changes: 19 additions & 5 deletions crates/script/src/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use foundry_common::{
use foundry_config::Config;
use futures::{future::join_all, StreamExt};
use itertools::Itertools;
use std::sync::Arc;
use std::{cmp::Ordering, sync::Arc};

pub async fn estimate_gas<P, T>(
tx: &mut WithOtherFields<TransactionRequest>,
Expand Down Expand Up @@ -67,11 +67,25 @@ pub async fn send_transaction(
if sequential_broadcast {
let from = tx.from.expect("no sender");

let nonce = provider.get_transaction_count(from).await?;

let tx_nonce = tx.nonce.expect("no nonce");
if nonce != tx_nonce {
bail!("EOA nonce changed unexpectedly while sending transactions. Expected {tx_nonce} got {nonce} from provider.")
for attempt in 0..5 {
let nonce = provider.get_transaction_count(from).await?;
match nonce.cmp(&tx_nonce) {
Ordering::Greater => {
bail!("EOA nonce changed unexpectedly while sending transactions. Expected {tx_nonce} got {nonce} from provider.")
}
Ordering::Less => {
if attempt == 4 {
bail!("After 5 attempts, provider nonce ({nonce}) is still behind expected nonce ({tx_nonce}).")
}
warn!("Expected nonce ({tx_nonce}) is ahead of provider nonce ({nonce}). Retrying in 1 second...");
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
}
Ordering::Equal => {
// Nonces are equal, we can proceed
break;
}
}
}
}

Expand Down

0 comments on commit 982f5a7

Please sign in to comment.