Skip to content

Commit

Permalink
fix: u64 overflow when updating relayer metrics (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekohex authored Apr 19, 2023
1 parent bdf6954 commit ac34994
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions crates/relayer-utils/src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use webb_proposals::ResourceId;
/// A struct for collecting metrics for particular resource.
#[derive(Debug, Clone)]
pub struct ResourceMetric {
/// Total gas spent on Resource.
/// Total gas spent (in gwei) on Resource.
pub total_gas_spent: GenericCounter<AtomicF64>,
/// Total fees earned on Resource.
pub total_fee_earned: GenericCounter<AtomicF64>,
Expand Down Expand Up @@ -174,7 +174,7 @@ impl Metrics {
let total_gas_spent_counter = register_counter!(
format!("resource_{resource_hex}_total_gas_spent"),
format!(
"The total number of gas spent on resource : {resource_hex}"
"The total number of gas (in gwei) spent on resource : {resource_hex}"
)
);
// Total fee earned on particular resource.
Expand Down
27 changes: 16 additions & 11 deletions crates/tx-relay/src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{sync::Arc, time::Duration};

use tokio::sync::Mutex;
use webb::evm::ethers::{
self,
abi::Detokenize,
prelude::{builders::ContractCall, Middleware},
};
Expand Down Expand Up @@ -95,22 +96,26 @@ where
finalized = true,
tx_hash = %receipt.transaction_hash,
);
// send the finalized status to the client.
let _ = stream
.send(Withdraw(WithdrawStatus::Finalized {
tx_hash: receipt.transaction_hash,
}))
.await;
// gas spent by relayer on particular resource.
let gas_price = receipt.gas_used.unwrap_or_default();
let gas_used = receipt.gas_used.unwrap_or_default();
let mut metrics = metrics.lock().await;
let resource_metric = metrics
.resource_metric_map
.entry(resource_id)
.or_insert_with(|| Metrics::register_resource_id_counters(resource_id));

resource_metric
.total_gas_spent
.inc_by(gas_price.as_u64() as f64);
drop(metrics);
let _ = stream
.send(Withdraw(WithdrawStatus::Finalized {
tx_hash: receipt.transaction_hash,
}))
.await;
// convert gas used to gwei.
let gas_used = ethers::utils::format_units(gas_used, "gwei")
.and_then(|gas| {
gas.parse::<f64>()
.map_err(|_| ethers::utils::ConversionError::ParseOverflow)
})
.unwrap_or_default();
resource_metric.total_gas_spent.inc_by(gas_used);
Ok(())
}

0 comments on commit ac34994

Please sign in to comment.