diff --git a/src/filter.rs b/src/filter.rs new file mode 100644 index 000000000..92e953c04 --- /dev/null +++ b/src/filter.rs @@ -0,0 +1,49 @@ +//! multilevel bloom filter interface +use hash::*; +use std::collections::HashMap; + +/// Represents bloom index in cache +/// +/// On bloom level 0, all positions represent different blooms. +/// On higher levels multiple positions represent one bloom +/// and should be transformed to `BlockIndex` to get index of this bloom +#[derive(Eq, PartialEq, Hash)] +pub struct BloomIndex { + level: u8, + level_index: usize, + index: usize, +} + +pub trait FilterDataSource { + /// returns reference to log at given position if it exists + fn bloom_at_index(&self, index: &BloomIndex) -> Option<&H2048>; +} + +pub trait Filter: Sized { + /// creates new filter instance + fn new(data_source: &T, index_size: usize, levels: u8) -> Self where T: FilterDataSource; + + /// converts block number and level to `BloomIndex` + fn bloom_index(&self, block_number: usize, level: u8) -> BloomIndex; + + /// add new bloom to all levels + fn add_bloom(&self, bloom: &H2048, block_number: usize) -> HashMap; + + /// add new blooms starting from block number + fn add_blooms(&self, blooms: &[H2048], block_number: usize) -> HashMap; + + /// reset bloom at level 0 and forces rebuild on higher levels + fn reset_bloom(&self, bloom: &H2048, block_number: usize) -> HashMap; + + /// sets lowest level bloom to 0 and forces rebuild on higher levels + fn clear_bloom(&self, block_number: usize) -> HashMap; + + /// returns numbers of blocks that may contain Address + fn blocks_with_address(&self, address: &Address) -> Vec; + + /// returns numbers of blocks that may contain Topic + fn blocks_with_topics(&self, topic: &H256) -> Vec; + + /// returns numbers of blocks that may log bloom + fn blocks_with_bloom(&self, bloom: &H2048) -> Vec; +} diff --git a/src/lib.rs b/src/lib.rs index acb03afb5..7ebf91f51 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,7 @@ pub mod sha3; pub mod hashdb; pub mod memorydb; pub mod math; +pub mod filter; //pub mod network;