Skip to content

Commit

Permalink
test: add missing unit test for op calc_next_block_base_fee (alloy-…
Browse files Browse the repository at this point in the history
…rs#1008)

* test: add missing unit test for op calc_next_block_base_fee

* complete test suite

* add base test
  • Loading branch information
tcoratger authored and ben186 committed Jul 27, 2024
1 parent 598a71d commit 62a30f2
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
33 changes: 32 additions & 1 deletion crates/eips/src/eip1559/basefee.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use crate::{
calc_next_block_base_fee,
eip1559::constants::{DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR, DEFAULT_ELASTICITY_MULTIPLIER},
eip1559::constants::{
BASE_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR, DEFAULT_ELASTICITY_MULTIPLIER,
OP_MAINNET_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR,
OP_MAINNET_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
OP_SEPOLIA_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR,
OP_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
},
};

/// BaseFeeParams contains the config parameters that control block base fee computation
Expand Down Expand Up @@ -29,6 +36,30 @@ impl BaseFeeParams {
}
}

/// Get the base fee parameters for Optimism Mainnet
pub const fn optimism() -> Self {
Self {
max_change_denominator: OP_MAINNET_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR,
elasticity_multiplier: OP_MAINNET_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
}
}

/// Get the base fee parameters for Optimism Sepolia
pub const fn optimism_sepolia() -> Self {
Self {
max_change_denominator: OP_SEPOLIA_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR,
elasticity_multiplier: OP_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
}
}

/// Get the base fee parameters for Base Sepolia
pub const fn base_sepolia() -> Self {
Self {
max_change_denominator: OP_SEPOLIA_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR,
elasticity_multiplier: BASE_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
}
}

/// Calculate the base fee for the next block based on the EIP-1559 specification.
///
/// See also [calc_next_block_base_fee]
Expand Down
20 changes: 20 additions & 0 deletions crates/eips/src/eip1559/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ pub const DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR: u64 = 8;

/// Elasticity multiplier as defined in [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)
pub const DEFAULT_ELASTICITY_MULTIPLIER: u64 = 2;

/// Base fee max change denominator for Optimism Sepolia as defined in the Optimism
/// [transaction costs](https://community.optimism.io/docs/developers/build/differences/#transaction-costs) doc.
pub(crate) const OP_SEPOLIA_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR: u128 = 50;

/// Base fee max change denominator for Optimism Sepolia as defined in the Optimism
/// [transaction costs](https://community.optimism.io/docs/developers/build/differences/#transaction-costs) doc.
pub(crate) const OP_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER: u128 = 6;

/// Base fee max change denominator for Optimism Mainnet as defined in the Optimism
/// [transaction costs](https://community.optimism.io/docs/developers/build/differences/#transaction-costs) doc.
pub(crate) const OP_MAINNET_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR: u128 = 50;

/// Base fee max change denominator for Optimism Mainnet as defined in the Optimism
/// [transaction costs](https://community.optimism.io/docs/developers/build/differences/#transaction-costs) doc.
pub(crate) const OP_MAINNET_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER: u128 = 6;

/// Base fee max change denominator for Base Sepolia as defined in the Optimism
/// [transaction costs](https://community.optimism.io/docs/developers/build/differences/#transaction-costs) doc.
pub(crate) const BASE_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER: u128 = 10;
96 changes: 96 additions & 0 deletions crates/eips/src/eip1559/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,100 @@ mod tests {
);
}
}

#[test]
fn calculate_optimism_sepolia_base_fee_success() {
let base_fee = [
1000000000, 1000000000, 1000000000, 1072671875, 1059263476, 1049238967, 1049238967, 0,
1, 2,
];
let gas_used = [
10000000, 10000000, 10000000, 9000000, 10001000, 0, 10000000, 10000000, 10000000,
10000000,
];
let gas_limit = [
10000000, 12000000, 14000000, 10000000, 14000000, 2000000, 18000000, 18000000,
18000000, 18000000,
];
let next_base_fee = [
1100000048, 1080000000, 1065714297, 1167067046, 1128881311, 1028254188, 1098203452, 1,
2, 3,
];

for i in 0..base_fee.len() {
assert_eq!(
next_base_fee[i],
calc_next_block_base_fee(
gas_used[i] as u128,
gas_limit[i] as u128,
base_fee[i] as u128,
BaseFeeParams::optimism_sepolia(),
) as u64
);
}
}

#[test]
fn calculate_optimism_base_fee_success() {
let base_fee = [
1000000000, 1000000000, 1000000000, 1072671875, 1059263476, 1049238967, 1049238967, 0,
1, 2,
];
let gas_used = [
10000000, 10000000, 10000000, 9000000, 10001000, 0, 10000000, 10000000, 10000000,
10000000,
];
let gas_limit = [
10000000, 12000000, 14000000, 10000000, 14000000, 2000000, 18000000, 18000000,
18000000, 18000000,
];
let next_base_fee = [
1100000048, 1080000000, 1065714297, 1167067046, 1128881311, 1028254188, 1098203452, 1,
2, 3,
];

for i in 0..base_fee.len() {
assert_eq!(
next_base_fee[i],
calc_next_block_base_fee(
gas_used[i] as u128,
gas_limit[i] as u128,
base_fee[i] as u128,
BaseFeeParams::optimism(),
) as u64
);
}
}

#[test]
fn calculate_base_sepolia_base_fee_success() {
let base_fee = [
1000000000, 1000000000, 1000000000, 1072671875, 1059263476, 1049238967, 1049238967, 0,
1, 2,
];
let gas_used = [
10000000, 10000000, 10000000, 9000000, 10001000, 0, 10000000, 10000000, 10000000,
10000000,
];
let gas_limit = [
10000000, 12000000, 14000000, 10000000, 14000000, 2000000, 18000000, 18000000,
18000000, 18000000,
];
let next_base_fee = [
1180000000, 1146666666, 1122857142, 1244299375, 1189416692, 1028254188, 1144836295, 1,
2, 3,
];

for i in 0..base_fee.len() {
assert_eq!(
next_base_fee[i],
calc_next_block_base_fee(
gas_used[i] as u128,
gas_limit[i] as u128,
base_fee[i] as u128,
BaseFeeParams::base_sepolia(),
) as u64
);
}
}
}

0 comments on commit 62a30f2

Please sign in to comment.