Skip to content

Commit

Permalink
feat: migrate from ethers to alloy
Browse files Browse the repository at this point in the history
  • Loading branch information
dyxushuai committed Feb 22, 2024
1 parent e7f6973 commit 38b07e3
Show file tree
Hide file tree
Showing 22 changed files with 2,529 additions and 4,476 deletions.
854 changes: 375 additions & 479 deletions Cargo.lock

Large diffs are not rendered by default.

1,042 changes: 335 additions & 707 deletions guests/eth-block/Cargo.lock

Large diffs are not rendered by default.

1,042 changes: 335 additions & 707 deletions guests/op-block/Cargo.lock

Large diffs are not rendered by default.

1,042 changes: 335 additions & 707 deletions guests/op-compose/Cargo.lock

Large diffs are not rendered by default.

1,042 changes: 335 additions & 707 deletions guests/op-derive/Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ alloy-primitives = { version = "0.6.2", default-features = false, features = [
"serde",
"std",
] }
alloy = { git = "https://github.com/alloy-rs/alloy.git", features = [
"provider-http",
"rpc-types-eth",
] }
alloy-sol-types = "0.6"
anyhow = "1.0"
bincode = "1.3.3"
bonsai-sdk = { workspace = true }
bytemuck = "1.13"
clap = { version = "4.0", features = ["derive"] }
env_logger = "0.11"
ethers-contract = { version = "2.0", features = ["optimism"] }
ethers-core = { version = "2.0", features = ["optimism"] }
ethers-providers = { version = "2.0", features = ["optimism"] }
hex = "0.4.3"
log = "0.4"
risc0-zkvm = { workspace = true, features = ["prove"] }
Expand Down
9 changes: 6 additions & 3 deletions host/src/operations/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

use std::fmt::Debug;

