diff --git a/sync/src/utils/bloom_filter.rs b/sync/src/utils/bloom_filter.rs index 335e0c38..3ed1928e 100644 --- a/sync/src/utils/bloom_filter.rs +++ b/sync/src/utils/bloom_filter.rs @@ -91,18 +91,14 @@ impl BloomFilter { let script = Script::new(output.script_pubkey.clone()); let is_update_needed = self.filter_flags == types::FilterFlags::All || (self.filter_flags == types::FilterFlags::PubKeyOnly && (script.is_pay_to_public_key() || script.is_multisig_script())); - for instruction in script.iter().filter_map(|i| i.ok()) { - if let Some(instruction_data) = instruction.data { - if bloom.contains(instruction_data) { - is_match = true; + if contains_any_instruction_data(&*bloom, script) { + is_match = true; - if is_update_needed { - bloom.insert(&serialize(&OutPoint { - hash: tx.hash.clone(), - index: output_index as u32, - })); - } - } + if is_update_needed { + bloom.insert(&serialize(&OutPoint { + hash: tx.hash.clone(), + index: output_index as u32, + })); } } } @@ -128,18 +124,8 @@ impl BloomFilter { // check if match any arbitrary script data element in any scriptSig in tx let script = Script::new(input.script_sig.clone()); - for instruction in script.iter() { - match instruction { - Err(_) => break, - Ok(instruction) => { - if let Some(instruction_data) = instruction.data { - is_match = bloom.contains(&*instruction_data); - if is_match { - return true; - } - } - } - } + if contains_any_instruction_data(&*bloom, script) { + return true; } } @@ -184,6 +170,23 @@ impl BloomFilterData { } } +fn contains_any_instruction_data(bloom: &BloomFilterData, script: Script) -> bool { + for instruction in script.iter() { + match instruction { + Err(_) => break, + Ok(instruction) => { + if let Some(instruction_data) = instruction.data { + if bloom.contains(&*instruction_data) { + return true; + } + } + }, + } + } + + false +} + #[cfg(test)] mod tests { extern crate test_data;