From 8e9829016e4a145d098eb2dc63a49becead639b7 Mon Sep 17 00:00:00 2001 From: Justin Langston Date: Wed, 9 Aug 2017 01:02:41 -0400 Subject: [PATCH 1/6] * 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 --- lib/services/p2p/bcoin.js | 19 +++++++++----- lib/services/p2p/index.js | 55 +++++++++++++++++++++++---------------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/lib/services/p2p/bcoin.js b/lib/services/p2p/bcoin.js index 487a539e..92883629 100644 --- a/lib/services/p2p/bcoin.js +++ b/lib/services/p2p/bcoin.js @@ -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; }; diff --git a/lib/services/p2p/index.js b/lib/services/p2p/index.js index 68a50bcf..4ad110bc 100644 --- a/lib/services/p2p/index.js +++ b/lib/services/p2p/index.js @@ -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; From 68fb731e832da6c28ebdd1bd40af763d306723e5 Mon Sep 17 00:00:00 2001 From: Justin Langston Date: Wed, 9 Aug 2017 02:10:46 -0400 Subject: [PATCH 2/6] switching to pool full event to indicate the sync is complete and p2p online --- lib/services/p2p/bcoin.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/services/p2p/bcoin.js b/lib/services/p2p/bcoin.js index 92883629..9c15c75f 100644 --- a/lib/services/p2p/bcoin.js +++ b/lib/services/p2p/bcoin.js @@ -20,7 +20,7 @@ Bcoin.prototype.start = function(callback) { self._bcoin.connect().then(function() { self._bcoin.startSync(); log.info('Waiting for Bcoin to sync'); - self._bcoin.pool.once('listening', function() { + self._bcoin.pool.once('full', function() { callback(); }); }); @@ -45,7 +45,6 @@ Bcoin.prototype._getConfig = function(options) { logLevel: 'info', port: options.port, persistent: true, - loader: require, workers: true }; if (options.prefix) { From fa7868e46897ae97037af1a45a415e0d53e3f338 Mon Sep 17 00:00:00 2001 From: Justin Langston Date: Wed, 9 Aug 2017 02:25:29 -0400 Subject: [PATCH 3/6] listen for event sooner and increasing maxOutbound peers --- lib/services/p2p/bcoin.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/services/p2p/bcoin.js b/lib/services/p2p/bcoin.js index 9c15c75f..c7f8d0f8 100644 --- a/lib/services/p2p/bcoin.js +++ b/lib/services/p2p/bcoin.js @@ -18,11 +18,11 @@ Bcoin.prototype.start = function(callback) { self._bcoin.open().then(function() { self._bcoin.connect().then(function() { - self._bcoin.startSync(); - log.info('Waiting for Bcoin to sync'); self._bcoin.pool.once('full', function() { callback(); }); + self._bcoin.startSync(); + log.info('Waiting for Bcoin to sync'); }); }); }; @@ -45,7 +45,9 @@ Bcoin.prototype._getConfig = function(options) { logLevel: 'info', port: options.port, persistent: true, - workers: true + workers: true, + maxOutbound: 16, + loader: require }; if (options.prefix) { config.prefix = options.prefix; From 79ca74421ae8708225d61edf0502b46286db2f8b Mon Sep 17 00:00:00 2001 From: Justin Langston Date: Wed, 9 Aug 2017 11:24:55 -0400 Subject: [PATCH 4/6] bcoin syncing gets stuck with workers true --- lib/services/p2p/bcoin.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/services/p2p/bcoin.js b/lib/services/p2p/bcoin.js index c7f8d0f8..57a5e0e1 100644 --- a/lib/services/p2p/bcoin.js +++ b/lib/services/p2p/bcoin.js @@ -45,9 +45,7 @@ Bcoin.prototype._getConfig = function(options) { logLevel: 'info', port: options.port, persistent: true, - workers: true, - maxOutbound: 16, - loader: require + workers: false }; if (options.prefix) { config.prefix = options.prefix; From 6ad7d359638f6f320b950ec463e5e2258aecb5e4 Mon Sep 17 00:00:00 2001 From: Justin Langston Date: Wed, 9 Aug 2017 11:51:38 -0400 Subject: [PATCH 5/6] workers should generally be enabled, should be fixed in bcoin if recurrant issue --- lib/services/p2p/bcoin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/p2p/bcoin.js b/lib/services/p2p/bcoin.js index 57a5e0e1..a24d7375 100644 --- a/lib/services/p2p/bcoin.js +++ b/lib/services/p2p/bcoin.js @@ -45,7 +45,7 @@ Bcoin.prototype._getConfig = function(options) { logLevel: 'info', port: options.port, persistent: true, - workers: false + workers: true }; if (options.prefix) { config.prefix = options.prefix; From ccf51ec337c08b9e10fb0c24c6d2783684e61e39 Mon Sep 17 00:00:00 2001 From: Justin Langston Date: Wed, 9 Aug 2017 13:24:24 -0400 Subject: [PATCH 6/6] catching the synced state better simpler connect handling --- lib/services/p2p/bcoin.js | 13 ++++++++----- lib/services/p2p/index.js | 21 ++++++++------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/services/p2p/bcoin.js b/lib/services/p2p/bcoin.js index a24d7375..fcff73c9 100644 --- a/lib/services/p2p/bcoin.js +++ b/lib/services/p2p/bcoin.js @@ -10,7 +10,7 @@ var Bcoin = function(options) { this.emitter = new EE(); }; -Bcoin.prototype.start = function(callback) { +Bcoin.prototype.start = function(done) { var self = this; self._bcoin = bcoin.fullnode(self._config); @@ -18,11 +18,14 @@ Bcoin.prototype.start = function(callback) { self._bcoin.open().then(function() { self._bcoin.connect().then(function() { - self._bcoin.pool.once('full', function() { - callback(); - }); - self._bcoin.startSync(); log.info('Waiting for Bcoin to sync'); + self._bcoin.startSync(); + if (self._bcoin.chain.synced){ + return done(); + } + self._bcoin.chain.once('full', function() { + done(); + }); }); }); }; diff --git a/lib/services/p2p/index.js b/lib/services/p2p/index.js index 4ad110bc..edaecfb0 100644 --- a/lib/services/p2p/index.js +++ b/lib/services/p2p/index.js @@ -171,17 +171,14 @@ P2P.prototype._broadcast = function(subscribers, name, entity) { P2P.prototype._connect = function() { var self = this; - 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.'); + self._pool.connect(); + var retryInterval = setInterval(function() { self._pool.connect(); - var retryInterval = setInterval(function() { - self._pool.connect(); - }, 5000); - self._pool.once('peerready', function() { - clearInterval(retryInterval); - }); - } + }, 5000); + self._pool.once('peerready', function() { + clearInterval(retryInterval); + }); }; P2P.prototype._getBestHeight = function() { @@ -315,9 +312,7 @@ 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)); - if (!self._bcoin) { - self.node.on('ready', self._connect.bind(self)); - } + self.node.on('ready', self._connect.bind(self)); }; P2P.prototype._setResourceFilter = function(filter, resource) {