From 1df679cd177ce55c3dc4598c0de2eb0bcfeec42f Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 24 Feb 2012 15:25:38 +0100 Subject: [PATCH] Change how wallet callbacks are run in the case of a tx that both sends and receives coins from the wallet (common case). --- src/com/google/bitcoin/core/Wallet.java | 11 ++++++++--- tests/com/google/bitcoin/core/WalletTest.java | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/com/google/bitcoin/core/Wallet.java b/src/com/google/bitcoin/core/Wallet.java index dec6da21..f5a57831 100644 --- a/src/com/google/bitcoin/core/Wallet.java +++ b/src/com/google/bitcoin/core/Wallet.java @@ -510,10 +510,15 @@ public class Wallet implements Serializable { boolean wasPending = wtx != null; if (!reorg && bestChain && !wasPending) { BigInteger newBalance = getBalance(); - if (valueSentToMe.compareTo(BigInteger.ZERO) > 0) { + int diff = valueDifference.compareTo(BigInteger.ZERO); + // We pick one callback based on the value difference, though a tx can of course both send and receive + // coins from the wallet. + if (diff > 0) { invokeOnCoinsReceived(tx, prevBalance, newBalance); - } - if (valueSentFromMe.compareTo(BigInteger.ZERO) > 0) { + } else if (diff == 0) { + // Hack. Invoke onCoinsSent in order to let the client save the wallet. This needs to go away. + invokeOnCoinsSent(tx, prevBalance, newBalance); + } else { invokeOnCoinsSent(tx, prevBalance, newBalance); } } diff --git a/tests/com/google/bitcoin/core/WalletTest.java b/tests/com/google/bitcoin/core/WalletTest.java index bf9dab7a..165e393f 100644 --- a/tests/com/google/bitcoin/core/WalletTest.java +++ b/tests/com/google/bitcoin/core/WalletTest.java @@ -223,9 +223,11 @@ public class WalletTest { // Pretend it makes it into the block chain, our wallet state is cleared but we still have the keys, and we // want to get back to our previous state. We can do this by just not confirming the transaction as // createSend is stateless. + txn[0] = txn[1] = null; StoredBlock b2 = createFakeBlock(params, blockStore, send1).storedBlock; wallet.receiveFromBlock(send1, b2, BlockChain.NewBlockType.BEST_CHAIN); assertEquals(bitcoinValueToFriendlyString(wallet.getBalance()), "0.90"); + assertEquals(null, txn[0]); assertEquals(txn[1].getHash(), send1.getHash()); assertEquals(bitcoinValueToFriendlyString(bigints[2]), "1.00"); assertEquals(bitcoinValueToFriendlyString(bigints[3]), "0.90");