From 78d32ce3a28a6005cf743cb6f8b4f623a85d3865 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 26 Jun 2016 23:12:33 -0700 Subject: [PATCH] block refactor. --- lib/bcoin/block.js | 60 ++++++++++++++++----------------- lib/bcoin/headers.js | 20 +++-------- lib/bcoin/merkleblock.js | 18 ---------- lib/bcoin/protocol/constants.js | 3 +- 4 files changed, 35 insertions(+), 66 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index bbc2604f..8020de3b 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -17,30 +17,14 @@ var constants = bcoin.protocol.constants; * @exports Block * @constructor * @extends AbstractBlock - * @param {NakedBlock} data - * @property {String} type - "block" (inv type). - * @property {Number} version - Block version. Note - * that BCoin reads versions as unsigned despite - * them being signed on the protocol level. This - * number will never be negative. - * @property {Hash} prevBlock - Previous block hash. - * @property {Hash} merkleRoot - Merkle root hash. - * @property {Number} ts - Timestamp. - * @property {Number} bits - * @property {Number} nonce - * @property {Number} totalTX - Transaction count. - * @property {Number} height - Block height (-1 if not in the chain). - * @property {TX[]} txs - Transaction vector. - * @property {Hash?} commitmentHash - Commitment hash for segwit. - * @property {Buffer?} witnessNonce - Witness nonce for segwit. - * @property {ReversedHash} rhash - Reversed block hash (uint256le). + * @param {NakedBlock} options */ -function Block(data) { +function Block(options) { if (!(this instanceof Block)) - return new Block(data); + return new Block(options); - bcoin.abstractblock.call(this, data); + bcoin.abstractblock.call(this, options); this.txs = []; this._cbHeight = null; @@ -50,30 +34,44 @@ function Block(data) { this._witnessSize = null; this._lastWitnessSize = 0; - if (data) - this.fromOptions(data); + if (options) + this.fromOptions(options); } utils.inherits(Block, bcoin.abstractblock); -Block.prototype.fromOptions = function fromOptions(data) { +/** + * Inject properties from options object. + * @private + * @param {Object} options + */ + +Block.prototype.fromOptions = function fromOptions(options) { var i; this._cbHeight = null; this._commitmentHash = null; - this._raw = data._raw || null; - this._size = data._size || null; - this._witnessSize = data._witnessSize != null ? data._witnessSize : null; + this._raw = options._raw || null; + this._size = options._size || null; + this._witnessSize = options._witnessSize != null + ? options._witnessSize + : null; - if (data.txs) { - for (i = 0; i < data.txs.length; i++) - this.addTX(data.txs[i]); + if (options.txs) { + for (i = 0; i < options.txs.length; i++) + this.addTX(options.txs[i]); } }; -Block.fromOptions = function fromOptions(data) { - return new Block().fromOptions(data); +/** + * Instantiate block from options. + * @param {Object} options + * @returns {Block} + */ + +Block.fromOptions = function fromOptions(options) { + return new Block().fromOptions(options); }; /** diff --git a/lib/bcoin/headers.js b/lib/bcoin/headers.js index c303c348..2206d565 100644 --- a/lib/bcoin/headers.js +++ b/lib/bcoin/headers.js @@ -15,26 +15,14 @@ var utils = require('./utils'); * @exports Headers * @constructor * @extends AbstractBlock - * @param {NakedBlock} data - * @property {Number} version - Block version. Note - * that BCoin reads versions as unsigned despite - * them being signed on the protocol level. This - * number will never be negative. - * @property {Hash} prevBlock - Previous block hash. - * @property {Hash} merkleRoot - Merkle root hash. - * @property {Number} ts - Timestamp. - * @property {Number} bits - * @property {Number} nonce - * @property {Number} totalTX - Transaction count. - * @property {Number} height - Block height (-1 if not present). - * @property {ReversedHash} rhash - Reversed block hash (uint256le). + * @param {NakedBlock} options */ -function Headers(data) { +function Headers(options) { if (!(this instanceof Headers)) - return new Headers(data); + return new Headers(options); - bcoin.abstractblock.call(this, data); + bcoin.abstractblock.call(this, options); } utils.inherits(Headers, bcoin.abstractblock); diff --git a/lib/bcoin/merkleblock.js b/lib/bcoin/merkleblock.js index 226dc4d4..e0c29ae9 100644 --- a/lib/bcoin/merkleblock.js +++ b/lib/bcoin/merkleblock.js @@ -18,24 +18,6 @@ var constants = bcoin.protocol.constants; * @constructor * @extends AbstractBlock * @param {NakedBlock} options - * @property {String} type - "merkleblock" (getdata type). - * @property {Number} version - Block version. Note - * that BCoin reads versions as unsigned despite - * them being signed on the protocol level. This - * number will never be negative. - * @property {Hash} prevBlock - Previous block hash. - * @property {Hash} merkleRoot - Merkle root hash. - * @property {Number} ts - Timestamp. - * @property {Number} bits - * @property {Number} nonce - * @property {Number} totalTX - Transaction count. - * @property {Number} height - Block height (-1 if not in the chain). - * @property {Buffer[]} hashes - * @property {Buffer} flags - * @property {TX[]} txs - Transaction vector. - * @property {Hash[]} matches - List of matched tx hashes. - * @property {Object} map - Map of matched tx hashes. - * @property {ReversedHash} rhash - Reversed block hash (uint256le). */ function MerkleBlock(options) { diff --git a/lib/bcoin/protocol/constants.js b/lib/bcoin/protocol/constants.js index 1a5b5807..a1103440 100644 --- a/lib/bcoin/protocol/constants.js +++ b/lib/bcoin/protocol/constants.js @@ -94,7 +94,8 @@ exports.inv = { FILTERED_BLOCK: 3, WITNESS_TX: 1 | (1 << 30), WITNESS_BLOCK: 2 | (1 << 30), - WITNESS_FILTERED_BLOCK: 3 | (1 << 30) + WITNESS_FILTERED_BLOCK: 3 | (1 << 30), + CMPCT_BLOCK: 4 }; /**