Skip to main content

zebra_state/service/finalized_state/disk_db/
tests.rs

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