fedimint_lnv2_server/
db.rs

1use fedimint_core::encoding::{Decodable, Encodable};
2use fedimint_core::util::SafeUrl;
3use fedimint_core::{PeerId, impl_db_lookup, impl_db_record};
4use fedimint_lnv2_common::ContractId;
5use fedimint_lnv2_common::contracts::{IncomingContract, OutgoingContract};
6use serde::{Deserialize, Serialize};
7use strum_macros::EnumIter;
8use tpe::DecryptionKeyShare;
9
10#[repr(u8)]
11#[derive(Clone, EnumIter, Debug)]
12pub enum DbKeyPrefix {
13    BlockCountVote = 0x41,
14    UnixTimeVote = 0x42,
15    IncomingContract = 0x43,
16    OutgoingContract = 0x44,
17    DecryptionKeyShare = 0x45,
18    Preimage = 0x46,
19    Gateway = 0x47,
20}
21
22impl std::fmt::Display for DbKeyPrefix {
23    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
24        write!(f, "{self:?}")
25    }
26}
27
28#[derive(Debug, Encodable, Decodable, Serialize)]
29pub struct BlockCountVoteKey(pub PeerId);
30
31#[derive(Clone, Debug, Encodable, Decodable)]
32pub struct BlockCountVotePrefix;
33
34impl_db_record!(
35    key = BlockCountVoteKey,
36    value = u64,
37    db_prefix = DbKeyPrefix::BlockCountVote,
38);
39
40impl_db_lookup!(key = BlockCountVoteKey, query_prefix = BlockCountVotePrefix);
41
42#[derive(Debug, Encodable, Decodable, Serialize)]
43pub struct UnixTimeVoteKey(pub PeerId);
44
45#[derive(Clone, Debug, Encodable, Decodable)]
46pub struct UnixTimeVotePrefix;
47
48impl_db_record!(
49    key = UnixTimeVoteKey,
50    value = u64,
51    db_prefix = DbKeyPrefix::UnixTimeVote,
52);
53
54impl_db_lookup!(key = UnixTimeVoteKey, query_prefix = UnixTimeVotePrefix);
55
56#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
57pub struct IncomingContractKey(pub ContractId);
58
59#[derive(Clone, Debug, Encodable, Decodable)]
60pub struct IncomingContractPrefix;
61
62impl_db_record!(
63    key = IncomingContractKey,
64    value = IncomingContract,
65    db_prefix = DbKeyPrefix::IncomingContract,
66    notify_on_modify = true
67);
68
69impl_db_lookup!(
70    key = IncomingContractKey,
71    query_prefix = IncomingContractPrefix
72);
73
74#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
75pub struct OutgoingContractKey(pub ContractId);
76
77#[derive(Clone, Debug, Encodable, Decodable)]
78pub struct OutgoingContractPrefix;
79
80impl_db_record!(
81    key = OutgoingContractKey,
82    value = OutgoingContract,
83    db_prefix = DbKeyPrefix::OutgoingContract,
84    notify_on_modify = true
85);
86
87impl_db_lookup!(
88    key = OutgoingContractKey,
89    query_prefix = OutgoingContractPrefix
90);
91
92#[derive(Debug, Encodable, Decodable, Serialize)]
93pub struct DecryptionKeyShareKey(pub ContractId);
94
95#[derive(Clone, Debug, Encodable, Decodable)]
96pub struct DecryptionKeySharePrefix;
97
98impl_db_record!(
99    key = DecryptionKeyShareKey,
100    value = DecryptionKeyShare,
101    db_prefix = DbKeyPrefix::DecryptionKeyShare,
102    notify_on_modify = true
103);
104
105impl_db_lookup!(
106    key = DecryptionKeyShareKey,
107    query_prefix = DecryptionKeySharePrefix
108);
109
110#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
111pub struct PreimageKey(pub ContractId);
112
113#[derive(Debug, Encodable, Decodable)]
114pub struct PreimagePrefix;
115
116impl_db_record!(
117    key = PreimageKey,
118    value = [u8; 32],
119    db_prefix = DbKeyPrefix::Preimage,
120    notify_on_modify = true
121);
122
123impl_db_lookup!(key = PreimageKey, query_prefix = PreimagePrefix);
124
125#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
126pub struct GatewayKey(pub SafeUrl);
127
128#[derive(Debug, Encodable, Decodable)]
129pub struct GatewayPrefix;
130
131impl_db_record!(
132    key = GatewayKey,
133    value = (),
134    db_prefix = DbKeyPrefix::Gateway,
135);
136
137impl_db_lookup!(key = GatewayKey, query_prefix = GatewayPrefix);