drop block subtype.

This commit is contained in:
Christopher Jeffrey 2016-02-24 07:08:02 -08:00
parent 0af709388c
commit 42818c0646
8 changed files with 35 additions and 30 deletions

View File

@ -21,8 +21,7 @@ function AbstractBlock(data) {
if (!(this instanceof AbstractBlock)) if (!(this instanceof AbstractBlock))
return new AbstractBlock(data); return new AbstractBlock(data);
this.type = 'block'; this.type = null;
this.subtype = null;
this.version = data.version; this.version = data.version;
this.prevBlock = utils.toHex(data.prevBlock); this.prevBlock = utils.toHex(data.prevBlock);
this.merkleRoot = utils.toHex(data.merkleRoot); this.merkleRoot = utils.toHex(data.merkleRoot);

View File

@ -24,7 +24,7 @@ function Block(data) {
bcoin.abstractblock.call(this, data); bcoin.abstractblock.call(this, data);
this.subtype = 'block'; this.type = 'block';
this.txs = data.txs || []; this.txs = data.txs || [];
@ -56,8 +56,6 @@ Block.prototype.getMerkleRoot = function getMerkleRoot() {
var hashes = []; var hashes = [];
var i, root; var i, root;
assert(this.subtype === 'block');
for (i = 0; i < this.txs.length; i++) for (i = 0; i < this.txs.length; i++)
hashes.push(this.txs[i].hash()); hashes.push(this.txs[i].hash());
@ -226,8 +224,7 @@ Block.prototype.inspect = function inspect() {
Block.prototype.toJSON = function toJSON() { Block.prototype.toJSON = function toJSON() {
return { return {
type: 'block', type: this.type,
subtype: this.subtype,
height: this.height, height: this.height,
relayedBy: this.relayedBy, relayedBy: this.relayedBy,
hash: utils.revHex(this.hash('hex')), hash: utils.revHex(this.hash('hex')),
@ -259,8 +256,7 @@ Block.fromJSON = function fromJSON(json) {
Block.prototype.toCompact = function toCompact() { Block.prototype.toCompact = function toCompact() {
return { return {
type: 'block', type: this.type,
subtype: this.subtype,
hash: this.hash('hex'), hash: this.hash('hex'),
prevBlock: this.prevBlock, prevBlock: this.prevBlock,
ts: this.ts, ts: this.ts,
@ -302,17 +298,29 @@ Block.prototype.toRaw = function toRaw(enc) {
return data; return data;
}; };
Block._fromRaw = function _fromRaw(data, enc) { Block._fromRaw = function _fromRaw(data, enc, type) {
var parser = new bcoin.protocol.parser(); var parser = new bcoin.protocol.parser();
if (enc === 'hex') if (enc === 'hex')
data = new Buffer(data, '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); return parser.parseBlock(data);
}; };
Block.fromRaw = function fromRaw(data, enc) { Block.fromRaw = function fromRaw(data, enc, type) {
return new Block(Block._fromRaw(data, enc, subtype), subtype); if (type === 'merkleblock')
return bcoin.merkleblock.fromRaw(data);
if (type === 'headers')
return bcoin.headers.fromRaw(data);
return new Block(Block._fromRaw(data, enc));
}; };
/** /**

View File

@ -703,7 +703,6 @@ BlockDB.prototype.getTXByAddress = function getTXByAddress(addresses, options, c
}); });
} }
tx.network = true;
tx.height = record.height; tx.height = record.height;
if (entry) { if (entry) {
@ -761,7 +760,6 @@ BlockDB.prototype.getTX = function getTX(hash, callback) {
if (err) if (err)
return callback(err); return callback(err);
tx.network = true;
tx.height = record.height; tx.height = record.height;
if (entry) { if (entry) {
@ -813,10 +811,8 @@ BlockDB.prototype.getBlock = function getBlock(hash, callback) {
return callback(e); return callback(e);
} }
block._fileOffset = record.offset; block._fileOffset = record.offset;
block.network = true;
block.height = record.height; block.height = record.height;
block.txs.forEach(function(tx) { block.txs.forEach(function(tx) {
tx.network = true;
tx.height = block.height; tx.height = block.height;
}); });
if (self.options.paranoid) { if (self.options.paranoid) {

View File

@ -541,7 +541,7 @@ Chain.prototype._verify = function _verify(block, prev) {
// locktimeMedian = true; // locktimeMedian = true;
// Can't verify any further when merkleblock or headers. // Can't verify any further when merkleblock or headers.
if (block.subtype !== 'block') if (block.type !== 'block')
return flags; return flags;
// Make sure the height contained in the coinbase is correct. // 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 self = this;
var height = prev.height + 1; var height = prev.height + 1;
if (!this.blockdb || block.subtype !== 'block') if (!this.blockdb || block.type !== 'block')
return callback(null, true); return callback(null, true);
if (block.isGenesis()) if (block.isGenesis())
@ -608,7 +608,7 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac
var height = prev.height + 1; var height = prev.height + 1;
var scriptCheck = true; var scriptCheck = true;
if (!this.blockdb || block.subtype !== 'block') if (!this.blockdb || block.type !== 'block')
return callback(null, true); return callback(null, true);
if (block.isGenesis()) if (block.isGenesis())
@ -1110,7 +1110,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
}, peer); }, peer);
block = { block = {
data: block._raw, data: block._raw,
subtype: block.subtype, type: block.type,
hash: block.hash('hex'), hash: block.hash('hex'),
prevBlock: block.prevBlock, prevBlock: block.prevBlock,
coinbaseHeight: block.getCoinbaseHeight() coinbaseHeight: block.getCoinbaseHeight()
@ -1299,8 +1299,13 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
delete self.orphan.map[hash]; delete self.orphan.map[hash];
self.orphan.count--; self.orphan.count--;
self.orphan.size -= block.data.length; 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); next(block);
} }

View File

@ -23,7 +23,7 @@ function Headers(data) {
bcoin.abstractblock.call(this, data); bcoin.abstractblock.call(this, data);
this.subtype = 'headers' this.type = 'headers'
if (!this._raw) if (!this._raw)
this._raw = this.render(); this._raw = this.render();
@ -76,7 +76,7 @@ Headers._fromRaw = function _fromRaw(data, enc) {
}; };
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));
}; };

View File

@ -21,7 +21,7 @@ function MerkleBlock(data) {
bcoin.abstractblock.call(this, data); bcoin.abstractblock.call(this, data);
this.subtype = 'merkleblock' this.type = 'merkleblock'
this.hashes = (data.hashes || []).map(function(hash) { this.hashes = (data.hashes || []).map(function(hash) {
return utils.toHex(hash); return utils.toHex(hash);
@ -63,9 +63,6 @@ MerkleBlock.prototype._verifyPartial = function _verifyPartial() {
var flags = this.flags; var flags = this.flags;
var i, root; var i, root;
if (this.subtype !== 'merkleblock')
return;
// Count leaves // Count leaves
for (i = this.totalTX; i > 0; i >>= 1) for (i = this.totalTX; i > 0; i >>= 1)
height++; height++;

View File

@ -168,7 +168,7 @@ Pool.prototype._init = function _init() {
this.chain.on('block', function(block, entry, peer) { this.chain.on('block', function(block, entry, peer) {
self.emit('block', block, peer); self.emit('block', block, peer);
// Emit merkle txs after the fact // Emit merkle txs after the fact
if (block.subtype === 'merkleblock') { if (block.type === 'merkleblock') {
block.txs.forEach(function(tx) { block.txs.forEach(function(tx) {
self._handleTX(tx, peer); self._handleTX(tx, peer);
}); });

View File

@ -55,7 +55,7 @@ function TX(data, block) {
} }
if (block && !data.ts) { if (block && !data.ts) {
if (block.subtype === 'merkleblock') { if (block.type === 'merkleblock') {
if (block.hasTX(this.hash('hex'))) if (block.hasTX(this.hash('hex')))
this.setBlock(block); this.setBlock(block);
} else { } else {