Skip to content

Commit

Permalink
provide additional commitment info in getCheckpointInfo call
Browse files Browse the repository at this point in the history
accept optional param in getLatestCheckpointIndex to get latest finalized checkpoint index
  • Loading branch information
sapinb committed Nov 5, 2024
1 parent c5ac4f5 commit 2867c19
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 41 deletions.
2 changes: 2 additions & 0 deletions bin/prover-client/src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ impl StrataProverClientApiServer for ProverClientRpc {
l1_range,
l2_range,
l2_blockid: Default::default(),
l1_blockid: Default::default(),
commitment: None,
};

let task_id = self
Expand Down
34 changes: 22 additions & 12 deletions bin/strata-client/src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use strata_rpc_types::{
};
use strata_rpc_utils::to_jsonrpsee_error;
use strata_state::{
batch::BatchCheckpoint,
block::L2BlockBundle,
bridge_duties::BridgeDuty,
bridge_ops::WithdrawalIntent,
Expand Down Expand Up @@ -586,18 +585,29 @@ impl<D: Database + Send + Sync + 'static> StrataApiServer for StrataRpcImpl<D> {
.get_checkpoint(idx)
.await
.map_err(|e| Error::Other(e.to_string()))?;
let batch_comm: Option<BatchCheckpoint> = entry.map(Into::into);
Ok(batch_comm.map(|bc| bc.batch_info().clone().into()))
}

async fn get_latest_checkpoint_index(&self) -> RpcResult<Option<u64>> {
let idx = self
.checkpoint_handle
.get_last_checkpoint_idx()
.await
.map_err(|e| Error::Other(e.to_string()))?;

return Ok(idx);
Ok(entry.map(Into::into))
}

Check warning on line 590 in bin/strata-client/src/rpc_server.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-client/src/rpc_server.rs#L589-L590

Added lines #L589 - L590 were not covered by tests

async fn get_latest_checkpoint_index(&self, finalized: Option<bool>) -> RpcResult<Option<u64>> {
let finalized = finalized.unwrap_or(false);
if finalized {

Check warning on line 594 in bin/strata-client/src/rpc_server.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-client/src/rpc_server.rs#L592-L594

Added lines #L592 - L594 were not covered by tests
// get last finalized checkpoint index from state
let (client_state, _) = self.get_cur_states().await?;
Ok(client_state
.l1_view()
.last_finalized_checkpoint()
.map(|checkpoint| checkpoint.batch_info.idx()))

Check warning on line 600 in bin/strata-client/src/rpc_server.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-client/src/rpc_server.rs#L596-L600

Added lines #L596 - L600 were not covered by tests
} else {
// get latest checkpoint index from db
let idx = self
.checkpoint_handle
.get_last_checkpoint_idx()
.await
.map_err(|e| Error::Other(e.to_string()))?;

Check warning on line 607 in bin/strata-client/src/rpc_server.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-client/src/rpc_server.rs#L603-L607

Added lines #L603 - L607 were not covered by tests

Ok(idx)

Check warning on line 609 in bin/strata-client/src/rpc_server.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-client/src/rpc_server.rs#L609

Added line #L609 was not covered by tests
}
}

