Skip to content

Commit

Permalink
Populate DHT with Pear.config.dht (#193)
Browse files Browse the repository at this point in the history
* Populate DHT with Pear.config.dht

* addNode: shouldAddNode

* shouldAddNode -> filterNode

* KNOWN_NODES

* add options.knownNodes

* options.knownNodes

* KNOWN_NODES minor change
  • Loading branch information
AndreiRegiani authored Sep 26, 2024
1 parent 368ad2c commit 39fc60d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -21,8 +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, addNode })
super({ ...opts, port, bootstrap, filterNode })

for (const node of knownNodes) this.addNode(node)

const { router, persistent } = defaultCacheOpts(opts)

Expand Down Expand Up @@ -543,7 +546,7 @@ function toRange (n) {
return typeof n === 'number' ? [n, n] : n
}

function addNode (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')
Expand Down
2 changes: 2 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions test/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,18 @@ test('fail to bootstrap completely', async function (t) {

await a.destroy()
})

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 }]

const b = new DHT({ knownNodes, bootstrap: [] })
await b.ready()

t.alike(b.toArray(), [{ host: '127.0.0.1', port: a.address().port }])

a.destroy()
b.destroy()
delete global.Pear
})

0 comments on commit 39fc60d

Please sign in to comment.