zebra_state/service/finalized_state/disk_db/
tests.rs1#![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
10impl Deref for DiskDb {
12 type Target = DB;
13
14 fn deref(&self) -> &Self::Target {
15 &self.db
16 }
17}
18
19impl DiskDb {
20 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#[test]
31fn zs_iter_opts_increments_key_by_one() {
32 let _init_guard = zebra_test::init();
33
34 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}
75
76#[test]
79fn lock_holder_matches_rocksdb_lock_messages() {
80 use super::LockHolder;
81
82 let _init_guard = zebra_test::init();
83
84 let cases = [
85 (
86 "IO error: While lock file: /state/LOCK: Resource temporarily unavailable",
87 Some(LockHolder::OtherProcess),
88 ),
89 (
90 "IO error: While lock file: /state/LOCK: Permission denied",
91 Some(LockHolder::OtherProcess),
92 ),
93 (
94 "IO error: Failed to create lock file: C:\\state\\LOCK: The process cannot access \
95 the file because it is being used by another process.\r\n",
96 Some(LockHolder::OtherProcess),
97 ),
98 (
99 "IO error: lock hold by current process, acquire time 1761209419 acquiring thread \
100 139890000000000: /state/LOCK: No locks available",
101 Some(LockHolder::ThisProcess),
102 ),
103 (
104 "IO error: While lock file: /state/LOCK: No locks available",
105 None,
106 ),
107 (
108 "IO error: Failed to create lock file: C:\\state\\LOCK: Access is denied.\r\n",
109 None,
110 ),
111 (
112 "IO error: while open a file for lock: /state/LOCK: Is a directory",
113 None,
114 ),
115 (
116 "IO error: No space left on device: While appending to file: /state/000001.log",
117 None,
118 ),
119 ];
120
121 for (message, expected) in cases {
122 assert_eq!(DiskDb::lock_holder(message), expected, "{message}");
123 }
124}
125
126#[test]
128fn open_errors_distinguish_lock_holders_from_io_errors() {
129 use super::LockHolder;
130
131 let _init_guard = zebra_test::init();
132
133 let dir = tempfile::tempdir().expect("temporary directory is writable");
134 let mut opts = rocksdb::Options::default();
135 opts.create_if_missing(true);
136
137 let db_path = dir.path().join("db");
138 let _db = DB::open(&opts, &db_path).expect("new database opens in an empty directory");
139 let lock_error = DB::open(&opts, &db_path)
140 .expect_err("second open fails because the first instance holds the lock");
141 let holder = DiskDb::lock_holder(lock_error.as_ref());
142
143 if cfg!(windows) {
144 assert!(
148 lock_error.as_ref().contains("Failed to create lock file"),
149 "{lock_error}"
150 );
151 assert!(
152 matches!(holder, Some(LockHolder::OtherProcess) | None),
153 "{lock_error}"
154 );
155 } else {
156 assert_eq!(holder, Some(LockHolder::ThisProcess), "{lock_error}");
157 }
158
159 let blocked_path = dir.path().join("blocked");
160 std::fs::create_dir_all(blocked_path.join("LOCK")).expect("temporary directory is writable");
161 let io_error = DB::open(&opts, &blocked_path)
162 .expect_err("open fails because the lock file path is a directory");
163 assert_eq!(io_error.kind(), rocksdb::ErrorKind::IOError, "{io_error}");
164 assert_eq!(DiskDb::lock_holder(io_error.as_ref()), None, "{io_error}");
165}
166
167#[test]
170fn read_only_secondary_removes_its_directory_on_drop() {
171 use zebra_chain::parameters::Network;
172
173 use crate::{
174 constants::{state_database_format_version_in_code, STATE_DATABASE_KIND},
175 Config,
176 };
177
178 let _init_guard = zebra_test::init();
179
180 let cache_dir = tempfile::tempdir().expect("temporary directory is writable");
181 let config = Config {
182 cache_dir: cache_dir.path().to_path_buf(),
183 ephemeral: false,
184 ..Config::default()
185 };
186 let network = Network::Mainnet;
187 let version = state_database_format_version_in_code();
188 let open = |read_only| {
189 DiskDb::new(
190 &config,
191 STATE_DATABASE_KIND,
192 &version,
193 &network,
194 [],
195 read_only,
196 )
197 .expect("database opens in a fresh cache directory")
198 };
199
200 let primary = open(false);
201 let secondary = open(true);
202 let secondary_path = secondary
203 .secondary_dir
204 .as_ref()
205 .expect("read-only instances have a secondary directory")
206 .path()
207 .to_path_buf();
208 assert!(secondary_path.exists());
209
210 drop(secondary.clone());
211 assert!(
212 secondary_path.exists(),
213 "dropping a clone must keep the secondary directory"
214 );
215
216 drop(secondary);
217 assert!(
218 !secondary_path.exists(),
219 "secondary directory {secondary_path:?} was not removed"
220 );
221 assert!(
222 primary.path().exists(),
223 "dropping a secondary must keep the primary database"
224 );
225}