Merge pull request #233 from weilu/pending-spending-utxo

wallet: reintroduce output.to to track pending spent utxo
This commit is contained in:
Daniel Cousens 2014-07-02 03:27:33 +10:00
commit ab20febbdc
2 changed files with 15 additions and 6 deletions

View file

@ -65,7 +65,7 @@ function Wallet(seed, network) {
for(var key in this.outputs){ for(var key in this.outputs){
var output = this.outputs[key] var output = this.outputs[key]
utxo.push(outputToUnspentOutput(output)) if(!output.to) utxo.push(outputToUnspentOutput(output))
} }
return utxo return utxo
@ -167,7 +167,7 @@ function Wallet(seed, network) {
} }
}) })
tx.ins.forEach(function(txIn) { tx.ins.forEach(function(txIn, i) {
// copy and convert to big-endian hex // copy and convert to big-endian hex
var txinId = new Buffer(txIn.hash) var txinId = new Buffer(txIn.hash)
Array.prototype.reverse.call(txinId) Array.prototype.reverse.call(txinId)
@ -178,10 +178,11 @@ function Wallet(seed, network) {
if (!(output in me.outputs)) return if (!(output in me.outputs)) return
if (isPending) { if (isPending) {
return me.outputs[output].pending = true me.outputs[output].to = txid + ':' + i
me.outputs[output].pending = true
} else {
delete me.outputs[output]
} }
delete me.outputs[output]
}) })
} }

View file

@ -215,6 +215,13 @@ describe('Wallet', function() {
it('parses wallet outputs to the expect format', function(){ it('parses wallet outputs to the expect format', function(){
assert.deepEqual(wallet.getUnspentOutputs(), [expectedUtxo]) assert.deepEqual(wallet.getUnspentOutputs(), [expectedUtxo])
}) })
it("ignores pending spending outputs (outputs with 'to' property)", function(){
var output = wallet.outputs[expectedOutputKey]
output.to = fakeTxId(0) + ':' + 0
output.pending = true
assert.deepEqual(wallet.getUnspentOutputs(), [])
})
}) })
describe('setUnspentOutputs', function(){ describe('setUnspentOutputs', function(){
@ -280,7 +287,7 @@ describe('Wallet', function() {
spendTx = Transaction.fromHex(fixtureTx2Hex) spendTx = Transaction.fromHex(fixtureTx2Hex)
}) })
it("outgoing: sets the pending flag on output instead of deleting it", function(){ it("outgoing: sets the pending flag and 'to' 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)
@ -291,6 +298,7 @@ describe('Wallet', function() {
wallet.processPendingTx(spendTx) wallet.processPendingTx(spendTx)
assert(wallet.outputs[key].pending) assert(wallet.outputs[key].pending)
assert.equal(wallet.outputs[key].to, spendTx.getId() + ':' + 0)
}) })
}) })
}) })