Wallet: remove txId:index storage for spent outputs
This commit is contained in:
parent
0f9674e65f
commit
735feab7ba
2 changed files with 16 additions and 10 deletions
|
@ -96,7 +96,7 @@ Wallet.prototype.processConfirmedTx = function(tx){
|
||||||
}
|
}
|
||||||
|
|
||||||
Wallet.prototype.__processTx = function(tx, isPending) {
|
Wallet.prototype.__processTx = function(tx, isPending) {
|
||||||
var txid = tx.getId()
|
var txId = tx.getId()
|
||||||
|
|
||||||
tx.outs.forEach(function(txOut, i) {
|
tx.outs.forEach(function(txOut, i) {
|
||||||
var address
|
var address
|
||||||
|
@ -109,7 +109,7 @@ Wallet.prototype.__processTx = function(tx, isPending) {
|
||||||
|
|
||||||
var myAddresses = this.addresses.concat(this.changeAddresses)
|
var myAddresses = this.addresses.concat(this.changeAddresses)
|
||||||
if (myAddresses.indexOf(address) > -1) {
|
if (myAddresses.indexOf(address) > -1) {
|
||||||
var output = txid + ':' + i
|
var output = txId + ':' + i
|
||||||
|
|
||||||
this.outputs[output] = {
|
this.outputs[output] = {
|
||||||
from: output,
|
from: output,
|
||||||
|
@ -131,8 +131,9 @@ Wallet.prototype.__processTx = function(tx, isPending) {
|
||||||
if (!(output in this.outputs)) return
|
if (!(output in this.outputs)) return
|
||||||
|
|
||||||
if (isPending) {
|
if (isPending) {
|
||||||
this.outputs[output].to = txid + ':' + i
|
|
||||||
this.outputs[output].pending = true
|
this.outputs[output].pending = true
|
||||||
|
this.outputs[output].spent = true
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
delete this.outputs[output]
|
delete this.outputs[output]
|
||||||
}
|
}
|
||||||
|
@ -206,7 +207,11 @@ Wallet.prototype.getUnspentOutputs = function() {
|
||||||
|
|
||||||
for(var key in this.outputs){
|
for(var key in this.outputs){
|
||||||
var output = this.outputs[key]
|
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
|
return utxo
|
||||||
|
|
|
@ -246,10 +246,10 @@ describe('Wallet', function() {
|
||||||
assert.deepEqual(wallet.getUnspentOutputs(), [utxo])
|
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]
|
var output = wallet.outputs[expectedOutputKey]
|
||||||
output.to = fakeTxId(0) + ':' + 0
|
|
||||||
output.pending = true
|
output.pending = true
|
||||||
|
output.spent = true
|
||||||
assert.deepEqual(wallet.getUnspentOutputs(), [])
|
assert.deepEqual(wallet.getUnspentOutputs(), [])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -331,18 +331,19 @@ describe('Wallet', function() {
|
||||||
spendTx = Transaction.fromHex(fixtureTx2Hex)
|
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 txIn = spendTx.ins[0]
|
||||||
var txInId = new Buffer(txIn.hash)
|
var txInId = new Buffer(txIn.hash)
|
||||||
Array.prototype.reverse.call(txInId)
|
Array.prototype.reverse.call(txInId)
|
||||||
txInId = txInId.toString('hex')
|
txInId = txInId.toString('hex')
|
||||||
|
|
||||||
var key = txInId + ':' + txIn.index
|
var key = txInId + ':' + txIn.index
|
||||||
assert(!wallet.outputs[key].pending)
|
var output = wallet.outputs[key]
|
||||||
|
|
||||||
|
assert(!output.pending)
|
||||||
wallet.processPendingTx(spendTx)
|
wallet.processPendingTx(spendTx)
|
||||||
assert(wallet.outputs[key].pending)
|
assert(output.pending)
|
||||||
assert.equal(wallet.outputs[key].to, spendTx.getId() + ':' + 0)
|
assert(output.spent, true)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue