Skip to content

Commit 60dfce3

Browse files
authored
Merge pull request #528 from rustaceanrob/26-1-14-filter-type
Add `FilterType` enum
2 parents e715414 + 6aff4bb commit 60dfce3

4 files changed

Lines changed: 36 additions & 5 deletions

File tree

src/builder.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use bitcoin::Network;
66
use super::{client::Client, node::Node};
77
use crate::chain::ChainState;
88
use crate::network::ConnectionType;
9-
use crate::Config;
109
use crate::TrustedPeer;
10+
use crate::{Config, FilterType};
1111

1212
const MIN_PEERS: u8 = 1;
1313
const MAX_PEERS: u8 = 15;
@@ -132,6 +132,12 @@ impl Builder {
132132
self
133133
}
134134

135+
/// Request a specific compact sketch type.
136+
pub fn filter_type(mut self, filter_type: FilterType) -> Self {
137+
self.config.filter_type = filter_type;
138+
self
139+
}
140+
135141
/// Consume the node builder and receive a [`Node`] and [`Client`].
136142
pub fn build(mut self) -> (Node, Client) {
137143
Node::new(self.network, core::mem::take(&mut self.config))

src/chain/chain.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ use super::{
1414
CFHeaderBatch, CFHeaderChanges, ChainState, Filter, FilterCheck, FilterHeaderRequest,
1515
FilterRequest, FilterRequestState, HeaderValidationExt, HeightMonitor, PeerId,
1616
};
17-
use crate::IndexedFilter;
1817
use crate::{chain::BlockHeaderChanges, messages::Event, Dialog, Info, Progress};
18+
use crate::{FilterType, IndexedFilter};
1919

20-
const FILTER_BASIC: u8 = 0x00;
2120
const CF_HEADER_BATCH_SIZE: u32 = 1_999;
2221
const FILTER_BATCH_SIZE: u32 = 999;
2322

@@ -28,6 +27,7 @@ pub(crate) struct Chain {
2827
network: Network,
2928
heights: Arc<Mutex<HeightMonitor>>,
3029
dialog: Arc<Dialog>,
30+
filter_type: FilterType,
3131
}
3232

3333
impl Chain {
@@ -37,6 +37,7 @@ impl Chain {
3737
dialog: Arc<Dialog>,
3838
height_monitor: Arc<Mutex<HeightMonitor>>,
3939
quorum_required: u8,
40+
filter_type: FilterType,
4041
) -> Self {
4142
let header_chain = match chain_state {
4243
ChainState::Snapshot(headers) => {
@@ -60,6 +61,7 @@ impl Chain {
6061
network,
6162
heights: height_monitor,
6263
dialog,
64+
filter_type,
6365
}
6466
}
6567

@@ -295,7 +297,7 @@ impl Chain {
295297
stop_hash,
296298
});
297299
GetCFHeaders {
298-
filter_type: FILTER_BASIC,
300+
filter_type: self.filter_type.into(),
299301
start_height: last_unchecked_cfheader,
300302
stop_hash,
301303
}
@@ -372,7 +374,7 @@ impl Chain {
372374
start_height: last_unchecked_filter,
373375
});
374376
GetCFilters {
375-
filter_type: FILTER_BASIC,
377+
filter_type: self.filter_type.into(),
376378
start_height: last_unchecked_filter,
377379
stop_hash,
378380
}
@@ -437,6 +439,7 @@ mod tests {
437439
use tokio::sync::Mutex;
438440

439441
use crate::chain::ChainState;
442+
use crate::FilterType;
440443
use crate::{
441444
chain::checkpoints::HeaderCheckpoint,
442445
messages::{Event, Info, Warning},
@@ -459,6 +462,7 @@ mod tests {
459462
Arc::new(Dialog::new(info_tx, warn_tx, event_tx)),
460463
height_monitor,
461464
peers,
465+
FilterType::Basic,
462466
)
463467
}
464468

src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,23 @@ impl IndexedBlock {
110110
}
111111
}
112112

113+
/// The type of filter to make a request for.
114+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
115+
#[non_exhaustive]
116+
pub enum FilterType {
117+
#[default]
118+
/// A golomb coded compact sketch based on siphash. Contains all spendable script types.
119+
Basic,
120+
}
121+
122+
impl From<FilterType> for u8 {
123+
fn from(value: FilterType) -> Self {
124+
match value {
125+
FilterType::Basic => 0x00,
126+
}
127+
}
128+
}
129+
113130
/// A compact block filter with associated height.
114131
#[derive(Debug, Clone, PartialEq, Eq)]
115132
pub struct IndexedFilter {
@@ -334,6 +351,7 @@ struct Config {
334351
chain_state: Option<ChainState>,
335352
connection_type: ConnectionType,
336353
peer_timeout_config: PeerTimeoutConfig,
354+
filter_type: FilterType,
337355
}
338356

339357
impl Default for Config {
@@ -345,6 +363,7 @@ impl Default for Config {
345363
chain_state: Default::default(),
346364
connection_type: Default::default(),
347365
peer_timeout_config: PeerTimeoutConfig::default(),
366+
filter_type: FilterType::default(),
348367
}
349368
}
350369
}

src/node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ impl Node {
7474
chain_state,
7575
connection_type,
7676
peer_timeout_config,
77+
filter_type,
7778
} = config;
7879
// Set up a communication channel between the node and client
7980
let (info_tx, info_rx) = mpsc::channel::<Info>(32);
@@ -107,6 +108,7 @@ impl Node {
107108
Arc::clone(&dialog),
108109
height_monitor,
109110
required_peers,
111+
filter_type,
110112
);
111113
(
112114
Self {

0 commit comments

Comments
 (0)