render block when getting size (optimization).

This commit is contained in:
Christopher Jeffrey 2016-05-18 04:30:40 -07:00
parent 6005f0f750
commit b04d4e3be1
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 29 additions and 1 deletions

View File

@ -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;
}

View File

@ -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;