diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index c5f5cfb5..78aeb754 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -57,6 +57,7 @@ function Pool(node, options) { this.destroyed = false; this.loaded = false; this.size = options.size || 8; + this.connected = false; this.chain = node.chain; this.mempool = node.mempool; @@ -171,6 +172,8 @@ Pool.prototype._init = function _init() { for (i = 0; i < this.size; i++) this._addPeer(); + + this.connected = true; } this.chain.on('block', function(block, entry, peer) { @@ -1737,6 +1740,9 @@ Pool.prototype.getSeed = function getSeed(priority) { var addr; if (priority) { + if (!this.connected) + return this.originalSeeds[0]; + addr = this._getRandom(this.originalSeeds); if (addr) return addr; @@ -1745,11 +1751,11 @@ Pool.prototype.getSeed = function getSeed(priority) { if (addr) return addr; - addr = this.seeds[Math.random() * (this.seeds.length - 1) | 0]; + addr = this.seeds[Math.random() * this.seeds.length | 0]; if (addr) return addr; - return this.originalSeeds[Math.random() * (this.originalSeeds.length - 1) | 0]; + return this.originalSeeds[Math.random() * this.originalSeeds.length | 0]; } // Hang back if we don't have a loader peer yet. @@ -1780,7 +1786,7 @@ Pool.prototype._getRandom = function _getRandom(seeds, uniq) { if (tries === seeds.length) return; - index = Math.round(Math.random() * (seeds.length - 1)); + index = Math.random() * seeds.length | 0; addr = seeds[index]; if (!tried[index]) { @@ -1884,13 +1890,10 @@ Pool.prototype.reject = function reject(peer, obj, reason, dos) { utils.debug('Rejecting %s %s: reason=%s', obj.type, obj.hash('hex'), reason); - if (!peer) - return false; - - // peer.reject({ - // reason: reason, - // data: obj.hash() - // }); + peer.reject({ + reason: reason, + data: obj.hash() + }); return false; };