Fixed issue where incorrect best hash was being selected.

This commit is contained in:
Chris Kleeschulte 2017-08-09 23:54:33 -04:00
parent 3575b6eda3
commit f2672205e5
3 changed files with 53 additions and 25 deletions

View File

@ -298,20 +298,22 @@ BlockService.prototype._getHash = function(blockArg) {
BlockService.prototype._handleReorg = function(hash, allHeaders) {
this._reorging = true; // while this is set, we won't be sending blocks
var self = this;
self._reorging = true; // while self is set, we won't be sending blocks
log.warn('Block Service: Chain reorganization detected! Our current block tip is: ' +
this._tip.hash + ' the current block: ' + hash + '.');
self._tip.hash + ' the current block: ' + hash + '.');
this._findCommonAncestor(hash, allHeaders, function(err, newHash, commonAncestorHash, oldBlocks) {
self._findCommonAncestor(hash, allHeaders, function(err, newHash, commonAncestorHash, oldBlocks) {
if (err) {
log.error('Block Service: A common ancestor block between hash: ' +
this._tip.hash + ' (our current tip) and: ' + newHash +
self._tip.hash + ' (our current tip) and: ' + newHash +
' (the forked block) could not be found. Bitcore-node must exit.');
this.node.stop();
self.node.stop();
return;
}
@ -319,11 +321,12 @@ BlockService.prototype._handleReorg = function(hash, allHeaders) {
var commonAncestorHeader = allHeaders.get(commonAncestorHash);
log.warn('Block Service: A common ancestor block was found to at hash: ' + commonAncestorHeader + '.');
this._broadcast(this.subscriptions.reorg, 'block/reorg', [commonAncestorHeader, oldBlocks]);
self._broadcast(self.subscriptions.reorg, 'block/reorg', [commonAncestorHeader, oldBlocks]);
this._onReorg(commonAncestorHeader, oldBlocks);
self._onReorg(commonAncestorHeader, oldBlocks);
this._reorging = false;
self._reorging = false;
self._sync();
});
@ -425,16 +428,20 @@ BlockService.prototype._processBlock = function() {
function(err) {
if (err) {
log.error('Block Service: Error: ' + err);
self.node.stop();
if (!self.node.stopping) {
log.error('Block Service: Error: ' + err);
self.node.stop();
}
return;
}
self._db.batch(operations, function(err) {
if (err) {
log.error('Block Service: Error: ' + err);
self.node.stop();
if (!self.node.stopping) {
log.error('Block Service: Error: ' + err);
self.node.stop();
}
return;
}
@ -445,8 +452,10 @@ BlockService.prototype._processBlock = function() {
self._db.put(tipOps.key, tipOps.value, function(err) {
if (err) {
log.error('Block Service: Error: ' + err);
self.node.stop();
if (!self.node.stopping) {
log.error('Block Service: Error: ' + err);
self.node.stop();
}
return;
}
@ -460,7 +469,6 @@ BlockService.prototype._processBlock = function() {
BlockService.prototype.onBlock = function(block, callback) {
var self = this;
setImmediate(function() {
callback(null, [{
type: 'put',

View File

@ -47,8 +47,10 @@ util.inherits(DB, Service);
DB.dependencies = [];
DB.prototype._onError = function(err) {
log.error('Db Service: error: ' + err);
this.node.stop();
if (!this._stopping) {
log.error('Db Service: error: ' + err);
this.node.stop();
}
};
DB.prototype._setDataPath = function() {

View File

@ -223,7 +223,13 @@ HeaderService.prototype._onHeaders = function(headers) {
value: self._encoding.encodeHeaderValue(header)
});
var newHdr = { hash: header.hash, prevHash: header.prevHash, height: header.height };
var newHdr = {
hash: header.hash,
prevHash: header.prevHash,
height: header.height,
chainwork: header.chainwork
};
self._headers.set(header.hash, newHdr, header.height);
}
@ -249,7 +255,7 @@ HeaderService.prototype._onHeaders = function(headers) {
return;
}
log.debug('Header Service: ' + self._headers.getLastIndex().hash + ' is the best block hash.');
log.debug('Header Service: ' + self._headers.getIndex(self._bestHeight).hash + ' is the best block hash.');
// at this point, we can check our header list to see if our starting tip diverged from the tip
// that we have now
@ -360,19 +366,31 @@ HeaderService.prototype._getPersistedHeaders = function(callback) {
});
return;
}
self._headers.set(self._encoding.decodeHeaderKey(data.key).hash, header, header.height);
// hold a bit less in memory
var newHdr = {
hash: header.hash,
prevHash: header.prevHash,
height: header.height,
chainwork: header.chainwork
};
self._headers.set(self._encoding.decodeHeaderKey(data.key).hash, newHdr, header.height);
});
stream.on('end', function() {
if (streamErr) {
return streamErr;
}
self._tip.hash = self._headers.getIndex(self._tip.height).hash;
var lastHeader = self._headers.getLastIndex();
if (lastHeader && lastHeader.chainwork) {
self._lastChainwork = lastHeader.chainwork;
}
var tipHeader = self._headers.getIndex(self._tip.height);
self._tip.hash = tipHeader.hash;
self._lastChainwork = tipHeader.chainwork;
self._db.batch(removalOps, callback);
});
};