3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 15:22:16 +00:00

Fix bloom filter sizing with watched scripts

This commit is contained in:
Devrandom 2013-12-08 13:32:25 -08:00 committed by Mike Hearn
parent 606f199e73
commit 751434ba7c
2 changed files with 13 additions and 3 deletions

View File

@ -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();

View File

@ -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));
}