diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index ebdc2ddb..5f8b2aa2 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -50,6 +50,7 @@ function Block(data) { this.txs = data.txs || []; this._cbHeight = null; this._commitmentHash = null; + this._raw = null; for (i = 0; i < this.txs.length; i++) { tx = this.txs[i]; @@ -78,6 +79,8 @@ Block.prototype.render = function render() { */ Block.prototype.renderNormal = function renderNormal() { + if (!this.hasWitness()) + return this.getRaw(); return bcoin.protocol.framer.block(this); }; @@ -87,6 +90,8 @@ Block.prototype.renderNormal = function renderNormal() { */ Block.prototype.renderWitness = function renderWitness() { + if (this.hasWitness()) + return this.getRaw(); return bcoin.protocol.framer.witnessBlock(this); }; @@ -99,6 +104,12 @@ Block.prototype.renderWitness = function renderWitness() { Block.prototype.getRaw = function getRaw() { var raw; + if (this._raw) { + assert(this._size > 0); + assert(this._witnessSize >= 0); + return this._raw; + } + if (this.hasWitness()) raw = bcoin.protocol.framer.witnessBlock(this); else @@ -107,6 +118,7 @@ Block.prototype.getRaw = function getRaw() { if (!this.mutable) { this._size = raw.length; this._witnessSize = raw._witnessSize; + this._raw = raw; } return raw; @@ -127,6 +139,14 @@ Block.prototype.getSizes = function getSizes() { }; } + if (!this.mutable) { + this.getRaw(); + return { + size: this._size, + witnessSize: this._witnessSize + }; + } + sizes = bcoin.protocol.framer.block.sizes(this); if (!this.mutable) { @@ -184,7 +204,12 @@ Block.prototype.getBaseSize = function getBaseSize() { */ Block.prototype.hasWitness = function hasWitness() { - for (var i = 0; i < this.txs.length; i++) { + var i; + + if (this._witnessSize > 0) + return true; + + for (i = 0; i < this.txs.length; i++) { if (this.txs[i].hasWitness()) return true; } diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index d4089fcc..a04505c0 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -369,6 +369,9 @@ TX.prototype.getBaseSize = function getBaseSize() { TX.prototype.hasWitness = function hasWitness() { var i; + if (this._witnessSize > 0) + return true; + for (i = 0; i < this.inputs.length; i++) { if (this.inputs[i].witness.items.length > 0) return true;