diff --git a/lib/services/p2p/bcoin.js b/lib/services/p2p/bcoin.js index 487a539e..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() { +Bcoin.prototype.start = function(done) { var self = this; self._bcoin = bcoin.fullnode(self._config); @@ -18,8 +18,14 @@ Bcoin.prototype.start = function() { self._bcoin.open().then(function() { self._bcoin.connect().then(function() { - self.emitter.emit('connect'); + log.info('Waiting for Bcoin to sync'); 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) { var config = { + db: 'leveldb', checkpoints: true, network: options.network || 'main', - listen: true + listen: true, + logConsole: true, + logLevel: 'info', + port: options.port, + persistent: true, + 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 183ffb94..ae178887 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,11 +170,15 @@ P2P.prototype._broadcast = function(subscribers, name, entity) { }; P2P.prototype._connect = function() { - this._connectCalled = this._connectCalled > 0 ? 2 : 1; - if (this._connectCalled > 1 || !this._bcoin) { - log.info('Connecting to p2p network.'); - this._pool.connect(); - } + var self = this; + log.info('Connecting to p2p network.'); + self._pool.connect(); + var retryInterval = setInterval(function() { + self._pool.connect(); + }, 5000); + self._pool.once('peerready', function() { + clearInterval(retryInterval); + }); }; P2P.prototype._getBestHeight = function() { @@ -181,7 +187,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 +304,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)); @@ -308,10 +313,6 @@ P2P.prototype._setListeners = function() { 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)); - } - }; 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() { var network = ['livenet', 'live', 'main', 'mainnet'].indexOf(this.node.network) !== -1? 'main' : 'testnet'; this._bcoin = new Bcoin({ +>>>>>>> 8811190de0a9e09baef7e63d85e065e72b9da8a4 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;