Skip to content

Commit

Permalink
fix(builder): desired block gas limit (#13351)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk authored Dec 12, 2024
1 parent 6ff2510 commit e957772
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions book/cli/reth/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ TxPool:
[default: 7]
--txpool.gas-limit <GAS_LIMIT>
--txpool.gas-limit <ENFORCED_GAS_LIMIT>
The default enforced gas limit for transactions entering the pool
[default: 30000000]
Expand Down Expand Up @@ -514,7 +514,7 @@ Builder:
[default: reth/<VERSION>/<OS>]
--builder.gaslimit <GAS_LIMIT>
Target gas ceiling for built blocks
Target gas limit for built blocks
[default: 30000000]
Expand Down
8 changes: 4 additions & 4 deletions crates/ethereum/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ use reth_consensus_common::validation::{
validate_header_base_fee, validate_header_extradata, validate_header_gas,
};
use reth_primitives::{BlockWithSenders, NodePrimitives, Receipt, SealedBlock, SealedHeader};
use reth_primitives_traits::{constants::MINIMUM_GAS_LIMIT, BlockBody};
use reth_primitives_traits::{
constants::{GAS_LIMIT_BOUND_DIVISOR, MINIMUM_GAS_LIMIT},
BlockBody,
};
use std::{fmt::Debug, sync::Arc, time::SystemTime};

/// The bound divisor of the gas limit, used in update calculations.
pub const GAS_LIMIT_BOUND_DIVISOR: u64 = 1024;

mod validation;
pub use validation::validate_block_post_execution;

Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl ConfigureEvmEnv for EthEvmConfig {
self.chain_spec.base_fee_params_at_timestamp(attributes.timestamp),
);

let mut gas_limit = U256::from(parent.gas_limit);
let mut gas_limit = U256::from(attributes.gas_limit);

// If we are on the London fork boundary, we need to multiply the parent's gas limit by the
// elasticity multiplier to get the new gas limit.
Expand Down
3 changes: 1 addition & 2 deletions crates/ethereum/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ reth-consensus.workspace = true
reth-beacon-consensus.workspace = true
reth-rpc.workspace = true
reth-node-api.workspace = true
reth-node-core.workspace = true
reth-chainspec.workspace = true
reth-primitives.workspace = true
reth-revm = { workspace = true, features = ["std"] }
Expand All @@ -43,7 +42,7 @@ eyre.workspace = true
reth-chainspec.workspace = true
reth-db.workspace = true
reth-exex.workspace = true
reth-node-api.workspace = true
reth-node-core.workspace = true
reth-payload-primitives.workspace = true
reth-e2e-test-utils.workspace = true
reth-rpc-eth-api.workspace = true
Expand Down
31 changes: 8 additions & 23 deletions crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Ethereum Node types config.
use std::sync::Arc;

use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig};
use reth_beacon_consensus::EthBeaconConsensus;
use reth_chainspec::ChainSpec;
Expand All @@ -24,7 +22,6 @@ use reth_node_builder::{
rpc::{EngineValidatorBuilder, RpcAddOns},
BuilderContext, Node, NodeAdapter, NodeComponentsBuilder, PayloadBuilderConfig, PayloadTypes,
};
use reth_node_core::version::default_extra_data_bytes;
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_primitives::{EthPrimitives, PooledTransactionsElement};
use reth_provider::{CanonStateSubscriptions, EthStorage};
Expand All @@ -35,6 +32,7 @@ use reth_transaction_pool::{
TransactionValidationTaskExecutor,
};
use reth_trie_db::MerklePatriciaTrie;
use std::sync::Arc;

use crate::{EthEngineTypes, EthEvmConfig};

Expand Down Expand Up @@ -230,24 +228,9 @@ where
}

/// A basic ethereum payload service.
#[derive(Clone, Debug)]
pub struct EthereumPayloadBuilder {
/// Payload builder configuration.
config: EthereumBuilderConfig,
}

impl Default for EthereumPayloadBuilder {
fn default() -> Self {
Self { config: EthereumBuilderConfig::new(default_extra_data_bytes()) }
}
}

impl EthereumPayloadBuilder {
/// Create new ethereum payload builder.
pub const fn new(config: EthereumBuilderConfig) -> Self {
Self { config }
}
}
#[derive(Clone, Default, Debug)]
#[non_exhaustive]
pub struct EthereumPayloadBuilder;