use alloy::rpc::types::eth::Transaction as AlloyTransaction;
use anyhow::Context;
use ethers_core::types::Transaction as EthersTransaction;
use log::{info, warn};
use risc0_zkvm::{compute_image_id, Receipt};
use serde::{Deserialize, Serialize};
Expand All @@ -40,8 +40,11 @@ pub async fn build_block<N: BlockBuilderStrategy>(
guest_elf: &[u8],
) -> anyhow::Result<Option<(String, Receipt)>>
where
N::TxEssence: 'static + Send + TryFrom<EthersTransaction> + Serialize + Deserialize<'static>,
<N::TxEssence as TryFrom<EthersTransaction>>::Error: Debug,
N::TxEssence: 'static
+ Send
+ TryFrom<AlloyTransaction, Error = anyhow::Error>
+ Serialize
+ Deserialize<'static>,
{
let build_args = cli.build_args().clone();
if build_args.block_count > 1 {
Expand Down
18 changes: 13 additions & 5 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,33 @@ alloy-rlp-derive = { version = "0.3", default-features = false }
alloy-sol-types = "0.6"
anyhow = "1.0"
bytes = "1.5"
ethers-core = { version = "2.0", features = ["optimism"] }
hashbrown = { workspace = true }
libflate = "2.0.0"
once_cell = "1.18"
revm = { workspace = true }
risc0-zkvm = { version = "0.20.1", default-features = false, features = ['std'] }
risc0-zkvm = { version = "0.20.1", default-features = false, features = [
'std',
] }
ruint = { version = "1.10", default-features = false }
serde = "1.0"
thiserror = "1.0"
zeth-primitives = { path = "../primitives", features = ["revm", "ethers"] }
zeth-primitives = { path = "../primitives", features = ["revm", "alloy"] }
k256 = { version = "=0.13.3", features = [
"std",
"ecdsa",
], default_features = false }

[target.'cfg(not(target_os = "zkvm"))'.dependencies]
chrono = { version = "0.4", default-features = false }
ethers-providers = { version = "2.0", features = ["optimism"] }
alloy = { git = "https://github.com/alloy-rs/alloy.git", features = [
"provider-http",
"rpc-types-eth",
] }
flate2 = "1.0.26"
log = "0.4"
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
tokio = { version = "1.23", features = ["full"] }
zeth-primitives = { path = "../primitives", features = ["revm", "ethers"] }
zeth-primitives = { path = "../primitives", features = ["revm", "alloy"] }

[dev-dependencies]
bincode = "1.3"
Expand Down
83 changes: 44 additions & 39 deletions lib/src/host/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ use std::{
sync::{Arc, Mutex},
};

use anyhow::{anyhow, Context, Result};
use ethers_core::types::{
Block as EthersBlock, EIP1186ProofResponse, Transaction as EthersTransaction,
use alloy::rpc::types::eth::{
Block as AlloyBlock, BlockTransactions, EIP1186AccountProofResponse,
Transaction as AlloyTransaction,
};
use anyhow::{anyhow, Context, Result};
use hashbrown::{HashMap, HashSet};
use log::{debug, info};
use zeth_primitives::{
block::Header,
ethers::{from_ethers_h160, from_ethers_h256, from_ethers_u256},
keccak::keccak,
transactions::{Transaction, TxEssence},
trie::{MptNode, MptNodeReference},
Expand All @@ -51,11 +51,11 @@ use crate::{
pub struct Data<E: TxEssence> {
pub db: MemDb,
pub parent_header: Header,
pub parent_proofs: HashMap<Address, EIP1186ProofResponse>,
pub parent_proofs: HashMap<Address, EIP1186AccountProofResponse>,
pub header: Option<Header>,
pub transactions: Vec<Transaction<E>>,
pub withdrawals: Vec<Withdrawal>,
pub proofs: HashMap<Address, EIP1186ProofResponse>,
pub proofs: HashMap<Address, EIP1186AccountProofResponse>,
pub ancestor_headers: Vec<Header>,
}

Expand All @@ -79,8 +79,7 @@ pub trait Preflight<E: TxEssence> {
/// Implements the [Preflight] trait for all compatible [BlockBuilderStrategy]s.
impl<N: BlockBuilderStrategy> Preflight<N::TxEssence> for N
where
N::TxEssence: TryFrom<EthersTransaction>,
<N::TxEssence as TryFrom<EthersTransaction>>::Error: Debug,
N::TxEssence: TryFrom<AlloyTransaction, Error = anyhow::Error>,
{
fn preflight_with_external_data(
chain_spec: &ChainSpec,
Expand All @@ -97,18 +96,21 @@ where

debug!(
"Initial block: {:?} ({:?})",
parent_block.number.unwrap(),
parent_block.hash.unwrap()
parent_block.header.number.unwrap(),
parent_block.header.hash.unwrap()
);
let parent_header: Header = parent_block.try_into().context("invalid parent block")?;
let parent_header: Header = parent_block
.header
.try_into()
.context("invalid parent block")?;

// Fetch the target block
let block = provider.get_full_block(&BlockQuery { block_no })?;

debug!(
"Final block number: {:?} ({:?})",
block.number.unwrap(),
block.hash.unwrap()
block.header.number.unwrap(),
block.header.hash.unwrap()
);
debug!("Transaction count: {:?}", block.transactions.len());

Expand All @@ -121,7 +123,8 @@ where
// Create the block builder, run the transactions and extract the DB
Self::preflight_with_local_data(chain_spec, provider_db, input).map(
move |mut headerless_preflight_data| {
headerless_preflight_data.header = Some(block.try_into().expect("invalid block"));
headerless_preflight_data.header =
Some(block.header.try_into().expect("invalid block"));
headerless_preflight_data
},
)
Expand Down Expand Up @@ -177,24 +180,22 @@ where
}
}

fn new_preflight_input<E>(
block: EthersBlock<EthersTransaction>,
parent_header: Header,
) -> Result<BlockBuildInput<E>>
fn new_preflight_input<E>(block: AlloyBlock, parent_header: Header) -> Result<BlockBuildInput<E>>
where
E: TxEssence + TryFrom<EthersTransaction>,
<E as TryFrom<EthersTransaction>>::Error: Debug,
E: TxEssence + TryFrom<AlloyTransaction, Error = anyhow::Error>,
{
// convert each transaction
let transactions = block
.transactions
.into_iter()
.enumerate()
.map(|(i, tx)| {
tx.try_into()
.map_err(|err| anyhow!("transaction {i} invalid: {err:?}"))
})
.collect::<Result<Vec<_>, _>>()?;
let transactions = match block.transactions {
BlockTransactions::Full(txs) => txs
.into_iter()
.enumerate()
.map(|(i, tx)| {
tx.try_into()
.map_err(|err| anyhow!("transaction {i} invalid: {err:?}"))
})
.collect::<Result<Vec<_>, _>>()?,
_ => unreachable!(),
};
// convert each withdrawal
let withdrawals = block
.withdrawals
Expand All @@ -210,11 +211,11 @@ where
let input = BlockBuildInput {
state_input: StateInput {
parent_header,
beneficiary: from_ethers_h160(block.author.context("author missing")?),
gas_limit: from_ethers_u256(block.gas_limit),
timestamp: from_ethers_u256(block.timestamp),
extra_data: block.extra_data.0.into(),
mix_hash: from_ethers_h256(block.mix_hash.context("mix_hash missing")?),
beneficiary: block.header.miner,
gas_limit: block.header.gas_limit,
timestamp: block.header.timestamp,
extra_data: block.header.extra_data,
mix_hash: block.header.mix_hash.context("mix_hash missing")?,
transactions,
withdrawals,
},
Expand Down Expand Up @@ -281,8 +282,8 @@ impl<E: TxEssence> TryFrom<Data<E>> for BlockBuildInput<E> {

fn proofs_to_tries(
state_root: B256,
parent_proofs: HashMap<Address, EIP1186ProofResponse>,
proofs: HashMap<Address, EIP1186ProofResponse>,
parent_proofs: HashMap<Address, EIP1186AccountProofResponse>,
proofs: HashMap<Address, EIP1186AccountProofResponse>,
) -> Result<(MptNode, HashMap<Address, StorageEntry>)> {
// if no addresses are provided, return the trie only consisting of the state root
if parent_proofs.is_empty() {
Expand Down Expand Up @@ -315,7 +316,7 @@ fn proofs_to_tries(
add_orphaned_leafs(address, &fini_proofs.account_proof, &mut state_nodes)?;

// if no slots are provided, return the trie only consisting of the storage root
let storage_root = from_ethers_h256(proof.storage_hash);
let storage_root = proof.storage_hash;
if proof.storage_proof.is_empty() {
storage.insert(address, (storage_root.into(), vec![]));
continue;
Expand All @@ -340,7 +341,11 @@ fn proofs_to_tries(

// assure that slots can be deleted from the storage trie
for storage_proof in &fini_proofs.storage_proof {
add_orphaned_leafs(storage_proof.key, &storage_proof.proof, &mut storage_nodes)?;
add_orphaned_leafs(
storage_proof.key.0,
&storage_proof.proof,
&mut storage_nodes,
)?;
}
// create the storage trie, from all the relevant nodes
let storage_trie = resolve_nodes(&storage_root_node, &storage_nodes);
Expand All @@ -350,7 +355,7 @@ fn proofs_to_tries(
let slots = proof
.storage_proof
.iter()
.map(|p| U256::from_be_bytes(p.key.into()))
.map(|p| U256::from_be_bytes(p.key.0.into()))
.collect();
storage.insert(address, (storage_trie, slots));
}
Expand Down
16 changes: 7 additions & 9 deletions lib/src/host/provider/cached_rpc_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@

use std::path::PathBuf;

use anyhow::{Context, Result};
use ethers_core::types::{
Block, Bytes, EIP1186ProofResponse, Transaction, TransactionReceipt, H256, U256,
};

use super::{
file_provider::FileProvider, rpc_provider::RpcProvider, AccountQuery, BlockQuery, MutProvider,
ProofQuery, Provider, StorageQuery,
};
use alloy::rpc::types::eth::{Block, EIP1186AccountProofResponse, TransactionReceipt};
use anyhow::{Context, Result};
use zeth_primitives::{Bytes, U256};

pub struct CachedRpcProvider {
cache: FileProvider,
Expand All @@ -43,7 +41,7 @@ impl Provider for CachedRpcProvider {
self.cache.save()
}

fn get_full_block(&mut self, query: &BlockQuery) -> Result<Block<Transaction>> {
fn get_full_block(&mut self, query: &BlockQuery) -> Result<Block> {
let cache_out = self.cache.get_full_block(query);
if cache_out.is_ok() {
return cache_out;
Expand All @@ -55,7 +53,7 @@ impl Provider for CachedRpcProvider {
Ok(out)
}

fn get_partial_block(&mut self, query: &BlockQuery) -> Result<Block<H256>> {
fn get_partial_block(&mut self, query: &BlockQuery) -> Result<Block> {
let cache_out = self.cache.get_partial_block(query);
if cache_out.is_ok() {
return cache_out;
Expand All @@ -79,7 +77,7 @@ impl Provider for CachedRpcProvider {
Ok(out)
}

fn get_proof(&mut self, query: &ProofQuery) -> Result<EIP1186ProofResponse> {
fn get_proof(&mut self, query: &ProofQuery) -> Result<EIP1186AccountProofResponse> {
let cache_out = self.cache.get_proof(query);
if cache_out.is_ok() {
return cache_out;
Expand Down Expand Up @@ -127,7 +125,7 @@ impl Provider for CachedRpcProvider {
Ok(out)
}

fn get_storage(&mut self, query: &StorageQuery) -> Result<H256> {
fn get_storage(&mut self, query: &StorageQuery) -> Result<U256> {
let cache_out = self.cache.get_storage(query);
if cache_out.is_ok() {
return cache_out;
Expand Down
Loading

0 comments on commit 38b07e3

Please sign in to comment.