Skip to content

Commit c4ac382

Browse files
authored
Merge pull request #332 from rustaceanrob/kill-filter-4-15
Remove the `filter` module
2 parents 2ba89a2 + 42aa5c2 commit c4ac382

File tree

8 files changed

+148
-158
lines changed

8 files changed

+148
-158
lines changed

src/chain/chain.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ use tokio::sync::Mutex;
1414

1515
use super::{
1616
block_queue::BlockQueue,
17+
cfheader_batch::CFHeaderBatch,
1718
checkpoints::{HeaderCheckpoint, HeaderCheckpoints},
18-
error::{BlockScanError, HeaderSyncError},
19+
error::{BlockScanError, CFHeaderSyncError, CFilterSyncError, HeaderSyncError},
1920
graph::{AcceptHeaderChanges, BlockTree, HeaderRejection, Tip},
20-
CFHeaderChanges, FilterHeaderRequest, FilterRequest, FilterRequestState, HeightMonitor, PeerId,
21+
CFHeaderChanges, Filter, FilterHeaderRequest, FilterRequest, FilterRequestState, HeightMonitor,
22+
PeerId,
2123
};
2224
#[cfg(feature = "filter-control")]
2325
use crate::error::FetchBlockError;
@@ -30,17 +32,14 @@ use crate::{
3032
db::{traits::HeaderStore, BlockHeaderChanges},
3133
dialog::Dialog,
3234
error::HeaderPersistenceError,
33-
filters::{
34-
cfheader_batch::CFHeaderBatch,
35-
error::{CFHeaderSyncError, CFilterSyncError},
36-
Filter, CF_HEADER_BATCH_SIZE, FILTER_BATCH_SIZE,
37-
},
3835
messages::{Event, Warning},
3936
IndexedBlock, Info, Progress,
4037
};
4138

4239
const REORG_LOOKBACK: u32 = 7;
4340
const FILTER_BASIC: u8 = 0x00;
41+
const CF_HEADER_BATCH_SIZE: u32 = 1_999;
42+
const FILTER_BATCH_SIZE: u32 = 999;
4443

