fix(builder): desired block gas limit (#13351)

This commit is contained in:
Roman Krasiuk
2024-12-12 17:29:33 +01:00
committed by GitHub
parent 6ff2510ad9
commit e9577729f8
19 changed files with 66 additions and 50 deletions

2
Cargo.lock generated
View File

@ -7626,6 +7626,7 @@ dependencies = [
"reth-payload-builder-primitives", "reth-payload-builder-primitives",
"reth-payload-primitives", "reth-payload-primitives",
"reth-primitives", "reth-primitives",
"reth-primitives-traits",
"reth-revm", "reth-revm",
"reth-storage-api", "reth-storage-api",
"reth-transaction-pool", "reth-transaction-pool",
@ -8962,7 +8963,6 @@ dependencies = [
"reth-consensus-common", "reth-consensus-common",
"reth-engine-primitives", "reth-engine-primitives",
"reth-errors", "reth-errors",
"reth-ethereum-consensus",
"reth-evm", "reth-evm",
"reth-evm-ethereum", "reth-evm-ethereum",
"reth-network-api", "reth-network-api",

View File

@ -458,7 +458,7 @@ TxPool:
[default: 7] [default: 7]
--txpool.gas-limit <GAS_LIMIT> --txpool.gas-limit <ENFORCED_GAS_LIMIT>
The default enforced gas limit for transactions entering the pool The default enforced gas limit for transactions entering the pool
[default: 30000000] [default: 30000000]
@ -514,7 +514,7 @@ Builder:
[default: reth/<VERSION>/<OS>] [default: reth/<VERSION>/<OS>]
--builder.gaslimit <GAS_LIMIT> --builder.gaslimit <GAS_LIMIT>
Target gas ceiling for built blocks Target gas limit for built blocks
[default: 30000000] [default: 30000000]

View File

@ -22,12 +22,12 @@ use reth_consensus_common::validation::{
validate_header_base_fee, validate_header_extradata, validate_header_gas, validate_header_base_fee, validate_header_extradata, validate_header_gas,
}; };
use reth_primitives::{BlockWithSenders, NodePrimitives, Receipt, SealedBlock, SealedHeader}; 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}; 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; mod validation;
pub use validation::validate_block_post_execution; pub use validation::validate_block_post_execution;

View File

@ -154,7 +154,7 @@ impl ConfigureEvmEnv for EthEvmConfig {
self.chain_spec.base_fee_params_at_timestamp(attributes.timestamp), 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 // 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. // elasticity multiplier to get the new gas limit.

View File

@ -27,7 +27,6 @@ reth-consensus.workspace = true
reth-beacon-consensus.workspace = true reth-beacon-consensus.workspace = true
reth-rpc.workspace = true reth-rpc.workspace = true
reth-node-api.workspace = true reth-node-api.workspace = true
reth-node-core.workspace = true
reth-chainspec.workspace = true reth-chainspec.workspace = true
reth-primitives.workspace = true reth-primitives.workspace = true
reth-revm = { workspace = true, features = ["std"] } reth-revm = { workspace = true, features = ["std"] }
@ -43,7 +42,7 @@ eyre.workspace = true
reth-chainspec.workspace = true reth-chainspec.workspace = true
reth-db.workspace = true reth-db.workspace = true
reth-exex.workspace = true reth-exex.workspace = true
reth-node-api.workspace = true reth-node-core.workspace = true
reth-payload-primitives.workspace = true reth-payload-primitives.workspace = true
reth-e2e-test-utils.workspace = true reth-e2e-test-utils.workspace = true
reth-rpc-eth-api.workspace = true reth-rpc-eth-api.workspace = true

View File

@ -1,7 +1,5 @@
//! Ethereum Node types config. //! Ethereum Node types config.
use std::sync::Arc;
use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig}; use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig};
use reth_beacon_consensus::EthBeaconConsensus; use reth_beacon_consensus::EthBeaconConsensus;
use reth_chainspec::ChainSpec; use reth_chainspec::ChainSpec;
@ -24,7 +22,6 @@ use reth_node_builder::{
rpc::{EngineValidatorBuilder, RpcAddOns}, rpc::{EngineValidatorBuilder, RpcAddOns},
BuilderContext, Node, NodeAdapter, NodeComponentsBuilder, PayloadBuilderConfig, PayloadTypes, BuilderContext, Node, NodeAdapter, NodeComponentsBuilder, PayloadBuilderConfig, PayloadTypes,
}; };
use reth_node_core::version::default_extra_data_bytes;
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService}; use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_primitives::{EthPrimitives, PooledTransactionsElement}; use reth_primitives::{EthPrimitives, PooledTransactionsElement};
use reth_provider::{CanonStateSubscriptions, EthStorage}; use reth_provider::{CanonStateSubscriptions, EthStorage};
@ -35,6 +32,7 @@ use reth_transaction_pool::{
TransactionValidationTaskExecutor, TransactionValidationTaskExecutor,
}; };
use reth_trie_db::MerklePatriciaTrie; use reth_trie_db::MerklePatriciaTrie;
use std::sync::Arc;
use crate::{EthEngineTypes, EthEvmConfig}; use crate::{EthEngineTypes, EthEvmConfig};
@ -230,24 +228,9 @@ where
} }
/// A basic ethereum payload service. /// A basic ethereum payload service.
#[derive(Clone, Debug)] #[derive(Clone, Default, Debug)]
pub struct EthereumPayloadBuilder { #[non_exhaustive]
/// Payload builder configuration. pub struct EthereumPayloadBuilder;
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 }
}
}
impl EthereumPayloadBuilder { impl EthereumPayloadBuilder {
/// A helper method initializing [`PayloadBuilderService`] with the given EVM config. /// A helper method initializing [`PayloadBuilderService`] with the given EVM config.
@ -270,9 +253,11 @@ impl EthereumPayloadBuilder {
PayloadBuilderAttributes = EthPayloadBuilderAttributes, PayloadBuilderAttributes = EthPayloadBuilderAttributes,
>, >,
{ {
let payload_builder =
reth_ethereum_payload_builder::EthereumPayloadBuilder::new(evm_config, self.config);
let conf = ctx.payload_builder_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() let payload_job_config = BasicPayloadJobGeneratorConfig::default()
.interval(conf.interval()) .interval(conf.interval())

View File

@ -14,6 +14,7 @@ workspace = true
[dependencies] [dependencies]
# reth # reth
reth-primitives.workspace = true reth-primitives.workspace = true
reth-primitives-traits.workspace = true
reth-revm.workspace = true reth-revm.workspace = true
reth-transaction-pool.workspace = true reth-transaction-pool.workspace = true
reth-payload-builder.workspace = true reth-payload-builder.workspace = true

View File

@ -1,16 +1,26 @@
use alloy_eips::eip1559::ETHEREUM_BLOCK_GAS_LIMIT;
use alloy_primitives::Bytes; use alloy_primitives::Bytes;
use reth_primitives_traits::constants::GAS_LIMIT_BOUND_DIVISOR;
/// Settings for the Ethereum builder. /// Settings for the Ethereum builder.
#[derive(PartialEq, Eq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
pub struct EthereumBuilderConfig { pub struct EthereumBuilderConfig {
/// Block extra data. /// Block extra data.
pub extra_data: Bytes, pub extra_data: Bytes,
/// Desired gas limit.
pub desired_gas_limit: u64,
} }
impl EthereumBuilderConfig { impl EthereumBuilderConfig {
/// Create new payload builder config. /// Create new payload builder config.
pub const fn new(extra_data: Bytes) -> Self { 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
} }
} }
@ -19,4 +29,19 @@ impl EthereumBuilderConfig {
pub fn extra_data(&self) -> Bytes { pub fn extra_data(&self) -> Bytes {
self.extra_data.clone() 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)
} }

View File

@ -88,6 +88,7 @@ where
timestamp: config.attributes.timestamp(), timestamp: config.attributes.timestamp(),
suggested_fee_recipient: config.attributes.suggested_fee_recipient(), suggested_fee_recipient: config.attributes.suggested_fee_recipient(),
prev_randao: config.attributes.prev_randao(), 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) self.evm_config.next_cfg_and_block_env(parent, next_attributes)
} }

View File

@ -231,6 +231,8 @@ pub struct NextBlockEnvAttributes {
pub suggested_fee_recipient: Address, pub suggested_fee_recipient: Address,
/// The randomness value for the next block. /// The randomness value for the next block.
pub prev_randao: B256, pub prev_randao: B256,
/// Block gas limit.
pub gas_limit: u64,
} }
/// Function hook that allows to modify a transaction environment. /// Function hook that allows to modify a transaction environment.

