This commit is contained in:
Sean Gilligan 2025-03-02 20:44:13 -06:00 committed by GitHub
commit 74648b5124
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -35,6 +35,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.IntStream;
import static java.lang.Math.E; import static java.lang.Math.E;
import static java.lang.Math.log; import static java.lang.Math.log;
@ -254,6 +255,8 @@ public class BloomFilter extends BaseMessage {
public synchronized void insert(byte[] object) { public synchronized void insert(byte[] object) {
for (int i = 0; i < hashFuncs; i++) for (int i = 0; i < hashFuncs; i++)
ByteUtils.setBitLE(data, murmurHash3(data, nTweak, i, object)); ByteUtils.setBitLE(data, murmurHash3(data, nTweak, i, object));
IntStream.range(0, (int) hashFuncs)
.forEach(i -> ByteUtils.setBitLE(data, murmurHash3(data, nTweak, i, object)));
} }
/** Inserts the given key and equivalent hashed form (for the address). */ /** Inserts the given key and equivalent hashed form (for the address). */
@ -299,10 +302,8 @@ public class BloomFilter extends BaseMessage {
* for when this can be a useful thing to do. * for when this can be a useful thing to do.
*/ */
public synchronized boolean matchesAll() { public synchronized boolean matchesAll() {
for (byte b : data) return IntStream.range(0, data.length)
if (b != (byte) 0xff) .allMatch(i -> data[i] == (byte) 0xff);
return false;
return true;
} }
/** /**
@ -327,7 +328,7 @@ public class BloomFilter extends BaseMessage {
* filtered block already has the matched transactions associated with it. * filtered block already has the matched transactions associated with it.
*/ */
public synchronized FilteredBlock applyAndUpdate(Block block) { public synchronized FilteredBlock applyAndUpdate(Block block) {
List<Transaction> txns = block.getTransactions(); List<Transaction> txns = Objects.requireNonNull(block.getTransactions());
List<Sha256Hash> txHashes = new ArrayList<>(txns.size()); List<Sha256Hash> txHashes = new ArrayList<>(txns.size());
List<Transaction> matched = new ArrayList<>(); List<Transaction> matched = new ArrayList<>();
byte[] bits = new byte[(int) Math.ceil(txns.size() / 8.0)]; byte[] bits = new byte[(int) Math.ceil(txns.size() / 8.0)];
@ -365,16 +366,17 @@ public class BloomFilter extends BaseMessage {
} }
} }
if (found) return true; if (found) return true;
for (TransactionInput input : tx.getInputs()) { return anyInputMatches(tx);
if (contains(input.getOutpoint().serialize())) { }
return true;
} private boolean anyInputMatches(Transaction tx) {
for (ScriptChunk chunk : input.getScriptSig().chunks()) { return tx.getInputs().stream().anyMatch(this::inputMatches);
if (chunk.isPushData() && contains(chunk.data)) }
return true;
} private boolean inputMatches(TransactionInput input) {
} return contains(input.getOutpoint().serialize()) ||
return false; input.getScriptSig().chunks().stream()
.anyMatch(c -> c.isPushData() && contains(c.data));
} }
@Override @Override