From 9b057037d9caa8d458ab6f11f4ddcf7571fba053 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:03:57 +0200 Subject: [PATCH] refactor: some small refactoring (#9657) --- crates/ethereum/payload/src/lib.rs | 35 +++++++++---------- crates/evm/src/provider.rs | 2 +- crates/evm/src/system_calls.rs | 13 +++---- .../stages/src/stages/hashing_storage.rs | 2 +- crates/stages/stages/src/stages/merkle.rs | 4 +-- .../stages/stages/src/test_utils/test_db.rs | 2 +- .../src/bundle_state/state_changes.rs | 4 +-- .../src/providers/database/provider.rs | 8 ++--- crates/storage/provider/src/writer/mod.rs | 6 ++-- crates/trie/common/src/account.rs | 2 +- crates/trie/trie/src/state.rs | 2 +- testing/testing-utils/src/generators.rs | 2 +- 12 files changed, 39 insertions(+), 43 deletions(-) diff --git a/crates/ethereum/payload/src/lib.rs b/crates/ethereum/payload/src/lib.rs index c7b491243..e853cb1ad 100644 --- a/crates/ethereum/payload/src/lib.rs +++ b/crates/ethereum/payload/src/lib.rs @@ -196,25 +196,24 @@ where } // Calculate the requests and the requests root. - let (requests, requests_root) = if chain_spec - .is_prague_active_at_timestamp(attributes.timestamp) - { - // We do not calculate the EIP-6110 deposit requests because there are no - // transactions in an empty payload. - let withdrawal_requests = post_block_withdrawal_requests_contract_call::( - &self.evm_config, - &mut db, - &initialized_cfg, - &initialized_block_env, - ) - .map_err(|err| PayloadBuilderError::Internal(err.into()))?; + let (requests, requests_root) = + if chain_spec.is_prague_active_at_timestamp(attributes.timestamp) { + // We do not calculate the EIP-6110 deposit requests because there are no + // transactions in an empty payload. + let withdrawal_requests = post_block_withdrawal_requests_contract_call( + &self.evm_config, + &mut db, + &initialized_cfg, + &initialized_block_env, + ) + .map_err(|err| PayloadBuilderError::Internal(err.into()))?; - let requests = withdrawal_requests; - let requests_root = calculate_requests_root(&requests); - (Some(requests.into()), Some(requests_root)) - } else { - (None, None) - }; + let requests = withdrawal_requests; + let requests_root = calculate_requests_root(&requests); + (Some(requests.into()), Some(requests_root)) + } else { + (None, None) + }; let header = Header { parent_hash: parent_block.hash(), diff --git a/crates/evm/src/provider.rs b/crates/evm/src/provider.rs index 2e73ff2fa..b847a0665 100644 --- a/crates/evm/src/provider.rs +++ b/crates/evm/src/provider.rs @@ -36,7 +36,7 @@ pub trait EvmEnvProvider: Send + Sync { { let mut cfg = CfgEnvWithHandlerCfg::new_with_spec_id(CfgEnv::default(), SpecId::LATEST); let mut block_env = BlockEnv::default(); - self.fill_env_with_header::(&mut cfg, &mut block_env, header, evm_config)?; + self.fill_env_with_header(&mut cfg, &mut block_env, header, evm_config)?; Ok((cfg, block_env)) } diff --git a/crates/evm/src/system_calls.rs b/crates/evm/src/system_calls.rs index 9d493f517..58759a866 100644 --- a/crates/evm/src/system_calls.rs +++ b/crates/evm/src/system_calls.rs @@ -90,7 +90,7 @@ where // if the block number is zero (genesis block) then the parent beacon block root must // be 0x0 and no system transaction may occur as per EIP-4788 if block_number == 0 { - if parent_beacon_block_root != B256::ZERO { + if !parent_beacon_block_root.is_zero() { return Err(BlockValidationError::CancunGenesisParentBeaconBlockRootNotZero { parent_beacon_block_root, } @@ -162,7 +162,7 @@ where .build(); // initialize a block from the env, because the post block call needs the block itself - apply_withdrawal_requests_contract_call::(evm_config, &mut evm_post_block) + apply_withdrawal_requests_contract_call(evm_config, &mut evm_post_block) } /// Applies the post-block call to the EIP-7002 withdrawal requests contract. @@ -256,11 +256,8 @@ where let amount = data.get_u64(); - withdrawal_requests.push(Request::WithdrawalRequest(WithdrawalRequest { - source_address, - validator_pubkey, - amount, - })); + withdrawal_requests + .push(WithdrawalRequest { source_address, validator_pubkey, amount }.into()); } Ok(withdrawal_requests) @@ -295,7 +292,7 @@ where .build(); // initialize a block from the env, because the post block call needs the block itself - apply_consolidation_requests_contract_call::(evm_config, &mut evm_post_block) + apply_consolidation_requests_contract_call(evm_config, &mut evm_post_block) } /// Applies the post-block call to the EIP-7251 consolidation requests contract. diff --git a/crates/stages/stages/src/stages/hashing_storage.rs b/crates/stages/stages/src/stages/hashing_storage.rs index 662f1d1a7..dbefa4b0e 100644 --- a/crates/stages/stages/src/stages/hashing_storage.rs +++ b/crates/stages/stages/src/stages/hashing_storage.rs @@ -535,7 +535,7 @@ mod tests { storage_cursor.delete_current()?; } - if entry.value != U256::ZERO { + if !entry.value.is_zero() { storage_cursor.upsert(bn_address.address(), entry)?; } } diff --git a/crates/stages/stages/src/stages/merkle.rs b/crates/stages/stages/src/stages/merkle.rs index 9bbd68ed3..4a1eaeb8e 100644 --- a/crates/stages/stages/src/stages/merkle.rs +++ b/crates/stages/stages/src/stages/merkle.rs @@ -562,7 +562,7 @@ mod tests { } let storage = storage_entries .into_iter() - .filter(|v| v.value != U256::ZERO) + .filter(|v| !v.value.is_zero()) .map(|v| (v.key, v.value)) .collect::>(); accounts.insert(key, (account, storage)); @@ -636,7 +636,7 @@ mod tests { storage_cursor.delete_current().unwrap(); } - if value != U256::ZERO { + if !value.is_zero() { let storage_entry = StorageEntry { key: hashed_slot, value }; storage_cursor.upsert(hashed_address, storage_entry).unwrap(); } diff --git a/crates/stages/stages/src/test_utils/test_db.rs b/crates/stages/stages/src/test_utils/test_db.rs index 8f72b5aab..bf00a4394 100644 --- a/crates/stages/stages/src/test_utils/test_db.rs +++ b/crates/stages/stages/src/test_utils/test_db.rs @@ -386,7 +386,7 @@ impl TestStageDB { tx.put::(hashed_address, account)?; // Insert into storage tables. - storage.into_iter().filter(|e| e.value != U256::ZERO).try_for_each(|entry| { + storage.into_iter().filter(|e| !e.value.is_zero()).try_for_each(|entry| { let hashed_entry = StorageEntry { key: keccak256(entry.key), ..entry }; let mut cursor = tx.cursor_dup_write::()?; diff --git a/crates/storage/provider/src/bundle_state/state_changes.rs b/crates/storage/provider/src/bundle_state/state_changes.rs index ba9acfccc..e1922f96a 100644 --- a/crates/storage/provider/src/bundle_state/state_changes.rs +++ b/crates/storage/provider/src/bundle_state/state_changes.rs @@ -5,7 +5,7 @@ use reth_db_api::{ cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW}, transaction::DbTxMut, }; -use reth_primitives::{Bytecode, StorageEntry, U256}; +use reth_primitives::{Bytecode, StorageEntry}; use reth_storage_errors::db::DatabaseError; use revm::db::states::{PlainStorageChangeset, StateChangeset}; @@ -77,7 +77,7 @@ impl StateChanges { } } - if entry.value != U256::ZERO { + if !entry.value.is_zero() { storages_cursor.upsert(address, entry)?; } } diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 7a7f80dc5..e8131f506 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -1001,7 +1001,7 @@ impl DatabaseProvider { } // insert value if needed - if *old_storage_value != U256::ZERO { + if !old_storage_value.is_zero() { plain_storage_cursor.upsert(*address, storage_entry)?; } } @@ -1099,7 +1099,7 @@ impl DatabaseProvider { } // insert value if needed - if *old_storage_value != U256::ZERO { + if !old_storage_value.is_zero() { plain_storage_cursor.upsert(*address, storage_entry)?; } } @@ -2704,7 +2704,7 @@ impl HashingWriter for DatabaseProvider { hashed_storage.delete_current()?; } - if value != U256::ZERO { + if !value.is_zero() { hashed_storage.upsert(hashed_address, StorageEntry { key, value })?; } } @@ -2744,7 +2744,7 @@ impl HashingWriter for DatabaseProvider { hashed_storage_cursor.delete_current()?; } - if value != U256::ZERO { + if !value.is_zero() { hashed_storage_cursor.upsert(hashed_address, StorageEntry { key, value })?; } Ok(()) diff --git a/crates/storage/provider/src/writer/mod.rs b/crates/storage/provider/src/writer/mod.rs index f5a3554d3..0223cbd07 100644 --- a/crates/storage/provider/src/writer/mod.rs +++ b/crates/storage/provider/src/writer/mod.rs @@ -7,7 +7,7 @@ use reth_db::{ Database, }; use reth_errors::{ProviderError, ProviderResult}; -use reth_primitives::{BlockNumber, StorageEntry, U256}; +use reth_primitives::{BlockNumber, StorageEntry}; use reth_storage_api::ReceiptWriter; use reth_storage_errors::writer::StorageWriterError; use reth_trie::HashedPostStateSorted; @@ -129,7 +129,7 @@ impl<'a, 'b, DB: Database> StorageWriter<'a, 'b, DB> { } } - if entry.value != U256::ZERO { + if !entry.value.is_zero() { hashed_storage_cursor.upsert(*hashed_address, entry)?; } } @@ -206,7 +206,7 @@ mod tests { use super::*; use crate::test_utils::create_test_provider_factory; use reth_db_api::transaction::DbTx; - use reth_primitives::{keccak256, Account, Address, B256}; + use reth_primitives::{keccak256, Account, Address, B256, U256}; use reth_trie::{HashedPostState, HashedStorage}; #[test] diff --git a/crates/trie/common/src/account.rs b/crates/trie/common/src/account.rs index 64860ab78..269202601 100644 --- a/crates/trie/common/src/account.rs +++ b/crates/trie/common/src/account.rs @@ -35,7 +35,7 @@ impl From for TrieAccount { storage_root_unhashed( storage .into_iter() - .filter(|(_, value)| *value != B256::ZERO) + .filter(|(_, value)| !value.is_zero()) .map(|(slot, value)| (slot, U256::from_be_bytes(*value))), ) }) diff --git a/crates/trie/trie/src/state.rs b/crates/trie/trie/src/state.rs index 84bfb8fd6..6a37475eb 100644 --- a/crates/trie/trie/src/state.rs +++ b/crates/trie/trie/src/state.rs @@ -309,7 +309,7 @@ impl HashedStorage { let mut non_zero_valued_slots = Vec::new(); let mut zero_valued_slots = HashSet::default(); for (hashed_slot, value) in self.storage { - if value == U256::ZERO { + if value.is_zero() { zero_valued_slots.insert(hashed_slot); } else { non_zero_valued_slots.push((hashed_slot, value)); diff --git a/testing/testing-utils/src/generators.rs b/testing/testing-utils/src/generators.rs index 4ef65043f..62923b1ac 100644 --- a/testing/testing-utils/src/generators.rs +++ b/testing/testing-utils/src/generators.rs @@ -245,7 +245,7 @@ where let mut old_entries: Vec<_> = new_entries .into_iter() .filter_map(|entry| { - let old = if entry.value != U256::ZERO { + let old = if !entry.value.is_zero() { storage.insert(entry.key, entry.value) } else { let old = storage.remove(&entry.key);