Skip to content

Commit

Permalink
Merge branch 'rkhalil/tidy-host' of github.com:risc0/zeth into rkhali…
Browse files Browse the repository at this point in the history
…l/derive-tx-exec
  • Loading branch information
hashcashier committed Feb 2, 2024
2 parents 148a089 + 07dc903 commit 1bb1942
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 76 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.

14 changes: 7 additions & 7 deletions host/src/operations/rollups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use zeth_lib::{
};
use zeth_primitives::{
block::Header,
mmr::{MerkleMountainRange, MerkleProof},
transactions::optimism::OptimismTxEssence,
tree::{MerkleMountainRange, MerkleProof},
};

use crate::{cli::Cli, operations::maybe_prove};
Expand Down Expand Up @@ -110,14 +110,14 @@ pub async fn derive_rollup_blocks(cli: Cli, file_reference: &String) -> anyhow::
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);
}

maybe_prove(
Expand Down Expand Up @@ -290,7 +290,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 @@ -400,7 +400,7 @@ pub async fn compose_derived_rollup_blocks(
// Lift
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;
info!("Lifting ... {:?}", &derive_output);
let lift_compose_input = ComposeInput {
block_image_id: OP_BLOCK_ID,
Expand Down Expand Up @@ -460,7 +460,7 @@ pub async fn compose_derived_rollup_blocks(
if left_op_tail != right_op_head {
info!(
"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
2 changes: 1 addition & 1 deletion lib/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
use zeth_primitives::{
block::Header,
mmr::Hash,
transactions::{Transaction, TxEssence},
tree::Hash,
trie::MptNode,
withdrawal::Withdrawal,
Address, Bytes, RlpBytes, B256, U256,
Expand Down
11 changes: 6 additions & 5 deletions lib/src/optimism/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,38 @@ use core::cmp::Ordering;
use std::collections::{BTreeMap, VecDeque};

use anyhow::{bail, ensure, Context, Result};
use serde::{Deserialize, Serialize};
use zeth_primitives::{
batch::{Batch, BatchEssence},
transactions::{
ethereum::EthereumTxEssence,
optimism::{OptimismTxEssence, OPTIMISM_DEPOSITED_TX_TYPE},
Transaction,
},
BlockHash, BlockNumber, B256, U256,
BlockHash, BlockNumber, U256,
};

use super::{
batcher_channel::BatcherChannels, batcher_db::BlockInput, config::ChainConfig, deposits,
};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default, Serialize, Deserialize, Ord, PartialOrd)]
pub struct BlockId {
pub hash: B256,
pub hash: BlockHash,
pub number: BlockNumber,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub struct L2BlockInfo {
pub hash: B256,
pub hash: BlockHash,
pub timestamp: u64,
pub l1_origin: BlockId,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Epoch {
pub number: BlockNumber,
pub hash: B256,
pub hash: BlockHash,
pub timestamp: u64,
pub base_fee_per_gas: U256,
pub deposits: Vec<Transaction<OptimismTxEssence>>,
Expand Down
32 changes: 20 additions & 12 deletions lib/src/optimism/composition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use risc0_zkvm::{guest::env, serde::to_vec, sha::Digest};
use serde::{Deserialize, Serialize};
use zeth_primitives::{
block::Header,
tree::{MerkleMountainRange, MerkleProof},
BlockHash, BlockNumber,
mmr,
mmr::{MerkleMountainRange, MerkleProof},
};

use crate::optimism::DeriveOutput;
use crate::optimism::{batcher::BlockId, DeriveOutput};

/// Denotes a zkVM Image ID.
pub type ImageId = [u32; 8];
Expand All @@ -33,7 +33,7 @@ pub struct ComposeInput {
pub derive_image_id: ImageId,
pub compose_image_id: ImageId,
pub operation: ComposeInputOperation,
pub eth_chain_merkle_root: [u8; 32],
pub eth_chain_merkle_root: mmr::Hash,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down Expand Up @@ -62,16 +62,18 @@ pub struct ComposeOutput {
pub derive_image_id: ImageId,
pub compose_image_id: ImageId,
pub operation: ComposeOutputOperation,
pub eth_chain_tail_block: (BlockNumber, BlockHash),
pub eth_chain_merkle_root: [u8; 32],
pub eth_chain_tail_block: BlockId,
pub eth_chain_merkle_root: mmr::Hash,
}

#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq)]
pub enum ComposeOutputOperation {
PREP,
AGGREGATE {
op_head: (BlockNumber, BlockHash),
op_tail: (BlockNumber, BlockHash),
op_head: BlockId,
op_tail: BlockId,
/// Whether the L1 block range has been validate as a correct Merkle commitment to
/// a continuous chain of L1 blocks.
eth_chain_continuity_validated: bool,
},
}
Expand Down Expand Up @@ -122,15 +124,21 @@ impl ComposeInput {
// Insert chain of blocks into mountain range
for block in eth_blocks {
// Validate parent relationship
if let Some((_, parent_hash)) = eth_tail {
if let Some(BlockId {
hash: parent_hash, ..
}) = eth_tail
{
assert_eq!(block.parent_hash, parent_hash);
}
// Derive block's keccak hash
let block_hash = block.hash();
// Insert hash into mountain range
mountain_range.append_leaf(block_hash.0, None);
// Mark block as new tail
eth_tail.replace((block.number, block_hash));
eth_tail.replace(BlockId {
number: block.number,
hash: block_hash,
});
}

ComposeOutput {
Expand Down Expand Up @@ -159,7 +167,7 @@ impl ComposeInput {
// Verify inclusion of ethereum tail in Merkle root
assert!(
eth_tail_proof
.verify(&self.eth_chain_merkle_root, &derive_output.eth_tail.1 .0),
.verify(&self.eth_chain_merkle_root, &derive_output.eth_tail.hash.0),
"Invalid ethereum tail inclusion proof!"
);
// Create output
Expand All @@ -172,7 +180,7 @@ impl ComposeInput {
op_tail: *derive_output
.derived_op_blocks
.last()
.unwrap_or(&derive_output.op_head),
.expect("Expected at least one derived block to compose."),
eth_chain_continuity_validated: false,
},
eth_chain_tail_block: derive_output.eth_tail,
Expand Down
29 changes: 16 additions & 13 deletions lib/src/optimism/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use zeth_primitives::{
Transaction, TxEssence,
},
trie::MptNode,
uint, Address, BlockHash, BlockNumber, FixedBytes, RlpBytes, B256, U256,
uint, Address, FixedBytes, RlpBytes, B256, U256,
};

#[cfg(not(target_os = "zkvm"))]
Expand Down Expand Up @@ -101,11 +101,11 @@ pub struct DeriveInput<D> {
#[derive(Debug, Clone, Deserialize, Eq, PartialEq, Serialize)]
pub struct DeriveOutput {
/// Ethereum tail block.
pub eth_tail: (BlockNumber, BlockHash),
pub eth_tail: BlockId,
/// Optimism head block.
pub op_head: (BlockNumber, BlockHash),
pub op_head: BlockId,
/// Derived Optimism blocks.
pub derived_op_blocks: Vec<(BlockNumber, BlockHash)>,
pub derived_op_blocks: Vec<BlockId>,
/// Image id of block builder guest
pub block_image_id: ImageId,
}
Expand Down Expand Up @@ -228,10 +228,10 @@ impl<D: BatcherDb> DeriveMachine<D> {
self.derive_input.op_head_block_no + self.derive_input.op_derive_block_count;

// Save starting op_head
let op_head = (
self.op_head_block_header.number,
self.op_head_block_header.hash(),
);
let op_head = BlockId {
number: self.op_head_block_header.number,
hash: self.op_head_block_header.hash(),
};

let mut derived_op_blocks = Vec::new();
let mut process_next_eth_block = false;
Expand Down Expand Up @@ -479,7 +479,10 @@ impl<D: BatcherDb> DeriveMachine<D> {
},
};

derived_op_blocks.push((new_block_head.number, new_block_hash));
derived_op_blocks.push(BlockId {
number: new_block_head.number,
hash: new_block_hash,
});
self.op_head_block_header = new_block_head;

if self.op_head_block_header.number == target_block_no {
Expand All @@ -497,10 +500,10 @@ impl<D: BatcherDb> DeriveMachine<D> {
}

Ok(DeriveOutput {
eth_tail: (
self.op_batcher.state.current_l1_block_number,
self.op_batcher.state.current_l1_block_hash,
),
eth_tail: BlockId {
number: self.op_batcher.state.current_l1_block_number,
hash: self.op_batcher.state.current_l1_block_hash,
},
op_head,
derived_op_blocks,
block_image_id: self.derive_input.block_image_id,
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub mod withdrawal;
pub mod ethers;

pub mod batch;
pub mod mmr;
#[cfg(feature = "revm")]
pub mod revm;
pub mod tree;

pub use alloy_primitives::*;
pub use alloy_rlp as rlp;
Expand Down
Loading

0 comments on commit 1bb1942

Please sign in to comment.