TransactionBuilder: move ABSURD_FEE to a constructor parameter

This commit is contained in:
Daniel Cousens 2016-11-12 13:19:11 +11:00 committed by Thomas Kerin
parent 61be897429
commit aeeee4f3ec

View file

@ -228,16 +228,17 @@ function buildInput (input, allowIncomplete) {
return scriptSig return scriptSig
} }
function TransactionBuilder (network) { function TransactionBuilder (network, maximumFeeRate) {
this.prevTxMap = {} this.prevTxMap = {}
this.network = network || networks.bitcoin this.network = network || networks.bitcoin
// WARNING: This is __NOT__ to be relied on, its just another potential safety mechanism (safety in-depth)
this.maximumFeeRate = maximumFeeRate || 1000
this.inputs = [] this.inputs = []
this.tx = new Transaction() this.tx = new Transaction()
} }
TransactionBuilder.ABSURD_FEERATE = 1000
TransactionBuilder.prototype.setLockTime = function (locktime) { TransactionBuilder.prototype.setLockTime = function (locktime) {
typeforce(types.UInt32, locktime) typeforce(types.UInt32, locktime)
@ -409,7 +410,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
if (!allowIncomplete) { if (!allowIncomplete) {
// do not rely on this, its merely a last resort // do not rely on this, its merely a last resort
if (this.__hasAbsurdFeeRate(tx.byteLength())) { if (this.__overMaximumFees(tx.byteLength())) {
throw new Error('Transaction has absurd fees') throw new Error('Transaction has absurd fees')
} }
} }
@ -505,7 +506,7 @@ TransactionBuilder.prototype.__canModifyOutputs = function () {
}) })
} }
TransactionBuilder.prototype.__hasAbsurdFeeRate = function (bytes) { TransactionBuilder.prototype.__overMaximumFees = function (bytes) {
// not all inputs will have .value defined // not all inputs will have .value defined
var incoming = this.inputs.reduce(function (a, x) { return a + (x.value >>> 0) }, 0) var incoming = this.inputs.reduce(function (a, x) { return a + (x.value >>> 0) }, 0)
@ -515,7 +516,7 @@ TransactionBuilder.prototype.__hasAbsurdFeeRate = function (bytes) {
var fee = incoming - outgoing var fee = incoming - outgoing
var feeRate = fee / bytes var feeRate = fee / bytes
return feeRate > TransactionBuilder.ABSURD_FEERATE return feeRate > this.maximumFeeRate
} }
module.exports = TransactionBuilder module.exports = TransactionBuilder