From 735feab7babebb6f14e0f3c4484bf774832dcb78 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sun, 17 Aug 2014 15:16:45 +1000 Subject: [PATCH] Wallet: remove txId:index storage for spent outputs --- src/wallet.js | 13 +++++++++---- test/wallet.js | 13 +++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index af28be0..8177a35 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -96,7 +96,7 @@ Wallet.prototype.processConfirmedTx = function(tx){ } Wallet.prototype.__processTx = function(tx, isPending) { - var txid = tx.getId() + var txId = tx.getId() tx.outs.forEach(function(txOut, i) { var address @@ -109,7 +109,7 @@ Wallet.prototype.__processTx = function(tx, isPending) { var myAddresses = this.addresses.concat(this.changeAddresses) if (myAddresses.indexOf(address) > -1) { - var output = txid + ':' + i + var output = txId + ':' + i this.outputs[output] = { from: output, @@ -131,8 +131,9 @@ Wallet.prototype.__processTx = function(tx, isPending) { if (!(output in this.outputs)) return if (isPending) { - this.outputs[output].to = txid + ':' + i this.outputs[output].pending = true + this.outputs[output].spent = true + } else { delete this.outputs[output] } @@ -206,7 +207,11 @@ Wallet.prototype.getUnspentOutputs = function() { for(var key in this.outputs){ var output = this.outputs[key] - if(!output.to) utxo.push(outputToUnspentOutput(output)) + + // Don't include pending spent outputs + if (!output.spent) { + utxo.push(outputToUnspentOutput(output)) + } } return utxo diff --git a/test/wallet.js b/test/wallet.js index 8e30d33..9917c83 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -246,10 +246,10 @@ describe('Wallet', function() { assert.deepEqual(wallet.getUnspentOutputs(), [utxo]) }) - it("ignores pending spending outputs (outputs with 'to' property)", function() { + it("ignores pending spending outputs (outputs with 'spent' property)", function() { var output = wallet.outputs[expectedOutputKey] - output.to = fakeTxId(0) + ':' + 0 output.pending = true + output.spent = true assert.deepEqual(wallet.getUnspentOutputs(), []) }) }) @@ -331,18 +331,19 @@ describe('Wallet', function() { spendTx = Transaction.fromHex(fixtureTx2Hex) }) - it("outgoing: sets the pending flag and 'to' on output", function() { + it("outgoing: sets the pending flag and 'spent' on output", function() { var txIn = spendTx.ins[0] var txInId = new Buffer(txIn.hash) Array.prototype.reverse.call(txInId) txInId = txInId.toString('hex') var key = txInId + ':' + txIn.index - assert(!wallet.outputs[key].pending) + var output = wallet.outputs[key] + assert(!output.pending) wallet.processPendingTx(spendTx) - assert(wallet.outputs[key].pending) - assert.equal(wallet.outputs[key].to, spendTx.getId() + ':' + 0) + assert(output.pending) + assert(output.spent, true) }) }) })