use crate::client::rpc::{LogFilter, window};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LogCursor {
filter: LogFilter,
next: u64,
span: u64,
confirmations: u64,
}
impl LogCursor {
#[must_use]
pub fn new(filter: LogFilter, from: u64, span: u64, confirmations: u64) -> LogCursor {
LogCursor { filter, next: from, span, confirmations }
}
#[must_use]
pub const fn next(&self) -> u64 {
self.next
}
#[must_use]
pub fn window(&self, head: u64) -> Option<LogFilter> {
let confirmed = head.checked_sub(self.confirmations)?;
if self.next > confirmed {
return None;
}
let last = self.next.saturating_add(self.span).min(confirmed);
Some(window(&self.filter, self.next, last))
}
pub fn read(&mut self, window: &LogFilter) {
if let Some(crate::client::BlockTag::Number(last)) = window.to_block {
self.next = self.next.max(last.saturating_add(1));
}
}
}
#[cfg(test)]
#[path = "tests/cursor.rs"]
mod tests;