diff --git a/lib/net/bip150.js b/lib/net/bip150.js index 220cae82..84ce9fcf 100644 --- a/lib/net/bip150.js +++ b/lib/net/bip150.js @@ -469,10 +469,10 @@ function AuthDB(options) { this.logger = null; this.resolve = dns.resolve; this.proxyServer = null; + this.dnsKnown = []; this.known = {}; this.authorized = []; - this.dns = []; this._init(options); } @@ -521,15 +521,17 @@ AuthDB.prototype._init = function _init(options) { AuthDB.prototype.addKnown = function addKnown(host, key) { var addr; - assert(typeof host === 'string'); + assert(typeof host === 'string', + 'Known host must be a string.'); + assert(Buffer.isBuffer(key) && key.length === 33, 'Invalid public key for known peer.'); addr = IP.parseHost(host); - // Defer this for resolution. if (addr.version === -1) { - this.dns.push([addr, key]); + // Defer this for resolution. + this.dnsKnown.push([addr, key]); return; } @@ -556,6 +558,8 @@ AuthDB.prototype.setKnown = function setKnown(map) { var keys = Object.keys(map); var i, host, key; + this.known = {}; + for (i = 0; i < keys.length; i++) { host = keys[i]; key = map[host]; @@ -571,6 +575,8 @@ AuthDB.prototype.setKnown = function setKnown(map) { AuthDB.prototype.setAuthorized = function setAuthorized(keys) { var i, key; + this.authorized.length = 0; + for (i = 0; i < keys.length; i++) { key = keys[i]; this.addAuthorized(key); @@ -596,7 +602,7 @@ AuthDB.prototype.getKnown = function getKnown(hostname) { }; /** - * Lookup any dns-based known peers. + * Lookup known peers. * @private * @returns {Promise} */ @@ -605,18 +611,18 @@ AuthDB.prototype.discover = co(function* discover() { var jobs = []; var i, addr; - for (i = 0; i < this.dns.length; i++) { - addr = this.dns[i]; + for (i = 0; i < this.dnsKnown.length; i++) { + addr = this.dnsKnown[i]; jobs.push(this.populate(addr[0], addr[1])); } - this.dns.length = 0; + this.dnsKnown.length = 0; yield Promise.all(jobs); }); /** - * Populate known peers with a dns-based host. + * Populate known peers with hosts. * @private * @param {Object} addr * @param {Buffer} key @@ -626,7 +632,7 @@ AuthDB.prototype.discover = co(function* discover() { AuthDB.prototype.populate = co(function* populate(addr, key) { var i, hosts, host; - assert(addr.version === -1); + assert(addr.version === -1, 'Resolved host passed.'); if (this.logger) this.logger.info('Resolving authorized hosts from: %s.', addr.host); diff --git a/lib/net/hostlist.js b/lib/net/hostlist.js index 5ecd7723..4c04cad2 100644 --- a/lib/net/hostlist.js +++ b/lib/net/hostlist.js @@ -35,11 +35,13 @@ function HostList(options) { this.proxyServer = null; this.resolve = dns.resolve; this.banTime = common.BAN_TIME; - this.rawSeeds = this.network.seeds; - this.rawNodes = []; - this.seeds = []; + this.rawSeeds = this.network.seeds; + this.dnsSeeds = []; + this.rawNodes = []; + this.dnsNodes = []; this.nodes = []; + this.banned = {}; this.map = {}; @@ -59,7 +61,8 @@ function HostList(options) { this.maxFailures = 10; this.maxRefs = 8; - this._init(options); + this._initOptions(options); + this._init(); } /** @@ -108,7 +111,7 @@ HostList.prototype._initOptions = function initOptions(options) { if (options.nodes) { assert(Array.isArray(options.nodes)); - this.setNodes(options.nodes); + this.rawNodes = options.nodes; } }; @@ -117,11 +120,9 @@ HostList.prototype._initOptions = function initOptions(options) { * @private */ -HostList.prototype._init = function init(options) { +HostList.prototype._init = function init() { var i; - this._initOptions(options); - for (i = 0; i < this.maxBuckets; i++) this.fresh.push(new Map()); @@ -129,6 +130,7 @@ HostList.prototype._init = function init(options) { this.used.push(new List()); this.setSeeds(this.rawSeeds); + this.setNodes(this.rawNodes); }; /** @@ -654,22 +656,6 @@ HostList.prototype.toArray = function toArray() { return out; }; -/** - * Set initial seeds. - * @param {String[]} seeds - */ - -HostList.prototype.setSeeds = function setSeeds(seeds) { - var i, seed; - - this.seeds.length = 0; - - for (i = 0; i < seeds.length; i++) { - seed = seeds[i]; - this.addSeed(seed); - } -}; - /** * Add a preferred seed. * @param {String} host @@ -677,24 +663,18 @@ HostList.prototype.setSeeds = function setSeeds(seeds) { HostList.prototype.addSeed = function addSeed(host) { var addr = IP.parseHost(host, this.network.port); - this.seeds.push(addr); - return addr; -}; -/** - * Set priority nodes. - * @param {String[]} nodes - */ - -HostList.prototype.setNodes = function setNodes(nodes) { - var i, node; - - this.rawNodes.length = 0; - - for (i = 0; i < nodes.length; i++) { - node = nodes[i]; - this.addNode(node); + if (addr.version === -1) { + // Defer for resolution. + this.dnsSeeds.push(addr); + return; } + + addr = NetAddress.fromHost(addr.host, addr.port, this.network); + + this.add(addr); + + return addr; }; /** @@ -704,12 +684,56 @@ HostList.prototype.setNodes = function setNodes(nodes) { HostList.prototype.addNode = function addNode(host) { var addr = IP.parseHost(host, this.network.port); - this.rawNodes.push(addr); + + if (addr.version === -1) { + // Defer for resolution. + this.dnsNodes.push(addr); + return; + } + + addr = NetAddress.fromHost(addr.host, addr.port, this.network); + + this.nodes.push(addr); + this.add(addr); + return addr; }; /** - * Discover hosts from seeds. + * Set initial seeds. + * @param {String[]} seeds + */ + +HostList.prototype.setSeeds = function setSeeds(seeds) { + var i, host; + + this.dnsSeeds.length = 0; + + for (i = 0; i < seeds.length; i++) { + host = seeds[i]; + this.addSeed(host); + } +}; + +/** + * Set priority nodes. + * @param {String[]} nodes + */ + +HostList.prototype.setNodes = function setNodes(nodes) { + var i, host; + + this.dnsNodes.length = 0; + this.nodes.length = 0; + + for (i = 0; i < nodes.length; i++) { + host = nodes[i]; + this.addNode(host); + } +}; + +/** + * Discover hosts from seeds and nodes. * @returns {Promise} */ @@ -717,16 +741,18 @@ HostList.prototype.discover = co(function* discover() { var jobs = []; var i, node, seed; - for (i = 0; i < this.rawNodes.length; i++) { - node = this.rawNodes[i]; - jobs.push(this.lookupNode(node)); + for (i = 0; i < this.dnsSeeds.length; i++) { + seed = this.dnsSeeds[i]; + jobs.push(this.populateSeed(seed)); } - for (i = 0; i < this.seeds.length; i++) { - seed = this.seeds[i]; - jobs.push(this.populate(seed)); + for (i = 0; i < this.dnsNodes.length; i++) { + node = this.dnsNodes[i]; + jobs.push(this.populateNode(node)); } + this.dnsNodes.length = 0; + yield Promise.all(jobs); }); @@ -736,8 +762,8 @@ HostList.prototype.discover = co(function* discover() { * @returns {Promise} */ -HostList.prototype.lookupNode = co(function* lookupNode(addr) { - var addrs = yield this.lookup(addr); +HostList.prototype.populateNode = co(function* populateNode(addr) { + var addrs = yield this.populate(addr); if (addrs.length === 0) return; @@ -752,8 +778,8 @@ HostList.prototype.lookupNode = co(function* lookupNode(addr) { * @returns {Promise} */ -HostList.prototype.populate = co(function* populate(seed) { - var addrs = yield this.lookup(seed); +HostList.prototype.populateSeed = co(function* populateSeed(seed) { + var addrs = yield this.populate(seed); var i, addr; for (i = 0; i < addrs.length; i++) { @@ -768,15 +794,11 @@ HostList.prototype.populate = co(function* populate(seed) { * @returns {Promise} */ -HostList.prototype.lookup = co(function* lookup(target) { +HostList.prototype.populate = co(function* populate(target) { var addrs = []; var i, addr, hosts, host; - if (target.version !== -1) { - addr = NetAddress.fromHost(target.host, target.port, this.network); - addrs.push(addr); - return addrs; - } + assert(target.version === -1, 'Resolved host passed.'); if (this.logger) this.logger.info('Resolving host: %s.', target.host);