fedimint_unknown_server/
lib.rs

1#![deny(clippy::pedantic)]
2#![allow(clippy::module_name_repetitions)]
3#![allow(clippy::must_use_candidate)]
4
5use std::collections::BTreeMap;
6
7use anyhow::bail;
8use async_trait::async_trait;
9use fedimint_core::config::{
10    ConfigGenModuleParams, ServerModuleConfig, ServerModuleConsensusConfig,
11    TypedServerModuleConfig, TypedServerModuleConsensusConfig,
12};
13use fedimint_core::core::ModuleInstanceId;
14use fedimint_core::db::{DatabaseTransaction, DatabaseVersion};
15use fedimint_core::module::audit::Audit;
16use fedimint_core::module::{
17    ApiEndpoint, CORE_CONSENSUS_VERSION, CoreConsensusVersion, InputMeta, ModuleConsensusVersion,
18    ModuleInit, SupportedModuleApiVersions, TransactionItemAmount,
19};
20use fedimint_core::{InPoint, OutPoint, PeerId};
21use fedimint_server_core::config::PeerHandleOps;
22use fedimint_server_core::migration::ServerModuleDbMigrationFn;
23use fedimint_server_core::{ServerModule, ServerModuleInit, ServerModuleInitArgs};
24pub use fedimint_unknown_common as common;
25use fedimint_unknown_common::config::{
26    UnknownClientConfig, UnknownConfig, UnknownConfigConsensus, UnknownConfigPrivate,
27    UnknownGenParams,
28};
29use fedimint_unknown_common::{
30    MODULE_CONSENSUS_VERSION, UnknownCommonInit, UnknownConsensusItem, UnknownInput,
31    UnknownInputError, UnknownModuleTypes, UnknownOutput, UnknownOutputError, UnknownOutputOutcome,
32};
33pub mod db;
34
35/// Generates the module
36#[derive(Debug, Clone)]
37pub struct UnknownInit;
38
39// TODO: Boilerplate-code
40impl ModuleInit for UnknownInit {
41    type Common = UnknownCommonInit;
42
43    /// Dumps all database items for debugging
44    async fn dump_database(
45        &self,
46        _dbtx: &mut DatabaseTransaction<'_>,
47        _prefix_names: Vec<String>,
48    ) -> Box<dyn Iterator<Item = (String, Box<dyn erased_serde::Serialize + Send>)> + '_> {
49        Box::new(vec![].into_iter())
50    }
51}
52
53/// Implementation of server module non-consensus functions
54#[async_trait]
55impl ServerModuleInit for UnknownInit {
56    type Module = Unknown;
57    type Params = UnknownGenParams;
58
59    /// Returns the version of this module
60    fn versions(&self, _core: CoreConsensusVersion) -> &[ModuleConsensusVersion] {
61        &[MODULE_CONSENSUS_VERSION]
62    }
63
64    fn supported_api_versions(&self) -> SupportedModuleApiVersions {
65        SupportedModuleApiVersions::from_raw(
66            (CORE_CONSENSUS_VERSION.major, CORE_CONSENSUS_VERSION.minor),
67            (
68                MODULE_CONSENSUS_VERSION.major,
69                MODULE_CONSENSUS_VERSION.minor,
70            ),
71            &[(0, 0)],
72        )
73    }
74
75    /// Initialize the module
76    async fn init(&self, args: &ServerModuleInitArgs<Self>) -> anyhow::Result<Self::Module> {
77        Ok(Unknown::new(args.cfg().to_typed()?))
78    }
79
80    /// Generates configs for all peers in a trusted manner for testing
81    fn trusted_dealer_gen(
82        &self,
83        peers: &[PeerId],
84        params: &ConfigGenModuleParams,
85    ) -> BTreeMap<PeerId, ServerModuleConfig> {
86        let _params = self.parse_params(params).unwrap();
87        // Generate a config for each peer
88        peers
89            .iter()
90            .map(|&peer| {
91                let config = UnknownConfig {
92                    private: UnknownConfigPrivate,
93                    consensus: UnknownConfigConsensus {},
94                };
95                (peer, config.to_erased())
96            })
97            .collect()
98    }
99
100    /// Generates configs for all peers in an untrusted manner
101    async fn distributed_gen(
102        &self,
103        _peers: &(dyn PeerHandleOps + Send + Sync),
104        params: &ConfigGenModuleParams,
105    ) -> anyhow::Result<ServerModuleConfig> {
106        let _params = self.parse_params(params).unwrap();
107
108        Ok(UnknownConfig {
109            private: UnknownConfigPrivate,
110            consensus: UnknownConfigConsensus {},
111        }
112        .to_erased())
113    }
114
115    /// Converts the consensus config into the client config
116    fn get_client_config(
117        &self,
118        config: &ServerModuleConsensusConfig,
119    ) -> anyhow::Result<UnknownClientConfig> {
120        let _config = UnknownConfigConsensus::from_erased(config)?;
121        Ok(UnknownClientConfig {})
122    }
123
124    fn validate_config(
125        &self,
126        _identity: &PeerId,
127        _config: ServerModuleConfig,
128    ) -> anyhow::Result<()> {
129        Ok(())
130    }
131
132    /// DB migrations to move from old to newer versions
133    fn get_database_migrations(
134        &self,
135    ) -> BTreeMap<DatabaseVersion, ServerModuleDbMigrationFn<Unknown>> {
136        let mut migrations: BTreeMap<DatabaseVersion, ServerModuleDbMigrationFn<_>> =
137            BTreeMap::new();
138        // Unknown module prior to v0.5.0 had a `DATABASE_VERSION` of 1, so we must
139        // insert a no-op migration to ensure that upgrades work.
140        migrations.insert(DatabaseVersion(0), Box::new(|_| Box::pin(async { Ok(()) })));
141        migrations
142    }
143}
144
145/// Unknown module
146#[derive(Debug)]
147pub struct Unknown {
148    pub cfg: UnknownConfig,
149}
150
151/// Implementation of consensus for the server module
152#[async_trait]
153impl ServerModule for Unknown {
154    /// Define the consensus types
155    type Common = UnknownModuleTypes;
156    type Init = UnknownInit;
157
158    async fn consensus_proposal(
159        &self,
160        _dbtx: &mut DatabaseTransaction<'_>,
161    ) -> Vec<UnknownConsensusItem> {
162        Vec::new()
163    }
164
165    async fn process_consensus_item<'a, 'b>(
166        &'a self,
167        _dbtx: &mut DatabaseTransaction<'b>,
168        _consensus_item: UnknownConsensusItem,
169        _peer_id: PeerId,
170    ) -> anyhow::Result<()> {
171        // WARNING: `process_consensus_item` should return an `Err` for items that do
172        // not change any internal consensus state. Failure to do so, will result in an
173        // (potentially significantly) increased consensus history size.
174        // If you are using this code as a template,
175        // make sure to read the [`ServerModule::process_consensus_item`] documentation,
176        bail!("The unknown module does not use consensus items");
177    }
178
179    async fn process_input<'a, 'b, 'c>(
180        &'a self,
181        _dbtx: &mut DatabaseTransaction<'c>,
182        _input: &'b UnknownInput,
183        _in_point: InPoint,
184    ) -> Result<InputMeta, UnknownInputError> {
185        unreachable!();
186    }
187
188    async fn process_output<'a, 'b>(
189        &'a self,
190        _dbtx: &mut DatabaseTransaction<'b>,
191        _output: &'a UnknownOutput,
192        _out_point: OutPoint,
193    ) -> Result<TransactionItemAmount, UnknownOutputError> {
194        unreachable!();
195    }
196
197    async fn output_status(
198        &self,
199        _dbtx: &mut DatabaseTransaction<'_>,
200        _out_point: OutPoint,
201    ) -> Option<UnknownOutputOutcome> {
202        unreachable!()
203    }
204
205    async fn audit(
206        &self,
207        _dbtx: &mut DatabaseTransaction<'_>,
208        _audit: &mut Audit,
209        _module_instance_id: ModuleInstanceId,
210    ) {
211    }
212
213    fn api_endpoints(&self) -> Vec<ApiEndpoint<Self>> {
214        Vec::new()
215    }
216}
217
218impl Unknown {
219    /// Create new module instance
220    pub fn new(cfg: UnknownConfig) -> Unknown {
221        Unknown { cfg }
222    }
223}