dig_store/types.rs
1//! The shared identifier + value types of the store surface.
2//!
3//! The coin/identity types are re-exported VERBATIM from `dig-merkle` (which re-exports the
4//! `chia-wallet-sdk` byte-source-of-truth) so a consumer depends on ONE canonical shape and never a
5//! shadow copy that could byte-drift:
6//!
7//! - [`Bytes32`] — a 32-byte identifier (a `store_id` / `launcher_id`, a merkle root, a DID id);
8//! - [`Coin`] / [`CoinSpend`] — the Chia coin + confirmed spend;
9//! - [`DataStore`] / [`DigDataStoreMetadata`] — the hydrated DataLayer coin + its on-chain metadata;
10//! - [`DidRef`] — a reference to an owning DID by its launcher id;
11//! - [`MerkleCoinSpend`] — the unsigned result of a lifecycle operation (coin spends + child store).
12//!
13//! [`RootHistory`] is the one `dig-store`-owned view type: the ordered list of merkle roots a store
14//! has anchored across its generations, produced by the on-chain lineage walk (SPEC §5).
15
16pub use dig_merkle::{
17 Bytes32, Coin, CoinSpend, DataStore, DidRef, DigDataStoreMetadata, MerkleCoinSpend,
18};
19
20/// The ordered history of merkle roots a store has anchored, oldest first.
21///
22/// Each entry is proven on chain by walking the singleton's lineage from the launcher forward (NC-9,
23/// SPEC §5). The last element is the latest root. A live store is never empty (the mint anchors
24/// generation 0); a fully-melted store's history still lists every root it anchored while live.
25#[derive(Debug, Clone, PartialEq, Eq, Default)]
26pub struct RootHistory {
27 /// The anchored roots, oldest → newest.
28 pub roots: Vec<Bytes32>,
29}
30
31impl RootHistory {
32 /// The most recently anchored root, if any.
33 pub fn latest(&self) -> Option<Bytes32> {
34 self.roots.last().copied()
35 }
36
37 /// The number of generations (root anchorings) the store has had.
38 pub fn generation_count(&self) -> usize {
39 self.roots.len()
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 fn id(b: u8) -> Bytes32 {
48 Bytes32::new([b; 32])
49 }
50
51 #[test]
52 fn root_history_reports_latest_and_generation_count() {
53 let empty = RootHistory::default();
54 assert_eq!(empty.latest(), None);
55 assert_eq!(empty.generation_count(), 0);
56
57 let history = RootHistory {
58 roots: vec![id(1), id(2), id(3)],
59 };
60 assert_eq!(history.latest(), Some(id(3)));
61 assert_eq!(history.generation_count(), 3);
62 }
63}