Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WEB3-146: Replace map types with alloy_primitives::map #256

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions steel/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

use crate::{state::StateDb, EvmBlockHeader, EvmEnv, GuestEvmEnv, MerkleTrie};
use ::serde::{Deserialize, Serialize};
use alloy_primitives::Bytes;
use revm::primitives::HashMap;
use alloy_primitives::{map::HashMap, Bytes};

/// Input committing to the corresponding execution block hash.
#[derive(Clone, Serialize, Deserialize)]
Expand All @@ -38,7 +37,8 @@ impl<H: EvmBlockHeader> BlockInput<H> {
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();
Expand Down
8 changes: 4 additions & 4 deletions steel/src/host/db/alloy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// 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::{
network::{BlockResponse, HeaderResponse, Network},
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,
Expand All @@ -44,7 +44,7 @@ pub struct AlloyDb<T: Transport + Clone, N: Network, P: Provider<T, N>> {
/// Handle to the Tokio runtime.
handle: Handle,
/// Bytecode cache to allow querying bytecode by hash instead of address.
contracts: HashMap<B256, Bytecode>,
contracts: B256HashMap<Bytecode>,

phantom: PhantomData<fn() -> (T, N)>,
}
Expand All @@ -59,7 +59,7 @@ impl<T: Transport + Clone, N: Network, P: Provider<T, N>> AlloyDb<T, N, P> {
provider_config: config,
block_hash,
handle: Handle::current(),
contracts: HashMap::new(),
contracts: Default::default(),
phantom: PhantomData,
}
}
Expand Down
36 changes: 17 additions & 19 deletions steel/src/host/db/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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},
Expand All @@ -32,20 +33,18 @@ use revm::{

/// A simple revm [Database] wrapper that records all DB queries.
pub struct ProofDb<D> {
accounts: HashMap<Address, HashSet<StorageKey>>,
contracts: HashMap<B256, Bytes>,
accounts: AddressHashMap<B256HashSet>,
contracts: B256HashMap<Bytes>,
block_hash_numbers: HashSet<BlockNumber>,

proofs: HashMap<Address, AccountProof>,

proofs: AddressHashMap<AccountProof>,
inner: D,
}

struct AccountProof {
/// The inclusion proof for this account.
account_proof: Vec<Bytes>,
/// The MPT inclusion proofs for several storage slots.
storage_proofs: HashMap<StorageKey, StorageProof>,
storage_proofs: B256HashMap<StorageProof>,
}

struct StorageProof {
Expand All @@ -59,11 +58,10 @@ impl<D: Database> ProofDb<D> {
/// 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,
}
}
Expand All @@ -76,7 +74,7 @@ impl<D: Database> ProofDb<D> {
}

/// Returns the referenced contracts
pub fn contracts(&self) -> &HashMap<B256, Bytes> {
pub fn contracts(&self) -> &B256HashMap<Bytes> {
&self.contracts
}

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

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

fn add_proof(
proofs: &mut HashMap<Address, AccountProof>,
proofs: &mut AddressHashMap<AccountProof>,
proof_response: EIP1186AccountProofResponse,
) -> Result<()> {
// convert the response into a StorageProof
Expand All @@ -277,15 +275,15 @@ 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,
"account_proof does not match"
);
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,
Expand Down
8 changes: 4 additions & 4 deletions steel/src/mpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -76,7 +76,7 @@ impl MerkleTrie {
pub fn from_rlp_nodes<T: AsRef<[u8]>>(
nodes: impl IntoIterator<Item = T>,
) -> alloy_rlp::Result<Self> {
let mut nodes_by_hash = HashMap::new();
let mut nodes_by_hash = B256HashMap::default();
let mut root_node_opt = None;

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

fn resolve_trie(root: Node, nodes_by_hash: &HashMap<B256, Node>) -> Node {
fn resolve_trie(root: Node, nodes_by_hash: &B256HashMap<Node>) -> Node {
match root {
Node::Null | Node::Leaf(..) => root,
Node::Extension(prefix, child) => {
Expand Down
16 changes: 10 additions & 6 deletions steel/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
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, HashMap, KECCAK_EMPTY},
primitives::{AccountInfo, Bytecode, KECCAK_EMPTY},
Database,
};

Expand All @@ -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<B256, Rc<MerkleTrie>>,
storage_tries: B256HashMap<Rc<MerkleTrie>>,
/// Contracts by their hash.
contracts: HashMap<B256, Bytes>,
contracts: B256HashMap<Bytes>,
/// Block hashes by their number.
block_hashes: HashMap<u64, B256>,
}
Expand Down Expand Up @@ -103,15 +107,15 @@ impl StateDb {
/// account.
pub struct WrapStateDb<'a> {
inner: &'a StateDb,
account_storage: HashMap<Address, Option<Rc<MerkleTrie>>>,
account_storage: AddressHashMap<Option<Rc<MerkleTrie>>>,
}

impl<'a> WrapStateDb<'a> {
/// Creates a new [Database] from the given [StateDb].
pub fn new(inner: &'a StateDb) -> Self {
Self {
inner,
account_storage: HashMap::new(),
account_storage: Default::default(),
}
}
}
Expand Down
Loading