From 6aff4bb9ecadcf6c135e75958c8002a3c202d7df Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Wed, 14 Jan 2026 11:47:49 +0000 Subject: [PATCH] Add `FilterType` enum Enables futurue experimentation by adding new filter variants that can be passed through the builder. Currently set to the only supported filter, basic. --- src/builder.rs | 8 +++++++- src/chain/chain.rs | 12 ++++++++---- src/lib.rs | 19 +++++++++++++++++++ src/node.rs | 2 ++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index ee894cf6..f1fab3f2 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -6,8 +6,8 @@ use bitcoin::Network; use super::{client::Client, node::Node}; use crate::chain::ChainState; use crate::network::ConnectionType; -use crate::Config; use crate::TrustedPeer; +use crate::{Config, FilterType}; const MIN_PEERS: u8 = 1; const MAX_PEERS: u8 = 15; @@ -132,6 +132,12 @@ impl Builder { self } + /// Request a specific compact sketch type. + pub fn filter_type(mut self, filter_type: FilterType) -> Self { + self.config.filter_type = filter_type; + self + } + /// Consume the node builder and receive a [`Node`] and [`Client`]. pub fn build(mut self) -> (Node, Client) { Node::new(self.network, core::mem::take(&mut self.config)) diff --git a/src/chain/chain.rs b/src/chain/chain.rs index c523e04a..7e1247b1 100644 --- a/src/chain/chain.rs +++ b/src/chain/chain.rs @@ -14,10 +14,9 @@ use super::{ CFHeaderBatch, CFHeaderChanges, ChainState, Filter, FilterCheck, FilterHeaderRequest, FilterRequest, FilterRequestState, HeaderValidationExt, HeightMonitor, PeerId, }; -use crate::IndexedFilter; use crate::{chain::BlockHeaderChanges, messages::Event, Dialog, Info, Progress}; +use crate::{FilterType, IndexedFilter}; -const FILTER_BASIC: u8 = 0x00; const CF_HEADER_BATCH_SIZE: u32 = 1_999; const FILTER_BATCH_SIZE: u32 = 999; @@ -28,6 +27,7 @@ pub(crate) struct Chain { network: Network, heights: Arc>, dialog: Arc, + filter_type: FilterType, } impl Chain { @@ -37,6 +37,7 @@ impl Chain { dialog: Arc, height_monitor: Arc>, quorum_required: u8, + filter_type: FilterType, ) -> Self { let header_chain = match chain_state { ChainState::Snapshot(headers) => { @@ -60,6 +61,7 @@ impl Chain { network, heights: height_monitor, dialog, + filter_type, } } @@ -295,7 +297,7 @@ impl Chain { stop_hash, }); GetCFHeaders { - filter_type: FILTER_BASIC, + filter_type: self.filter_type.into(), start_height: last_unchecked_cfheader, stop_hash, } @@ -372,7 +374,7 @@ impl Chain { start_height: last_unchecked_filter, }); GetCFilters { - filter_type: FILTER_BASIC, + filter_type: self.filter_type.into(), start_height: last_unchecked_filter, stop_hash, } @@ -437,6 +439,7 @@ mod tests { use tokio::sync::Mutex; use crate::chain::ChainState; + use crate::FilterType; use crate::{ chain::checkpoints::HeaderCheckpoint, messages::{Event, Info, Warning}, @@ -459,6 +462,7 @@ mod tests { Arc::new(Dialog::new(info_tx, warn_tx, event_tx)), height_monitor, peers, + FilterType::Basic, ) } diff --git a/src/lib.rs b/src/lib.rs index ce205b8a..2e670f15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,6 +110,23 @@ impl IndexedBlock { } } +/// The type of filter to make a request for. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)] +#[non_exhaustive] +pub enum FilterType { + #[default] + /// A golomb coded compact sketch based on siphash. Contains all spendable script types. + Basic, +} + +impl From for u8 { + fn from(value: FilterType) -> Self { + match value { + FilterType::Basic => 0x00, + } + } +} + /// A compact block filter with associated height. #[derive(Debug, Clone, PartialEq, Eq)] pub struct IndexedFilter { @@ -334,6 +351,7 @@ struct Config { chain_state: Option, connection_type: ConnectionType, peer_timeout_config: PeerTimeoutConfig, + filter_type: FilterType, } impl Default for Config { @@ -345,6 +363,7 @@ impl Default for Config { chain_state: Default::default(), connection_type: Default::default(), peer_timeout_config: PeerTimeoutConfig::default(), + filter_type: FilterType::default(), } } } diff --git a/src/node.rs b/src/node.rs index 89286e76..62cef75c 100644 --- a/src/node.rs +++ b/src/node.rs @@ -74,6 +74,7 @@ impl Node { chain_state, connection_type, peer_timeout_config, + filter_type, } = config; // Set up a communication channel between the node and client let (info_tx, info_rx) = mpsc::channel::(32); @@ -107,6 +108,7 @@ impl Node { Arc::clone(&dialog), height_monitor, required_peers, + filter_type, ); ( Self {