Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove default sizes #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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)
}
48 changes: 42 additions & 6 deletions test/_utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
function expand (values, indices) {
function addScriptLength (values, scriptLength) {
return values.map(function (x) {
if (x.script === undefined) {
x.script = { length: scriptLength }
}
return x
})
}

function addScriptLengthToExpected (expected, inputLength, outputLength) {
var newExpected = Object.assign({}, expected)

if (expected.inputs != null) {
newExpected.inputs = expected.inputs.map(function (input) {
var newInput = Object.assign({}, input)
if (newInput.script == null) {
newInput.script = {length: inputLength}
}
return newInput
})
}

if (expected.outputs != null) {
newExpected.outputs = expected.outputs.map(function (output) {
var newOutput = Object.assign({}, output)
if (newOutput.script == null) {
newOutput.script = {length: outputLength}
}
return newOutput
})
}

return newExpected
}

function expand (values, indices, scriptLength) {
if (indices) {
return values.map(function (x, i) {
return addScriptLength(values.map(function (x, i) {
if (typeof x === 'number') return { i: i, value: x }

var y = { i: i }
for (var k in x) y[k] = x[k]
return y
})
}), scriptLength)
}

return values.map(function (x, i) {
return addScriptLength(values.map(function (x, i) {
return typeof x === 'object' ? x : { value: x }
})
}), scriptLength)
}

function testValues (t, actual, expected) {
Expand All @@ -35,5 +70,6 @@ function testValues (t, actual, expected) {

module.exports = {
expand: expand,
testValues: testValues
testValues: testValues,
addScriptLengthToExpected: addScriptLengthToExpected
}
17 changes: 11 additions & 6 deletions test/accumulative.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ var utils = require('./_utils')

fixtures.forEach(function (f) {
tape(f.description, function (t) {
var inputs = utils.expand(f.inputs, true)
var outputs = utils.expand(f.outputs)
var actual = coinAccum(inputs, outputs, f.feeRate)
var inputLength = f.inputLength
var outputLength = f.outputLength

t.same(actual, f.expected)
var inputs = utils.expand(f.inputs, true, inputLength)
var outputs = utils.expand(f.outputs, false, outputLength)
var expected = utils.addScriptLengthToExpected(f.expected, inputLength, outputLength)

var actual = coinAccum(inputs, outputs, f.feeRate, inputLength, outputLength)

t.same(actual, expected)
if (actual.inputs) {
var feedback = coinAccum(actual.inputs, actual.outputs, f.feeRate)
t.same(feedback, f.expected)
var feedback = coinAccum(actual.inputs, actual.outputs, f.feeRate, inputLength, outputLength)
t.same(feedback, expected)
}

t.end()
Expand Down
13 changes: 9 additions & 4 deletions test/break.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ var utils = require('./_utils')

fixtures.forEach(function (f) {
tape(f.description, function (t) {
var finputs = utils.expand(f.inputs)
var foutputs = utils.expand([f.output])
var actual = coinBreak(finputs, foutputs[0], f.feeRate)
var inputLength = f.inputLength
var outputLength = f.outputLength

t.same(actual, f.expected)
var inputs = utils.expand(f.inputs, false, inputLength)
var outputs = utils.expand([f.output], false, outputLength)
var expected = utils.addScriptLengthToExpected(f.expected, inputLength, outputLength)

var actual = coinBreak(inputs, outputs[0], f.feeRate, inputLength, outputLength)

t.same(actual, expected)
t.end()
})
})
Loading