3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 07:12:17 +00:00

Change how wallet callbacks are run in the case of a tx that both sends and receives coins from the wallet (common case).

This commit is contained in:
Mike Hearn 2012-02-24 15:25:38 +01:00
parent c8e76a8f9b
commit 1df679cd17
2 changed files with 10 additions and 3 deletions

View File

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

View File

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