From 0d916312f21e8e68f459e9df5d858841dd508b0a Mon Sep 17 00:00:00 2001 From: Quentin Date: Tue, 21 May 2024 11:01:19 -0400 Subject: [PATCH 1/5] remove unnecessary formatting --- crates/primitives/src/account.rs | 6 + crates/storage/db-common/src/init.rs | 49 ++++---- .../bundle_state_with_receipts.rs | 6 + .../storage/provider/src/bundle_state/mod.rs | 1 + .../src/providers/database/provider.rs | 114 ++++++++++++------ 5 files changed, 108 insertions(+), 68 deletions(-) diff --git a/crates/primitives/src/account.rs b/crates/primitives/src/account.rs index 0fa55108e7c..49d7d6d88c2 100644 --- a/crates/primitives/src/account.rs +++ b/crates/primitives/src/account.rs @@ -23,6 +23,12 @@ impl Bytecode { } } +impl From for RevmBytecode { + fn from(value: Bytecode) -> Self { + value.0 + } +} + impl Compact for Bytecode { fn to_compact(self, buf: &mut B) -> usize where diff --git a/crates/storage/db-common/src/init.rs b/crates/storage/db-common/src/init.rs index 9f5cc692e36..4997f0b5f07 100644 --- a/crates/storage/db-common/src/init.rs +++ b/crates/storage/db-common/src/init.rs @@ -6,6 +6,8 @@ use reth_db::tables; use reth_db_api::{database::Database, transaction::DbTxMut, DatabaseError}; use reth_etl::Collector; use reth_primitives::{ + revm::compat::into_revm_acc, + stage::{StageCheckpoint, StageId}, Account, Address, Bytecode, ChainSpec, GenesisAccount, Receipts, StaticFileSegment, StorageEntry, B256, U256, }; @@ -18,6 +20,7 @@ use reth_provider::{ StageCheckpointWriter, StateWriter, StaticFileProviderFactory, }; use reth_stages_types::{StageCheckpoint, StageId}; +use reth_revm::db::states::BundleBuilder; use reth_trie::{IntermediateStateRootState, StateRoot as StateRootComputer, StateRootProgress}; use serde::{Deserialize, Serialize}; use std::{ @@ -116,7 +119,7 @@ pub fn init_genesis(factory: ProviderFactory) -> Result(tx, &static_file_provider, chain.clone())?; - insert_genesis_state::(tx, alloc.len(), alloc.iter())?; + insert_genesis_state::(tx, alloc.iter())?; // insert sync stage for stage in StageId::ALL { @@ -132,28 +135,24 @@ pub fn init_genesis(factory: ProviderFactory) -> Result( tx: &::TXMut, - capacity: usize, alloc: impl Iterator, ) -> ProviderResult<()> { - insert_state::(tx, capacity, alloc, 0) + insert_state::(tx, alloc, 0) } /// Inserts state at given block into database. pub fn insert_state<'a, 'b, DB: Database>( tx: &::TXMut, - capacity: usize, alloc: impl Iterator, block: u64, ) -> ProviderResult<()> { - let mut state_init: BundleStateInit = HashMap::with_capacity(capacity); - let mut reverts_init = HashMap::with_capacity(capacity); - let mut contracts: HashMap = HashMap::with_capacity(capacity); + let mut bundle_builder = BundleBuilder::default(); for (address, account) in alloc { let bytecode_hash = if let Some(code) = &account.code { let bytecode = Bytecode::new_raw(code.clone()); let hash = bytecode.hash_slow(); - contracts.insert(hash, bytecode); + bundle_builder = bundle_builder.contract(hash, bytecode.into()); Some(hash) } else { None @@ -167,36 +166,31 @@ pub fn insert_state<'a, 'b, DB: Database>( m.iter() .map(|(key, value)| { let value = U256::from_be_bytes(value.0); - (*key, (U256::ZERO, value)) + ((*key).into(), (U256::ZERO, value)) }) .collect::>() }) .unwrap_or_default(); - reverts_init.insert( - *address, - (Some(None), storage.keys().map(|k| StorageEntry::new(*k, U256::ZERO)).collect()), - ); - - state_init.insert( - *address, - ( - None, - Some(Account { + bundle_builder = bundle_builder + .revert_storage( + block, + *address, + storage.keys().map(|k| (*k, U256::ZERO)).collect::>(), + ) + .state_present_account_info( + *address, + into_revm_acc(Account { nonce: account.nonce.unwrap_or_default(), balance: account.balance, bytecode_hash, }), - storage, - ), - ); + ) + .state_storage(*address, storage); } - let all_reverts_init: RevertsInit = HashMap::from([(block, reverts_init)]); - let execution_outcome = ExecutionOutcome::new_init( - state_init, - all_reverts_init, - contracts.into_iter().collect(), + let execution_outcome = ExecutionOutcome::new( + bundle_builder.build(), Receipts::default(), block, Vec::new(), @@ -437,7 +431,6 @@ fn dump_state( let tx = provider_rw.deref_mut().tx_mut(); insert_state::( tx, - accounts.len(), accounts.iter().map(|(address, account)| (address, account)), block, )?; 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 a790d92fcfc..dabebe41784 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 @@ -6,6 +6,11 @@ use reth_db_api::{ }; pub use reth_execution_types::*; use reth_primitives::StaticFileSegment; +use reth_evm::execute::BatchBlockExecutionOutput; +use reth_primitives::{ + logs_bloom, revm::compat::into_reth_acc, Account, Address, BlockHash, BlockNumber, Bloom, + Bytecode, Log, Receipt, Receipts, StaticFileSegment, B256, U256, +}; use reth_storage_errors::provider::{ProviderError, ProviderResult}; pub use revm::db::states::OriginalValuesKnown; @@ -80,6 +85,7 @@ mod tests { revm::compat::{into_reth_acc, into_revm_acc}, Account, Address, Receipt, Receipts, StorageEntry, B256, U256, }; + use reth_primitives::{keccak256, revm::compat::into_revm_acc, StorageEntry}; use reth_trie::{test_utils::state_root, StateRoot}; use revm::{ db::{ diff --git a/crates/storage/provider/src/bundle_state/mod.rs b/crates/storage/provider/src/bundle_state/mod.rs index 1b3965a14a4..7f8a65793ca 100644 --- a/crates/storage/provider/src/bundle_state/mod.rs +++ b/crates/storage/provider/src/bundle_state/mod.rs @@ -8,6 +8,7 @@ mod state_reverts; pub use bundle_state_with_receipts::{ AccountRevertInit, BundleStateInit, ExecutionOutcome, OriginalValuesKnown, RevertsInit, }; +pub use bundle_state_with_receipts::{BundleStateWithReceipts, OriginalValuesKnown}; pub use hashed_state_changes::HashedStateChanges; pub use state_changes::StateChanges; pub use state_reverts::{StateReverts, StorageRevertsIter}; diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index a305d8ddc9d..78a79189f03 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -1,5 +1,6 @@ use crate::{ bundle_state::{BundleStateInit, ExecutionOutcome, HashedStateChanges, RevertsInit}, + bundle_state::{BundleStateWithReceipts, HashedStateChanges}, providers::{database::metrics, static_file::StaticFileWriter, StaticFileProvider}, to_range, traits::{ @@ -32,6 +33,13 @@ use reth_network_p2p::headers::downloader::SyncTarget; use reth_primitives::{ keccak256, revm::{config::revm_spec, env::fill_block_env}, + revm::{ + compat::{into_reth_acc, into_revm_acc}, + config::revm_spec, + env::fill_block_env, + }, + stage::{StageCheckpoint, StageId}, + trie::Nibbles, Account, Address, Block, BlockHash, BlockHashOrNumber, BlockNumber, BlockWithSenders, ChainInfo, ChainSpec, GotExpected, Head, Header, Receipt, Requests, SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment, StorageEntry, TransactionMeta, @@ -46,7 +54,10 @@ use reth_trie::{ updates::TrieUpdates, HashedPostState, Nibbles, StateRoot, }; -use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg, SpecId}; +use revm::{ + db::states::BundleBuilder, + primitives::{BlockEnv, CfgEnvWithHandlerCfg, SpecId}, +}; use std::{ cmp::Ordering, collections::{hash_map, BTreeMap, BTreeSet, HashMap, HashSet}, @@ -569,13 +580,14 @@ impl DatabaseProvider { let storage_changeset = self.get_or_take::(storage_range)?; - let account_changeset = self.get_or_take::(range)?; + let account_changeset = + self.get_or_take::(range.clone())?; // iterate previous value and get plain state value to create changeset // Double option around Account represent if Account state is know (first option) and // account is removed (Second Option) - let mut state: BundleStateInit = HashMap::new(); + let mut bundle_builder = BundleBuilder::new(range); // This is not working for blocks that are not at tip. as plain state is not the last // state of end range. We should rename the functions or add support to access @@ -584,39 +596,52 @@ impl DatabaseProvider { let mut plain_accounts_cursor = self.tx.cursor_write::()?; let mut plain_storage_cursor = self.tx.cursor_dup_write::()?; - let mut reverts: RevertsInit = HashMap::new(); - // add account changeset changes for (block_number, account_before) in account_changeset.into_iter().rev() { let AccountBeforeTx { info: old_info, address } = account_before; - match state.entry(address) { - hash_map::Entry::Vacant(entry) => { - let new_info = plain_accounts_cursor.seek_exact(address)?.map(|kv| kv.1); - entry.insert((old_info, new_info, HashMap::new())); - } - hash_map::Entry::Occupied(mut entry) => { - // overwrite old account state. - entry.get_mut().0 = old_info; + if !bundle_builder.get_states().contains(&address) { + let new_info = plain_accounts_cursor.seek_exact(address)?.map(|kv| kv.1); + if let Some(new_info) = new_info { + bundle_builder = + bundle_builder.state_present_account_info(address, into_revm_acc(new_info)); } } + if let Some(old_info) = old_info { + bundle_builder = + bundle_builder.state_original_account_info(address, into_revm_acc(old_info)); + } + // insert old info into reverts. - reverts.entry(block_number).or_default().entry(address).or_default().0 = Some(old_info); + bundle_builder = bundle_builder.revert_account_info( + block_number, + address, + Some(old_info.map(into_revm_acc)), + ); } + let mut state_storage: HashMap> = HashMap::new(); + let mut revert_storage: HashMap> = HashMap::new(); + // add storage changeset changes for (block_and_address, old_storage) in storage_changeset.into_iter().rev() { let BlockNumberAddress((block_number, address)) = block_and_address; // get account state or insert from plain state. - let account_state = match state.entry(address) { - hash_map::Entry::Vacant(entry) => { - let present_info = plain_accounts_cursor.seek_exact(address)?.map(|kv| kv.1); - entry.insert((present_info, present_info, HashMap::new())) + if !bundle_builder.get_states().contains(&address) { + let present_info = plain_accounts_cursor.seek_exact(address)?.map(|kv| kv.1); + if let Some(present_info) = present_info { + bundle_builder = bundle_builder + .state_original_account_info(address, into_revm_acc(present_info)) + .state_present_account_info(address, into_revm_acc(present_info)); } + } + + let account_state = match state_storage.entry(address) { hash_map::Entry::Occupied(entry) => entry.into_mut(), + hash_map::Entry::Vacant(entry) => entry.insert(HashMap::new()), }; // match storage. - match account_state.2.entry(old_storage.key) { + match account_state.entry(old_storage.key.into()) { hash_map::Entry::Vacant(entry) => { let new_storage = plain_storage_cursor .seek_by_key_subkey(address, old_storage.key)? @@ -629,44 +654,55 @@ impl DatabaseProvider { } }; - reverts - .entry(block_number) + revert_storage + .entry(block_and_address) .or_default() - .entry(address) - .or_default() - .1 - .push(old_storage); + .push((old_storage.key.into(), old_storage.value)); + + bundle_builder = bundle_builder + .state_storage(address, state_storage.entry(address).or_default().clone()); + bundle_builder = bundle_builder.revert_storage( + block_number, + address, + revert_storage.entry(block_and_address).or_default().clone(), + ); } + let bundle_state = bundle_builder.build(); + if TAKE { // iterate over local plain state remove all account and all storages. - for (address, (old_account, new_account, storage)) in &state { + + for (address, bundle_account) in bundle_state.state.iter() { // revert account if needed. - if old_account != new_account { + if bundle_account.is_info_changed() { let existing_entry = plain_accounts_cursor.seek_exact(*address)?; - if let Some(account) = old_account { - plain_accounts_cursor.upsert(*address, *account)?; + if let Some(account) = &bundle_account.original_info { + plain_accounts_cursor.upsert(*address, into_reth_acc(account.clone()))?; } else if existing_entry.is_some() { plain_accounts_cursor.delete_current()?; } } // revert storages - for (storage_key, (old_storage_value, _new_storage_value)) in storage { - let storage_entry = - StorageEntry { key: *storage_key, value: *old_storage_value }; + for (storage_key, storage_slot) in bundle_account.storage.iter() { + let b256_key: B256 = (*storage_key).into(); + let storage_entry = StorageEntry { + key: (*storage_key).into(), + value: storage_slot.original_value(), + }; // delete previous value // TODO: This does not use dupsort features if plain_storage_cursor - .seek_by_key_subkey(*address, *storage_key)? - .filter(|s| s.key == *storage_key) + .seek_by_key_subkey(*address, b256_key)? + .filter(|s| s.key == b256_key) .is_some() { plain_storage_cursor.delete_current()? } // insert value if needed - if *old_storage_value != U256::ZERO { + if storage_slot.original_value() != U256::ZERO { plain_storage_cursor.upsert(*address, storage_entry)?; } } @@ -690,11 +726,9 @@ impl DatabaseProvider { receipts.push(block_receipts); } - Ok(ExecutionOutcome::new_init( - state, - reverts, - Vec::new(), - receipts.into(), + Ok(ExecutionOutcome::new( + bundle_state, + reth_primitives::Receipts::from_vec(receipts), start_block_number, Vec::new(), )) From 0db32032c13e0b9f11601d68c9569248e679e9ef Mon Sep 17 00:00:00 2001 From: Quentin Date: Tue, 21 May 2024 11:05:31 -0400 Subject: [PATCH 2/5] rename var --- .../provider/src/providers/database/provider.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 78a79189f03..9c2812e034f 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -686,16 +686,14 @@ impl DatabaseProvider { // revert storages for (storage_key, storage_slot) in bundle_account.storage.iter() { - let b256_key: B256 = (*storage_key).into(); - let storage_entry = StorageEntry { - key: (*storage_key).into(), - value: storage_slot.original_value(), - }; + let storage_key: B256 = (*storage_key).into(); + let storage_entry = + StorageEntry { key: storage_key, value: storage_slot.original_value() }; // delete previous value // TODO: This does not use dupsort features if plain_storage_cursor - .seek_by_key_subkey(*address, b256_key)? - .filter(|s| s.key == b256_key) + .seek_by_key_subkey(*address, storage_key)? + .filter(|s| s.key == storage_key) .is_some() { plain_storage_cursor.delete_current()? From 70670a960a385d4567b74f8c5cf08860089150b6 Mon Sep 17 00:00:00 2001 From: Quentin Date: Sun, 16 Jun 2024 11:05:37 +0200 Subject: [PATCH 3/5] rebase --- Cargo.lock | 1 + bin/reth/src/commands/stage/drop.rs | 2 +- crates/storage/db-common/Cargo.toml | 1 + crates/storage/db-common/src/init.rs | 17 +++++------------ .../bundle_state/bundle_state_with_receipts.rs | 6 ------ crates/storage/provider/src/bundle_state/mod.rs | 1 - .../provider/src/providers/database/provider.rs | 12 ++++-------- 7 files changed, 12 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc8ebdd6a57..96180a3633e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6649,6 +6649,7 @@ dependencies = [ "reth-etl", "reth-primitives", "reth-provider", + "reth-revm", "reth-stages-types", "reth-trie", "serde", diff --git a/bin/reth/src/commands/stage/drop.rs b/bin/reth/src/commands/stage/drop.rs index 8297eafef81..2dce1a43089 100644 --- a/bin/reth/src/commands/stage/drop.rs +++ b/bin/reth/src/commands/stage/drop.rs @@ -103,7 +103,7 @@ impl Command { Default::default(), )?; let alloc = &self.env.chain.genesis().alloc; - insert_genesis_state::(tx, alloc.len(), alloc.iter())?; + insert_genesis_state::(tx, alloc.iter())?; } StageEnum::AccountHashing => { tx.clear::()?; diff --git a/crates/storage/db-common/Cargo.toml b/crates/storage/db-common/Cargo.toml index 0e6a3720dc8..222ee1214a8 100644 --- a/crates/storage/db-common/Cargo.toml +++ b/crates/storage/db-common/Cargo.toml @@ -18,6 +18,7 @@ reth-trie.workspace = true reth-etl.workspace = true reth-codecs.workspace = true reth-stages-types.workspace = true +reth-revm.workspace = true # misc eyre.workspace = true diff --git a/crates/storage/db-common/src/init.rs b/crates/storage/db-common/src/init.rs index 4997f0b5f07..163fe6d3675 100644 --- a/crates/storage/db-common/src/init.rs +++ b/crates/storage/db-common/src/init.rs @@ -6,21 +6,18 @@ use reth_db::tables; use reth_db_api::{database::Database, transaction::DbTxMut, DatabaseError}; use reth_etl::Collector; use reth_primitives::{ - revm::compat::into_revm_acc, - stage::{StageCheckpoint, StageId}, - Account, Address, Bytecode, ChainSpec, GenesisAccount, Receipts, StaticFileSegment, - StorageEntry, B256, U256, + revm::compat::into_revm_acc, Account, Address, Bytecode, ChainSpec, GenesisAccount, Receipts, + StaticFileSegment, StorageEntry, B256, U256, }; use reth_provider::{ - bundle_state::{BundleStateInit, RevertsInit}, errors::provider::ProviderResult, providers::{StaticFileProvider, StaticFileWriter}, BlockHashReader, BlockNumReader, ChainSpecProvider, DatabaseProviderRW, ExecutionOutcome, HashingWriter, HistoryWriter, OriginalValuesKnown, ProviderError, ProviderFactory, StageCheckpointWriter, StateWriter, StaticFileProviderFactory, }; -use reth_stages_types::{StageCheckpoint, StageId}; use reth_revm::db::states::BundleBuilder; +use reth_stages_types::{StageCheckpoint, StageId}; use reth_trie::{IntermediateStateRootState, StateRoot as StateRootComputer, StateRootProgress}; use serde::{Deserialize, Serialize}; use std::{ @@ -189,12 +186,8 @@ pub fn insert_state<'a, 'b, DB: Database>( .state_storage(*address, storage); } - let execution_outcome = ExecutionOutcome::new( - bundle_builder.build(), - Receipts::default(), - block, - Vec::new(), - ); + let execution_outcome = + ExecutionOutcome::new(bundle_builder.build(), Receipts::default(), block, Vec::new()); execution_outcome.write_to_storage(tx, None, OriginalValuesKnown::Yes)?; 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 dabebe41784..a790d92fcfc 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 @@ -6,11 +6,6 @@ use reth_db_api::{ }; pub use reth_execution_types::*; use reth_primitives::StaticFileSegment; -use reth_evm::execute::BatchBlockExecutionOutput; -use reth_primitives::{ - logs_bloom, revm::compat::into_reth_acc, Account, Address, BlockHash, BlockNumber, Bloom, - Bytecode, Log, Receipt, Receipts, StaticFileSegment, B256, U256, -}; use reth_storage_errors::provider::{ProviderError, ProviderResult}; pub use revm::db::states::OriginalValuesKnown; @@ -85,7 +80,6 @@ mod tests { revm::compat::{into_reth_acc, into_revm_acc}, Account, Address, Receipt, Receipts, StorageEntry, B256, U256, }; - use reth_primitives::{keccak256, revm::compat::into_revm_acc, StorageEntry}; use reth_trie::{test_utils::state_root, StateRoot}; use revm::{ db::{ diff --git a/crates/storage/provider/src/bundle_state/mod.rs b/crates/storage/provider/src/bundle_state/mod.rs index 7f8a65793ca..1b3965a14a4 100644 --- a/crates/storage/provider/src/bundle_state/mod.rs +++ b/crates/storage/provider/src/bundle_state/mod.rs @@ -8,7 +8,6 @@ mod state_reverts; pub use bundle_state_with_receipts::{ AccountRevertInit, BundleStateInit, ExecutionOutcome, OriginalValuesKnown, RevertsInit, }; -pub use bundle_state_with_receipts::{BundleStateWithReceipts, OriginalValuesKnown}; pub use hashed_state_changes::HashedStateChanges; pub use state_changes::StateChanges; pub use state_reverts::{StateReverts, StorageRevertsIter}; diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 9c2812e034f..b7f4fb3d47d 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -1,6 +1,5 @@ use crate::{ - bundle_state::{BundleStateInit, ExecutionOutcome, HashedStateChanges, RevertsInit}, - bundle_state::{BundleStateWithReceipts, HashedStateChanges}, + bundle_state::{ExecutionOutcome, HashedStateChanges}, providers::{database::metrics, static_file::StaticFileWriter, StaticFileProvider}, to_range, traits::{ @@ -32,14 +31,11 @@ use reth_evm::ConfigureEvmEnv; use reth_network_p2p::headers::downloader::SyncTarget; use reth_primitives::{ keccak256, - revm::{config::revm_spec, env::fill_block_env}, revm::{ compat::{into_reth_acc, into_revm_acc}, config::revm_spec, env::fill_block_env, }, - stage::{StageCheckpoint, StageId}, - trie::Nibbles, Account, Address, Block, BlockHash, BlockHashOrNumber, BlockNumber, BlockWithSenders, ChainInfo, ChainSpec, GotExpected, Head, Header, Receipt, Requests, SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment, StorageEntry, TransactionMeta, @@ -673,7 +669,7 @@ impl DatabaseProvider { if TAKE { // iterate over local plain state remove all account and all storages. - for (address, bundle_account) in bundle_state.state.iter() { + for (address, bundle_account) in &bundle_state.state { // revert account if needed. if bundle_account.is_info_changed() { let existing_entry = plain_accounts_cursor.seek_exact(*address)?; @@ -685,7 +681,7 @@ impl DatabaseProvider { } // revert storages - for (storage_key, storage_slot) in bundle_account.storage.iter() { + for (storage_key, storage_slot) in &bundle_account.storage { let storage_key: B256 = (*storage_key).into(); let storage_entry = StorageEntry { key: storage_key, value: storage_slot.original_value() }; @@ -726,7 +722,7 @@ impl DatabaseProvider { Ok(ExecutionOutcome::new( bundle_state, - reth_primitives::Receipts::from_vec(receipts), + reth_primitives::Receipts::from(receipts), start_block_number, Vec::new(), )) From 9cd339f5c86b2228ce1d096ca928509ae75fa2bb Mon Sep 17 00:00:00 2001 From: Quentin Date: Sun, 16 Jun 2024 19:31:32 +0200 Subject: [PATCH 4/5] use mut getters --- .../src/providers/database/provider.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index b7f4fb3d47d..6ce38ea5b40 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -615,9 +615,6 @@ impl DatabaseProvider { ); } - let mut state_storage: HashMap> = HashMap::new(); - let mut revert_storage: HashMap> = HashMap::new(); - // add storage changeset changes for (block_and_address, old_storage) in storage_changeset.into_iter().rev() { let BlockNumberAddress((block_number, address)) = block_and_address; @@ -631,7 +628,7 @@ impl DatabaseProvider { } } - let account_state = match state_storage.entry(address) { + let account_state = match bundle_builder.get_state_storage_mut().entry(address) { hash_map::Entry::Occupied(entry) => entry.into_mut(), hash_map::Entry::Vacant(entry) => entry.insert(HashMap::new()), }; @@ -650,18 +647,11 @@ impl DatabaseProvider { } }; - revert_storage - .entry(block_and_address) + bundle_builder + .get_revert_storage_mut() + .entry((block_number, address)) .or_default() .push((old_storage.key.into(), old_storage.value)); - - bundle_builder = bundle_builder - .state_storage(address, state_storage.entry(address).or_default().clone()); - bundle_builder = bundle_builder.revert_storage( - block_number, - address, - revert_storage.entry(block_and_address).or_default().clone(), - ); } let bundle_state = bundle_builder.build(); From 9b212d64a8b3e24e9838de716d01a2b88fa4813c Mon Sep 17 00:00:00 2001 From: Quentin Date: Sun, 16 Jun 2024 20:06:15 +0200 Subject: [PATCH 5/5] remove BundleStateInit, AccountRevertInit, and RevertInit --- .../execution-types/src/execution_outcome.rs | 91 +------------------ .../storage/provider/src/bundle_state/mod.rs | 4 +- 2 files changed, 4 insertions(+), 91 deletions(-) diff --git a/crates/evm/execution-types/src/execution_outcome.rs b/crates/evm/execution-types/src/execution_outcome.rs index aebc8d45b5e..d9fbe2ee250 100644 --- a/crates/evm/execution-types/src/execution_outcome.rs +++ b/crates/evm/execution-types/src/execution_outcome.rs @@ -1,15 +1,12 @@ 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, revm::compat::into_reth_acc, Account, Address, BlockNumber, Bloom, Bytecode, Log, + Receipt, Receipts, Requests, B256, U256, }; use reth_trie::HashedPostState; use revm::{ db::{states::BundleState, BundleAccount}, primitives::AccountInfo, }; -use std::collections::HashMap; /// Represents the outcome of block execution, including post-execution changes and reverts. /// @@ -36,16 +33,6 @@ pub struct ExecutionOutcome { pub requests: Vec, } -/// Type used to initialize revms bundle state. -pub type BundleStateInit = - HashMap, Option, HashMap)>; - -/// Types used inside `RevertsInit` to initialize revms reverts. -pub type AccountRevertInit = (Option>, Vec); - -/// Type used to initialize revms reverts. -pub type RevertsInit = HashMap>; - impl ExecutionOutcome { /// Creates a new `ExecutionOutcome`. /// @@ -60,48 +47,6 @@ impl ExecutionOutcome { Self { bundle, receipts, first_block, requests } } - /// Creates a new `ExecutionOutcome` from initialization parameters. - /// - /// This constructor initializes a new `ExecutionOutcome` instance using detailed - /// initialization parameters. - pub fn new_init( - state_init: BundleStateInit, - revert_init: RevertsInit, - contracts_init: Vec<(B256, Bytecode)>, - receipts: Receipts, - first_block: BlockNumber, - requests: Vec, - ) -> Self { - // sort reverts by block number - let mut reverts = revert_init.into_iter().collect::>(); - reverts.sort_unstable_by_key(|a| a.0); - - // initialize revm bundle - let bundle = BundleState::new( - state_init.into_iter().map(|(address, (original, present, storage))| { - ( - address, - original.map(into_revm_acc), - present.map(into_revm_acc), - storage.into_iter().map(|(k, s)| (k.into(), s)).collect(), - ) - }), - reverts.into_iter().map(|(_, reverts)| { - // does not needs to be sorted, it is done when taking reverts. - reverts.into_iter().map(|(address, (original, storage))| { - ( - address, - original.map(|i| i.map(into_revm_acc)), - storage.into_iter().map(|entry| (entry.key.into(), entry.value)), - ) - }) - }), - contracts_init.into_iter().map(|(code_hash, bytecode)| (code_hash, bytecode.0)), - ); - - Self { bundle, receipts, first_block, requests } - } - /// Return revm bundle state. pub const fn state(&self) -> &BundleState { &self.bundle @@ -395,37 +340,7 @@ mod tests { }; // Assert that creating a new ExecutionOutcome using the constructor matches exec_res - assert_eq!( - ExecutionOutcome::new(bundle, receipts.clone(), first_block, requests.clone()), - exec_res - ); - - // Create a BundleStateInit object and insert initial data - let mut state_init: BundleStateInit = HashMap::new(); - state_init - .insert(Address::new([2; 20]), (None, Some(Account::default()), HashMap::default())); - - // Create a HashMap for account reverts and insert initial data - let mut revert_inner: HashMap = HashMap::new(); - revert_inner.insert(Address::new([2; 20]), (None, vec![])); - - // Create a RevertsInit object and insert the revert_inner data - let mut revert_init: RevertsInit = HashMap::new(); - revert_init.insert(123, revert_inner); - - // Assert that creating a new ExecutionOutcome using the new_init method matches - // exec_res - assert_eq!( - ExecutionOutcome::new_init( - state_init, - revert_init, - vec![], - receipts, - first_block, - requests, - ), - exec_res - ); + assert_eq!(ExecutionOutcome::new(bundle, receipts, first_block, requests), exec_res); } #[test] diff --git a/crates/storage/provider/src/bundle_state/mod.rs b/crates/storage/provider/src/bundle_state/mod.rs index 1b3965a14a4..daeab99c223 100644 --- a/crates/storage/provider/src/bundle_state/mod.rs +++ b/crates/storage/provider/src/bundle_state/mod.rs @@ -5,9 +5,7 @@ mod hashed_state_changes; mod state_changes; mod state_reverts; -pub use bundle_state_with_receipts::{ - AccountRevertInit, BundleStateInit, ExecutionOutcome, OriginalValuesKnown, RevertsInit, -}; +pub use bundle_state_with_receipts::{ExecutionOutcome, OriginalValuesKnown}; pub use hashed_state_changes::HashedStateChanges; pub use state_changes::StateChanges; pub use state_reverts::{StateReverts, StorageRevertsIter};