Skip to content

Commit 982f5a7

Browse files
pogobouncerplusq
authored andcommitted
fix: running script with --broadcast for a transaction sequence can 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
1 parent 91bbb93 commit 982f5a7

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/script/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tracing.workspace = true
3333
clap = { version = "4", features = ["derive", "env", "unicode", "wrap_help"] }
3434
semver.workspace = true
3535
futures.workspace = true
36+
tokio.workspace = true
3637
async-recursion = "1.0.5"
3738

3839
itertools.workspace = true

crates/script/src/broadcast.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use foundry_common::{
2626
use foundry_config::Config;
2727
use futures::{future::join_all, StreamExt};
2828
use itertools::Itertools;
29-
use std::sync::Arc;
29+
use std::{cmp::Ordering, sync::Arc};
3030

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

70-
let nonce = provider.get_transaction_count(from).await?;
71-
7270
let tx_nonce = tx.nonce.expect("no nonce");
73-
if nonce != tx_nonce {
74-
bail!("EOA nonce changed unexpectedly while sending transactions. Expected {tx_nonce} got {nonce} from provider.")
71+
for attempt in 0..5 {
72+
let nonce = provider.get_transaction_count(from).await?;
73+
match nonce.cmp(&tx_nonce) {
74+
Ordering::Greater => {
75+
bail!("EOA nonce changed unexpectedly while sending transactions. Expected {tx_nonce} got {nonce} from provider.")
76+
}
77+
Ordering::Less => {
78+
if attempt == 4 {
79+
bail!("After 5 attempts, provider nonce ({nonce}) is still behind expected nonce ({tx_nonce}).")
80+
}
81+
warn!("Expected nonce ({tx_nonce}) is ahead of provider nonce ({nonce}). Retrying in 1 second...");
82+
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
83+
}
84+
Ordering::Equal => {
85+
// Nonces are equal, we can proceed
86+
break;
87+
}
88+
}
7589
}
7690
}
7791

0 commit comments

Comments
 (0)