Skip to main content

fedimint_walletv2_server/
db.rs

1use bitcoin::{TxOut, Txid};
2use fedimint_core::encoding::{Decodable, Encodable};
3use fedimint_core::{PeerId, impl_db_lookup, impl_db_record};
4use fedimint_walletv2_common::TxInfo;
5use secp256k1::ecdsa::Signature;
6use serde::Serialize;
7use strum_macros::EnumIter;
8
9use crate::{FederationTx, FederationWallet};
10
11#[repr(u8)]
12#[derive(Clone, EnumIter, Debug)]
13pub enum DbKeyPrefix {
14    Deposit = 0x30,
15    SpentDeposit = 0x31,
16    BlockCountVote = 0x32,
17    FeeRateVote = 0x33,
18    TxLog = 0x34,
19    TxInfoIndex = 0x35,
20    UnsignedTx = 0x36,
21    Signatures = 0x37,
22    UnconfirmedTx = 0x38,
23    FederationWallet = 0x39,
24}
25
26impl std::fmt::Display for DbKeyPrefix {
27    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
28        write!(f, "{self:?}")
29    }
30}
31
32#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
33pub struct DepositKey(pub u64);
34
35#[derive(Clone, Debug, Encodable, Decodable)]
36pub struct DepositPrefix;
37
38#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
39pub struct Deposit(pub bitcoin::OutPoint, pub TxOut);
40
41impl_db_record!(
42    key = DepositKey,
43    value = Deposit,
44    db_prefix = DbKeyPrefix::Deposit,
45);
46
47impl_db_lookup!(key = DepositKey, query_prefix = DepositPrefix);
48
49#[derive(Clone, Debug, Eq, PartialEq, Encodable, Decodable, Serialize)]
50pub struct SpentDepositKey(pub u64);
51
52#[derive(Clone, Debug, Encodable, Decodable)]
53pub struct SpentDepositPrefix;
54
55impl_db_record!(
56    key = SpentDepositKey,
57    value = (),
58    db_prefix = DbKeyPrefix::SpentDeposit
59);
60
61impl_db_lookup!(key = SpentDepositKey, query_prefix = SpentDepositPrefix);
62
63#[derive(Clone, Debug, Eq, PartialEq, Encodable, Decodable, Serialize)]
64pub struct FederationWalletPrefix;
65
66#[derive(Clone, Debug, Eq, PartialEq, Encodable, Decodable, Serialize)]
67pub struct FederationWalletKey;
68
69impl_db_record!(
70    key = FederationWalletKey,
71    value = FederationWallet,
72    db_prefix = DbKeyPrefix::FederationWallet,
73);
74
75impl_db_lookup!(
76    key = FederationWalletKey,
77    query_prefix = FederationWalletPrefix
78);
79
80#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
81pub struct TxInfoKey(pub u64);
82
83#[derive(Clone, Debug, Encodable, Decodable)]
84pub struct TxInfoPrefix;
85
86impl_db_record!(
87    key = TxInfoKey,
88    value = TxInfo,
89    db_prefix = DbKeyPrefix::TxLog,
90);
91
92impl_db_lookup!(key = TxInfoKey, query_prefix = TxInfoPrefix);
93
94#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
95pub struct TxInfoIndexKey(pub fedimint_core::OutPoint);
96
97#[derive(Clone, Debug, Encodable, Decodable)]
98pub struct TxInfoIndexPrefix;
99
100impl_db_record!(
101    key = TxInfoIndexKey,
102    value = u64,
103    db_prefix = DbKeyPrefix::TxInfoIndex,
104);
105
106impl_db_lookup!(key = TxInfoIndexKey, query_prefix = TxInfoIndexPrefix);
107
108#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
109pub struct UnsignedTxKey(pub Txid);
110
111#[derive(Clone, Debug, Encodable, Decodable)]
112pub struct UnsignedTxPrefix;
113
114impl_db_record!(
115    key = UnsignedTxKey,
116    value = FederationTx,
117    db_prefix = DbKeyPrefix::UnsignedTx,
118);
119
120impl_db_lookup!(key = UnsignedTxKey, query_prefix = UnsignedTxPrefix);
121
122#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
123pub struct SignaturesKey(pub Txid, pub PeerId);
124
125#[derive(Clone, Debug, Encodable, Decodable)]
126pub struct SignaturesTxidPrefix(pub Txid);
127
128#[derive(Clone, Debug, Encodable, Decodable)]
129pub struct SignaturesPrefix;
130
131impl_db_record!(
132    key = SignaturesKey,
133    value = Vec<Signature>,
134    db_prefix = DbKeyPrefix::Signatures,
135);
136
137impl_db_lookup!(key = SignaturesKey, query_prefix = SignaturesTxidPrefix);
138
139impl_db_lookup!(key = SignaturesKey, query_prefix = SignaturesPrefix);
140
141#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
142pub struct UnconfirmedTxKey(pub Txid);
143
144#[derive(Clone, Debug, Encodable, Decodable)]
145pub struct UnconfirmedTxPrefix;
146
147impl_db_record!(
148    key = UnconfirmedTxKey,
149    value = FederationTx,
150    db_prefix = DbKeyPrefix::UnconfirmedTx,
151);
152
153impl_db_lookup!(key = UnconfirmedTxKey, query_prefix = UnconfirmedTxPrefix);
154
155#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
156pub struct BlockCountVoteKey(pub PeerId);
157
158#[derive(Clone, Debug, Encodable, Decodable)]
159pub struct BlockCountVotePrefix;
160
161impl_db_record!(
162    key = BlockCountVoteKey,
163    value = u64,
164    db_prefix = DbKeyPrefix::BlockCountVote
165);
166
167impl_db_lookup!(key = BlockCountVoteKey, query_prefix = BlockCountVotePrefix);
168
169#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
170pub struct FeeRateVoteKey(pub PeerId);
171
172#[derive(Clone, Debug, Encodable, Decodable)]
173pub struct FeeRateVotePrefix;
174
175impl_db_record!(
176    key = FeeRateVoteKey,
177    value = Option<u64>,
178    db_prefix = DbKeyPrefix::FeeRateVote
179);
180
181impl_db_lookup!(key = FeeRateVoteKey, query_prefix = FeeRateVotePrefix);