diff --git a/lib/bcoin/abstractblock.js b/lib/bcoin/abstractblock.js index 4c4650fe..83bc782c 100644 --- a/lib/bcoin/abstractblock.js +++ b/lib/bcoin/abstractblock.js @@ -21,8 +21,7 @@ function AbstractBlock(data) { if (!(this instanceof AbstractBlock)) return new AbstractBlock(data); - this.type = 'block'; - this.subtype = null; + this.type = null; this.version = data.version; this.prevBlock = utils.toHex(data.prevBlock); this.merkleRoot = utils.toHex(data.merkleRoot); diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 9a7e9600..74f3471a 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -24,7 +24,7 @@ function Block(data) { bcoin.abstractblock.call(this, data); - this.subtype = 'block'; + this.type = 'block'; this.txs = data.txs || []; @@ -56,8 +56,6 @@ Block.prototype.getMerkleRoot = function getMerkleRoot() { var hashes = []; var i, root; - assert(this.subtype === 'block'); - for (i = 0; i < this.txs.length; i++) hashes.push(this.txs[i].hash()); @@ -226,8 +224,7 @@ Block.prototype.inspect = function inspect() { Block.prototype.toJSON = function toJSON() { return { - type: 'block', - subtype: this.subtype, + type: this.type, height: this.height, relayedBy: this.relayedBy, hash: utils.revHex(this.hash('hex')), @@ -259,8 +256,7 @@ Block.fromJSON = function fromJSON(json) { Block.prototype.toCompact = function toCompact() { return { - type: 'block', - subtype: this.subtype, + type: this.type, hash: this.hash('hex'), prevBlock: this.prevBlock, ts: this.ts, @@ -302,17 +298,29 @@ Block.prototype.toRaw = function toRaw(enc) { return data; }; -Block._fromRaw = function _fromRaw(data, enc) { +Block._fromRaw = function _fromRaw(data, enc, type) { var parser = new bcoin.protocol.parser(); if (enc === 'hex') data = new Buffer(data, 'hex'); + if (type === 'merkleblock') + return bcoin.merkleblock._fromRaw(data); + + if (type === 'headers') + return bcoin.headers._fromRaw(data); + return parser.parseBlock(data); }; -Block.fromRaw = function fromRaw(data, enc) { - return new Block(Block._fromRaw(data, enc, subtype), subtype); +Block.fromRaw = function fromRaw(data, enc, type) { + if (type === 'merkleblock') + return bcoin.merkleblock.fromRaw(data); + + if (type === 'headers') + return bcoin.headers.fromRaw(data); + + return new Block(Block._fromRaw(data, enc)); }; /** diff --git a/lib/bcoin/blockdb.js b/lib/bcoin/blockdb.js index 1e8ffe37..4181e7ee 100644 --- a/lib/bcoin/blockdb.js +++ b/lib/bcoin/blockdb.js @@ -703,7 +703,6 @@ BlockDB.prototype.getTXByAddress = function getTXByAddress(addresses, options, c }); } - tx.network = true; tx.height = record.height; if (entry) { @@ -761,7 +760,6 @@ BlockDB.prototype.getTX = function getTX(hash, callback) { if (err) return callback(err); - tx.network = true; tx.height = record.height; if (entry) { @@ -813,10 +811,8 @@ BlockDB.prototype.getBlock = function getBlock(hash, callback) { return callback(e); } block._fileOffset = record.offset; - block.network = true; block.height = record.height; block.txs.forEach(function(tx) { - tx.network = true; tx.height = block.height; }); if (self.options.paranoid) { diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 0ad8be6f..b5f5f71f 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -541,7 +541,7 @@ Chain.prototype._verify = function _verify(block, prev) { // locktimeMedian = true; // Can't verify any further when merkleblock or headers. - if (block.subtype !== 'block') + if (block.type !== 'block') return flags; // Make sure the height contained in the coinbase is correct. @@ -574,7 +574,7 @@ Chain.prototype._checkDuplicates = function _checkDuplicates(block, prev, callba var self = this; var height = prev.height + 1; - if (!this.blockdb || block.subtype !== 'block') + if (!this.blockdb || block.type !== 'block') return callback(null, true); if (block.isGenesis()) @@ -608,7 +608,7 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac var height = prev.height + 1; var scriptCheck = true; - if (!this.blockdb || block.subtype !== 'block') + if (!this.blockdb || block.type !== 'block') return callback(null, true); if (block.isGenesis()) @@ -1110,7 +1110,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) { }, peer); block = { data: block._raw, - subtype: block.subtype, + type: block.type, hash: block.hash('hex'), prevBlock: block.prevBlock, coinbaseHeight: block.getCoinbaseHeight() @@ -1299,8 +1299,13 @@ Chain.prototype.add = function add(initial, peer, callback, force) { delete self.orphan.map[hash]; self.orphan.count--; self.orphan.size -= block.data.length; - block = bcoin.block.fromRaw(block.data, block.subtype); - block.network = true; + + if (block.type === 'block') + block = bcoin.block.fromRaw(block.data); + else if (block.type === 'merkleblock') + block = bcoin.merkleblock.fromRaw(block.data); + else if (block.type === 'headers') + block = bcoin.headers.fromRaw(block.data); next(block); } diff --git a/lib/bcoin/headers.js b/lib/bcoin/headers.js index f3d946af..744e9ebf 100644 --- a/lib/bcoin/headers.js +++ b/lib/bcoin/headers.js @@ -23,7 +23,7 @@ function Headers(data) { bcoin.abstractblock.call(this, data); - this.subtype = 'headers' + this.type = 'headers' if (!this._raw) this._raw = this.render(); @@ -76,7 +76,7 @@ Headers._fromRaw = function _fromRaw(data, enc) { }; Headers.fromRaw = function fromRaw(data, enc) { - return new Headers(Headers._fromRaw(data, enc, subtype)); + return new Headers(Headers._fromRaw(data, enc)); }; diff --git a/lib/bcoin/merkleblock.js b/lib/bcoin/merkleblock.js index 3989a605..62d4ed4e 100644 --- a/lib/bcoin/merkleblock.js +++ b/lib/bcoin/merkleblock.js @@ -21,7 +21,7 @@ function MerkleBlock(data) { bcoin.abstractblock.call(this, data); - this.subtype = 'merkleblock' + this.type = 'merkleblock' this.hashes = (data.hashes || []).map(function(hash) { return utils.toHex(hash); @@ -63,9 +63,6 @@ MerkleBlock.prototype._verifyPartial = function _verifyPartial() { var flags = this.flags; var i, root; - if (this.subtype !== 'merkleblock') - return; - // Count leaves for (i = this.totalTX; i > 0; i >>= 1) height++; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index bc145bff..468d1991 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -168,7 +168,7 @@ Pool.prototype._init = function _init() { this.chain.on('block', function(block, entry, peer) { self.emit('block', block, peer); // Emit merkle txs after the fact - if (block.subtype === 'merkleblock') { + if (block.type === 'merkleblock') { block.txs.forEach(function(tx) { self._handleTX(tx, peer); }); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index c5636006..3a3af8a3 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -55,7 +55,7 @@ function TX(data, block) { } if (block && !data.ts) { - if (block.subtype === 'merkleblock') { + if (block.type === 'merkleblock') { if (block.hasTX(this.hash('hex'))) this.setBlock(block); } else {