<commit_before>const utils = require('./utils')

// O(n * n)
module.exports = function blackjack (utxos, outputs, feeRate) {
  const outAccum = outputs.reduce((a, x) => a + x.value, 0)
  const threshold = utils.dustThreshold({}, feeRate)

  let inAccum = 0
  let bytesAccum = utils.transactionBytes([], outputs)

  // accumulate inputs until we bust
  let inputs = []

  for (let i = 0; i < utxos.length; ++i) {
    const utxo = utxos[i]
    const bytesAfter = utils.inputBytes(utxo)
    const fee = feeRate * (bytesAccum + bytesAfter)

    // are we wasting value?
    if (inAccum + utxo.value > outAccum + fee + threshold) continue

    inAccum += utxo.value
    bytesAccum += bytesAfter
    inputs.push(utxo)

    // go again?
    if (inAccum < outAccum + fee) continue

    return utils.finalize(inputs, outputs, feeRate)
  }

  return {
    fee: feeRate * utils.transactionBytes(inputs, outputs)
  }
}<commit_msg>blackjack: cleanup<commit_after>const utils = require('./utils')

// O(n * n)
module.exports = function blackjack (utxos, outputs, feeRate) {
  const outAccum = outputs.reduce((a, x) => a + x.value, 0)
  const threshold = utils.dustThreshold({}, feeRate)

  // accumulate inputs until we bust
  let inAccum = 0
  let bytesAccum = utils.transactionBytes([], outputs)
  let inputs = []

  for (let i = 0; i < utxos.length; ++i) {
    const input = utxos[i]
    const inputBytes = utils.inputBytes(input)
    const fee = feeRate * (bytesAccum + inputBytes)

    // would it waste value?
    if ((inAccum + input.value) > (outAccum + fee + threshold)) continue

    inAccum += input.value
    bytesAccum += inputBytes
    inputs.push(input)

    // go again?
    if (inAccum < outAccum + fee) continue

    return utils.finalize(inputs, outputs, feeRate)
  }

  const fee = feeRate * bytesAccum
  return { fee }
}
{'fraction_non_alphanumeric': 0.12753147235905857, 'fraction_numerical': 0.003284072249589491, 'mean_word_length': 3.1545454545454548, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '3335875', 'n_tokens_mistral': 630, 'n_tokens_neox': 613, 'n_words': 174}