Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
bug fix & improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsimon authored and RomarQ committed Mar 7, 2023
1 parent a8674db commit 9a9cada
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
40 changes: 40 additions & 0 deletions pallets/acurast/common/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,24 @@ tests! {
0,
vec![((0,1), true), ((8,10), false)]
);
// ╭start ╭end
// _■■■■■■■■
// ranges:
// ■
// ■■
// ■■■■■■■■■■
// ■■
test_schedule_overlaps_start(
Schedule{
duration: 2,
start_time: 1,
end_time: 9,
interval: 2,
max_start_delay: 0,
},
0,
vec![((0,1), false), ((0,2), true), ((0, 10), true), ((8, 10), true), ((9, 10), false)]
);
// ╭start ╭end
// ■■___■■___■■___
// ■■
Expand Down Expand Up @@ -346,5 +364,27 @@ tests! {
2,
vec![((0,3), false), ((2,4), false), ((10,12), true), ((12,15), false)]
);
test_schedule_overlaps_end_before_start(
Schedule{
duration: 2,
start_time: 1,
end_time: 0,
interval: 2,
max_start_delay: 0,
},
0,
vec![((0,1), false), ((0,2), false)]
);
test_schedule_overlaps_equal_start_end(
Schedule{
duration: 2,
start_time: 0,
end_time: 0,
interval: 2,
max_start_delay: 0,
},
0,
vec![((0,1), false), ((0,2), false)]
);
}
}
22 changes: 12 additions & 10 deletions pallets/acurast/common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,25 @@ impl Schedule {

pub fn overlaps(&self, start_delay: u64, a: u64, b: u64) -> Option<bool> {
let (start, end) = self.range(start_delay)?;
if a == b || start == end || b <= start || a >= end {
if b <= a || start == end || b <= start || end <= a {
return Some(false);
}

// if query interval `[a, b]` starts before, we can pretend it only starts at `start`
let relative_a = a.checked_sub(start).or(Some(start))?;
let relative_a = a.checked_sub(start).unwrap_or(start);

if let Some(relative_b) = b.checked_sub(start) {
let a = relative_a % self.interval;
let _b = relative_b % self.interval;
let (a, b) = (
relative_a % self.interval,
if _b == 0 { self.interval } else { _b },
);
// b > a from here

let l = b.checked_sub(a)?;
Some(a < self.duration || l >= self.interval)
let b = if _b == 0 { self.interval } else { _b };

let l = b.checked_sub(a).unwrap_or(0);
// ╭a ╭b
// ■■■■______■■■■______
// OR
// ╭b ╭a ╭b'
// ■■■■______■■■■______
Some(b < a || a < self.duration || l >= self.interval)
} else {
Some(false)
}
Expand Down

0 comments on commit 9a9cada

Please sign in to comment.