use crate::client::rpc::{BlockTag, LogFilter};
#[must_use]
pub fn pages(from: u64, to: u64, span: u64) -> Pages {
Pages { next: Some(from).filter(|from| *from <= to), to, span }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pages {
next: Option<u64>,
to: u64,
span: u64,
}
impl Iterator for Pages {
type Item = (u64, u64);
fn next(&mut self) -> Option<(u64, u64)> {
let from = self.next?;
let last = from.saturating_add(self.span).min(self.to);
self.next = last.checked_add(1).filter(|next| *next <= self.to);
Some((from, last))
}
}
#[must_use]
pub fn is_narrow(filter: &LogFilter) -> bool {
!filter.address.is_empty() || filter.topics.iter().any(Option::is_some)
}
#[must_use]
pub fn window(filter: &LogFilter, from: u64, to: u64) -> LogFilter {
LogFilter {
from_block: Some(BlockTag::Number(from)),
to_block: Some(BlockTag::Number(to)),
block_hash: None,
..filter.clone()
}
}
#[cfg(test)]
#[path = "tests/pages.rs"]
mod tests;