reth_ethereum_payload_builder/config.rs
1use alloy_eips::eip1559::ETHEREUM_BLOCK_GAS_LIMIT_30M;
2use reth_primitives_traits::constants::GAS_LIMIT_BOUND_DIVISOR;
3
4/// Settings for the Ethereum builder.
5#[derive(PartialEq, Eq, Clone, Debug)]
6pub struct EthereumBuilderConfig {
7 /// Desired gas limit.
8 pub desired_gas_limit: u64,
9}
10
11impl Default for EthereumBuilderConfig {
12 fn default() -> Self {
13 Self::new()
14 }
15}
16
17impl EthereumBuilderConfig {
18 /// Create new payload builder config.
19 pub const fn new() -> Self {
20 Self { desired_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT_30M }
21 }
22
23 /// Set desired gas limit.
24 pub const fn with_gas_limit(mut self, desired_gas_limit: u64) -> Self {
25 self.desired_gas_limit = desired_gas_limit;
26 self
27 }
28}
29
30impl EthereumBuilderConfig {
31 /// Returns the gas limit for the next block based
32 /// on parent and desired gas limits.
33 pub fn gas_limit(&self, parent_gas_limit: u64) -> u64 {
34 calculate_block_gas_limit(parent_gas_limit, self.desired_gas_limit)
35 }
36}
37
38/// Calculate the gas limit for the next block based on parent and desired gas limits.
39/// Ref: <https://github.com/ethereum/go-ethereum/blob/88cbfab332c96edfbe99d161d9df6a40721bd786/core/block_validator.go#L166>
40pub fn calculate_block_gas_limit(parent_gas_limit: u64, desired_gas_limit: u64) -> u64 {
41 let delta = (parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR).saturating_sub(1);
42 let min_gas_limit = parent_gas_limit - delta;
43 let max_gas_limit = parent_gas_limit + delta;
44 desired_gas_limit.clamp(min_gas_limit, max_gas_limit)
45}