use std::path::Path;
use rings_core::dht::EntryStorage;
use rings_core::dht::RemovalStorage;
use rings_core::message::ReplayStorage;
use rings_core::storage::file::FileStorage;
use rings_core::storage::RecordAuthority;
pub use crate::consts::DHT_REMOVAL_STORAGE_CAPACITY;
pub use crate::consts::ONION_ENTRY_GUARD_STORAGE_CAPACITY;
pub use crate::consts::RELAY_INBOX_STORAGE_CAPACITY;
pub use crate::consts::TRANSACTION_REPLAY_STORAGE_CAPACITY;
use crate::error::Error;
use crate::error::Result;
use crate::measure::EvidenceStorage;
use crate::measure::MeasureStorage;
use crate::native::config::StorageConfig;
use crate::native::config::DEFAULT_STORAGE_CAPACITY;
use crate::onion::OnionEntryGuardStorage;
use crate::release::StoresReleased;
pub const DHT_REMOVAL_STORAGE_DIR: &str = "dht-removals";
pub const ONION_ENTRY_GUARD_STORAGE_DIR: &str = "onion-entry-guards";
pub const TRANSACTION_REPLAY_STORAGE_DIR: &str = "transaction-replay";
pub const RELAY_INBOX_STORAGE_DIR: &str = "relay-inbox";
pub const PROVISIONAL_EVIDENCE_STORAGE_DIR: &str = "provisional-evidence";
pub const DATA_STORAGE_DIR: &str = "data";
pub const MEASURE_STORAGE_DIR: &str = "measure";
pub fn sibling_storage_path(storage_path: &str, dir: &str) -> String {
Path::new(storage_path)
.parent()
.filter(|path| !path.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."))
.join(dir)
.to_string_lossy()
.to_string()
}
pub struct NodeStores {
pub data: EntryStorage,
pub relay: EntryStorage,
pub removals: RemovalStorage,
pub measure: MeasureStorage,
pub evidence: EvidenceStorage,
pub guards: OnionEntryGuardStorage,
pub replay: ReplayStorage,
pub released: StoresReleased,
}
async fn open_file(path: String, capacity: u32, authority: RecordAuthority) -> Result<FileStorage> {
FileStorage::new_with_cap_path_and_authority(capacity, path, authority)
.await
.map_err(Error::Storage)
}
impl NodeStores {
pub async fn open(data: &StorageConfig, measure: &StorageConfig) -> Result<Self> {
let beside_data = |dir| sibling_storage_path(&data.path, dir);
let data_store = open_file(
data.path.clone(),
data.capacity,
RecordAuthority::Disposable,
)
.await?;
let relay = open_file(
beside_data(RELAY_INBOX_STORAGE_DIR),
RELAY_INBOX_STORAGE_CAPACITY,
RecordAuthority::Retained,
)
.await?;
let removals = open_file(
beside_data(DHT_REMOVAL_STORAGE_DIR),
DHT_REMOVAL_STORAGE_CAPACITY,
RecordAuthority::Retained,
)
.await?;
let measure_store = open_file(
measure.path.clone(),
measure.capacity,
RecordAuthority::Disposable,
)
.await?;
let evidence = open_file(
sibling_storage_path(&measure.path, PROVISIONAL_EVIDENCE_STORAGE_DIR),
measure.capacity,
RecordAuthority::Disposable,
)
.await?;
let guards = open_file(
beside_data(ONION_ENTRY_GUARD_STORAGE_DIR),
ONION_ENTRY_GUARD_STORAGE_CAPACITY,
RecordAuthority::DurableCache,
)
.await?;
let replay = open_file(
beside_data(TRANSACTION_REPLAY_STORAGE_DIR),
TRANSACTION_REPLAY_STORAGE_CAPACITY,
RecordAuthority::Authoritative,
)
.await?;
let released = StoresReleased::of(
[
&data_store,
&relay,
&removals,
&measure_store,
&evidence,
&guards,
&replay,
]
.map(FileStorage::released),
);
Ok(Self {
data: Box::new(data_store),
relay: Box::new(relay),
removals: Box::new(removals),
measure: Box::new(measure_store),
evidence: Box::new(evidence),
guards: Box::new(guards),
replay: Box::new(replay),
released,
})
}
pub async fn open_under(root: &Path) -> Result<Self> {
let store = |dir: &str| root.join(dir).to_string_lossy().to_string();
Self::open(
&StorageConfig::new(&store(DATA_STORAGE_DIR), DEFAULT_STORAGE_CAPACITY),
&StorageConfig::new(&store(MEASURE_STORAGE_DIR), DEFAULT_STORAGE_CAPACITY),
)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sibling_stores_sit_beside_their_store() {
for dir in [
ONION_ENTRY_GUARD_STORAGE_DIR,
TRANSACTION_REPLAY_STORAGE_DIR,
RELAY_INBOX_STORAGE_DIR,
DHT_REMOVAL_STORAGE_DIR,
PROVISIONAL_EVIDENCE_STORAGE_DIR,
] {
assert_eq!(
sibling_storage_path(".rings/data", dir),
format!(".rings/{dir}")
);
assert_eq!(
sibling_storage_path("/tmp/rings/data", dir),
format!("/tmp/rings/{dir}")
);
assert_eq!(sibling_storage_path("data", dir), format!("./{dir}"));
}
}
#[test]
fn test_transaction_replay_storage_holds_a_full_store(
) -> std::result::Result<(), std::num::TryFromIntError> {
let capacity = usize::try_from(TRANSACTION_REPLAY_STORAGE_CAPACITY)?;
assert!(rings_core::message::TRANSACTION_REPLAY_STORE_MAX_BYTES <= capacity);
Ok(())
}
#[tokio::test]
async fn test_open_under_lays_every_store_out_under_the_root() -> Result<()> {
let root = std::env::temp_dir().join(format!("rings-node-stores-{}", uuid::Uuid::new_v4()));
let stores = NodeStores::open_under(&root).await;
let mut dirs = std::fs::read_dir(&root)
.map(|entries| {
entries
.flatten()
.map(|entry| entry.file_name().to_string_lossy().to_string())
.collect::<Vec<_>>()
})
.unwrap_or_default();
dirs.sort();
let _ = std::fs::remove_dir_all(&root);
stores?;
assert_eq!(dirs, [
DATA_STORAGE_DIR,
DHT_REMOVAL_STORAGE_DIR,
MEASURE_STORAGE_DIR,
ONION_ENTRY_GUARD_STORAGE_DIR,
PROVISIONAL_EVIDENCE_STORAGE_DIR,
RELAY_INBOX_STORAGE_DIR,
TRANSACTION_REPLAY_STORAGE_DIR,
]);
Ok(())
}
#[tokio::test]
async fn test_a_second_node_on_open_stores_fails_at_open() -> Result<()> {
let root = std::env::temp_dir().join(format!("rings-node-lock-{}", uuid::Uuid::new_v4()));
let first = NodeStores::open_under(&root).await;
let second = NodeStores::open_under(&root).await;
let first_opened = first.is_ok();
drop(first);
let reopened = NodeStores::open_under(&root).await;
let _ = std::fs::remove_dir_all(&root);
assert!(first_opened);
assert!(matches!(
second,
Err(Error::Storage(
rings_core::error::Error::StorageDirectoryLocked(_)
))
));
reopened.map(drop)
}
}