Browse Source

fixed issues with integration tests that use the faucet

hk-custom-address
Ruben de Vries 9 years ago
parent
commit
0e1424c2cb
  1. 17
      test/integration/_blockchain.js
  2. 22
      test/integration/multisig.js

17
test/integration/_blockchain.js

@ -8,7 +8,6 @@ var mainnet = new Blockchain('https://api.blocktrail.com/cb/v0.2.1/BTC', { api_k
var testnet = new Blockchain('https://api.blocktrail.com/cb/v0.2.1/tBTC', { 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, done) { testnet.faucet = function faucet (address, amount, done) {
async.retry(5, function (callback) {
httpify({ httpify({
method: 'POST', method: 'POST',
url: 'https://api.blocktrail.com/v1/tBTC/faucet/withdrawl?api_key=' + BLOCKTRAIL_API_KEY, url: 'https://api.blocktrail.com/v1/tBTC/faucet/withdrawl?api_key=' + BLOCKTRAIL_API_KEY,
@ -17,21 +16,29 @@ testnet.faucet = function faucet (address, amount, done) {
address: address, address: address,
amount: amount amount: amount
}) })
}, function (err) { }, function (err, result) {
if (err) return callback(err) if (err) return done(err)
if (result.body.code === 401) {
return done(new Error('Hit faucet rate limit; ' + result.body.msg))
}
// allow for TX to be processed
async.retry(5, function (callback) {
setTimeout(function () {
testnet.addresses.unspents(address, function (err, result) { testnet.addresses.unspents(address, function (err, result) {
if (err) return callback(err) if (err) return callback(err)
var unspent = result.filter(function (unspent) { var unspent = result.filter(function (unspent) {
return unspent.value > 1e3 return unspent.value >= amount
}).pop() }).pop()
if (!unspent) return callback(new Error('No unspent given')) if (!unspent) return callback(new Error('No unspent given'))
callback(null, unspent) callback(null, unspent)
}) })
}) }, 600)
}, done) }, done)
})
} }
module.exports = { module.exports = {

22
test/integration/multisig.js

@ -1,5 +1,6 @@
/* global describe, it */ /* global describe, it */
var async = require('async')
var assert = require('assert') var assert = require('assert')
var bitcoin = require('../../') var bitcoin = require('../../')
var blockchain = require('./_blockchain') var blockchain = require('./_blockchain')
@ -40,6 +41,9 @@ describe('bitcoinjs-lib (multisig)', function () {
blockchain.t.faucet(address, 2e4, function (err, unspent) { blockchain.t.faucet(address, 2e4, function (err, unspent) {
if (err) return done(err) if (err) return done(err)
var fee = 1e4
var targetValue = unspent.value - fee
// make a random destination address // make a random destination address
var targetAddress = bitcoin.ECPair.makeRandom({ var targetAddress = bitcoin.ECPair.makeRandom({
network: bitcoin.networks.testnet network: bitcoin.networks.testnet
@ -47,7 +51,7 @@ describe('bitcoinjs-lib (multisig)', function () {
var txb = new bitcoin.TransactionBuilder(bitcoin.networks.testnet) var txb = new bitcoin.TransactionBuilder(bitcoin.networks.testnet)
txb.addInput(unspent.txId, unspent.vout) txb.addInput(unspent.txId, unspent.vout)
txb.addOutput(targetAddress, 1e4) txb.addOutput(targetAddress, targetValue)
// sign with 1st and 3rd key // sign with 1st and 3rd key
txb.sign(0, keyPairs[0], redeemScript) txb.sign(0, keyPairs[0], redeemScript)
@ -60,16 +64,22 @@ describe('bitcoinjs-lib (multisig)', function () {
blockchain.t.transactions.propagate(tx.toHex(), function (err) { blockchain.t.transactions.propagate(tx.toHex(), function (err) {
if (err) return done(err) if (err) return done(err)
// allow for TX to be processed
async.retry(5, function (callback) {
setTimeout(function () {
// check that the above transaction included the intended address // check that the above transaction included the intended address
blockchain.t.addresses.unspents(targetAddress, function (err, unspents) { blockchain.t.addresses.unspents(targetAddress, function (err, unspents) {
if (err) return done(err) if (err) return callback(err)
assert(unspents.some(function (unspent) { var unspentFound = unspents.some(function (unspent) {
return unspent.txId === txId && unspent.value === 1e4 return unspent.txId === txId && unspent.value === targetValue
})) })
done() if (!unspentFound) return callback(new Error('Could not find unspent after propagate'))
callback()
}) })
}, 600)
}, done)
}) })
}) })
}) })

Loading…
Cancel
Save