Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 5 additions & 4 deletions packages/integration-tests/test/fixtures/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Circuit } from '@multiformats/multiaddr-matcher'
import { detect } from 'detect-browser'
import pWaitFor from 'p-wait-for'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import type { Libp2p, AbortOptions, ContentRouting, PeerId, PeerInfo } from '@libp2p/interface'
import type { Libp2p, AbortOptions, ContentRouting, PeerId, Provider } from '@libp2p/interface'
import type { AddressManager } from '@libp2p/interface-internal'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { CID, Version } from 'multiformats'
Expand Down Expand Up @@ -153,7 +153,7 @@ export interface MockContentRoutingComponents {
}

export class MockContentRouting implements ContentRouting {
static providers = new Map<string, PeerInfo[]>()
static providers = new Map<string, Provider[]>()
static data = new Map<string, Uint8Array>()

static reset (): void {
Expand All @@ -175,7 +175,8 @@ export class MockContentRouting implements ContentRouting {

providers.push({
id: this.peerId,
multiaddrs: this.addressManager.getAddresses()
multiaddrs: this.addressManager.getAddresses(),
routing: 'mock-content-routing'
})

MockContentRouting.providers.set(cid.toString(), providers)
Expand All @@ -185,7 +186,7 @@ export class MockContentRouting implements ContentRouting {

}

async * findProviders (cid: CID<unknown, number, number, Version>, options?: AbortOptions | undefined): AsyncGenerator<PeerInfo, void, undefined> {
async * findProviders (cid: CID<unknown, number, number, Version>, options?: AbortOptions | undefined): AsyncGenerator<Provider, void, undefined> {
yield * MockContentRouting.providers.get(cid.toString()) ?? []
}

Expand Down
9 changes: 8 additions & 1 deletion packages/interface/src/content-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import type { RoutingOptions } from './index.js'
import type { PeerInfo } from './peer-info.js'
import type { CID } from 'multiformats/cid'

export interface Provider extends PeerInfo {
/**
* Which routing subsystem found the provider
*/
routing: string
}

/**
* Any object that implements this Symbol as a property should return a
* Partial<ContentRouting> instance as the property value, similar to how
Expand Down Expand Up @@ -64,7 +71,7 @@ export interface ContentRouting {
* }
* ```
*/
findProviders(cid: CID, options?: RoutingOptions): AsyncIterable<PeerInfo>
findProviders(cid: CID, options?: RoutingOptions): AsyncIterable<Provider>

/**
* Puts a value corresponding to the passed key in a way that can later be
Expand Down
9 changes: 6 additions & 3 deletions packages/kad-dht/src/kad-dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
timeOperationGenerator
} from './utils.js'
import type { KadDHTComponents, KadDHTInit, Validators, Selectors, KadDHT as KadDHTInterface, QueryEvent, PeerInfoMapper, SetModeOptions } from './index.js'
import type { ContentRouting, CounterGroup, Logger, MetricGroup, PeerDiscovery, PeerDiscoveryEvents, PeerId, PeerInfo, PeerRouting, RoutingOptions, Startable } from '@libp2p/interface'
import type { ContentRouting, CounterGroup, Logger, MetricGroup, PeerDiscovery, PeerDiscoveryEvents, PeerId, PeerInfo, PeerRouting, Provider, RoutingOptions, Startable } from '@libp2p/interface'
import type { AbortOptions } from 'it-pushable'
import type { CID } from 'multiformats/cid'

Expand All @@ -46,10 +46,13 @@ class DHTContentRouting implements ContentRouting {
await this.dht.cancelReprovide(key)
}

async * findProviders (cid: CID, options: RoutingOptions = {}): AsyncGenerator<PeerInfo, void, undefined> {
async * findProviders (cid: CID, options: RoutingOptions = {}): AsyncGenerator<Provider, void, undefined> {
for await (const event of this.dht.findProviders(cid, options)) {
if (event.name === 'PROVIDER') {
yield * event.providers
yield * event.providers.map(peer => ({
...peer,
routing: 'kad-dht'
}))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/src/connection-manager/dial-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ export class DialQueue {
}

return true
} catch (err) {
this.log.trace('error calculating if multiaddr(s) were dialable', err)
} catch {

}

return false
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/src/content-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PeerSet } from '@libp2p/peer-collections'
import merge from 'it-merge'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { NoContentRoutersError } from './errors.js'
import type { AbortOptions, ComponentLogger, ContentRouting, Metrics, PeerInfo, PeerRouting, PeerStore, RoutingOptions, Startable } from '@libp2p/interface'
import type { AbortOptions, ComponentLogger, ContentRouting, Metrics, PeerRouting, PeerStore, Provider, RoutingOptions, Startable } from '@libp2p/interface'
import type { CID } from 'multiformats/cid'

export interface CompoundContentRoutingInit {
Expand Down Expand Up @@ -95,7 +95,7 @@ export class CompoundContentRouting implements ContentRouting, Startable {
/**
* Iterates over all content routers in parallel to find providers of the given key
*/
async * findProviders (key: CID, options: RoutingOptions = {}): AsyncGenerator<PeerInfo> {
async * findProviders (key: CID, options: RoutingOptions = {}): AsyncGenerator<Provider> {
if (this.routers.length === 0) {
throw new NoContentRoutersError('No content routers available')
}
Expand Down
34 changes: 22 additions & 12 deletions packages/libp2p/test/content-routing/content-routing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import sinon from 'sinon'
import { stubInterface } from 'sinon-ts'
import { createLibp2p } from '../../src/index.js'
import type { Libp2p } from '../../src/index.js'
import type { ContentRouting, PeerInfo } from '@libp2p/interface'
import type { ContentRouting, Provider } from '@libp2p/interface'
import type { StubbedInstance } from 'sinon-ts'

describe('content-routing', () => {
Expand Down Expand Up @@ -87,7 +87,8 @@ describe('content-routing', () => {
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
multiaddrs: [
multiaddr('/ip4/123.123.123.123/tcp/4001')
]
],
routing: 'test'
}
deferred.resolve()
})
Expand Down Expand Up @@ -136,7 +137,8 @@ describe('content-routing', () => {
delegate.findProviders.returns(async function * () {
yield {
id: node.peerId,
multiaddrs: []
multiaddrs: [],
routing: 'test'
}
deferred.resolve()
}())
Expand Down Expand Up @@ -173,7 +175,8 @@ describe('content-routing', () => {
id: peerIdFromString(provider),
multiaddrs: [
multiaddr('/ip4/0.0.0.0/tcp/0')
]
],
routing: 'test'
}
}())

Expand Down Expand Up @@ -224,11 +227,12 @@ describe('content-routing', () => {

it('should store the multiaddrs of a peer', async () => {
const providerPeerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
const result: PeerInfo = {
const result: Provider = {
id: providerPeerId,
multiaddrs: [
multiaddr('/ip4/123.123.123.123/tcp/49320')
]
],
routing: 'test'
}

router.findProviders.callsFake(async function * () {})
Expand All @@ -252,7 +256,8 @@ describe('content-routing', () => {
id: providerPeerId,
multiaddrs: [
multiaddr('/ip4/123.123.123.123/tcp/49320')
]
],
routing: 'test'
}

const defer = pDefer()
Expand All @@ -278,7 +283,8 @@ describe('content-routing', () => {
id: providerPeerId,
multiaddrs: [
multiaddr('/ip4/123.123.123.123/tcp/49320')
]
],
routing: 'test'
}

router.findProviders.callsFake(async function * () {
Expand All @@ -299,13 +305,15 @@ describe('content-routing', () => {
id: providerPeerId,
multiaddrs: [
multiaddr('/ip4/123.123.123.123/tcp/49320')
]
],
routing: 'test'
}
const result2 = {
id: providerPeerId,
multiaddrs: [
multiaddr('/ip4/213.213.213.213/tcp/2344')
]
],
routing: 'test'
}

router.findProviders.callsFake(async function * () {
Expand Down Expand Up @@ -352,7 +360,8 @@ describe('content-routing', () => {
id: providerPeerId,
multiaddrs: [
multiaddr('/ip4/123.123.123.123/tcp/2341')
]
],
routing: 'test'
}]

router.findProviders.callsFake(async function * () {
Expand All @@ -377,7 +386,8 @@ describe('content-routing', () => {
id: providerPeerId,
multiaddrs: [
multiaddr('/ip4/123.123.123.123/tcp/2341')
]
],
routing: 'test'
}]

router.findProviders.callsFake(async function * () {})
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function formatError (v: Error): string {
}

function isAggregateError (err?: any): err is AggregateError {
return err?.name === 'AggregateError'
return err instanceof AggregateError || (err?.name === 'AggregateError' && Array.isArray(err.errors))
}

// Add a formatter for stringifying Errors
Expand Down
9 changes: 5 additions & 4 deletions packages/transport-circuit-relay-v2/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Circuit } from '@multiformats/multiaddr-matcher'
import pWaitFor from 'p-wait-for'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { RELAY_V2_HOP_CODEC } from '../../../packages/transport-circuit-relay-v2/src/constants.js'
import type { Libp2p, AbortOptions, ContentRouting, PeerId, PeerInfo } from '@libp2p/interface'
import type { Libp2p, AbortOptions, ContentRouting, PeerId, Provider } from '@libp2p/interface'
import type { AddressManager } from '@libp2p/interface-internal'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { CID, Version } from 'multiformats'
Expand Down Expand Up @@ -133,7 +133,7 @@ export interface MockContentRoutingComponents {
}

export class MockContentRouting implements ContentRouting {
static providers = new Map<string, PeerInfo[]>()
static providers = new Map<string, Provider[]>()
static data = new Map<string, Uint8Array>()

static reset (): void {
Expand All @@ -155,7 +155,8 @@ export class MockContentRouting implements ContentRouting {

providers.push({
id: this.peerId,
multiaddrs: this.addressManager.getAddresses()
multiaddrs: this.addressManager.getAddresses(),
routing: 'mock-content-routing'
})

MockContentRouting.providers.set(cid.toString(), providers)
Expand All @@ -165,7 +166,7 @@ export class MockContentRouting implements ContentRouting {

}

async * findProviders (cid: CID<unknown, number, number, Version>, options?: AbortOptions | undefined): AsyncGenerator<PeerInfo, void, undefined> {
async * findProviders (cid: CID<unknown, number, number, Version>, options?: AbortOptions | undefined): AsyncGenerator<Provider, void, undefined> {
yield * MockContentRouting.providers.get(cid.toString()) ?? []
}

Expand Down
2 changes: 1 addition & 1 deletion packages/transport-webrtc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"it-stream-types": "^2.0.2",
"main-event": "^1.0.1",
"multiformats": "^13.4.0",
"node-datachannel": "^0.29.0",
"node-datachannel": "^0.30.0",
"p-defer": "^4.0.1",
"p-event": "^7.0.0",
"p-timeout": "^7.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export async function initiateConnection ({ rtcConfiguration, dataChannel, signa
})

const muxerFactory = new DataChannelMuxerFactory({
// @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
peerConnection,
dataChannelOptions: dataChannel
})
Expand Down Expand Up @@ -209,7 +208,6 @@ export async function initiateConnection ({ rtcConfiguration, dataChannel, signa

return {
remoteAddress: ma,
// @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
peerConnection,
muxerFactory
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
}
})
const muxerFactory = new DataChannelMuxerFactory({
// @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
peerConnection,
dataChannelOptions: this.init.dataChannel
})
Expand All @@ -229,7 +228,6 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
})

const webRTCConn = toMultiaddrConnection({
// @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
peerConnection,
remoteAddr: remoteAddress,
metrics: this.metrics?.listenerEvents,
Expand All @@ -246,7 +244,6 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
})

// close the connection on shut down
// @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
this._closeOnShutdown(peerConnection, webRTCConn)
} catch (err: any) {
this.log.error('incoming signaling error - %e', err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export async function connect (peerConnection: RTCPeerConnection | DirectRTCPeer
// Creating the connection before completion of the noise
// handshake ensures that the stream opening callback is set up
const maConn = toMultiaddrConnection({
// @ts-expect-error types are broken
peerConnection,
remoteAddr: options.remoteAddr,
metrics: options.events,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ export async function createDialerRTCPeerConnection (role: 'client' | 'server',
})

const muxerFactory = new DataChannelMuxerFactory({
// @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
peerConnection,
metrics: options.events,
dataChannelOptions: options.dataChannel
Expand Down
1 change: 0 additions & 1 deletion packages/transport-webrtc/test/maconn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('Multiaddr Connection', () => {
reset: () => {}
})
const maConn = toMultiaddrConnection({
// @ts-expect-error https://github.com/murat-dogan/node-datachannel/pull/370
peerConnection,
remoteAddr,
metrics,
Expand Down
Loading