Skip to main content

fedimint_mintv2_client/
client_db.rs

1use std::collections::{BTreeMap, BTreeSet};
2
3use bitcoin_hashes::hash160;
4use fedimint_core::encoding::{Decodable, Encodable};
5use fedimint_core::{impl_db_lookup, impl_db_record};
6use fedimint_mintv2_common::Denomination;
7use strum::Display;
8use strum_macros::EnumIter;
9
10use crate::SpendableNote;
11use crate::issuance::NoteIssuanceRequest;
12
13#[repr(u8)]
14#[derive(Clone, Display, EnumIter, Debug)]
15pub enum DbKeyPrefix {
16    Note = 0x20,
17    RecoveryState = 0x21,
18}
19
20#[derive(Debug, Clone, Encodable, Decodable)]
21pub struct SpendableNoteKey(pub SpendableNote);
22
23#[derive(Debug, Clone, Encodable, Decodable)]
24pub struct SpendableNotePrefix;
25
26#[derive(Debug, Clone, Encodable, Decodable)]
27pub struct SpendableNoteAmountPrefix(pub Denomination);
28
29impl_db_record!(
30    key = SpendableNoteKey,
31    value = (),
32    db_prefix = DbKeyPrefix::Note,
33);
34
35impl_db_lookup!(key = SpendableNoteKey, query_prefix = SpendableNotePrefix);
36
37impl_db_lookup!(
38    key = SpendableNoteKey,
39    query_prefix = SpendableNoteAmountPrefix
40);
41
42/// Key for storing recovery progress state
43#[derive(Debug, Clone, Encodable, Decodable)]
44pub struct RecoveryStateKey;
45
46/// Recovery state that can be checkpointed and resumed
47#[derive(Debug, Clone, Encodable, Decodable)]
48pub struct RecoveryState {
49    /// Next item index to download
50    pub next_index: u64,
51    /// Total items (for progress calculation)
52    pub total_items: u64,
53    /// Already recovered note requests, keyed by `nonce_hash` (for efficient
54    /// removal when inputs are seen)
55    pub requests: BTreeMap<hash160::Hash, NoteIssuanceRequest>,
56    /// Nonces seen (to detect duplicates)
57    pub nonces: BTreeSet<hash160::Hash>,
58}
59
60impl_db_record!(
61    key = RecoveryStateKey,
62    value = RecoveryState,
63    db_prefix = DbKeyPrefix::RecoveryState,
64);