Skip to content

Commit 1c84e78

Browse files
authored
Fix stack_flush_test (#333)
1 parent fcc87f4 commit 1c84e78

File tree

8 files changed

+432
-351
lines changed

8 files changed

+432
-351
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utxo/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ thiserror = "1.0"
1717

1818
[dev-dependencies]
1919
crypto = { path = '../crypto' }
20-
2120
itertools = "0.10"
21+
rstest = "0.15"
22+
test-utils = {path = '../test-utils'}

utxo/src/undo.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,17 @@ impl BlockUndo {
7575
pub mod test {
7676
use super::*;
7777
use crate::test_helper::create_utxo;
78-
use crypto::random::{make_pseudo_rng, Rng};
79-
80-
#[test]
81-
fn tx_undo_test() {
82-
let (utxo0, _) = create_utxo(0);
83-
let (utxo1, _) = create_utxo(1);
78+
use crypto::random::Rng;
79+
use rstest::rstest;
80+
use test_utils::random::{make_seedable_rng, Seed};
81+
82+
#[rstest]
83+
#[trace]
84+
#[case(Seed::from_entropy())]
85+
fn tx_undo_test(#[case] seed: Seed) {
86+
let mut rng = make_seedable_rng(seed);
87+
let (utxo0, _) = create_utxo(&mut rng, 0);
88+
let (utxo1, _) = create_utxo(&mut rng, 1);
8489
let mut tx_undo = TxUndo::new(vec![utxo0.clone()]);
8590

8691
// check push
@@ -102,16 +107,19 @@ pub mod test {
102107
}
103108
}
104109

105-
#[test]
106-
fn block_undo_test() {
110+
#[rstest]
111+
#[trace]
112+
#[case(Seed::from_entropy())]
113+
fn block_undo_test(#[case] seed: Seed) {
114+
let mut rng = make_seedable_rng(seed);
107115
let expected_height = BlockHeight::new(5);
108-
let (utxo0, _) = create_utxo(0);
109-
let (utxo1, _) = create_utxo(1);
116+
let (utxo0, _) = create_utxo(&mut rng, 0);
117+
let (utxo1, _) = create_utxo(&mut rng, 1);
110118
let tx_undo0 = TxUndo::new(vec![utxo0, utxo1]);
111119

112-
let (utxo2, _) = create_utxo(2);
113-
let (utxo3, _) = create_utxo(3);
114-
let (utxo4, _) = create_utxo(4);
120+
let (utxo2, _) = create_utxo(&mut rng, 2);
121+
let (utxo3, _) = create_utxo(&mut rng, 3);
122+
let (utxo4, _) = create_utxo(&mut rng, 4);
115123
let tx_undo1 = TxUndo::new(vec![utxo2, utxo3, utxo4]);
116124

117125
let blockundo = BlockUndo::new(vec![tx_undo0.clone(), tx_undo1.clone()], expected_height);

utxo/src/utxo_impl/mod.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,9 @@ impl<'a> UtxosView for UtxosCache<'a> {
448448
impl<'a> FlushableUtxoView for UtxosCache<'a> {
449449
fn batch_write(&mut self, utxo_entries: ConsumedUtxoCache) -> Result<(), Error> {
450450
for (key, entry) in utxo_entries.container {
451-
let parent_entry = self.utxos.get(&key);
452-
453451
// Ignore non-dirty entries (optimization).
454452
if entry.is_dirty {
453+
let parent_entry = self.utxos.get(&key);
455454
match parent_entry {
456455
None => {
457456
// The parent cache does not have an entry, while the child cache does.
@@ -516,35 +515,44 @@ mod simulation;
516515
#[cfg(test)]
517516
mod unit_test {
518517
use common::primitives::H256;
518+
use rstest::rstest;
519+
use test_utils::random::{make_seedable_rng, Seed};
519520

520521
use crate::test_helper::{insert_single_entry, Presence, DIRTY, FRESH};
521522
use crate::UtxosCache;
522523

523-
#[test]
524-
fn test_uncache() {
524+
#[rstest]
525+
#[trace]
526+
#[case(Seed::from_entropy())]
527+
fn test_uncache(#[case] seed: Seed) {
528+
let mut rng = make_seedable_rng(seed);
525529
let mut cache = UtxosCache::new_for_test(H256::random().into());
526530

527531
// when the entry is not dirty and not fresh
528-
let (utxo, outp) = insert_single_entry(&mut cache, &Presence::Present, Some(0), None);
532+
let (utxo, outp) =
533+
insert_single_entry(&mut rng, &mut cache, &Presence::Present, Some(0), None);
529534
let res = cache.uncache(&outp).expect("should return an entry");
530535
assert_eq!(res.utxo(), Some(utxo));
531536
assert!(!cache.has_utxo_in_cache(&outp));
532537

533538
// when the outpoint does not exist.
534-
let (_, outp) = insert_single_entry(&mut cache, &Presence::Absent, None, None);
539+
let (_, outp) = insert_single_entry(&mut rng, &mut cache, &Presence::Absent, None, None);
535540
assert_eq!(cache.uncache(&outp), None);
536541
assert!(!cache.has_utxo_in_cache(&outp));
537542

538543
// when the entry is fresh, entry cannot be removed.
539-
let (_, outp) = insert_single_entry(&mut cache, &Presence::Present, Some(FRESH), None);
544+
let (_, outp) =
545+
insert_single_entry(&mut rng, &mut cache, &Presence::Present, Some(FRESH), None);
540546
assert_eq!(cache.uncache(&outp), None);
541547

542548
// when the entry is dirty, entry cannot be removed.
543-
let (_, outp) = insert_single_entry(&mut cache, &Presence::Present, Some(DIRTY), None);
549+
let (_, outp) =
550+
insert_single_entry(&mut rng, &mut cache, &Presence::Present, Some(DIRTY), None);
544551
assert_eq!(cache.uncache(&outp), None);
545552

546553
// when the entry is both fresh and dirty, entry cannot be removed.
547-
let (_, outp) = insert_single_entry(&mut cache, &Presence::Present, Some(FRESH), None);
554+
let (_, outp) =
555+
insert_single_entry(&mut rng, &mut cache, &Presence::Present, Some(FRESH), None);
548556
assert_eq!(cache.uncache(&outp), None);
549557
}
550558
}

0 commit comments

Comments
 (0)