getCandidateOuputs ignores spent outputs

This commit is contained in:
Wei Lu 2014-03-25 10:35:33 +08:00
parent 756e877181
commit 77b5d1ee21
2 changed files with 29 additions and 10 deletions

View file

@ -207,7 +207,7 @@ var Wallet = function (seed, options) {
var unspent = []
for (var key in me.outputs){
var output = me.outputs[key]
if(!output.value.spend) unspent.push(output)
if(!output.spend) unspent.push(output)
}
var sortByValueDesc = unspent.sort(function(o1, o2){

View file

@ -356,15 +356,6 @@ describe('Wallet', function() {
}
]
wallet.setUnspentOutputs(utxo)
function scriptPubKeyFor(address, value){
var txOut = new TransactionOut({
value: value,
script: Script.createOutputScript(address)
})
return txOut.scriptPubKey()
}
})
describe('choosing utxo', function(){
@ -383,6 +374,25 @@ describe('Wallet', function() {
assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 })
assert.deepEqual(tx.ins[1].outpoint, { hash: fakeTxHash(2), index: 1 })
})
it('ignores spent outputs', function(){
utxo.push(
{
"hash": fakeTxHash(4),
"outputIndex": 0,
"scriptPubKey": scriptPubKeyFor(address2, 520000),
"address" : address2,
"value": 530000 // enough but spent before createTx
}
)
wallet.setUnspentOutputs(utxo)
wallet.outputs[fakeTxHash(4) + ":" + 0].spend = fakeTxHash(5) + ":" + 0
var tx = wallet.createTx(to, value)
assert.equal(tx.ins.length, 1)
assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 })
})
})
describe('transaction outputs', function(){
@ -467,6 +477,15 @@ describe('Wallet', function() {
function fakeTxHash(i) {
return "txtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtx" + i
}
function scriptPubKeyFor(address, value){
var txOut = new TransactionOut({
value: value,
script: Script.createOutputScript(address)
})
return txOut.scriptPubKey()
}
})
function assertEqual(obj1, obj2){