Skip to content

Commit 0f3ab9e

Browse files
authored
feat: add routing field to providers (#3340)
To better debug which routing system supplied a given provider, add a string field to the return type of `findProviders` which allows this information to be reported.
1 parent 7394509 commit 0f3ab9e

File tree

8 files changed

+51
-29
lines changed

8 files changed

+51
-29
lines changed

packages/integration-tests/test/fixtures/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Circuit } from '@multiformats/multiaddr-matcher'
55
import { detect } from 'detect-browser'
66
import pWaitFor from 'p-wait-for'
77
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
8-
import type { Libp2p, AbortOptions, ContentRouting, PeerId, PeerInfo } from '@libp2p/interface'
8+
import type { Libp2p, AbortOptions, ContentRouting, PeerId, Provider } from '@libp2p/interface'
99
import type { AddressManager } from '@libp2p/interface-internal'
1010
import type { Multiaddr } from '@multiformats/multiaddr'
1111
import type { CID, Version } from 'multiformats'
@@ -153,7 +153,7 @@ export interface MockContentRoutingComponents {
153153
}
154154

155155
export class MockContentRouting implements ContentRouting {
156-
static providers = new Map<string, PeerInfo[]>()
156+
static providers = new Map<string, Provider[]>()
157157
static data = new Map<string, Uint8Array>()
158158

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

176176
providers.push({
177177
id: this.peerId,
178-
multiaddrs: this.addressManager.getAddresses()
178+
multiaddrs: this.addressManager.getAddresses(),
179+
routing: 'mock-content-routing'
179180
})
180181

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

186187
}
187188

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

packages/interface/src/content-routing.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import type { RoutingOptions } from './index.js'
22
import type { PeerInfo } from './peer-info.js'
33
import type { CID } from 'multiformats/cid'
44

5+
export interface Provider extends PeerInfo {
6+
/**
7+
* Which routing subsystem found the provider
8+
*/
9+
routing: string
10+
}
11+
512
/**
613
* Any object that implements this Symbol as a property should return a
714
* Partial<ContentRouting> instance as the property value, similar to how
@@ -64,7 +71,7 @@ export interface ContentRouting {
6471
* }
6572
* ```
6673
*/
67-
findProviders(cid: CID, options?: RoutingOptions): AsyncIterable<PeerInfo>
74+
findProviders(cid: CID, options?: RoutingOptions): AsyncIterable<Provider>
6875

