ant_node/storage/mod.rs
1//! Storage subsystem for chunk persistence.
2//!
3//! This module provides content-addressed storage for chunks, one immutable file per
4//! chunk, along with a protocol handler that integrates with saorsa-core's `Protocol`
5//! trait for automatic message routing.
6//!
7//! # Architecture
8//!
9//! ```text
10//! ┌─────────────────────────────────────────────────────────┐
11//! │ AntProtocol (implements Protocol trait) │
12//! ├─────────────────────────────────────────────────────────┤
13//! │ protocol_id() = "autonomi.ant.chunk.v1" │
14//! │ │
15//! │ handle(peer_id, data) ──▶ decode AntProtocolMessage │
16//! │ │ │
17//! │ ┌─────────────────────────┼─────────────────┐ │
18//! │ ▼ ▼ ▼ │
19//! │ QuoteRequest ChunkPutRequest ChunkGetRequest
20//! │ │ │ │ │
21//! │ ▼ ▼ ▼ │
22//! │ QuoteGenerator PaymentVerifier ChunkStore│
23//! │ │ │ │ │
24//! │ └─────────────────────────┴─────────────────┘ │
25//! │ │ │
26//! │ return Ok(Some(response_bytes)) │
27//! └─────────────────────────────────────────────────────────┘
28//! ```
29//!
30//! # Example
31//!
32//! ```rust,ignore
33//! use std::sync::Arc;
34//! use ant_node::storage::{AntProtocol, ChunkStore, ChunkStoreConfig};
35//!
36//! // Create storage
37//! let config = ChunkStoreConfig::default();
38//! let storage = Arc::new(ChunkStore::new(config).await?);
39//!
40//! // Create protocol handler
41//! let protocol = AntProtocol::new(storage, Arc::new(payment_verifier), Arc::new(quote_generator));
42//!
43//! // Register with saorsa-core
44//! listener.register_protocol(protocol).await?;
45//! ```
46
47// `test-utils` makes this module public so integration tests and downstream harnesses can
48// reach the store directly, and that is not a narrow door: with the feature on, every `pub`
49// item inside it — `StoreLayout`, `CapacityVerdict`, every inherent method on `ChunkStore` —
50// is importable from outside the crate. The re-export list below is the boundary for the
51// DEFAULT build only. Anything relying on the wider surface is relying on a test feature, and
52// this comment is here so that is a decision rather than a discovery.
53#[cfg(any(test, feature = "test-utils"))]
54pub mod chunk_store;
55#[cfg(not(any(test, feature = "test-utils")))]
56pub(crate) mod chunk_store;
57mod handler;
58// This crate's own business: the cleanup is called once, from the node builder.
59pub(crate) mod legacy_artifacts;
60pub(crate) mod traffic;
61
62pub use crate::ant_protocol::XorName;
63pub(crate) use chunk_store::{rename_with_retry, Reservation};
64pub use chunk_store::{ChunkStore, ChunkStoreConfig};
65// Crate-private, as it was before the two stores became one: `CapacityVerdict` was
66// `pub(crate)` on the old store and has no caller outside this crate.
67pub(crate) use chunk_store::CapacityVerdict;
68pub use handler::AntProtocol;
69pub(crate) use handler::ChunkRequestContext;
70pub use handler::SELF_CLOSENESS_GATE_WIDTH;
71
72/// Bytes in one MiB.
73pub const MIB: u64 = 1024 * 1024;
74
75/// Bytes in one GiB.
76pub const GIB: u64 = 1024 * MIB;
77
78/// Default free disk space to keep unused on the storage partition.
79pub const DEFAULT_DISK_RESERVE: u64 = 500 * MIB;
80
81/// Statistics about storage operations.
82///
83/// Counters other than `current_chunks` are cumulative for the lifetime of the
84/// process; `current_chunks` is the live count.
85#[derive(Debug, Clone, Default)]
86pub struct StorageStats {
87 /// Total number of chunks stored.
88 pub chunks_stored: u64,
89 /// Total number of chunks retrieved.
90 pub chunks_retrieved: u64,
91 /// Total bytes stored.
92 pub bytes_stored: u64,
93 /// Total bytes retrieved.
94 pub bytes_retrieved: u64,
95 /// Number of duplicate writes (already exists).
96 pub duplicates: u64,
97 /// Number of verification failures on read.
98 pub verification_failures: u64,
99 /// Number of chunks currently persisted.
100 pub current_chunks: u64,
101}