View File

@ -16,9 +16,9 @@ pub struct PayloadBuilderArgs {
#[arg(long = "builder.extradata", value_parser = ExtradataValueParser::default(), default_value_t = default_extra_data())] #[arg(long = "builder.extradata", value_parser = ExtradataValueParser::default(), default_value_t = default_extra_data())]
pub extradata: String, pub extradata: String,
/// Target gas ceiling for built blocks. /// Target gas limit for built blocks.
#[arg(long = "builder.gaslimit", default_value = "30000000", value_name = "GAS_LIMIT")] #[arg(long = "builder.gaslimit", default_value_t = ETHEREUM_BLOCK_GAS_LIMIT, value_name = "GAS_LIMIT")]
pub max_gas_limit: u64, pub gas_limit: u64,
/// The interval at which the job should build a new payload after the last. /// The interval at which the job should build a new payload after the last.
/// ///
@ -41,7 +41,7 @@ impl Default for PayloadBuilderArgs {
fn default() -> Self { fn default() -> Self {
Self { Self {
extradata: default_extra_data(), extradata: default_extra_data(),
max_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT, gas_limit: ETHEREUM_BLOCK_GAS_LIMIT,
interval: Duration::from_secs(1), interval: Duration::from_secs(1),
deadline: SLOT_DURATION, deadline: SLOT_DURATION,
max_payload_tasks: 3, max_payload_tasks: 3,
@ -62,8 +62,8 @@ impl PayloadBuilderConfig for PayloadBuilderArgs {
self.deadline self.deadline
} }
fn max_gas_limit(&self) -> u64 { fn gas_limit(&self) -> u64 {
self.max_gas_limit self.gas_limit
} }
fn max_payload_tasks(&self) -> usize { fn max_payload_tasks(&self) -> usize {
@ -129,7 +129,7 @@ mod tests {
} }
#[test] #[test]
fn test_default_extradata() { fn test_default_extra_data() {
let extradata = default_extra_data(); let extradata = default_extra_data();
let args = CommandParser::<PayloadBuilderArgs>::parse_from([ let args = CommandParser::<PayloadBuilderArgs>::parse_from([
"reth", "reth",

View File

@ -52,7 +52,7 @@ pub struct TxPoolArgs {
/// The default enforced gas limit for transactions entering the pool /// The default enforced gas limit for transactions entering the pool
#[arg(long = "txpool.gas-limit", default_value_t = ETHEREUM_BLOCK_GAS_LIMIT)] #[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 /// Price bump percentage to replace an already existing blob transaction
#[arg(long = "blobpool.pricebump", default_value_t = REPLACE_BLOB_PRICE_BUMP)] #[arg(long = "blobpool.pricebump", default_value_t = REPLACE_BLOB_PRICE_BUMP)]
@ -105,7 +105,7 @@ impl Default for TxPoolArgs {
max_account_slots: TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER, max_account_slots: TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER,
price_bump: DEFAULT_PRICE_BUMP, price_bump: DEFAULT_PRICE_BUMP,
minimal_protocol_basefee: MIN_PROTOCOL_BASE_FEE, 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, blob_transaction_price_bump: REPLACE_BLOB_PRICE_BUMP,
max_tx_input_bytes: DEFAULT_MAX_TX_INPUT_BYTES, max_tx_input_bytes: DEFAULT_MAX_TX_INPUT_BYTES,
max_cached_entries: DEFAULT_MAX_CACHED_BLOBS, max_cached_entries: DEFAULT_MAX_CACHED_BLOBS,
@ -151,7 +151,7 @@ impl RethTransactionPoolConfig for TxPoolArgs {
replace_blob_tx_price_bump: self.blob_transaction_price_bump, replace_blob_tx_price_bump: self.blob_transaction_price_bump,
}, },
minimal_protocol_basefee: self.minimal_protocol_basefee, 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, pending_tx_listener_buffer_size: self.pending_tx_listener_buffer_size,
new_tx_listener_buffer_size: self.new_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, max_new_pending_txs_notifications: self.max_new_pending_txs_notifications,

View File

@ -24,8 +24,8 @@ pub trait PayloadBuilderConfig {
/// The deadline for when the payload builder job should resolve. /// The deadline for when the payload builder job should resolve.
fn deadline(&self) -> Duration; fn deadline(&self) -> Duration;
/// Target gas ceiling for built blocks. /// Target gas limit for built blocks.
fn max_gas_limit(&self) -> u64; fn gas_limit(&self) -> u64;
/// Maximum number of tasks to spawn for building a payload. /// Maximum number of tasks to spawn for building a payload.
fn max_payload_tasks(&self) -> usize; fn max_payload_tasks(&self) -> usize;

View File

@ -157,7 +157,7 @@ impl ConfigureEvmEnv for OpEvmConfig {
timestamp: U256::from(attributes.timestamp), timestamp: U256::from(attributes.timestamp),
difficulty: U256::ZERO, difficulty: U256::ZERO,
prevrandao: Some(attributes.prev_randao), 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 // calculate basefee based on parent block's gas usage
basefee: self.chain_spec.next_block_base_fee(parent, attributes.timestamp)?, basefee: self.chain_spec.next_block_base_fee(parent, attributes.timestamp)?,
// calculate excess gas based on parent block's blob gas usage // calculate excess gas based on parent block's blob gas usage

View File

@ -175,6 +175,7 @@ where
timestamp: attributes.timestamp(), timestamp: attributes.timestamp(),
suggested_fee_recipient: attributes.suggested_fee_recipient(), suggested_fee_recipient: attributes.suggested_fee_recipient(),
prev_randao: attributes.prev_randao(), 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) self.evm_config.next_cfg_and_block_env(parent, next_attributes)
} }

View File

@ -10,6 +10,9 @@ pub const RETH_CLIENT_VERSION: &str = concat!("reth/v", env!("CARGO_PKG_VERSION"
/// Minimum gas limit allowed for transactions. /// Minimum gas limit allowed for transactions.
pub const MINIMUM_GAS_LIMIT: u64 = 5000; 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. /// 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 /// In reality, the node can end up in this particular situation very rarely. It would happen only

View File

@ -116,6 +116,7 @@ pub trait LoadPendingBlock:
timestamp: latest.timestamp() + 12, timestamp: latest.timestamp() + 12,
suggested_fee_recipient: latest.beneficiary(), suggested_fee_recipient: latest.beneficiary(),
prev_randao: B256::random(), prev_randao: B256::random(),
gas_limit: latest.gas_limit(),
}, },
) )
.map_err(RethError::other) .map_err(RethError::other)

View File

@ -20,7 +20,6 @@ reth-rpc-api.workspace = true
reth-rpc-eth-api.workspace = true reth-rpc-eth-api.workspace = true
reth-engine-primitives.workspace = true reth-engine-primitives.workspace = true
reth-errors.workspace = true reth-errors.workspace = true
reth-ethereum-consensus.workspace = true
reth-provider.workspace = true reth-provider.workspace = true
reth-transaction-pool.workspace = true reth-transaction-pool.workspace = true
reth-network-api.workspace = true reth-network-api.workspace = true

View File

@ -16,10 +16,9 @@ use reth_chainspec::{ChainSpecProvider, EthereumHardforks};
use reth_consensus::{Consensus, FullConsensus, PostExecutionInput}; use reth_consensus::{Consensus, FullConsensus, PostExecutionInput};
use reth_engine_primitives::PayloadValidator; use reth_engine_primitives::PayloadValidator;
use reth_errors::{BlockExecutionError, ConsensusError, ProviderError}; use reth_errors::{BlockExecutionError, ConsensusError, ProviderError};
use reth_ethereum_consensus::GAS_LIMIT_BOUND_DIVISOR;
use reth_evm::execute::{BlockExecutorProvider, Executor}; use reth_evm::execute::{BlockExecutorProvider, Executor};
use reth_primitives::{GotExpected, NodePrimitives, SealedBlockWithSenders, SealedHeader}; 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::{ use reth_provider::{
BlockExecutionInput, BlockExecutionOutput, BlockReaderIdExt, StateProviderFactory, BlockExecutionInput, BlockExecutionOutput, BlockReaderIdExt, StateProviderFactory,
}; };