zebra_scan/storage/db/
tests.rs

1//! General scanner database tests.
2
3use std::{collections::BTreeMap, sync::Arc};
4
5use zebra_chain::{
6    block::{Block, Height},
7    parameters::Network::{self},
8    serialization::ZcashDeserializeInto,
9    transaction,
10};
11use zebra_state::{SaplingScannedResult, TransactionIndex};
12
13use crate::{
14    storage::{Storage, INSERT_CONTROL_INTERVAL},
15    tests::{FAKE_SAPLING_VIEWING_KEY, ZECPAGES_SAPLING_VIEWING_KEY},
16    Config,
17};
18
19#[cfg(test)]
20mod snapshot;
21
22#[cfg(test)]
23mod vectors;
24
25/// Returns an empty `Storage` suitable for testing.
26pub fn new_test_storage(network: &Network) -> Storage {
27    Storage::new(&Config::ephemeral(), network, false)
28}
29
30/// Add fake keys to `storage` for testing purposes.
31pub fn add_fake_keys(storage: &mut Storage) {
32    // Snapshot a birthday that is automatically set to activation height
33    storage.add_sapling_key(&ZECPAGES_SAPLING_VIEWING_KEY.to_string(), None);
34    // Snapshot a birthday above activation height
35    storage.add_sapling_key(&FAKE_SAPLING_VIEWING_KEY.to_string(), Height(1_000_000));
36}
37
38/// Add fake results to `storage` for testing purposes.
39///
40/// If `add_progress_marker` is `true`, adds a progress marker.
41/// If it is `false`, adds the transaction hashes from `height`.
42pub fn add_fake_results(
43    storage: &mut Storage,
44    network: &Network,
45    height: Height,
46    add_progress_marker: bool,
47) {
48    let blocks = network.blockchain_map();
49
50    let block: Arc<Block> = blocks
51        .get(&height.0)
52        .expect("block height has test data")
53        .zcash_deserialize_into()
54        .expect("test data deserializes");
55
56    if add_progress_marker {
57        // Fake a progress marker.
58        let next_progress_height = height.0.next_multiple_of(INSERT_CONTROL_INTERVAL);
59        storage.add_sapling_results(
60            &FAKE_SAPLING_VIEWING_KEY.to_string(),
61            Height(next_progress_height),
62            BTreeMap::new(),
63        );
64    } else {
65        // Fake results from the block at `height`.
66        storage.add_sapling_results(
67            &ZECPAGES_SAPLING_VIEWING_KEY.to_string(),
68            height,
69            block
70                .transactions
71                .iter()
72                .enumerate()
73                .map(|(index, tx)| (TransactionIndex::from_usize(index), tx.hash().into()))
74                .collect(),
75        );
76    }
77}
78
79/// Accepts an iterator of [`TransactionIndex`]es and returns a `BTreeMap` with empty results
80pub fn fake_sapling_results<T: IntoIterator<Item = TransactionIndex>>(
81    transaction_indexes: T,
82) -> BTreeMap<TransactionIndex, SaplingScannedResult> {
83    let mut fake_sapling_results = BTreeMap::new();
84
85    for transaction_index in transaction_indexes {
86        fake_sapling_results.insert(
87            transaction_index,
88            SaplingScannedResult::from(transaction::Hash::from([0; 32])),
89        );
90    }
91
92    fake_sapling_results
93}