fedimint_meta_server/
db.rs

1use fedimint_core::encoding::{Decodable, Encodable};
2use fedimint_core::{PeerId, impl_db_lookup, impl_db_record};
3use fedimint_meta_common::{MetaConsensusValue, MetaKey, MetaValue};
4use serde::Serialize;
5use strum_macros::EnumIter;
6
7/// Namespaces DB keys for this module
8#[repr(u8)]
9#[derive(Clone, EnumIter, Debug)]
10pub enum DbKeyPrefix {
11    /// How we want to vote
12    ///
13    /// Private, not part of the consensus, but only local state.
14    Desired = 0x00,
15    /// Current consensuson
16    Consensus = 0x01,
17    /// Current submitted votes
18    Submissions = 0x02,
19}
20
21// TODO: Boilerplate-code
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, Clone, Encodable, Decodable, Eq, PartialEq, Hash, Serialize)]
29pub struct MetaDesiredKey(pub MetaKey);
30
31#[derive(Debug, Encodable, Decodable)]
32pub struct MetaDesiredKeyPrefix;
33
34#[derive(Debug, Encodable, Decodable, Serialize, PartialEq, Eq)]
35pub struct MetaDesiredValue {
36    // Since AlephBft will merge and not re-submit the exact same item twice within one session,
37    // changing submitted item in sequence `a -> b -> a` will simply ignore the second `a`.
38    // To avoid this behavior, an otherwise meaningless `salt` field is used.
39    pub salt: u64,
40    pub value: MetaValue,
41}
42
43impl_db_record!(
44    key = MetaDesiredKey,
45    value = MetaDesiredValue,
46    db_prefix = DbKeyPrefix::Desired,
47);
48impl_db_lookup!(key = MetaDesiredKey, query_prefix = MetaDesiredKeyPrefix,);
49
50#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash, Serialize)]
51pub struct MetaConsensusKey(pub MetaKey);
52
53#[derive(Debug, Encodable, Decodable)]
54pub struct MetaConsensusKeyPrefix;
55
56impl_db_record!(
57    key = MetaConsensusKey,
58    value = MetaConsensusValue,
59    db_prefix = DbKeyPrefix::Consensus,
60);
61impl_db_lookup!(
62    key = MetaConsensusKey,
63    query_prefix = MetaConsensusKeyPrefix,
64);
65
66#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash, Serialize)]
67pub struct MetaSubmissionsKey {
68    pub key: MetaKey,
69    pub peer_id: PeerId,
70}
71
72#[derive(Debug, Encodable, Decodable)]
73pub struct MetaSubmissionsKeyPrefix;
74
75#[derive(Debug, Encodable, Decodable)]
76pub struct MetaSubmissionsByKeyPrefix(pub MetaKey);
77
78#[derive(Debug, Encodable, Decodable, Serialize, PartialEq, Eq)]
79pub struct MetaSubmissionValue {
80    pub salt: u64,
81    pub value: MetaValue,
82}
83
84impl_db_record!(
85    key = MetaSubmissionsKey,
86    value = MetaSubmissionValue,
87    db_prefix = DbKeyPrefix::Submissions,
88);
89impl_db_lookup!(
90    key = MetaSubmissionsKey,
91    query_prefix = MetaSubmissionsKeyPrefix,
92);
93impl_db_lookup!(
94    key = MetaSubmissionsKey,
95    query_prefix = MetaSubmissionsByKeyPrefix,
96);