Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use u128 for calc_blob_gasprice #764

Merged
merged 2 commits into from
Oct 2, 2023
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
4 changes: 2 additions & 2 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct BlockEnv {
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlobExcessGasAndPrice {
pub excess_blob_gas: u64,
pub blob_gasprice: u64,
pub blob_gasprice: u128,
}

impl BlobExcessGasAndPrice {
Expand Down Expand Up @@ -106,7 +106,7 @@ impl BlockEnv {
///
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
#[inline]
pub fn get_blob_gasprice(&self) -> Option<u64> {
pub fn get_blob_gasprice(&self) -> Option<u128> {
self.blob_excess_gas_and_price
.as_ref()
.map(|a| a.blob_gasprice)
Expand Down
25 changes: 20 additions & 5 deletions crates/primitives/src/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn calc_excess_blob_gas(parent_excess_blob_gas: u64, parent_blob_gas_used: u
///
/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers).
#[inline]
pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u64 {
pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u128 {
fake_exponential(
MIN_BLOB_GASPRICE,
excess_blob_gas,
Expand All @@ -63,7 +63,7 @@ pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u64 {
///
/// Panics if `denominator` is zero.
#[inline]
pub fn fake_exponential(factor: u64, numerator: u64, denominator: u64) -> u64 {
pub fn fake_exponential(factor: u64, numerator: u64, denominator: u64) -> u128 {
assert_ne!(denominator, 0, "attempt to divide by zero");
let factor = factor as u128;
let numerator = numerator as u128;
Expand All @@ -79,7 +79,7 @@ pub fn fake_exponential(factor: u64, numerator: u64, denominator: u64) -> u64 {
numerator_accum = (numerator_accum * numerator) / (denominator * i);
i += 1;
}
(output / denominator) as u64
output / denominator
}

/// Serde functions to serde as [bytes::Bytes] hex string
Expand Down Expand Up @@ -173,7 +173,22 @@ mod tests {
// https://github.com/ethereum/go-ethereum/blob/28857080d732857030eda80c69b9ba2c8926f221/consensus/misc/eip4844/eip4844_test.go#L60
#[test]
fn test_calc_blob_fee() {
for &(excess, expected) in &[(0, 1), (2314057, 1), (2314058, 2), (10 * 1024 * 1024, 23)] {
let blob_fee_vectors = &[
(0, 1),
(2314057, 1),
(2314058, 2),
(10 * 1024 * 1024, 23),
// calc_blob_gasprice approximates `e ** (excess_blob_gas / BLOB_GASPRICE_UPDATE_FRACTION)` using Taylor expansion
//
// to roughly find where boundaries will be hit:
// 2 ** bits = e ** (excess_blob_gas / BLOB_GASPRICE_UPDATE_FRACTION)
// excess_blob_gas = ln(2 ** bits) * BLOB_GASPRICE_UPDATE_FRACTION
(148099578, 18446739238971471609), // output is just below the overflow
(148099579, 18446744762204311910), // output is just after the overflow
(161087488, 902580055246494526580),
];

for &(excess, expected) in blob_fee_vectors {
let actual = calc_blob_gasprice(excess);
assert_eq!(actual, expected, "test: {excess}");
}
Expand All @@ -183,7 +198,7 @@ mod tests {
#[test]
fn fake_exp() {
for t @ &(factor, numerator, denominator, expected) in &[
(1u64, 0u64, 1u64, 1u64),
(1u64, 0u64, 1u64, 1u128),
(38493, 0, 1000, 38493),
(0, 1234, 2345, 0),
(1, 2, 1, 6), // approximate 7.389
Expand Down