Skip to main content

fedimint_empty_client/
lib.rs

1#![deny(clippy::pedantic)]
2#![allow(clippy::module_name_repetitions)]
3
4use std::collections::BTreeMap;
5
6use db::DbKeyPrefix;
7use fedimint_client_module::db::ClientModuleMigrationFn;
8use fedimint_client_module::error::ClientModuleError;
9use fedimint_client_module::module::init::{ClientModuleInit, ClientModuleInitArgs};
10use fedimint_client_module::module::recovery::NoModuleBackup;
11use fedimint_client_module::module::{ClientContext, ClientModule, IClientModule};
12use fedimint_client_module::sm::Context;
13use fedimint_core::core::{Account, Decoder, ModuleKind};
14use fedimint_core::db::{Database, DatabaseTransaction, DatabaseVersion};
15use fedimint_core::module::{
16    AmountUnit, Amounts, ApiVersion, ModuleCommon, ModuleInit, MultiApiVersion,
17};
18use fedimint_core::{Amount, apply, async_trait_maybe_send};
19pub use fedimint_empty_common as common;
20use fedimint_empty_common::config::EmptyClientConfig;
21use fedimint_empty_common::{EmptyCommonInit, EmptyModuleTypes};
22use states::EmptyStateMachine;
23use strum::IntoEnumIterator;
24
25pub mod api;
26pub mod db;
27pub mod states;
28
29#[derive(Debug)]
30pub struct EmptyClientModule {
31    #[allow(dead_code)]
32    cfg: EmptyClientConfig,
33    #[allow(dead_code)]
34    client_ctx: ClientContext<Self>,
35    #[allow(dead_code)]
36    db: Database,
37}
38
39/// Data needed by the state machine
40#[derive(Debug, Clone)]
41pub struct EmptyClientContext {
42    pub empty_decoder: Decoder,
43}
44
45// TODO: Boiler-plate
46impl Context for EmptyClientContext {
47    const KIND: Option<ModuleKind> = None;
48}
49
50#[apply(async_trait_maybe_send!)]
51impl ClientModule for EmptyClientModule {
52    type Init = EmptyClientInit;
53    type Common = EmptyModuleTypes;
54    type Backup = NoModuleBackup;
55    type ModuleStateMachineContext = EmptyClientContext;
56    type States = EmptyStateMachine;
57
58    fn context(&self) -> Self::ModuleStateMachineContext {
59        EmptyClientContext {
60            empty_decoder: self.decoder(),
61        }
62    }
63
64    fn input_fee(
65        &self,
66        _amount: &Amounts,
67        _input: &<Self::Common as ModuleCommon>::Input,
68    ) -> Option<Amounts> {
69        unreachable!()
70    }
71
72    fn output_fee(
73        &self,
74        _amount: &Amounts,
75        _output: &<Self::Common as ModuleCommon>::Output,
76    ) -> Option<Amounts> {
77        unreachable!()
78    }
79
80    async fn get_balance(
81        &self,
82        _dbtx: &mut DatabaseTransaction<'_>,
83        _unit: AmountUnit,
84        _account: Account,
85    ) -> Amount {
86        Amount::ZERO
87    }
88}
89
90#[derive(Debug, Clone)]
91pub struct EmptyClientInit;
92
93// TODO: Boilerplate-code
94impl ModuleInit for EmptyClientInit {
95    type Common = EmptyCommonInit;
96
97    async fn dump_database(
98        &self,
99        _dbtx: &mut DatabaseTransaction<'_>,
100        prefix_names: Vec<String>,
101    ) -> Box<dyn Iterator<Item = (String, Box<dyn erased_serde::Serialize + Send>)> + '_> {
102        let items: BTreeMap<String, Box<dyn erased_serde::Serialize + Send>> = BTreeMap::new();
103        let filtered_prefixes = DbKeyPrefix::iter().filter(|f| {
104            prefix_names.is_empty() || prefix_names.contains(&f.to_string().to_lowercase())
105        });
106
107        #[allow(clippy::never_loop)]
108        for table in filtered_prefixes {
109            match table {}
110        }
111
112        Box::new(items.into_iter())
113    }
114}
115
116/// Generates the client module
117#[apply(async_trait_maybe_send!)]
118impl ClientModuleInit for EmptyClientInit {
119    type Module = EmptyClientModule;
120
121    fn supported_api_versions(&self) -> MultiApiVersion {
122        MultiApiVersion::try_from_iter([ApiVersion { major: 0, minor: 0 }])
123            .expect("no version conflicts")
124    }
125
126    async fn init(
127        &self,
128        args: &ClientModuleInitArgs<Self>,
129    ) -> Result<Self::Module, ClientModuleError> {
130        Ok(EmptyClientModule {
131            cfg: args.cfg().clone(),
132            client_ctx: args.context(),
133            db: args.db().clone(),
134        })
135    }
136
137    fn get_database_migrations(&self) -> BTreeMap<DatabaseVersion, ClientModuleMigrationFn> {
138        BTreeMap::new()
139    }
140}