Browse Source

Wallet: remove txId:index storage for spent outputs

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
735feab7ba
  1. 13
      src/wallet.js
  2. 13
      test/wallet.js

13
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

13
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)
})
})
})

Loading…
Cancel
Save