Fix wrong balance calculation if identical (apart from the index) outputs are involved.

This regression was triggered by a53b508049, though before that change it only worked by luck.
This commit is contained in:
Andreas Schildbach
2016-05-25 14:19:13 +02:00
parent 0fb042b38f
commit a71da1b859
2 changed files with 12 additions and 2 deletions

View File

@@ -419,8 +419,8 @@ public class TransactionOutput extends ChildMessage {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TransactionOutput other = (TransactionOutput) o;
return value == other.value && (parent == null || parent == other.parent)
&& Arrays.equals(scriptBytes, other.scriptBytes);
return value == other.value && (parent == null || (parent == other.parent && getIndex() == other.getIndex()))
&& Arrays.equals(scriptBytes, other.scriptBytes);
}
@Override

View File

@@ -545,6 +545,16 @@ public class WalletTest extends TestWithWallet {
assertEquals(v4, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
}
@Test
public void balanceWithIdenticalOutputs() {
assertEquals(Coin.ZERO, wallet.getBalance(BalanceType.ESTIMATED));
Transaction tx = new Transaction(PARAMS);
tx.addOutput(Coin.COIN, myAddress);
tx.addOutput(Coin.COIN, myAddress); // identical to the above
wallet.addWalletTransaction(new WalletTransaction(Pool.UNSPENT, tx));
assertEquals(Coin.COIN.plus(Coin.COIN), wallet.getBalance(BalanceType.ESTIMATED));
}
// Intuitively you'd expect to be able to create a transaction with identical inputs and outputs and get an
// identical result to Bitcoin Core. However the signatures are not deterministic - signing the same data
// with the same key twice gives two different outputs. So we cannot prove bit-for-bit compatibility in this test