refactor mempool events from chain.
This commit is contained in:
parent
050d801849
commit
42f5ece282
@ -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();
|
||||
});
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user