@@ -22,26 +22,38 @@ use bitcoincore_rpc::RpcApi;
22
22
/// Block height
23
23
type Height = u32 ;
24
24
25
- /// Block header with associated block hash.
26
- type HashedHeader = ( BlockHash , Header ) ;
27
-
28
- /// Type that generates block [`Event`]s by matching a list of script pubkeys against a
29
- /// [`BlockFilter`].
25
+ /// Type that returns Bitcoin blocks by matching a list of script pubkeys (SPKs) against a
26
+ /// [`bip158::BlockFilter`].
27
+ ///
28
+ /// ## Note
29
+ ///
30
+ /// - You must add spks to `FilterIter` by using [`add_spks`]. If not you will get an error when
31
+ /// calling `next`. This is because [`match_any`] will be true for every query, which is usually
32
+ /// not what you want.
33
+ /// - Call `next` on the iterator to get the next [`Event`]. It is common to iterate `FilterIter`
34
+ /// [`by_ref`], so that you can continue to call methods on it.
35
+ /// - Use [`get_tip`] to find the tip of the remote node and to set the stop height.
36
+ /// - Iteration stops when filters for all heights have been scanned.
37
+ ///
38
+ /// [`add_spks`]: Self::add_spks
39
+ /// [`match_any`]: BlockFilter::match_any
40
+ /// [`get_tip`]: Self::get_tip
41
+ /// [`by_ref`]: Self::by_ref
30
42
#[ derive( Debug ) ]
31
43
pub struct FilterIter < ' c , C > {
32
- // RPC client
44
+ /// RPC client
33
45
client : & ' c C ,
34
- // SPK inventory
46
+ /// SPK inventory
35
47
spks : Vec < ScriptBuf > ,
36
- // block headers
37
- headers : BTreeMap < Height , HashedHeader > ,
38
- // heights of matching blocks
48
+ /// Block headers
49
+ headers : BTreeMap < Height , ( BlockHash , Header ) > ,
50
+ /// Heights of matching blocks
39
51
matched : BTreeSet < Height > ,
40
- // best height counter
52
+ /// Next height
41
53
height : Height ,
42
- // initial height
54
+ /// Initial height
43
55
start : Height ,
44
- // stop height
56
+ /// Stop height
45
57
stop : Height ,
46
58
}
47
59
@@ -112,7 +124,7 @@ impl<'c, C: RpcApi> FilterIter<'c, C> {
112
124
}
113
125
114
126
/// Return all of the block headers that were collected during the scan.
115
- pub fn block_headers ( & self ) -> & BTreeMap < Height , ( BlockHash , Header ) > {
127
+ pub fn headers ( & self ) -> & BTreeMap < Height , ( BlockHash , Header ) > {
116
128
& self . headers
117
129
}
118
130
}
@@ -171,8 +183,10 @@ impl<C: RpcApi> Iterator for FilterIter<'_, C> {
171
183
172
184
let header = self . client . get_block_header ( & hash) ?;
173
185
174
- let prev_height = height. saturating_sub ( 1 ) ;
175
- match self . headers . get ( & prev_height) . copied ( ) {
186
+ match height
187
+ . checked_sub ( 1 )
188
+ . and_then ( |prev_height| self . headers . get ( & prev_height) . copied ( ) )
189
+ {
176
190
// Not enough data.
177
191
None => break header,
178
192
// Ok, the chain is consistent.
0 commit comments