render block when getting size (optimization).
This commit is contained in:
parent
6005f0f750
commit
b04d4e3be1
@ -50,6 +50,7 @@ function Block(data) {
|
|||||||
this.txs = data.txs || [];
|
this.txs = data.txs || [];
|
||||||
this._cbHeight = null;
|
this._cbHeight = null;
|
||||||
this._commitmentHash = null;
|
this._commitmentHash = null;
|
||||||
|
this._raw = null;
|
||||||
|
|
||||||
for (i = 0; i < this.txs.length; i++) {
|
for (i = 0; i < this.txs.length; i++) {
|
||||||
tx = this.txs[i];
|
tx = this.txs[i];
|
||||||
@ -78,6 +79,8 @@ Block.prototype.render = function render() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Block.prototype.renderNormal = function renderNormal() {
|
Block.prototype.renderNormal = function renderNormal() {
|
||||||
|
if (!this.hasWitness())
|
||||||
|
return this.getRaw();
|
||||||
return bcoin.protocol.framer.block(this);
|
return bcoin.protocol.framer.block(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -87,6 +90,8 @@ Block.prototype.renderNormal = function renderNormal() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Block.prototype.renderWitness = function renderWitness() {
|
Block.prototype.renderWitness = function renderWitness() {
|
||||||
|
if (this.hasWitness())
|
||||||
|
return this.getRaw();
|
||||||
return bcoin.protocol.framer.witnessBlock(this);
|
return bcoin.protocol.framer.witnessBlock(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -99,6 +104,12 @@ Block.prototype.renderWitness = function renderWitness() {
|
|||||||
Block.prototype.getRaw = function getRaw() {
|
Block.prototype.getRaw = function getRaw() {
|
||||||
var raw;
|
var raw;
|
||||||
|
|
||||||
|
if (this._raw) {
|
||||||
|
assert(this._size > 0);
|
||||||
|
assert(this._witnessSize >= 0);
|
||||||
|
return this._raw;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.hasWitness())
|
if (this.hasWitness())
|
||||||
raw = bcoin.protocol.framer.witnessBlock(this);
|
raw = bcoin.protocol.framer.witnessBlock(this);
|
||||||
else
|
else
|
||||||
@ -107,6 +118,7 @@ Block.prototype.getRaw = function getRaw() {
|
|||||||
if (!this.mutable) {
|
if (!this.mutable) {
|
||||||
this._size = raw.length;
|
this._size = raw.length;
|
||||||
this._witnessSize = raw._witnessSize;
|
this._witnessSize = raw._witnessSize;
|
||||||
|
this._raw = raw;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 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);
|
sizes = bcoin.protocol.framer.block.sizes(this);
|
||||||
|
|
||||||
if (!this.mutable) {
|
if (!this.mutable) {
|
||||||
@ -184,7 +204,12 @@ Block.prototype.getBaseSize = function getBaseSize() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Block.prototype.hasWitness = function hasWitness() {
|
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())
|
if (this.txs[i].hasWitness())
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -369,6 +369,9 @@ TX.prototype.getBaseSize = function getBaseSize() {
|
|||||||
TX.prototype.hasWitness = function hasWitness() {
|
TX.prototype.hasWitness = function hasWitness() {
|
||||||
var i;
|
var i;
|
||||||
|
|
||||||
|
if (this._witnessSize > 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
for (i = 0; i < this.inputs.length; i++) {
|
for (i = 0; i < this.inputs.length; i++) {
|
||||||
if (this.inputs[i].witness.items.length > 0)
|
if (this.inputs[i].witness.items.length > 0)
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user