Skip to main content

fedimint_mintv2_client/
client_db.rs

1use std::collections::{BTreeMap, BTreeSet};
2
3use bitcoin_hashes::hash160;
4use fedimint_core::core::Account;
5use fedimint_core::encoding::{Decodable, Encodable};
6use fedimint_core::{impl_db_lookup, impl_db_record};
7use fedimint_mintv2_common::Denomination;
8use strum::Display;
9use strum_macros::EnumIter;
10
11use crate::SpendableNoteUndecoded;
12use crate::issuance::NoteIssuanceRequest;
13
14#[repr(u8)]
15#[derive(Clone, Display, EnumIter, Debug)]
16pub enum DbKeyPrefix {
17    Note = 0x20,
18    RecoveryState = 0x21,
19}
20
21/// Every account's notes share one table, split by the key's leading
22/// [`Account`]. Nonces derive from per-account secrets, so two accounts can
23/// never produce the same note.
24#[derive(Debug, Clone, Encodable, Decodable)]
25pub struct SpendableNoteKey(pub Account, pub SpendableNoteUndecoded);
26
27#[derive(Debug, Clone, Encodable, Decodable)]
28pub struct SpendableNotePrefix(pub Account);
29
30/// Relies on [`crate::SpendableNote`] encoding its denomination first, so an
31/// account and a denomination are a prefix of the whole key.
32#[derive(Debug, Clone, Encodable, Decodable)]
33pub struct SpendableNoteAmountPrefix(pub Account, pub Denomination);
34
35impl_db_record!(
36    key = SpendableNoteKey,
37    value = (),
38    db_prefix = DbKeyPrefix::Note,
39);
40
41impl_db_lookup!(key = SpendableNoteKey, query_prefix = SpendableNotePrefix);
42
43impl_db_lookup!(
44    key = SpendableNoteKey,
45    query_prefix = SpendableNoteAmountPrefix
46);
47
48/// Key for storing recovery progress state
49#[derive(Debug, Clone, Encodable, Decodable)]
50pub struct RecoveryStateKey;
51
52/// Recovery state that can be checkpointed and resumed
53#[derive(Debug, Clone, Encodable, Decodable)]
54pub struct RecoveryState {
55    /// Next item index to download
56    pub next_index: u64,
57    /// Total items (for progress calculation)
58    pub total_items: u64,
59    /// Already recovered note requests, keyed by `nonce_hash` (for efficient
60    /// removal when inputs are seen), each with the account whose subtree
61    /// re-derived it.
62    pub requests: BTreeMap<hash160::Hash, (Account, NoteIssuanceRequest)>,
63    /// Nonces seen (to detect duplicates)
64    pub nonces: BTreeSet<hash160::Hash>,
65}
66
67impl_db_record!(
68    key = RecoveryStateKey,
69    value = RecoveryState,
70    db_prefix = DbKeyPrefix::RecoveryState,
71);