wip
This commit is contained in:
parent
f69dfd0462
commit
35a21288b4
@ -49,7 +49,7 @@ inherits(BlockService, BaseService);
|
|||||||
|
|
||||||
BlockService.dependencies = [ 'p2p', 'db', 'header' ];
|
BlockService.dependencies = [ 'p2p', 'db', 'header' ];
|
||||||
|
|
||||||
BlockService.MAX_BLOCKS = 1;
|
BlockService.MAX_BLOCKS = 3
|
||||||
|
|
||||||
// --- public prototype functions
|
// --- public prototype functions
|
||||||
BlockService.prototype.getAPIMethods = function() {
|
BlockService.prototype.getAPIMethods = function() {
|
||||||
|
|||||||
@ -10,6 +10,7 @@ var async = require('async');
|
|||||||
var BN = require('bn.js');
|
var BN = require('bn.js');
|
||||||
var consensus = require('bcoin').consensus;
|
var consensus = require('bcoin').consensus;
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
var constants = require('../../constants');
|
||||||
|
|
||||||
var HeaderService = function(options) {
|
var HeaderService = function(options) {
|
||||||
|
|
||||||
@ -23,6 +24,7 @@ var HeaderService = function(options) {
|
|||||||
|
|
||||||
this.subscriptions = {};
|
this.subscriptions = {};
|
||||||
this.subscriptions.block = [];
|
this.subscriptions.block = [];
|
||||||
|
this.GENESIS_HASH = constants.BITCOIN_GENESIS_HASH[this.node.getNetworkName()];
|
||||||
};
|
};
|
||||||
|
|
||||||
inherits(HeaderService, BaseService);
|
inherits(HeaderService, BaseService);
|
||||||
@ -90,7 +92,23 @@ HeaderService.prototype.start = function(callback) {
|
|||||||
},
|
},
|
||||||
function(tip, next) {
|
function(tip, next) {
|
||||||
self._tip = tip;
|
self._tip = tip;
|
||||||
self._originalTip = Object.assign(self._tip, {});
|
if (self._tip.height === 0) {
|
||||||
|
self._headers.set(self.GENESIS_HASH, {
|
||||||
|
hash: self.GENESIS_HASH,
|
||||||
|
height: 0,
|
||||||
|
chainwork: HeaderService.STARTING_CHAINWORK,
|
||||||
|
version: 1,
|
||||||
|
prevHash: new Array(65).join('0'),
|
||||||
|
timestamp: 1231006505,
|
||||||
|
nonce: 2083236893,
|
||||||
|
bits: 0x1d00ffff,
|
||||||
|
merkleRoot: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
self._originalTip = {
|
||||||
|
height: self._tip.height,
|
||||||
|
hash: self._tip.hash
|
||||||
|
};
|
||||||
next();
|
next();
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
@ -121,12 +139,14 @@ HeaderService.prototype._startSubscriptions = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._subscribed = true;
|
this._subscribed = true;
|
||||||
|
|
||||||
if (!this._bus) {
|
if (!this._bus) {
|
||||||
this._bus = this.node.openBus({remoteAddress: 'localhost-header'});
|
this._bus = this.node.openBus({remoteAddress: 'localhost-header'});
|
||||||
}
|
}
|
||||||
|
|
||||||
this._bus.on('p2p/headers', this._onHeaders.bind(this));
|
this._bus.on('p2p/headers', this._onHeaders.bind(this));
|
||||||
this._bus.on('p2p/block', this._onBlock.bind(this));
|
this._bus.on('p2p/block', this._onBlock.bind(this));
|
||||||
|
|
||||||
this._bus.subscribe('p2p/headers');
|
this._bus.subscribe('p2p/headers');
|
||||||
this._bus.subscribe('p2p/block');
|
this._bus.subscribe('p2p/block');
|
||||||
|
|
||||||
@ -215,7 +235,30 @@ HeaderService.prototype._onHeaders = function(headers, convert) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
self._db._store.batch(dbOps, function() {
|
self._db._store.batch(dbOps, function() {
|
||||||
self._sync();
|
|
||||||
|
if (self._tip.height < self._bestHeight) {
|
||||||
|
self._sync();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug('Header Service: download complete.');
|
||||||
|
|
||||||
|
// have we reorg'ed since we've been shutdown?
|
||||||
|
if (self._originalTip.height > 0) {
|
||||||
|
|
||||||
|
var headerHash = self._headers.getIndex(self._originalTip.height).hash;
|
||||||
|
console.log(headerHash, self._originalTip, self._tip, self._headers.getIndex(0));
|
||||||
|
|
||||||
|
if (self._originalTip.hash !== headerHash) {
|
||||||
|
|
||||||
|
self.emit('reorg', headerHash, self._headers);
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
self.emit('headers', self._headers);
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -251,29 +294,12 @@ HeaderService.prototype._startSync = function() {
|
|||||||
HeaderService.prototype._sync = function() {
|
HeaderService.prototype._sync = function() {
|
||||||
|
|
||||||
|
|
||||||
if (this._tip.height < this._bestHeight) {
|
|
||||||
|
|
||||||
log.debug('Header Service: download progress: ' + this._tip.height + '/' +
|
log.debug('Header Service: download progress: ' + this._tip.height + '/' +
|
||||||
this._numNeeded + ' (' + (this._tip.height / this._numNeeded*100).toFixed(2) + '%)');
|
this._bestHeight + ' (' + (this._tip.height / this._bestHeight*100).toFixed(2) + '%)');
|
||||||
|
|
||||||
this._p2p.getHeaders({ startHash: this._tip.hash });
|
this._p2p.getHeaders({ startHash: this._tip.hash });
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug('Header Service: download complete.');
|
|
||||||
|
|
||||||
// have we reorg'ed since we've been shutdown?
|
|
||||||
var headerHash = this._headers.getIndex(this._originalTip.height).hash;
|
|
||||||
|
|
||||||
if (this._originalTip.hash !== headerHash) {
|
|
||||||
|
|
||||||
this.emit('reorg', headerHash, this._headers);
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
this.emit('headers', this._headers);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user