tests: loop faucet until an unspent is given

This commit is contained in:
Daniel Cousens 2015-11-06 10:49:06 +11:00
parent d338c3872d
commit 74fd5ae71d
4 changed files with 89 additions and 85 deletions

View file

@ -72,7 +72,7 @@
"wif": "^1.1.0"
},
"devDependencies": {
"async": "^0.9.0",
"async": "^1.5.0",
"blanket": "^1.1.0",
"browserify": "^10.0.0",
"bs58": "^2.0.1",

View file

@ -1,3 +1,4 @@
var async = require('async')
var Blockchain = require('cb-http-client')
var httpify = require('httpify')
@ -5,7 +6,13 @@ var BLOCKTRAIL_API_KEY = process.env.BLOCKTRAIL_API_KEY || 'c0bd8155c66e3fb148bb
var mainnet = new Blockchain('https://api.blocktrail.com/cb/v0.2.1/BTC', { api_key: BLOCKTRAIL_API_KEY })
var testnet = new Blockchain('https://api.blocktrail.com/cb/v0.2.1/tBTC', { api_key: BLOCKTRAIL_API_KEY })
testnet.faucet = function faucet (address, amount, callback) {
testnet.faucet = function faucet (address, amount, done) {
var unspents = []
async.whilst(
function condition () { return unspents.length === 0 },
function f (callback) {
httpify({
method: 'POST',
url: 'https://api.blocktrail.com/v1/tBTC/faucet/withdrawl?api_key=' + BLOCKTRAIL_API_KEY,
@ -14,7 +21,27 @@ testnet.faucet = function faucet (address, amount, callback) {
address: address,
amount: amount
})
}, callback)
}, function (err) {
if (err) return callback(err)
testnet.addresses.unspents(address, function (err, result) {
if (err) return callback(err)
// filter small unspents
unspents = result.filter(function (unspent) {
return unspent.value > 1e3
})
callback()
})
})
},
function (err) {
if (err) return done(err)
done(null, unspents)
}
)
}
module.exports = {

View file

@ -22,27 +22,17 @@ describe('bitcoinjs-lib (advanced)', function () {
})
it('can create an OP_RETURN transaction', function (done) {
this.timeout(20000)
this.timeout(30000)
var network = bitcoin.networks.testnet
var keyPair = bitcoin.ECPair.makeRandom({ network: network })
var address = keyPair.getAddress()
blockchain.t.faucet(address, 2e4, function (err) {
blockchain.t.faucet(address, 2e4, function (err, unspents) {
if (err) return done(err)
blockchain.t.addresses.unspents(address, function (err, unspents) {
if (err) return done(err)
// filter small unspents
unspents = unspents.filter(function (unspent) {
return unspent.value > 1e4
})
// use the oldest unspent
var unspent = unspents.pop()
if (!unspent) throw new Error('Faucet didn\'t provide an unspent')
var tx = new bitcoin.TransactionBuilder(network)
var data = new Buffer('bitcoinjs-lib')
var dataScript = bitcoin.script.nullDataOutput(data)
@ -73,4 +63,3 @@ describe('bitcoinjs-lib (advanced)', function () {
})
})
})
})

View file

@ -37,23 +37,12 @@ describe('bitcoinjs-lib (multisig)', function () {
var address = bitcoin.address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet)
// attempt to send funds to the source address
blockchain.t.faucet(address, 2e4, function (err) {
blockchain.t.faucet(address, 2e4, function (err, unspents) {
if (err) return done(err)
// get latest unspents from the address
blockchain.t.addresses.unspents(address, function (err, unspents) {
if (err) return done(err)
// filter small unspents
unspents = unspents.filter(function (unspent) {
return unspent.value > 1e4
})
// use the oldest unspent
var unspent = unspents.pop()
if (!unspent) throw new Error('Faucet didn\'t provide an unspent')
// make a random destination address
var targetAddress = bitcoin.ECPair.makeRandom({
network: bitcoin.networks.testnet
@ -88,4 +77,3 @@ describe('bitcoinjs-lib (multisig)', function () {
})
})
})
})