add tip event to chain.

This commit is contained in:
Christopher Jeffrey 2016-01-04 04:59:55 -08:00
parent 0350b93a0a
commit 20f8c82c7f
3 changed files with 15 additions and 5 deletions

View File

@ -31,6 +31,8 @@ function Chain(options) {
this.strict = this.options.strict || false; this.strict = this.options.strict || false;
this.cacheLimit = this.options.cacheLimit || 2000; this.cacheLimit = this.options.cacheLimit || 2000;
this.tip = null;
this.block = { this.block = {
list: [], list: [],
// Bloom filter for all known blocks // Bloom filter for all known blocks
@ -48,7 +50,6 @@ function Chain(options) {
hashes: [], hashes: [],
ts: [], ts: [],
heights: [], heights: [],
lookup: {},
lastTs: 0 lastTs: 0
}; };
@ -204,8 +205,9 @@ Chain.prototype._addIndex = function _addIndex(hash, ts, height) {
this.index.hashes.splice(pos, 0, hash); this.index.hashes.splice(pos, 0, hash);
this.index.heights.splice(pos, 0, height); this.index.heights.splice(pos, 0, height);
this.index.bloom.add(hash, 'hex'); 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, { this._save(hash, {
ts: ts, ts: ts,
@ -249,6 +251,9 @@ Chain.prototype.resetHeight = function resetHeight(height) {
this.index.ts[this.index.ts.length - 1] this.index.ts[this.index.ts.length - 1]
); );
this.tip = this.getTip();
this.emit('tip', this.tip);
ahead.forEach(function(hash) { ahead.forEach(function(hash) {
self._delete(hash); self._delete(hash);
}); });
@ -296,6 +301,9 @@ Chain.prototype._killFork = function _killFork(probe) {
this.index.ts.splice(index, 1); this.index.ts.splice(index, 1);
this.index.heights.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 // Delete both blocks, let's see what others will choose
this._delete(hash); this._delete(hash);

View File

@ -28,7 +28,6 @@ function Chain(options) {
this.prefix = 'bt/chain/'; this.prefix = 'bt/chain/';
this.storage = this.options.storage; this.storage = this.options.storage;
this.strict = this.options.strict || false; this.strict = this.options.strict || false;
this.cacheLimit = this.options.cacheLimit || 2000;
this.tip = null; this.tip = null;
@ -165,6 +164,7 @@ Chain.prototype._addIndex = function _addIndex(entry) {
this.index.heights[entry.hash] = entry.height; this.index.heights[entry.hash] = entry.height;
this.tip = this.index.entries[this.index.entries.length - 1]; this.tip = this.index.entries[this.index.entries.length - 1];
this.emit('tip', this.tip);
this._save(entry); this._save(entry);
@ -197,6 +197,7 @@ Chain.prototype.resetHeight = function resetHeight(height) {
this.index.hashes.length = height + 1; this.index.hashes.length = height + 1;
this.tip = this.index.entries[this.index.entries.length - 1]; this.tip = this.index.entries[this.index.entries.length - 1];
this.emit('tip', this.tip);
this.index.lastTs = Math.min( this.index.lastTs = Math.min(
this.index.lastTs, this.index.lastTs,

View File

@ -183,11 +183,12 @@ module.exports = function mine(pool, options) {
miner = exports.miner(options); miner = exports.miner(options);
// Use mempool
pool.on('tx', function(tx) { pool.on('tx', function(tx) {
miner.addTX(tx); miner.addTX(tx);
}); });
pool.on('block', function(block) { pool.chain.on('tip', function(block) {
miner.addBlock(block); miner.addBlock(block);
}); });