diff --git a/lib/services/header/index.js b/lib/services/header/index.js index 7e17e57a..1040642a 100644 --- a/lib/services/header/index.js +++ b/lib/services/header/index.js @@ -415,6 +415,11 @@ HeaderService.prototype._onHeader = function(header) { HeaderService.prototype._onHeaders = function(headers) { var self = this; + + if (self._headerInterval) { + clearInterval(self._headerInterval); + } + log.debug('Header Service: Received: ' + headers.length + ' header(s).'); var dbOps = []; @@ -666,10 +671,26 @@ HeaderService.prototype._startSync = function() { HeaderService.prototype._sync = function() { - log.info('Header Service: download progress: ' + this._tip.height + '/' + - this._bestHeight + ' (' + (this._tip.height / this._bestHeight*100.00).toFixed(2) + '%)'); + var self = this; - this._p2p.getHeaders({ startHash: this._tip.hash }); + var progress; + if (self._bestHeight === 0) { + progress = 0; + } else { + progress = (self._tip.height / self._bestHeight*100.00).toFixed(2); + } + + log.info('Header Service: download progress: ' + self._tip.height + '/' + + self._bestHeight + ' (' + progress + '%)'); + + self._p2p.getHeaders({ startHash: self._tip.hash }); + + // when connecting to a peer that isn't yet responding to getHeaders, we will start a interval timer + // to retry until we can get headers, this may be a very long interval + self._headerInterval = setInterval(function() { + log.info('Header Service: retrying get headers since ' + self._tip.hash); + self._p2p.getHeaders({ startHash: self._tip.hash }); + }, 2000); }; diff --git a/lib/services/p2p/bcoin.js b/lib/services/p2p/bcoin.js index fcff73c9..c3baa8fb 100644 --- a/lib/services/p2p/bcoin.js +++ b/lib/services/p2p/bcoin.js @@ -3,14 +3,12 @@ var index = require('../../'); var log = index.log; var bcoin = require('bcoin'); -var EE = require('events').EventEmitter; var Bcoin = function(options) { this._config = this._getConfig(options); - this.emitter = new EE(); }; -Bcoin.prototype.start = function(done) { +Bcoin.prototype.start = function(callback) { var self = this; self._bcoin = bcoin.fullnode(self._config); @@ -20,12 +18,8 @@ Bcoin.prototype.start = function(done) { self._bcoin.connect().then(function() { log.info('Waiting for Bcoin to sync'); self._bcoin.startSync(); - if (self._bcoin.chain.synced){ - return done(); - } - self._bcoin.chain.once('full', function() { - done(); - }); + // this will instruct the p2p service to start trying to connect to bcoin right away + callback(); }); }); }; diff --git a/lib/services/p2p/index.js b/lib/services/p2p/index.js index 55461e48..487ee3bc 100644 --- a/lib/services/p2p/index.js +++ b/lib/services/p2p/index.js @@ -128,9 +128,11 @@ P2P.prototype._disconnectPool = function() { }; P2P.prototype.stop = function(callback) { + if (this._bcoin){ return this._bcoin.stop(callback); } + setImmediate(callback); }; @@ -297,10 +299,31 @@ P2P.prototype._onPeerInventory = function(peer, message) { } }; +P2P.prototype._matchNetwork = function(network) { + + if (this.node.network !== network.name && + this.node.network !== network.alias) { + log.error('Configured network: "' + this.node.network + + '" does not match our peer\'s reported network: "' + + network.name + '".'); + return this.node.stop(); + } + + return this.node.network === network.name ? network.name : network.alias; + +}; + P2P.prototype._onPeerReady = function(peer, addr) { + // want to make sure the peer we are connecting to matches our network config. + var network = this._matchNetwork(peer.network); + + if (!network) { + return; + } + log.info('Connected to peer: ' + addr.ip.v4 + ', network: ' + - peer.network.alias + ', version: ' + peer.version + ', subversion: ' + + network + ', version: ' + peer.version + ', subversion: ' + peer.subversion + ', status: ' + peer.status + ', port: ' + peer.port + ', best height: ' + peer.bestHeight); @@ -362,20 +385,36 @@ P2P.prototype._setResourceFilter = function(filter, resource) { }; P2P.prototype._startBcoin = function(callback) { + var self = this; - const network = ['livenet', 'live', 'main', 'mainnet'].indexOf(this.node.network) !== -1? 'main' : 'testnet'; + + var network; + var port; + if (['livenet', 'live', 'main', 'mainnet'].indexOf(this.node.network) !== -1) { + network = 'main'; + port = this._configPeers[0].port || 8333; + } else if (this.node.network !== 'regtest') { + network = 'testnet'; + port = this._configPeers[0].port || 18333; + } else { + network = this.node.network; + port = this._configPeers[0].port || 48444; + } + self._bcoin = new Bcoin({ network: network, prefix: self.node.datadir, - port: 48333 + port: port }); + self._bcoin.start(callback); + }; P2P.prototype._startBcoinIfNecessary = function(callback) { if (!this._hasPeers()) { log.info('Peers not explicitly configured, starting a local bcoin node.'); - this._configPeers = [{ip: {v4: '127.0.0.1'}, port: 48333}]; + this._configPeers = [{ ip: { v4: '127.0.0.1'} }]; return this._startBcoin(callback); } setImmediate(callback); diff --git a/package-lock.json b/package-lock.json index ceb7d68b..bbc98cc2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "bitcore-node", - "version": "5.0.0-beta.3", + "version": "5.0.0-beta.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index cfb70c37..92536e36 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ ], "dependencies": { "async": "^2.5.0", - "bcoin": "bcoin-org/bcoin#57b98b2368c8106a9c215d73e40e1a7ae39042f3", + "bcoin": "1.0.0-beta.14", "bitcoind-rpc": "^0.6.0", "bitcore-lib": "5.0.0-beta.1", "bitcore-p2p": "5.0.0-beta.1", diff --git a/test/services/db/index.unit.js b/test/services/db/index.unit.js index b04dc692..d2f2833c 100644 --- a/test/services/db/index.unit.js +++ b/test/services/db/index.unit.js @@ -233,6 +233,7 @@ describe('DB', function() { }); describe('#close', function() { + this.timeout(3000); it('should close the store if there is a store and it is open', function(done) { var close = sandbox.stub().callsArgWith(0, null); @@ -246,6 +247,7 @@ describe('DB', function() { done(); }); }); + this.timeout(2000); }); describe('#getServiceTip', function() {