block/tx: refactor size calculation.

This commit is contained in:
Christopher Jeffrey 2017-07-25 02:12:35 -07:00
parent 0993bbacbf
commit 1e9855a4fa
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 39 additions and 39 deletions

View File

@ -160,7 +160,7 @@ Block.prototype.frame = function frame() {
} }
if (this._raw) { if (this._raw) {
assert(this._size > 0); assert(this._size >= 0);
assert(this._witness >= 0); assert(this._witness >= 0);
raw = new RawBlock(this._size, this._witness); raw = new RawBlock(this._size, this._witness);
raw.data = this._raw; raw.data = this._raw;
@ -170,7 +170,7 @@ Block.prototype.frame = function frame() {
raw = this.frameWitness(); raw = this.frameWitness();
this._raw = raw.data; this._raw = raw.data;
this._size = raw.total; this._size = raw.size;
this._witness = raw.witness; this._witness = raw.witness;
return raw; return raw;
@ -178,7 +178,7 @@ Block.prototype.frame = function frame() {
/** /**
* Calculate real size and size of the witness bytes. * Calculate real size and size of the witness bytes.
* @returns {Object} Contains `total` and `witness`. * @returns {Object} Contains `size` and `witness`.
*/ */
Block.prototype.getSizes = function getSizes() { Block.prototype.getSizes = function getSizes() {
@ -203,9 +203,9 @@ Block.prototype.getVirtualSize = function getVirtualSize() {
*/ */
Block.prototype.getWeight = function getWeight() { Block.prototype.getWeight = function getWeight() {
let sizes = this.getSizes(); let raw = this.getSizes();
let base = sizes.total - sizes.witness; let base = raw.size - raw.witness;
return base * (consensus.WITNESS_SCALE_FACTOR - 1) + sizes.total; return base * (consensus.WITNESS_SCALE_FACTOR - 1) + raw.size;
}; };
/** /**
@ -214,7 +214,7 @@ Block.prototype.getWeight = function getWeight() {
*/ */
Block.prototype.getSize = function getSize() { Block.prototype.getSize = function getSize() {
return this.getSizes().total; return this.getSizes().size;
}; };
/** /**
@ -223,8 +223,8 @@ Block.prototype.getSize = function getSize() {
*/ */
Block.prototype.getBaseSize = function getBaseSize() { Block.prototype.getBaseSize = function getBaseSize() {
let sizes = this.getSizes(); let raw = this.getSizes();
return sizes.total - sizes.witness; return raw.size - raw.witness;
}; };
/** /**
@ -757,11 +757,11 @@ Block.prototype.writeWitness = function writeWitness(bw) {
*/ */
Block.prototype.frameNormal = function frameNormal() { Block.prototype.frameNormal = function frameNormal() {
let sizes = this.getNormalSizes(); let raw = this.getNormalSizes();
let bw = new StaticWriter(sizes.total); let bw = new StaticWriter(raw.size);
this.writeNormal(bw); this.writeNormal(bw);
sizes.data = bw.render(); raw.data = bw.render();
return sizes; return raw;
}; };
/** /**
@ -772,11 +772,11 @@ Block.prototype.frameNormal = function frameNormal() {
*/ */
Block.prototype.frameWitness = function frameWitness() { Block.prototype.frameWitness = function frameWitness() {
let sizes = this.getWitnessSizes(); let raw = this.getWitnessSizes();
let bw = new StaticWriter(sizes.total); let bw = new StaticWriter(raw.size);
this.writeWitness(bw); this.writeWitness(bw);
sizes.data = bw.render(); raw.data = bw.render();
return sizes; return raw;
}; };
/** /**
@ -818,9 +818,9 @@ Block.prototype.getWitnessSizes = function getWitnessSizes() {
size += encoding.sizeVarint(this.txs.length); size += encoding.sizeVarint(this.txs.length);
for (let tx of this.txs) { for (let tx of this.txs) {
let sizes = tx.getSizes(); let raw = tx.getSizes();
size += sizes.total; size += raw.size;
witness += sizes.witness; witness += raw.witness;
} }
return new RawBlock(size, witness); return new RawBlock(size, witness);
@ -842,9 +842,9 @@ Block.isBlock = function isBlock(obj) {
* Helpers * Helpers
*/ */
function RawBlock(total, witness) { function RawBlock(size, witness) {
this.data = null; this.data = null;
this.total = total; this.size = size;
this.witness = witness; this.witness = witness;
} }

View File

@ -297,7 +297,7 @@ TX.prototype.frame = function frame() {
} }
if (this._raw) { if (this._raw) {
assert(this._size > 0); assert(this._size >= 0);
assert(this._witness >= 0); assert(this._witness >= 0);
raw = new RawTX(this._size, this._witness); raw = new RawTX(this._size, this._witness);
raw.data = this._raw; raw.data = this._raw;
@ -310,7 +310,7 @@ TX.prototype.frame = function frame() {
raw = this.frameNormal(); raw = this.frameNormal();
this._raw = raw.data; this._raw = raw.data;
this._size = raw.total; this._size = raw.size;
this._witness = raw.witness; this._witness = raw.witness;
return raw; return raw;
@ -318,7 +318,7 @@ TX.prototype.frame = function frame() {
/** /**
* Calculate total size and size of the witness bytes. * Calculate total size and size of the witness bytes.
* @returns {Object} Contains `total` and `witness`. * @returns {Object} Contains `size` and `witness`.
*/ */
TX.prototype.getSizes = function getSizes() { TX.prototype.getSizes = function getSizes() {
@ -363,8 +363,8 @@ TX.prototype.getSigopsSize = function getSigopsSize(sigops) {
TX.prototype.getWeight = function getWeight() { TX.prototype.getWeight = function getWeight() {
let raw = this.getSizes(); let raw = this.getSizes();
let base = raw.total - raw.witness; let base = raw.size - raw.witness;
return base * (consensus.WITNESS_SCALE_FACTOR - 1) + raw.total; return base * (consensus.WITNESS_SCALE_FACTOR - 1) + raw.size;
}; };
/** /**
@ -374,7 +374,7 @@ TX.prototype.getWeight = function getWeight() {
*/ */
TX.prototype.getSize = function getSize() { TX.prototype.getSize = function getSize() {
return this.getSizes().total; return this.getSizes().size;
}; };
/** /**
@ -386,7 +386,7 @@ TX.prototype.getSize = function getSize() {
TX.prototype.getBaseSize = function getBaseSize() { TX.prototype.getBaseSize = function getBaseSize() {
let raw = this.getSizes(); let raw = this.getSizes();
return raw.total - raw.witness; return raw.size - raw.witness;
}; };
/** /**
@ -2385,11 +2385,11 @@ TX.prototype.fromWitnessReader = function fromWitnessReader(br) {
*/ */
TX.prototype.frameNormal = function frameNormal() { TX.prototype.frameNormal = function frameNormal() {
let sizes = this.getNormalSizes(); let raw = this.getNormalSizes();
let bw = new StaticWriter(sizes.total); let bw = new StaticWriter(raw.size);
this.writeNormal(bw); this.writeNormal(bw);
sizes.data = bw.render(); raw.data = bw.render();
return sizes; return raw;
}; };
/** /**
@ -2400,11 +2400,11 @@ TX.prototype.frameNormal = function frameNormal() {
*/ */
TX.prototype.frameWitness = function frameWitness() { TX.prototype.frameWitness = function frameWitness() {
let sizes = this.getWitnessSizes(); let raw = this.getWitnessSizes();
let bw = new StaticWriter(sizes.total); let bw = new StaticWriter(raw.size);
this.writeWitness(bw); this.writeWitness(bw);
sizes.data = bw.render(); raw.data = bw.render();
return sizes; return raw;
}; };
/** /**
@ -2565,9 +2565,9 @@ TX.isTX = function isTX(obj) {
* Helpers * Helpers
*/ */
function RawTX(total, witness) { function RawTX(size, witness) {
this.data = null; this.data = null;
this.total = total; this.size = size;
this.witness = witness; this.witness = witness;
} }