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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#![cfg_attr(not(feature = "std"), no_std)]
use core::convert::TryFrom;
use sp_arithmetic::{
biguint::BigUint,
traits::{SaturatedConversion, Zero},
PerThing, Perquintill,
};
pub fn compute_inflation<P: PerThing>(stake: P, ideal_stake: P, falloff: P) -> P {
if stake < ideal_stake {
return stake / ideal_stake
}
if falloff < P::from_percent(1.into()) {
log::error!("Invalid inflation computation: falloff less than 1% is not supported");
return PerThing::zero()
}
let accuracy = {
let mut a = BigUint::from(Into::<u128>::into(P::ACCURACY));
a.lstrip();
a
};
let mut falloff = BigUint::from(falloff.deconstruct().into());
falloff.lstrip();
let ln2 = {
const LN2: u64 = 0_693_147_180_559_945_309;
let ln2 = P::from_rational(LN2.into(), Perquintill::ACCURACY.into());
BigUint::from(ln2.deconstruct().into())
};
let ln2_div_d = div_by_stripped(ln2.mul(&accuracy), &falloff);
let inpos_param = INPoSParam {
x_ideal: BigUint::from(ideal_stake.deconstruct().into()),
x: BigUint::from(stake.deconstruct().into()),
accuracy,
ln2_div_d,
};
let res = compute_taylor_serie_part(&inpos_param);
match u128::try_from(res.clone()) {
Ok(res) if res <= Into::<u128>::into(P::ACCURACY) => P::from_parts(res.saturated_into()),
_ => {
log::error!("Invalid inflation computation: unexpected result {:?}", res);
P::zero()
},
}
}
struct INPoSParam {
ln2_div_d: BigUint,
x_ideal: BigUint,
x: BigUint,
accuracy: BigUint,
}
fn compute_taylor_serie_part(p: &INPoSParam) -> BigUint {
let mut last_taylor_term = p.accuracy.clone();
let mut taylor_sum_positive = true;
let mut taylor_sum = last_taylor_term.clone();
for k in 1..300 {
last_taylor_term = compute_taylor_term(k, &last_taylor_term, p);
if last_taylor_term.is_zero() {
break
}
let last_taylor_term_positive = k % 2 == 0;
if taylor_sum_positive == last_taylor_term_positive {
taylor_sum = taylor_sum.add(&last_taylor_term);
} else {
if taylor_sum >= last_taylor_term {
taylor_sum = taylor_sum
.sub(&last_taylor_term)
.unwrap_or_else(|e| e);
} else {
taylor_sum_positive = !taylor_sum_positive;
taylor_sum = last_taylor_term
.clone()
.sub(&taylor_sum)
.unwrap_or_else(|e| e);
}
}
}
if !taylor_sum_positive {
return BigUint::zero()
}
taylor_sum.lstrip();
taylor_sum
}
fn compute_taylor_term(k: u32, previous_taylor_term: &BigUint, p: &INPoSParam) -> BigUint {
let x_minus_x_ideal =
p.x.clone()
.sub(&p.x_ideal)
.unwrap_or_else(|_| BigUint::zero());
let res = previous_taylor_term.clone().mul(&x_minus_x_ideal).mul(&p.ln2_div_d).div_unit(k);
let res = div_by_stripped(res, &p.accuracy);
let mut res = div_by_stripped(res, &p.accuracy);
res.lstrip();
res
}
fn div_by_stripped(mut a: BigUint, b: &BigUint) -> BigUint {
a.lstrip();
if b.len() == 0 {
log::error!("Computation error: Invalid division");
return BigUint::zero()
}
if b.len() == 1 {
return a.div_unit(b.checked_get(0).unwrap_or(1))
}
if b.len() > a.len() {
return BigUint::zero()
}
if b.len() == a.len() {
let mut new_a = a.mul(&BigUint::from(100_000u64.pow(2)));
new_a.lstrip();
debug_assert!(new_a.len() > b.len());
return new_a
.div(b, false)
.map(|res| res.0)
.unwrap_or_else(|| BigUint::zero())
.div_unit(100_000)
.div_unit(100_000)
}
a.div(b, false).map(|res| res.0).unwrap_or_else(|| BigUint::zero())
}