1//! Tests and test methods for low-level RocksDB access.
23#![allow(dead_code)]
45use std::ops::Deref;
67use crate::service::finalized_state::disk_db::{DiskDb, DB};
89// Enable older test code to automatically access the inner database via Deref coercion.
10impl Deref for DiskDb {
11type Target = DB;
1213fn deref(&self) -> &Self::Target {
14&self.db
15 }
16}
1718impl DiskDb {
19/// Returns a list of column family names in this database.
20pub fn list_cf(&self) -> Result<Vec<String>, rocksdb::Error> {
21let opts = DiskDb::options();
22let path = self.path();
2324 rocksdb::DB::list_cf(&opts, path)
25 }
26}
2728/// 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() {
31let _init_guard = zebra_test::init();
3233// TODO: add an empty key (`()` type or `[]` when serialized) test case
34let keys: [u32; 14] = [
350,
361,
37200,
38255,
39256,
40257,
4165535,
4265536,
4365537,
4416777215,
4516777216,
4616777217,
4716777218,
48 u32::MAX,
49 ];
5051for key in keys {
52let (_, bytes) = DiskDb::zs_iter_bounds(&..=key.to_be_bytes().to_vec());
53let mut extra_bytes = bytes.expect("there should be an upper bound");
54let bytes = extra_bytes.split_off(extra_bytes.len() - 4);
55let upper_bound = u32::from_be_bytes(bytes.clone().try_into().expect("should be 4 bytes"));
56let expected_upper_bound = key.wrapping_add(1);
5758assert_eq!(
59 expected_upper_bound, upper_bound,
60"the upper bound should be 1 greater than the original key"
61);
6263if expected_upper_bound == 0 {
64assert_eq!(
65 extra_bytes,
66vec![1],
67"there should be an extra byte with a value of 1"
68);
69 } else {
70assert_eq!(extra_bytes.len(), 0, "there should be no extra bytes");
71 }
72 }
73}