Skip to content

Commit

Permalink
build after derive
Browse files Browse the repository at this point in the history
  • Loading branch information
hashcashier committed Jan 19, 2024
1 parent 0b6a455 commit dc98f83
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
62 changes: 59 additions & 3 deletions host/src/operations/rollups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,73 @@ use anyhow::Context;
use log::info;
use zeth_guests::*;
use zeth_lib::{
host::rpc_db::RpcDb,
builder::OptimismStrategy,
consts::{Network, OP_MAINNET_CHAIN_SPEC},
host::{preflight::Preflight, rpc_db::RpcDb},
input::Input,
optimism::{
batcher_db::BatcherDb,
composition::{ComposeInput, ComposeInputOperation, ComposeOutputOperation},
config::OPTIMISM_CHAIN_SPEC,
DeriveInput, DeriveMachine,
},
};
use zeth_primitives::{block::Header, tree::MerkleMountainRange};
use zeth_primitives::{
block::Header, transactions::optimism::OptimismTxEssence, tree::MerkleMountainRange,
};

use crate::{
cli::Cli,
cache_file_path,
cli::{Cli, CoreArgs},
operations::{execute, maybe_prove},
};

async fn fetch_op_blocks(
core_args: &CoreArgs,
block_number: u64,
block_count: u64,
) -> anyhow::Result<Vec<Input<OptimismTxEssence>>> {
let mut op_blocks = vec![];
for i in 0..block_count {
let block_number = block_number + i;
let rpc_cache = core_args.cache.as_ref().map(|dir| {
cache_file_path(dir, &Network::Optimism.to_string(), block_number, "json.gz")
});
let rpc_url = core_args.op_rpc_url.clone();
// Collect block building data
let preflight_result = tokio::task::spawn_blocking(move || {
OptimismStrategy::run_preflight(
OP_MAINNET_CHAIN_SPEC.clone(),
rpc_cache,
rpc_url,
block_number,
)
})
.await?
.context("preflight failed")?;

// Create the guest input from [Init]
let input = preflight_result
.clone()
.try_into()
.context("invalid preflight data")?;

op_blocks.push(input);
}

Ok(op_blocks)
}

pub async fn derive_rollup_blocks(cli: Cli, file_reference: &String) -> anyhow::Result<()> {
info!("Fetching data ...");
let core_args = cli.core_args().clone();
let op_blocks = fetch_op_blocks(
&core_args,
core_args.block_number + 1,
core_args.block_count,
)
.await?;

let (derive_input, output) = tokio::task::spawn_blocking(move || {
let derive_input = DeriveInput {
db: RpcDb::new(
Expand All @@ -45,6 +94,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_blocks: op_blocks.clone(),
};
let mut derive_machine = DeriveMachine::new(&OPTIMISM_CHAIN_SPEC, derive_input)
.context("Could not create derive machine")?;
Expand All @@ -53,6 +103,7 @@ pub async fn derive_rollup_blocks(cli: Cli, file_reference: &String) -> anyhow::
db: derive_machine.derive_input.db.get_mem_db(),
op_head_block_no: core_args.block_number,
op_derive_block_count: core_args.block_count,
op_blocks,
};
let out: anyhow::Result<_> = Ok((derive_input_mem, derive_output));
out
Expand Down Expand Up @@ -227,11 +278,15 @@ pub async fn compose_derived_rollup_blocks(
core_args.op_rpc_url.clone(),
core_args.cache.clone(),
);
let op_head_block_no = core_args.block_number + op_block_index;
let op_blocks = fetch_op_blocks(&core_args, op_head_block_no + 1, composition_size).await?;

let (input, output, chain) = tokio::task::spawn_blocking(move || {
let derive_input = DeriveInput {
db,
op_head_block_no: core_args.block_number + op_block_index,
op_derive_block_count: composition_size,
op_blocks: op_blocks.clone(),
};
let mut derive_machine = DeriveMachine::new(&OPTIMISM_CHAIN_SPEC, derive_input)
.expect("Could not create derive machine");
Expand Down Expand Up @@ -262,6 +317,7 @@ pub async fn compose_derived_rollup_blocks(
db: derive_machine.derive_input.db.get_mem_db(),
op_head_block_no: core_args.block_number + op_block_index,
op_derive_block_count: composition_size,
op_blocks,
};
let out: anyhow::Result<_> = Ok((derive_input_mem, derive_output, eth_chain));
out
Expand Down
27 changes: 26 additions & 1 deletion lib/src/optimism/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use core::iter::once;
use std::mem;

use alloy_sol_types::{sol, SolInterface};
use anyhow::{bail, ensure, Context, Result};
Expand All @@ -32,7 +33,9 @@ use zeth_primitives::{
};

use crate::{
consts::ONE,
builder::{BlockBuilderStrategy, OptimismStrategy},
consts::{ONE, OP_MAINNET_CHAIN_SPEC},
input::Input,
optimism::{
batcher::{Batcher, BlockInfo},
batcher_db::BatcherDb,
Expand Down Expand Up @@ -79,6 +82,8 @@ pub struct DeriveInput<D> {
pub op_head_block_no: u64,
/// Block count for the operation.
pub op_derive_block_count: u64,
/// Block building data for execution
pub op_blocks: Vec<Input<OptimismTxEssence>>,
}

/// Represents the output of the derivation process.
Expand Down Expand Up @@ -303,6 +308,26 @@ impl<D: BatcherDb> DeriveMachine<D> {
}
}

// Execute transactions to verify valid state transitions
let op_blocks = mem::take(&mut self.derive_input.op_blocks);
if op_blocks.len() != derived_op_blocks.len() {
bail!(
"Mismatch between number of input op blocks {} and derived block count {}",
op_blocks.len(),
derived_op_blocks.len()
);
}
for (i, input) in op_blocks.into_iter().enumerate() {
let (header, _) = OptimismStrategy::build_from(&OP_MAINNET_CHAIN_SPEC, input)?;
if header.hash() != derived_op_blocks[i].1 {
bail!(
"Mismatch between built block {} and derived block {}.",
header.number,
&derived_op_blocks[i].0
)
}
}

Ok(DeriveOutput {
eth_tail: (
self.op_batcher.state.current_l1_block_number,
Expand Down

0 comments on commit dc98f83

Please sign in to comment.