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