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

feat(builder): add bundle gas stats to metrics #445

Merged
merged 2 commits into from
Oct 11, 2023
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
11 changes: 11 additions & 0 deletions crates/builder/src/bundle_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,12 @@ where
tx_hash,
block_number,
attempt_number,
gas_limit,
gas_used,
..
} => {
BuilderMetrics::increment_bundle_txns_success(self.builder_index);
BuilderMetrics::set_bundle_gas_stats(gas_limit, gas_used);
if attempt_number == 0 {
info!("Bundle with hash {tx_hash:?} landed in block {block_number}");
} else {
Expand Down Expand Up @@ -375,6 +378,8 @@ where
nonce,
block_number,
attempt_number,
gas_limit,
gas_used,
} => {
self.emit(BuilderEvent::transaction_mined(
self.builder_index,
Expand All @@ -383,6 +388,7 @@ where
block_number,
));
BuilderMetrics::increment_bundle_txns_success(self.builder_index);
BuilderMetrics::set_bundle_gas_stats(gas_limit, gas_used);
return Ok(SendBundleResult::Success {
block_number,
attempt_number,
Expand Down Expand Up @@ -545,6 +551,11 @@ impl BuilderMetrics {
metrics::increment_counter!("builder_bundle_fee_increases", "builder_index" => builder_index.to_string());
}

fn set_bundle_gas_stats(gas_limit: U256, gas_used: U256) {
metrics::counter!("builder_bundle_gas_limit", gas_limit.as_u64());
metrics::counter!("builder_bundle_gas_used", gas_used.as_u64());
}

fn set_current_fees(fees: &GasFees) {
metrics::gauge!(
"builder_current_max_fee",
Expand Down
34 changes: 33 additions & 1 deletion crates/builder/src/transaction_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub(crate) enum TrackerUpdate {
nonce: U256,
block_number: u64,
attempt_number: u64,
gas_limit: U256,
gas_used: U256,
},
StillPendingAfterWait,
LatestTxDropped {
Expand Down Expand Up @@ -288,11 +290,14 @@ where
.await
.context("tracker should check transaction status when the nonce changes")?;
if let TxStatus::Mined { block_number } = status {
let (gas_limit, gas_used) = self.get_mined_tx_gas_info(tx.tx_hash).await?;
out = TrackerUpdate::Mined {
tx_hash: tx.tx_hash,
nonce: self.nonce,
block_number,
attempt_number: tx.attempt_number,
gas_limit,
gas_used,
};
break;
}
Expand Down Expand Up @@ -321,11 +326,14 @@ where
TxStatus::Mined { block_number } => {
let nonce = self.nonce;
self.set_nonce_and_clear_state(nonce + 1);
let (gas_limit, gas_used) = self.get_mined_tx_gas_info(last_tx.tx_hash).await?;
Some(TrackerUpdate::Mined {
tx_hash: last_tx.tx_hash,
nonce,
block_number,
attempt_number: last_tx.attempt_number,
gas_limit,
gas_used,
})
}
TxStatus::Dropped => {
Expand Down Expand Up @@ -379,6 +387,14 @@ where
TransactionTrackerMetrics::set_current_fees(None);
}
}

async fn get_mined_tx_gas_info(&self, tx_hash: H256) -> anyhow::Result<(U256, U256)> {
let (tx, tx_receipt) = tokio::try_join!(
self.provider.get_transaction(tx_hash),
dancoombs marked this conversation as resolved.
Show resolved Hide resolved
self.provider.get_transaction_receipt(tx_hash),
)?;
Ok((tx.unwrap().gas, tx_receipt.unwrap().gas_used.unwrap()))
}
}

struct TransactionTrackerMetrics {}
Expand Down Expand Up @@ -417,7 +433,7 @@ impl TransactionTrackerMetrics {
mod tests {
use std::sync::Arc;

use ethers::types::{Address, Eip1559TransactionRequest};
use ethers::types::{Address, Eip1559TransactionRequest, Transaction, TransactionReceipt};
use mockall::Sequence;
use rundler_provider::MockProvider;

Expand Down Expand Up @@ -733,6 +749,22 @@ mod tests {
.returning(move || Ok(1))
.times(1);

provider.expect_get_transaction().returning(|_: H256| {
Ok(Some(Transaction {
gas: U256::from(0),
..Default::default()
}))
});

provider
.expect_get_transaction_receipt()
.returning(|_: H256| {
Ok(Some(TransactionReceipt {
gas_used: Some(U256::from(0)),
..Default::default()
}))
});

let tracker = create_tracker(sender, provider).await;

let tx = Eip1559TransactionRequest::new().nonce(0);
Expand Down
Loading