Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Wollac committed Dec 12, 2024
1 parent 6c14999 commit c406fd7
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 51 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

28 changes: 18 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,29 @@ 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
45 changes: 27 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,36 @@ 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..]))
}
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.

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: (build "")
RUST_LOG=info RISC0_DEV_MODE=1 ./target/debug/zeth-ethereum build --cache=bin/ethereum/data -b=1
Expand Down

0 comments on commit c406fd7

Please sign in to comment.