From 42f5ece28267e632c1ccd1d9a1cde72f980b1f91 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 18 Feb 2016 17:14:42 -0800 Subject: [PATCH] refactor mempool events from chain. --- lib/bcoin/blockdb.js | 16 ++++++++++++-- lib/bcoin/chain.js | 52 ++++++++++++++++++++------------------------ lib/bcoin/pool.js | 4 ++-- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/lib/bcoin/blockdb.js b/lib/bcoin/blockdb.js index 7f43c212..dff594df 100644 --- a/lib/bcoin/blockdb.js +++ b/lib/bcoin/blockdb.js @@ -252,7 +252,12 @@ BlockDB.prototype.saveBlock = function saveBlock(block, callback) { }); }); - batch.write(callback); + batch.write(function(err) { + if (err) + return callback(err); + self.emit('save block', block); + return callback(null, block); + }); }); }; @@ -285,9 +290,11 @@ BlockDB.prototype.removeBlock = function removeBlock(hash, callback) { // TODO: Add check to make sure we // can ONLY remove the last block. assert(block._fileOffset >= 0); + assert(block._fileOffset < self.index.size); self.data.truncateAsync(block._fileOffset, function(err) { if (err) return callback(err); + self.emit('remove block', block); return callback(null, block); }); }); @@ -999,7 +1006,7 @@ BlockDB.prototype.getHeight = function getHeight(callback) { }); }; -BlockDB.prototype.resetHeight = function resetHeight(height, callback) { +BlockDB.prototype.resetHeight = function resetHeight(height, callback, emit) { var self = this; this.getHeight(function(err, currentHeight) { if (err) @@ -1015,6 +1022,11 @@ BlockDB.prototype.resetHeight = function resetHeight(height, callback) { self.removeBlock(currentHeight, function(err, block) { if (err) return callback(err); + + // Emit the blocks we removed. + if (emit && block) + emit(block); + currentHeight--; next(); }); diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 129e2305..e5062e4b 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -134,6 +134,17 @@ Chain.prototype._init = function _init() { utils.debug('Warning: %d (%dmb) orphans cleared!', coin, utils.mb(size)); }); + // Update the mempool. + this.on('add block', function(block) { + if (self.mempool) + self.mempool.addBlock(block); + }); + + this.on('remove block', function(block) { + if (self.mempool) + self.mempool.removeBlock(block); + }); + this.loading = true; utils.debug('Chain is loading.'); @@ -361,15 +372,7 @@ Chain.prototype._saveBlock = function _saveBlock(block, callback) { if (!this.blockdb) return callback(); - this.blockdb.saveBlock(block, function(err) { - if (err) - return callback(err); - - if (self.mempool) - self.mempool.addBlock(block); - - return callback(); - }); + this.blockdb.saveBlock(block, callback); }; Chain.prototype._removeBlock = function _removeBlock(tip, callback) { @@ -378,24 +381,7 @@ Chain.prototype._removeBlock = function _removeBlock(tip, callback) { if (!this.blockdb) return callback(); - this.blockdb.removeBlock(tip, function(err, block) { - if (err) - return callback(err); - - if (!block) - return callback(); - - if (self.mempool) - self.mempool.removeBlock(block); - - self.emit('remove block', block.hash('hex')); - - block.txs.forEach(function(tx) { - self.emit('remove tx', tx.hash('hex')); - }); - - return callback(); - }); + this.blockdb.removeBlock(tip, callback); }; Chain.prototype._verifyContext = function _verifyContext(block, prev, callback) { @@ -837,6 +823,8 @@ Chain.prototype.revertHeight = function revertHeight(height, callback) { self.resetHeight(height); return done(); + }, function(block) { + self.emit('remove block', block); }); }); }; @@ -890,6 +878,8 @@ Chain.prototype.syncHeight = function syncHeight(callback) { return done(err); return done(); + }, function(block) { + self.emit('remove block', block); }); } }); @@ -1086,12 +1076,16 @@ Chain.prototype.add = function add(initial, peer, callback) { // The block has equal chainwork (an // alternate tip). Reset the chain, find // a new peer, and wait to see who wins. - return self._removeBlock(existing.hash, function(err) { + // return self.revertHeight(existing.height - 1, function(err) { + return self._removeBlock(existing.hash, function(err, existingBlock) { if (err) return done(err); self.resetHeight(existing.height - 1); + if (existingBlock) + self.emit('remove block', existingBlock); + self.emit('fork', block, { height: existing.height, expected: existing.hash, @@ -1154,6 +1148,8 @@ Chain.prototype.add = function add(initial, peer, callback) { if (block !== initial) self.emit('resolved', block, entry, peer); + self.emit('add block', block); + // Fullfill request self.request.fullfill(hash, block); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index e32f3014..dc7278af 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -678,10 +678,10 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) { if (self.chain.height % 20 === 0) { utils.debug( - 'Status: %s @ %s height=%d blocks=%d orphan=%d active=%d' + 'Status: tip=%s ts=%s height=%d blocks=%d orphans=%d active=%d' + ' queue=%d target=%s peers=%d pending=%d highest=%d', block.rhash, - new Date(block.ts * 1000).toISOString(), + new Date(block.ts * 1000).toISOString().slice(0, -5) + 'Z', self.chain.height, self.chain.total, self.chain.orphan.count,