Skip to content

Commit

Permalink
update revm state builder
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Jul 17, 2023
1 parent b9106d1 commit aaf94ad
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 29 deletions.
6 changes: 3 additions & 3 deletions crates/consensus/beacon/src/engine/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,15 @@ mod tests {
use reth_primitives::{
stage::StageCheckpoint, BlockBody, ChainSpec, ChainSpecBuilder, SealedHeader, MAINNET,
};
use reth_provider::{test_utils::TestExecutorFactory, PostState};
use reth_provider::{test_utils::TestExecutorFactory, BundleState};
use reth_stages::{test_utils::TestStages, ExecOutput, StageError};
use reth_tasks::TokioTaskExecutor;
use std::{collections::VecDeque, future::poll_fn, sync::Arc};
use tokio::sync::watch;

struct TestPipelineBuilder {
pipeline_exec_outputs: VecDeque<Result<ExecOutput, StageError>>,
executor_results: Vec<PostState>,
executor_results: Vec<BundleState>,
max_block: Option<BlockNumber>,
}

Expand All @@ -429,7 +429,7 @@ mod tests {

/// Set the executor results to use for the test consensus engine.
#[allow(dead_code)]
fn with_executor_results(mut self, executor_results: Vec<PostState>) -> Self {
fn with_executor_results(mut self, executor_results: Vec<BundleState>) -> Self {
self.executor_results = executor_results;
self
}
Expand Down
9 changes: 6 additions & 3 deletions crates/payload/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use reth_primitives::{
};
use reth_provider::{BlockReaderIdExt, BlockSource, BundleState, StateProviderFactory};
use reth_revm::{
database::State, env::tx_env_with_recovered, into_reth_log, revm::State as RevmState,
database::State,
env::tx_env_with_recovered,
into_reth_log,
revm::{State as RevmState, StateBuilder as RevmStateBuilder},
state_change::post_block_withdrawals_balance_increments,
};
use reth_rlp::Encodable;
Expand Down Expand Up @@ -572,7 +575,7 @@ fn build_payload<Pool, Client>(

let state = State::new(client.state_by_block_hash(parent_block.hash)?);
let wrapped_state = WrapDatabaseRef(cached_reads.as_db(&state));
let mut db = RevmState::new_with_transition(Box::new(wrapped_state));
let mut db = RevmStateBuilder::default().with_database(Box::new(wrapped_state)).build();

let mut cumulative_gas_used = 0;
let block_gas_limit: u64 = initialized_block_env.gas_limit.try_into().unwrap_or(u64::MAX);
Expand Down Expand Up @@ -748,7 +751,7 @@ where
debug!(parent_hash=?parent_block.hash, parent_number=parent_block.number, "building empty payload");

let state = client.state_by_block_hash(parent_block.hash)?;
let mut db = RevmState::new_with_transition(Box::new(State::new(&state)));
let mut db = RevmStateBuilder::default().with_database(Box::new(State::new(&state))).build();

let base_fee = initialized_block_env.basefee.to::<u64>();
let block_number = initialized_block_env.number.to::<u64>();
Expand Down
10 changes: 7 additions & 3 deletions crates/revm/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use reth_primitives::{
TransactionSigned, H256, U256,
};
use reth_provider::{change::BundleState, BlockExecutor, StateProvider};
use revm::{primitives::ResultAndState, DatabaseCommit, State as RevmState, EVM};
use revm::{
primitives::ResultAndState, DatabaseCommit, State as RevmState,
StateBuilder as RevmStateBuilder, EVM,
};
use std::sync::Arc;
use tracing::{debug, trace};

Expand Down Expand Up @@ -50,8 +53,9 @@ impl<'a> EVMProcessor<'a> {
/// Creates a new executor from the given chain spec and database.
pub fn new<DB: StateProvider + 'a>(chain_spec: Arc<ChainSpec>, db: State<DB>) -> Self {
let mut evm = EVM::new();
let mut revm_state = RevmState::new_with_transition(Box::new(db));
revm_state.set_state_clear_flag(false);
let revm_state =
RevmStateBuilder::default().with_database(Box::new(db)).without_state_clear().build();

evm.database(revm_state);

EVMProcessor {
Expand Down
25 changes: 18 additions & 7 deletions crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use reth_revm::{
env::tx_env_with_recovered,
revm::{
primitives::{db::DatabaseCommit, BlockEnv, CfgEnv, Env},
State as RevmState,
State as RevmState, StateBuilder as RevmStateBuilder,
},
tracing::{
js::{JsDbRequest, JsInspector},
Expand Down Expand Up @@ -106,7 +106,10 @@ where
let this = self.clone();
self.inner.eth_api.with_state_at_block(at, move |state| {
let mut results = Vec::with_capacity(transactions.len());
let mut db = RevmState::new_without_transitions(Box::new(State::new(state)));
let mut db = RevmStateBuilder::default()
.with_database(Box::new(State::new(state)))
.without_bundle_update()
.build();

let mut transactions = transactions.into_iter().peekable();
while let Some(tx) = transactions.next() {
Expand Down Expand Up @@ -224,7 +227,10 @@ where
let tx = transaction.into_recovered();

let provider = Box::new(State::new(state));
let mut db = RevmState::new_without_transitions(provider);
let mut db = RevmStateBuilder::default()
.with_database(provider)
.without_bundle_update()
.build();
// replay all transactions prior to the targeted transaction
replay_transactions_until::<RevmState<'_, Error>, _, _>(
&mut db,
Expand Down Expand Up @@ -315,7 +321,10 @@ where
// because JSTracer and all JS types are not Send
let (cfg, block_env, at) = self.inner.eth_api.evm_env_at(at).await?;
let state = self.inner.eth_api.state_at(at)?;
let mut db = RevmState::new_without_transitions(Box::new(State::new(state)));
let mut db = RevmStateBuilder::default()
.with_database(Box::new(State::new(state)))
.without_bundle_update()
.build();
let has_state_overrides = overrides.has_state();
let env = prepare_call_env(cfg, block_env, call, &mut db, overrides)?;

Expand Down Expand Up @@ -474,11 +483,13 @@ where
}
};

let database = Box::new(State::new(state));

let mut db = if let Some(db) = db {
let RevmState { cache, transition_builder, .. } = db;
RevmState { cache, transition_builder, database: Box::new(State::new(state)) }
let RevmState { cache, use_preloaded_bundle, transition_state, bundle_state, .. } = db;
RevmState { cache, transition_state, bundle_state, database, use_preloaded_bundle }
} else {
RevmState::new_with_transition(Box::new(State::new(state)))
RevmStateBuilder::default().with_database(database).build()
};

let mut stream = ReceiverStream::new(rx);
Expand Down
16 changes: 13 additions & 3 deletions crates/rpc/rpc/src/eth/api/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ use ethers_core::utils::get_contract_address;
use reth_network_api::NetworkInfo;
use reth_primitives::{AccessList, BlockId, BlockNumberOrTag, Bytes, U256};
use reth_provider::{BlockReaderIdExt, EvmEnvProvider, StateProvider, StateProviderFactory};
use reth_revm::{access_list::AccessListInspector, database::State, revm::State as RevmState};
use reth_revm::{
access_list::AccessListInspector,
database::State,
revm::{State as RevmState, StateBuilder as RevmStateBuilder},
};
use reth_rpc_types::CallRequest;
use reth_transaction_pool::TransactionPool;
use revm::{
Expand Down Expand Up @@ -92,7 +96,10 @@ where

// Configure the evm env
let mut env = build_call_evm_env(cfg, block, request)?;
let mut db = RevmState::new_without_transitions(Box::new(State::new(state)));
let mut db = RevmStateBuilder::default()
.with_database(Box::new(State::new(state)))
.without_bundle_update()
.build();

// if the request is a simple transfer we can optimize
// TODO(rakita) revm state
Expand Down Expand Up @@ -252,7 +259,10 @@ where
// <https://github.com/ethereum/go-ethereum/blob/8990c92aea01ca07801597b00c0d83d4e2d9b811/internal/ethapi/api.go#L1476-L1476>
env.cfg.disable_base_fee = true;

let mut db = RevmState::new_without_transitions(Box::new(State::new(state)));
let mut db = RevmStateBuilder::default()
.with_database(Box::new(State::new(state)))
.without_bundle_update()
.build();

if request.gas.is_none() && env.tx.gas_price > U256::ZERO {
// no gas limit was provided in the request, so we need to cap the request's gas limit
Expand Down
27 changes: 21 additions & 6 deletions crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use reth_provider::{BlockReaderIdExt, EvmEnvProvider, StateProviderBox, StatePro
use reth_revm::{
database::State,
env::{fill_block_env_with_coinbase, tx_env_with_recovered},
revm::State as RevmState,
revm::{State as RevmState, StateBuilder as RevmStateBuilder},
tracing::{TracingInspector, TracingInspectorConfig},
};
use reth_rpc_types::{
Expand Down Expand Up @@ -501,7 +501,10 @@ where
{
let (cfg, block_env, at) = self.evm_env_at(at).await?;
let state = self.state_at(at)?;
let mut db = RevmState::new_without_transitions(Box::new(State::new(state)));
let mut db = RevmStateBuilder::default()
.with_database(Box::new(State::new(state)))
.without_bundle_update()
.build();

let env = prepare_call_env(cfg, block_env, request, &mut db, overrides)?;
f(db, env)
Expand Down Expand Up @@ -541,7 +544,10 @@ where
{
let (cfg, block_env, at) = self.evm_env_at(at).await?;
let state = self.state_at(at)?;
let mut db = RevmState::new_without_transitions(Box::new(State::new(state)));
let mut db = RevmStateBuilder::default()
.with_database(Box::new(State::new(state)))
.without_bundle_update()
.build();

let env = prepare_call_env(cfg, block_env, request, &mut db, overrides)?;
inspect_and_return_db(db, env, inspector)
Expand All @@ -558,7 +564,10 @@ where
F: FnOnce(TracingInspector, ResultAndState) -> EthResult<R>,
{
self.with_state_at_block(at, |state| {
let db = RevmState::new_without_transitions(Box::new(State::new(state)));
let db = RevmStateBuilder::default()
.with_database(Box::new(State::new(state)))
.without_bundle_update()
.build();

let mut inspector = TracingInspector::new(config);
let (res, _) = inspect(db, env, &mut inspector)?;
Expand All @@ -578,7 +587,10 @@ where
F: for<'a> FnOnce(TracingInspector, ResultAndState, StateCacheDB<'a>) -> EthResult<R>,
{
self.with_state_at_block(at, |state| {
let db = RevmState::new_without_transitions(Box::new(State::new(state)));
let db = RevmStateBuilder::default()
.with_database(Box::new(State::new(state)))
.without_bundle_update()
.build();
let mut inspector = TracingInspector::new(config);
let (res, _, db) = inspect_and_return_db(db, env, &mut inspector)?;

Expand Down Expand Up @@ -633,7 +645,10 @@ where
let block_txs = block.body;

self.with_state_at_block(parent_block.into(), |state| {
let mut db = RevmState::new_without_transitions(Box::new(State::new(state)));
let mut db = RevmStateBuilder::default()
.with_database(Box::new(State::new(state)))
.without_bundle_update()
.build();

// replay all transactions prior to the targeted transaction
replay_transactions_until::<StateCacheDB<'_>, _, _>(
Expand Down
4 changes: 3 additions & 1 deletion crates/rpc/rpc/src/eth/revm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,9 @@ pub(crate) fn clone_into_empty_db<DBError: Send + 'static>(
RevmState {
cache: db.cache.clone(),
database,
transition_builder: db.transition_builder.clone(),
transition_state: db.transition_state.clone(),
bundle_state: db.bundle_state.clone(),
use_preloaded_bundle: db.use_preloaded_bundle,
}
}

Expand Down
16 changes: 13 additions & 3 deletions crates/rpc/rpc/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ use reth_rpc_types::{
BlockError, BlockOverrides, CallRequest, Index, TransactionInfo,
};
use reth_tasks::TaskSpawner;
use revm::{db::State as RevmState, primitives::Env, DatabaseCommit};
use revm::{
db::{State as RevmState, StateBuilder as RevmStateBuilder},
primitives::Env,
DatabaseCommit,
};
use revm_primitives::{ExecutionResult, ResultAndState};
use std::{collections::HashSet, future::Future, sync::Arc};
use tokio::sync::{oneshot, AcquireError, OwnedSemaphorePermit};
Expand Down Expand Up @@ -194,7 +198,10 @@ where
this.inner.eth_api.with_state_at_block(at, move |state| {
let mut results = Vec::with_capacity(calls.len());
let provider = Box::new(State::new(state));
let mut revm_state = RevmState::new_without_transitions(provider);
let mut revm_state = RevmStateBuilder::default()
.with_database(provider)
.without_bundle_update()
.build();

let mut calls = calls.into_iter().peekable();

Expand Down Expand Up @@ -356,7 +363,10 @@ where
.eth_api
.with_state_at_block(state_at.into(), move |state| {
let mut results = Vec::with_capacity(transactions.len());
let mut db = RevmState::new_without_transitions(Box::new(State::new(state)));
let mut db = RevmStateBuilder::default()
.with_database(Box::new(State::new(state)))
.without_bundle_update()
.build();

let mut transactions = transactions.into_iter().enumerate().peekable();

Expand Down

0 comments on commit aaf94ad

Please sign in to comment.