4544
#[derive(Debug)]
4645
pub(crate) struct Chain<H: HeaderStore> {

src/chain/error.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,101 @@ impl Display for HeaderSyncError {
4949

5050
impl_sourceless_error!(HeaderSyncError);
5151

52+
#[derive(Debug)]
53+
pub enum CFHeaderSyncError {
54+
EmptyMessage,
55+
UnknownStophash,
56+
StopHashMismatch,
57+
UnrequestedStophash,
58+
PrevHeaderMismatch,
59+
HeaderChainIndexOverflow,
60+
UnexpectedCFHeaderMessage,
61+
StartHeightMisalignment,
62+
}
63+
64+
impl core::fmt::Display for CFHeaderSyncError {
65+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
66+
match self {
67+
CFHeaderSyncError::EmptyMessage => write!(f, "empty headers message."),
68+
CFHeaderSyncError::UnknownStophash => {
69+
write!(f, "a stop hash received was not found in our chain.")
70+
}
71+
CFHeaderSyncError::StopHashMismatch => {
72+
write!(f, "the requested and received stop hashes do not match.")
73+
}
74+
CFHeaderSyncError::UnrequestedStophash => {
75+
write!(f, "we did not request this stop hash.")
76+
}
77+
CFHeaderSyncError::PrevHeaderMismatch => write!(f, "previous filter header mismatch."),
78+
CFHeaderSyncError::HeaderChainIndexOverflow => write!(
79+
f,
80+
"indexed out of bounds on the header chain trying to find a block hash."
81+
),
82+
CFHeaderSyncError::UnexpectedCFHeaderMessage => write!(
83+
f,
84+
"we already had a message from this peer staged in our queue."
85+
),
86+
CFHeaderSyncError::StartHeightMisalignment => write!(
87+
f,
88+
"the size of the batch and the requested start height do not align"
89+
),
90+
}
91+
}
92+
}
93+
94+
impl_sourceless_error!(CFHeaderSyncError);
95+
96+
#[derive(Debug)]
97+
pub enum CFilterSyncError {
98+
UnknownStophash,
99+
UnrequestedStophash,
100+
UnknownFilterHash,
101+
MisalignedFilterHash,
102+
Filter(FilterError),
103+
}
104+
105+
impl core::fmt::Display for CFilterSyncError {
106+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107+
match self {
108+
CFilterSyncError::UnknownStophash => {
109+
write!(f, "a stop hash recevied was not found in our chain.")
110+
}
111+
CFilterSyncError::UnrequestedStophash => {
112+
write!(f, "we did not request this stop hash.")
113+
}
114+
CFilterSyncError::UnknownFilterHash => write!(
115+
f,
116+
"we could not find the filter hash corresponding to that stop hash."
117+
),
118+
CFilterSyncError::MisalignedFilterHash => write!(
119+
f,
120+
"the filter hash from our header chain and this filter hash do not match."
121+
),
122+
CFilterSyncError::Filter(_) => write!(
123+
f,
124+
"the filter experienced an IO error checking for Script inclusions."
125+
),
126+
}
127+
}
128+
}
129+
130+
impl_sourceless_error!(CFilterSyncError);
131+
132+
#[derive(Debug)]
133+
pub enum FilterError {
134+
IORead,
135+
}
136+
137+
impl core::fmt::Display for FilterError {
138+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
139+
match self {
140+
FilterError::IORead => write!(f, "unable to read from the filter contents buffer."),
141+
}
142+
}
143+
}
144+
145+
impl_sourceless_error!(FilterError);
146+
52147
#[derive(Debug)]
53148
pub(crate) enum BlockScanError {
54149
NoBlockHash,

src/chain/mod.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! Notably, [`checkpoints`] contains known Bitcoin block hashes and heights with significant work, so Kyoto nodes do not have to sync from genesis.
44
pub(crate) mod block_queue;
5+
mod cfheader_batch;
56
#[allow(clippy::module_inception)]
67
pub(crate) mod chain;
78
/// Expected block header checkpoints and corresponding structure.
@@ -14,9 +15,13 @@ pub(crate) mod header_batch;
1415

1516
use std::collections::HashMap;
1617

17-
use bitcoin::{block::Header, BlockHash, FilterHash, FilterHeader};
18+
use bitcoin::hashes::{sha256d, Hash};
19+
use bitcoin::{bip158::BlockFilter, block::Header, BlockHash, FilterHash, FilterHeader, ScriptBuf};
1820

19-
use crate::{filters::cfheader_batch::CFHeaderBatch, network::PeerId};
21+
use crate::network::PeerId;
22+
23+
use cfheader_batch::CFHeaderBatch;
24+
use error::FilterError;
2025

2126
type Height = u32;
2227

@@ -110,6 +115,44 @@ pub(crate) enum CFHeaderChanges {
110115
Conflict,
111116
}
112117

118+
#[derive(Debug, Clone, PartialEq, Eq)]
119+
pub(crate) struct Filter {
120+
filter_hash: FilterHash,
121+
block_hash: BlockHash,
122+
block_filter: BlockFilter,
123+
}
124+
125+
#[allow(dead_code)]
126+
impl Filter {
127+
pub fn new(contents: Vec<u8>, block_hash: BlockHash) -> Self {
128+
let hash = sha256d::Hash::hash(&contents);
129+
let filter_hash = FilterHash::from_raw_hash(hash);
130+
let block_filter = BlockFilter::new(&contents);
131+
Self {
132+
filter_hash,
133+
block_hash,
134+
block_filter,
135+
}
136+
}
137+
138+
pub fn filter_hash(&self) -> &FilterHash {
139+
&self.filter_hash
140+
}
141+
142+
pub fn block_hash(&self) -> &BlockHash {
143+
&self.block_hash
144+
}
145+
146+
pub fn contains_any<'a>(
147+
&'a mut self,
148+
scripts: impl Iterator<Item = &'a ScriptBuf>,
149+
) -> Result<bool, FilterError> {
150+
self.block_filter
151+
.match_any(&self.block_hash, scripts.map(|script| script.to_bytes()))
152+
.map_err(|_| FilterError::IORead)
153+
}
154+
}
155+
113156
#[derive(Debug)]
114157
pub(crate) struct HeightMonitor {
115158
map: HashMap<PeerId, Height>,

src/filters/error.rs

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/filters/mod.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
pub mod chain;
7979
pub mod db;
8080

81-
mod filters;
8281
mod network;
8382
mod prelude;
8483

@@ -105,7 +104,7 @@ pub type BlockReceiver = tokio::sync::oneshot::Receiver<Result<IndexedBlock, Fet
105104
#[cfg(feature = "filter-control")]
106105
use crate::error::FetchBlockError;
107106
#[cfg(feature = "filter-control")]
108-
use filters::Filter;
107+
use chain::Filter;
109108
#[cfg(feature = "filter-control")]
110109
use std::collections::HashSet;
111110

src/node.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ use crate::{
1919
chain::{
2020
chain::Chain,
2121
checkpoints::{HeaderCheckpoint, HeaderCheckpoints},
22-
error::HeaderSyncError,
22+
error::{CFilterSyncError, HeaderSyncError},
2323
CFHeaderChanges, HeightMonitor,
2424
},
2525
db::traits::{HeaderStore, PeerStore},
2626
error::FetchHeaderError,
27-
filters::error::CFilterSyncError,
2827
network::{peer_map::PeerMap, LastBlockMonitor, PeerId, PeerTimeoutConfig},
2928
FilterSyncPolicy, NodeState, RejectPayload, TxBroadcastPolicy,
3029
};

0 commit comments

Comments
 (0)