1use alloy_eips::eip1559::ETHEREUM_BLOCK_GAS_LIMIT_30M;
2use reth_primitives_traits::constants::GAS_LIMIT_BOUND_DIVISOR;
34/// Settings for the Ethereum builder.
5#[derive(PartialEq, Eq, Clone, Debug)]
6pub struct EthereumBuilderConfig {
7/// Desired gas limit.
8pub desired_gas_limit: u64,
9/// Waits for the first payload to be built if there is no payload built when the payload is
10 /// being resolved.
11pub await_payload_on_missing: bool,
12}
1314impl Defaultfor EthereumBuilderConfig {
15fn default() -> Self {
16Self::new()
17 }
18}
1920impl EthereumBuilderConfig {
21/// Create new payload builder config.
22pub const fn new() -> Self {
23Self { desired_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT_30M, await_payload_on_missing: true }
24 }
2526/// Set desired gas limit.
27pub const fn with_gas_limit(mut self, desired_gas_limit: u64) -> Self {
28self.desired_gas_limit = desired_gas_limit;
29self30}
3132/// Configures whether the initial payload should be awaited when the payload job is being
33 /// resolved and no payload has been built yet.
34pub const fn with_await_payload_on_missing(mut self, await_payload_on_missing: bool) -> Self {
35self.await_payload_on_missing = await_payload_on_missing;
36self37}
38}
3940impl EthereumBuilderConfig {
41/// Returns the gas limit for the next block based
42 /// on parent and desired gas limits.
43pub fn gas_limit(&self, parent_gas_limit: u64) -> u64 {
44calculate_block_gas_limit(parent_gas_limit, self.desired_gas_limit)
45 }
46}
4748/// Calculate the gas limit for the next block based on parent and desired gas limits.
49/// Ref: <https://github.com/ethereum/go-ethereum/blob/88cbfab332c96edfbe99d161d9df6a40721bd786/core/block_validator.go#L166>
50pub fn calculate_block_gas_limit(parent_gas_limit: u64, desired_gas_limit: u64) -> u64 {
51let delta = (parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR).saturating_sub(1);
52let min_gas_limit = parent_gas_limit - delta;
53let max_gas_limit = parent_gas_limit + delta;
54desired_gas_limit.clamp(min_gas_limit, max_gas_limit)
55}