make wallet async functions truly async
This commit is contained in:
parent
7820ea7ea0
commit
4d4388f6bf
2 changed files with 42 additions and 32 deletions
|
@ -84,13 +84,14 @@ var Wallet = function (seed, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setUnspentOutputsAsync = function(utxo, callback) {
|
this.setUnspentOutputsAsync = function(utxo, callback) {
|
||||||
|
var error = null
|
||||||
try {
|
try {
|
||||||
this.setUnspentOutputs(utxo)
|
this.setUnspentOutputs(utxo)
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
return callback(err)
|
error = err
|
||||||
|
} finally {
|
||||||
|
process.nextTick(function(){ callback(error) })
|
||||||
}
|
}
|
||||||
|
|
||||||
return callback()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function outputToUnspentOutput(output){
|
function outputToUnspentOutput(output){
|
||||||
|
@ -206,14 +207,15 @@ var Wallet = function (seed, options) {
|
||||||
fixedFee = undefined
|
fixedFee = undefined
|
||||||
}
|
}
|
||||||
var tx = null
|
var tx = null
|
||||||
|
var error = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
tx = this.createTx(to, value, fixedFee)
|
tx = this.createTx(to, value, fixedFee)
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
return callback(err)
|
error = err
|
||||||
|
} finally {
|
||||||
|
process.nextTick(function(){ callback(error, tx) })
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(null, tx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.dustThreshold = 5430
|
this.dustThreshold = 5430
|
||||||
|
|
|
@ -284,24 +284,25 @@ describe('Wallet', function() {
|
||||||
wallet.setUnspentOutputs.restore()
|
wallet.setUnspentOutputs.restore()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('calls setUnspentOutputs', function(){
|
it('calls setUnspentOutputs', function(done){
|
||||||
sinon.stub(wallet, "setUnspentOutputs")
|
sinon.stub(wallet, "setUnspentOutputs")
|
||||||
|
|
||||||
var callback = sinon.spy()
|
var callback = function(){
|
||||||
var tx = wallet.setUnspentOutputsAsync(utxo, callback)
|
assert(wallet.setUnspentOutputs.calledWith(utxo))
|
||||||
|
done()
|
||||||
|
}
|
||||||
|
|
||||||
assert(wallet.setUnspentOutputs.calledWith(utxo))
|
wallet.setUnspentOutputsAsync(utxo, callback)
|
||||||
assert(callback.called)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('when setUnspentOutputs throws an error, it invokes callback with error', function(){
|
it('when setUnspentOutputs throws an error, it invokes callback with error', function(done){
|
||||||
sinon.stub(wallet, "setUnspentOutputs").throws()
|
sinon.stub(wallet, "setUnspentOutputs").throws()
|
||||||
|
|
||||||
var callback = sinon.spy()
|
var callback = function(err){
|
||||||
var tx = wallet.setUnspentOutputsAsync(utxo, callback)
|
assert(err instanceof Error)
|
||||||
|
done()
|
||||||
assert(callback.called)
|
}
|
||||||
assert(callback.args[0][0] instanceof Error)
|
wallet.setUnspentOutputsAsync(utxo, callback)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -549,34 +550,41 @@ describe('Wallet', function() {
|
||||||
wallet.createTx.restore()
|
wallet.createTx.restore()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('calls createTx', function(){
|
it('calls createTx', function(done){
|
||||||
sinon.stub(wallet, "createTx").returns("fakeTx")
|
sinon.stub(wallet, "createTx").returns("fakeTx")
|
||||||
|
|
||||||
var callback = sinon.spy()
|
var callback = function(err, tx){
|
||||||
var tx = wallet.createTxAsync(to, value, callback)
|
assert(wallet.createTx.calledWith(to, value))
|
||||||
|
assert.equal(err, null)
|
||||||
|
assert.equal(tx, "fakeTx")
|
||||||
|
done()
|
||||||
|
}
|
||||||
|
|
||||||
assert(wallet.createTx.calledWith(to, value))
|
wallet.createTxAsync(to, value, callback)
|
||||||
assert(callback.calledWith(null, "fakeTx"))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('calls createTx correctly when fee is specified', function(){
|
it('calls createTx correctly when fee is specified', function(done){
|
||||||
sinon.stub(wallet, "createTx").returns("fakeTx")
|
sinon.stub(wallet, "createTx").returns("fakeTx")
|
||||||
|
|
||||||
var callback = sinon.spy()
|
var callback = function(err, tx){
|
||||||
var tx = wallet.createTxAsync(to, value, fee, callback)
|
assert(wallet.createTx.calledWith(to, value, fee))
|
||||||
|
assert.equal(err, null)
|
||||||
|
assert.equal(tx, "fakeTx")
|
||||||
|
done()
|
||||||
|
}
|
||||||
|
|
||||||
assert(wallet.createTx.calledWith(to, value, fee))
|
wallet.createTxAsync(to, value, fee, callback)
|
||||||
assert(callback.calledWith(null, "fakeTx"))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('when createTx throws an error, it invokes callback with error', function(){
|
it('when createTx throws an error, it invokes callback with error', function(done){
|
||||||
sinon.stub(wallet, "createTx").throws()
|
sinon.stub(wallet, "createTx").throws()
|
||||||
|
|
||||||
var callback = sinon.spy()
|
var callback = function(err, tx){
|
||||||
var tx = wallet.createTxAsync(to, value, callback)
|
assert(err instanceof Error)
|
||||||
|
done()
|
||||||
|
}
|
||||||
|
|
||||||
assert(callback.called)
|
wallet.createTxAsync(to, value, callback)
|
||||||
assert(callback.args[0][0] instanceof Error)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue