From 4314d0cd6a8b99a03dfc6d13fd80adafe46c9683 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 15 May 2016 20:27:50 -0700 Subject: [PATCH] more tx tests. comments. --- lib/bcoin/network.js | 6 +- test/tx-test.js | 159 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 161 insertions(+), 4 deletions(-) diff --git a/lib/bcoin/network.js b/lib/bcoin/network.js index 090ebe4c..f410da36 100644 --- a/lib/bcoin/network.js +++ b/lib/bcoin/network.js @@ -13,7 +13,7 @@ var network = require('./protocol/network'); * Represents a network. * @exports Network * @constructor - * @param {Object|String} options - See {@link module:network}. + * @param {Object|NetworkType} options - See {@link module:network}. * @property {Number} height * @property {Rate} feeRate * @property {Rate} minRelay @@ -112,7 +112,7 @@ Network.prototype.getRate = function getRate() { * Set the default network. This network will be used * if nothing is passed as the `network` option for * certain objects. - * @param {String} type - Network type. + * @param {NetworkType} type - Network type. * @returns {Network} */ @@ -124,7 +124,7 @@ Network.set = function set(type) { /** * Get a network with a string or a Network object. - * @param {String|Network} options - Network type. + * @param {NetworkType|Network} options - Network type. * @returns {Network} */ diff --git a/test/tx-test.js b/test/tx-test.js index 4cdb7c10..f16d8995 100644 --- a/test/tx-test.js +++ b/test/tx-test.js @@ -45,16 +45,25 @@ function clearCache(tx, nocache) { delete tx._raw; delete tx._hash; + delete tx._inputValue; + delete tx._outputValue; + + tx._size = 0; + tx._witnessSize = 0; for (i = 0; i < tx.inputs.length; i++) { input = tx.inputs[i]; + delete input._address; delete input.script.raw; + delete input.script.redeem; + delete input.witness.redeem; if (input.coin) delete input.coin.script.raw; } for (i = 0; i < tx.outputs.length; i++) { output = tx.outputs[i]; + delete output._address; delete output.script.raw; } } @@ -380,6 +389,30 @@ describe('TX', function() { assert.ok(!tx.checkInputs(0)); }); + it('should fail on >51 bit output values from multiple', function () { + var tx = bcoin.tx({ + version: 1, + inputs: [createInput(constants.MAX_MONEY)], + outputs: [ + { + script: [], + value: Math.floor(constants.MAX_MONEY / 2) + }, + { + script: [], + value: Math.floor(constants.MAX_MONEY / 2) + }, + { + script: [], + value: Math.floor(constants.MAX_MONEY / 2) + } + ], + locktime: 0 + }); + assert.ok(!tx.isSane()); + assert.ok(!tx.checkInputs(0)); + }); + it('should fail on >51 bit fees from multiple', function () { var tx = bcoin.tx({ version: 1, @@ -399,7 +432,7 @@ describe('TX', function() { }); it('should fail on >51 bit fees from multiple txs', function () { - var data = utils.merge(bcoin.network.get().genesis, { height: 0 }); + var data = utils.merge({}, bcoin.network.get().genesis, { height: 0 }); var block = new bcoin.block(data); for (var i = 0; i < 3; i++) { var tx = bcoin.tx({ @@ -443,4 +476,128 @@ describe('TX', function() { tx.fromRaw(raw); }); }); + + it('should fail on 53 bit coin values', function () { + var tx = bcoin.tx({ + version: 1, + inputs: [createInput(utils.MAX_SAFE_INTEGER)], + outputs: [{ + script: [], + value: constants.MAX_MONEY + }], + locktime: 0 + }); + assert.ok(tx.isSane()); + assert.ok(!tx.checkInputs(0)); + }); + + it('should fail on 53 bit output values', function () { + var tx = bcoin.tx({ + version: 1, + inputs: [createInput(constants.MAX_MONEY)], + outputs: [{ + script: [], + value: utils.MAX_SAFE_INTEGER + }], + locktime: 0 + }); + assert.ok(!tx.isSane()); + assert.ok(!tx.checkInputs(0)); + }); + + it('should fail on 53 bit fees', function () { + var tx = bcoin.tx({ + version: 1, + inputs: [createInput(utils.MAX_SAFE_INTEGER)], + outputs: [{ + script: [], + value: 0 + }], + locktime: 0 + }); + assert.ok(tx.isSane()); + assert.ok(!tx.checkInputs(0)); + }); + + [utils.MAX_SAFE_ADDITION, utils.MAX_SAFE_INTEGER].forEach(function(MAX) { + it('should fail on >53 bit values from multiple', function () { + var tx = bcoin.tx({ + version: 1, + inputs: [ + createInput(MAX), + createInput(MAX), + createInput(MAX) + ], + outputs: [{ + script: [], + value: constants.MAX_MONEY + }], + locktime: 0 + }); + assert.ok(tx.isSane()); + assert.ok(!tx.checkInputs(0)); + }); + + it('should fail on >53 bit output values from multiple', function () { + var tx = bcoin.tx({ + version: 1, + inputs: [createInput(constants.MAX_MONEY)], + outputs: [ + { + script: [], + value: MAX + }, + { + script: [], + value: MAX + }, + { + script: [], + value: MAX + } + ], + locktime: 0 + }); + assert.ok(!tx.isSane()); + assert.ok(!tx.checkInputs(0)); + }); + + it('should fail on >53 bit fees from multiple', function () { + var tx = bcoin.tx({ + version: 1, + inputs: [ + createInput(MAX), + createInput(MAX), + createInput(MAX) + ], + outputs: [{ + script: [], + value: 0 + }], + locktime: 0 + }); + assert.ok(tx.isSane()); + assert.ok(!tx.checkInputs(0)); + }); + + it('should fail on >53 bit fees from multiple txs', function () { + var data = utils.merge({}, bcoin.network.get().genesis, { height: 0 }); + var block = new bcoin.block(data); + for (var i = 0; i < 3; i++) { + var tx = bcoin.tx({ + version: 1, + inputs: [ + createInput(MAX) + ], + outputs: [{ + script: [], + value: 0 + }], + locktime: 0 + }); + block.txs.push(tx); + } + assert.equal(block.getReward(), -1); + }); + }); });