Skip to content

Commit

Permalink
Merge branch 'main' of github.com:risc0/zeth into rkhalil/1.2.1-rc.0-…
Browse files Browse the repository at this point in the history
…perf
  • Loading branch information
hashcashier committed Dec 12, 2024
2 parents 73a33b6 + 4c6a74f commit 37e3985
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 82 deletions.
6 changes: 1 addition & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pot = "3.0.1"
rkyv = "0.8.9"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = { version = "1.0.128", features = ["alloc"] }
serde_with = "3.7.0"
serde_with = "3.11.0"
thiserror = "1.0.64"
tiny-keccak = "2.0.2"
tokio = { version = "1.41.0", features = ["full"] }
Expand Down
2 changes: 0 additions & 2 deletions bin/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ workspace = true
[dependencies]
alloy.workspace = true
alloy-chains.workspace = true
anyhow.workspace = true
clap.workspace = true
env_logger.workspace = true
tracing.workspace = true
log = "0.4.22"

[features]
disable-dev-mode = ["zeth/disable-dev-mode"]
Expand Down
27 changes: 17 additions & 10 deletions crates/core-ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod chain_spec;

use crate::chain_spec::{DEV, HOLESKY, MAINNET, SEPOLIA};
use anyhow::Context;
use k256::ecdsa::signature::hazmat::PrehashVerifier;
use k256::ecdsa::VerifyingKey;
use reth_chainspec::{ChainSpec, NamedChain};
use reth_consensus::Consensus;
Expand All @@ -25,7 +26,7 @@ use reth_evm::execute::{
};
use reth_evm_ethereum::execute::EthExecutorProvider;
use reth_primitives::revm_primitives::alloy_primitives::{BlockNumber, Sealable};
use reth_primitives::revm_primitives::{B256, U256};
use reth_primitives::revm_primitives::{Address, B256, U256};
use reth_primitives::{Block, Header, Receipt, SealedHeader, TransactionSigned};
use reth_revm::db::BundleState;
use reth_storage_errors::provider::ProviderError;
Expand All @@ -34,7 +35,6 @@ use std::mem::take;
use std::sync::Arc;
use zeth_core::db::MemoryDB;
use zeth_core::driver::CoreDriver;
use zeth_core::recover_sender;
use zeth_core::stateless::client::StatelessClient;
use zeth_core::stateless::execute::ExecutionStrategy;
use zeth_core::stateless::finalize::RethFinalizationStrategy;
Expand Down Expand Up @@ -110,21 +110,28 @@ where
// Instantiate execution engine using database
let mut executor = EthExecutorProvider::ethereum(chain_spec.clone())
.batch_executor(db.take().expect("Missing database."));
// Recover transaction signer addresses with non-det vk hints
let senders = core::iter::zip(signers, block.body.transactions())
.map(|(vk, tx)| {
recover_sender(vk, *tx.signature(), tx.signature_hash())
.expect("Sender recovery failed")
})
.collect::<Vec<_>>();
// Verify the transaction signatures and compute senders
let mut senders = Vec::with_capacity(block.body.transactions.len());
for (i, tx) in block.body.transactions().enumerate() {
let vk = &signers[i];
let sig = tx.signature();

sig.to_k256()
.and_then(|sig| vk.verify_prehash(tx.signature_hash().as_slice(), &sig))
.with_context(|| format!("invalid signature for tx {i}"))?;

senders.push(Address::from_public_key(vk))
}

// Execute transactions
let block_with_senders = take(block).with_senders_unchecked(senders);
executor
.execute_and_verify_one(BlockExecutionInput {
block: &block_with_senders,
total_difficulty: *total_difficulty,
})
.expect("Execution failed");
.context("execution failed")?;

// Return block
*block = block_with_senders.block;
// Return bundle state
Expand Down
1 change: 1 addition & 0 deletions crates/core-optimism/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ alloy-genesis.workspace = true
anyhow.workspace = true
k256.workspace = true
once_cell.workspace = true
op-alloy-consensus.workspace = true

reth-chainspec.workspace = true
reth-consensus.workspace = true
Expand Down
44 changes: 26 additions & 18 deletions crates/core-optimism/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// limitations under the License.

