diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 3c3ba1c8..40f4942a 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -25,8 +25,6 @@ function Block(data) { this.type = 'block'; - this._witness = data._witness || false; - this._cost = data._cost || 0; this._witnessSize = data._witnessSize || 0; this.txs = data.txs || []; @@ -45,7 +43,7 @@ utils.inherits(Block, bcoin.abstractblock); Block.prototype.render = function render() { if (this._raw) { - if (!this._witness) + if (!this._witnessSize) return this._raw; return bcoin.protocol.framer.block(this); } @@ -55,14 +53,14 @@ Block.prototype.render = function render() { this._raw = bcoin.protocol.framer.block(this); this._size = this._raw.length; - this._witness = false; + this._witnessSize = 0; return this._raw; }; Block.prototype.renderWitness = function renderWitness() { if (this._raw) { - if (this._witness) + if (this._witnessSize) return this._raw; return bcoin.protocol.framer.witnessBlock(this); } @@ -72,7 +70,7 @@ Block.prototype.renderWitness = function renderWitness() { this._raw = bcoin.protocol.framer.witnessBlock(this); this._size = this._raw.length; - this._witness = true; + this._witnessSize = this._raw._witnessSize; return this._raw; }; @@ -97,12 +95,6 @@ Block.prototype.getSize = function getSize() { return size; }; -Block.prototype.getCost = function getCost() { - if (!this._cost) - this._cost = this._renderWitness()._cost; - return this._cost; -}; - Block.prototype.hasWitness = function hasWitness() { for (var i = 0; i < this.txs.length; i++) { if (this.txs[i].hasWitness()) @@ -111,12 +103,12 @@ Block.prototype.hasWitness = function hasWitness() { return false; }; -Block.prototype.getSigopsCost = function getSigopCost(scriptHash) { +Block.prototype.getSigopsCost = function getSigopCost(scriptHash, accurate) { var cost = 0; var i; for (i = 0; i < this.txs.length; i++) - cost += this.txs[i].getSigopsCost(scriptHash); + cost += this.txs[i].getSigopsCost(scriptHash, accurate); return cost; }; diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 603b73e6..413228e9 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._cost = 0; this._witnessSize = 0; this.height = -1; @@ -101,10 +100,6 @@ MTX.prototype.getSize = function getSize() { return this.render().length; }; -MTX.prototype.getCost = function getCost() { - return this.renderWitness()._cost; -}; - MTX.prototype.addInput = function addInput(options, index) { var input, i; diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 3e3ac611..f39d650f 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -473,7 +473,6 @@ Framer.tx = function _tx(tx) { off += utils.writeU32(p, tx.locktime, off); - p._cost = p.length * 4; p._witnessSize = 0; return p; @@ -530,17 +529,13 @@ Framer.witnessTX = function _witnessTX(tx) { off += utils.copy(output, p, off); } - p._cost = (off - 2) * 4; - // NOTE: No varint item count here. for (i = 0; i < witnesses.length; i++) off += utils.copy(witnesses[i], p, off); off += utils.writeU32(p, tx.locktime, off); - p._cost += 4 * 4; - p._cost += witnessSize; - p._witnessSize = witnessSize; + p._witnessSize = witnessSize + 2; return p; }; @@ -584,7 +579,6 @@ Framer.witnessBlock = function _witnessBlock(block) { Framer._block = function _block(block, witness) { var off = 0; var txSize = 0; - var cost = 0; var witnessSize = 0; var txs = []; var hasWitness; @@ -601,7 +595,6 @@ Framer._block = function _block(block, witness) { : Framer.tx(block.txs[i]); txs.push(tx); txSize += tx.length; - cost += tx._cost; witnessSize += tx._witnessSize; } @@ -630,13 +623,10 @@ Framer._block = function _block(block, witness) { // txn_count off += utils.writeIntv(p, block.txs.length, off); - cost += off * 4; - // txs for (i = 0; i < txs.length; i++) off += utils.copy(txs[i], p, off); - p._cost = cost; p._witnessSize = witnessSize; return p; diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index e4042ec5..7979384f 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -331,10 +331,8 @@ Parser.parseHeaders = function parseHeaders(p) { Parser.parseBlock = function parseBlock(p) { var txs = []; - var cost = 0; var witnessSize = 0; var i, result, off, totalTX, tx; - var witness; if (p.length < 81) throw new Error('Invalid block size'); @@ -343,17 +341,12 @@ Parser.parseBlock = function parseBlock(p) { off = result.off; totalTX = result.r; - cost += off * 4; - for (i = 0; i < totalTX; i++) { tx = Parser.parseTX(p.slice(off)); if (!tx) throw new Error('Invalid tx count for block'); - if (tx.witness) - witness = true; tx._offset = off; off += tx._size; - cost += tx._cost; witnessSize += tx._witnessSize; txs.push(tx); } @@ -367,10 +360,8 @@ Parser.parseBlock = function parseBlock(p) { nonce: utils.readU32(p, 76), totalTX: totalTX, txs: txs, - _witness: witness, _raw: p, _size: p.length, - _cost: cost, _witnessSize: witnessSize }; }; @@ -556,6 +547,7 @@ Parser.parseTX = function parseTX(p) { return; txIn[i] = tx; + txIn[i].witness = []; tx._offset = off; off += tx._size; @@ -594,8 +586,6 @@ Parser.parseTX = function parseTX(p) { inputs: txIn, outputs: txOut, locktime: locktime, - _witness: false, - _cost: off * 4, _witnessSize: 0, _raw: p.length !== off ? p.slice(0, off) : p, _size: off @@ -610,7 +600,6 @@ Parser.isWitnessTX = function isWitnessTX(p) { }; Parser.parseWitnessTX = function parseWitnessTX(p) { - var cost = 0; var off = 0; var inCount, txIn, tx; var outCount, txOut; @@ -682,8 +671,6 @@ Parser.parseWitnessTX = function parseWitnessTX(p) { throw new Error('Invalid tx_out offset'); } - cost += (off - 2) * 4; - for (i = 0; i < inCount; i++) { tx = Parser.parseWitness(p.slice(off)); @@ -694,7 +681,6 @@ Parser.parseWitnessTX = function parseWitnessTX(p) { txIn[i]._witnessSize = tx._size; txIn[i]._witnessOffset = off; off += tx._size; - cost += tx._size; witnessSize += tx._size; if (off + 4 > p.length) @@ -703,7 +689,6 @@ Parser.parseWitnessTX = function parseWitnessTX(p) { locktime = utils.readU32(p, off); off += 4; - cost += 4 * 4; return { version: version, @@ -712,11 +697,9 @@ Parser.parseWitnessTX = function parseWitnessTX(p) { inputs: txIn, outputs: txOut, locktime: locktime, - _witness: true, _raw: off !== p.length ? p.slice(0, off) : p, _size: off, - _cost: cost, - _witnessSize: witnessSize + _witnessSize: witnessSize + 2 }; }; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index d5e1d405..daff267c 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -37,8 +37,6 @@ function TX(data, block, index) { this._raw = data._raw || null; this._size = data._size || 0; this._offset = data._offset || 0; - this._cost = data._cost || 0; - this._witness = data._witness || false; this._witnessSize = data._witnessSize || 0; this.height = data.height != null ? data.height : -1; @@ -92,11 +90,10 @@ TX.prototype.clone = function clone() { // return output; // }); - delete tx._witness; + delete tx._witnessSize; delete tx._raw; delete tx._size; delete tx._offset; - delete tx._cost; delete tx._hash; delete tx._whash; @@ -145,7 +142,7 @@ TX.prototype.hasWitness = function hasWitness() { TX.prototype.render = function render() { if (this._raw) { - if (!this._witness) + if (!this._witnessSize) return this._raw; return bcoin.protocol.framer.tx(this); } @@ -155,14 +152,14 @@ TX.prototype.render = function render() { this._raw = bcoin.protocol.framer.tx(this); this._size = this._raw.length; - this._witness = false; + this._witnessSize = 0; return this._raw; }; TX.prototype.renderWitness = function renderWitness() { if (this._raw) { - if (this._witness) + if (this._witnessSize) return this._raw; // We probably shouldn't even render it // as a witness tx if it doesn't have a witness. @@ -176,7 +173,7 @@ TX.prototype.renderWitness = function renderWitness() { this._raw = bcoin.protocol.framer.witnessTX(this); this._size = this._raw.length; - this._witness = true; + this._witnessSize = this._raw._witnessSize; return this._raw; }; @@ -185,12 +182,6 @@ TX.prototype.getSize = function getSize() { return this.render().length; }; -TX.prototype.getCost = function getCost() { - if (!this._cost) - this._cost = this.renderWitness()._cost; - return this._cost; -}; - TX.prototype._inputIndex = function _inputIndex(hash, index) { var i, ex; @@ -695,7 +686,7 @@ TX.prototype.getSigopsCost = function getSigopsCost(scriptHash, accurate) { cost += 0; }, this); - return cost; + return (cost + 3) / 4 | 0; }; TX.prototype.isStandard = function isStandard(flags) { @@ -965,7 +956,7 @@ TX.prototype.inspect = function inspect() { type: this.type, hash: utils.revHex(this.hash('hex')), witnessHash: utils.revHex(this.witnessHash('hex')), - witness: this._witness, + witness: this._witnessSize > 0, height: this.height, value: utils.btc(this.getValue()), fee: utils.btc(this.getFee()),