Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: running script with --broadcast for a transaction sequence can error out due to nonce desync from rpc latency #9096

Merged
merged 11 commits into from
Oct 14, 2024
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
klkvr marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}
}

Expand Down
Loading