Skip to content

Commit 37e53b5

Browse files
committed
TransactionBuilder: change to ABSURD_FEERATE, not ABSURD_FEE
1 parent d23d83c commit 37e53b5

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

src/transaction_builder.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,6 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
384384
if (!allowIncomplete) {
385385
if (!this.tx.ins.length) throw new Error('Transaction has no inputs')
386386
if (!this.tx.outs.length) throw new Error('Transaction has no outputs')
387-
388-
// do not rely on this, its merely a last resort
389-
if (this.__hasAbsurdFee()) throw new Error('Transaction has absurd fees')
390387
}
391388

392389
var tx = this.tx.clone()
@@ -408,6 +405,13 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
408405
tx.setInputScript(i, scriptSig)
409406
})
410407

408+
if (!allowIncomplete) {
409+
// do not rely on this, its merely a last resort
410+
if (this.__hasAbsurdFeeRate(tx.byteLength())) {
411+
throw new Error('Transaction has absurd fees')
412+
}
413+
}
414+
411415
return tx
412416
}
413417

@@ -499,18 +503,17 @@ TransactionBuilder.prototype.__canModifyOutputs = function () {
499503
})
500504
}
501505

502-
TransactionBuilder.prototype.__hasAbsurdFee = function () {
506+
TransactionBuilder.prototype.__hasAbsurdFeeRate = function (bytes) {
503507
// not all inputs will have .value defined
504508
var incoming = this.inputs.reduce(function (a, x) { return a + (x.value >>> 0) }, 0)
505509

506510
// but all outputs do, and if we have any input value
507511
// we can immediately determine if the outputs are too small
508512
var outgoing = this.tx.outs.reduce(function (a, x) { return a + x.value }, 0)
509513
var fee = incoming - outgoing
514+
var feeRate = fee / bytes
510515

511-
// its not fool-proof, but, it might help somebody
512-
// fee > 0.2BTC
513-
return fee > (0.2 * 1e8)
516+
return feeRate > 1000
514517
}
515518

516519
module.exports = TransactionBuilder

test/fixtures/transaction_builder.json

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,14 +666,28 @@
666666
"exception": "Transaction has absurd fees",
667667
"inputs": [
668668
{
669-
"txHex": "01000000000100e1f505000000000000000000",
670-
"vout": 0
669+
"txRaw": {
670+
"inputs": [],
671+
"outputs": [
672+
{
673+
"address": "1C5XhB1UkFuyCV1CG9dmXaXGu3xDL4nAjv",
674+
"value": 1000000000
675+
}
676+
],
677+
"incomplete": true
678+
},
679+
"vout": 0,
680+
"signs": [
681+
{
682+
"keyPair": "KzBQVXYUGDAvqG7VeU3C7ZMRYiwtsxSVVFcYGzKU9E4aUVDUquZU"
683+
}
684+
]
671685
}
672686
],
673687
"outputs": [
674688
{
675689
"script": "OP_DUP OP_HASH160 ff99e06c1a4ac394b4e1cb3d3a4b2b47749e339a OP_EQUALVERIFY OP_CHECKSIG",
676-
"value": 100000
690+
"value": 200000
677691
}
678692
]
679693
},

test/transaction_builder.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,31 @@ function construct (f, sign) {
2121
if (f.locktime !== undefined) txb.setLockTime(f.locktime)
2222

2323
f.inputs.forEach(function (input) {
24-
var prevTxScript
24+
var prevTx
25+
if (input.txRaw) {
26+
var constructed = construct(input.txRaw)
27+
if (input.txRaw.incomplete) prevTx = constructed.buildIncomplete()
28+
else prevTx = constructed.build()
29+
} else if (input.txHex) {
30+
prevTx = Transaction.fromHex(input.txHex)
31+
} else {
32+
prevTx = input.txId
33+
}
2534

26-
if (!input.txHex && input.prevTxScript) {
35+
var prevTxScript
36+
if (input.prevTxScript) {
2737
prevTxScript = bscript.fromASM(input.prevTxScript)
2838
}
2939

30-
txb.addInput(input.txId || Transaction.fromHex(input.txHex), input.vout, input.sequence, prevTxScript)
40+
txb.addInput(prevTx, input.vout, input.sequence, prevTxScript)
3141
})
3242

3343
f.outputs.forEach(function (output) {
34-
txb.addOutput(bscript.fromASM(output.script), output.value)
44+
if (output.address) {
45+
txb.addOutput(output.address, output.value)
46+
} else {
47+
txb.addOutput(bscript.fromASM(output.script), output.value)
48+
}
3549
})
3650

3751
if (sign === false) return txb

0 commit comments

Comments
 (0)