Skip to content

Commit

Permalink
chore: integrate into trevm
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Dec 2, 2024
1 parent 1ad8681 commit 506dcc1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
22 changes: 21 additions & 1 deletion src/db/sync_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use revm::{
BundleState,
},
primitives::{Account, AccountInfo, Bytecode},
DatabaseCommit, DatabaseRef, TransitionAccount, TransitionState,
Database, DatabaseCommit, DatabaseRef, TransitionAccount, TransitionState,
};
use std::{
collections::{hash_map, BTreeMap},
Expand Down Expand Up @@ -300,6 +300,26 @@ impl<DB: DatabaseRef> DatabaseCommit for ConcurrentState<DB> {
}
}

impl<Db: DatabaseRef> Database for ConcurrentState<Db> {
type Error = <Self as DatabaseRef>::Error;

fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
self.basic_ref(address)
}

fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
self.code_by_hash_ref(code_hash)
}

fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
self.storage_ref(address, index)
}

fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
self.block_hash_ref(number)
}
}

// Some code above and documentation is adapted from the revm crate, and is
// reproduced here under the terms of the MIT license.
//
Expand Down
37 changes: 31 additions & 6 deletions src/evm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
driver::DriveBlockResult, Block, BlockDriver, BundleDriver, Cfg, ChainDriver,
DriveBundleResult, DriveChainResult, ErroredState, EvmErrored, EvmExtUnchecked, EvmNeedsBlock,
EvmNeedsCfg, EvmNeedsTx, EvmReady, EvmTransacted, HasBlock, HasCfg, HasTx, NeedsCfg, NeedsTx,
TransactedState, Tx,
db::ConcurrentState, driver::DriveBlockResult, Block, BlockDriver, BundleDriver, Cfg,
ChainDriver, DriveBundleResult, DriveChainResult, ErroredState, EvmErrored, EvmExtUnchecked,
EvmNeedsBlock, EvmNeedsCfg, EvmNeedsTx, EvmReady, EvmTransacted, HasBlock, HasCfg, HasTx,
NeedsCfg, NeedsTx, TransactedState, Tx,
};
use alloc::{boxed::Box, fmt};
use alloy_primitives::{Address, Bytes, U256};
Expand Down Expand Up @@ -440,7 +440,7 @@ impl<Ext, Db: Database<Error = Infallible> + DatabaseCommit, TrevmState>
}
}

// --- ALL STATES, WITH STATE<DB>
// --- ALL STATES, WITH State<Db> or ConcurrentState<Db>

impl<Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'_, Ext, State<Db>, TrevmState> {
/// Set the [EIP-161] state clear flag, activated in the Spurious Dragon
Expand All @@ -450,6 +450,14 @@ impl<Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'_, Ext, State<Db>, T
}
}

impl<Ext, Db: DatabaseRef, TrevmState> Trevm<'_, Ext, ConcurrentState<Db>, TrevmState> {
/// Set the [EIP-161] state clear flag, activated in the Spurious Dragon
/// hardfork.
pub fn set_state_clear_flag(&mut self, flag: bool) {
self.inner.db_mut().set_state_clear_flag(flag)
}
}

// --- NEEDS CFG

impl<'a, Ext, Db: Database + DatabaseCommit> EvmNeedsCfg<'a, Ext, Db> {
Expand Down Expand Up @@ -878,7 +886,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasBlock> Trevm<'a, Ext
}
}

// --- Needs Block with State<Db>
// --- Needs Block with State<Db> or ConcurrentState<Db>

impl< Ext, Db: Database> EvmNeedsBlock<'_, Ext, State<Db>> {
/// Finish execution and return the outputs.
Expand All @@ -897,6 +905,23 @@ impl< Ext, Db: Database> EvmNeedsBlock<'_, Ext, State<Db>> {
}
}

impl<'a, Ext, Db: DatabaseRef> EvmNeedsBlock<'a, Ext, ConcurrentState<Db>> {
/// Finish execution and return the outputs.
///
/// ## Panics
///
/// If the State has not been built with StateBuilder::with_bundle_update.
///
/// See [`State::merge_transitions`] and [`State::take_bundle`].
pub fn finish(self) -> BundleState {
let Self { inner: mut evm, .. } = self;
evm.db_mut().merge_transitions(BundleRetention::Reverts);
let bundle = evm.db_mut().take_bundle();

bundle
}
}

// --- NEEDS TX

impl<'a, Ext, Db: Database + DatabaseCommit> EvmNeedsTx<'a, Ext, Db> {
Expand Down

0 comments on commit 506dcc1

Please sign in to comment.