Fixed issue where incorrect best hash was being selected.
This commit is contained in:
parent
3575b6eda3
commit
f2672205e5
@ -298,20 +298,22 @@ BlockService.prototype._getHash = function(blockArg) {
|
|||||||
|
|
||||||
BlockService.prototype._handleReorg = function(hash, allHeaders) {
|
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: ' +
|
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) {
|
if (err) {
|
||||||
|
|
||||||
log.error('Block Service: A common ancestor block between hash: ' +
|
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.');
|
' (the forked block) could not be found. Bitcore-node must exit.');
|
||||||
|
|
||||||
this.node.stop();
|
self.node.stop();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -319,11 +321,12 @@ BlockService.prototype._handleReorg = function(hash, allHeaders) {
|
|||||||
var commonAncestorHeader = allHeaders.get(commonAncestorHash);
|
var commonAncestorHeader = allHeaders.get(commonAncestorHash);
|
||||||
log.warn('Block Service: A common ancestor block was found to at hash: ' + commonAncestorHeader + '.');
|
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) {
|
function(err) {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
log.error('Block Service: Error: ' + err);
|
if (!self.node.stopping) {
|
||||||
self.node.stop();
|
log.error('Block Service: Error: ' + err);
|
||||||
|
self.node.stop();
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self._db.batch(operations, function(err) {
|
self._db.batch(operations, function(err) {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
log.error('Block Service: Error: ' + err);
|
if (!self.node.stopping) {
|
||||||
self.node.stop();
|
log.error('Block Service: Error: ' + err);
|
||||||
|
self.node.stop();
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -445,8 +452,10 @@ BlockService.prototype._processBlock = function() {
|
|||||||
self._db.put(tipOps.key, tipOps.value, function(err) {
|
self._db.put(tipOps.key, tipOps.value, function(err) {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
log.error('Block Service: Error: ' + err);
|
if (!self.node.stopping) {
|
||||||
self.node.stop();
|
log.error('Block Service: Error: ' + err);
|
||||||
|
self.node.stop();
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,7 +469,6 @@ BlockService.prototype._processBlock = function() {
|
|||||||
BlockService.prototype.onBlock = function(block, callback) {
|
BlockService.prototype.onBlock = function(block, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
|
||||||
setImmediate(function() {
|
setImmediate(function() {
|
||||||
callback(null, [{
|
callback(null, [{
|
||||||
type: 'put',
|
type: 'put',
|
||||||
|
|||||||
@ -47,8 +47,10 @@ util.inherits(DB, Service);
|
|||||||
DB.dependencies = [];
|
DB.dependencies = [];
|
||||||
|
|
||||||
DB.prototype._onError = function(err) {
|
DB.prototype._onError = function(err) {
|
||||||
log.error('Db Service: error: ' + err);
|
if (!this._stopping) {
|
||||||
this.node.stop();
|
log.error('Db Service: error: ' + err);
|
||||||
|
this.node.stop();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
DB.prototype._setDataPath = function() {
|
DB.prototype._setDataPath = function() {
|
||||||
|
|||||||
@ -223,7 +223,13 @@ HeaderService.prototype._onHeaders = function(headers) {
|
|||||||
value: self._encoding.encodeHeaderValue(header)
|
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);
|
self._headers.set(header.hash, newHdr, header.height);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -249,7 +255,7 @@ HeaderService.prototype._onHeaders = function(headers) {
|
|||||||
return;
|
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
|
// at this point, we can check our header list to see if our starting tip diverged from the tip
|
||||||
// that we have now
|
// that we have now
|
||||||
@ -360,19 +366,31 @@ HeaderService.prototype._getPersistedHeaders = function(callback) {
|
|||||||
});
|
});
|
||||||
return;
|
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() {
|
stream.on('end', function() {
|
||||||
|
|
||||||
if (streamErr) {
|
if (streamErr) {
|
||||||
return streamErr;
|
return streamErr;
|
||||||
}
|
}
|
||||||
self._tip.hash = self._headers.getIndex(self._tip.height).hash;
|
|
||||||
var lastHeader = self._headers.getLastIndex();
|
var tipHeader = self._headers.getIndex(self._tip.height);
|
||||||
if (lastHeader && lastHeader.chainwork) {
|
self._tip.hash = tipHeader.hash;
|
||||||
self._lastChainwork = lastHeader.chainwork;
|
self._lastChainwork = tipHeader.chainwork;
|
||||||
}
|
|
||||||
self._db.batch(removalOps, callback);
|
self._db.batch(removalOps, callback);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user