Skip to content

Commit

Permalink
fix op head
Browse files Browse the repository at this point in the history
  • Loading branch information
hashcashier committed Jan 25, 2024
1 parent bc125d3 commit 1665e74
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
24 changes: 18 additions & 6 deletions host/src/operations/rollups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub async fn derive_rollup_blocks(cli: Cli, file_reference: &String) -> anyhow::
op_head_block_no: core_args.block_number,
op_derive_block_count: core_args.block_count,
op_block_outputs: vec![],
builder_image_id: OP_BLOCK_ID,
block_image_id: OP_BLOCK_ID,
};
let mut derive_machine = DeriveMachine::new(
&OPTIMISM_CHAIN_SPEC,
Expand All @@ -84,7 +84,7 @@ pub async fn derive_rollup_blocks(cli: Cli, file_reference: &String) -> anyhow::
op_head_block_no: core_args.block_number,
op_derive_block_count: core_args.block_count,
op_block_outputs,
builder_image_id: OP_BLOCK_ID,
block_image_id: OP_BLOCK_ID,
};

info!("Running from memory ...");
Expand Down Expand Up @@ -254,7 +254,7 @@ pub async fn compose_derived_rollup_blocks(
op_head_block_no: core_args.block_number + op_block_index,
op_derive_block_count: composition_size,
op_block_outputs: vec![],
builder_image_id: OP_BLOCK_ID,
block_image_id: OP_BLOCK_ID,
};
let mut derive_machine = DeriveMachine::new(
&OPTIMISM_CHAIN_SPEC,
Expand Down Expand Up @@ -303,7 +303,7 @@ pub async fn compose_derived_rollup_blocks(
op_head_block_no: core_args.block_number + op_block_index,
op_derive_block_count: composition_size,
op_block_outputs,
builder_image_id: OP_BLOCK_ID,
block_image_id: OP_BLOCK_ID,
};

info!("Deriving ...");
Expand Down Expand Up @@ -355,6 +355,7 @@ pub async fn compose_derived_rollup_blocks(
.root(Some(&mut sibling_map))
.expect("No eth blocks loaded!");
let prep_compose_input = ComposeInput {
block_image_id: OP_BLOCK_ID,
derive_image_id: OP_DERIVE_ID,
compose_image_id: OP_COMPOSE_ID,
operation: ComposeInputOperation::PREP {
Expand Down Expand Up @@ -383,7 +384,9 @@ pub async fn compose_derived_rollup_blocks(
let mut join_queue = VecDeque::new();
for (derive_output, derive_receipt) in lift_queue {
let eth_tail_hash = derive_output.eth_tail.1 .0;
info!("Lifting ... {:?}", &derive_output);
let lift_compose_input = ComposeInput {
block_image_id: OP_BLOCK_ID,
derive_image_id: OP_DERIVE_ID,
compose_image_id: OP_COMPOSE_ID,
operation: ComposeInputOperation::LIFT {
Expand All @@ -392,11 +395,11 @@ pub async fn compose_derived_rollup_blocks(
},
eth_chain_merkle_root: eth_chain_root,
};
info!("Lifting ...");
let lift_compose_output = lift_compose_input
.clone()
.process()
.expect("Lift composition failed.");
info!("Lifted ... {:?}", &lift_compose_output);

let lift_compose_receipt = if let Some(receipt) = derive_receipt {
maybe_prove(
Expand All @@ -417,8 +420,11 @@ pub async fn compose_derived_rollup_blocks(

// Join
while join_queue.len() > 1 {
// Pop left output
let (left, left_receipt) = join_queue.pop_front().unwrap();
// Only peek at right output
let (right, _right_receipt) = join_queue.front().unwrap();
info!("Joining");
let ComposeOutputOperation::AGGREGATE {
op_tail: left_op_tail,
..
Expand All @@ -435,12 +441,17 @@ pub async fn compose_derived_rollup_blocks(
};
// Push dangling workloads (odd block count) to next round
if left_op_tail != right_op_head {
info!(
"Skipping dangling workload: {} - {}",
left_op_tail.0, right_op_head.0
);
join_queue.push_back((left, left_receipt));
continue;
}
// Pair up join
// Actually pop right output for pairing
let (right, right_receipt) = join_queue.pop_front().unwrap();
let join_compose_input = ComposeInput {
block_image_id: OP_BLOCK_ID,
derive_image_id: OP_DERIVE_ID,
compose_image_id: OP_COMPOSE_ID,
operation: ComposeInputOperation::JOIN { left, right },
Expand Down Expand Up @@ -474,6 +485,7 @@ pub async fn compose_derived_rollup_blocks(
// Finish
let (aggregate_output, aggregate_receipt) = join_queue.pop_front().unwrap();
let finish_compose_input = ComposeInput {
block_image_id: OP_BLOCK_ID,
derive_image_id: OP_DERIVE_ID,
compose_image_id: OP_COMPOSE_ID,
operation: ComposeInputOperation::FINISH {
Expand Down
15 changes: 15 additions & 0 deletions lib/src/optimism/composition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub type ImageId = [u32; 8];

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ComposeInput {
pub block_image_id: ImageId,
pub derive_image_id: ImageId,
pub compose_image_id: ImageId,
pub operation: ComposeInputOperation,
Expand Down Expand Up @@ -57,6 +58,7 @@ pub enum ComposeInputOperation {

#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq)]
pub struct ComposeOutput {
pub block_image_id: ImageId,
pub derive_image_id: ImageId,
pub compose_image_id: ImageId,
pub operation: ComposeOutputOperation,
Expand Down Expand Up @@ -93,6 +95,7 @@ impl ComposeInput {
.expect("Failed to validate prior aggregation");
}
// Validate context
assert_eq!(self.block_image_id, prior_output.block_image_id);
assert_eq!(self.derive_image_id, prior_output.derive_image_id);
assert_eq!(self.compose_image_id, prior_output.compose_image_id);
assert_eq!(
Expand Down Expand Up @@ -131,6 +134,7 @@ impl ComposeInput {
}

ComposeOutput {
block_image_id: self.block_image_id,
derive_image_id: self.derive_image_id,
compose_image_id: self.compose_image_id,
operation: ComposeOutputOperation::PREP,
Expand All @@ -150,6 +154,8 @@ impl ComposeInput {
env::verify(Digest::from(self.derive_image_id), &derive_journal)
.expect("Failed to lift derivation receipt");
}
// Verify usage of same block builder image id
assert_eq!(self.block_image_id, derive_output.block_image_id);
// Verify inclusion of ethereum tail in Merkle root
assert!(
eth_tail_proof
Expand All @@ -158,6 +164,7 @@ impl ComposeInput {
);
// Create output
ComposeOutput {
block_image_id: self.block_image_id,
derive_image_id: self.derive_image_id,
compose_image_id: self.compose_image_id,
operation: ComposeOutputOperation::AGGREGATE {
Expand Down Expand Up @@ -189,6 +196,9 @@ impl ComposeInput {
.expect("Failed to verify right composition receipt");
}
// Validate context
// block_image_id equality
assert_eq!(self.block_image_id, left_compose_output.block_image_id);
assert_eq!(self.block_image_id, right_compose_output.block_image_id);
// derive_image_id equality
assert_eq!(self.derive_image_id, left_compose_output.derive_image_id);
assert_eq!(self.derive_image_id, right_compose_output.derive_image_id);
Expand Down Expand Up @@ -225,6 +235,7 @@ impl ComposeInput {
assert_eq!(&left_op_tail, &right_op_head);

ComposeOutput {
block_image_id: self.block_image_id,
derive_image_id: self.derive_image_id,
compose_image_id: self.compose_image_id,
operation: ComposeOutputOperation::AGGREGATE {
Expand Down Expand Up @@ -258,6 +269,9 @@ impl ComposeInput {
.expect("Failed to validate aggregate receipt");
}
// Validate context
// block_image_id equality
assert_eq!(self.block_image_id, prep.block_image_id);
assert_eq!(self.block_image_id, aggregate.block_image_id);
// derive_image_id equality
assert_eq!(self.derive_image_id, prep.derive_image_id);
assert_eq!(self.derive_image_id, aggregate.derive_image_id);
Expand All @@ -279,6 +293,7 @@ impl ComposeInput {
};
// Output new aggregate with validated chain root
ComposeOutput {
block_image_id: self.block_image_id,
derive_image_id: self.derive_image_id,
compose_image_id: self.compose_image_id,
operation: ComposeOutputOperation::AGGREGATE {
Expand Down
19 changes: 11 additions & 8 deletions lib/src/optimism/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub struct DeriveInput<D> {
/// Block building data for execution
pub op_block_outputs: Vec<BlockBuildOutput>,
/// Image id of block builder guest
pub builder_image_id: ImageId,
pub block_image_id: ImageId,
}

/// Represents the output of the derivation process.
Expand All @@ -107,7 +107,7 @@ pub struct DeriveOutput {
/// Derived Optimism blocks.
pub derived_op_blocks: Vec<(BlockNumber, BlockHash)>,
/// Image id of block builder guest
pub builder_image_id: ImageId,
pub block_image_id: ImageId,
}

#[cfg(target_os = "zkvm")]
Expand Down Expand Up @@ -227,6 +227,12 @@ impl<D: BatcherDb> DeriveMachine<D> {
let target_block_no =
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 mut derived_op_blocks = Vec::new();
let mut process_next_eth_block = false;

Expand Down Expand Up @@ -407,7 +413,7 @@ impl<D: BatcherDb> DeriveMachine<D> {
let builder_journal =
to_vec(&output).expect("Failed to encode builder journal");
env::verify(
Digest::from(self.derive_input.builder_image_id),
Digest::from(self.derive_input.block_image_id),
&builder_journal,
)
.expect("Failed to validate block build output");
Expand Down Expand Up @@ -495,12 +501,9 @@ impl<D: BatcherDb> DeriveMachine<D> {
self.op_batcher.state.current_l1_block_number,
self.op_batcher.state.current_l1_block_hash,
),
op_head: (
self.op_head_block_header.number,
self.op_head_block_header.hash(),
),
op_head,
derived_op_blocks,
builder_image_id: self.derive_input.builder_image_id,
block_image_id: self.derive_input.block_image_id,
})
}

Expand Down

0 comments on commit 1665e74

Please sign in to comment.