Skip to content

Commit

Permalink
Some renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibek Pandey committed Jan 21, 2025
1 parent 4cfd6ed commit c175668
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 84 deletions.
4 changes: 2 additions & 2 deletions crates/btcio/src/writer/bundler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{sync::Arc, time::Duration};

use strata_db::types::{IntentEntry, IntentStatus, PayloadEntry};
use strata_db::types::{BundledPayloadEntry, IntentEntry, IntentStatus};
use strata_storage::ops::writer::EnvelopeDataOps;
use tokio::time::sleep;
use tracing::*;
Expand Down Expand Up @@ -30,7 +30,7 @@ async fn process_unbundled_entries(
for mut entry in unbundled {
// NOTE: In future, the logic to create payload will be different. We need to group
// intents and create payload entries accordingly
let payload_entry = PayloadEntry::new_unsigned(vec![entry.payload().clone()]);
let payload_entry = BundledPayloadEntry::new_unsigned(vec![entry.payload().clone()]);

// TODO: the following block till "Atomic Ends" should be atomic.
let idx = ops.get_next_payload_idx_async().await?;
Expand Down
10 changes: 5 additions & 5 deletions crates/btcio/src/writer/signer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use bitcoin::{consensus, Transaction};
use strata_db::types::{L1TxEntry, PayloadEntry};
use strata_db::types::{BundledPayloadEntry, L1TxEntry};
use strata_primitives::buf::Buf32;
use tracing::*;

Expand All @@ -20,7 +20,7 @@ type BlobIdx = u64;
/// 2. A signed intent needs to be resigned because somehow its inputs were spent/missing
/// 3. A confirmed block that includes the tx gets reorged
pub async fn create_and_sign_payload_envelopes<W: WriterRpc>(
payloadentry: &PayloadEntry,
payloadentry: &BundledPayloadEntry,
broadcast_handle: &L1BroadcastHandle,
ctx: Arc<WriterContext<W>>,
) -> Result<(Buf32, Buf32), EnvelopeError> {
Expand Down Expand Up @@ -59,7 +59,7 @@ pub async fn create_and_sign_payload_envelopes<W: WriterRpc>(

#[cfg(test)]
mod test {
use strata_db::types::{PayloadEntry, PayloadL1Status};
use strata_db::types::{BundledPayloadEntry, L1BundleStatus};
use strata_primitives::l1::payload::L1Payload;

use super::*;
Expand All @@ -76,9 +76,9 @@ mod test {

// First insert an unsigned blob
let payload = L1Payload::new_da([1; 100].to_vec());
let entry = PayloadEntry::new_unsigned(vec![payload]);
let entry = BundledPayloadEntry::new_unsigned(vec![payload]);

assert_eq!(entry.status, PayloadL1Status::Unsigned);
assert_eq!(entry.status, L1BundleStatus::Unsigned);
assert_eq!(entry.commit_txid, Buf32::zero());
assert_eq!(entry.reveal_txid, Buf32::zero());

Expand Down
74 changes: 37 additions & 37 deletions crates/btcio/src/writer/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bitcoin::Address;
use strata_config::btcio::WriterConfig;
use strata_db::{
traits::L1WriterDatabase,
types::{IntentEntry, L1TxStatus, PayloadEntry, PayloadL1Status},
types::{BundledPayloadEntry, IntentEntry, L1BundleStatus, L1TxStatus},
};
use strata_primitives::{
l1::payload::{PayloadDest, PayloadIntent},
Expand Down Expand Up @@ -137,7 +137,7 @@ fn get_next_payloadidx_to_watch(insc_ops: &EnvelopeDataOps) -> anyhow::Result<u6
let Some(payload) = insc_ops.get_payload_entry_by_idx_blocking(next_idx - 1)? else {
break;
};
if payload.status == PayloadL1Status::Finalized {
if payload.status == L1BundleStatus::Finalized {
break;
};
next_idx -= 1;
Expand Down Expand Up @@ -174,7 +174,7 @@ pub async fn watcher_task<W: WriterRpc>(
match payloadentry.status {
// If unsigned or needs resign, create new signed commit/reveal txs and update the
// entry
PayloadL1Status::Unsigned | PayloadL1Status::NeedsResign => {
L1BundleStatus::Unsigned | L1BundleStatus::NeedsResign => {
debug!(?payloadentry.status, %curr_payloadidx, "Processing unsigned payloadentry");
match create_and_sign_payload_envelopes(
&payloadentry,
Expand All @@ -185,7 +185,7 @@ pub async fn watcher_task<W: WriterRpc>(
{
Ok((cid, rid)) => {
let mut updated_entry = payloadentry.clone();
updated_entry.status = PayloadL1Status::Unpublished;
updated_entry.status = L1BundleStatus::Unpublished;
updated_entry.commit_txid = cid;
updated_entry.reveal_txid = rid;
insc_ops
Expand All @@ -206,13 +206,13 @@ pub async fn watcher_task<W: WriterRpc>(
}
}
// If finalized, nothing to do, move on to process next entry
PayloadL1Status::Finalized => {
L1BundleStatus::Finalized => {
curr_payloadidx += 1;
}
// If entry is signed but not finalized or excluded yet, check broadcast txs status
PayloadL1Status::Published
| PayloadL1Status::Confirmed
| PayloadL1Status::Unpublished => {
L1BundleStatus::Published
| L1BundleStatus::Confirmed
| L1BundleStatus::Unpublished => {
debug!(%curr_payloadidx, "Checking payloadentry's broadcast status");
let commit_tx = broadcast_handle
.get_tx_entry_by_id_async(payloadentry.commit_txid)
Expand All @@ -237,14 +237,14 @@ pub async fn watcher_task<W: WriterRpc>(
.put_payload_entry_async(curr_payloadidx, updated_entry)
.await?;

if new_status == PayloadL1Status::Finalized {
if new_status == L1BundleStatus::Finalized {
curr_payloadidx += 1;
}
}
_ => {
warn!(%curr_payloadidx, "Corresponding commit/reveal entry for payloadentry not found in broadcast db. Sign and create transactions again.");
let mut updated_entry = payloadentry.clone();
updated_entry.status = PayloadL1Status::Unsigned;
updated_entry.status = L1BundleStatus::Unsigned;
insc_ops
.put_payload_entry_async(curr_payloadidx, updated_entry)
.await?;
Expand All @@ -260,15 +260,15 @@ pub async fn watcher_task<W: WriterRpc>(
}

async fn update_l1_status(
payloadentry: &PayloadEntry,
new_status: &PayloadL1Status,
payloadentry: &BundledPayloadEntry,
new_status: &L1BundleStatus,
status_channel: &StatusChannel,
) {
// Update L1 status. Since we are processing one payloadentry at a time, if the entry is
// finalized/confirmed, then it means it is published as well
if *new_status == PayloadL1Status::Published
|| *new_status == PayloadL1Status::Confirmed
|| *new_status == PayloadL1Status::Finalized
if *new_status == L1BundleStatus::Published
|| *new_status == L1BundleStatus::Confirmed
|| *new_status == L1BundleStatus::Finalized
{
let status_updates = [
L1StatusUpdate::LastPublishedTxid(payloadentry.reveal_txid.into()),
Expand All @@ -283,23 +283,23 @@ async fn update_l1_status(
fn determine_payload_next_status(
commit_status: &L1TxStatus,
reveal_status: &L1TxStatus,
) -> PayloadL1Status {
) -> L1BundleStatus {
match (&commit_status, &reveal_status) {
// If reveal is finalized, both are finalized
(_, L1TxStatus::Finalized { .. }) => PayloadL1Status::Finalized,
(_, L1TxStatus::Finalized { .. }) => L1BundleStatus::Finalized,
// If reveal is confirmed, both are confirmed
(_, L1TxStatus::Confirmed { .. }) => PayloadL1Status::Confirmed,
(_, L1TxStatus::Confirmed { .. }) => L1BundleStatus::Confirmed,
// If reveal is published regardless of commit, the payload is published
(_, L1TxStatus::Published) => PayloadL1Status::Published,
(_, L1TxStatus::Published) => L1BundleStatus::Published,
// if commit has invalid inputs, needs resign
(L1TxStatus::InvalidInputs, _) => PayloadL1Status::NeedsResign,
(L1TxStatus::InvalidInputs, _) => L1BundleStatus::NeedsResign,
// If commit is unpublished, both are upublished
(L1TxStatus::Unpublished, _) => PayloadL1Status::Unpublished,
(L1TxStatus::Unpublished, _) => L1BundleStatus::Unpublished,
// If commit is published but not reveal, the payload is unpublished
(_, L1TxStatus::Unpublished) => PayloadL1Status::Unpublished,
(_, L1TxStatus::Unpublished) => L1BundleStatus::Unpublished,
// If reveal has invalid inputs, these need resign because we can do nothing with just
// commit tx confirmed. This should not occur in practice
(_, L1TxStatus::InvalidInputs) => PayloadL1Status::NeedsResign,
(_, L1TxStatus::InvalidInputs) => L1BundleStatus::NeedsResign,
}
}

Expand All @@ -326,21 +326,21 @@ mod test {
fn test_initialize_writer_state_with_existing_payloads() {
let iops = get_envelope_ops();

let mut e1: PayloadEntry = ArbitraryGenerator::new().generate();
e1.status = PayloadL1Status::Finalized;
let mut e1: BundledPayloadEntry = ArbitraryGenerator::new().generate();
e1.status = L1BundleStatus::Finalized;
iops.put_payload_entry_blocking(0, e1).unwrap();

let mut e2: PayloadEntry = ArbitraryGenerator::new().generate();
e2.status = PayloadL1Status::Published;
let mut e2: BundledPayloadEntry = ArbitraryGenerator::new().generate();
e2.status = L1BundleStatus::Published;
iops.put_payload_entry_blocking(1, e2).unwrap();
let expected_idx = 1; // All entries before this do not need to be watched.

let mut e3: PayloadEntry = ArbitraryGenerator::new().generate();
e3.status = PayloadL1Status::Unsigned;
let mut e3: BundledPayloadEntry = ArbitraryGenerator::new().generate();
e3.status = L1BundleStatus::Unsigned;
iops.put_payload_entry_blocking(2, e3).unwrap();

let mut e4: PayloadEntry = ArbitraryGenerator::new().generate();
e4.status = PayloadL1Status::Unsigned;
let mut e4: BundledPayloadEntry = ArbitraryGenerator::new().generate();
e4.status = L1BundleStatus::Unsigned;
iops.put_payload_entry_blocking(3, e4).unwrap();

let idx = get_next_payloadidx_to_watch(&iops).unwrap();
Expand All @@ -353,37 +353,37 @@ mod test {
// When both are unpublished
let (commit_status, reveal_status) = (L1TxStatus::Unpublished, L1TxStatus::Unpublished);
let next = determine_payload_next_status(&commit_status, &reveal_status);
assert_eq!(next, PayloadL1Status::Unpublished);
assert_eq!(next, L1BundleStatus::Unpublished);

// When both are Finalized
let fin = L1TxStatus::Finalized { confirmations: 5 };
let (commit_status, reveal_status) = (fin.clone(), fin);
let next = determine_payload_next_status(&commit_status, &reveal_status);
assert_eq!(next, PayloadL1Status::Finalized);
assert_eq!(next, L1BundleStatus::Finalized);

// When both are Confirmed
let conf = L1TxStatus::Confirmed { confirmations: 5 };
let (commit_status, reveal_status) = (conf.clone(), conf.clone());
let next = determine_payload_next_status(&commit_status, &reveal_status);
assert_eq!(next, PayloadL1Status::Confirmed);
assert_eq!(next, L1BundleStatus::Confirmed);

// When both are Published
let publ = L1TxStatus::Published;
let (commit_status, reveal_status) = (publ.clone(), publ.clone());
let next = determine_payload_next_status(&commit_status, &reveal_status);
assert_eq!(next, PayloadL1Status::Published);
assert_eq!(next, L1BundleStatus::Published);

// When both have invalid
let (commit_status, reveal_status) = (L1TxStatus::InvalidInputs, L1TxStatus::InvalidInputs);
let next = determine_payload_next_status(&commit_status, &reveal_status);
assert_eq!(next, PayloadL1Status::NeedsResign);
assert_eq!(next, L1BundleStatus::NeedsResign);

// When reveal has invalid inputs but commit is confirmed. I doubt this would happen in
// practice for our case.
// Then the payload status should be NeedsResign i.e. the payload should be signed again and
// published.
let (commit_status, reveal_status) = (conf.clone(), L1TxStatus::InvalidInputs);
let next = determine_payload_next_status(&commit_status, &reveal_status);
assert_eq!(next, PayloadL1Status::NeedsResign);
assert_eq!(next, L1BundleStatus::NeedsResign);
}
}
2 changes: 1 addition & 1 deletion crates/consensus-logic/src/duty/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ fn perform_duty<D: Database, E: ExecEngineCtl>(
Ok(())
}
Duty::CommitBatch(data) => {
info!(data = ?data, "commit batch duty");
info!(epoch_idx = ?data.idx(), "commit batch duty");

let checkpoint =
check_and_get_batch_checkpoint(data, checkpoint_handle, pool, params.as_ref())?;
Expand Down
14 changes: 7 additions & 7 deletions crates/db/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use strata_zkvm::ProofReceipt;

use crate::{
entities::bridge_tx_state::BridgeTxState,
types::{CheckpointEntry, IntentEntry, L1TxEntry, PayloadEntry},
types::{BundledPayloadEntry, CheckpointEntry, IntentEntry, L1TxEntry},
DbResult,
};

Expand Down Expand Up @@ -244,14 +244,14 @@ pub trait CheckpointDatabase {
fn put_batch_checkpoint(&self, batchidx: u64, entry: CheckpointEntry) -> DbResult<()>;
}

/// A trait encapsulating provider and store traits to create/update [`PayloadEntry`] in the
/// database and to fetch [`PayloadEntry`] and indices from the database
/// A trait encapsulating provider and store traits to create/update [`BundledPayloadEntry`] in the
/// database and to fetch [`BundledPayloadEntry`] and indices from the database
pub trait L1WriterDatabase {
/// Store the [`PayloadEntry`].
fn put_payload_entry(&self, idx: u64, payloadentry: PayloadEntry) -> DbResult<()>;
/// Store the [`BundledPayloadEntry`].
fn put_payload_entry(&self, idx: u64, payloadentry: BundledPayloadEntry) -> DbResult<()>;

/// Get a [`PayloadEntry`] by its index.
fn get_payload_entry_by_idx(&self, idx: u64) -> DbResult<Option<PayloadEntry>>;
/// Get a [`BundledPayloadEntry`] by its index.
fn get_payload_entry_by_idx(&self, idx: u64) -> DbResult<Option<BundledPayloadEntry>>;

/// Get the next payload index
fn get_next_payload_idx(&self) -> DbResult<u64>;
Expand Down
18 changes: 9 additions & 9 deletions crates/db/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,30 @@ impl IntentEntry {
}

/// Status of Intent indicating various stages of being bundled to L1 transaction.
/// Unbundled Intents are collected and bundled to create [`PayloadEntry].
/// Unbundled Intents are collected and bundled to create [`BundledPayloadEntry].
#[derive(Debug, Clone, PartialEq, BorshSerialize, BorshDeserialize, Arbitrary)]
pub enum IntentStatus {
// It is not bundled yet, and thus will be collected and processed by bundler.
Unbundled,
// It has been bundled to [`PayloadEntry`] with given bundle idx.
// It has been bundled to [`BundledPayloadEntry`] with given bundle idx.
Bundled(u64),
}

/// Represents data for a payload we're still planning to post to L1.
#[derive(Debug, Clone, PartialEq, BorshSerialize, BorshDeserialize, Arbitrary)]
pub struct PayloadEntry {
pub struct BundledPayloadEntry {
pub payloads: Vec<L1Payload>,
pub commit_txid: Buf32,
pub reveal_txid: Buf32,
pub status: PayloadL1Status,
pub status: L1BundleStatus,
}

impl PayloadEntry {
impl BundledPayloadEntry {
pub fn new(
payloads: Vec<L1Payload>,
commit_txid: Buf32,
reveal_txid: Buf32,
status: PayloadL1Status,
status: L1BundleStatus,
) -> Self {
Self {
payloads,
Expand All @@ -75,21 +75,21 @@ impl PayloadEntry {
}
}

/// Create new unsigned [`PayloadEntry`].
/// Create new unsigned [`BundledPayloadEntry`].
///
/// NOTE: This won't have commit - reveal pairs associated with it.
/// Because it is better to defer gathering utxos as late as possible to prevent being spent
/// by others. Those will be created and signed in a single step.
pub fn new_unsigned(payloads: Vec<L1Payload>) -> Self {
let cid = Buf32::zero();
let rid = Buf32::zero();
Self::new(payloads, cid, rid, PayloadL1Status::Unsigned)
Self::new(payloads, cid, rid, L1BundleStatus::Unsigned)
}
}

/// Various status that transactions corresponding to a payload can be in L1
#[derive(Debug, Clone, PartialEq, BorshSerialize, BorshDeserialize, Arbitrary)]
pub enum PayloadL1Status {
pub enum L1BundleStatus {
/// The payload has not been signed yet, i.e commit-reveal transactions have not been created
/// yet.
Unsigned,
Expand Down
3 changes: 0 additions & 3 deletions crates/l1tx/src/envelope/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use strata_primitives::{
l1::payload::{L1Payload, L1PayloadType},
params::Params,
};
use tracing::*;

// Generates a [`ScriptBuf`] that consists of `OP_IF .. OP_ENDIF` block
pub fn build_envelope_script(
Expand Down Expand Up @@ -45,9 +44,7 @@ fn build_payload_envelope(
));

// Insert actual data
trace!(batchdata_size = %payload.data().len(), "Inserting batch data");
for chunk in payload.data().chunks(520) {
trace!(size=%chunk.len(), "inserting chunk");
builder = builder.push_slice(PushBytesBuf::try_from(chunk.to_vec())?);
}
builder = builder.push_opcode(OP_ENDIF);
Expand Down
9 changes: 3 additions & 6 deletions crates/l1tx/src/envelope/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::from_utf8;

use bitcoin::{
opcodes::all::OP_IF,
script::{Instruction, Instructions},
Expand Down Expand Up @@ -84,11 +82,10 @@ fn parse_l1_payload(
Ok(L1Payload::new(payload, ptype))
}

fn parse_payload_type(bytes: &[u8], params: &RollupParams) -> Option<L1PayloadType> {
let str = from_utf8(bytes).ok()?;
if params.checkpoint_tag == str {
fn parse_payload_type(tag_bytes: &[u8], params: &RollupParams) -> Option<L1PayloadType> {
if params.checkpoint_tag.as_bytes() == tag_bytes {
Some(L1PayloadType::Checkpoint)
} else if params.da_tag == str {
} else if params.da_tag.as_bytes() == tag_bytes {
Some(L1PayloadType::Da)
} else {
None
Expand Down
Loading

0 comments on commit c175668

Please sign in to comment.