From 751434ba7c6d0f5cb4c074dafcd85956ac9b635b Mon Sep 17 00:00:00 2001 From: Devrandom Date: Sun, 8 Dec 2013 13:32:25 -0800 Subject: [PATCH] Fix bloom filter sizing with watched scripts --- core/src/main/java/com/google/bitcoin/core/Wallet.java | 10 +++++++--- .../test/java/com/google/bitcoin/core/WalletTest.java | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Wallet.java b/core/src/main/java/com/google/bitcoin/core/Wallet.java index cf0044cb..147c0c66 100644 --- a/core/src/main/java/com/google/bitcoin/core/Wallet.java +++ b/core/src/main/java/com/google/bitcoin/core/Wallet.java @@ -2956,7 +2956,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi for (Transaction tx : getTransactions(false)) { for (TransactionOutput out : tx.getOutputs()) { try { - if (out.isMine(this) && out.getScriptPubKey().isSentToRawPubKey()) + if (isTxOutputBloomFilterable(out)) size++; } catch (ScriptException e) { throw new RuntimeException(e); // If it is ours, we parsed the script correctly, so this shouldn't happen @@ -3026,8 +3026,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi for (int i = 0; i < tx.getOutputs().size(); i++) { TransactionOutput out = tx.getOutputs().get(i); try { - if ((out.isMine(this) && out.getScriptPubKey().isSentToRawPubKey()) || - out.isWatched(this)) { + if (isTxOutputBloomFilterable(out)) { TransactionOutPoint outPoint = new TransactionOutPoint(params, i, tx); filter.insert(outPoint.bitcoinSerialize()); } @@ -3040,6 +3039,11 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi return filter; } + private boolean isTxOutputBloomFilterable(TransactionOutput out) { + return (out.isMine(this) && out.getScriptPubKey().isSentToRawPubKey()) || + out.isWatched(this); + } + /** Returns the {@link CoinSelector} object which controls which outputs can be spent by this wallet. */ public CoinSelector getCoinSelector() { lock.lock(); diff --git a/core/src/test/java/com/google/bitcoin/core/WalletTest.java b/core/src/test/java/com/google/bitcoin/core/WalletTest.java index 6ee5b9a4..359a1dee 100644 --- a/core/src/test/java/com/google/bitcoin/core/WalletTest.java +++ b/core/src/test/java/com/google/bitcoin/core/WalletTest.java @@ -993,10 +993,14 @@ public class WalletTest extends TestWithWallet { @Test public void watchingScriptsSentFrom() throws Exception { + assertEquals(2, wallet.getBloomFilterElementCount()); + ECKey key = new ECKey(); ECKey notMyAddr = new ECKey(); Address watchedAddress = key.toAddress(params); wallet.addWatchedAddress(watchedAddress); + assertEquals(3, wallet.getBloomFilterElementCount()); + Transaction t1 = createFakeTx(params, CENT, watchedAddress); Transaction t2 = createFakeTx(params, COIN, notMyAddr); StoredBlock b1 = createFakeBlock(blockStore, t1).storedBlock; @@ -1006,7 +1010,9 @@ public class WalletTest extends TestWithWallet { st2.addInput(t1.getOutput(0)); st2.addInput(t2.getOutput(0)); wallet.receiveFromBlock(t1, b1, BlockChain.NewBlockType.BEST_CHAIN, 0); + assertEquals(4, wallet.getBloomFilterElementCount()); wallet.receiveFromBlock(st2, b1, BlockChain.NewBlockType.BEST_CHAIN, 0); + assertEquals(4, wallet.getBloomFilterElementCount()); assertEquals(CENT, st2.getValueSentFromMe(wallet)); }