* 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:
Justin Langston 2017-08-09 01:02:41 -04:00
parent 7809476147
commit 8e9829016e
No known key found for this signature in database
GPG Key ID: EBB3714C72F9FE5D
2 changed files with 45 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(callback) {
var self = this; var self = this;
self._bcoin = bcoin.fullnode(self._config); self._bcoin = bcoin.fullnode(self._config);
@ -18,8 +18,11 @@ 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');
self._bcoin.startSync(); 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) { 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,
loader: require,
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,10 +170,17 @@ 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) { self._connectCalled = self._connectCalled > 0 ? 2 : 1;
if(self._connectCalled > 1 || !self._bcoin) {
log.info('Connecting to p2p network.'); 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; 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 +307,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));
@ -307,11 +315,9 @@ P2P.prototype._setListeners = function() {
self._pool.on('peertx', self._onPeerTx.bind(self)); self._pool.on('peertx', self._onPeerTx.bind(self));
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)); if (!self._bcoin) {
if (self._bcoin) { self.node.on('ready', self._connect.bind(self));
self._bcoin.emitter.once('connect', self._connect.bind(self));
} }
}; };
P2P.prototype._setResourceFilter = function(filter, resource) { 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'; const network = ['livenet', 'live', 'main', 'mainnet'].indexOf(this.node.network) !== -1? 'main' : 'testnet';
this._bcoin = new Bcoin({ self._bcoin = new Bcoin({
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;