Fix tests Missing Input

This commit is contained in:
junderw 2019-04-07 22:27:16 +09:00
parent 1c24201a46
commit 8ec1911a26
No known key found for this signature in database
GPG key ID: B256185D3A971908
7 changed files with 397 additions and 459 deletions
test/integration

View file

@ -1,99 +1,109 @@
const assert = require('assert')
const bitcoin = require('../../')
const dhttp = require('dhttp/200')
const dhttpCallback = require('dhttp/200')
// use Promises
const dhttp = options => new Promise((resolve, reject) => {
return dhttpCallback(options, (err, data) => {
if (err) return reject(err)
else return resolve(data)
})
})
const APIPASS = process.env.APIPASS || 'satoshi'
const APIURL = 'https://regtest.bitbank.cc/1'
const NETWORK = bitcoin.networks.testnet
function broadcast (txHex, callback) {
dhttp({
function broadcast (txHex) {
return dhttp({
method: 'POST',
url: APIURL + '/t/push',
body: txHex
}, callback)
}
function mine (count, callback) {
dhttp({
method: 'POST',
url: APIURL + '/r/generate?count=' + count + '&key=' + APIPASS
}, callback)
}
function height (callback) {
dhttp({
method: 'GET',
url: APIURL + '/b/best/height'
}, callback)
}
function faucet (address, value, callback) {
dhttp({
method: 'POST',
url: APIURL + '/r/faucet?address=' + address + '&value=' + value + '&key=' + APIPASS
}, function (err, txId) {
if (err) return callback(err)
unspents(address, function (err, results) {
if (err) return callback(err)
const unspents = results.filter(x => x.txId === txId)
if (unspents.length === 0) return callback(new Error('Missing unspent'))
callback(null, unspents.pop())
})
})
}
function faucetComplex (output, value, callback) {
function mine (count) {
return dhttp({
method: 'POST',
url: APIURL + '/r/generate?count=' + count + '&key=' + APIPASS
})
}
function height () {
return dhttp({
method: 'GET',
url: APIURL + '/b/best/height'
})
}
async function faucet (address, value) {
let count = 0
let _unspents = []
const sleep = ms => new Promise(r => setTimeout(r, ms))
do {
if (count > 0) {
if (count >= 5) throw new Error('Missing Inputs')
console.log('Missing Inputs, retry #' + count)
await sleep(200)
}
const txId = await dhttp({
method: 'POST',
url: APIURL + '/r/faucet?address=' + address + '&value=' + value + '&key=' + APIPASS
})
await sleep(100)
const results = await unspents(address)
_unspents = results.filter(x => x.txId === txId)
count++
} while (_unspents.length === 0)
return _unspents.pop()
}
async function faucetComplex (output, value) {
const keyPair = bitcoin.ECPair.makeRandom({ network: NETWORK })
const p2pkh = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: NETWORK })
faucet(p2pkh.address, value * 2, (err, unspent) => {
if (err) return callback(err)
const unspent = await faucet(p2pkh.address, value * 2)
const txvb = new bitcoin.TransactionBuilder(NETWORK)
txvb.addInput(unspent.txId, unspent.vout, null, p2pkh.output)
txvb.addOutput(output, value)
txvb.sign(0, keyPair)
const txv = txvb.build()
const txvb = new bitcoin.TransactionBuilder(NETWORK)
txvb.addInput(unspent.txId, unspent.vout, null, p2pkh.output)
txvb.addOutput(output, value)
txvb.sign(0, keyPair)
const txv = txvb.build()
broadcast(txv.toHex(), function (err) {
if (err) return callback(err)
await broadcast(txv.toHex())
return callback(null, {
txId: txv.getId(),
vout: 0,
value
})
})
})
return {
txId: txv.getId(),
vout: 0,
value
}
}
function fetch (txId, callback) {
dhttp({
function fetch (txId) {
return dhttp({
method: 'GET',
url: APIURL + '/t/' + txId + '/json'
}, callback)
})
}
function unspents (address, callback) {
dhttp({
function unspents (address) {
return dhttp({
method: 'GET',
url: APIURL + '/a/' + address + '/unspents'
}, callback)
})
}
function verify (txo, callback) {
fetch(txo.txId, function (err, tx) {
if (err) return callback(err)
async function verify (txo) {
const tx = await fetch(txo.txId)
const txoActual = tx.outs[txo.vout]
if (txo.address) assert.strictEqual(txoActual.address, txo.address)
if (txo.value) assert.strictEqual(txoActual.value, txo.value)
callback()
})
const txoActual = tx.outs[txo.vout]
if (txo.address) assert.strictEqual(txoActual.address, txo.address)
if (txo.value) assert.strictEqual(txoActual.value, txo.value)
}
function getAddress (node, network) {
@ -108,6 +118,7 @@ function randomAddress () {
module.exports = {
broadcast,
dhttp,
faucet,
faucetComplex,
fetch,