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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use crate::date::Epoch;
use crate::stake::Stake;
use crate::value::{Value, ValueError};
use chain_core::packer::Codec;
use chain_core::property::ReadError;
use std::num::{NonZeroU32, NonZeroU64};
use typed_bytes::ByteBuilder;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompoundingType {
    Linear,
    Halvening,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
    any(test, feature = "property-test-api"),
    derive(test_strategy::Arbitrary)
)]
pub struct Ratio {
    pub numerator: u64,
    pub denominator: NonZeroU64,
}

impl PartialOrd for Ratio {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for Ratio {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        if self.denominator == other.denominator {
            self.numerator.cmp(&other.numerator)
        } else {
            let left = self.numerator as u128 * other.denominator.get() as u128;
            let right = other.numerator as u128 * self.denominator.get() as u128;
            left.cmp(&right)
        }
    }
}

impl Ratio {
    pub fn zero() -> Self {
        Ratio {
            numerator: 0,
            denominator: NonZeroU64::new(1).unwrap(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
    any(test, feature = "property-test-api"),
    derive(test_strategy::Arbitrary)
)]
pub struct TaxType {
    // what get subtracted as fixed value
    pub fixed: Value,
    // Ratio of tax after fixed amout subtracted
    pub ratio: Ratio,
    // Max limit of tax
    pub max_limit: Option<NonZeroU64>,
}

impl TaxType {
    pub fn zero() -> Self {
        TaxType {
            fixed: Value(0),
            ratio: Ratio::zero(),
            max_limit: None,
        }
    }

    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        bb.u64(self.fixed.0)
            .u64(self.ratio.numerator)
            .u64(self.ratio.denominator.get())
            .u64(self.max_limit.map_or(0, |v| v.get()))
    }

    pub fn read_frombuf<R: std::io::BufRead>(codec: &mut Codec<R>) -> Result<Self, ReadError> {
        let fixed = codec.get_be_u64().map(Value)?;
        let num = codec.get_be_u64()?;
        let denom = codec.get_be_u64()?;
        let limit = codec.get_be_u64()?;
        let denominator = NonZeroU64::new(denom).map_or_else(
            || {
                Err(ReadError::StructureInvalid(
                    "ratio fraction divisor invalid".to_string(),
                ))
            },
            Ok,
        )?;
        if num > denom {
            return Err(ReadError::StructureInvalid(
                "ratio fraction invalid bigger than 1".to_string(),
            ));
        }

        Ok(TaxType {
            fixed,
            ratio: Ratio {
                numerator: num,
                denominator,
            },
            max_limit: NonZeroU64::new(limit),
        })
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Limit {
    /// the drawn value will not be limited
    None,

    /// The drawn value will be limited by the absoluted stake in the system
    /// with a given ratio.
    ByStakeAbsolute(Ratio),
}

/// Parameters for rewards calculation. This controls:
///
/// * Rewards contributions
/// * Treasury cuts
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Parameters {
    /// This is an initial_value for the linear or halvening function.
    /// In the case of the linear function it is the value that is going to be calculated
    /// from the contribution.
    pub initial_value: u64,
    /// This is the ratio used by either the linear or the halvening function.
    /// The semantic and result of this is deeply linked to either.
    pub compounding_ratio: Ratio,
    /// The type of compounding
    pub compounding_type: CompoundingType,
    /// Number of epoch between reduction phase, cannot be 0
    pub epoch_rate: NonZeroU32,
    /// When to start
    pub epoch_start: Epoch,
    /// Max Drawing limit
    pub reward_drawing_limit_max: Limit,
    /// Pool Capping
    /// This doesn't really make sense
    pub pool_participation_capping: Option<(NonZeroU32, NonZeroU32)>,
}

impl Parameters {
    pub fn zero() -> Self {
        Parameters {
            initial_value: 0,
            compounding_ratio: Ratio::zero(),
            compounding_type: CompoundingType::Linear,
            epoch_rate: NonZeroU32::new(u32::max_value()).unwrap(),
            epoch_start: 0,
            reward_drawing_limit_max: Limit::None,
            pool_participation_capping: None,
        }
    }
}

/// A value distributed between tax and remaining
#[derive(Debug, Clone)]
pub struct TaxDistribution {
    pub taxed: Value,
    pub after_tax: Value,
}

#[derive(Debug, Clone)]
pub struct SystemInformation {
    pub declared_stake: Stake,
}

/// Calculate the reward contribution from the parameters
///
/// Note that the contribution in the system is still bounded by the remaining
/// rewards pot, which is not taken in considering for this calculation.
pub fn rewards_contribution_calculation(
    epoch: Epoch,
    params: &Parameters,
    system_info: &SystemInformation,
) -> Value {
    assert!(params.epoch_rate.get() != 0);

    if epoch < params.epoch_start {
        return Value::zero();
    }

    let zone = ((epoch - params.epoch_start) / params.epoch_rate.get()) as u64;
    let drawn = match params.compounding_type {
        CompoundingType::Linear => {
            // C - rratio * (#epoch / erate)
            let rr = &params.compounding_ratio;
            let reduce_by = (rr.numerator * zone) / rr.denominator.get();
            if params.initial_value >= reduce_by {
                Value(params.initial_value - reduce_by)
            } else {
                Value::zero()
            }
        }
        CompoundingType::Halvening => {
            // mathematical formula is : C * rratio ^ (#epoch / erate)
            // although we perform it as a for loop, with the rationale
            // that it allow for integer computation and that the reduce_epoch_rate
            // should prevent growth to large amount of zones
            let rr = &params.compounding_ratio;
            const SCALE: u128 = 1_000_000_000_000_000_000;

            let mut acc = params.initial_value as u128 * SCALE;
            for _ in 0..zone {
                acc *= rr.numerator as u128;
                acc /= rr.denominator.get() as u128;
            }

            Value((acc / SCALE) as u64)
        }
    };

    match params.reward_drawing_limit_max {
        Limit::None => drawn,
        Limit::ByStakeAbsolute(ratio) => {
            let x = (u64::from(system_info.declared_stake) as u128 * ratio.numerator as u128)
                / ratio.denominator.get() as u128;
            std::cmp::min(drawn, Value(x as u64))
        }
    }
}

/// Tax some value into the tax value and what is remaining
pub fn tax_cut(v: Value, tax_type: &TaxType) -> Result<TaxDistribution, ValueError> {
    let mut left = v;
    let mut taxed = Value::zero();

    // subtract fix amount
    match left - tax_type.fixed {
        Ok(left1) => {
            left = left1;
            taxed = (taxed + tax_type.fixed)?;
        }
        Err(_) => {
            return Ok(TaxDistribution {
                taxed: v,
                after_tax: Value::zero(),
            })
        }
    };

    // calculate and subtract ratio
    {
        let rr = tax_type.ratio;
        let olimit = tax_type.max_limit;

        const SCALE: u128 = 1_000_000_000;
        let out = ((((left.0 as u128 * SCALE) * rr.numerator as u128)
            / rr.denominator.get() as u128)
            / SCALE) as u64;
        let treasury_cut = match olimit {
            None => Value(out),
            Some(limit) => Value(std::cmp::min(limit.get(), out)),
        };

        match left - treasury_cut {
            Ok(left2) => {
                left = left2;
                taxed = (taxed + treasury_cut)?;
            }
            Err(_) => {
                left = Value::zero();
                taxed = (taxed + left)?;
            }
        }
    };

    Ok(TaxDistribution {
        taxed,
        after_tax: left,
    })
}

#[cfg(any(test, feature = "property-test-api"))]
mod tests {
    use super::*;
    #[cfg(test)]
    use quickcheck::TestResult;
    use quickcheck::{Arbitrary, Gen};
    use quickcheck_macros::quickcheck;

    #[quickcheck]
    fn tax_cut_fully_accounted(v: Value, treasury_tax: TaxType) -> TestResult {
        match tax_cut(v, &treasury_tax) {
            Ok(td) => {
                let sum = (td.taxed + td.after_tax).unwrap();
                if sum == v {
                    TestResult::passed()
                } else {
                    TestResult::error(format!(
                        "mismatch taxed={} remaining={} expected={} got={} for {:?}",
                        td.taxed, td.after_tax, v, sum, treasury_tax
                    ))
                }
            }
            Err(_) => TestResult::discard(),
        }
    }

    #[test]
    fn ratio_cmp_works() {
        use std::cmp::Ordering;
        let r1 = Ratio {
            numerator: 10,
            denominator: NonZeroU64::new(20).unwrap(),
        };
        let r2 = Ratio {
            numerator: 20,
            denominator: NonZeroU64::new(10).unwrap(),
        };
        let r3 = Ratio {
            numerator: 20,
            denominator: NonZeroU64::new(40).unwrap(),
        };
        assert_eq!(r1.cmp(&r2), Ordering::Less);
        assert_eq!(r2.cmp(&r1), Ordering::Greater);
        assert_eq!(r1.cmp(&r3), Ordering::Equal);
        assert_eq!(r3.cmp(&r1), Ordering::Equal);
    }

    #[test]
    fn rewards_contribution_calculation_epoch_start_smaller_than_epoch() {
        let mut params = Parameters::zero();
        params.epoch_start = 1;
        let epoch = 0;
        let system_info = SystemInformation {
            declared_stake: Stake::from_value(Value(100)),
        };
        assert_eq!(
            rewards_contribution_calculation(epoch, &params, &system_info),
            Value::zero()
        );
    }

    #[test]
    fn rewards_contribution_calculation_initial_value_smaller_than_reduce_by() {
        let params = Parameters {
            initial_value: 9,
            compounding_ratio: Ratio {
                numerator: 100,
                denominator: NonZeroU64::new(10).unwrap(),
            },
            compounding_type: CompoundingType::Linear,
            epoch_rate: NonZeroU32::new(1).unwrap(),
            epoch_start: 0,
            reward_drawing_limit_max: Limit::None,
            pool_participation_capping: None,
        };
        let epoch = 1;
        let system_info = SystemInformation {
            declared_stake: Stake::from_value(Value(100)),
        };
        assert_eq!(
            rewards_contribution_calculation(epoch, &params, &system_info),
            Value::zero()
        );
    }

    impl Arbitrary for TaxType {
        fn arbitrary<G: Gen>(gen: &mut G) -> Self {
            let fixed = Arbitrary::arbitrary(gen);
            let denominator = u64::arbitrary(gen) + 1;
            let numerator = u64::arbitrary(gen) % denominator;
            let max_limit = NonZeroU64::new(u64::arbitrary(gen));

            TaxType {
                fixed,
                ratio: Ratio {
                    numerator,
                    denominator: NonZeroU64::new(denominator).unwrap(),
                },
                max_limit,
            }
        }
    }

    impl Arbitrary for Limit {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            if bool::arbitrary(g) {
                Limit::None
            } else {
                Limit::ByStakeAbsolute(Ratio::arbitrary(g))
            }
        }
    }

    impl Arbitrary for Parameters {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            let epoch_rate = {
                let mut number = u32::arbitrary(g);
                if number == 0 {
                    number += 1;
                }
                NonZeroU32::new(number).unwrap()
            };

            Parameters {
                initial_value: u64::arbitrary(g),
                compounding_ratio: Ratio::arbitrary(g),
                compounding_type: CompoundingType::arbitrary(g),
                epoch_rate,
                epoch_start: Arbitrary::arbitrary(g),
                reward_drawing_limit_max: Limit::arbitrary(g),
                pool_participation_capping: None,
            }
        }
    }

    impl Arbitrary for CompoundingType {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            let option: u8 = u8::arbitrary(g) % 2;
            match option {
                0 => CompoundingType::Linear,
                2 => CompoundingType::Halvening,
                _ => unreachable!(),
            }
        }
    }
}