From 88d76b49070e525913fc4a49c8a3b3db0865d595 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Fri, 27 Sep 2024 15:41:00 +0200 Subject: [PATCH 1/2] use collections::HashMap --- steel/src/block.rs | 3 ++- steel/src/state.rs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/steel/src/block.rs b/steel/src/block.rs index 78f95d5f..209d5932 100644 --- a/steel/src/block.rs +++ b/steel/src/block.rs @@ -12,10 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::collections::HashMap; + use crate::{state::StateDb, EvmBlockHeader, EvmEnv, GuestEvmEnv, MerkleTrie}; use ::serde::{Deserialize, Serialize}; use alloy_primitives::Bytes; -use revm::primitives::HashMap; /// Input committing to the corresponding execution block hash. #[derive(Clone, Serialize, Deserialize)] diff --git a/steel/src/state.rs b/steel/src/state.rs index 758975fc..5dca1aad 100644 --- a/steel/src/state.rs +++ b/steel/src/state.rs @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{convert::Infallible, fmt::Debug, rc::Rc}; +use std::{collections::HashMap, convert::Infallible, fmt::Debug, rc::Rc}; use crate::mpt::{MerkleTrie, EMPTY_ROOT_HASH}; use alloy_primitives::{keccak256, Address, Bytes, TxNumber, B256, U256}; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; use revm::{ - primitives::{AccountInfo, Bytecode, HashMap, KECCAK_EMPTY}, + primitives::{AccountInfo, Bytecode, KECCAK_EMPTY}, Database, }; From 698c5ba0e903c9d3c7530a0f46075998a5365357 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz Date: Fri, 27 Sep 2024 16:47:27 +0200 Subject: [PATCH 2/2] use alloy_primitives::map --- steel/src/block.rs | 7 +++---- steel/src/host/db/alloy.rs | 8 ++++---- steel/src/host/db/proof.rs | 36 +++++++++++++++++------------------- steel/src/mpt.rs | 8 ++++---- steel/src/state.rs | 16 ++++++++++------ 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/steel/src/block.rs b/steel/src/block.rs index 209d5932..38203d0b 100644 --- a/steel/src/block.rs +++ b/steel/src/block.rs @@ -12,11 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::collections::HashMap; - use crate::{state::StateDb, EvmBlockHeader, EvmEnv, GuestEvmEnv, MerkleTrie}; use ::serde::{Deserialize, Serialize}; -use alloy_primitives::Bytes; +use alloy_primitives::{map::HashMap, Bytes}; /// Input committing to the corresponding execution block hash. #[derive(Clone, Serialize, Deserialize)] @@ -39,7 +37,8 @@ impl BlockInput { let header = self.header.seal_slow(); // validate that ancestor headers form a valid chain - let mut block_hashes = HashMap::with_capacity(self.ancestors.len() + 1); + let mut block_hashes = + HashMap::with_capacity_and_hasher(self.ancestors.len() + 1, Default::default()); block_hashes.insert(header.number(), header.seal()); let mut previous_header = header.inner(); diff --git a/steel/src/host/db/alloy.rs b/steel/src/host/db/alloy.rs index c6002eac..fca477f6 100644 --- a/steel/src/host/db/alloy.rs +++ b/steel/src/host/db/alloy.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{collections::HashMap, future::IntoFuture, marker::PhantomData}; +use std::{future::IntoFuture, marker::PhantomData}; use super::provider::{ProviderConfig, ProviderDb}; use alloy::{ @@ -20,7 +20,7 @@ use alloy::{ providers::Provider, transports::{Transport, TransportError}, }; -use alloy_primitives::{Address, BlockHash, B256, U256}; +use alloy_primitives::{map::B256HashMap, Address, BlockHash, B256, U256}; use revm::{ primitives::{AccountInfo, Bytecode}, Database, @@ -44,7 +44,7 @@ pub struct AlloyDb> { /// Handle to the Tokio runtime. handle: Handle, /// Bytecode cache to allow querying bytecode by hash instead of address. - contracts: HashMap, + contracts: B256HashMap, phantom: PhantomData (T, N)>, } @@ -59,7 +59,7 @@ impl> AlloyDb { provider_config: config, block_hash, handle: Handle::current(), - contracts: HashMap::new(), + contracts: Default::default(), phantom: PhantomData, } } diff --git a/steel/src/host/db/proof.rs b/steel/src/host/db/proof.rs index 5a6b9111..909811cf 100644 --- a/steel/src/host/db/proof.rs +++ b/steel/src/host/db/proof.rs @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::collections::{hash_map::Entry, HashMap, HashSet}; - use super::{provider::ProviderDb, AlloyDb}; use crate::MerkleTrie; use alloy::{ @@ -23,7 +21,10 @@ use alloy::{ rpc::types::EIP1186AccountProofResponse, transports::Transport, }; -use alloy_primitives::{Address, BlockNumber, Bytes, StorageKey, StorageValue, B256, U256}; +use alloy_primitives::{ + map::{hash_map, AddressHashMap, B256HashMap, B256HashSet, HashSet}, + Address, BlockNumber, Bytes, StorageKey, StorageValue, B256, U256, +}; use anyhow::{ensure, Context, Result}; use revm::{ primitives::{AccountInfo, Bytecode}, @@ -32,12 +33,10 @@ use revm::{ /// A simple revm [Database] wrapper that records all DB queries. pub struct ProofDb { - accounts: HashMap>, - contracts: HashMap, + accounts: AddressHashMap, + contracts: B256HashMap, block_hash_numbers: HashSet, - - proofs: HashMap, - + proofs: AddressHashMap, inner: D, } @@ -45,7 +44,7 @@ struct AccountProof { /// The inclusion proof for this account. account_proof: Vec, /// The MPT inclusion proofs for several storage slots. - storage_proofs: HashMap, + storage_proofs: B256HashMap, } struct StorageProof { @@ -59,11 +58,10 @@ impl ProofDb { /// Creates a new ProofDb instance, with a [Database]. pub fn new(db: D) -> Self { Self { - accounts: HashMap::new(), - contracts: HashMap::new(), - block_hash_numbers: HashSet::new(), - - proofs: HashMap::new(), + accounts: Default::default(), + contracts: Default::default(), + block_hash_numbers: Default::default(), + proofs: Default::default(), inner: db, } } @@ -76,7 +74,7 @@ impl ProofDb { } /// Returns the referenced contracts - pub fn contracts(&self) -> &HashMap { + pub fn contracts(&self) -> &B256HashMap { &self.contracts } @@ -176,7 +174,7 @@ impl> ProofDb) -> impl Fn(&Storag } fn add_proof( - proofs: &mut HashMap, + proofs: &mut AddressHashMap, proof_response: EIP1186AccountProofResponse, ) -> Result<()> { // convert the response into a StorageProof @@ -277,7 +275,7 @@ fn add_proof( .collect(); match proofs.entry(proof_response.address) { - Entry::Occupied(mut entry) => { + hash_map::Entry::Occupied(mut entry) => { let account_proof = entry.get_mut(); ensure!( account_proof.account_proof == proof_response.account_proof, @@ -285,7 +283,7 @@ fn add_proof( ); account_proof.storage_proofs.extend(storage_proofs); } - Entry::Vacant(entry) => { + hash_map::Entry::Vacant(entry) => { entry.insert(AccountProof { account_proof: proof_response.account_proof, storage_proofs, diff --git a/steel/src/mpt.rs b/steel/src/mpt.rs index d8516257..76c91ef7 100644 --- a/steel/src/mpt.rs +++ b/steel/src/mpt.rs @@ -12,9 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{collections::HashMap, fmt::Debug}; +use std::fmt::Debug; -use alloy_primitives::{b256, keccak256, B256}; +use alloy_primitives::{b256, keccak256, map::B256HashMap, B256}; use alloy_rlp::{BufMut, Decodable, Encodable, Header, PayloadView, EMPTY_STRING_CODE}; use nybbles::Nibbles; use serde::{Deserialize, Serialize}; @@ -76,7 +76,7 @@ impl MerkleTrie { pub fn from_rlp_nodes>( nodes: impl IntoIterator, ) -> alloy_rlp::Result { - let mut nodes_by_hash = HashMap::new(); + let mut nodes_by_hash = B256HashMap::default(); let mut root_node_opt = None; for rlp in nodes { @@ -332,7 +332,7 @@ fn parse_node(rlp: impl AsRef<[u8]>) -> alloy_rlp::Result<(Option, Node)> Ok(((rlp.len() >= 32).then(|| keccak256(rlp)), node)) } -fn resolve_trie(root: Node, nodes_by_hash: &HashMap) -> Node { +fn resolve_trie(root: Node, nodes_by_hash: &B256HashMap) -> Node { match root { Node::Null | Node::Leaf(..) => root, Node::Extension(prefix, child) => { diff --git a/steel/src/state.rs b/steel/src/state.rs index 5dca1aad..7485d699 100644 --- a/steel/src/state.rs +++ b/steel/src/state.rs @@ -12,10 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{collections::HashMap, convert::Infallible, fmt::Debug, rc::Rc}; +use std::{convert::Infallible, fmt::Debug, rc::Rc}; use crate::mpt::{MerkleTrie, EMPTY_ROOT_HASH}; -use alloy_primitives::{keccak256, Address, Bytes, TxNumber, B256, U256}; +use alloy_primitives::{ + keccak256, + map::{AddressHashMap, B256HashMap, HashMap}, + Address, Bytes, TxNumber, B256, U256, +}; use alloy_rlp_derive::{RlpDecodable, RlpEncodable}; use revm::{ primitives::{AccountInfo, Bytecode, KECCAK_EMPTY}, @@ -35,9 +39,9 @@ pub struct StateDb { state_trie: MerkleTrie, /// Storage MPTs to their root hash. /// [Rc] is used fore MPT deduplication. - storage_tries: HashMap>, + storage_tries: B256HashMap>, /// Contracts by their hash. - contracts: HashMap, + contracts: B256HashMap, /// Block hashes by their number. block_hashes: HashMap, } @@ -103,7 +107,7 @@ impl StateDb { /// account. pub struct WrapStateDb<'a> { inner: &'a StateDb, - account_storage: HashMap>>, + account_storage: AddressHashMap>>, } impl<'a> WrapStateDb<'a> { @@ -111,7 +115,7 @@ impl<'a> WrapStateDb<'a> { pub fn new(inner: &'a StateDb) -> Self { Self { inner, - account_storage: HashMap::new(), + account_storage: Default::default(), } } }