Skip to content
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
23 changes: 2 additions & 21 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,30 +1313,11 @@ impl EthApi {

response.reward = Some(rewards);

// calculate next base fee
// add the next block's base fee to the response
// The spec states that `base_fee_per_gas` "[..] includes the next block after the
// newest of the returned range, because this value can be derived from the
// newest block"
if let (Some(last_gas_used), Some(last_fee_per_gas)) =
(response.gas_used_ratio.last(), response.base_fee_per_gas.last())
{
let elasticity = self.backend.elasticity();
let last_fee_per_gas = *last_fee_per_gas as f64;
if last_gas_used > &0.5 {
// increase base gas
let increase = ((last_gas_used - 0.5) * 2f64) * elasticity;
let new_base_fee = (last_fee_per_gas + (last_fee_per_gas * increase)) as u128;
response.base_fee_per_gas.push(new_base_fee);
} else if last_gas_used < &0.5 {
// decrease gas
let increase = ((0.5 - last_gas_used) * 2f64) * elasticity;
let new_base_fee = (last_fee_per_gas - (last_fee_per_gas * increase)) as u128;
response.base_fee_per_gas.push(new_base_fee);
} else {
// same base gas
response.base_fee_per_gas.push(last_fee_per_gas as u128);
}
}
response.base_fee_per_gas.push(self.backend.fees().base_fee());

Ok(response)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,12 +1055,12 @@ impl Backend {
header.base_fee_per_gas.unwrap_or_default(),
);

// notify all listeners
self.notify_on_new_block(header, block_hash);

// update next base fee
self.fees.set_base_fee(next_block_base_fee);

// notify all listeners
self.notify_on_new_block(header, block_hash);

Comment on lines -1058 to +1063
Copy link
Member Author

Choose a reason for hiding this comment

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

this was a subject to race condition anyway, but this just ensures that value in fees is always up to date

Copy link
Member

Choose a reason for hiding this comment

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

right, nice catch

outcome
}

Expand Down
6 changes: 1 addition & 5 deletions crates/anvil/src/eth/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ impl FeeHistoryService {

/// Create a new history entry for the block
fn create_cache_entry(&self, hash: B256) -> (FeeHistoryCacheItem, Option<u64>) {
let elasticity = self.fees.elasticity();
// percentile list from 0.0 to 100.0 with a 0.5 resolution.
// this will create 200 percentile points
let reward_percentiles: Vec<f64> = {
Expand All @@ -199,10 +198,7 @@ impl FeeHistoryService {
block_number = Some(block.header.number);

let gas_used = block.header.gas_used as f64;
let gas_limit = block.header.gas_limit as f64;

let gas_target = gas_limit / elasticity;
item.gas_used_ratio = gas_used / (gas_target * elasticity);
item.gas_used_ratio = gas_used / block.header.gas_limit as f64;

// extract useful tx info (gas_used, effective_reward)
let mut transactions: Vec<(u128, u128)> = receipts
Expand Down
22 changes: 22 additions & 0 deletions crates/anvil/tests/it/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,25 @@ async fn test_tip_above_fee_cap() {
.to_string()
.contains("max priority fee per gas higher than max fee per gas"));
}

#[tokio::test(flavor = "multi_thread")]
async fn test_can_use_fee_history() {
let base_fee = 50u128;
let (_api, handle) = spawn(NodeConfig::test().with_base_fee(Some(base_fee))).await;
let provider = handle.http_provider();

for _ in 0..10 {
let fee_history = provider.get_fee_history(1, Default::default(), &[]).await.unwrap();
let next_base_fee = fee_history.base_fee_per_gas.last().unwrap();

let tx = TransactionRequest::default()
.with_to(Address::random())
.with_value(U256::from(100))
.with_gas_price(*next_base_fee);
let tx = WithOtherFields::new(tx);

let receipt =
provider.send_transaction(tx.clone()).await.unwrap().get_receipt().await.unwrap();
assert!(receipt.inner.inner.is_success());
}
}