Skip to content

Commit 5ee3283

Browse files
authored
feat: emit standardized routing events (#981)
Emit events with routing information for find providers operation.
1 parent 080e4f9 commit 5ee3283

11 files changed

Lines changed: 256 additions & 26 deletions

File tree

packages/bitswap/src/network.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type { WantOptions } from './bitswap.ts'
1515
import type { MultihashHasherLoader } from './index.ts'
1616
import type { Block } from './pb/message.ts'
1717
import type { QueuedBitswapMessage } from './utils/bitswap-message.ts'
18-
import type { Provider, Routing } from '@helia/interface/routing'
18+
import type { Provider, Routing, RoutingFindProvidersProgressEvents } from '@helia/interface/routing'
1919
import type { Libp2p, AbortOptions, Connection, PeerId, Topology, ComponentLogger, IdentifyResult, Counter, Metrics, Stream } from '@libp2p/interface'
2020
import type { Logger } from '@libp2p/logger'
2121
import type { PeerQueueJobOptions } from '@libp2p/utils'
@@ -54,7 +54,8 @@ export type BitswapNetworkWantProgressEvents =
5454
ProgressEvent<'bitswap:send-wantlist:error', { peer: PeerId, error: Error }> |
5555
ProgressEvent<'bitswap:find-providers', CID> |
5656
ProgressEvent<'bitswap:found-provider', BitswapProvider> |
57-
BitswapNetworkProgressEvents
57+
BitswapNetworkProgressEvents |
58+
RoutingFindProvidersProgressEvents
5859

5960
export type BitswapNetworkNotifyProgressEvents =
6061
BitswapNetworkProgressEvents |

packages/block-brokers/src/trustless-gateway/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TrustlessGatewayBlockBroker } from './broker.ts'
22
import type { TransformRequestInit } from './trustless-gateway.ts'
3-
import type { Routing, BlockBroker } from '@helia/interface'
3+
import type { Routing, BlockBroker, RoutingFindProvidersProgressEvents } from '@helia/interface'
44
import type { ComponentLogger } from '@libp2p/interface'
55
import type { CID } from 'multiformats'
66
import type { ProgressEvent } from 'progress-events'
@@ -38,7 +38,8 @@ export interface TrustlessGatewayProvider {
3838

3939
export type TrustlessGatewayGetBlockProgressEvents =
4040
ProgressEvent<'trustless-gateway:get-block:fetch', URL> |
41-
ProgressEvent<'trustless-gateway:found-provider', TrustlessGatewayProvider>
41+
ProgressEvent<'trustless-gateway:found-provider', TrustlessGatewayProvider> |
42+
RoutingFindProvidersProgressEvents
4243

4344
export interface TrustlessGatewayBlockBrokerInit {
4445
/**

packages/interface/src/blocks.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { RoutingFindProvidersProgressEvents } from './routing.ts'
12
import type { PeerId } from '@libp2p/interface'
23
import type { Multiaddr } from '@multiformats/multiaddr'
34
import type { Blockstore } from 'interface-blockstore'
@@ -37,7 +38,8 @@ export type PutManyBlocksProgressEvents =
3738
export type GetBlockProgressEvents =
3839
ProgressEvent<'blocks:get:providers:want', CID> |
3940
ProgressEvent<'blocks:get:blockstore:get', CID> |
40-
ProgressEvent<'blocks:get:blockstore:put', CID>
41+
ProgressEvent<'blocks:get:blockstore:put', CID> |
42+
RoutingFindProvidersProgressEvents
4143

4244
export type GetManyBlocksProgressEvents =
4345
ProgressEvent<'blocks:get-many:blockstore:get-many'> |

packages/interface/src/routing.ts

Lines changed: 141 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { AbortOptions, PeerId, PeerInfo, TraceOptions } from '@libp2p/interface'
22
import type { CID } from 'multiformats/cid'
3-
import type { ProgressOptions } from 'progress-events'
3+
import type { ProgressEvent, ProgressOptions } from 'progress-events'
44

55
/**
66
* When a routing operation involves reading values, these options allow
77
* controlling where the values are read from. Some implementations support a
88
* local cache that may be used in preference over network calls, for example
99
* when a record has a TTL.
1010
*/
11-
export interface RoutingOptions extends AbortOptions, ProgressOptions, TraceOptions {
11+
export interface RoutingOptions<Event extends ProgressEvent = any> extends AbortOptions, ProgressOptions<Event>, TraceOptions {
1212
/**
1313
* Pass `false` to not use the network
1414
*
@@ -55,7 +55,140 @@ export interface Provider extends PeerInfo {
5555
routing: string
5656
}
5757

58+
export interface RoutingFindProvidersStartEvent {
59+
routing: string
60+
cid: CID
61+
}
62+
63+
export interface RoutingFindProvidersEndEvent {
64+
routing: string
65+
cid: CID
66+
found: number
67+
}
68+
69+
export interface RoutingFindProvidersHttpGatewayProvider {
70+
routing: 'http-gateway-router'
71+
cid: CID
72+
provider: PeerInfo & {
73+
protocols: ['transport-ipfs-gateway-http']
74+
}
75+
}
76+
77+
export interface RoutingFindProvidersDelegatedHttpRoutingProvider {
78+
routing: 'delegated-http-router'
79+
cid: CID
80+
provider: PeerInfo & {
81+
routing: 'delegated-http-routing'
82+
protocols: string[]
83+
}
84+
}
85+
86+
export interface RoutingFindProvidersLibp2pProvider {
87+
routing: 'libp2p-router'
88+
cid: CID
89+
provider: PeerInfo
90+
}
91+
92+
export type RoutingFindProvidersProviderEvent = RoutingFindProvidersHttpGatewayProvider | RoutingFindProvidersDelegatedHttpRoutingProvider | RoutingFindProvidersLibp2pProvider
93+
94+
export type RoutingFindProvidersProgressEvents =
95+
ProgressEvent<'helia:routing:find-providers:start', RoutingFindProvidersStartEvent> |
96+
ProgressEvent<'helia:routing:find-providers:provider', RoutingFindProvidersProviderEvent> |
97+
ProgressEvent<'helia:routing:find-providers:end', RoutingFindProvidersEndEvent> |
98+
RoutingFindPeerProgressEvents
99+
100+
export interface RoutingProvideStartEvent {
101+
routing: string
102+
cid: CID
103+
}
104+
105+
export interface RoutingProvideEndEvent {
106+
routing: string
107+
cid: CID
108+
}
109+
110+
export type RoutingProvideProgressEvents =
111+
ProgressEvent<'helia:routing:provide:start', RoutingProvideStartEvent> |
112+
ProgressEvent<'helia:routing:provide:end', RoutingProvideEndEvent>
113+
114+
export interface RoutingCancelReprovideStartEvent {
115+
routing: string
116+
cid: CID
117+
}
118+
119+
export interface RoutingCancelReprovideEndEvent {
120+
routing: string
121+
cid: CID
122+
}
123+
124+
export type RoutingCancelReprovideProgressEvents =
125+
ProgressEvent<'helia:routing:cancel-reprovide:start', RoutingCancelReprovideStartEvent> |
126+
ProgressEvent<'helia:routing:cancel-reprovide:end', RoutingCancelReprovideEndEvent>
127+
128+
export interface RoutingPutStartEvent {
129+
routing: string
130+
key: Uint8Array
131+
value: Uint8Array
132+
}
133+
134+
export interface RoutingPutEndEvent {
135+
routing: string
136+
key: Uint8Array
137+
value: Uint8Array
138+
}
139+
140+
export type RoutingPutProgressEvents =
141+
ProgressEvent<'helia:routing:put:start', RoutingPutStartEvent> |
142+
ProgressEvent<'helia:routing:put:end', RoutingPutEndEvent>
143+
144+
export interface RoutingGetStartEvent {
145+
routing: string
146+
key: Uint8Array
147+
}
148+
149+
export interface RoutingGetEndEvent {
150+
routing: string
151+
key: Uint8Array
152+
}
153+
154+
export type RoutingGetProgressEvents =
155+
ProgressEvent<'helia:routing:get:start', RoutingGetStartEvent> |
156+
ProgressEvent<'helia:routing:get:end', RoutingGetEndEvent>
157+
158+
export interface RoutingFindPeerStartEvent {
159+
routing: string
160+
peerId: PeerId
161+
}
162+
163+
export interface RoutingFindPeerEndEvent {
164+
routing: string
165+
peerId: PeerId
166+
}
167+
168+
export type RoutingFindPeerProgressEvents =
169+
ProgressEvent<'helia:routing:find-peer:start', RoutingFindPeerStartEvent> |
170+
ProgressEvent<'helia:routing:find-peer:end', RoutingFindPeerEndEvent>
171+
172+
export interface RoutingGetClosestPeersStartEvent {
173+
routing: string
174+
key: Uint8Array
175+
}
176+
177+
export interface RoutingGetClosestPeersEndEvent {
178+
routing: string
179+
key: Uint8Array
180+
}
181+
182+
export type RoutingGetClosestPeersProgressEvents =
183+
ProgressEvent<'helia:routing:get-closest-peers:start', RoutingGetClosestPeersStartEvent> |
184+
ProgressEvent<'helia:routing:get-closest-peers:end', RoutingGetClosestPeersEndEvent>
185+
58186
export interface Routing {
187+
/**
188+
* The name of this routing implementation
189+
*/
190+
name: string
191+
59192
/**
60193
* The implementation of this method should ensure that network peers know the
61194
* caller can provide content that corresponds to the passed CID.
@@ -67,7 +200,7 @@ export interface Routing {
67200
* await contentRouting.provide(cid)
68201
* ```
69202
*/
70-
provide(cid: CID, options?: RoutingOptions): Promise<void>
203+
provide(cid: CID, options?: RoutingOptions<RoutingProvideProgressEvents>): Promise<void>
71204

72205
/**
73206
* Helia will periodically re-provide every previously provided CID. Use this
@@ -94,7 +227,7 @@ export interface Routing {
94227
* }
95228
* ```
96229
*/
97-
findProviders(cid: CID, options?: RoutingOptions): AsyncIterable<Provider>
230+
findProviders(cid: CID, options?: RoutingOptions<RoutingFindProvidersProgressEvents>): AsyncIterable<Provider>
98231

99232
/**
100233
* Puts a value corresponding to the passed key in a way that can later be
@@ -110,7 +243,7 @@ export interface Routing {
110243
* await contentRouting.put(key, value)
111244
* ```
112245
*/
113-
put(key: Uint8Array, value: Uint8Array, options?: RoutingOptions): Promise<void>
246+
put(key: Uint8Array, value: Uint8Array, options?: RoutingOptions<RoutingPutProgressEvents>): Promise<void>
114247

115248
/**
116249
* Retrieves a value from the network corresponding to the passed key.
@@ -124,7 +257,7 @@ export interface Routing {
124257
* const value = await contentRouting.get(key)
125258
* ```
126259
*/
127-
get(key: Uint8Array, options?: RoutingOptions): Promise<Uint8Array>
260+
get(key: Uint8Array, options?: RoutingOptions<RoutingGetProgressEvents>): Promise<Uint8Array>
128261

129262
/**
130263
* Searches the network for peer info corresponding to the passed peer id.
@@ -136,7 +269,7 @@ export interface Routing {
136269
* const peer = await peerRouting.findPeer(peerId, options)
137270
* ```
138271
*/
139-
findPeer(peerId: PeerId, options?: RoutingOptions): Promise<PeerInfo>
272+
findPeer(peerId: PeerId, options?: RoutingOptions<RoutingFindPeerProgressEvents>): Promise<PeerInfo>
140273

141274
/**
142275
* Search the network for peers that are closer to the passed key. Peer
@@ -151,5 +284,5 @@ export interface Routing {
151284
* }
152285
* ```
153286
*/
154-
getClosestPeers(key: Uint8Array, options?: RoutingOptions): AsyncIterable<PeerInfo>
287+
getClosestPeers(key: Uint8Array, options?: RoutingOptions<RoutingGetClosestPeersProgressEvents>): AsyncIterable<PeerInfo>
155288
}

packages/interop/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
},
5353
"dependencies": {
5454
"@helia/block-brokers": "^5.1.4",
55-
"@helia/dnslink": "^1.1.4",
5655
"@helia/car": "^5.3.10",
5756
"@helia/dag-cbor": "^5.0.7",
5857
"@helia/dag-json": "^5.0.7",
58+
"@helia/dnslink": "^1.1.4",
5959
"@helia/http": "^3.0.22",
6060
"@helia/interface": "^6.1.1",
6161
"@helia/ipns": "^9.1.9",

packages/mfs/test/touch.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ describe('touch', () => {
134134
await fs.cp(shardedDirCid, shardedDirPath)
135135

136136
await delay(2000)
137+
137138
await fs.touch(shardedDirPath, {
138139
recursive: true
139140
})

packages/routers/src/delegated-http-routing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function isIPNSKey (key: Uint8Array): boolean {
1919
}
2020

2121
class DelegatedHTTPRouter implements Routing {
22+
public readonly name = 'delegated-http-router'
2223
private readonly client: DelegatedRoutingV1HttpApiClient
2324

2425
constructor (components: DelegatedRoutingV1HttpApiClientComponents, init: DelegatedRoutingV1HttpApiClientInit) {

packages/routers/src/http-gateway-routing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function toUrl (info: PeerInfo): URL {
4545
}
4646

4747
class HTTPGatewayRouter implements Partial<Routing> {
48+
public readonly name = 'http-gateway-router'
4849
private readonly gateways: PeerInfo[]
4950
private readonly shuffle: boolean
5051

packages/routers/src/libp2p-routing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Libp2p, PeerId, PeerInfo } from '@libp2p/interface'
33
import type { CID } from 'multiformats'
44

55
class Libp2pRouter implements Routing {
6+
public readonly name = 'libp2p-router'
67
private readonly libp2p: Libp2p
78

89
constructor (libp2p: Libp2p) {

packages/utils/src/abstract-session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
406406
/**
407407
* This method should search for new providers and yield them.
408408
*/
409-
abstract findNewProviders (cid: CID, options: AbortOptions): AsyncGenerator<Provider>
409+
abstract findNewProviders (cid: CID, options: BlockRetrievalOptions<RetrieveBlockProgressEvents>): AsyncGenerator<Provider>
410410

411411
/**
412412
* The subclass should contact the provider and request the block from it.

0 commit comments

Comments
 (0)