Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split transition information from BatchInfo to BatchTransition #637

Merged
merged 9 commits into from
Feb 6, 2025
Prev Previous commit
Next Next commit
refactor: rename to CheckpointBaseStateCommitment
  • Loading branch information
prajwolrg authored and Prajwol Gyawali committed Feb 6, 2025
commit 90e7fd7df4d46f3afe15d77a87470521525299a4
9 changes: 7 additions & 2 deletions crates/consensus-logic/src/csm/client_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ fn handle_mature_l1_height(
// If l2 blocks is not in db then finalization will happen when
// l2Block is fetched from the network and the corresponding
//checkpoint is already finalized.
let blkid = checkpt.batch_info.l2_blockid;
let blkid = *checkpt.batch_info.final_l2_blockid();

match context.get_l2_block_data(&blkid) {
Ok(_) => {
Expand Down Expand Up @@ -403,7 +403,12 @@ fn find_l1_height_for_l2_blockid(
target_l2_blockid: &L2BlockId,
) -> Option<u64> {
checkpoints
.binary_search_by(|checkpoint| checkpoint.batch_info.l2_blockid.cmp(target_l2_blockid))
.binary_search_by(|checkpoint| {
checkpoint
.batch_info
.final_l2_blockid()
.cmp(target_l2_blockid)
})
.ok()
.map(|index| checkpoints[index].height)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/db/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use strata_primitives::{
l1::payload::{L1Payload, PayloadIntent},
};
use strata_state::batch::{
BatchCheckpoint, BatchInfo, BatchTransition, CheckpointBaseState, CommitmentInfo,
BatchCheckpoint, BatchInfo, BatchTransition, CheckpointBaseStateCommitment, CommitmentInfo,
};
use zkaleido::Proof;

Expand Down Expand Up @@ -222,7 +222,7 @@ impl CheckpointEntry {
pub fn new_pending_proof(
info: BatchInfo,
transition: BatchTransition,
checkpoint_base_state: CheckpointBaseState,
checkpoint_base_state: CheckpointBaseStateCommitment,
) -> Self {
let checkpoint =
BatchCheckpoint::new(info, transition, checkpoint_base_state, Proof::default());
Expand Down
8 changes: 4 additions & 4 deletions crates/sequencer/src/checkpoint/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use strata_consensus_logic::csm::message::ClientUpdateNotif;
use strata_db::{traits::Database, types::CheckpointEntry, DbError};
use strata_primitives::{buf::Buf32, l1::L1BlockCommitment, l2::L2BlockCommitment, params::Params};
use strata_state::{
batch::{BatchInfo, BatchTransition, CheckpointBaseState},
batch::{BatchInfo, BatchTransition, CheckpointBaseStateCommitment},
client_state::ClientState,
};
use strata_storage::NodeStorage;
Expand Down Expand Up @@ -104,7 +104,7 @@ fn get_next_batch(
state: &ClientState,
storage: &NodeStorage,
rollup_params_commitment: Buf32,
) -> Result<(BatchInfo, BatchTransition, CheckpointBaseState), Error> {
) -> Result<(BatchInfo, BatchTransition, CheckpointBaseStateCommitment), Error> {
if !state.is_chain_active() {
debug!("chain not active, no duties created");
return Err(Error::ChainInactive);
Expand Down Expand Up @@ -184,7 +184,7 @@ fn get_next_batch(
// Build the batch transition and batch info.
let new_transition =
BatchTransition::new(l1_transition, l2_transition, rollup_params_commitment);
let new_batch = BatchInfo::new(first_checkpoint_idx, l1_range, l2_range, tip_id);
let new_batch = BatchInfo::new(first_checkpoint_idx, l1_range, l2_range);
let genesis_state = new_transition.get_initial_checkpoint_base_state();

Ok((new_batch, new_transition, genesis_state))
Expand Down Expand Up @@ -216,7 +216,7 @@ fn get_next_batch(
let current_chain_state_root = current_chain_state.compute_state_root();
let l2_transition = (batch_transition.l2_transition.1, current_chain_state_root);

let new_batch_info = BatchInfo::new(batch_info.epoch + 1, l1_range, l2_range, tip_id);
let new_batch_info = BatchInfo::new(batch_info.epoch + 1, l1_range, l2_range);
let new_transition =
BatchTransition::new(l1_transition, l2_transition, rollup_params_commitment);

Expand Down
120 changes: 58 additions & 62 deletions crates/state/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ use strata_crypto::verify_schnorr_sig;
use strata_primitives::{
buf::{Buf32, Buf64},
l1::L1BlockCommitment,
l2::L2BlockCommitment,
l2::{L2BlockCommitment, L2BlockId},
};
use zkaleido::{Proof, ProofReceipt, PublicValues};

use crate::id::L2BlockId;

/// Consolidates all information required to describe and verify a batch checkpoint.
/// This includes metadata about the batch, the state transitions, checkpoint base state,
/// and the proof itself. The proof verifies that the transition in [`BatchTransition`]
Expand All @@ -25,8 +23,9 @@ pub struct BatchCheckpoint {
/// Transition data for L1 and L2 states, which is verified by the proof.
transition: BatchTransition,

/// Reference state against which batch transitions and corresponding proof is verified
checkpoint_base_state: CheckpointBaseState,
/// Reference state commitment against which batch transition and corresponding proof is
/// verified
checkpoint_base_state: CheckpointBaseStateCommitment,

/// Proof for the batch obtained from prover manager
proof: Proof,
Expand All @@ -36,7 +35,7 @@ impl BatchCheckpoint {
pub fn new(
batch_info: BatchInfo,
transition: BatchTransition,
checkpoint_base_state: CheckpointBaseState,
checkpoint_base_state: CheckpointBaseStateCommitment,
proof: Proof,
) -> Self {
Self {
Expand All @@ -55,7 +54,7 @@ impl BatchCheckpoint {
&self.transition
}

pub fn checkpoint_base_state(&self) -> &CheckpointBaseState {
pub fn checkpoint_base_state(&self) -> &CheckpointBaseStateCommitment {
&self.checkpoint_base_state
}

Expand Down Expand Up @@ -137,14 +136,52 @@ pub struct BatchInfo {
/// Checkpoint epoch
pub epoch: u64,

/// L1 height range(inclusive) the checkpoint covers
/// L1 block range(inclusive) the checkpoint covers
pub l1_range: (L1BlockCommitment, L1BlockCommitment),

/// L2 height range(inclusive) the checkpoint covers
/// L2 block range(inclusive) the checkpoint covers
pub l2_range: (L2BlockCommitment, L2BlockCommitment),
}

impl BatchInfo {
pub fn new(
checkpoint_idx: u64,
l1_range: (L1BlockCommitment, L1BlockCommitment),
l2_range: (L2BlockCommitment, L2BlockCommitment),
) -> Self {
Self {
epoch: checkpoint_idx,
l1_range,
l2_range,
}
}

pub fn epoch(&self) -> u64 {
self.epoch
}

/// The last L2 block upto which this checkpoint covers since the previous checkpoint
pub l2_blockid: L2BlockId,
/// Returns the final L2 block commitment in the batch's L2 range.
pub fn final_l2_blockid(&self) -> &L2BlockId {
self.l2_range.1.blkid()
}

/// check for whether the l2 block is covered by the checkpoint
pub fn includes_l2_block(&self, l2_block_height: u64) -> bool {
let (_, last_l2_commitment) = self.l2_range;
if l2_block_height <= last_l2_commitment.slot() {
return true;
}
false
}

/// check for whether the l1 block is covered by the checkpoint
pub fn includes_l1_block(&self, l1_block_height: u64) -> bool {
let (_, last_l1_commitment) = self.l1_range;
if l1_block_height <= last_l1_commitment.height() {
return true;
}
false
}
}

/// Describes state transitions for both L1 and L2, along with a commitment to the
Expand Down Expand Up @@ -186,76 +223,35 @@ impl BatchTransition {
}

/// Creates a [`CheckpointBaseState`] by taking the initial state of the [`BatchTransition`]
pub fn get_initial_checkpoint_base_state(&self) -> CheckpointBaseState {
CheckpointBaseState::new(self.l1_transition.0, self.l2_transition.0)
pub fn get_initial_checkpoint_base_state(&self) -> CheckpointBaseStateCommitment {
CheckpointBaseStateCommitment::new(self.l1_transition.0, self.l2_transition.0)
}

/// Creates a [`CheckpointBaseState`] by taking the final state of the [`BatchTransition`]
pub fn get_final_checkpoint_base_state(&self) -> CheckpointBaseState {
CheckpointBaseState::new(self.l1_transition.1, self.l2_transition.1)
pub fn get_final_checkpoint_base_state(&self) -> CheckpointBaseStateCommitment {
CheckpointBaseStateCommitment::new(self.l1_transition.1, self.l2_transition.1)
}

pub fn rollup_params_commitment(&self) -> Buf32 {
self.rollup_params_commitment
}
}

impl BatchInfo {
pub fn new(
checkpoint_idx: u64,
l1_range: (L1BlockCommitment, L1BlockCommitment),
l2_range: (L2BlockCommitment, L2BlockCommitment),
l2_blockid: L2BlockId,
) -> Self {
Self {
epoch: checkpoint_idx,
l1_range,
l2_range,
l2_blockid,
}
}

pub fn epoch(&self) -> u64 {
self.epoch
}

pub fn l2_blockid(&self) -> &L2BlockId {
&self.l2_blockid
}

/// check for whether the l2 block is covered by the checkpoint
pub fn includes_l2_block(&self, l2_block_height: u64) -> bool {
let (_, last_l2_commitment) = self.l2_range;
if l2_block_height <= last_l2_commitment.slot() {
return true;
}
false
}

/// check for whether the l1 block is covered by the checkpoint
pub fn includes_l1_block(&self, l1_block_height: u64) -> bool {
let (_, last_l1_commitment) = self.l1_range;
if l1_block_height <= last_l1_commitment.height() {
return true;
}
false
}
}

/// Represents the reference state against which batch transitions and proofs are verified.
/// Represents the reference state commitment against which batch transitions and proofs are
/// verified.
///
/// NOTE/TODO: This state serves as the starting point for verifying a checkpoint proof. If we move
/// towards a strict mode where we prove each checkpoint recursively, this should be replaced with
/// `GenesisState`.
#[derive(
Clone, Debug, PartialEq, Eq, Arbitrary, BorshDeserialize, BorshSerialize, Deserialize, Serialize,
)]
pub struct CheckpointBaseState {
pub struct CheckpointBaseStateCommitment {
pub initial_l1_state: Buf32,
pub initial_l2_state: Buf32,
}

impl CheckpointBaseState {
impl CheckpointBaseStateCommitment {
pub fn new(initial_l1_state: Buf32, initial_l2_state: Buf32) -> Self {
Self {
initial_l1_state,
Expand All @@ -267,13 +263,13 @@ impl CheckpointBaseState {
#[derive(Clone, Debug, PartialEq, Eq, BorshDeserialize, BorshSerialize)]
pub struct CheckpointProofOutput {
pub batch_transition: BatchTransition,
pub checkpoint_base_state: CheckpointBaseState,
pub checkpoint_base_state: CheckpointBaseStateCommitment,
}

impl CheckpointProofOutput {
pub fn new(
batch_transition: BatchTransition,
checkpoint_base_state: CheckpointBaseState,
checkpoint_base_state: CheckpointBaseStateCommitment,
) -> CheckpointProofOutput {
Self {
batch_transition,
Expand Down
8 changes: 4 additions & 4 deletions crates/state/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use strata_primitives::buf::Buf32;
use tracing::*;

use crate::{
batch::{BatchInfo, BatchTransition, CheckpointBaseState},
batch::{BatchInfo, BatchTransition, CheckpointBaseStateCommitment},
id::L2BlockId,
l1::{HeaderVerificationState, L1BlockId},
operation::{ClientUpdateOutput, SyncAction},
Expand Down Expand Up @@ -306,8 +306,8 @@ pub struct L1Checkpoint {
/// The inner checkpoint batch transition
pub batch_transition: BatchTransition,

/// Reference state against which batch transitions is verified
pub checkpoint_base_state: CheckpointBaseState,
/// Reference state commitment against which batch transitions is verified
pub checkpoint_base_state: CheckpointBaseStateCommitment,

/// If the checkpoint included proof
pub is_proved: bool,
Expand All @@ -320,7 +320,7 @@ impl L1Checkpoint {
pub fn new(
batch_info: BatchInfo,
batch_transition: BatchTransition,
checkpoint_base_state: CheckpointBaseState,
checkpoint_base_state: CheckpointBaseStateCommitment,
is_proved: bool,
height: u64,
) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/state/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ pub fn apply_writes_to_state(
panic!("operation: mismatched indices of pending checkpoint");
}

let fin_blockid = *checkpt.batch_info.l2_blockid();
let fin_blockid = *checkpt.batch_info.final_l2_blockid();
l1v.last_finalized_checkpoint = Some(checkpt);

// Update finalized blockid in StateSync
Expand Down