From 03d0b50a8db6caffcf2f4d2495880675d4807219 Mon Sep 17 00:00:00 2001 From: smtmfft Date: Fri, 27 Dec 2024 16:28:17 +0800 Subject: [PATCH] update preflight call logic Signed-off-by: smtmfft --- host/src/cache.rs | 1 + host/src/proof.rs | 2 ++ lib/src/builder.rs | 20 +++++++++++--------- lib/src/input.rs | 40 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/host/src/cache.rs b/host/src/cache.rs index 606a7f4a..93b18b54 100644 --- a/host/src/cache.rs +++ b/host/src/cache.rs @@ -113,6 +113,7 @@ mod test { l1_chain_spec.clone(), taiko_chain_spec.clone(), proof_request.clone(), + false, ); let provider = RpcBlockDataProvider::new( &taiko_chain_spec.rpc.clone(), diff --git a/host/src/proof.rs b/host/src/proof.rs index 1223af0c..fc5e2c12 100644 --- a/host/src/proof.rs +++ b/host/src/proof.rs @@ -415,10 +415,12 @@ pub async fn handle_proof( // Execute the proof generation. let total_time = Measurement::start("", false); + let v2_preflight = std::env::var("V2_PREFLIGHT").is_ok(); let raiko = Raiko::new( l1_chain_spec.clone(), taiko_chain_spec.clone(), proof_request.clone(), + v2_preflight, ); let provider = RpcBlockDataProvider::new( &taiko_chain_spec.rpc.clone(), diff --git a/lib/src/builder.rs b/lib/src/builder.rs index e1dae454..f77fd56b 100644 --- a/lib/src/builder.rs +++ b/lib/src/builder.rs @@ -3,7 +3,6 @@ use std::sync::Arc; use crate::primitives::keccak::keccak; use crate::primitives::mpt::StateAccount; -use crate::utils::generate_transactions; use crate::{ consts::{ChainSpec, MAX_BLOCK_HASH_AGE}, guest_mem_forget, @@ -24,7 +23,9 @@ use reth_primitives::revm_primitives::db::{Database, DatabaseCommit}; use reth_primitives::revm_primitives::{ Account, AccountInfo, AccountStatus, Bytecode, Bytes, HashMap, SpecId, }; -use reth_primitives::{Address, BlockWithSenders, Header, B256, KECCAK_EMPTY, U256}; +use reth_primitives::{ + Address, BlockWithSenders, Header, TransactionSigned, B256, KECCAK_EMPTY, U256, +}; use tracing::{debug, error}; pub fn calculate_block_header(input: &GuestInput) -> Header { @@ -60,6 +61,7 @@ pub struct RethBlockBuilder { pub chain_spec: ChainSpec, pub input: GuestInput, pub db: Option, + pub original_txs: Vec, } impl + DatabaseCommit + OptimisticDatabase> @@ -67,10 +69,15 @@ impl + DatabaseCommit + OptimisticDatabase> { /// Creates a new block builder. pub fn new(input: &GuestInput, db: DB) -> RethBlockBuilder { + // original tx comes from guest input L1 event, which is a super set of input.block.body + let original_txs = input + .get_original_txs() + .expect("get_original_txs should success"); RethBlockBuilder { chain_spec: input.chain_spec.clone(), - db: Some(db), input: input.clone(), + db: Some(db), + original_txs, } } @@ -129,12 +136,7 @@ impl + DatabaseCommit + OptimisticDatabase> // Generate the transactions from the tx list let mut block = self.input.block.clone(); - block.body = generate_transactions( - &self.input.chain_spec, - &self.input.taiko.block_proposed, - &self.input.taiko.tx_data, - &self.input.taiko.anchor_tx, - ); + block.body = self.original_txs.clone(); // Recover senders let mut block = block .with_recovered_senders() diff --git a/lib/src/input.rs b/lib/src/input.rs index 67bc5b11..5752a577 100644 --- a/lib/src/input.rs +++ b/lib/src/input.rs @@ -13,7 +13,8 @@ use serde_with::serde_as; #[cfg(not(feature = "std"))] use crate::no_std::*; use crate::{ - consts::ChainSpec, primitives::mpt::MptNode, prover::Proof, utils::zlib_compress_data, + consts::ChainSpec, primitives::mpt::MptNode, prover::Proof, utils::generate_transactions, + utils::zlib_compress_data, }; /// Represents the state of an account's storage. @@ -43,6 +44,43 @@ pub struct GuestInput { pub taiko: TaikoGuestInput, } +impl GuestInput { + pub fn get_original_txs(&self) -> Result> { + // Generate the transactions from the tx list + let block_body = generate_transactions( + &self.chain_spec, + &self.taiko.block_proposed, + &self.taiko.tx_data, + &self.taiko.anchor_tx, + ); + + // txs from the block should be a valid subset of txs from L1 + if Self::is_ordered_subset(&self.block.body, &block_body) { + Ok(block_body) + } else { + Err(anyhow!("mismatch txs between L1 & L2 block")) + } + } + + fn is_ordered_subset(a: &[T], b: &[T]) -> bool { + if a.len() > b.len() { + return false; + } + + let mut b_idx = 0; + for a_item in a { + while b_idx < b.len() && &b[b_idx] != a_item { + b_idx += 1; + } + if b_idx >= b.len() { + return false; + } + b_idx += 1; + } + true + } +} + /// External aggregation input. #[derive(Debug, Clone, Default, Deserialize, Serialize)] pub struct AggregationGuestInput {