Skip to content

Commit

Permalink
feat: clean up sync requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Dec 4, 2024
1 parent b4bbfb7 commit 205aa60
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
42 changes: 17 additions & 25 deletions src/db/builder.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use crate::db::ConcurrentState;
use revm::{
db::{
states::{state::DBBox, BundleState, TransitionState},
states::{BundleState, TransitionState},
EmptyDB,
},
primitives::{
db::{Database, DatabaseRef, WrapDatabaseRef},
B256,
},
primitives::{db::DatabaseRef, B256},
};
use std::collections::BTreeMap;

use super::{ConcurrentCacheState, ConcurrentStateInfo};

/// Allows building of State and initializing it with different options.
#[derive(Clone, Debug)]
pub struct ConcurrentStateBuilder<DB> {
pub struct ConcurrentStateBuilder<Db> {
/// Database that we use to fetch data from.
database: DB,
database: Db,
/// Enabled state clear flag that is introduced in Spurious Dragon hardfork.
/// Default is true as spurious dragon happened long time ago.
with_state_clear: bool,
Expand Down Expand Up @@ -47,15 +44,15 @@ impl ConcurrentStateBuilder<EmptyDB> {
}
}

impl<DB: DatabaseRef + Default> Default for ConcurrentStateBuilder<DB> {
impl<Db: DatabaseRef + Sync + Default> Default for ConcurrentStateBuilder<Db> {
fn default() -> Self {
Self::new_with_database(DB::default())
Self::new_with_database(Db::default())
}
}

impl<DB: DatabaseRef> ConcurrentStateBuilder<DB> {
impl<Db: DatabaseRef + Sync> ConcurrentStateBuilder<Db> {
/// Create a new builder with the given database.
pub const fn new_with_database(database: DB) -> Self {
pub const fn new_with_database(database: Db) -> Self {
Self {
database,
with_state_clear: true,
Expand All @@ -68,7 +65,10 @@ impl<DB: DatabaseRef> ConcurrentStateBuilder<DB> {
}

/// Set the database.
pub fn with_database<ODB: Database>(self, database: ODB) -> ConcurrentStateBuilder<ODB> {
pub fn with_database<ODb: DatabaseRef + Sync>(
self,
database: ODb,
) -> ConcurrentStateBuilder<ODb> {
// cast to the different database,
// Note that we return different type depending of the database NewDBError.
ConcurrentStateBuilder {
Expand All @@ -82,19 +82,11 @@ impl<DB: DatabaseRef> ConcurrentStateBuilder<DB> {
}
}

/// Takes [DatabaseRef] and wraps it with [WrapDatabaseRef].
pub fn with_database_ref<ODB: DatabaseRef>(
self,
database: ODB,
) -> ConcurrentStateBuilder<WrapDatabaseRef<ODB>> {
self.with_database(WrapDatabaseRef(database))
}

/// With boxed version of database.
pub fn with_database_boxed<Error>(
/// Alias for [`Self::with_database`], for revm compat reasons.
pub fn with_database_ref<ODb: DatabaseRef + Sync>(
self,
database: DBBox<'_, Error>,
) -> ConcurrentStateBuilder<DBBox<'_, Error>> {
database: ODb,
) -> ConcurrentStateBuilder<ODb> {
self.with_database(database)
}

Expand Down Expand Up @@ -141,7 +133,7 @@ impl<DB: DatabaseRef> ConcurrentStateBuilder<DB> {
}

/// Build the concurrent state.
pub fn build(mut self) -> ConcurrentState<DB> {
pub fn build(mut self) -> ConcurrentState<Db> {
let use_preloaded_bundle = if self.with_cache_prestate.is_some() {
self.with_bundle_prestate = None;
false
Expand Down
5 changes: 3 additions & 2 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod builder;

pub use builder::ConcurrentStateBuilder;

mod cache_state;
Expand All @@ -13,7 +14,7 @@ use revm::{
DatabaseRef,
};

impl<Ext, Db: DatabaseRef, TrevmState> Trevm<'_, Ext, ConcurrentState<Db>, TrevmState> {
impl<Ext, Db: DatabaseRef + Sync, TrevmState> Trevm<'_, Ext, ConcurrentState<Db>, TrevmState> {
/// Set the [EIP-161] state clear flag, activated in the Spurious Dragon
/// hardfork.
///
Expand All @@ -23,7 +24,7 @@ impl<Ext, Db: DatabaseRef, TrevmState> Trevm<'_, Ext, ConcurrentState<Db>, Trevm
}
}

impl<Ext, Db: DatabaseRef> EvmNeedsBlock<'_, Ext, ConcurrentState<Db>> {
impl<Ext, Db: DatabaseRef + Sync> EvmNeedsBlock<'_, Ext, ConcurrentState<Db>> {
/// Finish execution and return the outputs.
///
/// If the State has not been built with
Expand Down
10 changes: 5 additions & 5 deletions src/db/sync_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct ConcurrentStateInfo {
pub block_hashes: RwLock<BTreeMap<u64, B256>>,
}

impl<Db: DatabaseRef> ConcurrentState<Db> {
impl<Db: DatabaseRef + Sync> ConcurrentState<Db> {
/// Create a new [`ConcurrentState`] with the given database and cache
/// state.
pub const fn new(database: Db, info: ConcurrentStateInfo) -> Self {
Expand Down Expand Up @@ -243,8 +243,8 @@ impl<Db: DatabaseRef> ConcurrentState<Db> {
}
}

impl<DB: DatabaseRef> DatabaseRef for ConcurrentState<DB> {
type Error = DB::Error;
impl<Db: DatabaseRef + Sync> DatabaseRef for ConcurrentState<Db> {
type Error = Db::Error;

fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
self.load_cache_account_mut(address).map(|a| a.account_info())
Expand Down Expand Up @@ -325,14 +325,14 @@ impl<DB: DatabaseRef> DatabaseRef for ConcurrentState<DB> {
}
}

impl<DB: DatabaseRef> DatabaseCommit for ConcurrentState<DB> {
impl<Db: DatabaseRef + Sync> DatabaseCommit for ConcurrentState<Db> {
fn commit(&mut self, evm_state: revm::primitives::HashMap<Address, Account>) {
let transitions = self.info.cache.apply_evm_state(evm_state);
self.apply_transition(transitions);
}
}

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

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

0 comments on commit 205aa60

Please sign in to comment.