diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 16a05202..3c86ad13 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -31,6 +31,8 @@ function Chain(options) { this.strict = this.options.strict || false; this.cacheLimit = this.options.cacheLimit || 2000; + this.tip = null; + this.block = { list: [], // Bloom filter for all known blocks @@ -48,7 +50,6 @@ function Chain(options) { hashes: [], ts: [], heights: [], - lookup: {}, lastTs: 0 }; @@ -204,8 +205,9 @@ Chain.prototype._addIndex = function _addIndex(hash, ts, height) { this.index.hashes.splice(pos, 0, hash); this.index.heights.splice(pos, 0, height); this.index.bloom.add(hash, 'hex'); - // this.index.lookup[hash] = pos; - // this.index.lookup[height] = pos; + + this.tip = this.getTip(); + this.emit('tip', this.tip); this._save(hash, { ts: ts, @@ -249,6 +251,9 @@ Chain.prototype.resetHeight = function resetHeight(height) { this.index.ts[this.index.ts.length - 1] ); + this.tip = this.getTip(); + this.emit('tip', this.tip); + ahead.forEach(function(hash) { self._delete(hash); }); @@ -296,6 +301,9 @@ Chain.prototype._killFork = function _killFork(probe) { this.index.ts.splice(index, 1); this.index.heights.splice(index, 1); + this.tip = this.getTip(); + this.emit('tip', this.tip); + // Delete both blocks, let's see what others will choose this._delete(hash); diff --git a/lib/bcoin/fullchain.js b/lib/bcoin/fullchain.js index fad1f171..d85062c6 100644 --- a/lib/bcoin/fullchain.js +++ b/lib/bcoin/fullchain.js @@ -28,7 +28,6 @@ function Chain(options) { this.prefix = 'bt/chain/'; this.storage = this.options.storage; this.strict = this.options.strict || false; - this.cacheLimit = this.options.cacheLimit || 2000; this.tip = null; @@ -165,6 +164,7 @@ Chain.prototype._addIndex = function _addIndex(entry) { this.index.heights[entry.hash] = entry.height; this.tip = this.index.entries[this.index.entries.length - 1]; + this.emit('tip', this.tip); this._save(entry); @@ -197,6 +197,7 @@ Chain.prototype.resetHeight = function resetHeight(height) { this.index.hashes.length = height + 1; this.tip = this.index.entries[this.index.entries.length - 1]; + this.emit('tip', this.tip); this.index.lastTs = Math.min( this.index.lastTs, diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index 2dbc25b8..eccde6b6 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -183,11 +183,12 @@ module.exports = function mine(pool, options) { miner = exports.miner(options); + // Use mempool pool.on('tx', function(tx) { miner.addTX(tx); }); - pool.on('block', function(block) { + pool.chain.on('tip', function(block) { miner.addBlock(block); });