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

ConcurrentState #66

Merged
merged 9 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ revm = { version = "18.0.0", default-features = false }

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

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

[dev-dependencies]
alloy-rlp = { version = "0.3", default-features = false }
revm = { version = "18.0.0", features = [
Expand All @@ -56,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 @@ -65,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
169 changes: 169 additions & 0 deletions src/db/cache_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
use core::sync::atomic::AtomicBool;

use alloy_primitives::{Address, B256};
use dashmap::DashMap;
use revm::{
db::states::{plain_account::PlainStorage, CacheAccount},
primitives::{Account, AccountInfo, Bytecode, EvmState},
TransitionAccount,
};

/// A concurrent version of [`revm::db::CacheState`].
///
/// Most of the code for this has been reproduced from revm.
#[derive(Debug)]
pub struct ConcurrentCacheState {
/// Block state account with account state.
pub accounts: DashMap<Address, CacheAccount>,
/// Created contracts.
// TODO add bytecode counter for number of bytecodes added/removed.
pub contracts: DashMap<B256, Bytecode>,
/// Has EIP-161 state clear enabled (Spurious Dragon hardfork).
pub has_state_clear: AtomicBool,
}

impl Default for ConcurrentCacheState {
fn default() -> Self {
Self::new(true)
}
}

impl ConcurrentCacheState {
/// New default state.
pub fn new(has_state_clear: bool) -> Self {
Self {
accounts: DashMap::default(),
contracts: DashMap::default(),
has_state_clear: has_state_clear.into(),
}
}

/// Set state clear flag. EIP-161.
pub fn set_state_clear_flag(&self, has_state_clear: bool) {
self.has_state_clear.store(has_state_clear, core::sync::atomic::Ordering::Relaxed);
}

/// Insert not existing account.
pub fn insert_not_existing(&self, address: Address) {
self.accounts.insert(address, CacheAccount::new_loaded_not_existing());
}

/// Insert Loaded (Or LoadedEmptyEip161 if account is empty) account.
pub fn insert_account(&self, address: Address, info: AccountInfo) {
let account = if !info.is_empty() {
CacheAccount::new_loaded(info, PlainStorage::default())
} else {
CacheAccount::new_loaded_empty_eip161(PlainStorage::default())
};
self.accounts.insert(address, account);
}

/// Similar to `insert_account` but with storage.
pub fn insert_account_with_storage(
&self,
address: Address,
info: AccountInfo,
storage: PlainStorage,
) {
let account = if !info.is_empty() {
CacheAccount::new_loaded(info, storage)
} else {
CacheAccount::new_loaded_empty_eip161(storage)
};
self.accounts.insert(address, account);
}

/// Apply output of revm execution and create account transitions that are used to build BundleState.
pub fn apply_evm_state(&self, evm_state: EvmState) -> Vec<(Address, TransitionAccount)> {
let mut transitions = Vec::with_capacity(evm_state.len());
for (address, account) in evm_state {
if let Some(transition) = self.apply_account_state(address, account) {
transitions.push((address, transition));
}
}
transitions
}

/// Apply updated account state to the cached account.
/// Returns account transition if applicable.
fn apply_account_state(&self, address: Address, account: Account) -> Option<TransitionAccount> {
// not touched account are never changed.
if !account.is_touched() {
return None;
}

let mut this_account =
self.accounts.get_mut(&address).expect("All accounts should be present inside cache");

// If it is marked as selfdestructed inside revm
// we need to changed state to destroyed.
if account.is_selfdestructed() {
return this_account.selfdestruct();
}

let is_created = account.is_created();
let is_empty = account.is_empty();

// transform evm storage to storage with previous value.
let changed_storage = account
.storage
.into_iter()
.filter(|(_, slot)| slot.is_changed())
.map(|(key, slot)| (key, slot.into()))
.collect();

// Note: it can happen that created contract get selfdestructed in same block
// that is why is_created is checked after selfdestructed
//
// Note: Create2 opcode (Petersburg) was after state clear EIP (Spurious Dragon)
//
// Note: It is possibility to create KECCAK_EMPTY contract with some storage
// by just setting storage inside CRATE constructor. Overlap of those contracts
// is not possible because CREATE2 is introduced later.
if is_created {
return Some(this_account.newly_created(account.info, changed_storage));
}

// Account is touched, but not selfdestructed or newly created.
// Account can be touched and not changed.
// And when empty account is touched it needs to be removed from database.
// EIP-161 state clear
if is_empty {
if self.has_state_clear.load(core::sync::atomic::Ordering::Relaxed) {
// touch empty account.
this_account.touch_empty_eip161()
} else {
// if account is empty and state clear is not enabled we should save
// empty account.
this_account.touch_create_pre_eip161(changed_storage)
}
} else {
Some(this_account.change(account.info, changed_storage))
}
}
}

// Some code above and documentation is adapted from the revm crate, and is
// reproduced here under the terms of the MIT license.
//
// MIT License
//
// Copyright (c) 2021-2024 draganrakita
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
36 changes: 36 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
mod sync_state;
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
}
}
Loading
Loading