use super::SeqNo;
#[derive(Debug)]
pub(crate) struct SeqNoWindow {
max: SeqNo,
seen: u128,
}
impl SeqNoWindow {
pub fn new(seq: SeqNo) -> Self {
Self { max: seq, seen: 1 }
}
pub fn new_after(seq: SeqNo) -> Self {
Self {
max: seq,
seen: u128::MAX,
}
}
pub fn max_seq(&self) -> SeqNo {
self.max
}
pub fn accepts(&self, seq: SeqNo) -> bool {
seq > self.max || (*self.max - *seq < 128 && self.seen & (1u128 << (*self.max - *seq)) == 0)
}
pub fn update(&mut self, seq: SeqNo) {
if seq > self.max {
self.seen = self
.seen
.checked_shl((*seq - *self.max).min(128) as u32)
.unwrap_or(0);
self.max = seq;
}
if let Some(bit) = 1u128.checked_shl((*self.max - *seq).min(128) as u32) {
self.seen |= bit;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn initial_receipt_is_seen_but_earlier_numbers_are_not() {
let window = SeqNoWindow::new(100.into());
assert_eq!(window.max_seq(), 100.into());
assert!(!window.accepts(100.into()));
assert!(window.accepts(99.into()));
assert!(window.accepts(0.into()));
assert!(window.accepts(101.into()));
let window = SeqNoWindow::new(0.into());
assert!(!window.accepts(0.into()));
assert!(window.accepts(1.into()));
}
#[test]
fn reordered_receipts_are_recorded_without_moving_the_maximum() {
let mut window = SeqNoWindow::new(65535.into());
window.update(65537.into());
assert!(window.accepts(65536.into()));
assert!(window.accepts(65536.into()));
window.update(65536.into());
window.update(65536.into());
assert_eq!(window.max_seq(), 65537.into());
for seq in 65535..=65537 {
assert!(!window.accepts(seq.into()));
}
}
#[test]
fn oldest_position_is_retained_until_it_leaves_the_window() {
let mut window = SeqNoWindow::new(10.into());
window.update(137.into());
assert!(!window.accepts(10.into()));
assert!(window.accepts(11.into()));
window.update(138.into());
assert!(!window.accepts(10.into()));
assert!(window.accepts(11.into()));
window.update(11.into());
assert!(!window.accepts(11.into()));
window.update(10.into());
assert_eq!(window.max_seq(), 138.into());
assert!(window.accepts(12.into()));
assert!(!window.accepts(138.into()));
}
#[test]
fn large_jumps_clear_the_previous_bitmap() {
for gap in [128, 129, 1u64 << 32] {
let mut window = SeqNoWindow::new(138.into());
window.update(137.into());
let next = 138 + gap;
window.update(next.into());
assert_eq!(window.max_seq(), next.into());
assert!(!window.accepts(138.into()));
assert!(!window.accepts(next.into()));
assert!(window.accepts((next - 1).into()));
assert!(window.accepts((next - 127).into()));
assert!(!window.accepts((next - 128).into()));
}
}
#[test]
fn mixed_receipts_match_a_set_of_received_numbers() {
use std::collections::BTreeSet;
for start in [0u64, 65500, u64::MAX - 10000] {
let mut window = SeqNoWindow::new(start.into());
let mut received = BTreeSet::from([start]);
let mut max = start;
let deltas = [1, 63, -1, 64, -64, 65, -127, -128, 127, 128, 129, 0];
for delta in deltas.into_iter().cycle().take(240) {
let seq = max.saturating_add_signed(delta);
window.update(seq.into());
received.insert(seq);
max = max.max(seq);
assert_eq!(window.max_seq(), max.into());
for candidate in max.saturating_sub(130)..=max.saturating_add(2) {
let expected =
candidate >= max.saturating_sub(127) && !received.contains(&candidate);
assert_eq!(
window.accepts(candidate.into()),
expected,
"start={start}, receipt={seq}, max={max}, candidate={candidate}"
);
}
}
}
}
#[test]
fn restored_maximum_blocks_old_receipts_but_allows_new_gaps() {
let mut window = SeqNoWindow::new_after(200.into());
for seq in 0..=200 {
assert!(!window.accepts(seq.into()));
}
assert!(window.accepts(201.into()));
window.update(203.into());
assert!(!window.accepts(200.into()));
assert!(window.accepts(201.into()));
assert!(window.accepts(202.into()));
assert!(!window.accepts(203.into()));
window.update(201.into());
assert!(!window.accepts(201.into()));
assert!(window.accepts(202.into()));
}
}