Skip to content

Commit

Permalink
better coinbase diff calc (#836)
Browse files Browse the repository at this point in the history
* better coinbase diff calc

* lint
  • Loading branch information
eyusufatik authored Jul 1, 2024
1 parent d06849d commit 621d59c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
29 changes: 8 additions & 21 deletions crates/evm/src/evm/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ fn calc_diff_size<EXT, DB: Database>(
let InnerEvmContext {
db,
journaled_state,
env,
..
} = &mut context.evm.inner;

Expand Down Expand Up @@ -449,6 +450,13 @@ fn calc_diff_size<EXT, DB: Database>(
let slot_size = 2 * size_of::<U256>(); // key + value;
let mut diff_size = 0usize;

// no matter the type of transaction or its fee rates, a tx must pay at least base fee and L1 fee
// thus we increment the diff size by 20 (coinbase address) + 32 (coinbase balance change)
// notice, we don't add to diff size when an address explicitly sends funds to coinbase
if !account_changes.contains_key(&env.block.coinbase) {
diff_size += size_of::<Address>() + size_of::<U256>();
}

for (addr, account) in account_changes {
// Apply size of address of changed account
diff_size += size_of::<Address>();
Expand Down Expand Up @@ -500,27 +508,6 @@ fn calc_diff_size<EXT, DB: Database>(
}
}

// The diff size of balance change originating from priority fee is not included if priority fee is zero or None
// However l1 fee will be applied in any case thus balance change diff size must be applied
match context.evm.env.tx.gas_priority_fee {
Some(U256::ZERO) => {
// EIP 1559 enabled transaction, priority fee is zero, include the diff size of balance change for l1 fee
diff_size += size_of::<U256>();
// Include the diff size of coinbase address for l1 fee
diff_size += size_of::<Address>();
}
None => {
// If priority fee is None, meaning it is a legacy transaction
// Check if effective gas price is zero, if so include the diff size of balance change for l1 fee
if context.evm.env.effective_gas_price() == U256::ZERO {
diff_size += size_of::<U256>();
// Include the diff size of coinbase address for l1 fee
diff_size += size_of::<Address>();
}
}
_ => {}
}

Ok(diff_size)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/tests/queries/estimate_gas_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn test_tx_request_fields_gas() {
);
assert_eq!(
contract_diff_size.unwrap(),
serde_json::from_value::<EstimatedDiffSize>(json![{"gas":"0x6601","diffSize":"0xa8"}])
serde_json::from_value::<EstimatedDiffSize>(json![{"gas":"0x6601","diffSize":"0xdc"}])
.unwrap()
);

Expand Down

0 comments on commit 621d59c

Please sign in to comment.