Skip to content

Commit 96859d5

Browse files
lemmihjdjaustin
andauthored
Translate TestComputeRTeta/TestBaselineReward reward actor unit tests from go to rust (#193)
* Translate two unit tests from GO to Rust. * translate baseline reward test from golang to rust Co-authored-by: Josh Jones <[email protected]>
1 parent f5bfdc6 commit 96859d5

File tree

4 files changed

+666
-1
lines changed

4 files changed

+666
-1
lines changed

Cargo.lock

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

actors/reward/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ fvm_ipld_encoding = "0.1.0"
2626

2727
[dev-dependencies]
2828
fil_actors_runtime = { version = "8.0.0-alpha.1", path = "../runtime", features = ["test_utils", "sector-default"] }
29+
num = "0.4.0"
2930
[features]
3031
fil-actor = []
31-

actors/reward/src/logic.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,108 @@ fn compute_baseline_supply(theta: BigInt, baseline_total: &BigInt) -> BigInt {
102102

103103
one_sub * baseline_total
104104
}
105+
106+
#[cfg(test)]
107+
mod tests {
108+
use super::*;
109+
use num::BigRational;
110+
use num::ToPrimitive;
111+
use std::fs;
112+
use std::ops::Shl;
113+
114+
// Converted from: https://github.com/filecoin-project/specs-actors/blob/d56b240af24517443ce1f8abfbdab7cb22d331f1/actors/builtin/reward/reward_logic_test.go#L18
115+
// x => x/(2^128)
116+
fn q128_to_f64(x: BigInt) -> f64 {
117+
let denom = BigInt::from(1u64).shl(u128::BITS);
118+
BigRational::new(x, denom).to_f64().expect("BigInt cannot be expressed as a 64bit float")
119+
}
120+
121+
// Converted from: https://github.com/filecoin-project/specs-actors/blob/d56b240af24517443ce1f8abfbdab7cb22d331f1/actors/builtin/reward/reward_logic_test.go#L25
122+
#[test]
123+
fn test_compute_r_theta() {
124+
fn baseline_power_at(epoch: ChainEpoch) -> BigInt {
125+
(BigInt::from(epoch) + BigInt::from(1i64)) * BigInt::from(2048)
126+
}
127+
128+
assert_eq!(
129+
q128_to_f64(compute_r_theta(
130+
1,
131+
&baseline_power_at(1),
132+
&BigInt::from(2048 + 2 * 2048 / 2),
133+
&BigInt::from(2048 + 2 * 2048),
134+
)),
135+
0.5
136+
);
137+
138+
assert_eq!(
139+
q128_to_f64(compute_r_theta(
140+
1,
141+
&baseline_power_at(1),
142+
&BigInt::from(2048 + 2 * 2048 / 4),
143+
&BigInt::from(2048 + 2 * 2048),
144+
)),
145+
0.25
146+
);
147+
148+
let cumsum15 = (0..16).map(baseline_power_at).sum::<BigInt>();
149+
assert_eq!(
150+
q128_to_f64(compute_r_theta(
151+
16,
152+
&baseline_power_at(16),
153+
&(&cumsum15 + baseline_power_at(16) / BigInt::from(4)),
154+
&(&cumsum15 + baseline_power_at(16)),
155+
)),
156+
15.25
157+
);
158+
}
159+
160+
// Converted from: https://github.com/filecoin-project/specs-actors/blob/d56b240af24517443ce1f8abfbdab7cb22d331f1/actors/builtin/reward/reward_logic_test.go#L43
161+
#[test]
162+
fn test_baseline_reward() {
163+
let step = BigInt::from(5000_i64).shl(u128::BITS) - BigInt::from(77_777_777_777_i64); // offset from full integers
164+
let delta = BigInt::from(1_i64).shl(u128::BITS) - BigInt::from(33_333_333_333_i64); // offset from full integers
165+
166+
let mut prev_theta = BigInt::from(0i64);
167+
let mut theta = delta;
168+
169+
let mut b = String::from("t0, t1, y\n");
170+
let simple = compute_reward(
171+
0,
172+
BigInt::from(0i64),
173+
BigInt::from(0i64),
174+
&SIMPLE_TOTAL,
175+
&BASELINE_TOTAL,
176+
);
177+
178+
for _ in 0..512 {
179+
let mut reward = compute_reward(
180+
0,
181+
prev_theta.clone(),
182+
theta.clone(),
183+
&SIMPLE_TOTAL,
184+
&BASELINE_TOTAL,
185+
);
186+
reward -= &simple;
187+
188+
let prev_theta_str = &prev_theta.to_string();
189+
let theta_str = &theta.to_string();
190+
let reward_str = &reward.to_string();
191+
b.push_str(prev_theta_str);
192+
b.push(',');
193+
b.push_str(theta_str);
194+
b.push(',');
195+
b.push_str(reward_str);
196+
b.push('\n');
197+
198+
prev_theta += &step;
199+
theta += &step;
200+
}
201+
202+
// compare test output to golden file used for golang tests; file originally located at filecoin-project/specs-actors/actors/builtin/reward/testdata/TestBaselineReward.golden (current link: https://github.com/filecoin-project/specs-actors/blob/d56b240af24517443ce1f8abfbdab7cb22d331f1/actors/builtin/reward/testdata/TestBaselineReward.golden)
203+
let filename = "testdata/TestBaselineReward.golden";
204+
let golden_contents =
205+
fs::read_to_string(filename).expect("Something went wrong reading the file");
206+
207+
assert_eq!(golden_contents, b);
208+
}
209+
}

0 commit comments

Comments
 (0)