rings_node/native/
stores.rs1use std::path::Path;
20
21use rings_core::dht::EntryStorage;
22use rings_core::dht::RemovalStorage;
23use rings_core::message::ReplayStorage;
24use rings_core::storage::file::FileStorage;
25use rings_core::storage::RecordAuthority;
26
27pub use crate::consts::DHT_REMOVAL_STORAGE_CAPACITY;
28pub use crate::consts::ONION_ENTRY_GUARD_STORAGE_CAPACITY;
29pub use crate::consts::RELAY_INBOX_STORAGE_CAPACITY;
30pub use crate::consts::TRANSACTION_REPLAY_STORAGE_CAPACITY;
31use crate::error::Error;
32use crate::error::Result;
33use crate::measure::EvidenceStorage;
34use crate::measure::MeasureStorage;
35use crate::native::config::StorageConfig;
36use crate::native::config::DEFAULT_STORAGE_CAPACITY;
37use crate::onion::OnionEntryGuardStorage;
38use crate::release::StoresReleased;
39pub const DHT_REMOVAL_STORAGE_DIR: &str = "dht-removals";
41pub const ONION_ENTRY_GUARD_STORAGE_DIR: &str = "onion-entry-guards";
43pub const TRANSACTION_REPLAY_STORAGE_DIR: &str = "transaction-replay";
45pub const RELAY_INBOX_STORAGE_DIR: &str = "relay-inbox";
47pub const PROVISIONAL_EVIDENCE_STORAGE_DIR: &str = "provisional-evidence";
49pub const DATA_STORAGE_DIR: &str = "data";
51pub const MEASURE_STORAGE_DIR: &str = "measure";
53
54pub fn sibling_storage_path(storage_path: &str, dir: &str) -> String {
57 Path::new(storage_path)
58 .parent()
59 .filter(|path| !path.as_os_str().is_empty())
60 .unwrap_or_else(|| Path::new("."))
61 .join(dir)
62 .to_string_lossy()
63 .to_string()
64}
65
66pub struct NodeStores {
68 pub data: EntryStorage,
70 pub relay: EntryStorage,
72 pub removals: RemovalStorage,
74 pub measure: MeasureStorage,
76 pub evidence: EvidenceStorage,
78 pub guards: OnionEntryGuardStorage,
80 pub replay: ReplayStorage,
82 pub released: StoresReleased,
87}
88
89async fn open_file(path: String, capacity: u32, authority: RecordAuthority) -> Result<FileStorage> {
91 FileStorage::new_with_cap_path_and_authority(capacity, path, authority)
92 .await
93 .map_err(Error::Storage)
94}
95
96impl NodeStores {
97 pub async fn open(data: &StorageConfig, measure: &StorageConfig) -> Result<Self> {
100 let beside_data = |dir| sibling_storage_path(&data.path, dir);
101 let data_store = open_file(
102 data.path.clone(),
103 data.capacity,
104 RecordAuthority::Disposable,
105 )
106 .await?;
107 let relay = open_file(
108 beside_data(RELAY_INBOX_STORAGE_DIR),
109 RELAY_INBOX_STORAGE_CAPACITY,
110 RecordAuthority::Retained,
111 )
112 .await?;
113 let removals = open_file(
114 beside_data(DHT_REMOVAL_STORAGE_DIR),
115 DHT_REMOVAL_STORAGE_CAPACITY,
116 RecordAuthority::Retained,
117 )
118 .await?;
119 let measure_store = open_file(
120 measure.path.clone(),
121 measure.capacity,
122 RecordAuthority::Disposable,
123 )
124 .await?;
125 let evidence = open_file(
126 sibling_storage_path(&measure.path, PROVISIONAL_EVIDENCE_STORAGE_DIR),
127 measure.capacity,
128 RecordAuthority::Disposable,
129 )
130 .await?;
131 let guards = open_file(
132 beside_data(ONION_ENTRY_GUARD_STORAGE_DIR),
133 ONION_ENTRY_GUARD_STORAGE_CAPACITY,
134 RecordAuthority::DurableCache,
135 )
136 .await?;
137 let replay = open_file(
138 beside_data(TRANSACTION_REPLAY_STORAGE_DIR),
139 TRANSACTION_REPLAY_STORAGE_CAPACITY,
140 RecordAuthority::Authoritative,
141 )
142 .await?;
143 let released = StoresReleased::of(
145 [
146 &data_store,
147 &relay,
148 &removals,
149 &measure_store,
150 &evidence,
151 &guards,
152 &replay,
153 ]
154 .map(FileStorage::released),
155 );
156 Ok(Self {
157 data: Box::new(data_store),
158 relay: Box::new(relay),
159 removals: Box::new(removals),
160 measure: Box::new(measure_store),
161 evidence: Box::new(evidence),
162 guards: Box::new(guards),
163 replay: Box::new(replay),
164 released,
165 })
166 }
167
168 pub async fn open_under(root: &Path) -> Result<Self> {
172 let store = |dir: &str| root.join(dir).to_string_lossy().to_string();
173 Self::open(
174 &StorageConfig::new(&store(DATA_STORAGE_DIR), DEFAULT_STORAGE_CAPACITY),
175 &StorageConfig::new(&store(MEASURE_STORAGE_DIR), DEFAULT_STORAGE_CAPACITY),
176 )
177 .await
178 }
179}
180
181#[cfg(test)]
182mod tests {
183 use super::*;
184
185 #[test]
188 fn test_sibling_stores_sit_beside_their_store() {
189 for dir in [
190 ONION_ENTRY_GUARD_STORAGE_DIR,
191 TRANSACTION_REPLAY_STORAGE_DIR,
192 RELAY_INBOX_STORAGE_DIR,
193 DHT_REMOVAL_STORAGE_DIR,
194 PROVISIONAL_EVIDENCE_STORAGE_DIR,
195 ] {
196 assert_eq!(
197 sibling_storage_path(".rings/data", dir),
198 format!(".rings/{dir}")
199 );
200 assert_eq!(
201 sibling_storage_path("/tmp/rings/data", dir),
202 format!("/tmp/rings/{dir}")
203 );
204 assert_eq!(sibling_storage_path("data", dir), format!("./{dir}"));
205 }
206 }
207
208 #[test]
211 fn test_transaction_replay_storage_holds_a_full_store(
212 ) -> std::result::Result<(), std::num::TryFromIntError> {
213 let capacity = usize::try_from(TRANSACTION_REPLAY_STORAGE_CAPACITY)?;
214 assert!(rings_core::message::TRANSACTION_REPLAY_STORE_MAX_BYTES <= capacity);
215 Ok(())
216 }
217
218 #[tokio::test]
220 async fn test_open_under_lays_every_store_out_under_the_root() -> Result<()> {
221 let root = std::env::temp_dir().join(format!("rings-node-stores-{}", uuid::Uuid::new_v4()));
222 let stores = NodeStores::open_under(&root).await;
223 let mut dirs = std::fs::read_dir(&root)
224 .map(|entries| {
225 entries
226 .flatten()
227 .map(|entry| entry.file_name().to_string_lossy().to_string())
228 .collect::<Vec<_>>()
229 })
230 .unwrap_or_default();
231 dirs.sort();
232 let _ = std::fs::remove_dir_all(&root);
233 stores?;
234 assert_eq!(dirs, [
235 DATA_STORAGE_DIR,
236 DHT_REMOVAL_STORAGE_DIR,
237 MEASURE_STORAGE_DIR,
238 ONION_ENTRY_GUARD_STORAGE_DIR,
239 PROVISIONAL_EVIDENCE_STORAGE_DIR,
240 RELAY_INBOX_STORAGE_DIR,
241 TRANSACTION_REPLAY_STORAGE_DIR,
242 ]);
243 Ok(())
244 }
245
246 #[tokio::test]
250 async fn test_a_second_node_on_open_stores_fails_at_open() -> Result<()> {
251 let root = std::env::temp_dir().join(format!("rings-node-lock-{}", uuid::Uuid::new_v4()));
252 let first = NodeStores::open_under(&root).await;
253 let second = NodeStores::open_under(&root).await;
254 let first_opened = first.is_ok();
255 drop(first);
256 let reopened = NodeStores::open_under(&root).await;
257 let _ = std::fs::remove_dir_all(&root);
258 assert!(first_opened);
259 assert!(matches!(
260 second,
261 Err(Error::Storage(
262 rings_core::error::Error::StorageDirectoryLocked(_)
263 ))
264 ));
265 reopened.map(drop)
266 }
267}