zebra_state/service/finalized_state/disk_db/
tests.rs

1//! Tests and test methods for low-level RocksDB access.
2
3#![allow(dead_code)]
4
5use std::ops::Deref;
6
7use crate::service::finalized_state::disk_db::{DiskDb, DB};
8
9// Enable older test code to automatically access the inner database via Deref coercion.
10impl Deref for DiskDb {
11    type Target = DB;
12
13    fn deref(&self) -> &Self::Target {
14        &self.db
15    }
16}
17
18impl DiskDb {
19    /// Returns a list of column family names in this database.
20    pub fn list_cf(&self) -> Result<Vec<String>, rocksdb::Error> {
21        let opts = DiskDb::options();
22        let path = self.path();
23
24        rocksdb::DB::list_cf(&opts, path)
25    }
26}
27
28/// Check that zs_iter_opts returns an upper bound one greater than provided inclusive end bounds.
29#[test]
30fn zs_iter_opts_increments_key_by_one() {
31    let _init_guard = zebra_test::init();
32
33    // TODO: add an empty key (`()` type or `[]` when serialized) test case
34    let keys: [u32; 14] = [
35        0,
36        1,
37        200,
38        255,
39        256,
40        257,
41        65535,
42        65536,
43        65537,
44        16777215,
45        16777216,
46        16777217,
47        16777218,
48        u32::MAX,
49    ];
50
51    for key in keys {
52        let (_, bytes) = DiskDb::zs_iter_bounds(&..=key.to_be_bytes().to_vec());
53        let mut extra_bytes = bytes.expect("there should be an upper bound");
54        let bytes = extra_bytes.split_off(extra_bytes.len() - 4);
55        let upper_bound = u32::from_be_bytes(bytes.clone().try_into().expect("should be 4 bytes"));
56        let expected_upper_bound = key.wrapping_add(1);
57
58        assert_eq!(
59            expected_upper_bound, upper_bound,
60            "the upper bound should be 1 greater than the original key"
61        );
62
63        if expected_upper_bound == 0 {
64            assert_eq!(
65                extra_bytes,
66                vec![1],
67                "there should be an extra byte with a value of 1"
68            );
69        } else {
70            assert_eq!(extra_bytes.len(), 0, "there should be no extra bytes");
71        }
72    }
73}