Skip to content

Commit

Permalink
Test transaction_in_validity_window
Browse files Browse the repository at this point in the history
Add an unittest to exercise the transaction_in_validity_window functionality in the light history store
Fixed typo in range inclusion when pruning the validity store
  • Loading branch information
viquezclaudio committed May 15, 2024
1 parent 0f41696 commit f7eec07
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions blockchain/src/history/light_history_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,57 @@ mod tests {
assert_eq!(len_3, 15);
}

#[test]
fn transaction_in_validity_window_works() {
// Initialize History Store.
let env = VolatileDatabase::new(20).unwrap();
let history_store = LightHistoryStore::new(env.clone(), NetworkId::UnitAlbatross);

// Create historic transactions.
let ext_0 = create_transaction(Policy::genesis_block_number() + 1, 0);
let ext_1 = create_transaction(Policy::genesis_block_number() + 1, 1);

let hist_txs = vec![ext_0.clone(), ext_1.clone()];

// Add historic transactions to History Store.
let mut txn = env.write_transaction();
history_store.add_to_history(&mut txn, Policy::genesis_block_number() + 1, &hist_txs);

// Those transactions should be part of the valitidy window
assert_eq!(
history_store.tx_in_validity_window(&ext_0.tx_hash(), 0, Some(&txn)),
true
);

assert_eq!(
history_store.tx_in_validity_window(&ext_1.tx_hash(), 0, Some(&txn)),
true
);

// Now keep pushing transactions to the history store until we are past the transaction validity window
let validity_window_blocks = Policy::transaction_validity_window_blocks();

for bn in 2..validity_window_blocks + 10 {
let historic_txn = create_transaction(Policy::genesis_block_number() + bn, bn as u64);
history_store.add_to_history(
&mut txn,
Policy::genesis_block_number() + bn,
&vec![historic_txn],
);
}

// Since we are past the txn in validity window, the first two transaction should no longer be in it
assert_eq!(
history_store.tx_in_validity_window(&ext_0.tx_hash(), 0, Some(&txn)),
false
);

assert_eq!(
history_store.tx_in_validity_window(&ext_1.tx_hash(), 0, Some(&txn)),
false
);
}

#[test]
fn total_len_at_epoch() {
// Initialize History Store.
Expand Down
2 changes: 1 addition & 1 deletion blockchain/src/history/validity_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl ValidityStore {
// We need to prune the validity store
let count = num_blocks - Policy::transaction_validity_window_blocks();

for bn in [first_bn, first_bn + count] {
for bn in first_bn..first_bn + count {
self.delete_block_transactions(db_txn, bn);
}
}
Expand Down

0 comments on commit f7eec07

Please sign in to comment.