From 435f631f0519b38c74d609102d68a0fe844f0e09 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 19 May 2016 20:34:13 -0700 Subject: [PATCH] stop using hostname getter. --- lib/bcoin/peer.js | 14 ++++---------- lib/bcoin/pool.js | 13 ++++++------- lib/bcoin/utils.js | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index dd180580..83cb6750 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -76,6 +76,7 @@ function Peer(pool, options) { this.socket = null; this.host = null; this.port = 0; + this.hostname = null; this._createSocket = this.options.createSocket; this.priority = this.options.priority; this.chain = this.pool.chain; @@ -118,6 +119,8 @@ function Peer(pool, options) { assert(typeof this.host === 'string'); assert(typeof this.port === 'number'); + this.hostname = utils.stringifyHost(this); + if (!this.socket) throw new Error('No socket'); @@ -214,7 +217,7 @@ Peer.prototype._init = function init() { self.write(self.framer.getAddr()); - self.sendInv(self.pool.inv.list); + self.sendInv(self.pool.inv.items); if (self.pool.options.filterRate != null) { self.write(self.framer.feeFilter({ @@ -1435,15 +1438,6 @@ Peer.prototype.sendReject = function sendReject(obj, code, reason, score) { return this.pool.reject(this, obj, code, reason, score); }; -Peer.prototype.__defineGetter__('hostname', function() { - var host = this.host; - - if (utils.isIP(host) === 6) - host = '[' + host + ']'; - - return host + ':' + this.port; -}); - /** * Inspect the peer. * @returns {String} diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index b46d8722..fccffa61 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -180,7 +180,7 @@ function Pool(options) { // Currently broadcasted objects this.inv = { - list: [], + items: [], map: {}, timeout: options.invTimeout || 60000, interval: options.invInterval || 3000 @@ -1674,7 +1674,7 @@ Pool.prototype.announce = function announce(msg) { this.peers.load.sendInv(msg); for (i = 0; i < this.peers.regular.length; i++) - this.peers.all[i].sendInv(msg); + this.peers.regular[i].sendInv(msg); }; /** @@ -1694,7 +1694,7 @@ Pool.prototype.destroy = function destroy(callback) { this.stopSync(); - this.inv.list.slice().forEach(function(entry) { + this.inv.items.slice().forEach(function(entry) { entry.finish(); }); @@ -2177,7 +2177,7 @@ BroadcastItem.prototype.start = function start() { assert(!this.pool.inv.map[this.hash], 'Already started.'); this.pool.inv.map[this.hash] = this; - utils.binaryInsert(this.pool.inv.list, this, compare); + utils.binaryInsert(this.pool.inv.items, this, compare); this.refresh(); @@ -2201,8 +2201,7 @@ BroadcastItem.prototype.refresh = function refresh() { self.finish(new Error('Timed out.')); }, this.pool.inv.timeout); - for (i = 0; i < this.pool.peers.all.length; i++) - this.pool.peers.all[i].sendInv(this); + this.pool.announce(this); }; /** @@ -2220,7 +2219,7 @@ BroadcastItem.prototype.finish = function finish(err) { this.timeout = null; delete this.pool.inv.map[this.hash]; - utils.binaryRemove(this.pool.inv.list, this, compare); + utils.binaryRemove(this.pool.inv.items, this, compare); for (i = 0; i < this.callback.length; i++) this.callback[i](err); diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index d3333d84..695dccef 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -865,6 +865,21 @@ utils.parseHost = function parseHost(addr) { }; }; +/** + * Stringify a host. + * @param {Seed} seed + * @returns {String} + */ + +utils.stringifyHost = function stringifyHost(seed) { + var host = seed.host; + + if (utils.isIP(host) === 6) + host = '[' + host + ']'; + + return host + ':' + seed.port; +}; + /** * Test whether a string is an IP address. * @param {String?} ip