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

feat(providers): add tracing #113

Merged
merged 3 commits into from
Dec 24, 2020
Merged
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
16 changes: 16 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions ethers-core/src/types/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{
use std::{collections::BTreeMap, fmt, str::FromStr};

/// Transaction summary as found in the Txpool Inspection property.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TxpoolInspectSummary {
/// Recipient (None when contract creation)
pub to: Option<Address>,
Expand Down Expand Up @@ -122,7 +122,7 @@ pub struct TxpoolContent {
///
/// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_inspect) for more details
///
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct TxpoolInspect {
/// pending tx
pub pending: BTreeMap<Address, BTreeMap<String, TxpoolInspectSummary>>,
Expand Down
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
32 changes: 25 additions & 7 deletions ethers-middleware/src/gas_escalator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use std::sync::Arc;
use std::{pin::Pin, time::Instant};
use thiserror::Error;

#[cfg(any(feature = "async-std", feature = "tokio"))]
use tracing_futures::Instrument;

#[cfg(all(not(feature = "tokio"), feature = "async-std"))]
use async_std::task::spawn;
#[cfg(all(feature = "tokio", not(feature = "async-std")))]
Expand Down Expand Up @@ -136,7 +139,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 +177,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 +196,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
2 changes: 2 additions & 0 deletions ethers-providers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ async-tls = { version = "0.7.0", optional = true }
# needed for parsing while deserialization in gas oracles
serde-aux = "0.6.1"
auto_impl = "0.4.1"
tracing = "0.1.22"
tracing-futures = "0.2.4"

[dev-dependencies]
ethers = { version = "0.1.3", path = "../ethers" }
Expand Down
4 changes: 2 additions & 2 deletions ethers-providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub trait JsonRpcClient: Debug + Send + Sync {
async fn request<T, R>(&self, method: &str, params: T) -> Result<R, Self::Error>
where
T: Debug + Serialize + Send + Sync,
R: DeserializeOwned;
R: Serialize + DeserializeOwned;
}

use ethers_core::types::*;
Expand Down Expand Up @@ -333,7 +333,7 @@ pub trait Middleware: Sync + Send + Debug {
async fn get_filter_changes<T, R>(&self, id: T) -> Result<Vec<R>, Self::Error>
where
T: Into<U256> + Send + Sync,
R: DeserializeOwned + Send + Sync,
R: Serialize + DeserializeOwned + Send + Sync + Debug,
{
self.inner()
.get_filter_changes(id)
Expand Down
3 changes: 2 additions & 1 deletion ethers-providers/src/pending_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ impl<'a, P: JsonRpcClient> Future for PendingTransaction<'a, P> {

// if the transaction has at least K confirmations, return the receipt
// (subtract 1 since the tx already has 1 conf when it's mined)
if current_block >= inclusion_block + *this.confirmations - 1 {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was an off by 1 here

if current_block > inclusion_block + *this.confirmations - 1 {
let receipt = *receipt.clone();
*this.state = PendingTxState::Completed;
return Poll::Ready(Ok(receipt));
} else {
tracing::trace!(tx_hash = ?this.tx_hash, "confirmations {}/{}", current_block - inclusion_block + 1, this.confirmations);
*this.state = PendingTxState::PausedGettingBlockNumber(receipt.clone());
ctx.waker().wake_by_ref();
}
Expand Down
Loading