From b5b15f03a08dc89b2a1a11552525d57dcbc94719 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Wed, 19 Jun 2024 18:42:54 +0200 Subject: [PATCH] chore: remove `revm/compat` from `reth-primitives` (#8960) --- .../execution-types/src/execution_outcome.rs | 14 ++++------ crates/primitives-traits/src/account.rs | 24 +++++++++++++++- crates/primitives/src/revm/compat.rs | 28 ------------------- crates/primitives/src/revm/mod.rs | 9 ------ .../bundle_state_with_receipts.rs | 28 +++++++++---------- .../src/bundle_state/state_changes.rs | 4 +-- .../src/bundle_state/state_reverts.rs | 4 +-- .../storage/provider/src/test_utils/blocks.rs | 8 +++--- crates/trie/trie/benches/hash_post_state.rs | 4 +-- crates/trie/trie/src/state.rs | 6 ++-- 10 files changed, 54 insertions(+), 75 deletions(-) delete mode 100644 crates/primitives/src/revm/compat.rs diff --git a/crates/evm/execution-types/src/execution_outcome.rs b/crates/evm/execution-types/src/execution_outcome.rs index 8838c86d2..e7e861cf1 100644 --- a/crates/evm/execution-types/src/execution_outcome.rs +++ b/crates/evm/execution-types/src/execution_outcome.rs @@ -1,8 +1,6 @@ use reth_primitives::{ - logs_bloom, - revm::compat::{into_reth_acc, into_revm_acc}, - Account, Address, BlockNumber, Bloom, Bytecode, Log, Receipt, Receipts, Requests, StorageEntry, - B256, U256, + logs_bloom, Account, Address, BlockNumber, Bloom, Bytecode, Log, Receipt, Receipts, Requests, + StorageEntry, B256, U256, }; use reth_trie::HashedPostState; use revm::{ @@ -81,8 +79,8 @@ impl ExecutionOutcome { state_init.into_iter().map(|(address, (original, present, storage))| { ( address, - original.map(into_revm_acc), - present.map(into_revm_acc), + original.map(Into::into), + present.map(Into::into), storage.into_iter().map(|(k, s)| (k.into(), s)).collect(), ) }), @@ -91,7 +89,7 @@ impl ExecutionOutcome { reverts.into_iter().map(|(address, (original, storage))| { ( address, - original.map(|i| i.map(into_revm_acc)), + original.map(|i| i.map(Into::into)), storage.into_iter().map(|entry| (entry.key.into(), entry.value)), ) }) @@ -129,7 +127,7 @@ impl ExecutionOutcome { /// Get account if account is known. pub fn account(&self, address: &Address) -> Option> { - self.bundle.account(address).map(|a| a.info.clone().map(into_reth_acc)) + self.bundle.account(address).map(|a| a.info.clone().map(Into::into)) } /// Get storage if value is known. diff --git a/crates/primitives-traits/src/account.rs b/crates/primitives-traits/src/account.rs index df32be1dc..21a8d199b 100644 --- a/crates/primitives-traits/src/account.rs +++ b/crates/primitives-traits/src/account.rs @@ -5,7 +5,7 @@ use byteorder::{BigEndian, ReadBytesExt}; use bytes::Buf; use derive_more::Deref; use reth_codecs::{main_codec, Compact}; -use revm_primitives::{Bytecode as RevmBytecode, JumpTable}; +use revm_primitives::{AccountInfo, Bytecode as RevmBytecode, JumpTable}; use serde::{Deserialize, Serialize}; /// An Ethereum account. @@ -122,6 +122,28 @@ impl Compact for Bytecode { } } +impl From for Account { + fn from(revm_acc: AccountInfo) -> Self { + let code_hash = revm_acc.code_hash; + Self { + balance: revm_acc.balance, + nonce: revm_acc.nonce, + bytecode_hash: (code_hash != KECCAK_EMPTY).then_some(code_hash), + } + } +} + +impl From for AccountInfo { + fn from(reth_acc: Account) -> Self { + Self { + balance: reth_acc.balance, + nonce: reth_acc.nonce, + code_hash: reth_acc.bytecode_hash.unwrap_or(KECCAK_EMPTY), + code: None, + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/primitives/src/revm/compat.rs b/crates/primitives/src/revm/compat.rs deleted file mode 100644 index 808ac98bb..000000000 --- a/crates/primitives/src/revm/compat.rs +++ /dev/null @@ -1,28 +0,0 @@ -use crate::{revm_primitives::AccountInfo, Account, KECCAK_EMPTY}; - -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; - -/// Converts a Revm [`AccountInfo`] into a Reth [`Account`]. -/// -/// Sets `bytecode_hash` to `None` if `code_hash` is [`KECCAK_EMPTY`]. -pub fn into_reth_acc(revm_acc: AccountInfo) -> Account { - let code_hash = revm_acc.code_hash; - Account { - balance: revm_acc.balance, - nonce: revm_acc.nonce, - bytecode_hash: (code_hash != KECCAK_EMPTY).then_some(code_hash), - } -} - -/// Converts a Revm [`AccountInfo`] into a Reth [`Account`]. -/// -/// Sets `code_hash` to [`KECCAK_EMPTY`] if `bytecode_hash` is `None`. -pub fn into_revm_acc(reth_acc: Account) -> AccountInfo { - AccountInfo { - balance: reth_acc.balance, - nonce: reth_acc.nonce, - code_hash: reth_acc.bytecode_hash.unwrap_or(KECCAK_EMPTY), - code: None, - } -} diff --git a/crates/primitives/src/revm/mod.rs b/crates/primitives/src/revm/mod.rs index f3c4ac62d..9937a209b 100644 --- a/crates/primitives/src/revm/mod.rs +++ b/crates/primitives/src/revm/mod.rs @@ -1,14 +1,5 @@ //! Helpers for working with revm. -/// The `compat` module contains utility functions that perform conversions between reth and revm, -/// compare analogous types from the two implementations, and calculate intrinsic gas usage. -/// -/// The included conversion methods can be used to convert between: -/// * reth's [Log](crate::Log) type and revm's [Log](revm_primitives::Log) type. -/// * reth's [Account](crate::Account) type and revm's [`AccountInfo`](revm_primitives::AccountInfo) -/// type. -pub mod compat; - /// Reth block execution/validation configuration and constants pub mod config; diff --git a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs index a790d92fc..4d3b92b05 100644 --- a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs +++ b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs @@ -76,9 +76,7 @@ mod tests { models::{AccountBeforeTx, BlockNumberAddress}, }; use reth_primitives::{ - keccak256, - revm::compat::{into_reth_acc, into_revm_acc}, - Account, Address, Receipt, Receipts, StorageEntry, B256, U256, + keccak256, Account, Address, Receipt, Receipts, StorageEntry, B256, U256, }; use reth_trie::{test_utils::state_root, StateRoot}; use revm::{ @@ -149,9 +147,9 @@ mod tests { .write_to_db(provider.tx_ref(), 1) .expect("Could not write reverts to DB"); - let reth_account_a = into_reth_acc(account_a); - let reth_account_b = into_reth_acc(account_b); - let reth_account_b_changed = into_reth_acc(account_b_changed.clone()); + let reth_account_a = account_a.into(); + let reth_account_b = account_b.into(); + let reth_account_b_changed = account_b_changed.clone().into(); // Check plain state assert_eq!( @@ -926,7 +924,7 @@ mod tests { // destroy account 1 let address1 = Address::with_last_byte(1); let account1_old = prestate.remove(&address1).unwrap(); - state.insert_account(address1, into_revm_acc(account1_old.0)); + state.insert_account(address1, account1_old.0.into()); state.commit(HashMap::from([( address1, RevmAccount { @@ -946,7 +944,7 @@ mod tests { let account2_slot2_old_value = *account2.1.get(&slot2_key).unwrap(); state.insert_account_with_storage( address2, - into_revm_acc(account2.0), + account2.0.into(), HashMap::from([(slot2, account2_slot2_old_value)]), ); @@ -956,7 +954,7 @@ mod tests { address2, RevmAccount { status: AccountStatus::Touched, - info: into_revm_acc(account2.0), + info: account2.0.into(), storage: HashMap::from_iter([( slot2, EvmStorageSlot::new_changed(account2_slot2_old_value, account2_slot2_new_value), @@ -969,14 +967,14 @@ mod tests { // change balance of account 3 let address3 = Address::with_last_byte(3); let account3 = prestate.get_mut(&address3).unwrap(); - state.insert_account(address3, into_revm_acc(account3.0)); + state.insert_account(address3, account3.0.into()); account3.0.balance = U256::from(24); state.commit(HashMap::from([( address3, RevmAccount { status: AccountStatus::Touched, - info: into_revm_acc(account3.0), + info: account3.0.into(), storage: HashMap::default(), }, )])); @@ -986,14 +984,14 @@ mod tests { // change nonce of account 4 let address4 = Address::with_last_byte(4); let account4 = prestate.get_mut(&address4).unwrap(); - state.insert_account(address4, into_revm_acc(account4.0)); + state.insert_account(address4, account4.0.into()); account4.0.nonce = 128; state.commit(HashMap::from([( address4, RevmAccount { status: AccountStatus::Touched, - info: into_revm_acc(account4.0), + info: account4.0.into(), storage: HashMap::default(), }, )])); @@ -1008,7 +1006,7 @@ mod tests { address1, RevmAccount { status: AccountStatus::Touched | AccountStatus::Created, - info: into_revm_acc(account1_new), + info: account1_new.into(), storage: HashMap::default(), }, )])); @@ -1024,7 +1022,7 @@ mod tests { address1, RevmAccount { status: AccountStatus::Touched | AccountStatus::Created, - info: into_revm_acc(account1_new), + info: account1_new.into(), storage: HashMap::from_iter([( slot20, EvmStorageSlot::new_changed(U256::ZERO, account1_slot20_value), diff --git a/crates/storage/provider/src/bundle_state/state_changes.rs b/crates/storage/provider/src/bundle_state/state_changes.rs index 0587f3693..57c3b837f 100644 --- a/crates/storage/provider/src/bundle_state/state_changes.rs +++ b/crates/storage/provider/src/bundle_state/state_changes.rs @@ -4,7 +4,7 @@ use reth_db_api::{ cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW}, transaction::{DbTx, DbTxMut}, }; -use reth_primitives::{revm::compat::into_reth_acc, Bytecode, StorageEntry, U256}; +use reth_primitives::{Bytecode, StorageEntry, U256}; use reth_storage_errors::db::DatabaseError; use revm::db::states::{PlainStorageChangeset, StateChangeset}; @@ -34,7 +34,7 @@ impl StateChanges { for (address, account) in self.0.accounts { if let Some(account) = account { tracing::trace!(target: "provider::bundle_state", ?address, "Updating plain state account"); - accounts_cursor.upsert(address, into_reth_acc(account))?; + accounts_cursor.upsert(address, account.into())?; } else if accounts_cursor.seek_exact(address)?.is_some() { tracing::trace!(target: "provider::bundle_state", ?address, "Deleting plain state account"); accounts_cursor.delete_current()?; diff --git a/crates/storage/provider/src/bundle_state/state_reverts.rs b/crates/storage/provider/src/bundle_state/state_reverts.rs index 3736b5148..d65fcaa82 100644 --- a/crates/storage/provider/src/bundle_state/state_reverts.rs +++ b/crates/storage/provider/src/bundle_state/state_reverts.rs @@ -5,7 +5,7 @@ use reth_db_api::{ models::{AccountBeforeTx, BlockNumberAddress}, transaction::{DbTx, DbTxMut}, }; -use reth_primitives::{revm::compat::into_reth_acc, BlockNumber, StorageEntry, B256, U256}; +use reth_primitives::{BlockNumber, StorageEntry, B256, U256}; use reth_storage_errors::db::DatabaseError; use revm::db::states::{PlainStateReverts, PlainStorageRevert, RevertToSlot}; use std::iter::Peekable; @@ -82,7 +82,7 @@ impl StateReverts { for (address, info) in account_block_reverts { account_changeset_cursor.append_dup( block_number, - AccountBeforeTx { address, info: info.map(into_reth_acc) }, + AccountBeforeTx { address, info: info.map(Into::into) }, )?; } } diff --git a/crates/storage/provider/src/test_utils/blocks.rs b/crates/storage/provider/src/test_utils/blocks.rs index 5fb8beeb2..2a0f900a5 100644 --- a/crates/storage/provider/src/test_utils/blocks.rs +++ b/crates/storage/provider/src/test_utils/blocks.rs @@ -5,9 +5,9 @@ use alloy_rlp::Decodable; use reth_db::tables; use reth_db_api::{database::Database, models::StoredBlockBodyIndices}; use reth_primitives::{ - alloy_primitives, b256, hex_literal::hex, revm::compat::into_reth_acc, Address, BlockNumber, - Bytes, Header, Receipt, Requests, SealedBlock, SealedBlockWithSenders, TxType, Withdrawal, - Withdrawals, B256, U256, + alloy_primitives, b256, hex_literal::hex, Account, Address, BlockNumber, Bytes, Header, + Receipt, Requests, SealedBlock, SealedBlockWithSenders, TxType, Withdrawal, Withdrawals, B256, + U256, }; use reth_trie::root::{state_root_unhashed, storage_root_unhashed}; use revm::{ @@ -119,7 +119,7 @@ fn bundle_state_root(execution_outcome: &ExecutionOutcome) -> B256 { ( address, ( - into_reth_acc(info.clone()), + Into::::into(info.clone()), storage_root_unhashed( account .storage diff --git a/crates/trie/trie/benches/hash_post_state.rs b/crates/trie/trie/benches/hash_post_state.rs index dced866cf..636ce4462 100644 --- a/crates/trie/trie/benches/hash_post_state.rs +++ b/crates/trie/trie/benches/hash_post_state.rs @@ -1,7 +1,7 @@ #![allow(missing_docs, unreachable_pub)] use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use proptest::{prelude::*, strategy::ValueTree, test_runner::TestRunner}; -use reth_primitives::{keccak256, revm::compat::into_reth_acc, Address, B256, U256}; +use reth_primitives::{keccak256, Address, B256, U256}; use reth_trie::{HashedPostState, HashedStorage}; use revm::db::{states::BundleBuilder, BundleAccount}; use std::collections::HashMap; @@ -30,7 +30,7 @@ fn from_bundle_state_seq(state: &HashMap) -> HashedPostS for (address, account) in state { let hashed_address = keccak256(address); - this.accounts.insert(hashed_address, account.info.clone().map(into_reth_acc)); + this.accounts.insert(hashed_address, account.info.clone().map(Into::into)); let hashed_storage = HashedStorage::from_iter( account.status.was_destroyed(), diff --git a/crates/trie/trie/src/state.rs b/crates/trie/trie/src/state.rs index 13f91a697..821dcc971 100644 --- a/crates/trie/trie/src/state.rs +++ b/crates/trie/trie/src/state.rs @@ -12,9 +12,7 @@ use reth_db_api::{ transaction::DbTx, }; use reth_execution_errors::StateRootError; -use reth_primitives::{ - keccak256, revm::compat::into_reth_acc, Account, Address, BlockNumber, B256, U256, -}; +use reth_primitives::{keccak256, Account, Address, BlockNumber, B256, U256}; use revm::db::BundleAccount; use std::{ collections::{hash_map, HashMap, HashSet}, @@ -41,7 +39,7 @@ impl HashedPostState { .into_par_iter() .map(|(address, account)| { let hashed_address = keccak256(address); - let hashed_account = account.info.clone().map(into_reth_acc); + let hashed_account = account.info.clone().map(Into::into); let hashed_storage = HashedStorage::from_iter( account.status.was_destroyed(), account.storage.iter().map(|(key, value)| {