Skip to content

Commit 3a38e02

Browse files
authored
refactor(anvil): simplify effective_reward calculation in FeeHistoryService (#12640)
refactor(anvil): simplify `effective_reward` calculation in `FeeHistoryService::create_cache_entry` - cleanup useless casts - more elegant default rewards array
1 parent 236576f commit 3a38e02

File tree

1 file changed

+10
-30
lines changed

1 file changed

+10
-30
lines changed

crates/anvil/src/eth/fees.rs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ use std::{
66
task::{Context, Poll},
77
};
88

9-
use alloy_consensus::Header;
9+
use alloy_consensus::{Header, Transaction};
1010
use alloy_eips::{
1111
calc_next_block_base_fee, eip1559::BaseFeeParams, eip7691::MAX_BLOBS_PER_BLOCK_ELECTRA,
1212
eip7840::BlobParams,
1313
};
1414
use alloy_primitives::B256;
15-
use anvil_core::eth::transaction::TypedTransaction;
1615
use futures::StreamExt;
1716
use parking_lot::{Mutex, RwLock};
1817
use revm::{context_interface::block::BlobExcessGasAndPrice, primitives::hardfork::SpecId};
@@ -252,13 +251,13 @@ impl FeeHistoryService {
252251
};
253252

254253
let mut block_number: Option<u64> = None;
255-
let base_fee = header.base_fee_per_gas.map(|g| g as u128).unwrap_or_default();
254+
let base_fee = header.base_fee_per_gas.unwrap_or_default();
256255
let excess_blob_gas = header.excess_blob_gas.map(|g| g as u128);
257256
let blob_gas_used = header.blob_gas_used.map(|g| g as u128);
258257
let base_fee_per_blob_gas = header.blob_fee(self.blob_params);
259258

260259
let mut item = FeeHistoryCacheItem {
261-
base_fee,
260+
base_fee: base_fee as u128,
262261
gas_used_ratio: 0f64,
263262
blob_gas_used_ratio: 0f64,
264263
rewards: Vec::new(),
@@ -285,31 +284,12 @@ impl FeeHistoryService {
285284
.enumerate()
286285
.map(|(i, receipt)| {
287286
let gas_used = receipt.cumulative_gas_used();
288-
let effective_reward =
289-
match block.body.transactions.get(i).map(|tx| &tx.transaction) {
290-
Some(TypedTransaction::Legacy(t)) => {
291-
t.tx().gas_price.saturating_sub(base_fee)
292-
}
293-
Some(TypedTransaction::EIP2930(t)) => {
294-
t.tx().gas_price.saturating_sub(base_fee)
295-
}
296-
Some(TypedTransaction::EIP1559(t)) => t
297-
.tx()
298-
.max_priority_fee_per_gas
299-
.min(t.tx().max_fee_per_gas.saturating_sub(base_fee)),
300-
// TODO: This probably needs to be extended to extract 4844 info.
301-
Some(TypedTransaction::EIP4844(t)) => t
302-
.tx()
303-
.tx()
304-
.max_priority_fee_per_gas
305-
.min(t.tx().tx().max_fee_per_gas.saturating_sub(base_fee)),
306-
Some(TypedTransaction::EIP7702(t)) => t
307-
.tx()
308-
.max_priority_fee_per_gas
309-
.min(t.tx().max_fee_per_gas.saturating_sub(base_fee)),
310-
Some(TypedTransaction::Deposit(_)) => 0,
311-
None => 0,
312-
};
287+
let effective_reward = block
288+
.body
289+
.transactions
290+
.get(i)
291+
.map(|tx| tx.as_ref().effective_tip_per_gas(base_fee).unwrap_or(0))
292+
.unwrap_or(0);
313293

314294
(gas_used, effective_reward)
315295
})
@@ -334,7 +314,7 @@ impl FeeHistoryService {
334314
})
335315
.collect();
336316
} else {
337-
item.rewards = reward_percentiles.iter().map(|_| 0).collect();
317+
item.rewards = vec![0; reward_percentiles.len()];
338318
}
339319
(item, block_number)
340320
}

0 commit comments

Comments
 (0)