Skip to content

Commit 434e7b3

Browse files
authored
WEB3-146: Replace map types with alloy_primitives::map (#256)
This fixes the current compilation errors. Previously, we used to use `revm::primitives::HashMap` to have the same hash in Steel and `revm`. However, with the latest (minor) revm version, this has been switched to using `alloy_primitves::map::HashMap`. Thus, in this PR we also replace all `{Hash,BTree}{Map,Set}` with their more performant and specific equivalent in `alloy_primitives::map`. ### Additional context - alloy-rs/alloy#1365
1 parent 772f283 commit 434e7b3

File tree

5 files changed

+38
-36
lines changed

5 files changed

+38
-36
lines changed

steel/src/block.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
use crate::{state::StateDb, EvmBlockHeader, EvmEnv, GuestEvmEnv, MerkleTrie};
1616
use ::serde::{Deserialize, Serialize};
17-
use alloy_primitives::Bytes;
18-
use revm::primitives::HashMap;
17+
use alloy_primitives::{map::HashMap, Bytes};
1918

2019
/// Input committing to the corresponding execution block hash.
2120
#[derive(Clone, Serialize, Deserialize)]
@@ -38,7 +37,8 @@ impl<H: EvmBlockHeader> BlockInput<H> {
3837
let header = self.header.seal_slow();
3938

4039
// validate that ancestor headers form a valid chain
41-
let mut block_hashes = HashMap::with_capacity(self.ancestors.len() + 1);
40+
let mut block_hashes =
41+
HashMap::with_capacity_and_hasher(self.ancestors.len() + 1, Default::default());
4242
block_hashes.insert(header.number(), header.seal());
4343

4444
let mut previous_header = header.inner();

steel/src/host/db/alloy.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{collections::HashMap, future::IntoFuture, marker::PhantomData};
15+
use std::{future::IntoFuture, marker::PhantomData};
1616

1717
use super::provider::{ProviderConfig, ProviderDb};
1818
use alloy::{
1919
network::{BlockResponse, HeaderResponse, Network},
2020
providers::Provider,
2121
transports::{Transport, TransportError},
2222
};
23-
use alloy_primitives::{Address, BlockHash, B256, U256};
23+
use alloy_primitives::{map::B256HashMap, Address, BlockHash, B256, U256};
2424
use revm::{
2525
primitives::{AccountInfo, Bytecode},
2626
Database,
@@ -44,7 +44,7 @@ pub struct AlloyDb<T: Transport + Clone, N: Network, P: Provider<T, N>> {
4444
/// Handle to the Tokio runtime.
4545
handle: Handle,
4646
/// Bytecode cache to allow querying bytecode by hash instead of address.
47-
contracts: HashMap<B256, Bytecode>,
47+
contracts: B256HashMap<Bytecode>,
4848

4949
phantom: PhantomData<fn() -> (T, N)>,
5050
}
@@ -59,7 +59,7 @@ impl<T: Transport + Clone, N: Network, P: Provider<T, N>> AlloyDb<T, N, P> {
5959
provider_config: config,
6060
block_hash,
6161
handle: Handle::current(),
62-
contracts: HashMap::new(),
62+
contracts: Default::default(),
6363
phantom: PhantomData,
6464
}
6565
}

steel/src/host/db/proof.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::collections::{hash_map::Entry, HashMap, HashSet};
16-
1715
use super::{provider::ProviderDb, AlloyDb};
1816
use crate::MerkleTrie;
1917
use alloy::{
@@ -23,7 +21,10 @@ use alloy::{
2321
rpc::types::EIP1186AccountProofResponse,
2422
transports::Transport,
2523
};
26-
use alloy_primitives::{Address, BlockNumber, Bytes, StorageKey, StorageValue, B256, U256};
24+
use alloy_primitives::{
25+
map::{hash_map, AddressHashMap, B256HashMap, B256HashSet, HashSet},
26+
Address, BlockNumber, Bytes, StorageKey, StorageValue, B256, U256,
27+
};
2728
use anyhow::{ensure, Context, Result};
2829
use revm::{
2930
primitives::{AccountInfo, Bytecode},
@@ -32,20 +33,18 @@ use revm::{
3233

3334
/// A simple revm [Database] wrapper that records all DB queries.
3435
pub struct ProofDb<D> {
35-
accounts: HashMap<Address, HashSet<StorageKey>>,
36-
contracts: HashMap<B256, Bytes>,
36+
accounts: AddressHashMap<B256HashSet>,
37+
contracts: B256HashMap<Bytes>,
3738
block_hash_numbers: HashSet<BlockNumber>,
38-
39-
proofs: HashMap<Address, AccountProof>,
40-
39+
proofs: AddressHashMap<AccountProof>,
4140
inner: D,
4241
}
4342

4443
struct AccountProof {
4544
/// The inclusion proof for this account.
4645
account_proof: Vec<Bytes>,
4746
/// The MPT inclusion proofs for several storage slots.
48-
storage_proofs: HashMap<StorageKey, StorageProof>,
47+
storage_proofs: B256HashMap<StorageProof>,
4948
}
5049

5150
struct StorageProof {
@@ -59,11 +58,10 @@ impl<D: Database> ProofDb<D> {
5958
/// Creates a new ProofDb instance, with a [Database].
6059
pub fn new(db: D) -> Self {
6160
Self {
62-
accounts: HashMap::new(),
63-
contracts: HashMap::new(),
64-
block_hash_numbers: HashSet::new(),
65-
66-
proofs: HashMap::new(),
61+
accounts: Default::default(),
62+
contracts: Default::default(),
63+
block_hash_numbers: Default::default(),
64+
proofs: Default::default(),
6765
inner: db,
6866
}
6967
}
@@ -76,7 +74,7 @@ impl<D: Database> ProofDb<D> {
7674
}
7775

7876
/// Returns the referenced contracts
79-
pub fn contracts(&self) -> &HashMap<B256, Bytes> {
77+
pub fn contracts(&self) -> &B256HashMap<Bytes> {
8078
&self.contracts
8179
}
8280

@@ -176,7 +174,7 @@ impl<T: Transport + Clone, N: Network, P: Provider<T, N>> ProofDb<AlloyDb<T, N,
176174
.flat_map(|proof| proof.account_proof.iter());
177175
let state_trie = MerkleTrie::from_rlp_nodes(state_nodes).context("accountProof invalid")?;
178176

179-
let mut storage_tries = HashMap::new();
177+
let mut storage_tries = B256HashMap::default();
180178
for (address, storage_keys) in &self.accounts {
181179
// if no storage keys have been accessed, we don't need to prove anything
182180
if storage_keys.is_empty() {
@@ -258,7 +256,7 @@ fn filter_existing_keys(account_proof: Option<&AccountProof>) -> impl Fn(&Storag
258256
}
259257

260258
fn add_proof(
261-
proofs: &mut HashMap<Address, AccountProof>,
259+
proofs: &mut AddressHashMap<AccountProof>,
262260
proof_response: EIP1186AccountProofResponse,
263261
) -> Result<()> {
264262
// convert the response into a StorageProof
@@ -277,15 +275,15 @@ fn add_proof(
277275
.collect();
278276

279277
match proofs.entry(proof_response.address) {
280-
Entry::Occupied(mut entry) => {
278+
hash_map::Entry::Occupied(mut entry) => {
281279
let account_proof = entry.get_mut();
282280
ensure!(
283281
account_proof.account_proof == proof_response.account_proof,
284282
"account_proof does not match"
285283
);
286284
account_proof.storage_proofs.extend(storage_proofs);
287285
}
288-
Entry::Vacant(entry) => {
286+
hash_map::Entry::Vacant(entry) => {
289287
entry.insert(AccountProof {
290288
account_proof: proof_response.account_proof,
291289
storage_proofs,

steel/src/mpt.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{collections::HashMap, fmt::Debug};
15+
use std::fmt::Debug;
1616

17-
use alloy_primitives::{b256, keccak256, B256};
17+
use alloy_primitives::{b256, keccak256, map::B256HashMap, B256};
1818
use alloy_rlp::{BufMut, Decodable, Encodable, Header, PayloadView, EMPTY_STRING_CODE};
1919
use nybbles::Nibbles;
2020
use serde::{Deserialize, Serialize};
@@ -76,7 +76,7 @@ impl MerkleTrie {
7676
pub fn from_rlp_nodes<T: AsRef<[u8]>>(
7777
nodes: impl IntoIterator<Item = T>,
7878
) -> alloy_rlp::Result<Self> {
79-
let mut nodes_by_hash = HashMap::new();
79+
let mut nodes_by_hash = B256HashMap::default();
8080
let mut root_node_opt = None;
8181

8282
for rlp in nodes {
@@ -332,7 +332,7 @@ fn parse_node(rlp: impl AsRef<[u8]>) -> alloy_rlp::Result<(Option<B256>, Node)>
332332
Ok(((rlp.len() >= 32).then(|| keccak256(rlp)), node))
333333
}
334334

335-
fn resolve_trie(root: Node, nodes_by_hash: &HashMap<B256, Node>) -> Node {
335+
fn resolve_trie(root: Node, nodes_by_hash: &B256HashMap<Node>) -> Node {
336336
match root {
337337
Node::Null | Node::Leaf(..) => root,
338338
Node::Extension(prefix, child) => {

steel/src/state.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
use std::{convert::Infallible, fmt::Debug, rc::Rc};
1616

1717
use crate::mpt::{MerkleTrie, EMPTY_ROOT_HASH};
18-
use alloy_primitives::{keccak256, Address, Bytes, TxNumber, B256, U256};
18+
use alloy_primitives::{
19+
keccak256,
20+
map::{AddressHashMap, B256HashMap, HashMap},
21+
Address, Bytes, TxNumber, B256, U256,
22+
};
1923
use alloy_rlp_derive::{RlpDecodable, RlpEncodable};
2024
use revm::{
21-
primitives::{AccountInfo, Bytecode, HashMap, KECCAK_EMPTY},
25+
primitives::{AccountInfo, Bytecode, KECCAK_EMPTY},
2226
Database,
2327
};
2428

@@ -35,9 +39,9 @@ pub struct StateDb {
3539
state_trie: MerkleTrie,
3640
/// Storage MPTs to their root hash.
3741
/// [Rc] is used fore MPT deduplication.
38-
storage_tries: HashMap<B256, Rc<MerkleTrie>>,
42+
storage_tries: B256HashMap<Rc<MerkleTrie>>,
3943
/// Contracts by their hash.
40-
contracts: HashMap<B256, Bytes>,
44+
contracts: B256HashMap<Bytes>,
4145
/// Block hashes by their number.
4246
block_hashes: HashMap<u64, B256>,
4347
}
@@ -103,15 +107,15 @@ impl StateDb {
103107
/// account.
104108
pub struct WrapStateDb<'a> {
105109
inner: &'a StateDb,
106-
account_storage: HashMap<Address, Option<Rc<MerkleTrie>>>,
110+
account_storage: AddressHashMap<Option<Rc<MerkleTrie>>>,
107111
}
108112

109113
impl<'a> WrapStateDb<'a> {
110114
/// Creates a new [Database] from the given [StateDb].
111115
pub fn new(inner: &'a StateDb) -> Self {
112116
Self {
113117
inner,
114-
account_storage: HashMap::new(),
118+
account_storage: Default::default(),
115119
}
116120
}
117121
}

0 commit comments

Comments
 (0)