From 5e330c513e65114ca43b1bbb1eed06851fd45c50 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 20 Jun 2016 03:16:23 -0700 Subject: [PATCH] dust threshold. misc. --- lib/bcoin/output.js | 10 ++++++- lib/bcoin/script.js | 72 ++++++++++++++++++++------------------------- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 6a976ff6..c8526d5c 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -178,6 +178,7 @@ Output.prototype.toJSON = function toJSON() { */ Output.prototype.getDustThreshold = function getDustThreshold(rate) { + var scale = constants.WITNESS_SCALE_FACTOR; var size; if (rate == null) @@ -186,7 +187,14 @@ Output.prototype.getDustThreshold = function getDustThreshold(rate) { if (this.script.isUnspendable()) return 0; - size = this.getSize() + 148; + size = this.getSize(); + + if (this.script.isProgram()) { + // 75% segwit discount applied to script size. + size += 32 + 4 + 1 + (107 / scale | 0) + 4; + } else { + size += 32 + 4 + 1 + 107 + 4; + } return 3 * bcoin.tx.getMinFee(size, rate); }; diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 3ee1dbf6..ad6c4051 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -45,6 +45,14 @@ function Witness(options) { this.fromOptions(options); } +Witness.prototype.__defineGetter__('length', function() { + return this.items.length; +}); + +Witness.prototype.__defineSetter__('length', function(length) { + return this.items.length = length; +}); + /** * Inject properties from options object. * @private @@ -435,14 +443,6 @@ Witness.prototype.set = function set(i, data) { this.items[i] = Witness.encodeItem(data); }; -Witness.prototype.__defineGetter__('length', function() { - return this.items.length; -}); - -Witness.prototype.__defineSetter__('length', function(length) { - return this.items.length = length; -}); - /** * Encode a witness item. * @param {Number|String|Buffer|BN} data @@ -576,6 +576,14 @@ function Stack(items) { this.items = items || []; } +Stack.prototype.__defineGetter__('length', function() { + return this.items.length; +}); + +Stack.prototype.__defineSetter__('length', function(length) { + return this.items.length = length; +}); + /** * Inspect the stack. * @returns {String} Human-readable stack. @@ -604,14 +612,6 @@ Stack.prototype.toASM = function toASM(decode) { return Script.formatASM(this.items, decode); }; -Stack.prototype.__defineGetter__('length', function() { - return this.items.length; -}); - -Stack.prototype.__defineSetter__('length', function(length) { - return this.items.length = length; -}); - /** * Pop the redeem script off the stack and deserialize it. * @returns {Script|null} The redeem script. @@ -746,6 +746,13 @@ Stack.prototype.set = function set(i, value) { return this.items[i] = value; }; +/** + * Swap stack values. + * @private + * @param {Number} i1 - Index 1. + * @param {Number} i2 - Index 2. + */ + Stack.prototype._swap = function _swap(i1, i2) { var v1, v2; @@ -795,7 +802,7 @@ Stack.prototype.ifdup = function ifdup() { throw new ScriptError('INVALID_STACK_OPERATION', opcodes.OP_IFDUP); if (Script.bool(this.top(-1))) - this.push(Script.array(this.top(-1))); + this.push(this.top(-1)); }; /** @@ -1090,6 +1097,14 @@ function Script(options) { this.fromOptions(options); } +Script.prototype.__defineGetter__('length', function() { + return this.code.length; +}); + +Script.prototype.__defineSetter__('length', function(length) { + return this.code.length = length; +}); + /** * Inject properties from options object. * @private @@ -3309,14 +3324,6 @@ Script.prototype.set = function set(i, data) { this.code[i] = Opcode.from(data); }; -Script.prototype.__defineGetter__('length', function() { - return this.code.length; -}); - -Script.prototype.__defineSetter__('length', function(length) { - return this.code.length = length; -}); - /** * Test whether the data element is a ripemd160 hash. * @param {Buffer?} hash @@ -3939,21 +3946,6 @@ Script.getSmall = function getSmall(op) { return -1; }; -/** - * Convert a number to a small integer (OP_0-OP_16). - * @param {Number} num - * @returns {Number} opcode - */ - -Script.toSmall = function toSmall(op) { - assert(op >= 0 && op <= 16); - - if (op === 0) - return opcodes.OP_0; - - return op + 0x50; -}; - /** * Verify an input and output script, and a witness if present. * @param {Script} input