Skip to main content

dig_coinstore/
lib.rs

1//! # dig-coinstore
2//!
3//! Persistent global coin state database for the DIG Network L2 blockchain.
4//!
5//! This crate manages the authoritative database of all spent and unspent coins
6//! across the entire blockchain using the coinset model (UTXO-like). It accepts
7//! validated blocks as input, applies their state transitions, and provides a
8//! rich query API for coin lookups by ID, puzzle hash, hint, parent, and height.
9//!
10//! ## Crate boundary
11//!
12//! - **Input:** Pre-validated `BlockData` (additions, removals, coinbase, hints)
13//! - **Output:** `CoinRecord`s, `CoinState`s, state roots, Merkle proofs
14//! - **Not in scope:** CLVM execution, block production, networking, consensus
15//!
16//! ## Storage backends
17//!
18//! Storage is feature-gated:
19//! - `rocksdb-storage` (default) — RocksDB with bloom filters and column families
20//! - `lmdb-storage` — LMDB with memory-mapped I/O
21//! - `full-storage` — Both backends enabled
22//!
23//! ## Module organization
24//!
25//! | Module | Responsibility | Requirement domain |
26//! |--------|---------------|--------------------|
27//! | [`coin_store`] | Primary public API struct | API-001 |
28//! | [`config`] | Configuration types and constants | API-003 |
29//! | [`error`] | Error enum | API-004 |
30//! | [`types`] | Domain types (CoinRecord, BlockData, etc.) | API-002, API-005..009 |
31//! | [`block_apply`] | Block application pipeline | BLK-001..014 |
32//! | [`rollback`] | Rollback / reorg recovery | RBK-001..007 |
33//! | [`queries`] | Coin state queries | QRY-001..011 |
34//! | [`hints`] | Hint store | HNT-001..006 |
35//! | [`storage`] | Backend trait + implementations | STO-001..008 |
36//! | [`merkle`] | Sparse Merkle tree + proofs | MRK-001..006 |
37//! | [`cache`] | In-memory caching (unspent set, LRU, counters) | PRF-001..003 |
38//! | [`archive`] | Tiered spent coin archival | PRF-005 |
39//!
40//! ## Spec reference
41//!
42//! Master specification: `docs/resources/SPEC.md`
43//! Requirements: `docs/requirements/IMPLEMENTATION_ORDER.md`
44//!
45//! See also:
46//! - Chia CoinStore: `chia/full_node/coin_store.py`
47//! - Chia HintStore: `chia/full_node/hint_store.py`
48
49// ─────────────────────────────────────────────────────────────────────────────
50// Re-exports: Chia ecosystem types (STR-005)
51// ─────────────────────────────────────────────────────────────────────────────
52// These re-exports ensure consumers of dig-coinstore can access core Chia types
53// without adding direct dependencies on chia-protocol or dig-clvm.
54//
55// The dependency chain is:
56//   chia-protocol  →  dig-clvm  →  dig-coinstore  →  consumers
57//       (defines)    (re-exports)   (re-exports)
58//
59// Requirement: STR-005
60// Spec: docs/requirements/domains/crate_structure/specs/STR-005.md
61
62/// The fundamental coin type in the Chia coinset model.
63/// `CoinId = sha256(parent_coin_info || puzzle_hash || amount)`.
64/// Re-exported from `dig-clvm` (which re-exports from `chia-protocol`).
65pub use dig_clvm::Coin;
66
67/// A 32-byte hash used for coin IDs, puzzle hashes, block hashes, state roots.
68/// Re-exported from `dig-clvm` (which re-exports from `chia-protocol`).
69pub use dig_clvm::Bytes32;
70
71/// Lightweight coin state for the sync protocol: coin + created_height + spent_height.
72/// Re-exported from `dig-clvm` (which re-exports from `chia-protocol`).
73pub use dig_clvm::CoinState;
74
75/// Filters for batch coin state queries (include_spent, include_unspent, include_hinted, min_amount).
76/// Used by `batch_coin_states_by_puzzle_hashes()` (QRY-007).
77/// Not re-exported by dig-clvm, so imported directly from chia-protocol.
78pub use chia_protocol::CoinStateFilters;
79
80// STO-007: at least one storage backend feature must be enabled so `rocksdb` / `heed` stay optional
81// dependencies and backend modules are never half-linked. Without this, `cargo build --no-default-features`
82// would compile an unusable crate (no `RocksDbBackend` / `LmdbBackend` symbols).
83//
84// Spec: docs/requirements/domains/storage/specs/STO-007.md — "Compile-Time Validation"
85#[cfg(not(any(feature = "rocksdb-storage", feature = "lmdb-storage")))]
86compile_error!(
87    "dig-coinstore: enable at least one storage backend Cargo feature: \
88     `rocksdb-storage` and/or `lmdb-storage`, or `full-storage` for both. \
89     The default package features include `rocksdb-storage`."
90);
91
92// ─────────────────────────────────────────────────────────────────────────────
93// Top-level modules
94// ─────────────────────────────────────────────────────────────────────────────
95// Each module corresponds to one or more requirement domains in
96// docs/requirements/domains/. The module hierarchy is defined by STR-002.
97
98/// CoinStore struct — primary public API orchestration.
99/// See: docs/requirements/domains/crate_api/specs/API-001.md
100pub mod coin_store;
101
102/// Configuration types and constants.
103/// See: docs/requirements/domains/crate_api/specs/API-003.md
104pub mod config;
105
106// API-003: expose config surface at crate root (see also `storage::StorageBackend` trait).
107pub use config::{
108    default_storage_backend_for_features, CoinStoreConfig, StorageBackend,
109    BLOOM_FILTER_BITS_PER_KEY, DEFAULT_LMDB_MAP_SIZE, DEFAULT_MAX_QUERY_RESULTS,
110    DEFAULT_MAX_SNAPSHOTS, DEFAULT_ROCKSDB_MAX_OPEN_FILES, DEFAULT_ROCKSDB_WRITE_BUFFER_SIZE,
111};
112
113/// Error types for all coinstore operations.
114/// See: docs/requirements/domains/crate_api/specs/API-004.md
115pub mod error;
116
117pub use error::CoinStoreError;
118
119/// Domain types: CoinRecord, BlockData, CoinAddition, result structs, [`CoinStoreStats`], [`CoinStoreSnapshot`],
120/// [`UnspentLineageInfo`], type aliases [`CoinId`] / [`PuzzleHash`].
121/// See: docs/requirements/domains/crate_api/specs/API-002.md
122pub mod types;
123
124// Wire-shaped coin row for interop (see `types` module doc: mirrors upstream `CoinRecord` until
125// `chia-protocol` in the `dig-clvm` graph exposes it; then replace with `pub use chia_protocol::CoinRecord as ChiaCoinRecord`).
126pub use types::{
127    ApplyBlockResult, BlockData, ChiaCoinRecord, CoinAddition, CoinId, CoinRecord,
128    CoinStoreSnapshot, CoinStoreStats, PuzzleHash, RollbackResult, UnspentLineageInfo,
129};
130
131/// Block application pipeline (Phase 1 validation + Phase 2 mutation).
132/// See: docs/requirements/domains/block_application/specs/
133pub mod block_apply;
134
135/// Rollback pipeline for chain reorganization recovery.
136/// See: docs/requirements/domains/rollback/specs/
137pub mod rollback;
138
139/// All query method implementations on CoinStore.
140/// See: docs/requirements/domains/queries/specs/
141pub mod queries;
142
143/// Hint store for puzzle hash hints on coins.
144/// See: docs/requirements/domains/hints/specs/
145pub mod hints;
146
147/// Tiered spent coin archival (hot/archive/prune).
148/// See: docs/requirements/domains/performance/specs/PRF-005.md
149pub mod archive;
150
151// ─────────────────────────────────────────────────────────────────────────────
152// Subdirectory modules
153// ─────────────────────────────────────────────────────────────────────────────
154
155/// Storage backend abstraction (trait + RocksDB/LMDB implementations).
156/// Backend modules are feature-gated: `rocksdb-storage`, `lmdb-storage`.
157/// See: docs/requirements/domains/storage/specs/
158pub mod storage;
159
160/// Open a concrete [`storage::StorageBackend`] for the engine in [`config::CoinStoreConfig::backend`].
161///
162/// **STO-007:** runtime factory when one or both backends are compiled (`full-storage`); callers that only need
163/// the KV layer (not [`coin_store::CoinStore`]) use this entry point. See [`storage::open_storage_backend`].
164pub use storage::open_storage_backend;
165
166/// Sparse Merkle tree for state root computation and proofs.
167/// See: docs/requirements/domains/merkle/specs/
168pub mod merkle;
169
170/// In-memory caching: unspent set, LRU cache, materialized counters.
171/// See: docs/requirements/domains/performance/specs/PRF-001..003.md
172pub mod cache;