use anyhow::Context;
use k256::ecdsa::signature::hazmat::PrehashVerifier;
use k256::ecdsa::VerifyingKey;
use op_alloy_consensus::TxDeposit;
use reth_chainspec::NamedChain;
use reth_consensus::Consensus;
use reth_evm::execute::{
Expand All @@ -25,16 +27,15 @@ use reth_optimism_chainspec::{
use reth_optimism_consensus::OptimismBeaconConsensus;
use reth_optimism_evm::OpExecutorProvider;
use reth_primitives::revm_primitives::alloy_primitives::{BlockNumber, Sealable};
use reth_primitives::revm_primitives::{B256, U256};
use reth_primitives::{Block, Header, Receipt, SealedHeader, TransactionSigned, TxType};
use reth_primitives::revm_primitives::{Address, B256, U256};
use reth_primitives::{Block, Header, Receipt, SealedHeader, Transaction, TransactionSigned};
use reth_revm::db::BundleState;
use reth_storage_errors::provider::ProviderError;
use std::fmt::Display;
use std::mem::take;
use std::sync::Arc;
use zeth_core::db::MemoryDB;
use zeth_core::driver::CoreDriver;
use zeth_core::recover_sender;
use zeth_core::stateless::client::StatelessClient;
use zeth_core::stateless::execute::ExecutionStrategy;
use zeth_core::stateless::finalize::RethFinalizationStrategy;
Expand Down Expand Up @@ -109,28 +110,35 @@ where
// Instantiate execution engine using database
let mut executor = OpExecutorProvider::optimism(chain_spec.clone())
.batch_executor(db.take().expect("Missing database"));
// Recover transaction signer addresses with non-det vk hints
let mut vk = signers.iter();
let senders = block
.body
.transactions()
.map(|tx| {
if matches!(tx.tx_type(), TxType::Deposit) {
tx.recover_signer().unwrap()
} else {
recover_sender(vk.next().unwrap(), *tx.signature(), tx.signature_hash())
.expect("Sender recovery failed")
}
})
.collect::<Vec<_>>();
// Verify the transaction signatures and compute senders
let mut vk_it = signers.iter();
let mut senders = Vec::with_capacity(block.body.transactions.len());
for (i, tx) in block.body.transactions().enumerate() {
let sender = if let Transaction::Deposit(TxDeposit { from, .. }) = tx.transaction {
// Deposit transactions are unsigned and contain the sender
from
} else {
let vk = vk_it.next().unwrap();
let sig = tx.signature();

sig.to_k256()
.and_then(|sig| vk.verify_prehash(tx.signature_hash().as_slice(), &sig))
.with_context(|| format!("invalid signature for tx {i}"))?;

Address::from_public_key(vk)
};
senders.push(sender);
}

// Execute transactions
let block_with_senders = take(block).with_senders_unchecked(senders);
executor
.execute_and_verify_one(BlockExecutionInput {
block: &block_with_senders,
total_difficulty: *total_difficulty,
})
.expect("Execution failed");
.context("execution failed")?;

// Return block
*block = block_with_senders.block;
// Return bundle state
Expand Down
23 changes: 0 additions & 23 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::keccak::keccak;
use alloy_primitives::{Address, Signature, B256};
use k256::ecdsa::signature::hazmat::PrehashVerifier;
use k256::ecdsa::VerifyingKey;
use k256::elliptic_curve::sec1::ToEncodedPoint;
use k256::PublicKey;

pub mod db;
pub mod driver;
pub mod keccak;
pub mod mpt;
pub mod rescue;
pub mod stateless;

pub fn recover_sender(
verifying_key: &VerifyingKey,
signature: Signature,
transaction_hash: B256,
) -> anyhow::Result<Address> {
// Verify signature
let signature = signature.to_k256()?;
verifying_key.verify_prehash(transaction_hash.as_slice(), &signature)?;
// Derive wallet address
let public_key = PublicKey::from(verifying_key).to_encoded_point(false);
let public_key = public_key.as_bytes();
let hash = keccak(&public_key[1..]);

Ok(Address::from_slice(&hash[12..]))
}
12 changes: 6 additions & 6 deletions crates/core/src/stateless/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ where
type Execution: ExecutionStrategy<Driver, Wrapper<Database>>;
type Finalization: FinalizationStrategy<Driver, Database>;

fn data_from_slice(
slice: &[u8],
) -> anyhow::Result<StatelessClientData<Driver::Block, Driver::Header>> {
Ok(pot::from_slice(slice)?)
}

fn data_from_parts(
rkyv_slice: &[u8],
pot_slice: &[u8],
Expand All @@ -50,6 +44,12 @@ where
Ok(StatelessClientData::<Driver::Block, Driver::Header>::from_parts(rkyv_data, chain_data))
}

fn data_from_slice(
slice: &[u8],
) -> anyhow::Result<StatelessClientData<Driver::Block, Driver::Header>> {
Ok(pot::from_slice(slice)?)
}

fn validate(
data: StatelessClientData<Driver::Block, Driver::Header>,
) -> anyhow::Result<StatelessClientEngine<Driver, Database>> {
Expand Down
1 change: 0 additions & 1 deletion crates/preflight-ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ workspace = true
alloy.workspace = true
reth-chainspec.workspace = true
reth-primitives.workspace = true
reth-revm.workspace = true
2 changes: 0 additions & 2 deletions crates/preflight/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ reth-chainspec.workspace = true
reth-revm.workspace = true
reth-primitives.workspace = true
reth-storage-errors.workspace = true

risc0-zkvm.workspace = true
2 changes: 1 addition & 1 deletion crates/preflight/src/provider/rpc_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct RpcProvider<N: Network> {

impl<N: Network> RpcProvider<N> {
pub fn new(rpc_url: String) -> anyhow::Result<Self> {
let retry_layer = RetryBackoffLayer::new(100, 50, 300);
let retry_layer = RetryBackoffLayer::new(10, 100, 330);

let client = RpcClient::builder()
.layer(retry_layer)
Expand Down
3 changes: 1 addition & 2 deletions crates/zeth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ workspace = true

[dependencies.risc0-zkvm]
workspace = true
default-features = true
features = ["std", "unstable"]
features = ["std", "client", "unstable"]

[dependencies.zeth-guests]
workspace = true
Expand Down
16 changes: 11 additions & 5 deletions guests/reth-ethereum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ fn main() {
let stateless_client_data_rkyv = env::read_frame();
let stateless_client_data_pot = env::read_frame();
env::log("Deserializing input data");
let stateless_client_data = RethStatelessClient::data_from_parts(&stateless_client_data_rkyv, &stateless_client_data_pot)
.expect("Failed to load client data from stdin");
let stateless_client_data = RethStatelessClient::data_from_parts(
&stateless_client_data_rkyv,
&stateless_client_data_pot,
)
.expect("Failed to load client data from stdin");
let validation_depth = stateless_client_data.blocks.len() as u64;
assert!(stateless_client_data.chain.is_ethereum(), "This program only supports Ethereum chains");
assert!(
stateless_client_data.chain.is_ethereum(),
"This program only supports Ethereum chains"
);
let chain_id = stateless_client_data.chain as u64;
// Build the block
env::log("Validating blocks");
let engine = RethStatelessClient::validate(stateless_client_data)
.expect("block validation failed");
let engine =
RethStatelessClient::validate(stateless_client_data).expect("block validation failed");
// Build the journal (todo: make this a strategy)
let block_hash = engine.data.parent_header.hash_slow();
let total_difficulty = engine.data.total_difficulty;
Expand Down
1 change: 1 addition & 0 deletions guests/reth-optimism/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions guests/reth-optimism/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ fn main() {
let stateless_client_data_rkyv = env::read_frame();
let stateless_client_data_pot = env::read_frame();
env::log("Deserializing input data");
let stateless_client_data = OpRethStatelessClient::data_from_parts(&stateless_client_data_rkyv, &stateless_client_data_pot)
.expect("Failed to load client data from stdin");
let stateless_client_data = OpRethStatelessClient::data_from_parts(
&stateless_client_data_rkyv,
&stateless_client_data_pot,
)
.expect("Failed to load client data from stdin");
let validation_depth = stateless_client_data.blocks.len() as u64;
assert!(stateless_client_data.chain.is_optimism(), "This program only supports Optimism chains");
assert!(
stateless_client_data.chain.is_optimism(),
"This program only supports Optimism chains"
);
let chain_id = stateless_client_data.chain as u64;
// Build the block
env::log("Validating blocks");
let engine = OpRethStatelessClient::validate(stateless_client_data)
.expect("block validation failed");
let engine =
OpRethStatelessClient::validate(stateless_client_data).expect("block validation failed");
// Build the journal (todo: make this a strategy)
let block_hash = engine.data.parent_header.hash_slow();
let total_difficulty = engine.data.total_difficulty;
Expand Down
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ test:
cargo test --all-targets -p zeth-testeth -F ef-tests

just test-cache-eth
just test-cache-op

test-cache-eth +ARGS="": (build ARGS)
RUST_LOG=info ./target/debug/zeth-ethereum build --cache=bin/ethereum/data -b=1
Expand Down
Loading

0 comments on commit 37e3985

Please sign in to comment.