block refactor.

This commit is contained in:
Christopher Jeffrey 2016-06-26 23:12:33 -07:00
parent a95ab18379
commit 78d32ce3a2
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 35 additions and 66 deletions

View File

@ -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);
};
/**

View File

@ -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);

View File

@ -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) {

View File

@ -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
};
/**