Skip to content

Commit 82b7506

Browse files
authored
Merge pull request #295 from rustaceanrob/filter-2-13
Improve `Filter` API
2 parents 2f60acc + 87a9403 commit 82b7506

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

example/managed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async fn main() {
7474
Event::IndexedFilter(mut filter) => {
7575
let height = filter.height();
7676
tracing::info!("Checking filter: {height}");
77-
if filter.contains_any(&addresses) {
77+
if filter.contains_any(addresses.iter()) {
7878
let hash = *filter.block_hash();
7979
tracing::info!("Found script at {}!", hash);
8080
let indexed_block = requester.get_block(hash).await.unwrap();

src/chain/chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ impl<H: HeaderStore> Chain<H> {
727727
#[cfg(not(feature = "filter-control"))]
728728
if !self.block_queue.contains(&filter_message.block_hash)
729729
&& filter
730-
.contains_any(&self.scripts)
730+
.contains_any(self.scripts.iter())
731731
.map_err(CFilterSyncError::Filter)?
732732
{
733733
// Add to the block queue

src/filters/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ pub(crate) mod cfheader_chain;
77
pub(crate) mod error;
88
pub(crate) mod filter_chain;
99

10-
use std::collections::HashSet;
11-
1210
use bitcoin::hashes::{sha256d, Hash};
1311
use bitcoin::{bip158::BlockFilter, BlockHash, FilterHash, ScriptBuf};
1412

@@ -42,12 +40,12 @@ impl Filter {
4240
&self.block_hash
4341
}
4442

45-
pub fn contains_any(&mut self, scripts: &HashSet<ScriptBuf>) -> Result<bool, FilterError> {
43+
pub fn contains_any<'a>(
44+
&'a mut self,
45+
scripts: impl Iterator<Item = &'a ScriptBuf>,
46+
) -> Result<bool, FilterError> {
4647
self.block_filter
47-
.match_any(
48-
&self.block_hash,
49-
&mut scripts.iter().map(|script| script.to_bytes()),
50-
)
48+
.match_any(&self.block_hash, scripts.map(|script| script.to_bytes()))
5149
.map_err(|_| FilterError::IORead)
5250
}
5351
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl IndexedFilter {
216216
}
217217

218218
/// Does the filter contain a positive match for any of the provided scripts
219-
pub fn contains_any(&mut self, scripts: &HashSet<ScriptBuf>) -> bool {
219+
pub fn contains_any<'a>(&'a mut self, scripts: impl Iterator<Item = &'a ScriptBuf>) -> bool {
220220
self.filter
221221
.contains_any(scripts)
222222
.expect("vec reader is infallible")

0 commit comments

Comments
 (0)