diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index df87cfe2..7a901a88 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -413,18 +413,14 @@ Chain.prototype.getLast = function getLast(cb) { return cb(this.index.hashes[this.index.hashes.length - 1]); }; -Chain.prototype.getStartHeight = function getLast(cb) { +Chain.prototype.getStartHeight = function getStartHeight() { if (!this.options.fullNode) { if (this.options.startHeight != null) { return this.options.startHeight; } return 0; } - for (var i = 0; i < this.index.heights.length; i++) { - if (this.index.heights[i + 1] !== this.index.heights[i] + 1) { - return this.index.heights[i]; - } - } + return this.index.heights[this.index.heights.length - 1]; }; Chain.prototype.locatorHashes = function(index) { diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 215ca533..8b8212c8 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -96,9 +96,7 @@ Peer.prototype._init = function init() { 'Sent version (%s): height=%s', ip, this.pool.chain.getStartHeight()); self.pool.emit('debug', 'version (%s): sending locator hashes', ip); - self.chain.on('load', function() { - self.loadHeaders(self.chain.locatorHashes(), 0); - }); + self.loadHeaders(self.chain.locatorHashes(), 0); }); } diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index b56b89ba..07e127e1 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -7,6 +7,8 @@ var utils = bcoin.utils; var assert = utils.assert; function Pool(options) { + var self = this; + if (!(this instanceof Pool)) return new Pool(options); @@ -90,7 +92,18 @@ function Pool(options) { this.createConnection = options.createConnection; assert(this.createConnection); - this._init(); + this.chain.on('debug', function() { + var args = Array.prototype.slice.call(arguments); + self.emit.apply(self, ['debug'].concat(args)); + }); + + if (!this.chain.loading) { + this._init(); + } else { + this.chain.once('load', function() { + self._init(); + }); + } } inherits(Pool, EventEmitter); module.exports = Pool; @@ -109,11 +122,6 @@ Pool.prototype._init = function _init() { self._scheduleRequests(); self._loadRange(preload); }); - - this.chain.on('debug', function() { - var args = Array.prototype.slice.call(arguments); - self.emit.apply(self, ['debug'].concat(args)); - }); }; Pool.prototype._addLoader = function _addLoader() {