Skip to content

Commit

Permalink
l1/primitives: Rename EpochedL1BlockManifest and L1BlockManifest
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibek Pandey committed Dec 10, 2024
1 parent 89b8f5c commit 7501c9c
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 44 deletions.
6 changes: 2 additions & 4 deletions bin/strata-client/src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ mod tests {
use strata_primitives::{
bridge::OperatorIdx,
buf::Buf32,
l1::{
BitcoinAmount, EpochedL1BlockManifest, L1BlockManifest, L1TxProof, OutputRef, XOnlyPk,
},
l1::{BitcoinAmount, L1BlockManifest, L1BlockRecord, L1TxProof, OutputRef, XOnlyPk},
};
use strata_rocksdb::{test_utils::get_rocksdb_tmp_instance, L1Db};
use strata_state::{
Expand Down Expand Up @@ -383,7 +381,7 @@ mod tests {
})
.collect();

let mf: EpochedL1BlockManifest = arb.generate();
let mf: L1BlockManifest = arb.generate();

// Insert block data
let res = l1_db.put_block_data(idx, mf.clone(), txs.clone());
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus-logic/src/csm/client_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ pub fn filter_verified_checkpoints(
mod tests {
use bitcoin::params::MAINNET;
use strata_db::traits::L1Database;
use strata_primitives::{block_credential, l1::L1BlockManifest};
use strata_primitives::{block_credential, l1::L1BlockRecord};
use strata_rocksdb::test_utils::get_common_db;
use strata_state::{l1::L1BlockId, operation};
use strata_test_utils::{
Expand Down
8 changes: 4 additions & 4 deletions crates/consensus-logic/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use strata_db::{errors::DbError, traits::*};
use strata_primitives::{
buf::{Buf32, Buf64},
evm_exec::create_evm_extra_payload,
l1::L1BlockManifest,
l1::L1BlockRecord,
params::{OperatorConfig, Params},
};
use strata_state::{
Expand Down Expand Up @@ -87,14 +87,14 @@ fn load_pre_genesis_l1_manifests(
l1_db: &impl L1Database,
horizon_height: u64,
genesis_height: u64,
) -> anyhow::Result<Vec<L1BlockManifest>> {
) -> anyhow::Result<Vec<L1BlockRecord>> {
let mut manifests = Vec::new();
for height in horizon_height..=genesis_height {
let Some(mf) = l1_db.get_block_manifest(height)? else {
return Err(Error::MissingL1BlockHeight(height).into());
};

manifests.push(mf.into_manifest());
manifests.push(mf.into_record());
}

Ok(manifests)
Expand Down Expand Up @@ -141,7 +141,7 @@ pub fn make_genesis_block(params: &Params) -> L2BlockBundle {

pub fn make_genesis_chainstate(
gblock: &L2BlockBundle,
pregenesis_mfs: Vec<L1BlockManifest>,
pregenesis_mfs: Vec<L1BlockRecord>,
params: &Params,
) -> Chainstate {
let genesis_blkid = gblock.header().get_blockid();
Expand Down
8 changes: 4 additions & 4 deletions crates/consensus-logic/src/l1_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use strata_db::traits::{Database, L1Database};
use strata_primitives::{
block_credential::CredRule,
buf::Buf32,
l1::{Epoch, EpochedL1BlockManifest, L1BlockManifest, L1TxProof},
l1::{Epoch, L1BlockManifest, L1BlockRecord, L1TxProof},
params::{Params, RollupParams},
vk::RollupVerifyingKey,
};
Expand Down Expand Up @@ -192,16 +192,16 @@ pub fn verify_proof(checkpoint: &BatchCheckpoint, rollup_params: &RollupParams)

/// Given a block, generates a manifest of the parts we care about that we can
/// store in the database.
fn generate_block_manifest(block: &Block, epoch: Epoch) -> EpochedL1BlockManifest {
fn generate_block_manifest(block: &Block, epoch: Epoch) -> L1BlockManifest {
let blockid = Buf32::from(block.block_hash().to_raw_hash().to_byte_array());
let root = block
.witness_root()
.map(|x| x.to_byte_array())
.unwrap_or_default();
let header = serialize(&block.header);

let mf = L1BlockManifest::new(blockid, header, Buf32::from(root));
EpochedL1BlockManifest::new(mf, epoch)
let mf = L1BlockRecord::new(blockid, header, Buf32::from(root));
L1BlockManifest::new(mf, epoch)
}

fn generate_l1txs(blockdata: &BlockData) -> Vec<L1Tx> {
Expand Down
4 changes: 2 additions & 2 deletions crates/db/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait L1Database {
/// Atomically extends the chain with a new block, providing the manifest
/// and a list of transactions we find relevant. Returns error if
/// provided out-of-order.
fn put_block_data(&self, idx: u64, mf: EpochedL1BlockManifest, txs: Vec<L1Tx>) -> DbResult<()>;
fn put_block_data(&self, idx: u64, mf: L1BlockManifest, txs: Vec<L1Tx>) -> DbResult<()>;

/// Stores an MMR checkpoint so we have to query less far back. If the
/// provided height does not match the entries in the MMR, will return an
Expand All @@ -59,7 +59,7 @@ pub trait L1Database {
fn get_chain_tip(&self) -> DbResult<Option<u64>>;

/// Gets the block manifest for a block index.
fn get_block_manifest(&self, idx: u64) -> DbResult<Option<EpochedL1BlockManifest>>;
fn get_block_manifest(&self, idx: u64) -> DbResult<Option<L1BlockManifest>>;

/// Returns a half-open interval of block hashes, if we have all of them
/// present. Otherwise, returns error.
Expand Down
26 changes: 13 additions & 13 deletions crates/primitives/src/l1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ impl L1TxProof {

/// Includes `L1BlockManifest` along with scan rules that it is applied to
#[derive(Clone, Debug, PartialEq, Eq, BorshSerialize, BorshDeserialize, Arbitrary)]
pub struct EpochedL1BlockManifest {
pub struct L1BlockManifest {
/// The actual l1 manifest
manifest: L1BlockManifest,
record: L1BlockRecord,
/// Epoch, whose scan rules are applied to this manifest
epoch: Epoch,
}
Expand All @@ -134,32 +134,32 @@ pub enum Epoch {
AtTransition(u64, u64),
}

impl EpochedL1BlockManifest {
pub fn new(manifest: L1BlockManifest, epoch: Epoch) -> Self {
Self { manifest, epoch }
impl L1BlockManifest {
pub fn new(record: L1BlockRecord, epoch: Epoch) -> Self {
Self { record, epoch }
}

pub fn header(&self) -> &[u8] {
self.manifest.header()
self.record.header()
}

pub fn block_hash(&self) -> Buf32 {
self.manifest.block_hash()
self.record.block_hash()
}

pub fn txs_root(&self) -> Buf32 {
self.manifest.txs_root()
self.record.txs_root()
}

pub fn into_manifest(self) -> L1BlockManifest {
self.manifest
pub fn into_record(self) -> L1BlockRecord {
self.record
}
}

/// Describes an L1 block and associated data that we need to keep around.
// TODO should we include the block index here?
#[derive(Clone, Debug, PartialEq, Eq, BorshSerialize, BorshDeserialize, Arbitrary)]
pub struct L1BlockManifest {
pub struct L1BlockRecord {
/// Block hash/ID, kept here so we don't have to be aware of the hash function
/// here. This is what we use in the MMR.
blockid: Buf32,
Expand All @@ -173,7 +173,7 @@ pub struct L1BlockManifest {
txs_root: Buf32,
}

impl L1BlockManifest {
impl L1BlockRecord {
pub fn new(blockid: Buf32, header: Vec<u8>, txs_root: Buf32) -> Self {
Self {
blockid,
Expand All @@ -196,7 +196,7 @@ impl L1BlockManifest {
}
}

impl From<Block> for L1BlockManifest {
impl From<Block> for L1BlockRecord {
fn from(block: Block) -> Self {
let blockid = Buf32(block.block_hash().to_raw_hash().to_byte_array());
let root = block
Expand Down
12 changes: 6 additions & 6 deletions crates/rocksdb-store/src/l1/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use strata_db::{errors::DbError, traits::*, DbResult};
use strata_mmr::CompactMmr;
use strata_primitives::{
buf::Buf32,
l1::{EpochedL1BlockManifest, L1TxRef},
l1::{L1BlockManifest, L1TxRef},
};
use strata_state::l1::L1Tx;
use tracing::*;
Expand Down Expand Up @@ -42,7 +42,7 @@ impl L1Db {
}

impl L1Database for L1Db {
fn put_block_data(&self, idx: u64, mf: EpochedL1BlockManifest, txs: Vec<L1Tx>) -> DbResult<()> {
fn put_block_data(&self, idx: u64, mf: L1BlockManifest, txs: Vec<L1Tx>) -> DbResult<()> {
// If there is latest block then expect the idx to be 1 greater than the block number, else
// allow arbitrary block number to be inserted
match self.get_latest_block_number()? {
Expand Down Expand Up @@ -179,7 +179,7 @@ impl L1Database for L1Db {
Ok(res)
}

fn get_block_manifest(&self, idx: u64) -> DbResult<Option<EpochedL1BlockManifest>> {
fn get_block_manifest(&self, idx: u64) -> DbResult<Option<L1BlockManifest>> {
Ok(self.db.get::<L1BlockSchema>(&idx)?)
}

Expand Down Expand Up @@ -238,11 +238,11 @@ mod tests {
idx: u64,
db: &L1Db,
num_txs: usize,
) -> (EpochedL1BlockManifest, Vec<L1Tx>, CompactMmr) {
) -> (L1BlockManifest, Vec<L1Tx>, CompactMmr) {
let arb = ArbitraryGenerator::new();

// TODO maybe tweak this to make it a bit more realistic?
let mf: EpochedL1BlockManifest = arb.generate();
let mf: L1BlockManifest = arb.generate();
let txs: Vec<L1Tx> = (0..num_txs)
.map(|i| {
let proof = L1TxProof::new(i as u32, arb.generate());
Expand Down Expand Up @@ -289,7 +289,7 @@ mod tests {
.collect();
let res = db.put_block_data(
invalid_idx,
ArbitraryGenerator::new().generate::<EpochedL1BlockManifest>(),
ArbitraryGenerator::new().generate::<L1BlockManifest>(),
txs,
);
assert!(res.is_err(), "Should fail to insert to db");
Expand Down
4 changes: 2 additions & 2 deletions crates/rocksdb-store/src/l1/schemas.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use strata_mmr::CompactMmr;
use strata_primitives::{buf::Buf32, l1::EpochedL1BlockManifest};
use strata_primitives::{buf::Buf32, l1::L1BlockManifest};
use strata_state::l1::L1Tx;

use crate::{
Expand All @@ -13,7 +13,7 @@ type HeaderHash = Buf32;
// L1 Block Schema and corresponding codecs implementation
define_table_with_seek_key_codec!(
/// A table to store L1 Block data. Maps block index to header
(L1BlockSchema) u64 => EpochedL1BlockManifest
(L1BlockSchema) u64 => L1BlockManifest
);

// L1 Txns Schema and corresponding codecs implementation
Expand Down
4 changes: 2 additions & 2 deletions crates/state/src/l1/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl L1HeaderRecord {
}
}

impl From<&strata_primitives::l1::L1BlockManifest> for L1HeaderRecord {
fn from(value: &strata_primitives::l1::L1BlockManifest) -> Self {
impl From<&strata_primitives::l1::L1BlockRecord> for L1HeaderRecord {
fn from(value: &strata_primitives::l1::L1BlockRecord) -> Self {
Self {
blkid: value.block_hash().into(),
buf: value.header().to_vec(),
Expand Down
12 changes: 6 additions & 6 deletions crates/test-utils/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bitcoin::{
hashes::Hash,
Block, Transaction,
};
use strata_primitives::{buf::Buf32, l1::L1BlockManifest};
use strata_primitives::{buf::Buf32, l1::L1BlockRecord};
use strata_state::l1::{
get_difficulty_adjustment_height, BtcParams, HeaderVerificationState, L1BlockId, TimestampStore,
};
Expand All @@ -26,11 +26,11 @@ pub fn get_test_bitcoin_txs() -> Vec<Transaction> {
.collect()
}

pub fn gen_l1_chain(len: usize) -> Vec<L1BlockManifest> {
pub fn gen_l1_chain(len: usize) -> Vec<L1BlockRecord> {
// FIXME this is bad, the blocks generated are nonsensical
let mut blocks = vec![];
for _ in 0..len {
let block: L1BlockManifest = ArbitraryGenerator::new().generate();
let block: L1BlockRecord = ArbitraryGenerator::new().generate();
blocks.push(block);
}
blocks
Expand Down Expand Up @@ -117,16 +117,16 @@ impl BtcChainSegment {
}
}

pub fn get_block_manifest(&self, height: u32) -> L1BlockManifest {
pub fn get_block_manifest(&self, height: u32) -> L1BlockRecord {
let header = self.get_header(height);
L1BlockManifest::new(
L1BlockRecord::new(
Buf32::from(header.block_hash().as_raw_hash().to_byte_array()),
serialize(&header),
Buf32::from(header.merkle_root.as_raw_hash().to_byte_array()),
)
}

pub fn get_block_manifests(&self, from_height: u32, len: usize) -> Vec<L1BlockManifest> {
pub fn get_block_manifests(&self, from_height: u32, len: usize) -> Vec<L1BlockRecord> {
let mut blocks = Vec::with_capacity(len);
for i in 0..len {
let block = self.get_block_manifest(from_height + i as u32);
Expand Down

0 comments on commit 7501c9c

Please sign in to comment.