Skip to content

Commit

Permalink
feat: Use risc0-ethereum-trie (#140)
Browse files Browse the repository at this point in the history
* update risc0

* patch homestead sigs

* patch trie db

* revert to MemDB

* feat: hashbrown perf

* fix batching changeset updates

* downgrade journal mismatch to warning message

* copyright years

* todo nit

* readme nit

* migrate to steel-trie

* use public risc0-ethereum-trie

* fix Cargo.lock

* Apply suggestions from code review

Co-authored-by: Rami <2418646+hashcashier@users.noreply.github.com>

* Fix unresolvable key endianness

* minor improvements

* upgrade risc0-ethereum-trie

* switch to main

* update Cargo.lock

---------

Co-authored-by: Rami Khalil <the.rami.khalil@gmail.com>
Co-authored-by: Rami <2418646+hashcashier@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 14, 2025
1 parent becc6a2 commit 8e5663a
Show file tree
Hide file tree
Showing 20 changed files with 550 additions and 1,935 deletions.
149 changes: 113 additions & 36 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ features = ["unstable"]
version = "1.2.2"
features = ["unstable"]

[workspace.dependencies.risc0-ethereum-trie]
git = "https://github.com/risc0/risc0-ethereum"
features = ["orphan", "rkyv", "rlp_serialize", "serde"]

# External
[workspace.dependencies]
# Alloy
Expand Down Expand Up @@ -81,6 +85,7 @@ zeth-preflight = { path = "crates/preflight" }
zeth-preflight-ethereum = { path = "crates/preflight-ethereum" }
zeth-preflight-optimism = { path = "crates/preflight-optimism" }


# Others
anyhow = "1.0.89"
async-trait = "0.1.83"
Expand All @@ -89,6 +94,7 @@ bytemuck = "1.19.0"
clap = { version = "4.0", features = ["derive"] }
env_logger = "0.11.5"
hashbrown = { version = "0.15.2", features = ["rayon"] }
itertools = "0.14"
k256 = { version = "0.13.3", features = ["serde", "pem"] }
log = "0.4.22"
flate2 = "1.0.34"
Expand All @@ -98,7 +104,5 @@ rkyv = { version = "0.8.9", features = ["hashbrown-0_15"] }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = { version = "1.0.128", features = ["alloc"] }
serde_with = "3.11.0"
thiserror = "1.0.64"
tiny-keccak = "2.0.2"
tokio = { version = "1.41.0", features = ["full"] }
tracing = { version = "0.1.40", features = ["log"] }
11 changes: 5 additions & 6 deletions bin/benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use alloy::primitives::U256;
use alloy::primitives::{keccak256, U256};
use alloy_chains::NamedChain;
use clap::Parser;
use std::process::Command;
use tracing::{error, info};
use zeth::cli::ProveArgs;
use zeth_core::keccak::keccak;

#[derive(clap::Parser, Debug, Clone)]
#[command(name = "zeth-benchmark")]
Expand Down Expand Up @@ -57,7 +56,7 @@ fn main() {
let build_args = &cli.prove_args.run_args.build_args;
let chain_id = build_args.chain.or(cli.chain_id).unwrap();
// generate sequence of starting block numbers to benchmark
let seed = keccak(
let seed = keccak256(
[
(chain_id as u64).to_be_bytes(),
build_args.block_number.to_be_bytes(),
Expand All @@ -70,9 +69,9 @@ fn main() {
let block_numbers = (0..cli.sample_count)
.map(|i| {
build_args.block_number
+ U256::from_be_bytes(keccak(
[seed.as_slice(), i.to_be_bytes().as_slice()].concat(),
))
+ U256::from_be_bytes(
keccak256([seed.as_slice(), i.to_be_bytes().as_slice()].concat()).0,
)
.reduce_mod(U256::from(cli.sample_range))
.to::<u64>()
})
Expand Down
3 changes: 1 addition & 2 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ k256.workspace = true
pot.workspace = true
rkyv.workspace = true
serde.workspace = true
thiserror.workspace = true
tiny-keccak.workspace = true
risc0-ethereum-trie.workspace = true

reth-chainspec.workspace = true
reth-primitives.workspace = true
Expand Down
11 changes: 4 additions & 7 deletions crates/core/src/db/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::keccak::keccak;
use crate::map::NoMapHasher;
use crate::mpt::MptNode;
use crate::rescue::Recoverable;
use crate::stateless::data::StorageEntry;
use alloy_consensus::Account;
use alloy_primitives::map::{AddressHashMap, B256HashMap, HashMap};
use alloy_primitives::{Address, B256, U256};
use alloy_primitives::{keccak256, Address, B256, U256};
use reth_primitives::revm_primitives::db::Database;
use reth_primitives::revm_primitives::{AccountInfo, Bytecode};
use reth_revm::DatabaseRef;
use reth_storage_errors::provider::ProviderError;

#[derive(Default)]
pub struct TrieDB {
pub accounts: MptNode,
pub accounts: MptNode<Account>,
pub storage: AddressHashMap<StorageEntry>,
pub contracts: B256HashMap<Bytecode>,
pub block_hashes: HashMap<u64, B256, NoMapHasher>,
Expand All @@ -45,8 +44,7 @@ impl DatabaseRef for TrieDB {
fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
Ok(self
.accounts
.get_rlp::<Account>(&keccak(address))
.unwrap()
.get_rlp(keccak256(address))?
.map(|acc| AccountInfo {
balance: acc.balance,
nonce: acc.nonce,
Expand All @@ -63,8 +61,7 @@ impl DatabaseRef for TrieDB {
let entry = self.storage.get(&address).unwrap();
Ok(entry
.storage_trie
.get_rlp(&keccak(index.to_be_bytes::<32>()))
.unwrap()
.get_rlp(keccak256(index.to_be_bytes::<32>()))?
.unwrap_or_default())
}

Expand Down
32 changes: 0 additions & 32 deletions crates/core/src/keccak.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

pub mod db;
pub mod driver;
pub mod keccak;
pub mod map;
pub mod mpt;
pub mod rescue;
Expand Down
Loading

0 comments on commit 8e5663a

Please sign in to comment.