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

Fix a bug in Transaction.getValueSentFromMe() in which inputs connected to outputs that existed in the wallet but were not actually owned by us were counted. Resolves issue 36. Patch from Jan Møller.

This commit is contained in:
Mike Hearn 2011-07-06 12:12:18 +00:00
parent bcff039a62
commit 2c74beb9f5
3 changed files with 25 additions and 2 deletions

View File

@ -6,3 +6,4 @@ Micheal Swiggs <bobby.simpson87@gmail.com>
Gary Rowe <g.rowe@froot.co.uk>
Noa Resare <noa@resare.com>
John Sample <jwsample@gmail.com>
Jan Møller <jan.moller@gmail.com>

View File

@ -170,6 +170,10 @@ public class Transaction extends Message implements Serializable {
connected = input.getConnectedOutput(wallet.pending);
if (connected == null)
continue;
// The connected output may be the change to the sender of a previous input sent to this wallet. In this
// case we ignore it.
if (!connected.isMine(wallet))
continue;
v = v.add(connected.getValue());
}
return v;

View File

@ -35,10 +35,11 @@ public class WalletTest {
private Address myAddress;
private Wallet wallet;
private BlockStore blockStore;
private ECKey myKey;
@Before
public void setUp() throws Exception {
ECKey myKey = new ECKey();
myKey = new ECKey();
myAddress = myKey.toAddress(params);
wallet = new Wallet(params);
wallet.addKey(myKey);
@ -212,6 +213,23 @@ public class WalletTest {
assertEquals(nanos, send2.getValueSentFromMe(wallet));
}
@Test
public void transactions() throws Exception {
// This test covers a bug in which Transaction.getValueSentFromMe was calculating incorrectly.
Transaction tx = createFakeTx(Utils.toNanoCoins(1, 0), myAddress);
// Now add another output (ie, change) that goes to some other address.
Address someOtherGuy = new ECKey().toAddress(params);
TransactionOutput output = new TransactionOutput(params, tx, Utils.toNanoCoins(0, 5), someOtherGuy);
tx.addOutput(output);
wallet.receive(tx, null, BlockChain.NewBlockType.BEST_CHAIN);
// Now the other guy creates a transaction which spends that change.
Transaction tx2 = new Transaction(params);
tx2.addInput(output);
tx2.addOutput(new TransactionOutput(params, tx2, Utils.toNanoCoins(0, 5), myAddress));
// tx2 doesn't send any coins from us, even though the output is in the wallet.
assertEquals(Utils.toNanoCoins(0, 0), tx2.getValueSentFromMe(wallet));
}
@Test
public void finneyAttack() throws Exception {
// A Finney attack is where a miner includes a transaction spending coins to themselves but does not