diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 923371ec..e89a1509 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -69,11 +69,8 @@ function TX(data, block) { this.ps = this.ts === 0 ? utils.now() : 0; // Discourage fee snipping a la bitcoind - // if (data.lock == null && this.chain) { - // this.lock = this.chain.height(); - // if ((Math.random() * 10 | 0) === 0) - // this.lock = Math.max(0, this.lock - (Math.random() * 100 | 0)); - // } + // if (data.lock == null) + // this._avoidFeeSnipping(); } TX.prototype.clone = function clone() { @@ -136,14 +133,6 @@ TX.prototype._input = function _input(obj, index) { seq: options.seq }); - // Locktime won't work without a seq < uint32max - // if (this.lock !== 0) - // input.seq = 0; - - // Replace by fee opt-in - // if (this.rbf) - // input.seq = 0xffffffff - 1; - // Try modifying existing input first i = this._inputIndex(input.out.hash, input.out.index); if (i !== -1) { @@ -958,6 +947,9 @@ TX.prototype.getUnspent = function getUnspent(unspent, address, fee) { TX.prototype.fillUnspent = function fillUnspent(unspent, address, fee) { var result; + if (unspent) + this.unspent = unspent; + if (address) this.changeAddress = address; @@ -966,7 +958,7 @@ TX.prototype.fillUnspent = function fillUnspent(unspent, address, fee) { assert(this.changeAddress); - result = this.getUnspent(unspent, this.changeAddress, this.hardFee); + result = this.getUnspent(this.unspent, this.changeAddress, this.hardFee); if (!result.inputs) return result; @@ -1013,6 +1005,9 @@ TX.prototype._recalculateFee = function recalculateFee() { real = Math.ceil(size / 1024) * constants.tx.fee; fee = this.getFee().toNumber(); + // if (this.hardFee) + // real = this.hardFee; + if (real === fee) { if (this.changeIndex === -1) this.outputs.pop(); @@ -1076,6 +1071,40 @@ TX.prototype.funds = function funds(side) { return acc; }; +TX.prototype._avoidFeeSnipping = function _avoidFeeSnipping() { + if (!this.chain) + return; + + this.lock = this.chain.height(); + + if ((Math.random() * 10 | 0) === 0) + this.lock = Math.max(0, this.lock - (Math.random() * 100 | 0)); +}; + +TX.prototype.setLockTime = function setLockTime(lock) { + var i, input; + + this.lock = lock; + + for (i = 0; i < this.inputs.length; i++) { + input = this.inputs[i]; + if (input.seq === 0xffffffff) + input.seq = 0; + } +}; + +TX.prototype.increaseFee = function increaseFee(fee) { + var i, input; + + this.hardFee = fee || this.getFee().add(new bn(10000)); + this.fillUnspent(); + + for (i = 0; i < this.inputs.length; i++) { + input = this.inputs[i]; + input.seq = 0xffffffff - 1; + } +}; + TX.prototype.full = function full() { if (this.inputs.length === 0) return false;