From 8755b16235c77536359cac6cd25c20bbe6a924f3 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 28 Feb 2016 07:55:09 -0800 Subject: [PATCH] more witness size. --- lib/bcoin/block.js | 24 ++++++++++++++---------- lib/bcoin/mtx.js | 1 - lib/bcoin/protocol/framer.js | 33 +++++++++++++++++++++++++++++++++ lib/bcoin/tx.js | 17 ++++++----------- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 40f4942a..ba7cbdea 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -43,7 +43,7 @@ utils.inherits(Block, bcoin.abstractblock); Block.prototype.render = function render() { if (this._raw) { - if (!this._witnessSize) + if (this._witnessSize === 0) return this._raw; return bcoin.protocol.framer.block(this); } @@ -60,7 +60,7 @@ Block.prototype.render = function render() { Block.prototype.renderWitness = function renderWitness() { if (this._raw) { - if (this._witnessSize) + if (this._witnessSize > 0) return this._raw; return bcoin.protocol.framer.witnessBlock(this); } @@ -75,13 +75,13 @@ Block.prototype.renderWitness = function renderWitness() { return this._raw; }; -Block.prototype.getSize = function getSize() { +Block.prototype.getBlockSize = function getBlockSize() { var size = this._size; var witnessSize = this._witnessSize; var raw, base; - if (!size) { - raw = bcoin.protocol.framer.witnessBlock(this); + if (!this._size) { + raw = this.renderWitness(); size = raw.length; witnessSize = raw._witnessSize; } @@ -95,6 +95,10 @@ Block.prototype.getSize = function getSize() { return size; }; +Block.prototype.getSize = function getSize() { + return this._raw.length; +}; + Block.prototype.hasWitness = function hasWitness() { for (var i = 0; i < this.txs.length; i++) { if (this.txs[i].hasWitness()) @@ -103,14 +107,14 @@ Block.prototype.hasWitness = function hasWitness() { return false; }; -Block.prototype.getSigopsCost = function getSigopCost(scriptHash, accurate) { - var cost = 0; +Block.prototype.getSigops = function getSigops(scriptHash, accurate) { + var total = 0; var i; for (i = 0; i < this.txs.length; i++) - cost += this.txs[i].getSigopsCost(scriptHash, accurate); + total += this.txs[i].getSigops(scriptHash, accurate); - return cost; + return total; }; Block.prototype.getMerkleRoot = function getMerkleRoot() { @@ -182,7 +186,7 @@ Block.prototype._verify = function _verify() { // Size can't be bigger than MAX_BLOCK_SIZE if (this.txs.length > constants.block.maxSize - || this.getSize() > constants.block.maxSize) { + || this.getBlockSize() > constants.block.maxSize) { utils.debug('Block is too large: %s', this.rhash); return false; } diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 413228e9..0b9dca4d 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -40,7 +40,6 @@ function MTX(options) { this._raw = null; this._size = 0; this._offset = 0; - this._witnessSize = 0; this.height = -1; diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index f39d650f..5249ebf3 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -540,6 +540,39 @@ Framer.witnessTX = function _witnessTX(tx) { return p; }; +Framer.witnessBlockSize = function witnessBlockSize(block) { + var size = 0; + var i; + + for (i = 0; i < block.txs.length; i++) { + size += Framer.witnessTXSize(block.txs[i]); + } + + return size; +}; + +Framer.witnessTXSize = function witnessTXSize(tx) { + var off = 0; + var size = 0; + var i, witness; + + for (i = 0; i < tx.inputs.length; i++) { + witness = tx.inputs[i].witness; + + if (!witness) + continue; + + size += utils.sizeIntv(witness.length); + + for (i = 0; i < witness.length; i++) { + chunk = witness[i]; + size += utils.sizeIntv(chunk.length) + chunk.length; + } + } + + return size; +}; + Framer.witness = function _witness(witness) { var off = 0; var size = 0; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index daff267c..d1f89ae2 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -37,7 +37,6 @@ function TX(data, block, index) { this._raw = data._raw || null; this._size = data._size || 0; this._offset = data._offset || 0; - this._witnessSize = data._witnessSize || 0; this.height = data.height != null ? data.height : -1; @@ -90,7 +89,6 @@ TX.prototype.clone = function clone() { // return output; // }); - delete tx._witnessSize; delete tx._raw; delete tx._size; delete tx._offset; @@ -142,7 +140,7 @@ TX.prototype.hasWitness = function hasWitness() { TX.prototype.render = function render() { if (this._raw) { - if (!this._witnessSize) + if (!bcoin.protocol.parser.isWitnessTX(this._raw)) return this._raw; return bcoin.protocol.framer.tx(this); } @@ -152,14 +150,13 @@ TX.prototype.render = function render() { this._raw = bcoin.protocol.framer.tx(this); this._size = this._raw.length; - this._witnessSize = 0; return this._raw; }; TX.prototype.renderWitness = function renderWitness() { if (this._raw) { - if (this._witnessSize) + if (bcoin.protocol.parser.isWitnessTX(this._raw)) return this._raw; // We probably shouldn't even render it // as a witness tx if it doesn't have a witness. @@ -173,13 +170,12 @@ TX.prototype.renderWitness = function renderWitness() { this._raw = bcoin.protocol.framer.witnessTX(this); this._size = this._raw.length; - this._witnessSize = this._raw._witnessSize; return this._raw; }; TX.prototype.getSize = function getSize() { - return this.render().length; + return this._raw.length; }; TX.prototype._inputIndex = function _inputIndex(hash, index) { @@ -624,7 +620,7 @@ TX.prototype.isFinal = function isFinal(height, ts) { return true; }; -TX.prototype.getSigops = function getSigops(scriptHash, accurate) { +TX.prototype._getSigops = function _getSigops(scriptHash, accurate) { var total = 0; this.inputs.forEach(function(input) { @@ -657,8 +653,8 @@ TX.prototype.getSigops = function getSigops(scriptHash, accurate) { return total; }; -TX.prototype.getSigopsCost = function getSigopsCost(scriptHash, accurate) { - var cost = this.getSigops(scriptHash, accurate) * 4; +TX.prototype.getSigops = function getSigops(scriptHash, accurate) { + var cost = this._getSigops(scriptHash, accurate) * 4; this.inputs.forEach(function(input) { var prev; @@ -956,7 +952,6 @@ TX.prototype.inspect = function inspect() { type: this.type, hash: utils.revHex(this.hash('hex')), witnessHash: utils.revHex(this.witnessHash('hex')), - witness: this._witnessSize > 0, height: this.height, value: utils.btc(this.getValue()), fee: utils.btc(this.getFee()),