Skip to content

Commit e368af5

Browse files
committed
refactor(bip158): fixup internals
- use `checked_sub` to get the previous header - remove unneeded type alias - rename `headers` method - increase documentation
1 parent 9ded1d2 commit e368af5

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

crates/bitcoind_rpc/src/bip158.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,38 @@ use bitcoincore_rpc::RpcApi;
2222
/// Block height
2323
type Height = u32;
2424

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
3042
#[derive(Debug)]
3143
pub struct FilterIter<'c, C> {
32-
// RPC client
44+
/// RPC client
3345
client: &'c C,
34-
// SPK inventory
46+
/// SPK inventory
3547
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
3951
matched: BTreeSet<Height>,
40-
// best height counter
52+
/// Next height
4153
height: Height,
42-
// initial height
54+
/// Initial height
4355
start: Height,
44-
// stop height
56+
/// Stop height
4557
stop: Height,
4658
}
4759

@@ -112,7 +124,7 @@ impl<'c, C: RpcApi> FilterIter<'c, C> {
112124
}
113125

114126
/// 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)> {
116128
&self.headers
117129
}
118130
}
@@ -171,8 +183,10 @@ impl<C: RpcApi> Iterator for FilterIter<'_, C> {
171183

172184
let header = self.client.get_block_header(&hash)?;
173185

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+
{
176190
// Not enough data.
177191
None => break header,
178192
// Ok, the chain is consistent.

0 commit comments

Comments
 (0)