Skip to content

Commit

Permalink
update preflight call logic
Browse files Browse the repository at this point in the history
Signed-off-by: smtmfft <[email protected]>
  • Loading branch information
smtmfft committed Dec 27, 2024
1 parent 4a9b703 commit 03d0b50
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
1 change: 1 addition & 0 deletions host/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions host/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
20 changes: 11 additions & 9 deletions lib/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -60,17 +61,23 @@ pub struct RethBlockBuilder<DB> {
pub chain_spec: ChainSpec,
pub input: GuestInput,
pub db: Option<DB>,
pub original_txs: Vec<TransactionSigned>,
}

impl<DB: Database<Error = ProviderError> + DatabaseCommit + OptimisticDatabase>
RethBlockBuilder<DB>
{
/// Creates a new block builder.
pub fn new(input: &GuestInput, db: DB) -> RethBlockBuilder<DB> {
// 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,
}
}

Expand Down Expand Up @@ -129,12 +136,7 @@ impl<DB: Database<Error = ProviderError> + 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()
Expand Down
40 changes: 39 additions & 1 deletion lib/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -43,6 +44,43 @@ pub struct GuestInput {
pub taiko: TaikoGuestInput,
}

impl GuestInput {
pub fn get_original_txs(&self) -> Result<Vec<TransactionSigned>> {
// 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<T: PartialEq>(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 {
Expand Down

0 comments on commit 03d0b50

Please sign in to comment.