devimint/federation/
config.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use bitcoincore_rpc::bitcoin::Network;
use fedimint_core::config::{EmptyGenParams, ServerModuleConfigGenParamsRegistry};
use fedimint_core::envs::{
    is_env_var_set, BitcoinRpcConfig, FM_DEVIMINT_DISABLE_MODULE_LNV2_ENV,
    FM_USE_UNKNOWN_MODULE_ENV,
};
use fedimint_core::module::ServerModuleInit as _;
use fedimint_ln_server::common::config::{
    LightningGenParams, LightningGenParamsConsensus, LightningGenParamsLocal,
};
use fedimint_ln_server::LightningInit;
use fedimint_meta_server::{MetaGenParams, MetaInit};
use fedimint_mint_server::common::config::{MintGenParams, MintGenParamsConsensus};
use fedimint_mint_server::MintInit;
use fedimint_unknown_server::common::config::UnknownGenParams;
use fedimint_unknown_server::UnknownInit;
use fedimint_wallet_client::config::{
    WalletGenParams, WalletGenParamsConsensus, WalletGenParamsLocal,
};
use fedimint_wallet_server::WalletInit;
use fedimintd::default_esplora_server;
use fedimintd::envs::FM_DISABLE_META_MODULE_ENV;
use legacy_types::{LegacyFeeConsensus, LegacyMintGenParams, LegacyMintGenParamsConsensus};

use crate::version_constants::VERSION_0_5_0_ALPHA;

/// Duplicate default fedimint module setup
pub fn attach_default_module_init_params(
    bitcoin_rpc: &BitcoinRpcConfig,
    module_init_params: &mut ServerModuleConfigGenParamsRegistry,
    network: Network,
    finality_delay: u32,
    fedimintd_version: &semver::Version,
) {
    module_init_params.attach_config_gen_params(
        LightningInit::kind(),
        LightningGenParams {
            local: LightningGenParamsLocal {
                bitcoin_rpc: bitcoin_rpc.clone(),
            },
            consensus: LightningGenParamsConsensus { network },
        },
    );

    // TODO(support:v0.4): v0.5 introduced relative fees for the mint module by
    // changing the field names for FeeConsensus. We need to support the old fields
    // in devimint since fedimint-cli uses JSON encoding for admin dkg
    if fedimintd_version >= &VERSION_0_5_0_ALPHA {
        module_init_params.attach_config_gen_params(
            MintInit::kind(),
            MintGenParams {
                local: EmptyGenParams::default(),
                consensus: MintGenParamsConsensus::new(
                    2,
                    fedimint_mint_common::config::FeeConsensus::zero(),
                ),
            },
        )
    } else {
        module_init_params.attach_config_gen_params(
            MintInit::kind(),
            LegacyMintGenParams {
                local: EmptyGenParams::default(),
                consensus: LegacyMintGenParamsConsensus::new(2, LegacyFeeConsensus::default()),
            },
        )
    };

    module_init_params.attach_config_gen_params(
        WalletInit::kind(),
        WalletGenParams {
            local: WalletGenParamsLocal {
                bitcoin_rpc: bitcoin_rpc.clone(),
            },
            consensus: WalletGenParamsConsensus {
                network,
                finality_delay,
                client_default_bitcoin_rpc: default_esplora_server(network),
                fee_consensus: fedimint_wallet_client::config::FeeConsensus::default(),
            },
        },
    );

    // TODO(support:v0.3): v0.5 introduced lnv2 modules, so we need to skip
    // attaching the module for old fedimintd versions
    if fedimintd_version >= &VERSION_0_5_0_ALPHA
        && !is_env_var_set(FM_DEVIMINT_DISABLE_MODULE_LNV2_ENV)
    {
        module_init_params.attach_config_gen_params(
            fedimint_lnv2_server::LightningInit::kind(),
            fedimint_lnv2_common::config::LightningGenParams {
                local: fedimint_lnv2_common::config::LightningGenParamsLocal {
                    bitcoin_rpc: bitcoin_rpc.clone(),
                },
                consensus: fedimint_lnv2_common::config::LightningGenParamsConsensus {
                    fee_consensus: fedimint_lnv2_common::config::FeeConsensus::new(1000)
                        .expect("Relative fee is within range"),
                    network,
                },
            },
        );
    }

    if !is_env_var_set(FM_DISABLE_META_MODULE_ENV) {
        module_init_params.attach_config_gen_params(MetaInit::kind(), MetaGenParams::default());
    }

    if is_env_var_set(FM_USE_UNKNOWN_MODULE_ENV) {
        module_init_params
            .attach_config_gen_params(UnknownInit::kind(), UnknownGenParams::default());
    }
}

mod legacy_types {
    use fedimint_core::config::EmptyGenParams;
    use fedimint_core::encoding::{Decodable, Encodable};
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct LegacyMintGenParams {
        pub local: EmptyGenParams,
        pub consensus: LegacyMintGenParamsConsensus,
    }

    #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, Encodable, Decodable)]
    pub struct LegacyFeeConsensus {
        pub note_issuance_abs: fedimint_core::Amount,
        pub note_spend_abs: fedimint_core::Amount,
    }

    impl Default for LegacyFeeConsensus {
        fn default() -> Self {
            Self {
                note_issuance_abs: fedimint_core::Amount::ZERO,
                note_spend_abs: fedimint_core::Amount::ZERO,
            }
        }
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct LegacyMintGenParamsConsensus {
        denomination_base: u16,
        fee_consensus: LegacyFeeConsensus,
    }

    impl LegacyMintGenParamsConsensus {
        pub fn new(denomination_base: u16, fee_consensus: LegacyFeeConsensus) -> Self {
            Self {
                denomination_base,
                fee_consensus,
            }
        }
    }

    impl fedimint_core::config::ModuleInitParams for LegacyMintGenParams {
        type Local = EmptyGenParams;
        type Consensus = LegacyMintGenParamsConsensus;

        fn from_parts(local: Self::Local, consensus: Self::Consensus) -> Self {
            Self { local, consensus }
        }

        fn to_parts(self) -> (Self::Local, Self::Consensus) {
            (self.local, self.consensus)
        }
    }
}