From e39e11564371a11229c8d2fa7699eed7ce9f0b18 Mon Sep 17 00:00:00 2001 From: pogo Date: Fri, 11 Oct 2024 15:06:57 +0300 Subject: [PATCH 1/5] fix for issue #9095 --- crates/script/src/broadcast.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/script/src/broadcast.rs b/crates/script/src/broadcast.rs index 3faaa441e24c..3d0d4dfda6ff 100644 --- a/crates/script/src/broadcast.rs +++ b/crates/script/src/broadcast.rs @@ -70,8 +70,10 @@ pub async fn send_transaction( let nonce = provider.get_transaction_count(from).await?; let tx_nonce = tx.nonce.expect("no nonce"); - if nonce != tx_nonce { + if nonce > tx_nonce { bail!("EOA nonce changed unexpectedly while sending transactions. Expected {tx_nonce} got {nonce} from provider.") + } else if nonce < tx_nonce { + warn!("Expected nonce ({tx_nonce}) is ahead of provider nonce ({nonce}). Proceeding with expected."); } } From 59235e114080c2c8a73e4549390f943ec2a30206 Mon Sep 17 00:00:00 2001 From: pogo Date: Fri, 11 Oct 2024 17:10:22 +0300 Subject: [PATCH 2/5] changed 'if' statement into 'match' --- crates/script/src/broadcast.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/script/src/broadcast.rs b/crates/script/src/broadcast.rs index 3d0d4dfda6ff..1799544510ff 100644 --- a/crates/script/src/broadcast.rs +++ b/crates/script/src/broadcast.rs @@ -27,6 +27,7 @@ use foundry_config::Config; use futures::{future::join_all, StreamExt}; use itertools::Itertools; use std::sync::Arc; +use std::cmp::Ordering; pub async fn estimate_gas( tx: &mut WithOtherFields, @@ -70,10 +71,17 @@ pub async fn send_transaction( 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.") - } else if nonce < tx_nonce { - warn!("Expected nonce ({tx_nonce}) is ahead of provider nonce ({nonce}). Proceeding with expected."); + + match nonce.cmp(&tx_nonce) { + Ordering::Greater => { + bail!("EOA nonce changed unexpectedly while sending transactions. Expected {tx_nonce} got {nonce} from provider.") + } + Ordering::Less => { + warn!("Expected nonce ({tx_nonce}) is ahead of provider nonce ({nonce}). Proceeding with expected."); + } + Ordering::Equal => { + // Nonces are equal, no action needed + } } } From 4ae05d6b26d368c4d8530810309d6fa371f5af36 Mon Sep 17 00:00:00 2001 From: pogo Date: Sat, 12 Oct 2024 09:42:45 +0300 Subject: [PATCH 3/5] fmt fix --- crates/script/src/broadcast.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/script/src/broadcast.rs b/crates/script/src/broadcast.rs index 1799544510ff..4087f3a2e087 100644 --- a/crates/script/src/broadcast.rs +++ b/crates/script/src/broadcast.rs @@ -26,8 +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; +use std::{cmp::Ordering, sync::Arc}; pub async fn estimate_gas( tx: &mut WithOtherFields, From 2ed1993a31651ca6687cf8369760e6320ccbc814 Mon Sep 17 00:00:00 2001 From: pogo Date: Sun, 13 Oct 2024 14:10:12 +0300 Subject: [PATCH 4/5] repeat ask for provider nonce on desync --- crates/script/src/broadcast.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/crates/script/src/broadcast.rs b/crates/script/src/broadcast.rs index 4087f3a2e087..ad15782d3e2f 100644 --- a/crates/script/src/broadcast.rs +++ b/crates/script/src/broadcast.rs @@ -67,19 +67,23 @@ 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"); - - match nonce.cmp(&tx_nonce) { - Ordering::Greater => { - bail!("EOA nonce changed unexpectedly while sending transactions. Expected {tx_nonce} got {nonce} from provider.") - } - Ordering::Less => { - warn!("Expected nonce ({tx_nonce}) is ahead of provider nonce ({nonce}). Proceeding with expected."); - } - Ordering::Equal => { - // Nonces are equal, no action needed + 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..."); + std::thread::sleep(std::time::Duration::from_millis(1000)); + } + Ordering::Equal => { + // Nonces are equal, we can proceed + } } } } From b7ce1517921e9f8827073b7510d51ebd3bca440d Mon Sep 17 00:00:00 2001 From: pogo Date: Mon, 14 Oct 2024 10:33:32 +0300 Subject: [PATCH 5/5] loop break and tokio::time use instead of std::thread --- Cargo.lock | 1 + crates/script/Cargo.toml | 1 + crates/script/src/broadcast.rs | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index e597a7a91fd0..257f63e31f63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3497,6 +3497,7 @@ dependencies = [ "serde", "serde_json", "tempfile", + "tokio", "tracing", "yansi", ] diff --git a/crates/script/Cargo.toml b/crates/script/Cargo.toml index 9c042661775c..37cc13741a7b 100644 --- a/crates/script/Cargo.toml +++ b/crates/script/Cargo.toml @@ -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 diff --git a/crates/script/src/broadcast.rs b/crates/script/src/broadcast.rs index ad15782d3e2f..2adfaaf7c541 100644 --- a/crates/script/src/broadcast.rs +++ b/crates/script/src/broadcast.rs @@ -79,10 +79,11 @@ pub async fn send_transaction( 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..."); - std::thread::sleep(std::time::Duration::from_millis(1000)); + tokio::time::sleep(std::time::Duration::from_millis(1000)).await; } Ordering::Equal => { // Nonces are equal, we can proceed + break; } } }