Skip to content

Commit

Permalink
Improve tx byte count estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
petejkim committed Feb 5, 2019
1 parent eabde80 commit b3c487a
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
// 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
var TX_BASE_SIZE = 10

var TX_INPUT_SIZE = {
LEGACY: 148,
P2SH: 92,
BECH32: 69
}

var TX_OUTPUT_SIZE = {
LEGACY: 34,
P2SH: 32,
BECH32: 31
}

function inputBytes (input) {
return TX_INPUT_BASE + TX_INPUT_PUBKEYHASH
return TX_INPUT_SIZE[input.type] || TX_INPUT_SIZE.LEGACY
}

function outputBytes (output) {
return TX_OUTPUT_BASE + TX_OUTPUT_PUBKEYHASH
return TX_OUTPUT_SIZE[output.type] || TX_OUTPUT_SIZE.LEGACY
}

function dustThreshold (output, feeRate) {
Expand All @@ -19,7 +27,7 @@ function dustThreshold (output, feeRate) {
}

function transactionBytes (inputs, outputs) {
return TX_EMPTY_SIZE +
return TX_BASE_SIZE +
inputs.reduce(function (a, x) { return a + inputBytes(x) }, 0) +
outputs.reduce(function (a, x) { return a + outputBytes(x) }, 0)
}
Expand Down

0 comments on commit b3c487a

Please sign in to comment.