async fn get_l2_block_status(&self, block_height: u64) -> RpcResult<L2BlockStatus> {
Expand Down
29 changes: 15 additions & 14 deletions crates/consensus-logic/src/client_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use strata_db::traits::{
};
use strata_primitives::prelude::*;
use strata_state::{
batch::{BatchCheckpoint, BatchInfo},
batch::{BatchCheckpoint, BatchCheckpointWithCommitment, BatchInfo, CommitmentInfo},
block,
client_state::*,
header::L2Header,
Expand Down Expand Up @@ -165,11 +165,12 @@ pub fn process_event<D: Database>(
writes.push(ClientStateWrite::CheckpointsReceived(
checkpoints
.iter()
.map(|x| {
.map(|batchcheckpoint_with_commitment| {
let batchcheckpoint = &batchcheckpoint_with_commitment.batch_checkpoint;

Check warning on line 169 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L168-L169

Added lines #L168 - L169 were not covered by tests
L1Checkpoint::new(
x.batch_info().clone(),
x.bootstrap_state().clone(),
!x.proof().is_empty(),
batchcheckpoint.batch_info().clone(),
batchcheckpoint.bootstrap_state().clone(),
!batchcheckpoint.proof().is_empty(),

Check warning on line 173 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L171-L173

Added lines #L171 - L173 were not covered by tests
*height,
)
})
Expand Down Expand Up @@ -278,9 +279,9 @@ fn handle_maturable_height(
/// A vector containing the valid sequence of `BatchCheckpoint`s, starting from the first valid one.
pub fn filter_verified_checkpoints(
state: &ClientState,
checkpoints: &[BatchCheckpoint],
checkpoints: &[BatchCheckpointWithCommitment],

Check warning on line 282 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L282

Added line #L282 was not covered by tests
params: &RollupParams,
) -> Vec<BatchCheckpoint> {
) -> Vec<BatchCheckpointWithCommitment> {

Check warning on line 284 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L284

Added line #L284 was not covered by tests
let l1_view = state.l1_view();
let last_verified = l1_view.verified_checkpoints().last();
let last_finalized = l1_view.last_finalized_checkpoint();
Expand All @@ -296,14 +297,14 @@ pub fn filter_verified_checkpoints(
let mut result_checkpoints = Vec::new();

for checkpoint in checkpoints {
let curr_idx = checkpoint.batch_info().idx;
let curr_idx = checkpoint.batch_checkpoint.batch_info().idx;

Check warning on line 300 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L300

Added line #L300 was not covered by tests
if curr_idx != expected_idx {
warn!(%expected_idx, %curr_idx, "Received invalid checkpoint idx, ignoring.");
continue;
}
if expected_idx == 0 && verify_proof(checkpoint, params).is_ok() {
if expected_idx == 0 && verify_proof(&checkpoint.batch_checkpoint, params).is_ok() {

Check warning on line 305 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L305

Added line #L305 was not covered by tests
result_checkpoints.push(checkpoint.clone());
last_valid_checkpoint = Some(checkpoint.batch_info());
last_valid_checkpoint = Some(checkpoint.batch_checkpoint.batch_info());

Check warning on line 307 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L307

Added line #L307 was not covered by tests
} else if expected_idx == 0 {
warn!(%expected_idx, "Received invalid checkpoint proof, ignoring.");
} else {
Expand All @@ -313,8 +314,8 @@ pub fn filter_verified_checkpoints(
let last_l2_tsn = last_valid_checkpoint
.expect("There should be a last_valid_checkpoint")
.l2_transition;
let l1_tsn = checkpoint.batch_info().l1_transition;
let l2_tsn = checkpoint.batch_info().l2_transition;
let l1_tsn = checkpoint.batch_checkpoint.batch_info().l1_transition;
let l2_tsn = checkpoint.batch_checkpoint.batch_info().l2_transition;

Check warning on line 318 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L317-L318

Added lines #L317 - L318 were not covered by tests

if l1_tsn.0 == last_l1_tsn.1 {
warn!(obtained = ?l1_tsn.0, expected = ?last_l1_tsn.1, "Received invalid checkpoint l1 transition, ignoring.");
Expand All @@ -324,9 +325,9 @@ pub fn filter_verified_checkpoints(
warn!(obtained = ?l2_tsn.0, expected = ?last_l2_tsn.1, "Received invalid checkpoint l2 transition, ignoring.");
continue;
}
if verify_proof(checkpoint, params).is_ok() {
if verify_proof(&checkpoint.batch_checkpoint, params).is_ok() {

Check warning on line 328 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L328

Added line #L328 was not covered by tests
result_checkpoints.push(checkpoint.clone());
last_valid_checkpoint = Some(checkpoint.batch_info());
last_valid_checkpoint = Some(checkpoint.batch_checkpoint.batch_info());

Check warning on line 330 in crates/consensus-logic/src/client_transition.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/client_transition.rs#L330

Added line #L330 was not covered by tests
} else {
warn!(%expected_idx, "Received invalid checkpoint proof, ignoring.");
continue;
Expand Down
9 changes: 9 additions & 0 deletions crates/consensus-logic/src/duty/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ fn extract_batch_duties(
return Ok(vec![]);
};

// BlockHash of last L1 block covered by the checkpoint
let Some(tip_blockid) = state.l1_view().tip_blkid() else {
debug!("no blocks read from L1");

Check warning on line 57 in crates/consensus-logic/src/duty/extractor.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/duty/extractor.rs#L56-L57

Added lines #L56 - L57 were not covered by tests
// Cannot create checkpoint without L1 blocks
return Ok(vec![]);

Check warning on line 59 in crates/consensus-logic/src/duty/extractor.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/duty/extractor.rs#L59

Added line #L59 was not covered by tests
};

match state.l1_view().last_finalized_checkpoint() {
// Cool, we are producing first batch!
None => {
Expand Down Expand Up @@ -102,6 +109,7 @@ fn extract_batch_duties(
l1_transition,
l2_transition,
tip_id,
*tip_blockid,

Check warning on line 112 in crates/consensus-logic/src/duty/extractor.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/duty/extractor.rs#L112

Added line #L112 was not covered by tests
(0, current_l1_state.total_accumulated_pow),
rollup_params_commitment,
);
Expand Down Expand Up @@ -137,6 +145,7 @@ fn extract_batch_duties(
l1_transition,
l2_transition,
tip_id,
*tip_blockid,

Check warning on line 148 in crates/consensus-logic/src/duty/extractor.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/duty/extractor.rs#L148

Added line #L148 was not covered by tests
(
checkpoint.l1_pow_transition.1,
current_l1_state.total_accumulated_pow,
Expand Down
18 changes: 15 additions & 3 deletions crates/consensus-logic/src/l1_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use strata_primitives::{
use strata_risc0_adapter::Risc0Verifier;
use strata_sp1_adapter::SP1Verifier;
use strata_state::{
batch::BatchCheckpoint, l1::L1Tx, sync_event::SyncEvent, tx::ProtocolOperation,
batch::{BatchCheckpoint, BatchCheckpointWithCommitment, CommitmentInfo},
l1::L1Tx,
sync_event::SyncEvent,
tx::ProtocolOperation,
};
use strata_tx_parser::messages::{BlockData, L1Event};
use strata_zkvm::ZKVMVerifier;
Expand Down Expand Up @@ -130,7 +133,7 @@ where
fn check_for_da_batch(
blockdata: &BlockData,
seq_pubkey: Option<XOnlyPublicKey>,
) -> Vec<BatchCheckpoint> {
) -> Vec<BatchCheckpointWithCommitment> {

Check warning on line 136 in crates/consensus-logic/src/l1_handler.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/l1_handler.rs#L136

Added line #L136 was not covered by tests
let protocol_ops_txs = blockdata.protocol_ops_txs();

let signed_checkpts = protocol_ops_txs
Expand All @@ -155,7 +158,16 @@ fn check_for_da_batch(
}
}
let checkpoint: BatchCheckpoint = signed_checkpoint.clone().into();
Some(checkpoint)

let blockhash = Buf32::from(*blockdata.block().block_hash().as_byte_array());
let txid = Buf32::from(*tx.compute_txid().as_byte_array());
let wtxid = Buf32::from(*tx.compute_wtxid().as_byte_array());
let commitment_info = CommitmentInfo::new(blockhash, txid, wtxid);

Some(BatchCheckpointWithCommitment::new(
checkpoint,
commitment_info,
))

Check warning on line 170 in crates/consensus-logic/src/l1_handler.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/l1_handler.rs#L161-L170

Added lines #L161 - L170 were not covered by tests
});
sig_verified_checkpoints.collect()
}
Expand Down
7 changes: 7 additions & 0 deletions crates/consensus-logic/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ fn handle_sync_event<D: Database, E: ExecEngineCtl>(

SyncAction::WriteCheckpoints(_height, checkpoints) => {
for c in checkpoints.iter() {
let commitment_info = &c.commitment;
let c = &c.batch_checkpoint;

Check warning on line 217 in crates/consensus-logic/src/worker.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/worker.rs#L216-L217

Added lines #L216 - L217 were not covered by tests
let idx = c.batch_info().idx();
let pstatus = CheckpointProvingStatus::ProofReady;
let cstatus = CheckpointConfStatus::Confirmed;
Expand All @@ -222,14 +224,18 @@ fn handle_sync_event<D: Database, E: ExecEngineCtl>(
c.proof().clone(),
pstatus,
cstatus,
Some(commitment_info.clone().into()),

Check warning on line 227 in crates/consensus-logic/src/worker.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/worker.rs#L227

Added line #L227 was not covered by tests
);

// Store
state.checkpoint_db().put_checkpoint_blocking(idx, entry)?;
}
}
// FIXME: never called
SyncAction::FinalizeCheckpoints(_height, checkpoints) => {
for c in checkpoints.iter() {
let commitment_info = &c.commitment;
let c = &c.batch_checkpoint;

Check warning on line 238 in crates/consensus-logic/src/worker.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/worker.rs#L237-L238

Added lines #L237 - L238 were not covered by tests
let idx = c.batch_info().idx();
let pstatus = CheckpointProvingStatus::ProofReady;
let cstatus = CheckpointConfStatus::Finalized;
Expand All @@ -239,6 +245,7 @@ fn handle_sync_event<D: Database, E: ExecEngineCtl>(
c.proof().clone(),
pstatus,
cstatus,
Some(commitment_info.clone().into()),

Check warning on line 248 in crates/consensus-logic/src/worker.rs

View check run for this annotation

Codecov / codecov/patch

crates/consensus-logic/src/worker.rs#L248

Added line #L248 was not covered by tests
);

// Update
Expand Down
25 changes: 24 additions & 1 deletion crates/db/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bitcoin::{
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use strata_primitives::buf::Buf32;
use strata_state::batch::{BatchCheckpoint, BatchInfo, BootstrapState};
use strata_state::batch::{BatchCheckpoint, BatchInfo, BootstrapState, CommitmentInfo};
use strata_zkvm::Proof;

/// Represents data for a blob we're still planning to inscribe.
Expand Down Expand Up @@ -157,6 +157,9 @@ pub struct CheckpointEntry {

/// Confirmation Status
pub confirmation_status: CheckpointConfStatus,

/// checkpoint txn info
pub commitment: Option<CheckpointCommitment>,
}

impl CheckpointEntry {
Expand All @@ -166,13 +169,15 @@ impl CheckpointEntry {
proof: Proof,
proving_status: CheckpointProvingStatus,
confirmation_status: CheckpointConfStatus,
commitment: Option<CheckpointCommitment>,

Check warning on line 172 in crates/db/src/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/db/src/types.rs#L172

Added line #L172 was not covered by tests
) -> Self {
Self {
batch_info,
bootstrap,
proof,
proving_status,
confirmation_status,
commitment,

Check warning on line 180 in crates/db/src/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/db/src/types.rs#L180

Added line #L180 was not covered by tests
}
}

Expand All @@ -188,6 +193,7 @@ impl CheckpointEntry {
Proof::default(),
CheckpointProvingStatus::PendingProof,
CheckpointConfStatus::Pending,
None,

Check warning on line 196 in crates/db/src/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/db/src/types.rs#L196

Added line #L196 was not covered by tests
)
}

Expand Down Expand Up @@ -225,6 +231,23 @@ pub enum CheckpointConfStatus {
Finalized,
}

#[derive(Debug, Clone, PartialEq, BorshSerialize, BorshDeserialize, Arbitrary)]
pub struct CheckpointCommitment {
pub blockhash: Buf32,
pub txid: Buf32,
pub wtxid: Buf32,
}

impl From<CommitmentInfo> for CheckpointCommitment {
fn from(value: CommitmentInfo) -> Self {
Self {
blockhash: value.blockhash,
txid: value.txid,
wtxid: value.wtxid,
}
}

Check warning on line 248 in crates/db/src/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/db/src/types.rs#L242-L248

Added lines #L242 - L248 were not covered by tests
}

#[cfg(test)]
mod tests {
use serde_json;
Expand Down
1 change: 1 addition & 0 deletions crates/proof-impl/checkpoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub fn process_checkpoint_proof(
l2_batch_output.final_snapshot.hash,
),
l2_batch_output.final_snapshot.l2_blockid,
l1_batch_output.final_snapshot.hash.into(),

Check warning on line 89 in crates/proof-impl/checkpoint/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/proof-impl/checkpoint/src/lib.rs#L89

Added line #L89 was not covered by tests
(
l1_batch_output.initial_snapshot.acc_pow,
l1_batch_output.final_snapshot.acc_pow,
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub trait StrataApi {

/// Get latest checkpoint info
#[method(name = "getLatestCheckpointIndex")]
async fn get_latest_checkpoint_index(&self) -> RpcResult<Option<u64>>;
async fn get_latest_checkpoint_index(&self, finalized: Option<bool>) -> RpcResult<Option<u64>>;

/// Get nth checkpoint info if any
#[method(name = "getCheckpointInfo")]
Expand Down
39 changes: 38 additions & 1 deletion crates/rpc/types/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
//! - implementation of RPC client
//! - crate for just data structures that represents the JSON responses from Bitcoin core RPC

use bitcoin::{Network, Txid};
use bitcoin::{hashes::Hash, Network, Txid, Wtxid};
use serde::{Deserialize, Serialize};
use strata_db::types::{CheckpointCommitment, CheckpointEntry};
use strata_state::{
batch::BatchInfo, bridge_duties::BridgeDuty, bridge_ops::WithdrawalIntent, id::L2BlockId,
l1::L1BlockId,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -196,6 +198,27 @@ pub struct RawBlockWitness {
pub raw_chain_state: Vec<u8>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]

Check warning on line 201 in crates/rpc/types/src/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/rpc/types/src/types.rs#L201

Added line #L201 was not covered by tests
pub struct RpcCheckpointCommitmentInfo {
/// block where checkpoint was posted
pub blockhash: L1BlockId,
/// txid of txn for this checkpoint
pub txid: Txid,
/// wtxid of txn for this checkpoint
pub wtxid: Wtxid,
// other info
}

impl From<CheckpointCommitment> for RpcCheckpointCommitmentInfo {
fn from(value: CheckpointCommitment) -> Self {
Self {
blockhash: value.blockhash.into(),
txid: Txid::from_byte_array(*value.txid.as_ref()),
wtxid: Wtxid::from_byte_array(*value.wtxid.as_ref()),
}
}

Check warning on line 219 in crates/rpc/types/src/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/rpc/types/src/types.rs#L213-L219

Added lines #L213 - L219 were not covered by tests
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RpcCheckpointInfo {
/// The index of the checkpoint
Expand All @@ -206,6 +229,10 @@ pub struct RpcCheckpointInfo {
pub l2_range: (u64, u64),
/// L2 block that this checkpoint covers
pub l2_blockid: L2BlockId,
/// Last L1 block covered by this checkpoint
pub l1_blockid: L1BlockId,
/// Info on txn where checkpoint is committed on chain
pub commitment: Option<RpcCheckpointCommitmentInfo>,
}

impl From<BatchInfo> for RpcCheckpointInfo {
Expand All @@ -215,10 +242,20 @@ impl From<BatchInfo> for RpcCheckpointInfo {
l1_range: value.l1_range,
l2_range: value.l2_range,
l2_blockid: value.l2_blockid,
l1_blockid: value.l1_blockid,
commitment: None,

Check warning on line 246 in crates/rpc/types/src/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/rpc/types/src/types.rs#L245-L246

Added lines #L245 - L246 were not covered by tests
}
}
}

impl From<CheckpointEntry> for RpcCheckpointInfo {
fn from(value: CheckpointEntry) -> Self {
let mut item: Self = value.batch_info.into();
item.commitment = value.commitment.map(Into::into);
item
}

Check warning on line 256 in crates/rpc/types/src/types.rs

View check run for this annotation

Codecov / codecov/patch

crates/rpc/types/src/types.rs#L252-L256

Added lines #L252 - L256 were not covered by tests
}

/// The duties assigned to an operator within a given range.
///
/// # Note
Expand Down
Loading

0 comments on commit 2867c19

Please sign in to comment.