dubp_common/currency_params/genesis_block_params/
v10.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 in genesis block v10
17
18use crate::*;
19
20/// Currency parameters
21#[derive(Debug, Copy, Clone, Deserialize, Serialize, PartialEq)]
22pub struct BlockV10Parameters {
23    /// UD target growth rate (see Relative Theorie of Money)
24    pub c: f64,
25    /// Duration between the creation of two UD (in seconds)
26    pub dt: u64,
27    /// Amount of the initial UD
28    pub ud0: usize,
29    /// Minimum duration between the writing of 2 certifications from the same issuer (in seconds)
30    pub sig_period: u64,
31    /// Maximum number of active certifications at the same time (for the same issuer)
32    pub sig_stock: usize,
33    /// Maximum retention period of a pending certification
34    pub sig_window: u64,
35    /// Time to expiry of written certification
36    pub sig_validity: u64,
37    /// Minimum number of certifications required to become a member
38    pub sig_qty: usize,
39    /// Maximum retention period of a pending identity
40    pub idty_window: u64,
41    /// Maximum retention period of a pending membership
42    pub ms_window: u64,
43    /// Percentage of referring members who must be within step_max steps of each member
44    pub x_percent: f64,
45    /// Time to expiry of written membership
46    pub ms_validity: u64,
47    /// For a member to respect the distance rule,
48    /// there must exist for more than x_percent % of the referring members
49    /// a path of less than step_max steps from the referring member to the evaluated member.
50    pub step_max: usize,
51    /// Number of blocks used for calculating median time.
52    pub median_time_blocks: usize,
53    /// The average time for writing 1 block (wished time)
54    pub avg_gen_time: u64,
55    /// The number of blocks required to evaluate again PoWMin value
56    pub dt_diff_eval: usize,
57    /// The percent of previous issuers to reach for personalized difficulty
58    pub percent_rot: f64,
59    /// Time of first UD.
60    pub ud_time0: u64,
61    /// Time of first reevaluation of the UD.
62    pub ud_reeval_time0: u64,
63    /// Time period between two re-evaluation of the UD.
64    pub dt_reeval: u64,
65}
66
67impl Default for BlockV10Parameters {
68    fn default() -> BlockV10Parameters {
69        BlockV10Parameters {
70            c: 0.0488,
71            dt: 86_400,
72            ud0: 1_000,
73            sig_period: 432_000,
74            sig_stock: 100,
75            sig_window: 5_259_600,
76            sig_validity: 63_115_200,
77            sig_qty: 5,
78            idty_window: 5_259_600,
79            ms_window: 5_259_600,
80            x_percent: 0.8,
81            ms_validity: 31_557_600,
82            step_max: 5,
83            median_time_blocks: 24,
84            avg_gen_time: 300,
85            dt_diff_eval: 12,
86            percent_rot: 0.67,
87            ud_time0: 1_488_970_800,
88            ud_reeval_time0: 1_490_094_000,
89            dt_reeval: 15_778_800,
90        }
91    }
92}
93
94impl std::str::FromStr for BlockV10Parameters {
95    type Err = ParseParamsError;
96
97    fn from_str(source: &str) -> Result<Self, Self::Err> {
98        let params: Vec<&str> = source.split(':').collect();
99        Ok(BlockV10Parameters {
100            c: params[0].parse()?,
101            dt: params[1].parse()?,
102            ud0: params[2].parse()?,
103            sig_period: params[3].parse()?,
104            sig_stock: params[4].parse()?,
105            sig_window: params[5].parse()?,
106            sig_validity: params[6].parse()?,
107            sig_qty: params[7].parse()?,
108            idty_window: params[8].parse()?,
109            ms_window: params[9].parse()?,
110            x_percent: params[10].parse()?,
111            ms_validity: params[11].parse()?,
112            step_max: params[12].parse()?,
113            median_time_blocks: params[13].parse()?,
114            avg_gen_time: params[14].parse()?,
115            dt_diff_eval: params[15].parse()?,
116            percent_rot: params[16].parse()?,
117            ud_time0: params[17].parse()?,
118            ud_reeval_time0: params[18].parse()?,
119            dt_reeval: params[19].parse()?,
120        })
121    }
122}
123
124impl ToString for BlockV10Parameters {
125    fn to_string(&self) -> String {
126        format!(
127            "{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}",
128            self.c,
129            self.dt,
130            self.ud0,
131            self.sig_period,
132            self.sig_stock,
133            self.sig_window,
134            self.sig_validity,
135            self.sig_qty,
136            self.idty_window,
137            self.ms_window,
138            self.x_percent,
139            self.ms_validity,
140            self.step_max,
141            self.median_time_blocks,
142            self.avg_gen_time,
143            self.dt_diff_eval,
144            self.percent_rot,
145            self.ud_time0,
146            self.ud_reeval_time0,
147            self.dt_reeval,
148        )
149    }
150}
151
152#[cfg(test)]
153mod tests {
154    use super::*;
155
156    #[test]
157    fn test_genesis_params_from_str() -> Result<(), ParseParamsError> {
158        let genesis_params_str = "0.0488:86400:1000:432000:100:5259600:63115200:5:5259600:5259600:0.8:31557600:5:24:300:12:0.67:1488970800:1490094000:15778800";
159
160        let genesis_params = BlockV10Parameters::from_str(genesis_params_str)?;
161
162        assert_eq!(genesis_params_str, genesis_params.to_string());
163
164        assert_eq!(
165            BlockV10Parameters {
166                c: 0.0488,
167                dt: 86_400,
168                ud0: 1_000,
169                sig_period: 432_000,
170                sig_stock: 100,
171                sig_window: 5_259_600,
172                sig_validity: 63_115_200,
173                sig_qty: 5,
174                idty_window: 5_259_600,
175                ms_window: 5_259_600,
176                x_percent: 0.8,
177                ms_validity: 31557600,
178                step_max: 5,
179                median_time_blocks: 24,
180                avg_gen_time: 300,
181                dt_diff_eval: 12,
182                percent_rot: 0.67,
183                ud_time0: 1_488_970_800,
184                ud_reeval_time0: 1_490_094_000,
185                dt_reeval: 15_778_800,
186            },
187            genesis_params
188        );
189
190        Ok(())
191    }
192}