fix(rpc): add block number validation in eth_simulateV1 (#21396)

Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Matthias Seitz
2026-01-26 14:47:20 +01:00
committed by GitHub
parent 6cfd369d17
commit 507cf58db0
2 changed files with 27 additions and 3 deletions

View File

@@ -96,7 +96,23 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
self.spawn_with_state_at_block(block, move |this, mut db| {
let mut blocks: Vec<SimulatedBlock<RpcBlock<Self::NetworkTypes>>> =
Vec::with_capacity(block_state_calls.len());
// Track previous block number for validation
let mut prev_block_number = parent.number();
for block in block_state_calls {
// Validate block number ordering if overridden
if let Some(number) = block.block_overrides.as_ref().and_then(|o| o.number) {
let number: u64 = number.try_into().unwrap_or(u64::MAX);
if number <= prev_block_number {
return Err(EthApiError::other(EthSimulateError::BlockNumberInvalid {
got: number,
parent: prev_block_number,
})
.into());
}
}
let mut evm_env = this
.evm_config()
.next_evm_env(&parent, &this.next_env_attributes(&parent)?)
@@ -234,6 +250,9 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
parent = result.block.clone_sealed_header();
// Update tracking for next iteration's validation
prev_block_number = parent.number();
let block = simulate::build_simulated_block::<Self::Error, _>(
result.block,
results,

View File

@@ -39,8 +39,13 @@ pub enum EthSimulateError {
#[error("Client adjustable limit reached")]
GasLimitReached,
/// Block number in sequence did not increase.
#[error("Block number in sequence did not increase")]
BlockNumberInvalid,
#[error("block numbers must be in order: {got} <= {parent}")]
BlockNumberInvalid {
/// The block number that was provided.
got: u64,
/// The parent block number.
parent: u64,
},
/// Block timestamp in sequence did not increase or stay the same.
#[error("Block timestamp in sequence did not increase")]
BlockTimestampInvalid,
@@ -96,7 +101,7 @@ impl EthSimulateError {
Self::IntrinsicGasTooLow => -38013,
Self::InsufficientFunds { .. } => -38014,
Self::BlockGasLimitExceeded => -38015,
Self::BlockNumberInvalid => -38020,
Self::BlockNumberInvalid { .. } => -38020,
Self::BlockTimestampInvalid => -38021,
Self::PrecompileSelfReference => -38022,
Self::PrecompileDuplicateAddress => -38023,