Skip to content

Commit c1c5974

Browse files
lemmihjdjaustin
andauthored
Reward actor unit tests (part 2) (#201)
* Translate two unit tests from GO to Rust. * translate baseline reward test from golang to rust * Translate TestSimpleReward test from Go to Rust * Convert BaselineRewardGrowth test from Go to Rust Co-authored-by: Josh Jones <[email protected]>
1 parent fd7fcf7 commit c1c5974

File tree

2 files changed

+603
-0
lines changed

2 files changed

+603
-0
lines changed

actors/reward/src/logic.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ lazy_static! {
3838
/// lambda = ln(2) / (6 * epochsInYear)
3939
/// for Q.128: int(lambda * 2^128)
4040
static ref LAMBDA: BigInt = BigInt::from(37396271439864487274534522888786u128);
41+
4142
}
4243

4344
/// Compute BaselinePower(t) from BaselinePower(t-1) with an additional multiplication
@@ -105,6 +106,12 @@ fn compute_baseline_supply(theta: BigInt, baseline_total: &BigInt) -> BigInt {
105106

106107
#[cfg(test)]
107108
mod tests {
109+
const SECONDS_IN_HOUR: i64 = 60 * 60;
110+
const EPOCH_DURATION_IN_SECONDS: i64 = 30;
111+
const EPOCHS_IN_HOUR: i64 = SECONDS_IN_HOUR / EPOCH_DURATION_IN_SECONDS;
112+
const EPOCHS_IN_DAY: i64 = 24 * EPOCHS_IN_HOUR;
113+
const EPOCHS_IN_YEAR: i64 = 365 * EPOCHS_IN_DAY;
114+
108115
use super::*;
109116
use num::BigRational;
110117
use num::ToPrimitive;
@@ -206,4 +213,87 @@ mod tests {
206213

207214
assert_eq!(golden_contents, b);
208215
}
216+
217+
// Converted from: https://github.com/filecoin-project/specs-actors/blob/d56b240af24517443ce1f8abfbdab7cb22d331f1/actors/builtin/reward/reward_logic_test.go#L70
218+
#[test]
219+
fn test_simple_reward() {
220+
let mut b = String::from("x, y\n");
221+
for i in 0..512 {
222+
let x: i64 = i * 5000;
223+
let reward = compute_reward(
224+
x,
225+
BigInt::from(0i64),
226+
BigInt::from(0i64),
227+
&SIMPLE_TOTAL,
228+
&BASELINE_TOTAL,
229+
);
230+
231+
let x_str = &x.to_string();
232+
let reward_str = &reward.to_string();
233+
b.push_str(x_str);
234+
b.push(',');
235+
b.push_str(reward_str);
236+
b.push('\n');
237+
}
238+
239+
// compare test output to golden file used for golang tests; file originally located at filecoin-project/specs-actors/actors/builtin/reward/testdata/TestSimpleReward.golden (current link: https://github.com/filecoin-project/specs-actors/blob/d56b240af24517443ce1f8abfbdab7cb22d331f1/actors/builtin/reward/testdata/TestSimpleReward.golden)
240+
let filename = "testdata/TestSimpleReward.golden";
241+
let golden_contents =
242+
fs::read_to_string(filename).expect("Something went wrong reading the file");
243+
244+
assert_eq!(golden_contents, b);
245+
}
246+
247+
// Converted from: https://github.com/filecoin-project/specs-actors/blob/d56b240af24517443ce1f8abfbdab7cb22d331f1/actors/builtin/reward/reward_logic_test.go#L82
248+
#[test]
249+
fn test_baseline_reward_growth() {
250+
fn baseline_in_years(start: StoragePower, x: ChainEpoch) -> StoragePower {
251+
let mut baseline = start;
252+
for _ in 0..(x * EPOCHS_IN_YEAR) {
253+
baseline = baseline_power_from_prev(&baseline);
254+
}
255+
baseline
256+
}
257+
258+
struct GrowthTestCase {
259+
start_val: StoragePower,
260+
err_bound: f64,
261+
}
262+
263+
let cases: [GrowthTestCase; 7] = [
264+
// 1 byte
265+
GrowthTestCase { start_val: StoragePower::from(1i64), err_bound: 1.0 },
266+
// GiB
267+
GrowthTestCase { start_val: StoragePower::from(1i64 << 30), err_bound: 1e-3 },
268+
// TiB
269+
GrowthTestCase { start_val: StoragePower::from(1i64 << 40), err_bound: 1e-6 },
270+
// PiB
271+
GrowthTestCase { start_val: StoragePower::from(1i64 << 50), err_bound: 1e-8 },
272+
// EiB
273+
GrowthTestCase { start_val: BASELINE_INITIAL_VALUE.clone(), err_bound: 1e-8 },
274+
// ZiB
275+
GrowthTestCase { start_val: StoragePower::from(1u128 << 70), err_bound: 1e-8 },
276+
// non power of 2 ~ 1 EiB
277+
GrowthTestCase {
278+
start_val: StoragePower::from(513_633_559_722_596_517_u128),
279+
err_bound: 1e-8,
280+
},
281+
];
282+
283+
for case in cases {
284+
let years = 1u32;
285+
let end = baseline_in_years(case.start_val.clone(), 1);
286+
287+
// logic from golang test was preserved to enable future testing of more than one year
288+
let multiplier = BigInt::pow(&BigInt::from(2u32), years);
289+
let expected = case.start_val * multiplier;
290+
let diff = &expected - end;
291+
292+
let perr = BigRational::new(diff, expected)
293+
.to_f64()
294+
.expect("BigInt cannot be expressed as a 64bit float");
295+
296+
assert!(perr < case.err_bound);
297+
}
298+
}
209299
}

0 commit comments

Comments
 (0)