From d68509da1dbe6742bbcf5b7337fde0adc548dd74 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 14 May 2016 14:53:40 -0700 Subject: [PATCH] comments. refactor. --- lib/bcoin/output.js | 19 ++++++++++++++++--- lib/bcoin/tx.js | 7 +++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index f81aa24d..7e062715 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -10,6 +10,7 @@ var bn = require('bn.js'); var utils = require('./utils'); var assert = utils.assert; var BufferWriter = require('./writer'); +var Framer = bcoin.protocol.framer; /** * Represents a transaction output. @@ -152,8 +153,14 @@ Output.prototype.toJSON = function toJSON() { }; }; +/** + * Calculate the dust threshold for this + * output, based on serialize size and rate. + * @param {Number?} rate + * @returns {BN} + */ + Output.prototype.getDustThreshold = function getDustThreshold(rate) { - var framer = bcoin.protocol.framer; var size; if (rate == null) @@ -162,12 +169,18 @@ Output.prototype.getDustThreshold = function getDustThreshold(rate) { if (this.script.isUnspendable()) return new bn(0); - size = framer.output(this, new BufferWriter()).written; + size = Framer.output(this, new BufferWriter()).written; size += 148; return bcoin.tx.getMinFee(size, rate).muln(3); }; +/** + * Test whether the output should be considered dust. + * @param {Number?} rate + * @returns {Boolean} + */ + Output.prototype.isDust = function isDust(rate) { return this.value.cmp(this.getDustThreshold(rate)) < 0; }; @@ -203,7 +216,7 @@ Output.fromJSON = function fromJSON(json) { */ Output.prototype.toRaw = function toRaw(enc) { - var data = bcoin.protocol.framer.output(this); + var data = Framer.output(this); if (enc === 'hex') data = data.toString('hex'); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index ce094d29..a995fa5e 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1470,6 +1470,13 @@ TX.prototype.getMinFee = function getMinFee(size, rate) { return TX.getMinFee(size, rate); }; +/** + * Calculate minimum fee based on rate and size. + * @param {Number?} size + * @param {Number?} rate - Rate of satoshi per kB. + * @returns {BN} fee + */ + TX.getMinFee = function getMinFee(size, rate) { var fee;