impl EthereumPayloadBuilder {
/// A helper method initializing [`PayloadBuilderService`] with the given EVM config.
Expand All @@ -270,9 +253,11 @@ impl EthereumPayloadBuilder {
PayloadBuilderAttributes = EthPayloadBuilderAttributes,
>,
{
let payload_builder =
reth_ethereum_payload_builder::EthereumPayloadBuilder::new(evm_config, self.config);
let conf = ctx.payload_builder_config();
let payload_builder = reth_ethereum_payload_builder::EthereumPayloadBuilder::new(
evm_config,
EthereumBuilderConfig::new(conf.extradata_bytes()).with_gas_limit(conf.gas_limit()),
);

let payload_job_config = BasicPayloadJobGeneratorConfig::default()
.interval(conf.interval())
Expand Down
1 change: 1 addition & 0 deletions crates/ethereum/payload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ workspace = true
[dependencies]
# reth
reth-primitives.workspace = true
reth-primitives-traits.workspace = true
reth-revm.workspace = true
reth-transaction-pool.workspace = true
reth-payload-builder.workspace = true
Expand Down
27 changes: 26 additions & 1 deletion crates/ethereum/payload/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
use alloy_eips::eip1559::ETHEREUM_BLOCK_GAS_LIMIT;
use alloy_primitives::Bytes;
use reth_primitives_traits::constants::GAS_LIMIT_BOUND_DIVISOR;

/// Settings for the Ethereum builder.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct EthereumBuilderConfig {
/// Block extra data.
pub extra_data: Bytes,
/// Desired gas limit.
pub desired_gas_limit: u64,
}

impl EthereumBuilderConfig {
/// Create new payload builder config.
pub const fn new(extra_data: Bytes) -> Self {
Self { extra_data }
Self { extra_data, desired_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT }
}

/// Set desired gas limit.
pub const fn with_gas_limit(mut self, desired_gas_limit: u64) -> Self {
self.desired_gas_limit = desired_gas_limit;
self
}
}

Expand All @@ -19,4 +29,19 @@ impl EthereumBuilderConfig {
pub fn extra_data(&self) -> Bytes {
self.extra_data.clone()
}

/// Returns the gas limit for the next block based
/// on parent and desired gas limits.
pub fn gas_limit(&self, parent_gas_limit: u64) -> u64 {
calculate_block_gas_limit(parent_gas_limit, self.desired_gas_limit)
}
}

/// Calculate the gas limit for the next block based on parent and desired gas limits.
/// Ref: <https://github.com/ethereum/go-ethereum/blob/88cbfab332c96edfbe99d161d9df6a40721bd786/core/block_validator.go#L166>
pub fn calculate_block_gas_limit(parent_gas_limit: u64, desired_gas_limit: u64) -> u64 {
let delta = (parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR).saturating_sub(1);
let min_gas_limit = parent_gas_limit - delta;
let max_gas_limit = parent_gas_limit + delta;
desired_gas_limit.clamp(min_gas_limit, max_gas_limit)
}
1 change: 1 addition & 0 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ where
timestamp: config.attributes.timestamp(),
suggested_fee_recipient: config.attributes.suggested_fee_recipient(),
prev_randao: config.attributes.prev_randao(),
gas_limit: self.builder_config.gas_limit(parent.gas_limit),
};
self.evm_config.next_cfg_and_block_env(parent, next_attributes)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ pub struct NextBlockEnvAttributes {
pub suggested_fee_recipient: Address,
/// The randomness value for the next block.
pub prev_randao: B256,
/// Block gas limit.
pub gas_limit: u64,
}

/// Function hook that allows to modify a transaction environment.
Expand Down
14 changes: 7 additions & 7 deletions crates/node/core/src/args/payload_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ pub struct PayloadBuilderArgs {
#[arg(long = "builder.extradata", value_parser = ExtradataValueParser::default(), default_value_t = default_extra_data())]
pub extradata: String,

/// Target gas ceiling for built blocks.
#[arg(long = "builder.gaslimit", default_value = "30000000", value_name = "GAS_LIMIT")]
pub max_gas_limit: u64,
/// Target gas limit for built blocks.
#[arg(long = "builder.gaslimit", default_value_t = ETHEREUM_BLOCK_GAS_LIMIT, value_name = "GAS_LIMIT")]
pub gas_limit: u64,

/// The interval at which the job should build a new payload after the last.
///
Expand All @@ -41,7 +41,7 @@ impl Default for PayloadBuilderArgs {
fn default() -> Self {
Self {
extradata: default_extra_data(),
max_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT,
gas_limit: ETHEREUM_BLOCK_GAS_LIMIT,
interval: Duration::from_secs(1),
deadline: SLOT_DURATION,
max_payload_tasks: 3,
Expand All @@ -62,8 +62,8 @@ impl PayloadBuilderConfig for PayloadBuilderArgs {
self.deadline
}

fn max_gas_limit(&self) -> u64 {
self.max_gas_limit
fn gas_limit(&self) -> u64 {
self.gas_limit
}

fn max_payload_tasks(&self) -> usize {
Expand Down Expand Up @@ -129,7 +129,7 @@ mod tests {
}

#[test]
fn test_default_extradata() {
fn test_default_extra_data() {
let extradata = default_extra_data();
let args = CommandParser::<PayloadBuilderArgs>::parse_from([
"reth",
Expand Down
6 changes: 3 additions & 3 deletions crates/node/core/src/args/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct TxPoolArgs {

/// The default enforced gas limit for transactions entering the pool
#[arg(long = "txpool.gas-limit", default_value_t = ETHEREUM_BLOCK_GAS_LIMIT)]
pub gas_limit: u64,
pub enforced_gas_limit: u64,

/// Price bump percentage to replace an already existing blob transaction
#[arg(long = "blobpool.pricebump", default_value_t = REPLACE_BLOB_PRICE_BUMP)]
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Default for TxPoolArgs {
max_account_slots: TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER,
price_bump: DEFAULT_PRICE_BUMP,
minimal_protocol_basefee: MIN_PROTOCOL_BASE_FEE,
gas_limit: ETHEREUM_BLOCK_GAS_LIMIT,
enforced_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT,
blob_transaction_price_bump: REPLACE_BLOB_PRICE_BUMP,
max_tx_input_bytes: DEFAULT_MAX_TX_INPUT_BYTES,
max_cached_entries: DEFAULT_MAX_CACHED_BLOBS,
Expand Down Expand Up @@ -151,7 +151,7 @@ impl RethTransactionPoolConfig for TxPoolArgs {
replace_blob_tx_price_bump: self.blob_transaction_price_bump,
},
minimal_protocol_basefee: self.minimal_protocol_basefee,
gas_limit: self.gas_limit,
gas_limit: self.enforced_gas_limit,
pending_tx_listener_buffer_size: self.pending_tx_listener_buffer_size,
new_tx_listener_buffer_size: self.new_tx_listener_buffer_size,
max_new_pending_txs_notifications: self.max_new_pending_txs_notifications,
Expand Down
4 changes: 2 additions & 2 deletions crates/node/core/src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub trait PayloadBuilderConfig {
/// The deadline for when the payload builder job should resolve.
fn deadline(&self) -> Duration;

/// Target gas ceiling for built blocks.
fn max_gas_limit(&self) -> u64;
/// Target gas limit for built blocks.
fn gas_limit(&self) -> u64;

/// Maximum number of tasks to spawn for building a payload.
fn max_payload_tasks(&self) -> usize;
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl ConfigureEvmEnv for OpEvmConfig {
timestamp: U256::from(attributes.timestamp),
difficulty: U256::ZERO,
prevrandao: Some(attributes.prev_randao),
gas_limit: U256::from(parent.gas_limit),
gas_limit: U256::from(attributes.gas_limit),
// calculate basefee based on parent block's gas usage
basefee: self.chain_spec.next_block_base_fee(parent, attributes.timestamp)?,
// calculate excess gas based on parent block's blob gas usage
Expand Down
1 change: 1 addition & 0 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ where
timestamp: attributes.timestamp(),
suggested_fee_recipient: attributes.suggested_fee_recipient(),
prev_randao: attributes.prev_randao(),
gas_limit: attributes.gas_limit.unwrap_or(parent.gas_limit),
};
self.evm_config.next_cfg_and_block_env(parent, next_attributes)
}
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives-traits/src/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub const RETH_CLIENT_VERSION: &str = concat!("reth/v", env!("CARGO_PKG_VERSION"
/// Minimum gas limit allowed for transactions.
pub const MINIMUM_GAS_LIMIT: u64 = 5000;

/// The bound divisor of the gas limit, used in update calculations.
pub const GAS_LIMIT_BOUND_DIVISOR: u64 = 1024;

/// The number of blocks to unwind during a reorg that already became a part of canonical chain.
///
/// In reality, the node can end up in this particular situation very rarely. It would happen only
Expand Down
1 change: 1 addition & 0 deletions crates/rpc/rpc-eth-api/src/helpers/pending_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub trait LoadPendingBlock:
timestamp: latest.timestamp() + 12,
suggested_fee_recipient: latest.beneficiary(),
prev_randao: B256::random(),
gas_limit: latest.gas_limit(),
},
)
.map_err(RethError::other)
Expand Down
1 change: 0 additions & 1 deletion crates/rpc/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ reth-rpc-api.workspace = true
reth-rpc-eth-api.workspace = true
reth-engine-primitives.workspace = true
reth-errors.workspace = true
reth-ethereum-consensus.workspace = true
reth-provider.workspace = true
reth-transaction-pool.workspace = true
reth-network-api.workspace = true
Expand Down
3 changes: 1 addition & 2 deletions crates/rpc/rpc/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ use reth_chainspec::{ChainSpecProvider, EthereumHardforks};
use reth_consensus::{Consensus, FullConsensus, PostExecutionInput};
use reth_engine_primitives::PayloadValidator;
use reth_errors::{BlockExecutionError, ConsensusError, ProviderError};
use reth_ethereum_consensus::GAS_LIMIT_BOUND_DIVISOR;
use reth_evm::execute::{BlockExecutorProvider, Executor};
use reth_primitives::{GotExpected, NodePrimitives, SealedBlockWithSenders, SealedHeader};
use reth_primitives_traits::{Block as _, BlockBody};
use reth_primitives_traits::{constants::GAS_LIMIT_BOUND_DIVISOR, Block as _, BlockBody};
use reth_provider::{
BlockExecutionInput, BlockExecutionOutput, BlockReaderIdExt, StateProviderFactory,
};
Expand Down

0 comments on commit e957772

Please sign in to comment.