fedimint_meta_server/
db.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::{impl_db_lookup, impl_db_record, PeerId};
use fedimint_meta_common::{MetaConsensusValue, MetaKey, MetaValue};
use serde::Serialize;
use strum_macros::EnumIter;

/// Namespaces DB keys for this module
#[repr(u8)]
#[derive(Clone, EnumIter, Debug)]
pub enum DbKeyPrefix {
    /// How we want to vote
    ///
    /// Private, not part of the consensus, but only local state.
    Desired = 0x00,
    /// Current consensuson
    Consensus = 0x01,
    /// Current submitted votes
    Submissions = 0x02,
}

// TODO: Boilerplate-code
impl std::fmt::Display for DbKeyPrefix {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash, Serialize)]
pub struct MetaDesiredKey(pub MetaKey);

#[derive(Debug, Encodable, Decodable)]
pub struct MetaDesiredKeyPrefix;

#[derive(Debug, Encodable, Decodable, Serialize, PartialEq, Eq)]
pub struct MetaDesiredValue {
    // Since AlephBft will merge and not re-submit the exact same item twice within one session,
    // changing submitted item in sequence `a -> b -> a` will simply ignore the second `a`.
    // To avoid this behavior, an otherwise meaningless `salt` field is used.
    pub salt: u64,
    pub value: MetaValue,
}

impl_db_record!(
    key = MetaDesiredKey,
    value = MetaDesiredValue,
    db_prefix = DbKeyPrefix::Desired,
);
impl_db_lookup!(key = MetaDesiredKey, query_prefix = MetaDesiredKeyPrefix,);

#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash, Serialize)]
pub struct MetaConsensusKey(pub MetaKey);

#[derive(Debug, Encodable, Decodable)]
pub struct MetaConsensusKeyPrefix;

impl_db_record!(
    key = MetaConsensusKey,
    value = MetaConsensusValue,
    db_prefix = DbKeyPrefix::Consensus,
);
impl_db_lookup!(
    key = MetaConsensusKey,
    query_prefix = MetaConsensusKeyPrefix,
);

#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash, Serialize)]
pub struct MetaSubmissionsKey {
    pub key: MetaKey,
    pub peer_id: PeerId,
}

#[derive(Debug, Encodable, Decodable)]
pub struct MetaSubmissionsKeyPrefix;

#[derive(Debug, Encodable, Decodable)]
pub struct MetaSubmissionsByKeyPrefix(pub MetaKey);

#[derive(Debug, Encodable, Decodable, Serialize, PartialEq, Eq)]
pub struct MetaSubmissionValue {
    pub salt: u64,
    pub value: MetaValue,
}

impl_db_record!(
    key = MetaSubmissionsKey,
    value = MetaSubmissionValue,
    db_prefix = DbKeyPrefix::Submissions,
);
impl_db_lookup!(
    key = MetaSubmissionsKey,
    query_prefix = MetaSubmissionsKeyPrefix,
);
impl_db_lookup!(
    key = MetaSubmissionsKey,
    query_prefix = MetaSubmissionsByKeyPrefix,
);