diff --git a/crates/driver/src/domain/competition/solution/settlement.rs b/crates/driver/src/domain/competition/solution/settlement.rs index c2d62f41fa..b92766e356 100644 --- a/crates/driver/src/domain/competition/solution/settlement.rs +++ b/crates/driver/src/domain/competition/solution/settlement.rs @@ -178,7 +178,7 @@ impl Settlement { ) .await?; let price = eth.gas_price().await?; - let gas = Gas::new(gas, eth.block_gas_limit())?; + let gas = Gas::new(gas, eth.block_gas_limit(), eth.tx_gas_limit())?; // Ensure that the solver has sufficient balance for the settlement to be mined // even if the gas price keeps climbing during the tx submission. @@ -430,7 +430,11 @@ pub struct Gas { impl Gas { /// Computes settlement gas parameters given estimates for gas and gas /// price. - pub fn new(estimate: eth::Gas, block_limit: eth::Gas) -> Result { + pub fn new( + estimate: eth::Gas, + block_limit: eth::Gas, + tx_gas_limit: eth::Gas, + ) -> Result { // We don't allow for solutions to take up more than half of the block's gas // limit. This is to ensure that block producers attempt to include the // settlement transaction in the next block as long as it is reasonably @@ -441,7 +445,11 @@ impl Gas { // will not exceed the remaining space in the block next and ignore transactions // whose gas limit exceed the remaining space (without simulating the actual // gas required). - let max_gas = eth::Gas(block_limit.0 / eth::U256::from(2)); + // Additionally cap by the configured per-tx gas limit. Operators set + // this per chain (e.g. to EIP-7825's 16,777,215 cap on Mainnet Fusaka) + // so the mempool can't reject the settlement for exceeding the per-tx + // ceiling. + let max_gas = std::cmp::min(eth::Gas(block_limit.0 / eth::U256::from(2)), tx_gas_limit); if estimate > max_gas { return Err(solution::Error::GasLimitExceeded(estimate, max_gas)); } @@ -467,3 +475,74 @@ impl Gas { self.limit * max_fee_per_gas.into() } } + +#[cfg(test)] +mod tests { + use super::*; + + fn gas(value: u64) -> eth::Gas { + eth::Gas(eth::U256::from(value)) + } + + /// EIP-7825 per-transaction gas cap (2^24 - 1) introduced in Mainnet's + /// Fusaka hardfork. Used in tests as a representative value for the + /// configurable `tx_gas_limit` knob on Mainnet. + const EIP_7825_MAINNET_TX_GAS_CAP: u64 = (1 << 24) - 1; + + #[test] + fn rejects_solution_above_tx_gas_limit() { + // Block limit (120M) is high enough that half the block (60M) exceeds + // the configured per-tx limit (EIP-7825 cap, 16,777,215). The per-tx + // limit must win. + let block_limit = gas(120_000_000); + let tx_gas_limit = gas(EIP_7825_MAINNET_TX_GAS_CAP); + let estimate = gas(20_000_000); + let err = Gas::new(estimate, block_limit, tx_gas_limit).unwrap_err(); + match err { + solution::Error::GasLimitExceeded(used, limit) => { + assert_eq!(used, estimate); + assert_eq!(limit, tx_gas_limit); + } + other => panic!("unexpected error: {other:?}"), + } + } + + #[test] + fn accepts_solution_at_tx_gas_limit() { + let block_limit = gas(120_000_000); + let tx_gas_limit = gas(EIP_7825_MAINNET_TX_GAS_CAP); + let result = Gas::new(tx_gas_limit, block_limit, tx_gas_limit).unwrap(); + assert_eq!(result.estimate, tx_gas_limit); + // The 2x buffer would otherwise push limit to 2 * tx_gas_limit; the + // min(max_gas, ...) clamp must keep it at the configured cap. + assert_eq!(result.limit, tx_gas_limit); + } + + #[test] + fn small_block_limit_still_caps_at_half() { + // On chains with a low block gas limit, the half-block cap is tighter + // than the configured per-tx limit and must keep applying. + let block_limit = gas(10_000_000); + let tx_gas_limit = gas(EIP_7825_MAINNET_TX_GAS_CAP); + let estimate = gas(6_000_000); + let err = Gas::new(estimate, block_limit, tx_gas_limit).unwrap_err(); + match err { + solution::Error::GasLimitExceeded(_, limit) => assert_eq!(limit, gas(5_000_000)), + other => panic!("unexpected error: {other:?}"), + } + } + + #[test] + fn high_tx_gas_limit_lets_half_block_bind() { + // Non-Fusaka chain: tx_gas_limit configured well above half the block, + // so the half-block cap is the binding limit. + let block_limit = gas(120_000_000); + let tx_gas_limit = gas(100_000_000); + let estimate = gas(70_000_000); + let err = Gas::new(estimate, block_limit, tx_gas_limit).unwrap_err(); + match err { + solution::Error::GasLimitExceeded(_, limit) => assert_eq!(limit, gas(60_000_000)), + other => panic!("unexpected error: {other:?}"), + } + } +} diff --git a/crates/driver/src/infra/blockchain/mod.rs b/crates/driver/src/infra/blockchain/mod.rs index 8b3211998f..0dd0f53a46 100644 --- a/crates/driver/src/infra/blockchain/mod.rs +++ b/crates/driver/src/infra/blockchain/mod.rs @@ -91,6 +91,7 @@ struct Inner { current_block: CurrentBlockWatcher, balance_simulator: BalanceSimulator, balance_overrider: Arc, + tx_gas_limit: eth::Gas, } impl Ethereum { @@ -105,6 +106,7 @@ impl Ethereum { addresses: contracts::Addresses, gas: Arc, current_block_args: &shared::current_block::Arguments, + tx_gas_limit: eth::Gas, ) -> Self { let Rpc { web3, chain, args } = rpc; let current_block_stream = current_block_args @@ -132,6 +134,7 @@ impl Ethereum { gas, balance_simulator, balance_overrider, + tx_gas_limit, }), web3, } @@ -190,6 +193,12 @@ impl Ethereum { self.inner.current_block.borrow().gas_limit.into() } + /// Per-transaction gas limit configured for this driver. Operators set + /// this per chain (e.g. EIP-7825's 16,777,215 cap on Fusaka chains). + pub fn tx_gas_limit(&self) -> eth::Gas { + self.inner.tx_gas_limit + } + /// Returns the current [`eth::Ether`] balance of the specified account. pub async fn balance(&self, address: eth::Address) -> Result { self.web3 diff --git a/crates/driver/src/run.rs b/crates/driver/src/run.rs index 90f5d6ce50..83cfe9b00e 100644 --- a/crates/driver/src/run.rs +++ b/crates/driver/src/run.rs @@ -16,6 +16,7 @@ use { }, }, clap::Parser, + eth_domain_types as eth, futures::future::join_all, http_client::HttpClientFactory, shared::arguments::tracing_config, @@ -195,7 +196,14 @@ async fn ethereum( .await .expect("initialize gas price estimator"), ); - Ethereum::new(ethrpc, config.contracts.clone(), gas, current_block_args).await + Ethereum::new( + ethrpc, + config.contracts.clone(), + gas, + current_block_args, + eth::Gas(config.tx_gas_limit), + ) + .await } async fn solvers(config: &config::Config, eth: &Ethereum) -> Vec { diff --git a/crates/driver/src/tests/setup/solver.rs b/crates/driver/src/tests/setup/solver.rs index f18f11c64c..4a3bdf9c38 100644 --- a/crates/driver/src/tests/setup/solver.rs +++ b/crates/driver/src/tests/setup/solver.rs @@ -479,6 +479,7 @@ impl Solver { block_stream_poll_interval: None, node_ws_url: Some(config.blockchain.web3_ws_url.parse().unwrap()), }, + eth_domain_types::Gas(eth_domain_types::U256::from(45_000_000u64)), ) .await;