From 7a8fcb7b47ff72e4a736a536d0b5bdd7da1a7af1 Mon Sep 17 00:00:00 2001 From: Andrei Regiani Date: Tue, 24 Sep 2024 01:11:19 -0300 Subject: [PATCH 1/7] Populate DHT with Pear.config.dht --- index.js | 10 ++++++++-- test/connections.js | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e230b9e6..087c875a 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,13 @@ class HyperDHT extends DHT { const port = opts.port || 49737 const bootstrap = opts.bootstrap || BOOTSTRAP_NODES - super({ ...opts, port, bootstrap, addNode }) + super({ ...opts, port, bootstrap, shouldAddNode }) + + if (Array.isArray(global?.Pear?.config?.dht)) { + global.Pear.config.dht.forEach(node => { + this.addNode(node) + }) + } const { router, persistent } = defaultCacheOpts(opts) @@ -543,7 +549,7 @@ function toRange (n) { return typeof n === 'number' ? [n, n] : n } -function addNode (node) { +function shouldAddNode (node) { // always skip these testnet nodes that got mixed in by accident, until they get updated return !(node.port === 49738 && (node.host === '134.209.28.98' || node.host === '167.99.142.185')) && !(node.port === 9400 && node.host === '35.233.47.252') && !(node.host === '150.136.142.116') diff --git a/test/connections.js b/test/connections.js index adb7c6f4..6c7f533d 100644 --- a/test/connections.js +++ b/test/connections.js @@ -788,3 +788,25 @@ test('fail to bootstrap completely', async function (t) { await a.destroy() }) + +test('Populate DHT with available nodes from Pear.config.dht', async function (t) { + const a = new DHT({ bootstrap: [] }) + await a.ready() + + global.Pear = { + config: { + dht: [ + { host: '127.0.0.1', port: a.address().port } + ] + } + } + + const b = new DHT({ bootstrap: [] }) + await b.ready() + + t.alike(b.toArray(), [{ host: '127.0.0.1', port: a.address().port }]) + + a.destroy() + b.destroy() + delete global.Pear +}) From e1ba46f5814174d08297b230a44fea0597f77c12 Mon Sep 17 00:00:00 2001 From: Andrei Regiani Date: Tue, 24 Sep 2024 01:15:21 -0300 Subject: [PATCH 2/7] addNode: shouldAddNode --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 087c875a..1dabdbb4 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ class HyperDHT extends DHT { const port = opts.port || 49737 const bootstrap = opts.bootstrap || BOOTSTRAP_NODES - super({ ...opts, port, bootstrap, shouldAddNode }) + super({ ...opts, port, bootstrap, addNode: shouldAddNode }) if (Array.isArray(global?.Pear?.config?.dht)) { global.Pear.config.dht.forEach(node => { From 0eac12f4357050721fddbbea17ec3a789fabc762 Mon Sep 17 00:00:00 2001 From: Andrei Regiani Date: Tue, 24 Sep 2024 22:52:26 -0300 Subject: [PATCH 3/7] shouldAddNode -> filterNode --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1dabdbb4..66843ef2 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ class HyperDHT extends DHT { const port = opts.port || 49737 const bootstrap = opts.bootstrap || BOOTSTRAP_NODES - super({ ...opts, port, bootstrap, addNode: shouldAddNode }) + super({ ...opts, port, bootstrap, filterNode }) if (Array.isArray(global?.Pear?.config?.dht)) { global.Pear.config.dht.forEach(node => { @@ -549,7 +549,7 @@ function toRange (n) { return typeof n === 'number' ? [n, n] : n } -function shouldAddNode (node) { +function filterNode (node) { // always skip these testnet nodes that got mixed in by accident, until they get updated return !(node.port === 49738 && (node.host === '134.209.28.98' || node.host === '167.99.142.185')) && !(node.port === 9400 && node.host === '35.233.47.252') && !(node.host === '150.136.142.116') From 1afe43af81a08f75efc0d57e12c33b0f8c4da876 Mon Sep 17 00:00:00 2001 From: Andrei Regiani Date: Thu, 26 Sep 2024 00:40:14 -0300 Subject: [PATCH 4/7] KNOWN_NODES --- index.js | 8 ++------ lib/constants.js | 2 ++ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 66843ef2..e6831d62 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ const Persistent = require('./lib/persistent') const Router = require('./lib/router') const Server = require('./lib/server') const connect = require('./lib/connect') -const { FIREWALL, BOOTSTRAP_NODES, COMMANDS } = require('./lib/constants') +const { FIREWALL, BOOTSTRAP_NODES, KNOWN_NODES, COMMANDS } = require('./lib/constants') const { hash, createKeyPair } = require('./lib/crypto') const { decode } = require('hypercore-id-encoding') const RawStreamSet = require('./lib/raw-stream-set') @@ -24,11 +24,7 @@ class HyperDHT extends DHT { super({ ...opts, port, bootstrap, filterNode }) - if (Array.isArray(global?.Pear?.config?.dht)) { - global.Pear.config.dht.forEach(node => { - this.addNode(node) - }) - } + for (const node of KNOWN_NODES) this.addNode(node) const { router, persistent } = defaultCacheOpts(opts) diff --git a/lib/constants.js b/lib/constants.js index 1981801d..416241a1 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -19,6 +19,8 @@ exports.BOOTSTRAP_NODES = [ '138.68.147.8@node3.hyperdht.org:49737' ] +exports.KNOWN_NODES = global?.Pear?.config?.dht || [] + exports.FIREWALL = { UNKNOWN: 0, OPEN: 1, From 3c4c2b47ed661565a0de3bfb7bcc9079af667f21 Mon Sep 17 00:00:00 2001 From: Andrei Regiani Date: Thu, 26 Sep 2024 10:30:53 -0300 Subject: [PATCH 5/7] add options.knownNodes --- index.js | 3 ++- test/connections.js | 11 ++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index e6831d62..ad717af8 100644 --- a/index.js +++ b/index.js @@ -21,10 +21,11 @@ class HyperDHT extends DHT { constructor (opts = {}) { const port = opts.port || 49737 const bootstrap = opts.bootstrap || BOOTSTRAP_NODES + const knownNodes = opts.knownNodes || KNOWN_NODES super({ ...opts, port, bootstrap, filterNode }) - for (const node of KNOWN_NODES) this.addNode(node) + for (const node of knownNodes) this.addNode(node) const { router, persistent } = defaultCacheOpts(opts) diff --git a/test/connections.js b/test/connections.js index 6c7f533d..64680d95 100644 --- a/test/connections.js +++ b/test/connections.js @@ -792,16 +792,9 @@ test('fail to bootstrap completely', async function (t) { test('Populate DHT with available nodes from Pear.config.dht', async function (t) { const a = new DHT({ bootstrap: [] }) await a.ready() + const knownNodes = [{ host: '127.0.0.1', port: a.address().port }] - global.Pear = { - config: { - dht: [ - { host: '127.0.0.1', port: a.address().port } - ] - } - } - - const b = new DHT({ bootstrap: [] }) + const b = new DHT({ knownNodes, bootstrap: [] }) await b.ready() t.alike(b.toArray(), [{ host: '127.0.0.1', port: a.address().port }]) From 4f3eff1f618b6a954160aee8450fed7f9b6d4b38 Mon Sep 17 00:00:00 2001 From: Andrei Regiani Date: Thu, 26 Sep 2024 10:36:26 -0300 Subject: [PATCH 6/7] options.knownNodes --- test/connections.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/connections.js b/test/connections.js index 64680d95..1adb25a1 100644 --- a/test/connections.js +++ b/test/connections.js @@ -789,7 +789,7 @@ test('fail to bootstrap completely', async function (t) { await a.destroy() }) -test('Populate DHT with available nodes from Pear.config.dht', async function (t) { +test('Populate DHT with options.knownNodes', async function (t) { const a = new DHT({ bootstrap: [] }) await a.ready() const knownNodes = [{ host: '127.0.0.1', port: a.address().port }] From 5ead931a04d6363fc2ab373d84e8567f4b6e099b Mon Sep 17 00:00:00 2001 From: Andrei Regiani Date: Thu, 26 Sep 2024 11:40:06 -0300 Subject: [PATCH 7/7] KNOWN_NODES minor change --- lib/constants.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/constants.js b/lib/constants.js index 416241a1..d1948371 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -19,7 +19,7 @@ exports.BOOTSTRAP_NODES = [ '138.68.147.8@node3.hyperdht.org:49737' ] -exports.KNOWN_NODES = global?.Pear?.config?.dht || [] +exports.KNOWN_NODES = global.Pear?.config.dht || [] exports.FIREWALL = { UNKNOWN: 0,