diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 0c8c2558..148ce031 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -361,6 +361,16 @@ Block.prototype.getReward = function getReward() { }; }; +Block.prototype.getEntry = function getEntry(chain) { + chain = chain || bcoin.chain.global; + return chain.getBlock(this); +}; + +Block.prototype.isOrphan = function isOrphan(chain) { + chain = chain || bcoin.chain.global; + return chain.hasBlock(this.prevBlock); +}; + Block.prototype.__defineGetter__('rhash', function() { return utils.revHex(this.hash('hex')); }); @@ -388,6 +398,14 @@ Block.prototype.__defineGetter__('coinbase', function() { return tx; }); +Block.prototype.__defineGetter__('entry', function() { + return this.getEntry(bcoin.chain.global); +}); + +Block.prototype.__defineGetter__('orphan', function() { + return this.isOrphan(bcoin.chain.global); +}); + Block.prototype.inspect = function inspect() { var copy = bcoin.block(this, this.subtype); copy.__proto__ = null; diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index d35077f7..02159409 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -73,7 +73,7 @@ function Chain(options) { // Last TS after preload, needed for fill percent this.index.lastTs = this.index.entries[this.index.entries.length - 1].ts; - bcoin.chain.global = this; + Chain.global = this; this.loading = false; this._init(); @@ -195,7 +195,7 @@ Chain.prototype.resetHeight = function resetHeight(height) { this.orphan.map = {}; this.orphan.bmap = {}; this.orphan.count = 0; - this.orhpan.size = 0; + this.orphan.size = 0; this.index.entries.length = height + 1; this.index.heights = this.index.entries.reduce(function(out, entry) { if (!self.options.fullNode) { @@ -293,6 +293,9 @@ Chain.prototype.add = function add(block, peer) { // Breaking here only works because // we deleted the orphan map in resetHeight. this.emit('block', block, peer); + this.emit('entry', entry); + if (block !== initial) + this.emit('resolved', entry); break; } } @@ -300,6 +303,9 @@ Chain.prototype.add = function add(block, peer) { // Validated known block at this point - add it to index code = this._addIndex(entry); this.emit('block', block, peer); + this.emit('entry', entry); + if (block !== initial) + this.emit('resolved', entry); } // Fullfill request diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index 3e0cbdb4..b27086d3 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -21,6 +21,8 @@ function Miner(options) { if (!(this instanceof Miner)) return new Miner(options); + EventEmitter.call(this); + if (!options) options = {}; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 88e74c04..c068a3c8 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -145,6 +145,8 @@ function Pool(options) { self.emit.apply(self, ['debug'].concat(args)); }); + Pool.global = this; + if (!this.chain.loading) { this._init(); } else { @@ -498,7 +500,12 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer) { // Resolve orphan chain if (!this.options.headers) { if (!this.chain.hasBlock(block.prevBlock)) { - this._addIndex(block, peer); + // NOTE: If we were to emit new orphans here, we + // would not need to store full blocks as orphans. + // However, the listener would not be able to see + // the height until later. + if (this._addIndex(block, peer)) + this.emit('pool block', block, peer); peer.loadBlocks( this.chain.locatorHashes(), this.chain.getOrphanRoot(block) @@ -508,7 +515,8 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer) { } // Add to index and emit/save - this._addIndex(block, peer); + if (this._addIndex(block, peer)) + this.emit('pool block', block, peer); }; Pool.prototype._addIndex = function _addIndex(block, peer) { @@ -521,11 +529,8 @@ Pool.prototype._addIndex = function _addIndex(block, peer) { res = this.chain.add(block, peer); - if (res) { - this.emit('debug', - 'Chain Error: ' + bcoin.chain.msg(res), - peer); - } + if (res > 0) + this.emit('chain-error', bcoin.chain.msg(res)); // Do not emit if nothing was added to the chain if (this.chain.size() === size) {