6976
/**
7077
* Puts a value corresponding to the passed key in a way that can later be

packages/kad-dht/src/kad-dht.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
timeOperationGenerator
2525
} from './utils.js'
2626
import type { KadDHTComponents, KadDHTInit, Validators, Selectors, KadDHT as KadDHTInterface, QueryEvent, PeerInfoMapper, SetModeOptions } from './index.js'
27-
import type { ContentRouting, CounterGroup, Logger, MetricGroup, PeerDiscovery, PeerDiscoveryEvents, PeerId, PeerInfo, PeerRouting, RoutingOptions, Startable } from '@libp2p/interface'
27+
import type { ContentRouting, CounterGroup, Logger, MetricGroup, PeerDiscovery, PeerDiscoveryEvents, PeerId, PeerInfo, PeerRouting, Provider, RoutingOptions, Startable } from '@libp2p/interface'
2828
import type { AbortOptions } from 'it-pushable'
2929
import type { CID } from 'multiformats/cid'
3030

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

49-
async * findProviders (cid: CID, options: RoutingOptions = {}): AsyncGenerator<PeerInfo, void, undefined> {
49+
async * findProviders (cid: CID, options: RoutingOptions = {}): AsyncGenerator<Provider, void, undefined> {
5050
for await (const event of this.dht.findProviders(cid, options)) {
5151
if (event.name === 'PROVIDER') {
52-
yield * event.providers
52+
yield * event.providers.map(peer => ({
53+
...peer,
54+
routing: 'kad-dht'
55+
}))
5356
}
5457
}
5558
}

packages/libp2p/src/connection-manager/dial-queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,8 @@ export class DialQueue {
515515
}
516516

517517
return true
518-
} catch (err) {
519-
this.log.trace('error calculating if multiaddr(s) were dialable', err)
518+
} catch {
519+
520520
}
521521

522522
return false

packages/libp2p/src/content-routing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { PeerSet } from '@libp2p/peer-collections'
33
import merge from 'it-merge'
44
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
55
import { NoContentRoutersError } from './errors.js'
6-
import type { AbortOptions, ComponentLogger, ContentRouting, Metrics, PeerInfo, PeerRouting, PeerStore, RoutingOptions, Startable } from '@libp2p/interface'
6+
import type { AbortOptions, ComponentLogger, ContentRouting, Metrics, PeerRouting, PeerStore, Provider, RoutingOptions, Startable } from '@libp2p/interface'
77
import type { CID } from 'multiformats/cid'
88

99
export interface CompoundContentRoutingInit {
@@ -95,7 +95,7 @@ export class CompoundContentRouting implements ContentRouting, Startable {
9595
/**
9696
* Iterates over all content routers in parallel to find providers of the given key
9797
*/
98-
async * findProviders (key: CID, options: RoutingOptions = {}): AsyncGenerator<PeerInfo> {
98+
async * findProviders (key: CID, options: RoutingOptions = {}): AsyncGenerator<Provider> {
9999
if (this.routers.length === 0) {
100100
throw new NoContentRoutersError('No content routers available')
101101
}

packages/libp2p/test/content-routing/content-routing.spec.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import sinon from 'sinon'
1313
import { stubInterface } from 'sinon-ts'
1414
import { createLibp2p } from '../../src/index.js'
1515
import type { Libp2p } from '../../src/index.js'
16-
import type { ContentRouting, PeerInfo } from '@libp2p/interface'
16+
import type { ContentRouting, Provider } from '@libp2p/interface'
1717
import type { StubbedInstance } from 'sinon-ts'
1818

1919
describe('content-routing', () => {
@@ -87,7 +87,8 @@ describe('content-routing', () => {
8787
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
8888
multiaddrs: [
8989
multiaddr('/ip4/123.123.123.123/tcp/4001')
90-
]
90+
],
91+
routing: 'test'
9192
}
9293
deferred.resolve()
9394
})
@@ -136,7 +137,8 @@ describe('content-routing', () => {
136137
delegate.findProviders.returns(async function * () {
137138
yield {
138139
id: node.peerId,
139-
multiaddrs: []
140+
multiaddrs: [],
141+
routing: 'test'
140142
}
141143
deferred.resolve()
142144
}())
@@ -173,7 +175,8 @@ describe('content-routing', () => {
173175
id: peerIdFromString(provider),
174176
multiaddrs: [
175177
multiaddr('/ip4/0.0.0.0/tcp/0')
176-
]
178+
],
179+
routing: 'test'
177180
}
178181
}())
179182

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

225228
it('should store the multiaddrs of a peer', async () => {
226229
const providerPeerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
227-
const result: PeerInfo = {
230+
const result: Provider = {
228231
id: providerPeerId,
229232
multiaddrs: [
230233
multiaddr('/ip4/123.123.123.123/tcp/49320')
231-
]
234+
],
235+
routing: 'test'
232236
}
233237

234238
router.findProviders.callsFake(async function * () {})
@@ -252,7 +256,8 @@ describe('content-routing', () => {
252256
id: providerPeerId,
253257
multiaddrs: [
254258
multiaddr('/ip4/123.123.123.123/tcp/49320')
255-
]
259+
],
260+
routing: 'test'
256261
}
257262

258263
const defer = pDefer()
@@ -278,7 +283,8 @@ describe('content-routing', () => {
278283
id: providerPeerId,
279284
multiaddrs: [
280285
multiaddr('/ip4/123.123.123.123/tcp/49320')
281-
]
286+
],
287+
routing: 'test'
282288
}
283289

284290
router.findProviders.callsFake(async function * () {
@@ -299,13 +305,15 @@ describe('content-routing', () => {
299305
id: providerPeerId,
300306
multiaddrs: [
301307
multiaddr('/ip4/123.123.123.123/tcp/49320')
302-
]
308+
],
309+
routing: 'test'
303310
}
304311
const result2 = {
305312
id: providerPeerId,
306313
multiaddrs: [
307314
multiaddr('/ip4/213.213.213.213/tcp/2344')
308-
]
315+
],
316+
routing: 'test'
309317
}
310318

311319
router.findProviders.callsFake(async function * () {
@@ -352,7 +360,8 @@ describe('content-routing', () => {
352360
id: providerPeerId,
353361
multiaddrs: [
354362
multiaddr('/ip4/123.123.123.123/tcp/2341')
355-
]
363+
],
364+
routing: 'test'
356365
}]
357366

358367
router.findProviders.callsFake(async function * () {
@@ -377,7 +386,8 @@ describe('content-routing', () => {
377386
id: providerPeerId,
378387
multiaddrs: [
379388
multiaddr('/ip4/123.123.123.123/tcp/2341')
380-
]
389+
],
390+
routing: 'test'
381391
}]
382392

383393
router.findProviders.callsFake(async function * () {})

packages/logger/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function formatError (v: Error): string {
107107
}
108108

109109
function isAggregateError (err?: any): err is AggregateError {
110-
return err?.name === 'AggregateError'
110+
return err instanceof AggregateError || (err?.name === 'AggregateError' && Array.isArray(err.errors))
111111
}
112112

113113
// Add a formatter for stringifying Errors

packages/transport-circuit-relay-v2/test/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Circuit } from '@multiformats/multiaddr-matcher'
44
import pWaitFor from 'p-wait-for'
55
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
66
import { RELAY_V2_HOP_CODEC } from '../../../packages/transport-circuit-relay-v2/src/constants.js'
7-
import type { Libp2p, AbortOptions, ContentRouting, PeerId, PeerInfo } from '@libp2p/interface'
7+
import type { Libp2p, AbortOptions, ContentRouting, PeerId, Provider } from '@libp2p/interface'
88
import type { AddressManager } from '@libp2p/interface-internal'
99
import type { Multiaddr } from '@multiformats/multiaddr'
1010
import type { CID, Version } from 'multiformats'
@@ -133,7 +133,7 @@ export interface MockContentRoutingComponents {
133133
}
134134

135135
export class MockContentRouting implements ContentRouting {
136-
static providers = new Map<string, PeerInfo[]>()
136+
static providers = new Map<string, Provider[]>()
137137
static data = new Map<string, Uint8Array>()
138138

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

156156
providers.push({
157157
id: this.peerId,
158-
multiaddrs: this.addressManager.getAddresses()
158+
multiaddrs: this.addressManager.getAddresses(),
159+
routing: 'mock-content-routing'
159160
})
160161

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

166167
}
167168

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

0 commit comments

Comments
 (0)