fedimint_lnv2_server/
db.rs

1use fedimint_core::encoding::{Decodable, Encodable};
2use fedimint_core::util::SafeUrl;
3use fedimint_core::{OutPoint, 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 = 0x01,
14    UnixTimeVote = 0x02,
15    IncomingContract = 0x03,
16    IncomingContractOutpoint = 0x04,
17    OutgoingContract = 0x05,
18    DecryptionKeyShare = 0x06,
19    Preimage = 0x07,
20    Gateway = 0x08,
21}
22
23impl std::fmt::Display for DbKeyPrefix {
24    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
25        write!(f, "{self:?}")
26    }
27}
28
29#[derive(Debug, Encodable, Decodable, Serialize)]
30pub struct BlockCountVoteKey(pub PeerId);
31
32#[derive(Clone, Debug, Encodable, Decodable)]
33pub struct BlockCountVotePrefix;
34
35impl_db_record!(
36    key = BlockCountVoteKey,
37    value = u64,
38    db_prefix = DbKeyPrefix::BlockCountVote,
39);
40
41impl_db_lookup!(key = BlockCountVoteKey, query_prefix = BlockCountVotePrefix);
42
43#[derive(Debug, Encodable, Decodable, Serialize)]
44pub struct UnixTimeVoteKey(pub PeerId);
45
46#[derive(Clone, Debug, Encodable, Decodable)]
47pub struct UnixTimeVotePrefix;
48
49impl_db_record!(
50    key = UnixTimeVoteKey,
51    value = u64,
52    db_prefix = DbKeyPrefix::UnixTimeVote,
53);
54
55impl_db_lookup!(key = UnixTimeVoteKey, query_prefix = UnixTimeVotePrefix);
56
57#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
58pub struct IncomingContractKey(pub OutPoint);
59
60#[derive(Clone, Debug, Encodable, Decodable)]
61pub struct IncomingContractPrefix;
62
63impl_db_record!(
64    key = IncomingContractKey,
65    value = IncomingContract,
66    db_prefix = DbKeyPrefix::IncomingContract,
67    notify_on_modify = true
68);
69
70impl_db_lookup!(
71    key = IncomingContractKey,
72    query_prefix = IncomingContractPrefix
73);
74
75#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
76pub struct IncomingContractOutpointKey(pub ContractId);
77
78#[derive(Clone, Debug, Encodable, Decodable)]
79pub struct IncomingContractOutpointPrefix;
80
81impl_db_record!(
82    key = IncomingContractOutpointKey,
83    value = OutPoint,
84    db_prefix = DbKeyPrefix::IncomingContractOutpoint,
85    notify_on_modify = true
86);
87
88impl_db_lookup!(
89    key = IncomingContractOutpointKey,
90    query_prefix = IncomingContractOutpointPrefix
91);
92
93#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
94pub struct OutgoingContractKey(pub OutPoint);
95
96#[derive(Clone, Debug, Encodable, Decodable)]
97pub struct OutgoingContractPrefix;
98
99impl_db_record!(
100    key = OutgoingContractKey,
101    value = OutgoingContract,
102    db_prefix = DbKeyPrefix::OutgoingContract,
103    notify_on_modify = true
104);
105
106impl_db_lookup!(
107    key = OutgoingContractKey,
108    query_prefix = OutgoingContractPrefix
109);
110
111#[derive(Debug, Encodable, Decodable, Serialize)]
112pub struct DecryptionKeyShareKey(pub OutPoint);
113
114#[derive(Clone, Debug, Encodable, Decodable)]
115pub struct DecryptionKeySharePrefix;
116
117impl_db_record!(
118    key = DecryptionKeyShareKey,
119    value = DecryptionKeyShare,
120    db_prefix = DbKeyPrefix::DecryptionKeyShare,
121    notify_on_modify = true
122);
123
124impl_db_lookup!(
125    key = DecryptionKeyShareKey,
126    query_prefix = DecryptionKeySharePrefix
127);
128
129#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
130pub struct PreimageKey(pub OutPoint);
131
132#[derive(Debug, Encodable, Decodable)]
133pub struct PreimagePrefix;
134
135impl_db_record!(
136    key = PreimageKey,
137    value = [u8; 32],
138    db_prefix = DbKeyPrefix::Preimage,
139    notify_on_modify = true
140);
141
142impl_db_lookup!(key = PreimageKey, query_prefix = PreimagePrefix);
143
144#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
145pub struct GatewayKey(pub SafeUrl);
146
147#[derive(Debug, Encodable, Decodable)]
148pub struct GatewayPrefix;
149
150impl_db_record!(
151    key = GatewayKey,
152    value = (),
153    db_prefix = DbKeyPrefix::Gateway,
154);
155
156impl_db_lookup!(key = GatewayKey, query_prefix = GatewayPrefix);