Skip to content
Merged
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
18 changes: 14 additions & 4 deletions crates/common/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,18 @@ pub fn calculate_base_fee_per_blob_gas(parent_excess_blob_gas: u64, update_fract
// Defined in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844)
pub fn fake_exponential(factor: u64, numerator: u64, denominator: u64) -> u64 {
let mut i = 1;
let mut output = 0;
let mut numerator_accum = factor * denominator;
while numerator_accum > 0 {
let mut output = U256::zero();
let mut numerator_accum = U256::from(factor) * denominator;
while !numerator_accum.is_zero() {
output += numerator_accum;
numerator_accum = numerator_accum * numerator / (denominator * i);
i += 1;
}
output / denominator
if (output / denominator) > U256::from(u64::MAX) {
u64::MAX
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should panic here, since I'm sure this is unreachable. In a later PR we can update this with an explanation.

} else {
(output / denominator).as_u64()
}
}

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -983,4 +987,10 @@ mod test {
let res = calc_excess_blob_gas(&parent, schedule, fork);
assert_eq!(res, 3538944)
}

#[test]
fn test_fake_exponential_overflow() {
// With u64 this overflows
fake_exponential(57532635, 3145728, 3338477);
}
Comment on lines +991 to +995
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add a test with the maximum expected inputs here, for good measure:

fake_exponential(MIN_BASE_FEE_PER_BLOB_GAS, u64::MAX, BLOB_BASE_FEE_UPDATE_FRACTION);

Copy link
Collaborator

Choose a reason for hiding this comment

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

This test panics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created issue to define a bound #5096

}