diff --git a/crates/btcio/src/reader/query.rs b/crates/btcio/src/reader/query.rs index 4f766b82b..0767e872b 100644 --- a/crates/btcio/src/reader/query.rs +++ b/crates/btcio/src/reader/query.rs @@ -477,7 +477,7 @@ mod test { let (event_tx, _event_rx) = mpsc::channel::(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); diff --git a/crates/chaintsn/src/epoch.rs b/crates/chaintsn/src/epoch.rs index 9a79ae4b3..ad0969c6e 100644 --- a/crates/chaintsn/src/epoch.rs +++ b/crates/chaintsn/src/epoch.rs @@ -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, @@ -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 @@ -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 = (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(); @@ -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(); @@ -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()); diff --git a/crates/consensus-logic/src/csm/client_transition.rs b/crates/consensus-logic/src/csm/client_transition.rs index 8e80ecdeb..43acccd56 100644 --- a/crates/consensus-logic/src/csm/client_transition.rs +++ b/crates/consensus-logic/src/csm/client_transition.rs @@ -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(¶ms); let genesis_blockid = genesis_block.header().get_blockid(); @@ -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); } }), @@ -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); } @@ -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| { @@ -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({ @@ -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] - ); } }), }, @@ -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({ @@ -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] - ); } }), }, @@ -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: &[], }, ], @@ -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| {} }), diff --git a/crates/state/src/block.rs b/crates/state/src/block.rs index a33b8799a..8510e43b0 100644 --- a/crates/state/src/block.rs +++ b/crates/state/src/block.rs @@ -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)); } diff --git a/crates/state/src/chain_state.rs b/crates/state/src/chain_state.rs index 6592e56c7..a8b49a2d7 100644 --- a/crates/state/src/chain_state.rs +++ b/crates/state/src/chain_state.rs @@ -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 + /// 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() } @@ -103,6 +111,8 @@ impl Chainstate { &self.epoch_state.deposits_table } + /// Beacuse of dumb testing reasons we have to expose this. Should ONLY be + /// used in tests. pub fn deposits_table_mut(&mut self) -> &mut DepositsTable { &mut self.epoch_state.deposits_table } @@ -209,6 +219,11 @@ impl EpochState { self.finalized_epoch.last_blkid() } + /// 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() } diff --git a/crates/state/src/client_state.rs b/crates/state/src/client_state.rs index 898fe9de5..ce160d603 100644 --- a/crates/state/src/client_state.rs +++ b/crates/state/src/client_state.rs @@ -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); diff --git a/crates/state/src/operation.rs b/crates/state/src/operation.rs index 9f4caaed5..20e1ed42e 100644 --- a/crates/state/src/operation.rs +++ b/crates/state/src/operation.rs @@ -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. diff --git a/crates/test-utils/src/evm_ee.rs b/crates/test-utils/src/evm_ee.rs index d3a22acf8..e2216194a 100644 --- a/crates/test-utils/src/evm_ee.rs +++ b/crates/test-utils/src/evm_ee.rs @@ -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;