Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate DHT with Pear.config.dht #193

Merged
merged 7 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 || []
AndreiRegiani marked this conversation as resolved.
Show resolved Hide resolved

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
})
Loading