use super::{state::*, *};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum BlockSizeEstimate {
Confirmed(u32),
Advertised(u32),
Unknown,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct BlockRangeRequest {
pub(super) start_height: block::Height,
pub(super) count: u32,
pub(super) anchor_hash: block::Hash,
pub(super) estimated_bytes: u64,
pub(super) expected_blocks: Vec<ExpectedBlock>,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(super) struct ExpectedBlock {
pub(super) height: block::Height,
pub(super) hash: block::Hash,
pub(super) estimated_bytes: u64,
}
impl BlockRangeRequest {
pub(super) fn end_height(&self) -> block::Height {
height_after_count(self.start_height, self.count)
.and_then(previous_height)
.expect("range request count is non-zero")
}
pub(super) fn contains(&self, height: block::Height) -> bool {
self.start_height <= height && height <= self.end_height()
}
pub(super) fn offset_for_height(&self, height: block::Height) -> Option<u32> {
self.contains(height)
.then(|| height.0.checked_sub(self.start_height.0))
.flatten()
}
pub(super) fn expected_hash(&self, height: block::Height) -> Option<block::Hash> {
self.expected_blocks
.iter()
.find_map(|expected| (expected.height == height).then_some(expected.hash))
}
pub(super) fn estimated_bytes_for_height(&self, height: block::Height) -> Option<u64> {
self.expected_blocks
.iter()
.find_map(|expected| (expected.height == height).then_some(expected.estimated_bytes))
}
}