tx/block: cache hashes.
This commit is contained in:
parent
d73c80c21d
commit
33bcc08656
@ -58,6 +58,7 @@ function AbstractBlock(options) {
|
|||||||
|
|
||||||
this._valid = null;
|
this._valid = null;
|
||||||
this._hash = null;
|
this._hash = null;
|
||||||
|
this._hhash = null;
|
||||||
this._size = null;
|
this._size = null;
|
||||||
this._witnessSize = null;
|
this._witnessSize = null;
|
||||||
|
|
||||||
@ -140,6 +141,7 @@ AbstractBlock.prototype.parseJSON = function parseJSON(json) {
|
|||||||
|
|
||||||
AbstractBlock.prototype.hash = function hash(enc) {
|
AbstractBlock.prototype.hash = function hash(enc) {
|
||||||
var hash = this._hash;
|
var hash = this._hash;
|
||||||
|
var hex;
|
||||||
|
|
||||||
if (!hash) {
|
if (!hash) {
|
||||||
hash = crypto.hash256(this.abbr());
|
hash = crypto.hash256(this.abbr());
|
||||||
@ -147,7 +149,17 @@ AbstractBlock.prototype.hash = function hash(enc) {
|
|||||||
this._hash = hash;
|
this._hash = hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
return enc === 'hex' ? hash.toString('hex') : hash;
|
if (enc === 'hex') {
|
||||||
|
hex = this._hhash;
|
||||||
|
if (!hex) {
|
||||||
|
hex = hash.toString('hex');
|
||||||
|
if (!this.mutable)
|
||||||
|
this._hhash = hex;
|
||||||
|
}
|
||||||
|
hash = hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -86,6 +86,7 @@ function TX(options) {
|
|||||||
this.mutable = false;
|
this.mutable = false;
|
||||||
|
|
||||||
this._hash = null;
|
this._hash = null;
|
||||||
|
this._hhash = null;
|
||||||
this._whash = null;
|
this._whash = null;
|
||||||
|
|
||||||
this._raw = null;
|
this._raw = null;
|
||||||
@ -219,6 +220,7 @@ TX.prototype.unsetBlock = function unsetBlock() {
|
|||||||
|
|
||||||
TX.prototype.hash = function _hash(enc) {
|
TX.prototype.hash = function _hash(enc) {
|
||||||
var hash = this._hash;
|
var hash = this._hash;
|
||||||
|
var hex;
|
||||||
|
|
||||||
if (!hash) {
|
if (!hash) {
|
||||||
hash = crypto.hash256(this.toNormal());
|
hash = crypto.hash256(this.toNormal());
|
||||||
@ -226,7 +228,17 @@ TX.prototype.hash = function _hash(enc) {
|
|||||||
this._hash = hash;
|
this._hash = hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
return enc === 'hex' ? hash.toString('hex') : hash;
|
if (enc === 'hex') {
|
||||||
|
hex = this._hhash;
|
||||||
|
if (!hex) {
|
||||||
|
hex = hash.toString('hex');
|
||||||
|
if (!this.mutable)
|
||||||
|
this._hhash = hex;
|
||||||
|
}
|
||||||
|
hash = hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -348,6 +348,8 @@ describe('Script', function() {
|
|||||||
tx._witnessSize = -1;
|
tx._witnessSize = -1;
|
||||||
tx._lastWitnessSize = 0;
|
tx._lastWitnessSize = 0;
|
||||||
tx._hash = null;
|
tx._hash = null;
|
||||||
|
tx._hhash = null;
|
||||||
|
tx._whash = null;
|
||||||
tx._inputValue = -1;
|
tx._inputValue = -1;
|
||||||
tx._outputValue = -1;
|
tx._outputValue = -1;
|
||||||
tx._hashPrevouts = null;
|
tx._hashPrevouts = null;
|
||||||
|
|||||||
@ -47,6 +47,8 @@ function clearCache(tx, nocache) {
|
|||||||
tx._witnessSize = -1;
|
tx._witnessSize = -1;
|
||||||
tx._lastWitnessSize = 0;
|
tx._lastWitnessSize = 0;
|
||||||
tx._hash = null;
|
tx._hash = null;
|
||||||
|
tx._hhash = null;
|
||||||
|
tx._whash = null;
|
||||||
tx._inputValue = -1;
|
tx._inputValue = -1;
|
||||||
tx._outputValue = -1;
|
tx._outputValue = -1;
|
||||||
tx._hashPrevouts = null;
|
tx._hashPrevouts = null;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user