|
| 1 | +use alloc::vec::Vec; |
| 2 | +use dashcore::bip158::BlockFilter; |
| 3 | +use dashcore::prelude::CoreBlockHeight; |
| 4 | +use dashcore::{Address, BlockHash}; |
| 5 | +use rayon::prelude::{IntoParallelIterator, ParallelIterator}; |
| 6 | +use std::collections::{BTreeSet, HashMap}; |
| 7 | + |
| 8 | +#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] |
| 9 | +pub struct FilterMatchKey { |
| 10 | + height: CoreBlockHeight, |
| 11 | + hash: BlockHash, |
| 12 | +} |
| 13 | + |
| 14 | +impl FilterMatchKey { |
| 15 | + pub fn new(height: CoreBlockHeight, hash: BlockHash) -> Self { |
| 16 | + Self { |
| 17 | + height, |
| 18 | + hash, |
| 19 | + } |
| 20 | + } |
| 21 | + pub fn height(&self) -> CoreBlockHeight { |
| 22 | + self.height |
| 23 | + } |
| 24 | + pub fn hash(&self) -> &BlockHash { |
| 25 | + &self.hash |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// Check compact filters for addresses and return the keys that matched. |
| 30 | +pub fn check_compact_filters_for_addresses( |
| 31 | + input: &HashMap<FilterMatchKey, BlockFilter>, |
| 32 | + addresses: Vec<Address>, |
| 33 | +) -> BTreeSet<FilterMatchKey> { |
| 34 | + let script_pubkey_bytes: Vec<Vec<u8>> = |
| 35 | + addresses.iter().map(|address| address.script_pubkey().to_bytes()).collect(); |
| 36 | + |
| 37 | + input |
| 38 | + .into_par_iter() |
| 39 | + .filter_map(|(key, filter)| { |
| 40 | + filter |
| 41 | + .match_any(key.hash(), script_pubkey_bytes.iter().map(|v| v.as_slice())) |
| 42 | + .unwrap_or(false) |
| 43 | + .then_some(key.clone()) |
| 44 | + }) |
| 45 | + .collect() |
| 46 | +} |
| 47 | + |
| 48 | +#[cfg(test)] |
| 49 | +mod tests { |
| 50 | + use super::*; |
| 51 | + use crate::Network; |
| 52 | + use dashcore::{Block, Transaction}; |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn test_empty_input_returns_empty() { |
| 56 | + let result = check_compact_filters_for_addresses(&HashMap::new(), vec![]); |
| 57 | + assert!(result.is_empty()); |
| 58 | + } |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_empty_addresses_returns_empty() { |
| 62 | + let address = Address::dummy(Network::Regtest, 1); |
| 63 | + let tx = Transaction::dummy(&address, 0..0, &[1]); |
| 64 | + let block = Block::dummy(100, vec![tx]); |
| 65 | + let filter = BlockFilter::dummy(&block); |
| 66 | + let key = FilterMatchKey::new(100, block.block_hash()); |
| 67 | + |
| 68 | + let mut input = HashMap::new(); |
| 69 | + input.insert(key.clone(), filter); |
| 70 | + |
| 71 | + let output = check_compact_filters_for_addresses(&input, vec![]); |
| 72 | + assert!(!output.contains(&key)); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_matching_filter() { |
| 77 | + let address = Address::dummy(Network::Regtest, 1); |
| 78 | + let tx = Transaction::dummy(&address, 0..0, &[1]); |
| 79 | + let block = Block::dummy(100, vec![tx]); |
| 80 | + let filter = BlockFilter::dummy(&block); |
| 81 | + let key = FilterMatchKey::new(100, block.block_hash()); |
| 82 | + |
| 83 | + let mut input = HashMap::new(); |
| 84 | + input.insert(key.clone(), filter); |
| 85 | + |
| 86 | + let output = check_compact_filters_for_addresses(&input, vec![address]); |
| 87 | + assert!(output.contains(&key)); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_non_matching_filter() { |
| 92 | + let address = Address::dummy(Network::Regtest, 1); |
| 93 | + let address_other = Address::dummy(Network::Regtest, 2); |
| 94 | + |
| 95 | + let tx = Transaction::dummy(&address_other, 0..0, &[1]); |
| 96 | + let block = Block::dummy(100, vec![tx]); |
| 97 | + let filter = BlockFilter::dummy(&block); |
| 98 | + let key = FilterMatchKey::new(100, block.block_hash()); |
| 99 | + |
| 100 | + let mut input = HashMap::new(); |
| 101 | + input.insert(key.clone(), filter); |
| 102 | + |
| 103 | + let output = check_compact_filters_for_addresses(&input, vec![address]); |
| 104 | + assert!(!output.contains(&key)); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn test_batch_mixed_results() { |
| 109 | + let unrelated_address = Address::dummy(Network::Regtest, 0); |
| 110 | + let address_1 = Address::dummy(Network::Regtest, 1); |
| 111 | + let address_2 = Address::dummy(Network::Regtest, 2); |
| 112 | + |
| 113 | + let tx_1 = Transaction::dummy(&address_1, 0..0, &[1]); |
| 114 | + let block_1 = Block::dummy(100, vec![tx_1]); |
| 115 | + let filter_1 = BlockFilter::dummy(&block_1); |
| 116 | + let key_1 = FilterMatchKey::new(100, block_1.block_hash()); |
| 117 | + |
| 118 | + let tx_2 = Transaction::dummy(&address_2, 0..0, &[2]); |
| 119 | + let block_2 = Block::dummy(200, vec![tx_2]); |
| 120 | + let filter_2 = BlockFilter::dummy(&block_2); |
| 121 | + let key_2 = FilterMatchKey::new(200, block_2.block_hash()); |
| 122 | + |
| 123 | + let tx_3 = Transaction::dummy(&unrelated_address, 0..0, &[10]); |
| 124 | + let block_3 = Block::dummy(300, vec![tx_3]); |
| 125 | + let filter_3 = BlockFilter::dummy(&block_3); |
| 126 | + let key_3 = FilterMatchKey::new(300, block_3.block_hash()); |
| 127 | + |
| 128 | + let mut input = HashMap::new(); |
| 129 | + input.insert(key_1.clone(), filter_1); |
| 130 | + input.insert(key_2.clone(), filter_2); |
| 131 | + input.insert(key_3.clone(), filter_3); |
| 132 | + |
| 133 | + let output = check_compact_filters_for_addresses(&input, vec![address_1, address_2]); |
| 134 | + assert_eq!(output.len(), 2); |
| 135 | + assert!(output.contains(&key_1)); |
| 136 | + assert!(output.contains(&key_2)); |
| 137 | + assert!(!output.contains(&key_3)); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn test_output_sorted_by_height() { |
| 142 | + let address = Address::dummy(Network::Regtest, 1); |
| 143 | + |
| 144 | + // Create blocks at different heights (inserted in non-sorted order) |
| 145 | + let heights = [500, 100, 300, 200, 400]; |
| 146 | + let mut input = HashMap::new(); |
| 147 | + |
| 148 | + for (i, &height) in heights.iter().enumerate() { |
| 149 | + let tx = Transaction::dummy(&address, 0..0, &[i as u64]); |
| 150 | + let block = Block::dummy(height, vec![tx]); |
| 151 | + let filter = BlockFilter::dummy(&block); |
| 152 | + let key = FilterMatchKey::new(height, block.block_hash()); |
| 153 | + input.insert(key, filter); |
| 154 | + } |
| 155 | + |
| 156 | + let output = check_compact_filters_for_addresses(&input, vec![address]); |
| 157 | + |
| 158 | + // Verify output is sorted by height (ascending) |
| 159 | + let heights_out: Vec<CoreBlockHeight> = output.iter().map(|k| k.height()).collect(); |
| 160 | + let mut sorted_heights = heights_out.clone(); |
| 161 | + sorted_heights.sort(); |
| 162 | + |
| 163 | + assert_eq!(heights_out, sorted_heights); |
| 164 | + assert_eq!(heights_out, vec![100, 200, 300, 400, 500]); |
| 165 | + } |
| 166 | +} |
0 commit comments