* making embedded bcoin sync persistent
* correctly shutdown embedded bcoin instance * wait until bcoin is listening on P2P before trying to connect * align bcoin port and peer port
This commit is contained in:
parent
7809476147
commit
8e9829016e
@ -10,7 +10,7 @@ var Bcoin = function(options) {
|
||||
this.emitter = new EE();
|
||||
};
|
||||
|
||||
Bcoin.prototype.start = function() {
|
||||
Bcoin.prototype.start = function(callback) {
|
||||
var self = this;
|
||||
self._bcoin = bcoin.fullnode(self._config);
|
||||
|
||||
@ -18,8 +18,11 @@ Bcoin.prototype.start = function() {
|
||||
|
||||
self._bcoin.open().then(function() {
|
||||
self._bcoin.connect().then(function() {
|
||||
self.emitter.emit('connect');
|
||||
self._bcoin.startSync();
|
||||
log.info('Waiting for Bcoin to sync');
|
||||
self._bcoin.pool.once('listening', function() {
|
||||
callback();
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
@ -34,16 +37,20 @@ Bcoin.prototype.stop = function() {
|
||||
|
||||
Bcoin.prototype._getConfig = function(options) {
|
||||
var config = {
|
||||
db: 'leveldb',
|
||||
checkpoints: true,
|
||||
network: options.network || 'main',
|
||||
listen: true
|
||||
listen: true,
|
||||
logConsole: true,
|
||||
logLevel: 'info',
|
||||
port: options.port,
|
||||
persistent: true,
|
||||
loader: require,
|
||||
workers: true
|
||||
};
|
||||
if (options.prefix) {
|
||||
config.prefix = options.prefix;
|
||||
}
|
||||
if (options.logLevel) {
|
||||
config.logLevel = options.logLevel;
|
||||
}
|
||||
return config;
|
||||
};
|
||||
|
||||
|
||||
@ -19,7 +19,6 @@ var P2P = function(options) {
|
||||
BaseService.call(this, options);
|
||||
this._options = options;
|
||||
|
||||
this._startBcoinIfNecessary();
|
||||
this._initP2P();
|
||||
this._initPubSub();
|
||||
this._bcoin = null;
|
||||
@ -109,13 +108,13 @@ P2P.prototype.sendTransaction = function(tx) {
|
||||
|
||||
|
||||
P2P.prototype.start = function(callback) {
|
||||
|
||||
var self = this;
|
||||
self._initCache();
|
||||
self._initPool();
|
||||
this._setListeners();
|
||||
callback();
|
||||
|
||||
self._startBcoinIfNecessary(function(){
|
||||
self._initCache();
|
||||
self._initPool();
|
||||
self._setListeners();
|
||||
callback();
|
||||
});
|
||||
};
|
||||
|
||||
P2P.prototype._disconnectPool = function() {
|
||||
@ -126,6 +125,9 @@ P2P.prototype._disconnectPool = function() {
|
||||
};
|
||||
|
||||
P2P.prototype.stop = function(callback) {
|
||||
if (this._bcoin){
|
||||
return this._bcoin.stop(callback);
|
||||
}
|
||||
setImmediate(callback);
|
||||
};
|
||||
|
||||
@ -168,10 +170,17 @@ P2P.prototype._broadcast = function(subscribers, name, entity) {
|
||||
};
|
||||
|
||||
P2P.prototype._connect = function() {
|
||||
this._connectCalled = this._connectCalled > 0 ? 2 : 1;
|
||||
if (this._connectCalled > 1 || !this._bcoin) {
|
||||
var self = this;
|
||||
self._connectCalled = self._connectCalled > 0 ? 2 : 1;
|
||||
if(self._connectCalled > 1 || !self._bcoin) {
|
||||
log.info('Connecting to p2p network.');
|
||||
this._pool.connect();
|
||||
self._pool.connect();
|
||||
var retryInterval = setInterval(function() {
|
||||
self._pool.connect();
|
||||
}, 5000);
|
||||
self._pool.once('peerready', function() {
|
||||
clearInterval(retryInterval);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -181,7 +190,7 @@ P2P.prototype._getBestHeight = function() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
var maxHeight = 0;
|
||||
var maxHeight = -1;
|
||||
for(var i = 0; i < this._peers.length; i++) {
|
||||
if (this._peers[i].bestHeight > maxHeight) {
|
||||
maxHeight = this._peers[i].bestHeight;
|
||||
@ -298,7 +307,6 @@ P2P.prototype._removePeer = function(peer) {
|
||||
};
|
||||
|
||||
P2P.prototype._setListeners = function() {
|
||||
|
||||
var self = this;
|
||||
self.node.on('stopping', self._disconnectPool.bind(self));
|
||||
self._pool.on('peerready', self._onPeerReady.bind(self));
|
||||
@ -307,11 +315,9 @@ P2P.prototype._setListeners = function() {
|
||||
self._pool.on('peertx', self._onPeerTx.bind(self));
|
||||
self._pool.on('peerblock', self._onPeerBlock.bind(self));
|
||||
self._pool.on('peerheaders', self._onPeerHeaders.bind(self));
|
||||
self.node.on('ready', self._connect.bind(self));
|
||||
if (self._bcoin) {
|
||||
self._bcoin.emitter.once('connect', self._connect.bind(self));
|
||||
if (!self._bcoin) {
|
||||
self.node.on('ready', self._connect.bind(self));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
P2P.prototype._setResourceFilter = function(filter, resource) {
|
||||
@ -331,21 +337,24 @@ P2P.prototype._setResourceFilter = function(filter, resource) {
|
||||
|
||||
};
|
||||
|
||||
P2P.prototype._startBcoin = function() {
|
||||
P2P.prototype._startBcoin = function(callback) {
|
||||
var self = this;
|
||||
const network = ['livenet', 'live', 'main', 'mainnet'].indexOf(this.node.network) !== -1? 'main' : 'testnet';
|
||||
this._bcoin = new Bcoin({
|
||||
self._bcoin = new Bcoin({
|
||||
network: network,
|
||||
prefix: this.node.datadir
|
||||
prefix: self.node.datadir,
|
||||
port: 48333
|
||||
});
|
||||
this._bcoin.start();
|
||||
self._bcoin.start(callback);
|
||||
};
|
||||
|
||||
P2P.prototype._startBcoinIfNecessary = function() {
|
||||
P2P.prototype._startBcoinIfNecessary = function(callback) {
|
||||
if (!this._hasPeers()) {
|
||||
log.info('Peers not explicitly configured, starting a local bcoin node.');
|
||||
this._startBcoin();
|
||||
this._options.peers = [{ ip: { v4: '127.0.0.1' }, port: 48444}];
|
||||
this._configPeers = [{ip: {v4: '127.0.0.1'}, port: 48333}];
|
||||
return this._startBcoin(callback);
|
||||
}
|
||||
setImmediate(callback);
|
||||
};
|
||||
|
||||
module.exports = P2P;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user