Skip to content

Commit

Permalink
misc: reworked broken tests to reflect new design
Browse files Browse the repository at this point in the history
  • Loading branch information
delbonis committed Jan 3, 2025
1 parent ac7b766 commit 4d7f7a7
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 62 deletions.
2 changes: 1 addition & 1 deletion crates/btcio/src/reader/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ mod test {
let (event_tx, _event_rx) = mpsc::channel::<L1Event>(10);
let mut chstate: Chainstate = ArbitraryGenerator::new().generate();
let clstate: ClientState = ArbitraryGenerator::new().generate();
let curr_epoch = chstate.epoch();
let curr_epoch = chstate.cur_epoch();

let ctx = get_reader_ctx(event_tx, chstate.clone(), clstate);
let mut state = get_reader_state(&ctx);
Expand Down
47 changes: 32 additions & 15 deletions crates/chaintsn/src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ fn next_rand_op_pos(rng: &mut SlotRng, num: u32) -> u32 {
#[cfg(test)]
mod tests {
use rand_core::SeedableRng;
use strata_primitives::{buf::Buf32, l1::BitcoinAmount, params::OperatorConfig};
use strata_primitives::{
buf::Buf32,
l1::{BitcoinAmount, L1BlockCommitment},
params::OperatorConfig,
};
use strata_state::{
block::{ExecSegment, L1Segment, L2BlockBody},
bridge_state::OperatorTable,
Expand Down Expand Up @@ -327,7 +331,13 @@ mod tests {

#[test]
fn test_process_l1_view_update_with_deposit_update_tx() {
let mut chs: Chainstate = ArbitraryGenerator::new().generate();
let mut ag = ArbitraryGenerator::new();
let mut chs: Chainstate = ag.generate();

// Setting this to a sane value.
let safe_block = L1BlockCommitment::new(0, ag.generate());
chs.epoch_state_mut().set_safe_l1_block(safe_block);

// get the l1 view state of the chain state
let params = gen_params();
// TODO refactor
Expand All @@ -338,29 +348,30 @@ mod tests {

let epoch_state = chs.epoch_state().clone(); // TODO refactor
let mut state_cache = StateCache::new(chs, epoch_state);
let amt: BitcoinAmount = ArbitraryGenerator::new().generate();
let amt: BitcoinAmount = BitcoinAmount::from_int_btc(10);

let new_payloads_with_deposit_update_tx: Vec<L1HeaderPayload> =
(1..=params.rollup().l1_reorg_safe_depth + 1)
.map(|idx| {
let record = ArbitraryGenerator::new_with_size(1 << 15).generate();
let proof = ArbitraryGenerator::new_with_size(1 << 12).generate();
let tx = ArbitraryGenerator::new_with_size(1 << 8).generate();
let mut ag = ArbitraryGenerator::new_with_size(1 << 16);
let record = ag.generate();
let proof = ag.generate();
let tx = ag.generate();

let mut deposit_update_txs = Vec::new();

let l1tx = if idx == 1 {
if idx == 1 {
let protocol_op = ProtocolOperation::Deposit(DepositInfo {
amt,
outpoint: ArbitraryGenerator::new().generate(),
outpoint: ag.generate(),
address: [0; 20].to_vec(),
});
L1Tx::new(proof, tx, protocol_op)
} else {
ArbitraryGenerator::new_with_size(1 << 15).generate()
let tx = DepositUpdateTx::new(L1Tx::new(proof, tx, protocol_op), idx);
deposit_update_txs.push(tx);
};

let deposit_update_tx = DepositUpdateTx::new(l1tx, idx);
L1HeaderPayload::new(tip_height + idx as u64, record)
.with_deposit_update_txs(vec![deposit_update_tx])
.with_deposit_update_txs(deposit_update_txs)
.build()
})
.collect();
Expand Down Expand Up @@ -395,7 +406,13 @@ mod tests {

#[test]
fn test_process_l1_view_update() {
let mut chs: Chainstate = ArbitraryGenerator::new().generate();
let mut ag = ArbitraryGenerator::new();
let mut chs: Chainstate = ag.generate();

// Setting this to a sane value.
let safe_block = L1BlockCommitment::new(0, ag.generate());
chs.epoch_state_mut().set_safe_l1_block(safe_block);

let params = gen_params();
//let header_record = chs.l1_view();
let old_safe_height = chs.epoch_state().safe_l1_height();
Expand All @@ -419,7 +436,7 @@ mod tests {

assert_eq!(new_payloads.len() as u64, blocks_range_end - 1);

let mut l1_segment = L1Segment::new(new_payloads.clone());
let l1_segment = L1Segment::new(new_payloads.clone());

// Process the L1 view update for matured blocks
let result = process_l1_segment(&mut state_cache, &l1_segment, params.rollup());
Expand Down
76 changes: 34 additions & 42 deletions crates/consensus-logic/src/csm/client_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ mod tests {
let l1_verification_state =
chain.get_verification_state(genesis as u32 + 1, &MAINNET.clone().into());

let horizon_block = L1BlockCommitment::new(horizon, l1_chain[0].block_hash().into());
let genesis_block = genesis::make_genesis_block(&params);
let genesis_blockid = genesis_block.header().get_blockid();

Expand All @@ -552,19 +553,14 @@ mod tests {
description: "At horizon block",
events: &[TestEvent {
event: SyncEvent::L1Block(horizon, l1_chain[0].block_hash().into()),
expected_writes: &[ClientStateWrite::AcceptL1Block(
l1_chain[0].block_hash().into(),
)],
expected_writes: &[ClientStateWrite::SetL1Tip(horizon_block)],
expected_actions: &[],
}],
state_assertions: Box::new({
let l1_chain = l1_chain.clone();
move |state| {
assert!(!state.is_chain_active());
assert_eq!(
state.most_recent_l1_block(),
Some(&l1_chain[0].block_hash().into())
);
assert_eq!(state.tip_l1_blkid(), &l1_chain[0].block_hash().into());
assert_eq!(state.next_exp_l1_block(), horizon + 1);
}
}),
Expand All @@ -573,19 +569,17 @@ mod tests {
description: "At horizon block + 1",
events: &[TestEvent {
event: SyncEvent::L1Block(horizon + 1, l1_chain[1].block_hash().into()),
expected_writes: &[ClientStateWrite::AcceptL1Block(
expected_writes: &[ClientStateWrite::SetL1Tip(L1BlockCommitment::new(
horizon + 1,
l1_chain[1].block_hash().into(),
)],
))],
expected_actions: &[],
}],
state_assertions: Box::new({
let l1_chain = l1_chain.clone();
move |state| {
assert!(!state.is_chain_active());
assert_eq!(
state.most_recent_l1_block(),
Some(&l1_chain[1].block_hash().into())
);
assert_eq!(state.tip_l1_blkid(), &l1_chain[1].block_hash().into());
// Because values for horizon is 40318, genesis is 40320
assert_eq!(state.next_exp_l1_block(), genesis);
}
Expand All @@ -598,9 +592,10 @@ mod tests {
genesis,
l1_chain[(genesis - horizon) as usize].block_hash().into(),
),
expected_writes: &[ClientStateWrite::AcceptL1Block(
expected_writes: &[ClientStateWrite::SetL1Tip(L1BlockCommitment::new(
genesis,
l1_chain[(genesis - horizon) as usize].block_hash().into(),
)],
))],
expected_actions: &[],
}],
state_assertions: Box::new(move |state| {
Expand All @@ -617,11 +612,12 @@ mod tests {
.block_hash()
.into(),
),
expected_writes: &[ClientStateWrite::AcceptL1Block(
expected_writes: &[ClientStateWrite::SetL1Tip(L1BlockCommitment::new(
genesis + 1,
l1_chain[(genesis + 1 - horizon) as usize]
.block_hash()
.into(),
)],
))],
expected_actions: &[],
}],
state_assertions: Box::new({
Expand All @@ -630,18 +626,12 @@ mod tests {
move |state| {
assert!(!state.is_chain_active());
assert_eq!(
state.most_recent_l1_block(),
Some(
&l1_chain[(genesis + 1 - horizon) as usize]
.block_hash()
.into()
)
state.tip_l1_blkid(),
&l1_chain[(genesis + 1 - horizon) as usize]
.block_hash()
.into()
);
assert_eq!(state.next_exp_l1_block(), genesis + 2);
assert_eq!(
state.l1_view().local_unaccepted_blocks(),
&blkids[0..(genesis + 1 - horizon + 1) as usize]
);
}
}),
},
Expand All @@ -654,11 +644,12 @@ mod tests {
.block_hash()
.into(),
),
expected_writes: &[ClientStateWrite::AcceptL1Block(
expected_writes: &[ClientStateWrite::SetL1Tip(L1BlockCommitment::new(
genesis + 2,
l1_chain[(genesis + 2 - horizon) as usize]
.block_hash()
.into(),
)],
))],
expected_actions: &[],
}],
state_assertions: Box::new({
Expand All @@ -667,18 +658,12 @@ mod tests {
move |state| {
assert!(!state.is_chain_active());
assert_eq!(
state.most_recent_l1_block(),
Some(
&l1_chain[(genesis + 2 - horizon) as usize]
.block_hash()
.into()
)
state.tip_l1_blkid(),
&l1_chain[(genesis + 2 - horizon) as usize]
.block_hash()
.into()
);
assert_eq!(state.next_exp_l1_block(), genesis + 3);
assert_eq!(
state.l1_view().local_unaccepted_blocks(),
&blkids[0..(genesis + 2 - horizon + 1) as usize]
);
}
}),
},
Expand Down Expand Up @@ -710,11 +695,12 @@ mod tests {
.block_hash()
.into(),
),
expected_writes: &[ClientStateWrite::AcceptL1Block(
expected_writes: &[ClientStateWrite::SetL1Tip(L1BlockCommitment::new(
genesis + 3,
l1_chain[(genesis + 3 - horizon) as usize]
.block_hash()
.into(),
)],
))],
expected_actions: &[],
},
],
Expand All @@ -730,7 +716,13 @@ mod tests {
description: "Rollback to genesis height",
events: &[TestEvent {
event: SyncEvent::L1Revert(genesis),
expected_writes: &[ClientStateWrite::RollbackL1BlocksTo(genesis)],
expected_writes: &[
ClientStateWrite::RollbackL1BlocksTo(genesis),
ClientStateWrite::SetL1Tip(L1BlockCommitment::new(
genesis,
l1_chain[(genesis - horizon) as usize].block_hash().into(),
)),
],
expected_actions: &[],
}],
state_assertions: Box::new({ move |state| {} }),
Expand Down
2 changes: 1 addition & 1 deletion crates/state/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ mod tests {
assert!(!validate_block_segments(&arb_exec_block));

// mutate the l2Block's body to create a new block with arbitrary l1 segment
let blk_body = L2BlockBody::new(arb_l1_segment, block.body().exec_segment().clone());
let blk_body = L2BlockBody::new(Some(arb_l1_segment), block.body().exec_segment().clone());
let arb_l1_block = L2Block::new(block.header().clone(), blk_body);
assert!(!validate_block_segments(&arb_l1_block));
}
Expand Down
15 changes: 15 additions & 0 deletions crates/state/src/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ impl Chainstate {
&self.epoch_state
}

/// Returns a mut ref to the epoch state.
///
/// Beacuse of dumb testing reasons we have to expose this. Should ONLY be

Check failure on line 92 in crates/state/src/chain_state.rs

View workflow job for this annotation

GitHub Actions / Check code spellings

Beacuse ==> Because
/// used in tests.
pub fn epoch_state_mut(&mut self) -> &mut EpochState {
&mut self.epoch_state
}

pub fn pending_withdrawals(&self) -> &[WithdrawalIntent] {
self.pending_withdraws.entries()
}

Check warning on line 100 in crates/state/src/chain_state.rs

View check run for this annotation

Codecov / codecov/patch

crates/state/src/chain_state.rs#L98-L100

Added lines #L98 - L100 were not covered by tests
Expand All @@ -103,6 +111,8 @@ impl Chainstate {
&self.epoch_state.deposits_table
}

/// Beacuse of dumb testing reasons we have to expose this. Should ONLY be

Check failure on line 114 in crates/state/src/chain_state.rs

View workflow job for this annotation

GitHub Actions / Check code spellings

Beacuse ==> Because
/// used in tests.
pub fn deposits_table_mut(&mut self) -> &mut DepositsTable {
&mut self.epoch_state.deposits_table
}
Expand Down Expand Up @@ -209,6 +219,11 @@ impl EpochState {
self.finalized_epoch.last_blkid()
}

Check warning on line 220 in crates/state/src/chain_state.rs

View check run for this annotation

Codecov / codecov/patch

crates/state/src/chain_state.rs#L218-L220

Added lines #L218 - L220 were not covered by tests

/// Only exposed because of testing reasons.
pub fn set_safe_l1_block(&mut self, commitment: L1BlockCommitment) {
self.safe_l1_block = commitment;
}

pub fn safe_l1_blkid(&self) -> &L1BlockId {
self.safe_l1_block.blkid()
}

Check warning on line 229 in crates/state/src/chain_state.rs

View check run for this annotation

Codecov / codecov/patch

crates/state/src/chain_state.rs#L227-L229

Added lines #L227 - L229 were not covered by tests
Expand Down
4 changes: 4 additions & 0 deletions crates/state/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ impl ClientState {
&mut self.local_l1_view
}

pub fn tip_l1_blkid(&self) -> &L1BlockId {
self.l1_view().tip_l1_blkid()
}

/// Overwrites the sync state.
pub fn set_sync_state(&mut self, ss: SyncState) {
self.sync_state = Some(ss);
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 @@ -62,7 +62,7 @@ pub enum ClientStateWrite {
/// Accept an L2 block and its height and update tip state.
AcceptL2Block(L2BlockId, u64),

/// Rolls back L1 blocks to this block height.
/// Rolls back checkpoints to whatever was present at this block height.
RollbackL1BlocksTo(u64),

/// Sets the L1 tip, performing no validation.
Expand Down
3 changes: 1 addition & 2 deletions crates/test-utils/src/evm_ee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ impl L2Segment {
let el_proof_in = evm_segment.get_input(&height);
let el_proof_out = evm_segment.get_output(&height);
let evm_ee_segment = generate_exec_update(el_proof_out);
let l1_segment = L1Segment::new_empty();
let body = L2BlockBody::new(l1_segment, evm_ee_segment);
let body = L2BlockBody::new(None, evm_ee_segment);

let slot = prev_block.header().blockidx() + 1;
let ts = el_proof_in.timestamp;
Expand Down

0 comments on commit 4d7f7a7

Please sign in to comment.