Skip to content

Commit 41b93ea

Browse files
committed
chore: port calc block gas limit
1 parent 8219286 commit 41b93ea

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

crates/eips/src/eip1559/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use alloy_primitives::U256;
33
/// The default Ethereum block gas limit.
44
pub const ETHEREUM_BLOCK_GAS_LIMIT: u64 = 30_000_000;
55

6+
/// The bound divisor of the gas limit, used in update calculations.
7+
pub const GAS_LIMIT_BOUND_DIVISOR: u64 = 1024;
8+
69
/// The minimum tx fee below which the txpool will reject the transaction.
710
///
811
/// Configured to `7` WEI which is the lowest possible value of base fee under mainnet EIP-1559

crates/eips/src/eip1559/helpers.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::eip1559::BaseFeeParams;
1+
use crate::eip1559::{constants::GAS_LIMIT_BOUND_DIVISOR, BaseFeeParams};
22

33
/// Calculate the base fee for the next block based on the EIP-1559 specification.
44
///
@@ -61,6 +61,15 @@ pub fn calc_next_block_base_fee(
6161
}
6262
}
6363

64+
/// Calculate the gas limit for the next block based on parent and desired gas limits.
65+
/// Ref: <https://github.com/ethereum/go-ethereum/blob/88cbfab332c96edfbe99d161d9df6a40721bd786/core/block_validator.go#L166>
66+
pub fn calculate_block_gas_limit(parent_gas_limit: u64, desired_gas_limit: u64) -> u64 {
67+
let delta = (parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR).saturating_sub(1);
68+
let min_gas_limit = parent_gas_limit - delta;
69+
let max_gas_limit = parent_gas_limit + delta;
70+
desired_gas_limit.clamp(min_gas_limit, max_gas_limit)
71+
}
72+
6473
#[cfg(test)]
6574
mod tests {
6675
use super::*;

crates/eips/src/eip1559/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ pub use basefee::BaseFeeParams;
88
mod constants;
99
pub use constants::{
1010
DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR, DEFAULT_ELASTICITY_MULTIPLIER,
11-
ETHEREUM_BLOCK_GAS_LIMIT, INITIAL_BASE_FEE, MIN_PROTOCOL_BASE_FEE, MIN_PROTOCOL_BASE_FEE_U256,
11+
ETHEREUM_BLOCK_GAS_LIMIT, GAS_LIMIT_BOUND_DIVISOR, INITIAL_BASE_FEE, MIN_PROTOCOL_BASE_FEE,
12+
MIN_PROTOCOL_BASE_FEE_U256,
1213
};
1314

1415
mod helpers;
15-
pub use helpers::calc_next_block_base_fee;
16+
pub use helpers::{calc_next_block_base_fee, calculate_block_gas_limit};

0 commit comments

Comments
 (0)