1use core::fmt;
2use std::ops;
3use std::str::FromStr;
4use std::time::SystemTime;
5
6use fedimint_client_module::module::init::recovery::RecoveryFromHistoryCommon;
7use fedimint_core::core::OperationId;
8use fedimint_core::encoding::{Decodable, Encodable};
9use fedimint_core::{TransactionId, impl_db_lookup, impl_db_record};
10use serde::{Deserialize, Serialize};
11use strum_macros::EnumIter;
12
13use crate::backup::WalletRecoveryState;
14
15#[derive(Clone, EnumIter, Debug)]
16pub enum DbKeyPrefix {
17 NextPegInTweakIndex = 0x2c,
18 PegInTweakIndex = 0x2d,
19 ClaimedPegIn = 0x2e,
20 RecoveryFinalized = 0x2f,
21 RecoveryState = 0x30,
22 SupportsSafeDeposit = 0x31,
23 ExternalReservedStart = 0xb0,
26 CoreInternalReservedStart = 0xd0,
29 CoreInternalReservedEnd = 0xff,
32}
33
34impl std::fmt::Display for DbKeyPrefix {
35 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
36 write!(f, "{self:?}")
37 }
38}
39
40#[derive(
45 Copy,
46 Clone,
47 Debug,
48 Encodable,
49 Decodable,
50 Serialize,
51 Deserialize,
52 Default,
53 PartialEq,
54 Eq,
55 PartialOrd,
56 Ord,
57)]
58pub struct TweakIdx(pub u64);
59
60impl fmt::Display for TweakIdx {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 f.write_fmt(format_args!("TweakIdx({})", self.0))
63 }
64}
65
66impl FromStr for TweakIdx {
67 type Err = <u64 as FromStr>::Err;
68
69 fn from_str(s: &str) -> Result<Self, Self::Err> {
70 Ok(Self(FromStr::from_str(s)?))
71 }
72}
73
74impl TweakIdx {
75 #[must_use]
76 pub fn next(self) -> Self {
77 Self(self.0 + 1)
78 }
79
80 #[must_use]
81 pub fn prev(self) -> Option<Self> {
82 self.0.checked_sub(1).map(Self)
83 }
84
85 #[must_use]
86 pub fn advance(self, i: u64) -> Self {
87 Self(self.0 + i)
88 }
89
90 pub fn saturating_sub(&self, rhs: TweakIdx) -> u64 {
91 self.0.saturating_sub(rhs.0)
92 }
93}
94
95impl ops::Sub for TweakIdx {
96 type Output = u64;
97
98 fn sub(self, rhs: Self) -> Self::Output {
99 self.0 - rhs.0
100 }
101}
102
103#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
105pub struct NextPegInTweakIndexKey;
106
107impl_db_record!(
108 key = NextPegInTweakIndexKey,
109 value = TweakIdx,
110 db_prefix = DbKeyPrefix::NextPegInTweakIndex,
111);
112
113#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
116pub struct PegInTweakIndexKey(pub TweakIdx);
117
118#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
119pub struct PegInTweakIndexPrefix;
120
121#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
122pub struct PegInTweakIndexData {
123 pub operation_id: OperationId,
125 pub creation_time: SystemTime,
127 pub last_check_time: Option<SystemTime>,
129 pub next_check_time: Option<SystemTime>,
131 pub claimed: Vec<bitcoin::OutPoint>,
133}
134
135impl_db_record!(
136 key = PegInTweakIndexKey,
137 value = PegInTweakIndexData,
138 db_prefix = DbKeyPrefix::PegInTweakIndex,
139);
140
141impl_db_lookup!(
142 key = PegInTweakIndexKey,
143 query_prefix = PegInTweakIndexPrefix
144);
145
146#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
147pub struct ClaimedPegInKey {
148 pub peg_in_index: TweakIdx,
149 pub btc_out_point: bitcoin::OutPoint,
150}
151
152#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
153pub struct ClaimedPegInPrefix;
154
155#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
156pub struct ClaimedPegInData {
157 pub claim_txid: TransactionId,
162 pub change: Vec<fedimint_core::OutPoint>,
163}
164
165impl_db_record!(
166 key = ClaimedPegInKey,
167 value = ClaimedPegInData,
168 db_prefix = DbKeyPrefix::ClaimedPegIn,
169 notify_on_modify = true,
170);
171impl_db_lookup!(key = ClaimedPegInKey, query_prefix = ClaimedPegInPrefix);
172
173#[derive(Debug, Clone, Encodable, Decodable, Serialize)]
174pub struct RecoveryFinalizedKey;
175
176#[derive(Debug, Clone, Encodable, Decodable)]
177pub struct RecoveryFinalizedKeyPrefix;
178
179impl_db_record!(
180 key = RecoveryFinalizedKey,
181 value = bool,
182 db_prefix = DbKeyPrefix::RecoveryFinalized,
183);
184
185#[derive(Debug, Clone, Encodable, Decodable, Serialize)]
186pub struct RecoveryStateKey;
187
188#[derive(Debug, Clone, Encodable, Decodable)]
189pub struct RestoreStateKeyPrefix;
190
191impl_db_record!(
192 key = RecoveryStateKey,
193 value = (WalletRecoveryState, RecoveryFromHistoryCommon),
194 db_prefix = DbKeyPrefix::RecoveryState,
195);
196
197#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
198pub struct SupportsSafeDepositKey;
199
200#[derive(Clone, Debug, Encodable, Decodable)]
201pub struct SupportsSafeDepositPrefix;
202
203impl_db_record!(
204 key = SupportsSafeDepositKey,
205 value = (),
206 db_prefix = DbKeyPrefix::SupportsSafeDeposit,
207);
208
209impl_db_lookup!(
210 key = SupportsSafeDepositKey,
211 query_prefix = SupportsSafeDepositPrefix
212);