dubp_currency_params/
lib.rs

1//  Copyright (C) 2020  Éloïs SANCHEZ.
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16//! Duniter protocol currency parameters
17
18#![deny(
19    clippy::unwrap_used,
20    missing_docs,
21    missing_debug_implementations,
22    missing_copy_implementations,
23    trivial_casts,
24    trivial_numeric_casts,
25    unsafe_code,
26    unstable_features,
27    unused_import_braces
28)]
29
30pub mod constants;
31pub mod genesis_block_params;
32
33pub use dubp_common::prelude::*;
34
35// Crate imports
36pub(crate) use crate::constants::*;
37pub(crate) use crate::genesis_block_params::v10::BlockV10Parameters;
38pub(crate) use serde::{Deserialize, Serialize};
39pub(crate) use thiserror::Error;
40
41#[derive(Copy, Clone, Debug, PartialEq)]
42/// Currency parameters
43pub struct CurrencyParameters {
44    /// Protocol version
45    pub protocol_version: usize,
46    /// UD target growth rate (see Relative Theorie of Money)
47    pub c: f64,
48    /// Duration between the creation of two UD (in seconds)
49    pub dt: u64,
50    /// Amount of the initial UD
51    pub ud0: usize,
52    /// Minimum duration between the writing of 2 certifications from the same issuer (in seconds)
53    pub sig_period: u64,
54    /// Minimum duration between two renewals of the same certification
55    pub sig_renew_period: u64,
56    /// Maximum number of active certifications at the same time (for the same issuer)
57    pub sig_stock: usize,
58    /// Maximum retention period of a pending certification
59    pub sig_window: u64,
60    /// Time to expiry of written certification
61    pub sig_validity: u64,
62    /// Minimum number of certifications required to become a member
63    pub sig_qty: usize,
64    /// Maximum retention period of a pending identity
65    pub idty_window: u64,
66    /// Maximum retention period of a pending membership
67    pub ms_window: u64,
68    /// Maximum retention period of a pending transaction
69    pub tx_window: u64,
70    /// Percentage of referring members who must be within step_max steps of each member
71    pub x_percent: f64,
72    /// Time to expiry of written membership
73    pub ms_validity: u64,
74    /// Minimum duration between the writing of 2 memberships from the same issuer (in seconds)
75    pub ms_period: u64,
76    /// For a member to respect the distance rule,
77    /// there must exist for more than x_percent % of the referring members
78    /// a path of less than step_max steps from the referring member to the evaluated member.
79    pub step_max: usize,
80    /// Number of blocks used for calculating median time.
81    pub median_time_blocks: usize,
82    /// The average time for writing 1 block (wished time)
83    pub avg_gen_time: u64,
84    /// The number of blocks required to evaluate again PoWMin value
85    pub dt_diff_eval: usize,
86    /// The percent of previous issuers to reach for personalized difficulty
87    pub percent_rot: f64,
88    /// Time of first UD.
89    pub ud_time0: u64,
90    /// Time of first reevaluation of the UD.
91    pub ud_reeval_time0: u64,
92    /// Time period between two re-evaluation of the UD.
93    pub dt_reeval: u64,
94    /// Maximum roolback length
95    pub fork_window_size: usize,
96}
97
98impl From<(&CurrencyName, BlockV10Parameters)> for CurrencyParameters {
99    fn from(source: (&CurrencyName, BlockV10Parameters)) -> CurrencyParameters {
100        let (currency_name, block_params) = source;
101        let sig_renew_period = match currency_name.0.as_str() {
102            DEFAULT_CURRENCY => *DEFAULT_SIG_RENEW_PERIOD,
103            "g1" => 5_259_600,
104            "g1-test" => 5_259_600 / 5,
105            _ => *DEFAULT_SIG_RENEW_PERIOD,
106        };
107        let ms_period = match currency_name.0.as_str() {
108            DEFAULT_CURRENCY => *DEFAULT_MS_PERIOD,
109            "g1" => 5_259_600,
110            "g1-test" => 5_259_600 / 5,
111            _ => *DEFAULT_MS_PERIOD,
112        };
113        let tx_window = match currency_name.0.as_str() {
114            DEFAULT_CURRENCY => *DEFAULT_TX_WINDOW,
115            "g1" => 604_800,
116            "g1-test" => 604_800,
117            _ => *DEFAULT_TX_WINDOW,
118        };
119        let fork_window_size = match currency_name.0.as_str() {
120            DEFAULT_CURRENCY => *DEFAULT_FORK_WINDOW_SIZE,
121            "g1" => 100,
122            "g1-test" => 100,
123            _ => *DEFAULT_FORK_WINDOW_SIZE,
124        };
125        CurrencyParameters {
126            protocol_version: 10,
127            c: block_params.c,
128            dt: block_params.dt,
129            ud0: block_params.ud0,
130            sig_period: block_params.sig_period,
131            sig_renew_period,
132            sig_stock: block_params.sig_stock,
133            sig_window: block_params.sig_window,
134            sig_validity: block_params.sig_validity,
135            sig_qty: block_params.sig_qty,
136            idty_window: block_params.idty_window,
137            ms_window: block_params.ms_window,
138            tx_window,
139            x_percent: block_params.x_percent,
140            ms_validity: block_params.ms_validity,
141            ms_period,
142            step_max: block_params.step_max,
143            median_time_blocks: block_params.median_time_blocks,
144            avg_gen_time: block_params.avg_gen_time,
145            dt_diff_eval: block_params.dt_diff_eval,
146            percent_rot: block_params.percent_rot,
147            ud_time0: block_params.ud_time0,
148            ud_reeval_time0: block_params.ud_reeval_time0,
149            dt_reeval: block_params.dt_reeval,
150            fork_window_size,
151        }
152    }
153}
154
155impl CurrencyParameters {
156    /// Get max value of connectivity (=1/x_percent)
157    pub fn max_connectivity(&self) -> f64 {
158        1.0 / self.x_percent
159    }
160}