From 56a5d383bbb3995b7e6184f6b280094610b63bcd Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 4 May 2023 12:02:42 +0100 Subject: [PATCH 1/5] fix!: do not dial private addresses in browsers Dialling is very expensive in browsers. If we're dialling addresses that include private networks, chances are they're not going to succeed so filter them out for browsers only. This can be re-enabled by configuring a permissive connection gater, see the upgrade guide for more information. BREAKING CHANGE: browsers will no longer try to dial private addresses by default --- doc/migrations/v0.44-v0.45.md | 37 ++++++++++++++++++++++++ package.json | 1 + src/config/connection-gater.browser.ts | 31 ++++++++++++++++++++ src/config/connection-gater.ts | 19 ++++++++++++ src/libp2p.ts | 14 ++------- test/configuration/utils.ts | 4 ++- test/connection-manager/direct.spec.ts | 12 +++++--- test/connection-manager/resolver.spec.ts | 8 +++-- test/upgrading/upgrader.spec.ts | 15 ++++++---- test/utils/base-options.browser.ts | 4 ++- 10 files changed, 119 insertions(+), 26 deletions(-) create mode 100644 src/config/connection-gater.browser.ts create mode 100644 src/config/connection-gater.ts diff --git a/doc/migrations/v0.44-v0.45.md b/doc/migrations/v0.44-v0.45.md index c8b1aa8365..d666e7bf3f 100644 --- a/doc/migrations/v0.44-v0.45.md +++ b/doc/migrations/v0.44-v0.45.md @@ -13,6 +13,7 @@ A migration guide for refactoring your application code from libp2p v0.44.x to v - [`peer:update`](#peerupdate) - [`self:peer:update`](#selfpeerupdate) - [Atomic peer store methods](#atomic-peer-store-methods) +- [Do not dial private addresses by default in browsers](#do-not-dial-private-addresses-by-default-in-browsers) ## Services @@ -234,7 +235,43 @@ await node.peerStore.merge(peerId, { }) ``` +## Do not dial private addresses by default in browsers + +Browsers are incredibly resource-constrained environments in which to run a network-heavy program like libp2p. + +This is compounded by the fact that remote peers often include private network addresses in their peer records, so a libp2p node will often waste resources by trying to dial unroutable addresses. + +The default [connection gater][] used by libp2p in browsers will filter out any private addresses and not attempt to dial them. + +No change has been made to the connection gater used by Node.js. + +This can be re-enabled by configuring a more permissive connection gater: + +**Before** + +```js +import { createLibp2p } from 'libp2p' + +const node = createLibp2p({ + // ... other options here +}) +``` + +**After** + +```js +import { createLibp2p } from 'libp2p' + +const node = createLibp2p({ + connectionGater: { + denyDialMultiaddr: () => false + } + // ... other options here +}) +``` + [Connection]: https://libp2p.github.io/js-libp2p-interfaces/interfaces/_libp2p_interface_connection.Connection.html [PeerId]: https://libp2p.github.io/js-libp2p-interfaces/types/_libp2p_interface_peer_id.PeerId.html [Identify]: https://github.com/libp2p/specs/blob/master/identify/README.md [AutoNAT]: https://github.com/libp2p/specs/blob/master/autonat/README.md +[Connection Gater]: https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md#configuring-connection-gater \ No newline at end of file diff --git a/package.json b/package.json index dade51f8fd..0b85de1a3b 100644 --- a/package.json +++ b/package.json @@ -223,6 +223,7 @@ "sinon-ts": "^1.0.0" }, "browser": { + "./dist/src/config/connection-gater.js": "./dist/src/config/connection-gater.browser.js", "nat-api": false } } diff --git a/src/config/connection-gater.browser.ts b/src/config/connection-gater.browser.ts new file mode 100644 index 0000000000..15466e75ea --- /dev/null +++ b/src/config/connection-gater.browser.ts @@ -0,0 +1,31 @@ +import type { ConnectionGater } from '@libp2p/interface-connection-gater' +import type { Multiaddr } from '@multiformats/multiaddr' +import isPrivate from 'private-ip' + +/** + * Returns a connection gater that disallows dialling private addresses by + * default. Browsers are severely limited in their resource usage so don't + * waste time trying to dial undiallable addresses. + */ +export function connectionGater (gater: ConnectionGater = {}): ConnectionGater { + return { + denyDialPeer: async () => await Promise.resolve(false), + denyDialMultiaddr: async (multiaddr: Multiaddr) => { + const tuples = multiaddr.stringTuples() + + if (tuples[0][0] === 4 || tuples[0][0] === 41) { + return Boolean(isPrivate(`${tuples[0][1]}`)) + } + + return false + }, + denyInboundConnection: async () => await Promise.resolve(false), + denyOutboundConnection: async () => await Promise.resolve(false), + denyInboundEncryptedConnection: async () => await Promise.resolve(false), + denyOutboundEncryptedConnection: async () => await Promise.resolve(false), + denyInboundUpgradedConnection: async () => await Promise.resolve(false), + denyOutboundUpgradedConnection: async () => await Promise.resolve(false), + filterMultiaddrForPeer: async () => await Promise.resolve(true), + ...gater + } +} diff --git a/src/config/connection-gater.ts b/src/config/connection-gater.ts new file mode 100644 index 0000000000..99b9895bf1 --- /dev/null +++ b/src/config/connection-gater.ts @@ -0,0 +1,19 @@ +import type { ConnectionGater } from '@libp2p/interface-connection-gater' + +/** + * Returns a default connection gater implementation that allows everything + */ +export function connectionGater (gater: ConnectionGater = {}): ConnectionGater { + return { + denyDialPeer: async () => await Promise.resolve(false), + denyDialMultiaddr: async () => await Promise.resolve(false), + denyInboundConnection: async () => await Promise.resolve(false), + denyOutboundConnection: async () => await Promise.resolve(false), + denyInboundEncryptedConnection: async () => await Promise.resolve(false), + denyOutboundEncryptedConnection: async () => await Promise.resolve(false), + denyInboundUpgradedConnection: async () => await Promise.resolve(false), + denyOutboundUpgradedConnection: async () => await Promise.resolve(false), + filterMultiaddrForPeer: async () => await Promise.resolve(true), + ...gater + } +} diff --git a/src/libp2p.ts b/src/libp2p.ts index e109fe1d93..e4916fa72f 100644 --- a/src/libp2p.ts +++ b/src/libp2p.ts @@ -38,6 +38,7 @@ import { validateConfig } from './config.js' import { ContentRouting, contentRouting } from '@libp2p/interface-content-routing' import { PeerRouting, peerRouting } from '@libp2p/interface-peer-routing' import { peerDiscovery } from '@libp2p/interface-peer-discovery' +import { connectionGater } from './config/connection-gater.js' const log = logger('libp2p') @@ -82,18 +83,7 @@ export class Libp2pNode extends EventEmitter await Promise.resolve(false), - denyDialMultiaddr: async () => await Promise.resolve(false), - denyInboundConnection: async () => await Promise.resolve(false), - denyOutboundConnection: async () => await Promise.resolve(false), - denyInboundEncryptedConnection: async () => await Promise.resolve(false), - denyOutboundEncryptedConnection: async () => await Promise.resolve(false), - denyInboundUpgradedConnection: async () => await Promise.resolve(false), - denyOutboundUpgradedConnection: async () => await Promise.resolve(false), - filterMultiaddrForPeer: async () => await Promise.resolve(true), - ...init.connectionGater - } + connectionGater: connectionGater(init.connectionGater) }) this.peerStore = this.configureComponent('peerStore', new PersistentPeerStore(components, { diff --git a/test/configuration/utils.ts b/test/configuration/utils.ts index 3862147c9e..fafd199c2d 100644 --- a/test/configuration/utils.ts +++ b/test/configuration/utils.ts @@ -10,6 +10,7 @@ import type { Libp2pInit, Libp2pOptions } from '../../src/index.js' import type { PeerId } from '@libp2p/interface-peer-id' import * as cborg from 'cborg' import { circuitRelayTransport } from '../../src/circuit-relay/index.js' +import { mockConnectionGater } from '@libp2p/interface-mocks' const relayAddr = MULTIADDRS_WEBSOCKETS[0] @@ -88,5 +89,6 @@ export const pubsubSubsystemOptions: Libp2pOptions<{ pubsub: PubSub }> = mergeOp ], services: { pubsub: (components: PubSubComponents) => new MockPubSub(components) - } + }, + connectionGater: mockConnectionGater() }) diff --git a/test/connection-manager/direct.spec.ts b/test/connection-manager/direct.spec.ts index a365a4c32b..65ee548bfc 100644 --- a/test/connection-manager/direct.spec.ts +++ b/test/connection-manager/direct.spec.ts @@ -373,7 +373,8 @@ describe('libp2p.dialer (direct, WebSockets)', () => { ], services: { identify: identifyService() - } + }, + connectionGater: mockConnectionGater() }) if (libp2p.services.identify == null) { @@ -413,7 +414,8 @@ describe('libp2p.dialer (direct, WebSockets)', () => { ], connectionEncryption: [ plaintext() - ] + ], + connectionGater: mockConnectionGater() }) await libp2p.start() @@ -441,7 +443,8 @@ describe('libp2p.dialer (direct, WebSockets)', () => { ], connectionEncryption: [ plaintext() - ] + ], + connectionGater: mockConnectionGater() }) await libp2p.hangUp(MULTIADDRS_WEBSOCKETS[0]) @@ -460,7 +463,8 @@ describe('libp2p.dialer (direct, WebSockets)', () => { ], connectionEncryption: [ plaintext() - ] + ], + connectionGater: mockConnectionGater() }) await libp2p.start() diff --git a/test/connection-manager/resolver.spec.ts b/test/connection-manager/resolver.spec.ts index 5640920685..05d02e5b33 100644 --- a/test/connection-manager/resolver.spec.ts +++ b/test/connection-manager/resolver.spec.ts @@ -8,7 +8,7 @@ import { codes as ErrorCodes } from '../../src/errors.js' import { MULTIADDRS_WEBSOCKETS } from '../fixtures/browser.js' import type { PeerId } from '@libp2p/interface-peer-id' import pDefer from 'p-defer' -import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-mocks' +import { mockConnection, mockConnectionGater, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-mocks' import { peerIdFromString } from '@libp2p/peer-id' import { createFromJSON } from '@libp2p/peer-id-factory' import { RELAY_V2_HOP_CODEC } from '../../src/circuit-relay/constants.js' @@ -66,7 +66,8 @@ describe('dialing (resolvable addresses)', () => { }, connectionEncryption: [ plaintext() - ] + ], + connectionGater: mockConnectionGater() }), createLibp2pNode({ addresses: { @@ -91,7 +92,8 @@ describe('dialing (resolvable addresses)', () => { ], services: { relay: circuitRelayServer() - } + }, + connectionGater: mockConnectionGater() }) ]) diff --git a/test/upgrading/upgrader.spec.ts b/test/upgrading/upgrader.spec.ts index a93fa64a30..c70ab46c57 100644 --- a/test/upgrading/upgrader.spec.ts +++ b/test/upgrading/upgrader.spec.ts @@ -677,7 +677,8 @@ describe('libp2p.upgrader', () => { ], connectionEncryption: [ plaintext() - ] + ], + connectionGater: mockConnectionGater() }) await libp2p.start() @@ -694,7 +695,8 @@ describe('libp2p.upgrader', () => { ], connectionEncryption: [ plaintext() - ] + ], + connectionGater: mockConnectionGater() }) await remoteLibp2p.start() @@ -745,7 +747,8 @@ describe('libp2p.upgrader', () => { test: (components: any) => { localDeferred.resolve(components) } - } + }, + connectionGater: mockConnectionGater() }) remoteLibp2p = await createLibp2p({ @@ -763,7 +766,8 @@ describe('libp2p.upgrader', () => { test: (components: any) => { remoteDeferred.resolve(components) } - } + }, + connectionGater: mockConnectionGater() }) const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer }) @@ -820,7 +824,8 @@ describe('libp2p.upgrader', () => { test: (components: any) => { localDeferred.resolve(components) } - } + }, + connectionGater: mockConnectionGater() }) remoteLibp2p = await createLibp2p({ diff --git a/test/utils/base-options.browser.ts b/test/utils/base-options.browser.ts index 9c1a5e82c8..dfcd782ecc 100644 --- a/test/utils/base-options.browser.ts +++ b/test/utils/base-options.browser.ts @@ -7,6 +7,7 @@ import type { Libp2pOptions } from '../../src/index.js' import mergeOptions from 'merge-options' import { circuitRelayTransport } from '../../src/circuit-relay/index.js' import type { ServiceMap } from '@libp2p/interface-libp2p' +import { mockConnectionGater } from '@libp2p/interface-mocks' export function createBaseOptions (overrides?: Libp2pOptions): Libp2pOptions { const options: Libp2pOptions = { @@ -21,7 +22,8 @@ export function createBaseOptions (overrides?: Libp2 ], connectionEncryption: [ plaintext() - ] + ], + connectionGater: mockConnectionGater() } return mergeOptions(options, overrides) From 222e07ac2c509c2740df945c9f4aaeca33b8db0a Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 4 May 2023 12:18:11 +0100 Subject: [PATCH 2/5] chore: fix tests --- examples/webrtc-direct/dialer.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/webrtc-direct/dialer.js b/examples/webrtc-direct/dialer.js index 913e9d4119..071e07b263 100644 --- a/examples/webrtc-direct/dialer.js +++ b/examples/webrtc-direct/dialer.js @@ -15,7 +15,10 @@ document.addEventListener('DOMContentLoaded', async () => { bootstrap({ list: [`/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct/p2p/${hardcodedPeerId}`] }) - ] + ], + connectionGater: { + denyDialMultiaddr: () => false + } }) const status = document.getElementById('status') From ff921b0909fefde006c2aa88d115c6bf4120b76e Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 4 May 2023 15:42:18 +0100 Subject: [PATCH 3/5] chore: remove resolves --- src/config/connection-gater.browser.ts | 16 ++++++++-------- src/config/connection-gater.ts | 18 +++++++++--------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/config/connection-gater.browser.ts b/src/config/connection-gater.browser.ts index 15466e75ea..c5c58581fb 100644 --- a/src/config/connection-gater.browser.ts +++ b/src/config/connection-gater.browser.ts @@ -9,7 +9,7 @@ import isPrivate from 'private-ip' */ export function connectionGater (gater: ConnectionGater = {}): ConnectionGater { return { - denyDialPeer: async () => await Promise.resolve(false), + denyDialPeer: async () => false, denyDialMultiaddr: async (multiaddr: Multiaddr) => { const tuples = multiaddr.stringTuples() @@ -19,13 +19,13 @@ export function connectionGater (gater: ConnectionGater = {}): ConnectionGater { return false }, - denyInboundConnection: async () => await Promise.resolve(false), - denyOutboundConnection: async () => await Promise.resolve(false), - denyInboundEncryptedConnection: async () => await Promise.resolve(false), - denyOutboundEncryptedConnection: async () => await Promise.resolve(false), - denyInboundUpgradedConnection: async () => await Promise.resolve(false), - denyOutboundUpgradedConnection: async () => await Promise.resolve(false), - filterMultiaddrForPeer: async () => await Promise.resolve(true), + denyInboundConnection: async () => false, + denyOutboundConnection: async () => false, + denyInboundEncryptedConnection: async () => false, + denyOutboundEncryptedConnection: async () => false, + denyInboundUpgradedConnection: async () => false, + denyOutboundUpgradedConnection: async () => false, + filterMultiaddrForPeer: async () => true, ...gater } } diff --git a/src/config/connection-gater.ts b/src/config/connection-gater.ts index 99b9895bf1..2f872a2ee6 100644 --- a/src/config/connection-gater.ts +++ b/src/config/connection-gater.ts @@ -5,15 +5,15 @@ import type { ConnectionGater } from '@libp2p/interface-connection-gater' */ export function connectionGater (gater: ConnectionGater = {}): ConnectionGater { return { - denyDialPeer: async () => await Promise.resolve(false), - denyDialMultiaddr: async () => await Promise.resolve(false), - denyInboundConnection: async () => await Promise.resolve(false), - denyOutboundConnection: async () => await Promise.resolve(false), - denyInboundEncryptedConnection: async () => await Promise.resolve(false), - denyOutboundEncryptedConnection: async () => await Promise.resolve(false), - denyInboundUpgradedConnection: async () => await Promise.resolve(false), - denyOutboundUpgradedConnection: async () => await Promise.resolve(false), - filterMultiaddrForPeer: async () => await Promise.resolve(true), + denyDialPeer: async () => false, + denyDialMultiaddr: async () => false, + denyInboundConnection: async () => false, + denyOutboundConnection: async () => false, + denyInboundEncryptedConnection: async () => false, + denyOutboundEncryptedConnection: async () => false, + denyInboundUpgradedConnection: async () => false, + denyOutboundUpgradedConnection: async () => false, + filterMultiaddrForPeer: async () => true, ...gater } } From 5dd79909aa0e1901a63db8f8c80295050775db27 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 4 May 2023 19:38:59 +0100 Subject: [PATCH 4/5] chore: update peer discovery methods --- examples/delegated-routing/package.json | 2 +- examples/libp2p-in-the-browser/package.json | 2 +- examples/webrtc-direct/package.json | 2 +- package.json | 11 +++++------ test/content-routing/content-routing.node.ts | 6 +++--- test/content-routing/dht/operation.node.ts | 14 +++++++++----- test/content-routing/utils.ts | 4 ++-- test/core/get-public-key.spec.ts | 4 ++-- test/peer-discovery/index.node.ts | 10 +++++----- test/peer-routing/peer-routing.node.ts | 8 ++++---- test/peer-routing/utils.ts | 4 ++-- 11 files changed, 35 insertions(+), 32 deletions(-) diff --git a/examples/delegated-routing/package.json b/examples/delegated-routing/package.json index 1478d8570b..2534983cd2 100644 --- a/examples/delegated-routing/package.json +++ b/examples/delegated-routing/package.json @@ -9,7 +9,7 @@ "libp2p": "file:../../", "@libp2p/delegated-content-routing": "^4.0.0", "@libp2p/delegated-peer-routing": "^4.0.0", - "@libp2p/kad-dht": "^9.1.0", + "@libp2p/kad-dht": "^9.1.4", "@libp2p/mplex": "^8.0.1", "@libp2p/webrtc-star": "^7.0.0", "@libp2p/websockets": "^6.0.1", diff --git a/examples/libp2p-in-the-browser/package.json b/examples/libp2p-in-the-browser/package.json index a47e6c3e30..284d4a6014 100644 --- a/examples/libp2p-in-the-browser/package.json +++ b/examples/libp2p-in-the-browser/package.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "@chainsafe/libp2p-noise": "^11.0.0", - "@libp2p/bootstrap": "^7.0.0", + "@libp2p/bootstrap": "^8.0.0", "@libp2p/mplex": "^8.0.1", "@libp2p/webrtc-star": "^7.0.0", "@libp2p/websockets": "^6.0.1", diff --git a/examples/webrtc-direct/package.json b/examples/webrtc-direct/package.json index f5f2c8eaf0..04f541ebd0 100644 --- a/examples/webrtc-direct/package.json +++ b/examples/webrtc-direct/package.json @@ -11,7 +11,7 @@ "dependencies": { "@libp2p/webrtc-direct": "^6.0.0", "@chainsafe/libp2p-noise": "^11.0.0", - "@libp2p/bootstrap": "^7.0.0", + "@libp2p/bootstrap": "^8.0.0", "@libp2p/mplex": "^8.0.1", "libp2p": "file:../../", "wrtc": "^0.4.7" diff --git a/package.json b/package.json index 0b85de1a3b..bc885ffc31 100644 --- a/package.json +++ b/package.json @@ -127,11 +127,10 @@ "@libp2p/interface-connection-gater": "^3.0.0", "@libp2p/interface-connection-manager": "^3.0.0", "@libp2p/interface-content-routing": "^2.1.0", - "@libp2p/interface-dht": "^2.0.1", "@libp2p/interface-keychain": "^2.0.4", "@libp2p/interface-libp2p": "^3.0.0", "@libp2p/interface-metrics": "^4.0.0", - "@libp2p/interface-peer-discovery": "^1.1.0", + "@libp2p/interface-peer-discovery": "^2.0.0", "@libp2p/interface-peer-id": "^2.0.1", "@libp2p/interface-peer-info": "^1.0.3", "@libp2p/interface-peer-routing": "^1.1.0", @@ -190,17 +189,17 @@ "@chainsafe/libp2p-gossipsub": "^7.0.0", "@chainsafe/libp2p-noise": "^11.0.0", "@chainsafe/libp2p-yamux": "^4.0.0", - "@libp2p/bootstrap": "^7.0.0", + "@libp2p/bootstrap": "^8.0.0", "@libp2p/daemon-client": "^6.0.2", "@libp2p/daemon-server": "^5.0.2", "@libp2p/floodsub": "^7.0.1", "@libp2p/interface-compliance-tests": "^3.0.6", "@libp2p/interface-connection-compliance-tests": "^2.0.8", "@libp2p/interface-connection-encrypter-compliance-tests": "^5.0.0", - "@libp2p/interface-mocks": "^11.0.0", + "@libp2p/interface-mocks": "^12.0.0", "@libp2p/interop": "^8.0.0", - "@libp2p/kad-dht": "^9.1.0", - "@libp2p/mdns": "^7.0.0", + "@libp2p/kad-dht": "^9.1.4", + "@libp2p/mdns": "^8.0.0", "@libp2p/mplex": "^8.0.1", "@libp2p/pubsub": "^7.0.1", "@libp2p/tcp": "^7.0.1", diff --git a/test/content-routing/content-routing.node.ts b/test/content-routing/content-routing.node.ts index 79cf47b4a7..3ad77ec0ff 100644 --- a/test/content-routing/content-routing.node.ts +++ b/test/content-routing/content-routing.node.ts @@ -15,7 +15,7 @@ import type { PeerInfo } from '@libp2p/interface-peer-info' import type { ContentRouting } from '@libp2p/interface-content-routing' import { StubbedInstance, stubInterface } from 'sinon-ts' import { peerIdFromString } from '@libp2p/peer-id' -import type { DHT } from '@libp2p/interface-dht' +import type { KadDHT } from '@libp2p/kad-dht' describe('content-routing', () => { describe('no routers', () => { @@ -50,7 +50,7 @@ describe('content-routing', () => { describe('via dht router', () => { const number = 5 - let nodes: Array> + let nodes: Array> before(async () => { nodes = await Promise.all([ @@ -222,7 +222,7 @@ describe('content-routing', () => { }) describe('via dht and delegate routers', () => { - let node: Libp2p<{ dht: DHT }> + let node: Libp2p<{ dht: KadDHT }> let delegate: StubbedInstance beforeEach(async () => { diff --git a/test/content-routing/dht/operation.node.ts b/test/content-routing/dht/operation.node.ts index 3c87b86d74..776134eb2a 100644 --- a/test/content-routing/dht/operation.node.ts +++ b/test/content-routing/dht/operation.node.ts @@ -9,7 +9,7 @@ import { subsystemMulticodecs } from './utils.js' import { createPeerId } from '../../utils/creators/peer.js' import type { PeerId } from '@libp2p/interface-peer-id' import type { Libp2p } from '@libp2p/interface-libp2p' -import type { DualDHT } from '@libp2p/interface-dht' +import type { DualKadDHT } from '@libp2p/kad-dht' import { createLibp2p } from '../../../src/index.js' import { kadDHT } from '@libp2p/kad-dht' import { tcp } from '@libp2p/tcp' @@ -34,8 +34,8 @@ async function getRemoteAddr (remotePeerId: PeerId, libp2p: Libp2p): Promise { let peerId: PeerId let remotePeerId: PeerId - let libp2p: Libp2p<{ dht: DualDHT }> - let remoteLibp2p: Libp2p<{ dht: DualDHT }> + let libp2p: Libp2p<{ dht: DualKadDHT }> + let remoteLibp2p: Libp2p<{ dht: DualKadDHT }> let remAddr: Multiaddr beforeEach(async () => { @@ -62,7 +62,9 @@ describe('DHT subsystem operates correctly', () => { mplex() ], services: { - dht: kadDHT() + dht: kadDHT({ + allowQueryWithZeroPeers: true + }) } }) @@ -81,7 +83,9 @@ describe('DHT subsystem operates correctly', () => { mplex() ], services: { - dht: kadDHT() + dht: kadDHT({ + allowQueryWithZeroPeers: true + }) } }) diff --git a/test/content-routing/utils.ts b/test/content-routing/utils.ts index 6195ba4e3d..f58f1213cb 100644 --- a/test/content-routing/utils.ts +++ b/test/content-routing/utils.ts @@ -1,9 +1,9 @@ -import type { DHT } from '@libp2p/interface-dht' +import type { KadDHT } from '@libp2p/kad-dht' import { kadDHT } from '@libp2p/kad-dht' import type { Libp2pOptions } from '../../src/index.js' import { createBaseOptions } from '../utils/base-options.js' -export function createRoutingOptions (...overrides: Libp2pOptions[]): Libp2pOptions<{ dht: DHT }> { +export function createRoutingOptions (...overrides: Libp2pOptions[]): Libp2pOptions<{ dht: KadDHT }> { return createBaseOptions({ services: { dht: kadDHT() diff --git a/test/core/get-public-key.spec.ts b/test/core/get-public-key.spec.ts index 3ba63cb823..b496d9bc29 100644 --- a/test/core/get-public-key.spec.ts +++ b/test/core/get-public-key.spec.ts @@ -7,11 +7,11 @@ import { createPeerId } from '../utils/creators/peer.js' import { createLibp2pNode } from '../../src/libp2p.js' import sinon from 'sinon' import { kadDHT } from '@libp2p/kad-dht' -import type { DHT } from '@libp2p/interface-dht' +import type { KadDHT } from '@libp2p/kad-dht' import type { Libp2p } from '@libp2p/interface-libp2p' describe('getPublicKey', () => { - let libp2p: Libp2p<{ dht: DHT }> + let libp2p: Libp2p<{ dht: KadDHT }> beforeEach(async () => { const peerId = await createPeerId() diff --git a/test/peer-discovery/index.node.ts b/test/peer-discovery/index.node.ts index ce6fbcc402..6dd60e8c4b 100644 --- a/test/peer-discovery/index.node.ts +++ b/test/peer-discovery/index.node.ts @@ -16,15 +16,15 @@ import { createLibp2p } from '../../src/index.js' import { EventEmitter } from '@libp2p/interfaces/events' import type { Libp2pOptions } from '../../src/index.js' import type { Libp2p } from '@libp2p/interface-libp2p' -import type { DHT } from '@libp2p/interface-dht' +import type { KadDHT } from '@libp2p/kad-dht' import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery' -import { symbol } from '@libp2p/interface-peer-discovery' +import { peerDiscovery } from '@libp2p/interface-peer-discovery' const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0') class TestPeerDiscovery extends EventEmitter implements PeerDiscovery { - get [symbol] (): true { - return true + get [peerDiscovery] (): PeerDiscovery { + return this } get [Symbol.toStringTag] (): '@libp2p/test-peer-discovery' { @@ -174,7 +174,7 @@ describe('peer discovery scenarios', () => { it('kad-dht should discover other peers', async () => { const deferred = defer() - const getConfig = (peerId: PeerId): Libp2pOptions<{ dht: DHT }> => createBaseOptions({ + const getConfig = (peerId: PeerId): Libp2pOptions<{ dht: KadDHT }> => createBaseOptions({ peerId, addresses: { listen: [ diff --git a/test/peer-routing/peer-routing.node.ts b/test/peer-routing/peer-routing.node.ts index 11d707cda7..c26875c49a 100644 --- a/test/peer-routing/peer-routing.node.ts +++ b/test/peer-routing/peer-routing.node.ts @@ -14,7 +14,7 @@ import { createBaseOptions } from '../utils/base-options.js' import { createRoutingOptions } from './utils.js' import type { PeerId } from '@libp2p/interface-peer-id' import { createEd25519PeerId } from '@libp2p/peer-id-factory' -import { DHT, EventTypes, MessageType } from '@libp2p/interface-dht' +import { KadDHT, EventTypes, MessageType } from '@libp2p/kad-dht' import type { PeerInfo } from '@libp2p/interface-peer-info' import type { PeerRouting } from '@libp2p/interface-peer-routing' import { StubbedInstance, stubInterface } from 'sinon-ts' @@ -56,7 +56,7 @@ describe('peer-routing', () => { }) describe('via dht router', () => { - let nodes: Array> + let nodes: Array> before(async () => { nodes = await Promise.all([ @@ -312,7 +312,7 @@ describe('peer-routing', () => { }) describe('via dht and delegate routers', () => { - let node: Libp2p<{ dht: DHT }> + let node: Libp2p<{ dht: KadDHT }> let delegate: StubbedInstance beforeEach(async () => { @@ -552,7 +552,7 @@ describe('peer-routing', () => { }) describe('peer routing refresh manager service', () => { - let node: Libp2p<{ dht: DHT }> + let node: Libp2p<{ dht: KadDHT }> let peerIds: PeerId[] before(async () => { diff --git a/test/peer-routing/utils.ts b/test/peer-routing/utils.ts index 6195ba4e3d..f58f1213cb 100644 --- a/test/peer-routing/utils.ts +++ b/test/peer-routing/utils.ts @@ -1,9 +1,9 @@ -import type { DHT } from '@libp2p/interface-dht' +import type { KadDHT } from '@libp2p/kad-dht' import { kadDHT } from '@libp2p/kad-dht' import type { Libp2pOptions } from '../../src/index.js' import { createBaseOptions } from '../utils/base-options.js' -export function createRoutingOptions (...overrides: Libp2pOptions[]): Libp2pOptions<{ dht: DHT }> { +export function createRoutingOptions (...overrides: Libp2pOptions[]): Libp2pOptions<{ dht: KadDHT }> { return createBaseOptions({ services: { dht: kadDHT() From d1264d17d90d22c93f32d05c5ed679db2e3fd0ee Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 4 May 2023 19:46:01 +0100 Subject: [PATCH 5/5] chore: fix up examples --- examples/peer-and-content-routing/1.js | 6 +++++- examples/peer-and-content-routing/2.js | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/peer-and-content-routing/1.js b/examples/peer-and-content-routing/1.js index 252d910331..ae1f5e699d 100644 --- a/examples/peer-and-content-routing/1.js +++ b/examples/peer-and-content-routing/1.js @@ -17,7 +17,11 @@ const createNode = async () => { streamMuxers: [mplex()], connectionEncryption: [noise()], services: { - dht: kadDHT(), + dht: kadDHT({ + // this is necessary because this node is not connected to the public network + // it can be removed if, for example bootstrappers are configured + allowQueryWithZeroPeers: true + }), identify: identifyService() } }) diff --git a/examples/peer-and-content-routing/2.js b/examples/peer-and-content-routing/2.js index c7d67fad64..c335055ea9 100644 --- a/examples/peer-and-content-routing/2.js +++ b/examples/peer-and-content-routing/2.js @@ -19,7 +19,11 @@ const createNode = async () => { streamMuxers: [mplex()], connectionEncryption: [noise()], services: { - dht: kadDHT(), + dht: kadDHT({ + // this is necessary because this node is not connected to the public network + // it can be removed if, for example bootstrappers are configured + allowQueryWithZeroPeers: true + }), identify: identifyService() } })