Skip to content

Commit

Permalink
Merge branch 'rkhalil/polish' of github.com:risc0/zeth into rkhalil/s…
Browse files Browse the repository at this point in the history
…tark2snark
  • Loading branch information
hashcashier committed Feb 2, 2024
2 parents 01bce1d + 699116f commit ece1be6
Show file tree
Hide file tree
Showing 29 changed files with 501 additions and 388 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
env:
CARGO_TERM_COLOR: always
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RISC0_VERSION: 0.19.1
RISC0_VERSION: 0.20.0
RISC0_TOOLCHAIN_VERSION: test-release-2

concurrency:
Expand Down
28 changes: 14 additions & 14 deletions guests/eth-block/Cargo.lock

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

4 changes: 2 additions & 2 deletions guests/eth-block/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pub fn main() {
let mut output = EthereumStrategy::build_from(&ETH_MAINNET_CHAIN_SPEC, input)
.expect("Failed to build the resulting block");
// Abridge successful construction results
if let Some(trie_root) = output.compress_state() {
if let Some(replaced_state) = output.replace_state_with_hash() {
// Leak memory, save cycles
core::mem::forget(trie_root);
core::mem::forget(replaced_state);
}
// Output the construction result
env::commit(&output);
Expand Down
4 changes: 2 additions & 2 deletions guests/op-block/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pub fn main() {
let mut output = OptimismStrategy::build_from(&OP_MAINNET_CHAIN_SPEC, input)
.expect("Failed to build the resulting block");
// Abridge successful construction results
if let Some(trie_root) = output.compress_state() {
if let Some(replaced_state) = output.replace_state_with_hash() {
// Leak memory, save cycles
core::mem::forget(trie_root);
core::mem::forget(replaced_state);
}
// Output the construction result
env::commit(&output);
Expand Down
10 changes: 5 additions & 5 deletions host/src/operations/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ where

let init_spec = chain_spec.clone();
let preflight_result = tokio::task::spawn_blocking(move || {
N::run_preflight(init_spec, rpc_cache, rpc_url, core_args.block_number)
N::preflight_with_external_data(init_spec, rpc_cache, rpc_url, core_args.block_number)
})
.await?;
let preflight_data = preflight_result.context("preflight failed")?;
Expand All @@ -72,9 +72,9 @@ where

match &output {
BlockBuildOutput::SUCCESS {
new_block_hash,
new_block_head,
new_block_state,
hash: new_block_hash,
head: new_block_head,
state: new_block_state,
} => {
info!("Verifying final state using provider data ...");
preflight_data.verify_block(new_block_head, new_block_state)?;
Expand All @@ -86,7 +86,7 @@ where
}
}

let compressed_output = output.with_state_compressed();
let compressed_output = output.with_state_hashed();
let result = match &cli {
Cli::Build(..) => None,
Cli::Run(run_args) => {
Expand Down
18 changes: 9 additions & 9 deletions host/src/operations/rollups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use zeth_lib::{
};
use zeth_primitives::{
block::Header,
mmr::{MerkleMountainRange, MerkleProof},
transactions::optimism::OptimismTxEssence,
tree::{MerkleMountainRange, MerkleProof},
};

use crate::{
Expand Down Expand Up @@ -107,14 +107,14 @@ pub async fn derive_rollup_blocks(cli: Cli) -> anyhow::Result<Option<(String, Re
info!("In-memory test complete");
println!(
"Eth tail: {} {}",
derive_output.eth_tail.0, derive_output.eth_tail.1
derive_output.eth_tail.number, derive_output.eth_tail.hash
);
println!(
"Op Head: {} {}",
derive_output.op_head.0, derive_output.op_head.1
derive_output.op_head.number, derive_output.op_head.hash
);
for derived_block in &derive_output.derived_op_blocks {
println!("Derived: {} {}", derived_block.0, derived_block.1);
println!("Derived: {} {}", derived_block.number, derived_block.hash);
}

let final_result = match &cli {
Expand Down Expand Up @@ -201,7 +201,7 @@ pub async fn compose_derived_rollup_blocks(
let eth_tail = derive_machine
.derive_input
.db
.get_full_eth_block(derive_output.eth_tail.0)
.get_full_eth_block(derive_output.eth_tail.number)
.context("could not fetch eth tail")?
.block_header
.clone();
Expand Down Expand Up @@ -310,7 +310,7 @@ pub async fn compose_derived_rollup_blocks(
info!("Lifting {} proofs...", lift_queue.len());
let mut join_queue = VecDeque::new();
for (derive_output, derive_receipt) in lift_queue {
let eth_tail_hash = derive_output.eth_tail.1 .0;
let eth_tail_hash = derive_output.eth_tail.hash.0;
trace!("Lifting ... {:?}", &derive_output);
let lift_compose_input = ComposeInput {
block_image_id: OP_BLOCK_ID,
Expand Down Expand Up @@ -370,8 +370,8 @@ pub async fn compose_derived_rollup_blocks(
if left_op_tail != right_op_head {
trace!(
"Skipping dangling workload: {} - {}",
left_op_tail.0,
right_op_head.0
left_op_tail.number,
right_op_head.number
);
join_queue.push_back((left, left_receipt));
continue;
Expand Down Expand Up @@ -479,7 +479,7 @@ async fn build_op_blocks(
for input in op_block_inputs {
let output = OptimismStrategy::build_from(&OP_MAINNET_CHAIN_SPEC, input.clone())
.expect("Failed to build op block")
.with_state_compressed();
.with_state_hashed();

if let Some((bonsai_receipt_uuid, receipt)) =
maybe_prove(cli, &input, OP_BLOCK_ELF, &output, Default::default()).await
Expand Down
43 changes: 32 additions & 11 deletions lib/src/builder/execute/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,38 @@ impl TxExecStrategy<EthereumTxEssence> for EthTxExecStrategy {
{
use chrono::{TimeZone, Utc};
let dt = Utc
.timestamp_opt(block_builder.input.timestamp.try_into().unwrap(), 0)
.timestamp_opt(
block_builder
.input
.state_input
.timestamp
.try_into()
.unwrap(),
0,
)
.unwrap();

trace!("Block no. {}", header.number);
trace!(" EVM spec ID: {:?}", spec_id);
trace!(" Timestamp: {}", dt);
trace!(" Transactions: {}", block_builder.input.transactions.len());
trace!(" Withdrawals: {}", block_builder.input.withdrawals.len());
trace!(" Fee Recipient: {:?}", block_builder.input.beneficiary);
trace!(" Gas limit: {}", block_builder.input.gas_limit);
trace!(
" Transactions: {}",
block_builder.input.state_input.transactions.len()
);
trace!(
" Withdrawals: {}",
block_builder.input.state_input.withdrawals.len()
);
trace!(
" Fee Recipient: {:?}",
block_builder.input.state_input.beneficiary
);
trace!(" Gas limit: {}", block_builder.input.state_input.gas_limit);
trace!(" Base fee per gas: {}", header.base_fee_per_gas);
trace!(" Extra data: {:?}", block_builder.input.extra_data);
trace!(
" Extra data: {:?}",
block_builder.input.state_input.extra_data
);
}

// initialize the Evm
Expand All @@ -92,12 +112,12 @@ impl TxExecStrategy<EthereumTxEssence> for EthTxExecStrategy {
.modify_block_env(|blk_env| {
// set the EVM block environment
blk_env.number = header.number.try_into().unwrap();
blk_env.coinbase = block_builder.input.beneficiary;
blk_env.coinbase = block_builder.input.state_input.beneficiary;
blk_env.timestamp = header.timestamp;
blk_env.difficulty = U256::ZERO;
blk_env.prevrandao = Some(header.mix_hash);
blk_env.basefee = header.base_fee_per_gas;
blk_env.gas_limit = block_builder.input.gas_limit;
blk_env.gas_limit = block_builder.input.state_input.gas_limit;
})
.with_db(block_builder.db.take().unwrap())
.build();
Expand All @@ -110,7 +130,7 @@ impl TxExecStrategy<EthereumTxEssence> for EthTxExecStrategy {
// process all the transactions
let mut tx_trie = MptNode::default();
let mut receipt_trie = MptNode::default();
for (tx_no, tx) in take(&mut block_builder.input.transactions)
for (tx_no, tx) in take(&mut block_builder.input.state_input.transactions)
.into_iter()
.enumerate()
{
Expand All @@ -129,7 +149,8 @@ impl TxExecStrategy<EthereumTxEssence> for EthTxExecStrategy {
}

// verify transaction gas
let block_available_gas = block_builder.input.gas_limit - cumulative_gas_used;
let block_available_gas =
block_builder.input.state_input.gas_limit - cumulative_gas_used;
if block_available_gas < tx.essence.gas_limit() {
bail!("Error at transaction {}: gas exceeds block limit", tx_no);
}
Expand Down Expand Up @@ -202,7 +223,7 @@ impl TxExecStrategy<EthereumTxEssence> for EthTxExecStrategy {

// process withdrawals unconditionally after any transactions
let mut withdrawals_trie = MptNode::default();
for (i, withdrawal) in take(&mut block_builder.input.withdrawals)
for (i, withdrawal) in take(&mut block_builder.input.state_input.withdrawals)
.into_iter()
.enumerate()
{
Expand Down
47 changes: 33 additions & 14 deletions lib/src/builder/execute/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,34 @@ impl TxExecStrategy<OptimismTxEssence> for OpTxExecStrategy {
{
use chrono::{TimeZone, Utc};
let dt = Utc
.timestamp_opt(block_builder.input.timestamp.try_into().unwrap(), 0)
.timestamp_opt(
block_builder
.input
.state_input
.timestamp
.try_into()
.unwrap(),
0,
)
.unwrap();

trace!("Block no. {}", header.number);
trace!(" EVM spec ID: {:?}", spec_id);
trace!(" Timestamp: {}", dt);
trace!(" Transactions: {}", block_builder.input.transactions.len());
trace!(" Fee Recipient: {:?}", block_builder.input.beneficiary);
trace!(" Gas limit: {}", block_builder.input.gas_limit);
trace!(
" Transactions: {}",
block_builder.input.state_input.transactions.len()
);
trace!(
" Fee Recipient: {:?}",
block_builder.input.state_input.beneficiary
);
trace!(" Gas limit: {}", block_builder.input.state_input.gas_limit);
trace!(" Base fee per gas: {}", header.base_fee_per_gas);
trace!(" Extra data: {:?}", block_builder.input.extra_data);
trace!(
" Extra data: {:?}",
block_builder.input.state_input.extra_data
);
}

let mut evm = Evm::builder()
Expand All @@ -93,12 +110,12 @@ impl TxExecStrategy<OptimismTxEssence> for OpTxExecStrategy {
.modify_block_env(|blk_env| {
// set the EVM block environment
blk_env.number = header.number.try_into().unwrap();
blk_env.coinbase = block_builder.input.beneficiary;
blk_env.coinbase = block_builder.input.state_input.beneficiary;
blk_env.timestamp = header.timestamp;
blk_env.difficulty = U256::ZERO;
blk_env.prevrandao = Some(header.mix_hash);
blk_env.basefee = header.base_fee_per_gas;
blk_env.gas_limit = block_builder.input.gas_limit;
blk_env.gas_limit = block_builder.input.state_input.gas_limit;
})
.with_db(block_builder.db.take().unwrap())
.append_handler_register(optimism::optimism_handle_register)
Expand All @@ -112,25 +129,27 @@ impl TxExecStrategy<OptimismTxEssence> for OpTxExecStrategy {
// process all the transactions
let mut tx_trie = MptNode::default();
let mut receipt_trie = MptNode::default();
for (tx_no, tx) in take(&mut block_builder.input.transactions)
for (tx_no, tx) in take(&mut block_builder.input.state_input.transactions)
.into_iter()
.enumerate()
{
// verify the transaction signature
let tx_from = tx
.recover_from()
.with_context(|| format!("Error recovering address for transaction {}", tx_no))?;

#[cfg(not(target_os = "zkvm"))]
{
let tx_hash = tx.hash();
trace!("Tx no. {} (hash: {})", tx_no, tx_hash);
trace!(" Type: {}", tx.essence.tx_type());
trace!(" Fr: {:?}", tx_from);
trace!(" To: {:?}", tx.essence.to().unwrap_or_default());
}

// verify the transaction signature
let tx_from = tx
.recover_from()
.with_context(|| format!("Error recovering address for transaction {}", tx_no))?;

// verify transaction gas
let block_available_gas = block_builder.input.gas_limit - cumulative_gas_used;
let block_available_gas =
block_builder.input.state_input.gas_limit - cumulative_gas_used;
if block_available_gas < tx.essence.gas_limit() {
bail!("Error at transaction {}: gas exceeds block limit", tx_no);
}
Expand Down
Loading

0 comments on commit ece1be6

Please sign in to comment.