Paired down what information we are persisting as far as headers go.
This commit is contained in:
parent
7809476147
commit
e3b96a2d32
@ -116,8 +116,8 @@ HeaderService.prototype.start = function(callback) {
|
||||
|
||||
self._db._store.put(self._encoding.encodeHeaderKey(0, self.GENESIS_HASH),
|
||||
self._encoding.encodeHeaderValue(genesisHeader), next);
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
next();
|
||||
},
|
||||
@ -190,7 +190,7 @@ HeaderService.prototype._onBlock = function(block) {
|
||||
}
|
||||
|
||||
for (var i = 0; i < this.subscriptions.block.length; i++) {
|
||||
this.subscriptions.block[i].emit('header/block', block);
|
||||
this.subscriptions.block[i].emit('header/block', block, header);
|
||||
}
|
||||
};
|
||||
|
||||
@ -198,44 +198,57 @@ HeaderService.prototype._onHeaders = function(headers) {
|
||||
|
||||
var self = this;
|
||||
|
||||
|
||||
log.debug('Header Service: Received: ' + headers.length + ' header(s).');
|
||||
|
||||
var prevHeader = self._headers.getLastIndex();
|
||||
|
||||
var dbOps = [];
|
||||
|
||||
for(var i = 0; i < headers.length; i++) {
|
||||
|
||||
var header = headers[i];
|
||||
if (header instanceof Header) {
|
||||
header = header.toObject();
|
||||
}
|
||||
|
||||
header.height = ++self._tip.height;
|
||||
header.chainwork = self._getChainwork(header).toString(16, 64);
|
||||
self._lastChainwork = header.chainwork;
|
||||
|
||||
self._tip.hash = header.hash;
|
||||
header.chainwork = self._getChainwork(header, prevHeader).toString(16, 64);
|
||||
|
||||
dbOps.push({
|
||||
type: 'put',
|
||||
key: self._encoding.encodeHeaderKey(header.height, header.hash),
|
||||
value: self._encoding.encodeHeaderValue(header)
|
||||
});
|
||||
prevHeader = header;
|
||||
self._headers.set(header.hash, header, header.height);
|
||||
|
||||
var newHdr = { hash: header.hash, prevHash: header.prevHash, height: header.height };
|
||||
self._headers.set(header.hash, newHdr, header.height);
|
||||
|
||||
}
|
||||
|
||||
var tipOps = utils.encodeTip(self._tip, self.name);
|
||||
|
||||
dbOps.push({
|
||||
type: 'put',
|
||||
key: tipOps.key,
|
||||
value: tipOps.value
|
||||
});
|
||||
|
||||
self._db.batch(dbOps, function() {
|
||||
self._db.batch(dbOps, function(err) {
|
||||
|
||||
if(err) {
|
||||
log.error(err);
|
||||
this.node.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
if (self._tip.height < self._bestHeight) {
|
||||
self._sync();
|
||||
return;
|
||||
}
|
||||
|
||||
log.info('Header Service: ' + self._headers.size - 1 + ' headers downloaded and persisted.');
|
||||
log.info('Header Service: ' + self._headers.getLastIndex().hash + ' is the best block hash.');
|
||||
log.debug('Header Service: ' + self._headers.getLastIndex().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
|
||||
@ -310,9 +323,11 @@ HeaderService.prototype._getPersistedHeaders = function(callback) {
|
||||
var self = this;
|
||||
|
||||
var startingHeight = self._tip.height;
|
||||
|
||||
if (self._tip.height > self._checkpoint) {
|
||||
self._tip.height -= self._checkpoint;
|
||||
}
|
||||
|
||||
var removalOps = [];
|
||||
|
||||
var start = self._encoding.encodeHeaderKey(0);
|
||||
@ -335,6 +350,7 @@ HeaderService.prototype._getPersistedHeaders = function(callback) {
|
||||
stream.on('data', function(data) {
|
||||
var header = self._encoding.decodeHeaderValue(data.value);
|
||||
// any records with a height greater than our current tip height can be scheduled for removal
|
||||
// because they will be replaced shortly
|
||||
if (header.height > self._tip.height) {
|
||||
removalOps.push({
|
||||
type: 'del',
|
||||
@ -350,6 +366,10 @@ HeaderService.prototype._getPersistedHeaders = function(callback) {
|
||||
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;
|
||||
}
|
||||
self._db.batch(removalOps, callback);
|
||||
});
|
||||
|
||||
@ -357,8 +377,7 @@ HeaderService.prototype._getPersistedHeaders = function(callback) {
|
||||
|
||||
HeaderService.prototype._getChainwork = function(header, prevHeader) {
|
||||
|
||||
var lastChainwork = prevHeader ? prevHeader.chainwork : HeaderService.STARTING_CHAINWORK;
|
||||
var prevChainwork = new BN(new Buffer(lastChainwork, 'hex'));
|
||||
var prevChainwork = new BN(new Buffer(this._lastChainwork || HeaderService.STARTING_CHAINWORK, 'hex'));
|
||||
|
||||
return this._computeChainwork(header.bits, prevChainwork);
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user