<commit_before>const assert = require('assert')
const bitcoin = require('../../')
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 || '<PASSWORD>'
const APIURL = 'https://regtest.bitbank.cc/1'
const NETWORK = bitcoin.networks.testnet

function broadcast (txHex) {
  return dhttp({
    method: 'POST',
    url: APIURL + '/t/push',
    body: txHex
  })
}

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'
  })
}

function _faucetRequest (address, value) {
  return dhttp({
    method: 'POST',
    url: APIURL + '/r/faucet?address=' + address + '&value=' + value + '&key=' + APIPASS
  })
}

async function faucet (address, value) {
  let count = 0
  let _unspents = []
  const sleep = ms => new Promise((resolve, reject) => setTimeout(resolve, ms))
  const randInt = (min, max) => min + Math.floor((max - min + 1) * Math.random())
  do {
    if (count > 0) {
      if (count >= 5) throw new Error('Missing Inputs')
      console.log('Missing Inputs, retry #' + count)
      await sleep(randInt(150, 250))
    }

    const txId = await _faucetRequest(address, value)
      .then(
        v => v, // Pass success value as is
        async err => {
          // Bad Request error is fixed by making sure height is >= 432
          const currentHeight = await height()
          if (err.message === 'Bad Request' && currentHeight < 432) {
            await mine(432 - currentHeight)
            return _faucetRequest(address, value)
          } else {
            throw err
          }
        }
      )

    await sleep(randInt(50, 150))

    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 })

  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()<commit_msg>Fix race condition for two integration test jobs<commit_after>const assert = require('assert')
const bitcoin = require('../../')
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 || '<PASSWORD>'
const APIURL = 'https://regtest.bitbank.cc/1'
const NETWORK = bitcoin.networks.testnet

function broadcast (txHex) {
  return dhttp({
    method: 'POST',
    url: APIURL + '/t/push',
    body: txHex
  })
}

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'
  })
}

function _faucetRequest (address, value) {
  return dhttp({
    method: 'POST',
    url: APIURL + '/r/faucet?address=' + address + '&value=' + value + '&key=' + APIPASS
  })
}

async function faucet (address, value) {
  let count = 0
  let _unspents = []
  const sleep = ms => new Promise((resolve, reject) => setTimeout(resolve, ms))
  const randInt = (min, max) => min + Math.floor((max - min + 1) * Math.random())
  do {
    if (count > 0) {
      if (count >= 5) throw new Error('Missing Inputs')
      console.log('Missing Inputs, retry #' + count)
      await sleep(randInt(150, 250))
    }

    const txId = await _faucetRequest(address, value)
      .then(
        v => v, // Pass success value as is
        async err => {
          // Bad Request error is fixed by making sure height is >= 432
          const currentHeight = await height()
          if (err.message === 'Bad Request' && currentHeight < 432) {
            await mine(432 - currentHeight)
            return _faucetRequest(address, value)
          } else if (err.message === 'Bad Request' && currentHeight >= 432) {
            return _faucetRequest(address, value)
          } else {
            throw err
          }
        }
      )

    await sleep(randInt(50, 150))

    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 })

  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()
{'fraction_non_alphanumeric': 0.1422680412371134, 'fraction_numerical': 0.013683223992502344, 'mean_word_length': 2.9062957540263543, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3194308', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1751, 'n_words': 482}