diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 235db915..97f3f7e9 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -63,6 +63,7 @@ function MTX(options) { options = {}; this.version = options.version || 1; + this.flag = options.flag || 1; this.inputs = []; this.outputs = []; this.locktime = 0; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 1b8fb2fa..342008ff 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -56,6 +56,7 @@ function TX(data) { assert(data, 'TX data is required.'); assert(typeof data.version === 'number'); + assert(typeof data.flag === 'number'); assert(Array.isArray(data.inputs)); assert(Array.isArray(data.outputs)); assert(typeof data.locktime === 'number'); @@ -1429,7 +1430,8 @@ TX.prototype.getModifiedSize = function getModifiedSize(size) { * @param {Number?} size - Size to calculate priority * based on. If not present, modified size will be * calculated and used. - * @returns {Number} priority + * @returns {Object} data - Object containing + * `priority` and `value`. */ TX.prototype.getPriority = function getPriority(height, size) { @@ -1485,7 +1487,7 @@ TX.prototype.getPriority = function getPriority(height, size) { */ TX.prototype.isFree = function isFree(height, size) { - var priority; + var data; if (height == null) { height = this.height; @@ -1493,9 +1495,9 @@ TX.prototype.isFree = function isFree(height, size) { height = bcoin.network.get().height + 1; } - priority = this.getPriority(height, size).priority; + data = this.getPriority(height, size); - return priority > constants.tx.FREE_THRESHOLD; + return data.priority > constants.tx.FREE_THRESHOLD; }; /** @@ -1528,38 +1530,6 @@ TX.prototype.getRate = function getRate(size) { return TX.getRate(size, this.getFee()); }; -/** - * Calculate minimum fee based on rate and size. - * @param {Number?} size - * @param {Rate?} rate - Rate of satoshi per kB. - * @returns {Amount} fee - */ - -TX.getMinFee = function getMinFee(size, rate) { - var fee; - - if (rate == null) - rate = constants.tx.MIN_RELAY; - - fee = Math.floor(rate * size / 1000); - - if (fee === 0 && rate > 0) - fee = rate; - - return fee; -}; - -/** - * Calculate a fee rate based on size and fees. - * @param {Number} size - * @param {Amount} fee - * @returns {Rate} - */ - -TX.getRate = function(size, fee) { - return Math.floor(fee * 1000 / size); -}; - /** * Calculate the minimum fee in order for the transaction * to be relayable, but _round to the nearest kilobyte @@ -1730,6 +1700,38 @@ TX.prototype.__defineGetter__('wtxid', function() { return this.rwhash; }); +/** + * Calculate minimum fee based on rate and size. + * @param {Number?} size + * @param {Rate?} rate - Rate of satoshi per kB. + * @returns {Amount} fee + */ + +TX.getMinFee = function getMinFee(size, rate) { + var fee; + + if (rate == null) + rate = constants.tx.MIN_RELAY; + + fee = Math.floor(rate * size / 1000); + + if (fee === 0 && rate > 0) + fee = rate; + + return fee; +}; + +/** + * Calculate a fee rate based on size and fees. + * @param {Number} size + * @param {Amount} fee + * @returns {Rate} + */ + +TX.getRate = function(size, fee) { + return Math.floor(fee * 1000 / size); +}; + /** * Inspect the transaction and return a more * user-friendly representation of the data. @@ -1739,8 +1741,8 @@ TX.prototype.__defineGetter__('wtxid', function() { TX.prototype.inspect = function inspect() { return { type: 'tx', - hash: utils.revHex(this.hash('hex')), - witnessHash: utils.revHex(this.witnessHash('hex')), + hash: this.rhash, + witnessHash: this.rwhash, size: this.getSize(), virtualSize: this.maxSize(), height: this.height, @@ -1757,6 +1759,7 @@ TX.prototype.inspect = function inspect() { index: this.index, changeIndex: this.changeIndex || -1, version: this.version, + flag: this.flag, inputs: this.inputs, outputs: this.outputs, locktime: this.locktime @@ -1783,6 +1786,7 @@ TX.prototype.toJSON = function toJSON() { index: this.index, changeIndex: this.changeIndex || -1, version: this.version, + flag: this.flag, inputs: this.inputs.map(function(input) { return input.toJSON(); }), @@ -1811,6 +1815,7 @@ TX.parseJSON = function fromJSON(json) { index: json.index, changeIndex: json.changeIndex || -1, version: json.version, + flag: json.flag, inputs: json.inputs.map(function(input) { return bcoin.input.parseJSON(input); }), diff --git a/scripts/gen.js b/scripts/gen.js index 0def9546..0c5be4af 100644 --- a/scripts/gen.js +++ b/scripts/gen.js @@ -30,6 +30,7 @@ function createGenesisBlock(options) { tx = { version: 1, + flag: 1, inputs: [{ prevout: { hash: constants.NULL_HASH, diff --git a/test/script-test.js b/test/script-test.js index f92bff08..f1a51782 100644 --- a/test/script-test.js +++ b/test/script-test.js @@ -297,6 +297,7 @@ describe('Script', function() { it('should handle script test' + suffix + ': ' + comments, function() { var coin = bcoin.tx({ version: 1, + flag: 1, inputs: [{ prevout: { hash: constants.NULL_HASH, @@ -315,6 +316,7 @@ describe('Script', function() { }); var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [{ prevout: { hash: coin.hash('hex'), diff --git a/test/tx-test.js b/test/tx-test.js index 8940a984..834fe809 100644 --- a/test/tx-test.js +++ b/test/tx-test.js @@ -306,6 +306,7 @@ describe('TX', function() { it('should fail on >51 bit coin values', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [createInput(constants.MAX_MONEY + 1)], outputs: [{ script: [], @@ -320,6 +321,7 @@ describe('TX', function() { it('should handle 51 bit coin values', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [createInput(constants.MAX_MONEY)], outputs: [{ script: [], @@ -334,6 +336,7 @@ describe('TX', function() { it('should fail on >51 bit output values', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [createInput(constants.MAX_MONEY)], outputs: [{ script: [], @@ -348,6 +351,7 @@ describe('TX', function() { it('should handle 51 bit output values', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [createInput(constants.MAX_MONEY)], outputs: [{ script: [], @@ -362,6 +366,7 @@ describe('TX', function() { it('should fail on >51 bit fees', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [createInput(constants.MAX_MONEY + 1)], outputs: [{ script: [], @@ -376,6 +381,7 @@ describe('TX', function() { it('should fail on >51 bit values from multiple', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [ createInput(Math.floor(constants.MAX_MONEY / 2)), createInput(Math.floor(constants.MAX_MONEY / 2)), @@ -394,6 +400,7 @@ describe('TX', function() { it('should fail on >51 bit output values from multiple', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [createInput(constants.MAX_MONEY)], outputs: [ { @@ -418,6 +425,7 @@ describe('TX', function() { it('should fail on >51 bit fees from multiple', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [ createInput(Math.floor(constants.MAX_MONEY / 2)), createInput(Math.floor(constants.MAX_MONEY / 2)), @@ -439,6 +447,7 @@ describe('TX', function() { for (var i = 0; i < 3; i++) { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [ createInput(Math.floor(constants.MAX_MONEY / 2)) ], @@ -456,6 +465,7 @@ describe('TX', function() { it('should fail to parse >53 bit values', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [ createInput(Math.floor(constants.MAX_MONEY / 2)) ], @@ -483,6 +493,7 @@ describe('TX', function() { it('should fail on 53 bit coin values', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [createInput(utils.MAX_SAFE_INTEGER)], outputs: [{ script: [], @@ -497,6 +508,7 @@ describe('TX', function() { it('should fail on 53 bit output values', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [createInput(constants.MAX_MONEY)], outputs: [{ script: [], @@ -511,6 +523,7 @@ describe('TX', function() { it('should fail on 53 bit fees', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [createInput(utils.MAX_SAFE_INTEGER)], outputs: [{ script: [], @@ -526,6 +539,7 @@ describe('TX', function() { it('should fail on >53 bit values from multiple', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [ createInput(MAX), createInput(MAX), @@ -544,6 +558,7 @@ describe('TX', function() { it('should fail on >53 bit output values from multiple', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [createInput(constants.MAX_MONEY)], outputs: [ { @@ -568,6 +583,7 @@ describe('TX', function() { it('should fail on >53 bit fees from multiple', function() { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [ createInput(MAX), createInput(MAX), @@ -589,6 +605,7 @@ describe('TX', function() { for (var i = 0; i < 3; i++) { var tx = bcoin.tx({ version: 1, + flag: 1, inputs: [ createInput(MAX) ],