Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feat(middleware): add tracing to gas escalator
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed Dec 24, 2020
1 parent 1d87a69 commit 395bfce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions ethers-middleware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ url = { version = "2.1.1", default-features = false }
# optional for runtime
tokio = { version = "0.2.22", optional = true }
async-std = { version = "1.6.5", optional = true }
tracing = "0.1.22"
tracing-futures = "0.2.4"

[dev-dependencies]
ethers = { version = "0.1.3", path = "../ethers" }
Expand Down
30 changes: 23 additions & 7 deletions ethers-middleware/src/gas_escalator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use futures_util::lock::Mutex;
use std::sync::Arc;
use std::{pin::Pin, time::Instant};
use thiserror::Error;
use tracing_futures::Instrument;

#[cfg(all(not(feature = "tokio"), feature = "async-std"))]
use async_std::task::spawn;
Expand Down Expand Up @@ -136,7 +137,11 @@ where
{
let this2 = this.clone();
spawn(async move {
this2.escalate().await.unwrap();
this2
.escalate()
.instrument(tracing::trace_span!("gas-escalation"))
.await
.unwrap();
});
}

Expand Down Expand Up @@ -170,15 +175,16 @@ where
txs.pop().expect("should have element in vector");

let receipt = self.get_transaction_receipt(tx_hash).await?;
tracing::trace!(tx_hash = ?tx_hash, "checking if exists");
if receipt.is_none() {
let old_gas_price = replacement_tx.gas_price.expect("gas price must be set");
// Get the new gas price based on how much time passed since the
// tx was last broadcast
let new_gas_price = self.escalator.get_gas_price(
replacement_tx.gas_price.expect("gas price must be set"),
now.duration_since(time).as_secs(),
);
let new_gas_price = self
.escalator
.get_gas_price(old_gas_price, now.duration_since(time).as_secs());

let new_txhash = if Some(new_gas_price) != replacement_tx.gas_price {
let new_txhash = if new_gas_price != old_gas_price {
// bump the gas price
replacement_tx.gas_price = Some(new_gas_price);

Expand All @@ -188,7 +194,17 @@ where
.send_transaction(replacement_tx.clone(), priority)
.await
{
Ok(tx_hash) => *tx_hash,
Ok(new_tx_hash) => {
let new_tx_hash = *new_tx_hash;
tracing::trace!(
old_tx_hash = ?tx_hash,
new_tx_hash = ?new_tx_hash,
old_gas_price = ?old_gas_price,
new_gas_price = ?new_gas_price,
"escalated"
);
new_tx_hash
}
Err(err) => {
if err.to_string().contains("nonce too low") {
// ignore "nonce too low" errors because they
Expand Down

0 comments on commit 395bfce

Please sign in to comment.