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

escalator drop test #28

Open
wants to merge 4 commits into
base: hyperlane
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions ethers-middleware/src/gas_escalator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use ethers_providers::{interval, FromErr, Middleware, PendingTransaction, Stream
#[cfg(not(target_arch = "wasm32"))]
use tokio::spawn;

const RANDOM_TX_DROP_TRESHOLD: u64 = 3;

pub type ToEscalate = Arc<Mutex<Vec<MonitoredTransaction>>>;

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -57,6 +59,8 @@ pub(crate) struct GasEscalatorMiddlewareInternal<M> {
/// The transactions which are currently being monitored for escalation
#[allow(clippy::type_complexity)]
pub txs: ToEscalate,
// need an arc-mutex for interior mutability in `send_transaction`
random_tx_drop_counter: Arc<Mutex<u64>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -204,6 +208,15 @@ where
block: Option<BlockId>,
) -> Result<PendingTransaction<'_, M::Provider>, GasEscalatorError<M>> {
let tx = tx.into();
let random_tx_drop_counter = {
let mut lock = self.random_tx_drop_counter.lock().await;
*lock += 1;
*lock
};
if random_tx_drop_counter % RANDOM_TX_DROP_TRESHOLD == 0 {
tracing::warn!(tx = ?tx, "Dropping random transaction");
return Ok(PendingTransaction::new(TxHash::random(), self.inner.provider()));
}

match self.inner.send_transaction(tx.clone(), block).await {
Ok(pending_tx) => {
Expand Down Expand Up @@ -250,8 +263,11 @@ where

let txs: ToEscalate = Default::default();

let this =
Arc::new(GasEscalatorMiddlewareInternal { inner: inner.clone(), txs: txs.clone() });
let this = Arc::new(GasEscalatorMiddlewareInternal {
inner: inner.clone(),
txs: txs.clone(),
random_tx_drop_counter: Arc::new(Mutex::new(0)),
});

let esc = EscalationTask { inner, escalator, frequency, txs };

Expand Down
2 changes: 1 addition & 1 deletion ethers-middleware/src/nonce_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use thiserror::Error;
use tracing::instrument;

const DEFAULT_TX_COUNT_FOR_RESYNC: u64 = 10;
const DEFAULT_TX_COUNT_FOR_RESYNC: u64 = 7;

#[derive(Debug)]
/// Middleware used for calculating nonces locally, useful for signing multiple
Expand Down
Loading