Skip to content

Commit b712d1e

Browse files
zhiqiangxualanxuanorth
authored
optimize next_not_elapsed (#1317)
* optimize next_not_elapsed * add test for next_not_elapsed * fix clippy * Improved test for next_not_elapsed * rm next_not_elapsed_checking --------- Co-authored-by: alanxu <[email protected]> Co-authored-by: Alex North <[email protected]>
1 parent eee1cd4 commit b712d1e

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

actors/miner/src/deadline_info.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,24 @@ impl DeadlineInfo {
132132

133133
/// Returns the next instance of this deadline that has not yet elapsed.
134134
pub fn next_not_elapsed(self) -> Self {
135-
std::iter::successors(Some(self), |info| {
136-
Some(Self::new(
137-
info.next_period_start(),
138-
info.index,
139-
info.current_epoch,
140-
self.w_post_period_deadlines,
141-
self.w_post_proving_period,
142-
self.w_post_challenge_window,
143-
self.w_post_challenge_lookback,
144-
self.fault_declaration_cutoff,
145-
))
146-
})
147-
.find(|info| !info.has_elapsed())
148-
.unwrap() // the iterator is infinite, so `find` won't ever return `None`
135+
if !self.has_elapsed() {
136+
return self;
137+
}
138+
139+
// has elapsed, advance by some multiples of w_post_proving_period
140+
let gap = self.current_epoch - self.close;
141+
let delta_periods = 1 + gap / self.w_post_proving_period;
142+
143+
Self::new(
144+
self.period_start + self.w_post_proving_period * delta_periods,
145+
self.index,
146+
self.current_epoch,
147+
self.w_post_period_deadlines,
148+
self.w_post_proving_period,
149+
self.w_post_challenge_window,
150+
self.w_post_challenge_lookback,
151+
self.fault_declaration_cutoff,
152+
)
149153
}
150154

151155
pub fn quant_spec(&self) -> QuantSpec {

actors/miner/tests/deadlines.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@ fn quantization_spec_rounds_to_the_next_deadline() {
1212
assert_eq!(d.next_not_elapsed().last(), quant.quantize_up(curr));
1313
}
1414

15+
#[test]
16+
fn next_not_elapsed() {
17+
let policy = Policy::default();
18+
let deadline_duration = policy.wpost_proving_period / policy.wpost_period_deadlines as i64;
19+
let period_start = 12345; // Arbitrary
20+
21+
// Check every deadline.
22+
for deadline_idx in 0..policy.wpost_period_deadlines {
23+
let open = period_start + deadline_idx as i64 * deadline_duration;
24+
// Check every epoch from before the deadline opens until multiple proving periods later.
25+
for curr in (open - 1)..(open + deadline_duration + 2 * policy.wpost_proving_period) {
26+
let d = new_deadline_info(&policy, period_start, deadline_idx, curr);
27+
// Find next non-elapsed instance the naive way: by checking them in order.
28+
let expected = std::iter::successors(Some(d), |prev| {
29+
Some(new_deadline_info(
30+
&policy,
31+
prev.next_period_start(),
32+
prev.index,
33+
prev.current_epoch,
34+
))
35+
})
36+
.find(|info| !info.has_elapsed())
37+
.unwrap();
38+
39+
assert_eq!(expected, d.next_not_elapsed());
40+
if curr < open + deadline_duration {
41+
assert!(!d.has_elapsed());
42+
assert_eq!(d, d.next_not_elapsed());
43+
}
44+
}
45+
}
46+
}
47+
1548
// All proving periods equivalent mod WPoStProving period should give equivalent
1649
// dlines for a given epoch. Only the offset property should matter
1750
#[test]

0 commit comments

Comments
 (0)