use core::fmt::Debug;
use std::fmt::Display;
use std::sync::PoisonError;
use std::sync::RwLockWriteGuard;
use bitcoin::bip158;
use flat_filters_store::FlatFiltersStore;
pub mod flat_filters_store;
pub mod kv_filter_database;
pub mod network_filters;
pub trait BlockFilterStore: Send + Sync {
fn get_filter(&self, block_height: u32) -> Option<bip158::BlockFilter>;
fn put_filter(&self, block_height: u32, block_filter: bip158::BlockFilter);
fn put_height(&self, height: u32);
fn get_height(&self) -> Option<u32>;
}
pub enum IteratableFilterStoreError {
Io(std::io::Error),
Eof,
Poisoned,
FilterTooLarge,
}
impl Debug for IteratableFilterStoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
IteratableFilterStoreError::Io(e) => write!(f, "I/O error: {e}"),
IteratableFilterStoreError::Eof => write!(f, "End of file"),
IteratableFilterStoreError::Poisoned => write!(f, "Lock poisoned"),
IteratableFilterStoreError::FilterTooLarge => write!(f, "Filter too large"),
}
}
}
impl From<std::io::Error> for IteratableFilterStoreError {
fn from(e: std::io::Error) -> Self {
IteratableFilterStoreError::Io(e)
}
}
impl Display for IteratableFilterStoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(self, f)
}
}
impl From<PoisonError<RwLockWriteGuard<'_, FlatFiltersStore>>> for IteratableFilterStoreError {
fn from(_: PoisonError<RwLockWriteGuard<'_, FlatFiltersStore>>) -> Self {
IteratableFilterStoreError::Poisoned
}
}
pub trait IteratableFilterStore:
Send + Sync + IntoIterator<Item = (u32, bip158::BlockFilter)>
{
type I: Iterator<Item = (u32, bip158::BlockFilter)>;
fn iter(&self, start_height: Option<usize>) -> Result<Self::I, IteratableFilterStoreError>;
fn put_filter(
&self,
block_filter: bip158::BlockFilter,
height: u32,
) -> Result<(), IteratableFilterStoreError>;
fn set_height(&self, height: u32) -> Result<(), IteratableFilterStoreError>;
fn get_height(&self) -> Result<u32, IteratableFilterStoreError>;
}