Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored duplicate hash related code in tx.js and abstractblock.js #1130

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions lib/behaviors/hasHashBehavior.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';

const hash256 = require('bcrypto/lib/hash256');
const util = require('../utils/util');

class HasHashBehavior {

constructor() {
this._hash = null;
this._hhash = null;
this._rhash = null;
this._whash = null;
}

/**
* Clear any cached values (abstract).
*/

_refresh() {
this._hash = null;
this._hhash = null;
this._rhash = null;
this._whash = null;
}

/**
* Clear any cached values.
*/

refresh() {
return this._refresh();
}

/**
* Get little-endian block hash.
* @returns {Hash}
*/

rhash() {
return util.revHex(this.hash());
}

/**
* Hash the block headers.
* @param {Function} serializedBufferProviderFunc
* @param {String?} enc - Can be `'hex'` or `null`.
* @returns {Hash|Buffer} hash
*/

setHash(serializedBufferProviderFunc, enc) {
let h = this._hash;

if (!h) {
h = hash256.digest(serializedBufferProviderFunc());
if (!this.mutable)
this._hash = h;
}

if (enc === 'hex') {
let hex = this._hhash;
if (!hex) {
hex = h.toString('hex');
if (!this.mutable)
this._hhash = hex;
}
h = hex;
}

return h;
}
}

/*
* Expose
*/

module.exports = HasHashBehavior;
52 changes: 4 additions & 48 deletions lib/primitives/abstractblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const bio = require('bufio');
const util = require('../utils/util');
const InvItem = require('./invitem');
const consensus = require('../protocol/consensus');
const HasHashBehavior = require('../behaviors/hasHashBehavior');

/**
* Abstract Block
Expand All @@ -27,13 +28,14 @@ const consensus = require('../protocol/consensus');
* @property {Number} nonce
*/

class AbstractBlock {
class AbstractBlock extends HasHashBehavior {
/**
* Create an abstract block.
* @constructor
*/

constructor() {
super();
this.version = 1;
this.prevBlock = consensus.ZERO_HASH;
this.merkleRoot = consensus.ZERO_HASH;
Expand All @@ -42,9 +44,6 @@ class AbstractBlock {
this.nonce = 0;

this.mutable = false;

this._hash = null;
this._hhash = null;
}

/**
Expand Down Expand Up @@ -111,49 +110,14 @@ class AbstractBlock {
return false;
}

/**
* Clear any cached values (abstract).
*/

_refresh() {
this._hash = null;
this._hhash = null;
}

/**
* Clear any cached values.
*/

refresh() {
return this._refresh();
}

/**
* Hash the block headers.
* @param {String?} enc - Can be `'hex'` or `null`.
* @returns {Hash|Buffer} hash
*/

hash(enc) {
let h = this._hash;

if (!h) {
h = hash256.digest(this.toHead());
if (!this.mutable)
this._hash = h;
}

if (enc === 'hex') {
let hex = this._hhash;
if (!hex) {
hex = h.toString('hex');
if (!this.mutable)
this._hhash = hex;
}
h = hex;
}

return h;
return super.setHash(this.toHead.bind(this), enc);
}

/**
Expand Down Expand Up @@ -238,14 +202,6 @@ class AbstractBlock {
throw new Error('Abstract method.');
}

/**
* Get little-endian block hash.
* @returns {Hash}
*/

rhash() {
return util.revHex(this.hash());
}

/**
* Convert the block to an inv item.
Expand Down
41 changes: 5 additions & 36 deletions lib/primitives/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const ScriptError = require('../script/scripterror');
const {encoding} = bio;
const {hashType} = Script;
const {inspectSymbol} = require('../utils');
const HasHashBehavior = require('../behaviors/hasHashBehavior');

/**
* TX
Expand All @@ -37,25 +38,22 @@ const {inspectSymbol} = require('../utils');
* @property {Number} locktime
*/

class TX {
class TX extends HasHashBehavior {
/**
* Create a transaction.
* @constructor
* @param {Object?} options
*/

constructor(options) {
super();
this.version = 1;
this.inputs = [];
this.outputs = [];
this.locktime = 0;

this.mutable = false;

this._hash = null;
this._hhash = null;
this._whash = null;

this._raw = null;
this._offset = -1;
this._block = false;
Expand Down Expand Up @@ -153,9 +151,7 @@ class TX {
*/

refresh() {
this._hash = null;
this._hhash = null;
this._whash = null;
super.refresh();

this._raw = null;
this._size = -1;
Expand All @@ -176,25 +172,7 @@ class TX {
*/

hash(enc) {
let h = this._hash;

if (!h) {
h = hash256.digest(this.toNormal());
if (!this.mutable)
this._hash = h;
}

if (enc === 'hex') {
let hex = this._hhash;
if (!hex) {
hex = h.toString('hex');
if (!this.mutable)
this._hhash = hex;
}
h = hex;
}

return h;
return super.setHash(this.toNormal.bind(this), enc);
}

/**
Expand Down Expand Up @@ -2016,15 +1994,6 @@ class TX {
return false;
}

/**
* Get little-endian tx hash.
* @returns {Hash}
*/

rhash() {
return util.revHex(this.hash());
}

/**
* Get little-endian wtx hash.
* @returns {Hash}
Expand Down