Skip to main content

miden_node_store/state/
mod.rs

1//! Abstraction to synchronize state modifications.
2//!
3//! The [State] provides data access and modifications methods, its main purpose is to ensure that
4//! data is atomically written, and that reads are consistent.
5
6mod block_lifecycle;
7mod bootstrap;
8mod disk_monitor;
9mod lifecycle;
10mod loader;
11mod replica;
12mod tip;
13mod view;
14mod writer;
15
16use std::path::PathBuf;
17use std::sync::Arc;
18
19use arc_swap::ArcSwap;
20pub use lifecycle::LoadedState;
21use miden_protocol::block::BlockNumber;
22pub use replica::{BlockCache, BlockNotification, ProofCache, ProofNotification};
23use tokio::sync::watch;
24pub use view::{ScopedBlockNum, ScopedBlockRange, StateView, StateWitnesses, TransactionInputs};
25use view::{SnapshotGuard, StateSnapshot};
26pub use writer::{BlockWriter, ProofWriter, WriterTask};
27
28use crate::blocks::BlockStore;
29use crate::db::Db;
30use crate::proven_tip::ProvenTipWriter;
31
32// CHAIN STATE
33// ================================================================================================
34
35/// The rollup state, read-only.
36///
37/// Mutations go through the [`BlockWriter`] and [`ProofWriter`] capabilities returned by
38/// [`LoadedState::start`]; every holder of this type can only query and subscribe.
39///
40/// All tree and database reads go through a request-scoped [`StateView`] obtained from
41/// [`State::view`]; this type itself only exposes chain-tip queries, subscriptions, and
42/// block-store access.
43pub struct State {
44    /// Root directory containing the store's on-disk data.
45    data_directory: PathBuf,
46
47    /// The database which stores block headers, nullifiers, notes, and the latest states of
48    /// accounts.
49    db: Arc<Db>,
50
51    /// The block store which stores full block contents for all blocks.
52    block_store: Arc<BlockStore>,
53
54    /// Atomically swappable pointer to the latest in-memory state snapshot.
55    ///
56    /// Readers load the snapshot wait-free via [`ArcSwap::load_full`]; the
57    /// [`WriteWorker`](writer::WriteWorker) task atomically replaces the pointer after each
58    /// committed block. Readers holding an old snapshot are unaffected by the swap.
59    latest_snapshot: Arc<ArcSwap<StateSnapshot>>,
60
61    /// The latest proven-in-sequence block number, updated by the proof scheduler or `apply_proof`.
62    proven_tip: ProvenTipWriter,
63
64    /// Watch sender fired after each block is committed. Replicas subscribe via
65    /// `subscribe_committed_tip()` to be woken when new blocks arrive.
66    committed_tip_tx: Arc<watch::Sender<BlockNumber>>,
67
68    /// FIFO cache of recent committed blocks for replica subscriptions. When a subscriber needs a
69    /// block that has been evicted, it falls back to loading from the block store.
70    pub(crate) block_cache: BlockCache,
71
72    /// FIFO cache of recent block proofs for replica subscriptions. When a subscriber needs a proof
73    /// that has been evicted, it falls back to loading from the block store.
74    pub(crate) proof_cache: ProofCache,
75}