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) {
assert(this._size > 0);
assert(this._size >= 0);
assert(this._witness >= 0);
raw = new RawBlock(this._size, this._witness);
raw.data = this._raw;
@ -170,7 +170,7 @@ Block.prototype.frame = function frame() {
raw = this.frameWitness();
this._raw = raw.data;
this._size = raw.total;
this._size = raw.size;
this._witness = raw.witness;
return raw;
@ -178,7 +178,7 @@ Block.prototype.frame = function frame() {
/**
* 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() {
@ -203,9 +203,9 @@ Block.prototype.getVirtualSize = function getVirtualSize() {
*/
Block.prototype.getWeight = function getWeight() {
let sizes = this.getSizes();
let base = sizes.total - sizes.witness;
return base * (consensus.WITNESS_SCALE_FACTOR - 1) + sizes.total;
let raw = this.getSizes();
let base = raw.size - raw.witness;
return base * (consensus.WITNESS_SCALE_FACTOR - 1) + raw.size;
};
/**
@ -214,7 +214,7 @@ Block.prototype.getWeight = function getWeight() {
*/
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() {
let sizes = this.getSizes();
return sizes.total - sizes.witness;
let raw = this.getSizes();
return raw.size - raw.witness;
};
/**
@ -757,11 +757,11 @@ Block.prototype.writeWitness = function writeWitness(bw) {
*/
Block.prototype.frameNormal = function frameNormal() {
let sizes = this.getNormalSizes();
let bw = new StaticWriter(sizes.total);
let raw = this.getNormalSizes();
let bw = new StaticWriter(raw.size);
this.writeNormal(bw);
sizes.data = bw.render();
return sizes;
raw.data = bw.render();
return raw;
};
/**
@ -772,11 +772,11 @@ Block.prototype.frameNormal = function frameNormal() {
*/
Block.prototype.frameWitness = function frameWitness() {
let sizes = this.getWitnessSizes();
let bw = new StaticWriter(sizes.total);
let raw = this.getWitnessSizes();
let bw = new StaticWriter(raw.size);
this.writeWitness(bw);
sizes.data = bw.render();
return sizes;
raw.data = bw.render();
return raw;
};
/**
@ -818,9 +818,9 @@ Block.prototype.getWitnessSizes = function getWitnessSizes() {
size += encoding.sizeVarint(this.txs.length);
for (let tx of this.txs) {
let sizes = tx.getSizes();
size += sizes.total;
witness += sizes.witness;
let raw = tx.getSizes();
size += raw.size;
witness += raw.witness;
}
return new RawBlock(size, witness);
@ -842,9 +842,9 @@ Block.isBlock = function isBlock(obj) {
* Helpers
*/
function RawBlock(total, witness) {
function RawBlock(size, witness) {
this.data = null;
this.total = total;
this.size = size;
this.witness = witness;
}

View File

@ -297,7 +297,7 @@ TX.prototype.frame = function frame() {
}
if (this._raw) {
assert(this._size > 0);
assert(this._size >= 0);
assert(this._witness >= 0);
raw = new RawTX(this._size, this._witness);
raw.data = this._raw;
@ -310,7 +310,7 @@ TX.prototype.frame = function frame() {
raw = this.frameNormal();
this._raw = raw.data;
this._size = raw.total;
this._size = raw.size;
this._witness = raw.witness;
return raw;
@ -318,7 +318,7 @@ TX.prototype.frame = function frame() {
/**
* 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() {
@ -363,8 +363,8 @@ TX.prototype.getSigopsSize = function getSigopsSize(sigops) {
TX.prototype.getWeight = function getWeight() {
let raw = this.getSizes();
let base = raw.total - raw.witness;
return base * (consensus.WITNESS_SCALE_FACTOR - 1) + raw.total;
let base = raw.size - raw.witness;
return base * (consensus.WITNESS_SCALE_FACTOR - 1) + raw.size;
};
/**
@ -374,7 +374,7 @@ TX.prototype.getWeight = function getWeight() {
*/
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() {
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() {
let sizes = this.getNormalSizes();
let bw = new StaticWriter(sizes.total);
let raw = this.getNormalSizes();
let bw = new StaticWriter(raw.size);
this.writeNormal(bw);
sizes.data = bw.render();
return sizes;
raw.data = bw.render();
return raw;
};
/**
@ -2400,11 +2400,11 @@ TX.prototype.frameNormal = function frameNormal() {
*/
TX.prototype.frameWitness = function frameWitness() {
let sizes = this.getWitnessSizes();
let bw = new StaticWriter(sizes.total);
let raw = this.getWitnessSizes();
let bw = new StaticWriter(raw.size);
this.writeWitness(bw);
sizes.data = bw.render();
return sizes;
raw.data = bw.render();
return raw;
};
/**
@ -2565,9 +2565,9 @@ TX.isTX = function isTX(obj) {
* Helpers
*/
function RawTX(total, witness) {
function RawTX(size, witness) {
this.data = null;
this.total = total;
this.size = size;
this.witness = witness;
}