Permalink
Browse files

Fix a bug in Transaction.getValueSentFromMe() in which inputs connect…

…ed to outputs that existed in the wallet but were not actually owned by us were counted. Resolves issue 36. Patch from Jan Møller.

git-svn-id: http://bitcoinj.googlecode.com/svn/trunk@124 a743e126-8abc-4207-e589-8f9b47be99bf
  • Loading branch information...
hearn@google.com
hearn@google.com committed Jul 6, 2011
1 parent 4c9b7f5 commit 0d4657ac15b3e630aae72ec35f6ea9935a3d7c3f
Showing with 25 additions and 2 deletions.
  1. +2 −1 AUTHORS
  2. +4 −0 src/com/google/bitcoin/core/Transaction.java
  3. +19 −1 tests/com/google/bitcoin/core/WalletTest.java
View
@@ -5,4 +5,5 @@ Thilo Planz <thilo@cpan.org>
Micheal Swiggs <bobby.simpson87@gmail.com>
Gary Rowe <g.rowe@froot.co.uk>
Noa Resare <noa@resare.com>
-John Sample <jwsample@gmail.com>
+John Sample <jwsample@gmail.com>
+Jan Møller <jan.moller@gmail.com>
@@ -170,6 +170,10 @@ public BigInteger getValueSentFromMe(Wallet wallet) throws ScriptException {
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;
@@ -35,10 +35,11 @@
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 void balances() throws Exception {
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

0 comments on commit 0d4657a

Please sign in to comment.