Merge pull request #516 from nitsujlangston/embeddedBcoinStartupHandling

Embedded bcoin node stability improvements
This commit is contained in:
Chris Kleeschulte 2017-08-09 13:59:49 -04:00 committed by GitHub
commit 93a78cadf1
2 changed files with 48 additions and 29 deletions

View File

@ -10,7 +10,7 @@ var Bcoin = function(options) {
this.emitter = new EE(); this.emitter = new EE();
}; };
Bcoin.prototype.start = function() { Bcoin.prototype.start = function(done) {
var self = this; var self = this;
self._bcoin = bcoin.fullnode(self._config); self._bcoin = bcoin.fullnode(self._config);
@ -18,8 +18,14 @@ Bcoin.prototype.start = function() {
self._bcoin.open().then(function() { self._bcoin.open().then(function() {
self._bcoin.connect().then(function() { self._bcoin.connect().then(function() {
self.emitter.emit('connect'); log.info('Waiting for Bcoin to sync');
self._bcoin.startSync(); self._bcoin.startSync();
if (self._bcoin.chain.synced){
return done();
}
self._bcoin.chain.once('full', function() {
done();
});
}); });
}); });
}; };
@ -34,16 +40,19 @@ Bcoin.prototype.stop = function() {
Bcoin.prototype._getConfig = function(options) { Bcoin.prototype._getConfig = function(options) {
var config = { var config = {
db: 'leveldb',
checkpoints: true, checkpoints: true,
network: options.network || 'main', network: options.network || 'main',
listen: true listen: true,
logConsole: true,
logLevel: 'info',
port: options.port,
persistent: true,
workers: true
}; };
if (options.prefix) { if (options.prefix) {
config.prefix = options.prefix; config.prefix = options.prefix;
} }
if (options.logLevel) {
config.logLevel = options.logLevel;
}
return config; return config;
}; };

View File

@ -19,7 +19,6 @@ var P2P = function(options) {
BaseService.call(this, options); BaseService.call(this, options);
this._options = options; this._options = options;
this._startBcoinIfNecessary();
this._initP2P(); this._initP2P();
this._initPubSub(); this._initPubSub();
this._bcoin = null; this._bcoin = null;
@ -109,13 +108,13 @@ P2P.prototype.sendTransaction = function(tx) {
P2P.prototype.start = function(callback) { P2P.prototype.start = function(callback) {
var self = this; var self = this;
self._initCache(); self._startBcoinIfNecessary(function(){
self._initPool(); self._initCache();
this._setListeners(); self._initPool();
callback(); self._setListeners();
callback();
});
}; };
P2P.prototype._disconnectPool = function() { P2P.prototype._disconnectPool = function() {
@ -126,6 +125,9 @@ P2P.prototype._disconnectPool = function() {
}; };
P2P.prototype.stop = function(callback) { P2P.prototype.stop = function(callback) {
if (this._bcoin){
return this._bcoin.stop(callback);
}
setImmediate(callback); setImmediate(callback);
}; };
@ -168,11 +170,15 @@ P2P.prototype._broadcast = function(subscribers, name, entity) {
}; };
P2P.prototype._connect = function() { P2P.prototype._connect = function() {
this._connectCalled = this._connectCalled > 0 ? 2 : 1; var self = this;
if (this._connectCalled > 1 || !this._bcoin) { log.info('Connecting to p2p network.');
log.info('Connecting to p2p network.'); self._pool.connect();
this._pool.connect(); var retryInterval = setInterval(function() {
} self._pool.connect();
}, 5000);
self._pool.once('peerready', function() {
clearInterval(retryInterval);
});
}; };
P2P.prototype._getBestHeight = function() { P2P.prototype._getBestHeight = function() {
@ -181,7 +187,7 @@ P2P.prototype._getBestHeight = function() {
return 0; return 0;
} }
var maxHeight = 0; var maxHeight = -1;
for(var i = 0; i < this._peers.length; i++) { for(var i = 0; i < this._peers.length; i++) {
if (this._peers[i].bestHeight > maxHeight) { if (this._peers[i].bestHeight > maxHeight) {
maxHeight = this._peers[i].bestHeight; maxHeight = this._peers[i].bestHeight;
@ -298,7 +304,6 @@ P2P.prototype._removePeer = function(peer) {
}; };
P2P.prototype._setListeners = function() { P2P.prototype._setListeners = function() {
var self = this; var self = this;
self.node.on('stopping', self._disconnectPool.bind(self)); self.node.on('stopping', self._disconnectPool.bind(self));
self._pool.on('peerready', self._onPeerReady.bind(self)); self._pool.on('peerready', self._onPeerReady.bind(self));
@ -308,10 +313,6 @@ P2P.prototype._setListeners = function() {
self._pool.on('peerblock', self._onPeerBlock.bind(self)); self._pool.on('peerblock', self._onPeerBlock.bind(self));
self._pool.on('peerheaders', self._onPeerHeaders.bind(self)); self._pool.on('peerheaders', self._onPeerHeaders.bind(self));
self.node.on('ready', self._connect.bind(self)); self.node.on('ready', self._connect.bind(self));
if (self._bcoin) {
self._bcoin.emitter.once('connect', self._connect.bind(self));
}
}; };
P2P.prototype._setResourceFilter = function(filter, resource) { P2P.prototype._setResourceFilter = function(filter, resource) {
@ -331,21 +332,30 @@ P2P.prototype._setResourceFilter = function(filter, resource) {
}; };
<<<<<<< HEAD
P2P.prototype._startBcoin = function(callback) {
var self = this;
const network = ['livenet', 'live', 'main', 'mainnet'].indexOf(this.node.network) !== -1? 'main' : 'testnet';
self._bcoin = new Bcoin({
=======
P2P.prototype._startBcoin = function() { P2P.prototype._startBcoin = function() {
var network = ['livenet', 'live', 'main', 'mainnet'].indexOf(this.node.network) !== -1? 'main' : 'testnet'; var network = ['livenet', 'live', 'main', 'mainnet'].indexOf(this.node.network) !== -1? 'main' : 'testnet';
this._bcoin = new Bcoin({ this._bcoin = new Bcoin({
>>>>>>> 8811190de0a9e09baef7e63d85e065e72b9da8a4
network: network, 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()) { if (!this._hasPeers()) {
log.info('Peers not explicitly configured, starting a local bcoin node.'); log.info('Peers not explicitly configured, starting a local bcoin node.');
this._startBcoin(); this._configPeers = [{ip: {v4: '127.0.0.1'}, port: 48333}];
this._options.peers = [{ ip: { v4: '127.0.0.1' }, port: 48444}]; return this._startBcoin(callback);
} }
setImmediate(callback);
}; };
module.exports = P2P; module.exports = P2P;