From a0c25aed288d2ac6c8a6acfb62d0d0bd21841386 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 17 Jan 2013 17:50:04 -0500 Subject: [PATCH] Make bloom filter false positive rate configurable. --- .../com/google/bitcoin/core/PeerGroup.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java index 36151ebff..4c741608d 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java @@ -131,6 +131,12 @@ public class PeerGroup extends AbstractIdleService { // A bloom filter generated from all connected wallets that is given to new peers private BloomFilter bloomFilter; + /** A reasonable default for the bloom filter false positive rate on mainnet. + * Users for which low data usage is of utmost concern, 0.0001 may be better, for users + * to whom anonymity is of utmost concern, 0.001 should provide very good privacy */ + public static final double DEFAULT_BLOOM_FILTER_FP_RATE = 0.0005; + // The false positive rate for bloomFilter + private double bloomFilterFPRate = DEFAULT_BLOOM_FILTER_FP_RATE; /** * Creates a PeerGroup with the given parameters. No chain is provided so this node will report its chain height @@ -150,8 +156,7 @@ public class PeerGroup extends AbstractIdleService { public PeerGroup(NetworkParameters params, AbstractBlockChain chain) { this(params, chain, null); } - - + /** *

Creates a PeerGroup for the given network and chain, using the provided Netty {@link ClientBootstrap} object. *

@@ -545,9 +550,9 @@ public class PeerGroup extends AbstractIdleService { if (chain == null || !chain.shouldVerifyTransactions()) { long nTweak = new Random().nextLong(); - BloomFilter filter = new BloomFilter(elements, 0.001, nTweak); + BloomFilter filter = new BloomFilter(elements, bloomFilterFPRate, nTweak); for (Wallet w : wallets) - filter.merge(w.getBloomFilter(elements, 0.001, nTweak)); + filter.merge(w.getBloomFilter(elements, bloomFilterFPRate, nTweak)); bloomFilter = filter; log.info("Sending all peers an updated Bloom Filter."); for (Peer peer : peers) @@ -557,6 +562,18 @@ public class PeerGroup extends AbstractIdleService { } catch (IOException e) { } } } + + /** + * Sets the false positive rate of bloom filters given to peers. + * Be careful regenerating the bloom filter too often, as it decreases anonymity because remote nodes can + * compare transactions against both the new and old filters to significantly decrease the false positive rate. + * + * See the docs for {@link BloomFilter#BloomFilter(int, double)} for a brief explanation of anonymity when using bloom filters. + */ + public void setBloomFilterFalsePositiveRate(double bloomFilterFPRate) { + this.bloomFilterFPRate = bloomFilterFPRate; + recalculateFastCatchupAndFilter(); + } /** * Unlinks the given wallet so it no longer receives broadcast transactions or has its transactions announced.