Skip to content

Commit

Permalink
chore: make it a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Dec 2, 2024
1 parent d08659b commit 0cdc489
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ alloy = { version = "=0.5.4", default-features = false, features = ["consensus",
revm = { version = "18.0.0", default-features = false }

zenith-types = { version = "0.10", optional = true }
dashmap = "6.1.0"

dashmap = { version = "6.1.0", optional = true }

[dev-dependencies]
alloy-rlp = { version = "0.3", default-features = false }
Expand All @@ -57,6 +58,7 @@ tokio = { version = "1.39", features = ["macros", "rt-multi-thread"] }
[features]
default = [
"std",
"concurrent-db",
"revm/std",
"revm/c-kzg",
"revm/blst",
Expand All @@ -66,6 +68,8 @@ default = [

std = ["revm/std", "alloy/std", "alloy-rlp/std", "alloy-primitives/std", "alloy-sol-types/std", "dep:zenith-types"]

concurrent-db = ["std", "dep:dashmap"]

test-utils = ["revm/test-utils", "revm/std", "revm/serde-json", "revm/alloydb"]

secp256k1 = ["revm/secp256k1"]
Expand Down
31 changes: 31 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,34 @@ pub use sync_state::{ConcurrentState, StateInfo};

mod cache_state;
pub use cache_state::ConcurrentCacheState;

use crate::{EvmNeedsBlock, Trevm};
use revm::{
db::{states::bundle_state::BundleRetention, BundleState},
DatabaseRef,
};

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)
}
}

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
}
}
6 changes: 2 additions & 4 deletions src/db/sync_state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::db::ConcurrentCacheState;
use alloc::{collections::BTreeMap, vec::Vec};
use alloy_primitives::{Address, B256, U256};
use dashmap::mapref::one::RefMut;
use revm::{
Expand All @@ -9,10 +10,7 @@ use revm::{
primitives::{Account, AccountInfo, Bytecode},
Database, DatabaseCommit, DatabaseRef, TransitionAccount, TransitionState,
};
use std::{
collections::{hash_map, BTreeMap},
sync::RwLock,
};
use std::{collections::hash_map, sync::RwLock};

/// State of blockchain.
///
Expand Down
41 changes: 8 additions & 33 deletions src/evm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
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,
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 All @@ -20,8 +20,8 @@ use revm::{
///
/// See the [crate-level documentation](crate) for more information.
pub struct Trevm<'a, Ext, Db: Database + DatabaseCommit, TrevmState> {
inner: Box<Evm<'a, Ext, Db>>,
state: TrevmState,
pub(crate) inner: Box<Evm<'a, Ext, Db>>,
pub(crate) state: TrevmState,
}

impl<Ext, Db: Database + DatabaseCommit, TrevmState> fmt::Debug for Trevm<'_, Ext, Db, TrevmState> {
Expand Down Expand Up @@ -440,7 +440,7 @@ impl<Ext, Db: Database<Error = Infallible> + DatabaseCommit, TrevmState>
}
}

// --- ALL STATES, WITH State<Db> or ConcurrentState<Db>
// --- ALL STATES, WITH State<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,14 +450,6 @@ 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 @@ -886,7 +878,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasBlock> Trevm<'a, Ext
}
}

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

impl<Ext, Db: Database> EvmNeedsBlock<'_, Ext, State<Db>> {
/// Finish execution and return the outputs.
Expand All @@ -905,23 +897,6 @@ 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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ mod connect;
pub use connect::{DbConnect, EvmFactory};

/// Contains database implementations for concurrent EVM operation.
#[cfg(feature = "concurrent-db")]
pub mod db;

mod driver;
Expand Down

0 comments on commit 0cdc489

Please sign in to comment.