fedimint_server/consensus/
db.rs1use std::collections::BTreeMap;
2use std::fmt::Debug;
3
4use fedimint_core::core::ModuleInstanceId;
5use fedimint_core::db::{DatabaseTransaction, DatabaseVersion, IDatabaseTransactionOpsCoreTyped};
6use fedimint_core::encoding::{Decodable, Encodable};
7use fedimint_core::session_outcome::{AcceptedItem, ConsensusItem, SignedSessionOutcome};
8use fedimint_core::util::BoxStream;
9use fedimint_core::{
10 OutPoint, TransactionId, apply, async_trait_maybe_send, impl_db_lookup, impl_db_record,
11};
12use fedimint_server_core::migration::{
13 DynModuleHistoryItem, DynServerDbMigrationFn, IServerDbMigrationContext,
14};
15use futures::StreamExt;
16use serde::Serialize;
17
18use crate::consensus::peer_availability::PeerAvailabilityState;
19use crate::db::DbKeyPrefix;
20
21#[derive(Clone, Debug, Encodable, Decodable)]
22pub struct AcceptedItemKey(pub u64);
23
24#[derive(Clone, Debug, Encodable, Decodable)]
25pub struct AcceptedItemPrefix;
26
27impl_db_record!(
28 key = AcceptedItemKey,
29 value = AcceptedItem,
30 db_prefix = DbKeyPrefix::AcceptedItem,
31 notify_on_modify = true,
32);
33impl_db_lookup!(key = AcceptedItemKey, query_prefix = AcceptedItemPrefix);
34
35#[derive(Debug, Encodable, Decodable, Serialize)]
36pub struct AcceptedTransactionKey(pub TransactionId);
37
38#[derive(Debug, Encodable, Decodable)]
39pub struct AcceptedTransactionKeyPrefix;
40
41impl_db_record!(
42 key = AcceptedTransactionKey,
43 value = Vec<ModuleInstanceId>,
44 db_prefix = DbKeyPrefix::AcceptedTransaction,
45 notify_on_modify = true,
46);
47impl_db_lookup!(
48 key = AcceptedTransactionKey,
49 query_prefix = AcceptedTransactionKeyPrefix
50);
51
52#[derive(Debug, Encodable, Decodable)]
53pub struct SignedSessionOutcomeKey(pub u64);
54
55#[derive(Debug, Encodable, Decodable)]
56pub struct SignedSessionOutcomePrefix;
57
58impl_db_record!(
59 key = SignedSessionOutcomeKey,
60 value = SignedSessionOutcome,
61 db_prefix = DbKeyPrefix::SignedSessionOutcome,
62 notify_on_modify = true,
63);
64impl_db_lookup!(
65 key = SignedSessionOutcomeKey,
66 query_prefix = SignedSessionOutcomePrefix
67);
68
69#[derive(Debug, Encodable, Decodable)]
70pub struct PeerAvailabilityKey;
71
72#[derive(Debug, Encodable, Decodable)]
74pub struct PeerAvailabilityResetsKey;
75
76impl_db_record!(
77 key = PeerAvailabilityResetsKey,
78 value = BTreeMap<fedimint_core::PeerId, PeerAvailabilityState>,
79 db_prefix = DbKeyPrefix::PeerAvailabilityResets,
80 notify_on_modify = false,
81);
82
83impl_db_record!(
84 key = PeerAvailabilityKey,
85 value = PeerAvailabilityState,
86 db_prefix = DbKeyPrefix::PeerAvailability,
87 notify_on_modify = false,
88);
89
90#[derive(Debug, Encodable, Decodable)]
91pub struct AlephUnitsKey(pub u64);
92
93#[derive(Debug, Encodable, Decodable)]
94pub struct AlephUnitsPrefix;
95
96impl_db_record!(
97 key = AlephUnitsKey,
98 value = Vec<u8>,
99 db_prefix = DbKeyPrefix::AlephUnits,
100 notify_on_modify = false,
101);
102impl_db_lookup!(key = AlephUnitsKey, query_prefix = AlephUnitsPrefix);
103
104pub fn get_global_database_migrations() -> BTreeMap<DatabaseVersion, DynServerDbMigrationFn> {
105 BTreeMap::new()
106}
107
108pub struct ServerDbMigrationContext;
111
112#[apply(async_trait_maybe_send!)]
113impl IServerDbMigrationContext for ServerDbMigrationContext {
114 async fn get_module_history_stream<'s, 'tx>(
115 &'s self,
116 module_instance_id: ModuleInstanceId,
117 dbtx: &'s mut DatabaseTransaction<'tx>,
118 ) -> BoxStream<'s, DynModuleHistoryItem>
119 where
120 'tx: 's,
121 {
122 dbtx.ensure_global().expect("Dbtx must be global");
123
124 let active_session_items = dbtx
128 .find_by_prefix(&AcceptedItemPrefix)
129 .await
130 .map(|(_, item)| item)
131 .collect::<Vec<_>>()
132 .await;
133
134 let stream =
135 dbtx.find_by_prefix(&SignedSessionOutcomePrefix)
136 .await
137 .flat_map(|(_, signed_session_outcome): (_, SignedSessionOutcome)| {
139 futures::stream::iter(signed_session_outcome.session_outcome.items)
140 })
141 .chain(futures::stream::iter(active_session_items))
144 .flat_map(move |item| {
145 let history_items =
146 match item.item {
147 ConsensusItem::Transaction(tx) => {
148 let txid = tx.tx_hash();
149 let input_items = tx.inputs.into_iter().filter_map(|input| {
150 (input.module_instance_id() == module_instance_id)
151 .then_some(DynModuleHistoryItem::Input(input))
152 });
153
154 let output_items = tx.outputs.into_iter().zip(0..).filter_map(
155 |(output, out_idx)| {
156 (output.module_instance_id() == module_instance_id)
157 .then_some(DynModuleHistoryItem::Output(
158 output,
159 OutPoint { txid, out_idx },
160 ))
161 },
162 );
163
164 input_items.chain(output_items).collect::<Vec<_>>()
165 }
166 ConsensusItem::Module(mci) => {
167 if mci.module_instance_id() == module_instance_id {
168 vec![DynModuleHistoryItem::ConsensusItem(mci)]
169 } else {
170 vec![]
171 }
172 }
173 ConsensusItem::Default { .. } => {
174 unreachable!("We never save unknown CIs on the server side")
175 }
176 };
177 futures::stream::iter(history_items)
178 });
179
180 Box::pin(stream)
181 }
182}