fedimint_mint_common/
config.rs

1use std::collections::BTreeMap;
2
3use fedimint_core::config::EmptyGenParams;
4use fedimint_core::core::ModuleKind;
5use fedimint_core::encoding::{Decodable, Encodable};
6use fedimint_core::module::serde_json;
7use fedimint_core::{Amount, PeerId, Tiered, plugin_types_trait_impl_config};
8use serde::{Deserialize, Serialize};
9use tbs::{AggregatePublicKey, PublicKeyShare};
10
11use crate::MintCommonInit;
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct MintGenParams {
15    pub local: EmptyGenParams,
16    pub consensus: MintGenParamsConsensus,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct MintGenParamsConsensus {
21    denomination_base: u16,
22    fee_consensus: FeeConsensus,
23}
24
25// The maximum size of an E-Cash note (1,000,000 coins)
26// Changing this value is considered a breaking change because it is not saved
27// in `MintGenParamsConsensus` but instead is hardcoded here
28const MAX_DENOMINATION_SIZE: Amount = Amount::from_bitcoins(1_000_000);
29
30impl MintGenParamsConsensus {
31    pub fn new(denomination_base: u16, fee_consensus: FeeConsensus) -> Self {
32        Self {
33            denomination_base,
34            fee_consensus,
35        }
36    }
37
38    pub fn denomination_base(&self) -> u16 {
39        self.denomination_base
40    }
41
42    pub fn fee_consensus(&self) -> FeeConsensus {
43        self.fee_consensus.clone()
44    }
45
46    pub fn gen_denominations(&self) -> Vec<Amount> {
47        Tiered::gen_denominations(self.denomination_base, MAX_DENOMINATION_SIZE)
48            .tiers()
49            .copied()
50            .collect()
51    }
52}
53
54#[derive(Clone, Debug, Serialize, Deserialize)]
55pub struct MintConfig {
56    pub private: MintConfigPrivate,
57    pub consensus: MintConfigConsensus,
58}
59
60#[derive(Clone, Debug, Serialize, Deserialize, Decodable, Encodable)]
61pub struct MintConfigLocal;
62
63#[derive(Clone, Debug, Serialize, Deserialize, Encodable, Decodable)]
64pub struct MintConfigConsensus {
65    /// The set of public keys for blind-signing all peers and note
66    /// denominations
67    pub peer_tbs_pks: BTreeMap<PeerId, Tiered<PublicKeyShare>>,
68    /// Fees charged for ecash transactions
69    pub fee_consensus: FeeConsensus,
70    /// The maximum amount of change a client can request
71    pub max_notes_per_denomination: u16,
72}
73
74#[derive(Clone, Debug, Serialize, Deserialize)]
75pub struct MintConfigPrivate {
76    /// Secret keys for blind-signing ecash of varying note denominations
77    pub tbs_sks: Tiered<tbs::SecretKeyShare>,
78}
79
80#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Encodable, Decodable, Hash)]
81pub struct MintClientConfig {
82    pub tbs_pks: Tiered<AggregatePublicKey>,
83    pub fee_consensus: FeeConsensus,
84    pub peer_tbs_pks: BTreeMap<PeerId, Tiered<tbs::PublicKeyShare>>,
85    pub max_notes_per_denomination: u16,
86}
87
88impl std::fmt::Display for MintClientConfig {
89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90        write!(
91            f,
92            "MintClientConfig {}",
93            serde_json::to_string(self).map_err(|_e| std::fmt::Error)?
94        )
95    }
96}
97
98// Wire together the configs for this module
99plugin_types_trait_impl_config!(
100    MintCommonInit,
101    MintGenParams,
102    EmptyGenParams,
103    MintGenParamsConsensus,
104    MintConfig,
105    MintConfigPrivate,
106    MintConfigConsensus,
107    MintClientConfig
108);
109
110#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, Encodable, Decodable)]
111pub struct FeeConsensus {
112    base: Amount,
113    parts_per_million: u64,
114}
115
116impl FeeConsensus {
117    /// The mint module will charge a non-configurable base fee of one hundred
118    /// millisatoshis per transaction input and output to account for the costs
119    /// incurred by the federation for processing the transaction. On top of
120    /// that the federation may charge a additional relative fee per input and
121    /// output of up to one thousand parts per million which is equal to one
122    /// tenth of one percent.
123    ///
124    /// # Errors
125    /// - This constructor returns an error if the relative fee is in excess of
126    ///   one thousand parts per million.
127    pub fn new(parts_per_million: u64) -> anyhow::Result<Self> {
128        anyhow::ensure!(
129            parts_per_million <= 1_000,
130            "Relative fee over one thousand parts per million is excessive"
131        );
132
133        Ok(Self {
134            base: Amount::from_msats(100),
135            parts_per_million,
136        })
137    }
138
139    pub fn zero() -> Self {
140        Self {
141            base: Amount::ZERO,
142            parts_per_million: 0,
143        }
144    }
145
146    pub fn fee(&self, amount: Amount) -> Amount {
147        Amount::from_msats(self.fee_msats(amount.msats))
148    }
149
150    fn fee_msats(&self, msats: u64) -> u64 {
151        msats
152            .saturating_mul(self.parts_per_million)
153            .saturating_div(1_000_000)
154            .checked_add(self.base.msats)
155            .expect("The division creates sufficient headroom to add the base fee")
156    }
157}
158
159#[test]
160fn test_fee_consensus() {
161    let fee_consensus = FeeConsensus::new(1_000).expect("Relative fee is within range");
162
163    assert_eq!(
164        fee_consensus.fee(Amount::from_msats(999)),
165        Amount::from_msats(100)
166    );
167
168    assert_eq!(
169        fee_consensus.fee(Amount::from_sats(1)),
170        Amount::from_msats(100) + Amount::from_msats(1)
171    );
172
173    assert_eq!(
174        fee_consensus.fee(Amount::from_sats(1000)),
175        Amount::from_sats(1) + Amount::from_msats(100)
176    );
177
178    assert_eq!(
179        fee_consensus.fee(Amount::from_bitcoins(1)),
180        Amount::from_sats(100_000) + Amount::from_msats(100)
181    );
182
183    assert_eq!(
184        fee_consensus.fee(Amount::from_bitcoins(100_000)),
185        Amount::from_bitcoins(100) + Amount::from_msats(100)
186    );
187}