From d88d421de658e825e32f4a84091fbf43a1d39adc Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 23 Jul 2013 18:07:22 +0200 Subject: [PATCH] Make BloomFilter support match-all filters better --- .../com/google/bitcoin/core/BloomFilter.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/BloomFilter.java b/core/src/main/java/com/google/bitcoin/core/BloomFilter.java index b99e8da4..a952d823 100644 --- a/core/src/main/java/com/google/bitcoin/core/BloomFilter.java +++ b/core/src/main/java/com/google/bitcoin/core/BloomFilter.java @@ -232,16 +232,36 @@ public class BloomFilter extends Message { Utils.setBitLE(data, hash(i, object)); } + /** + * Sets this filter to match all objects + */ + public void setMatchAll() { + data = new byte[] {(byte) 0xff}; + } + /** * Copies filter into this. * filter must have the same size, hash function count and nTweak or an exception will be thrown. */ public void merge(BloomFilter filter) { - Preconditions.checkArgument(filter.data.length == this.data.length && - filter.hashFuncs == this.hashFuncs && - filter.nTweak == this.nTweak); - for (int i = 0; i < data.length; i++) - this.data[i] |= filter.data[i]; + if (!this.matchesAll() && !filter.matchesAll()) { + Preconditions.checkArgument(filter.data.length == this.data.length && + filter.hashFuncs == this.hashFuncs && + filter.nTweak == this.nTweak); + for (int i = 0; i < data.length; i++) + this.data[i] |= filter.data[i]; + } else + this.data = new byte[] {(byte) 0xff}; + } + + /** + * Returns true if this filter will match anything + */ + public boolean matchesAll() { + for (byte b : data) + if (b != (byte) 0xff) + return false; + return true; } @Override