Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: eth_simulateV1 support #10829

Merged
merged 16 commits into from
Sep 18, 2024
Prev Previous commit
Next Next commit
handle basefee correctly
  • Loading branch information
klkvr committed Sep 14, 2024
commit 6e597ac96cebae7b5e4e7d2bfca8b208261a5e9e
25 changes: 24 additions & 1 deletion crates/rpc/rpc-eth-api/src/helpers/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use alloy_primitives::{Bytes, TxKind, B256, U256};
use futures::Future;
use reth_evm::{ConfigureEvm, ConfigureEvmEnv};
use reth_primitives::{
basefee::calc_next_block_base_fee,
revm_primitives::{
BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ExecutionResult, HaltReason,
ResultAndState, TransactTo, TxEnv,
Expand Down Expand Up @@ -104,13 +105,35 @@ pub trait EthCall: Call + LoadPendingBlock {
let this = self.clone();
self.spawn_with_state_at_block(block, move |state| {
let mut db = CacheDB::new(StateProviderDatabase::new(state));
let mut blocks = Vec::with_capacity(block_state_calls.len());
let mut blocks: Vec<
SimulatedBlock<Block<WithOtherFields<reth_rpc_types::Transaction>>>,
> = Vec::with_capacity(block_state_calls.len());
let mut gas_used = 0;
for block in block_state_calls {
// Increase number and timestamp for every new block
block_env.number += U256::from(1);
block_env.timestamp += U256::from(1);

if !validation {
block_env.basefee = U256::ZERO;
} else {
let chain_spec = LoadPendingBlock::provider(&this).chain_spec();
let base_fee_params =
chain_spec.base_fee_params_at_timestamp(block_env.timestamp.to());
let base_fee = if let Some(latest) = blocks.last() {
let header = &latest.inner.header;
calc_next_block_base_fee(
header.gas_used,
header.gas_limit,
header.base_fee_per_gas.unwrap_or_default(),
base_fee_params,
)
} else {
base_block.header.next_block_base_fee(base_fee_params).unwrap_or_default() as u128
};
block_env.basefee = U256::from(base_fee);
}

let SimBlock { block_overrides, state_overrides, mut calls } = block;

if let Some(block_overrides) = block_overrides {
Expand Down