Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions accumulative.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var utils = require('./utils')

// add inputs until we reach or surpass the target value (or deplete)
// worst-case: O(n)
module.exports = function accumulative (utxos, outputs, feeRate) {
module.exports = function accumulative (utxos, outputs, feeRate, changeInputLengthEstimate, changeOutputLength) {
if (!isFinite(utils.uintOrNaN(feeRate))) return {}
var bytesAccum = utils.transactionBytes([], outputs)

Expand Down Expand Up @@ -31,7 +31,7 @@ module.exports = function accumulative (utxos, outputs, feeRate) {
// go again?
if (inAccum < outAccum + fee) continue

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

return { fee: feeRate * bytesAccum }
Expand Down
6 changes: 3 additions & 3 deletions blackjack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ var utils = require('./utils')

// only add inputs if they don't bust the target value (aka, exact match)
// worst-case: O(n)
module.exports = function blackjack (utxos, outputs, feeRate) {
module.exports = function blackjack (utxos, outputs, feeRate, changeInputLengthEstimate, changeOutputLength) {
if (!isFinite(utils.uintOrNaN(feeRate))) return {}

var bytesAccum = utils.transactionBytes([], outputs)

var inAccum = 0
var inputs = []
var outAccum = utils.sumOrNaN(outputs)
var threshold = utils.dustThreshold({}, feeRate)
var threshold = utils.dustThreshold(feeRate, changeInputLengthEstimate)

for (var i = 0; i < utxos.length; ++i) {
var input = utxos[i]
Expand All @@ -28,7 +28,7 @@ module.exports = function blackjack (utxos, outputs, feeRate) {
// go again?
if (inAccum < outAccum + fee) continue

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

return { fee: feeRate * bytesAccum }
Expand Down
5 changes: 2 additions & 3 deletions break.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var utils = require('./utils')

// break utxos into the maximum number of output possible
module.exports = function broken (utxos, output, feeRate) {
module.exports = function broken (utxos, output, feeRate, changeInputLengthEstimate, changeOutputLength) {
if (!isFinite(utils.uintOrNaN(feeRate))) return {}

var bytesAccum = utils.transactionBytes(utxos, [])
Expand Down Expand Up @@ -29,6 +29,5 @@ module.exports = function broken (utxos, output, feeRate) {
outAccum += value
outputs.push(output)
}

return utils.finalize(utxos, outputs, feeRate)
return utils.finalize(utxos, outputs, feeRate, changeInputLengthEstimate, changeOutputLength)
}
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ function utxoScore (x, feeRate) {
return x.value - (feeRate * utils.inputBytes(x))
}

module.exports = function coinSelect (utxos, outputs, feeRate) {
module.exports = function coinSelect (utxos, outputs, feeRate, changeInputLengthEstimate, changeOutputLength) {
Copy link
Contributor

@dcousens dcousens Aug 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we did this, it would have to be an options object with defaults.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

utxos = utxos.concat().sort(function (a, b) {
return utxoScore(b, feeRate) - utxoScore(a, feeRate)
})

// attempt to use the blackjack strategy first (no change output)
var base = blackjack(utxos, outputs, feeRate)
var base = blackjack(utxos, outputs, feeRate, changeInputLengthEstimate, changeOutputLength)
if (base.inputs) return base

// else, try the accumulative strategy
return accumulative(utxos, outputs, feeRate)
return accumulative(utxos, outputs, feeRate, changeInputLengthEstimate, changeOutputLength)
}
8 changes: 4 additions & 4 deletions split.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var utils = require('./utils')

// split utxos between each output, ignores outputs with .value defined
module.exports = function split (utxos, outputs, feeRate) {
module.exports = function split (utxos, outputs, feeRate, changeInputLengthEstimate, changeOutputLength) {
if (!isFinite(utils.uintOrNaN(feeRate))) return {}

var bytesAccum = utils.transactionBytes(utxos, outputs)
Expand All @@ -17,7 +17,7 @@ module.exports = function split (utxos, outputs, feeRate) {
return a + !isFinite(x.value)
}, 0)

if (remaining === 0 && unspecified === 0) return utils.finalize(utxos, outputs, feeRate)
if (remaining === 0 && unspecified === 0) return utils.finalize(utxos, outputs, feeRate, changeInputLengthEstimate, changeOutputLength)

var splitOutputsCount = outputs.reduce(function (a, x) {
return a + !x.value
Expand All @@ -26,7 +26,7 @@ module.exports = function split (utxos, outputs, feeRate) {

// ensure every output is either user defined, or over the threshold
if (!outputs.every(function (x) {
return x.value !== undefined || (splitValue > utils.dustThreshold(x, feeRate))
return x.value !== undefined || (splitValue > utils.dustThreshold(feeRate, changeInputLengthEstimate))
})) return { fee: fee }

// assign splitValue to outputs not user defined
Expand All @@ -40,5 +40,5 @@ module.exports = function split (utxos, outputs, feeRate) {
return y
})

return utils.finalize(utxos, outputs, feeRate)
return utils.finalize(utxos, outputs, feeRate, changeInputLengthEstimate, changeOutputLength)
}
22 changes: 9 additions & 13 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// baseline estimates, used to improve performance
var TX_EMPTY_SIZE = 4 + 1 + 1 + 4
var TX_INPUT_BASE = 32 + 4 + 1 + 4
var TX_INPUT_PUBKEYHASH = 107
var TX_OUTPUT_BASE = 8 + 1
var TX_OUTPUT_PUBKEYHASH = 25

function inputBytes (input) {
return TX_INPUT_BASE + (input.script ? input.script.length : TX_INPUT_PUBKEYHASH)
return TX_INPUT_BASE + input.script.length
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not clear what happens if the input provided has no script 🤔

Copy link

@itsMikeLowrey itsMikeLowrey Mar 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @dcousens has proposed that an option object with some defaults in case the user doesn't feed in input script and length.

}

function outputBytes (output) {
return TX_OUTPUT_BASE + (output.script ? output.script.length : TX_OUTPUT_PUBKEYHASH)
return TX_OUTPUT_BASE + output.script.length
}

function dustThreshold (output, feeRate) {
/* ... classify the output for input estimate */
return inputBytes({}) * feeRate
function dustThreshold (feeRate, inputLenghtEstimate) {
return inputBytes({script: {length: inputLenghtEstimate}}) * feeRate
}

function transactionBytes (inputs, outputs) {
Expand All @@ -40,16 +37,15 @@ function sumOrNaN (range) {
return range.reduce(function (a, x) { return a + uintOrNaN(x.value) }, 0)
}

var BLANK_OUTPUT = outputBytes({})

function finalize (inputs, outputs, feeRate) {
function finalize (inputs, outputs, feeRate, changeInputLengthEstimate, changeOutputLength) {
var bytesAccum = transactionBytes(inputs, outputs)
var feeAfterExtraOutput = feeRate * (bytesAccum + BLANK_OUTPUT)
var blankOutputBytes = outputBytes({script: {length: changeOutputLength}})
var feeAfterExtraOutput = feeRate * (bytesAccum + blankOutputBytes)
var remainderAfterExtraOutput = sumOrNaN(inputs) - (sumOrNaN(outputs) + feeAfterExtraOutput)

// is it worth a change output?
if (remainderAfterExtraOutput > dustThreshold({}, feeRate)) {
outputs = outputs.concat({ value: remainderAfterExtraOutput })
if (remainderAfterExtraOutput > dustThreshold(feeRate, changeInputLengthEstimate)) {
outputs = outputs.concat({ value: remainderAfterExtraOutput, script: {length: changeOutputLength} })
}

var fee = sumOrNaN(inputs) - sumOrNaN(outputs)
Expand Down