Skip to content

Commit 95d95da

Browse files
authored
fix: add provider events to bitswap and trustless gateways (#888)
Add progress events that describe when bitswap/trustless gateways have found providers for blocks.
1 parent eaa042e commit 95d95da

File tree

16 files changed

+170
-57
lines changed

16 files changed

+170
-57
lines changed

packages/bitswap/src/bitswap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class Bitswap implements BitswapInterface {
9696
signal
9797
})
9898

99-
options.onProgress?.(new CustomProgressEvent<{ cid: CID, sender: PeerId }>('bitswap:want-block:received', { cid, sender: result.sender }))
99+
options.onProgress?.(new CustomProgressEvent<{ cid: CID, sender: PeerId }>('bitswap:block', { cid, sender: result.sender }))
100100

101101
return result.block
102102
} finally {

packages/bitswap/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ export type BitswapNotifyProgressEvents =
2424
BitswapNetworkNotifyProgressEvents
2525

2626
export type BitswapWantBlockProgressEvents =
27-
ProgressEvent<'bitswap:want-block:unwant', CID> |
28-
ProgressEvent<'bitswap:want-block:block', CID> |
29-
ProgressEvent<'bitswap:want-block:received', { cid: CID, sender: PeerId }> |
27+
ProgressEvent<'bitswap:unwant', CID> |
28+
ProgressEvent<'bitswap:want', CID> |
29+
ProgressEvent<'bitswap:block', { cid: CID, sender: PeerId }> |
3030
BitswapNetworkWantProgressEvents
3131

3232
export type { BitswapNetworkNotifyProgressEvents }
3333
export type { BitswapNetworkWantProgressEvents }
3434
export type { BitswapNetworkProgressEvents }
3535
export type { WantType }
36+
export type { BitswapProvider } from './network.ts'
3637

3738
export interface WantListEntry {
3839
cid: CID

packages/bitswap/src/network.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,41 @@ import type { CID } from 'multiformats/cid'
2424
import type { ProgressEvent, ProgressOptions } from 'progress-events'
2525
import type { Uint8ArrayList } from 'uint8arraylist'
2626

27+
export interface BitswapProvider {
28+
/**
29+
* The type of provider
30+
*/
31+
type: 'bitswap'
32+
33+
/**
34+
* the CID the provider can supply the block for
35+
*/
36+
cid: CID
37+
38+
/**
39+
* The provider info
40+
*/
41+
provider: Provider
42+
43+
/**
44+
* Which routing subsystem found the provider
45+
*/
46+
routing: string
47+
}
48+
2749
export type BitswapNetworkProgressEvents =
28-
ProgressEvent<'bitswap:network:dial', PeerId | Multiaddr | Multiaddr[]>
50+
ProgressEvent<'bitswap:dial', PeerId | Multiaddr | Multiaddr[]>
2951

3052
export type BitswapNetworkWantProgressEvents =
31-
ProgressEvent<'bitswap:network:send-wantlist', PeerId> |
32-
ProgressEvent<'bitswap:network:send-wantlist:error', { peer: PeerId, error: Error }> |
33-
ProgressEvent<'bitswap:network:find-providers', CID> |
53+
ProgressEvent<'bitswap:send-wantlist', PeerId> |
54+
ProgressEvent<'bitswap:send-wantlist:error', { peer: PeerId, error: Error }> |
55+
ProgressEvent<'bitswap:find-providers', CID> |
56+
ProgressEvent<'bitswap:found-provider', BitswapProvider> |
3457
BitswapNetworkProgressEvents
3558

3659
export type BitswapNetworkNotifyProgressEvents =
3760
BitswapNetworkProgressEvents |
38-
ProgressEvent<'bitswap:network:send-block', PeerId>
61+
ProgressEvent<'bitswap:send-block', PeerId>
3962

4063
export interface NetworkInit {
4164
hashLoader?: MultihashHasherLoader
@@ -250,7 +273,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
250273
* Find bitswap providers for a given `cid`.
251274
*/
252275
async * findProviders (cid: CID, options?: AbortOptions & ProgressOptions<BitswapNetworkWantProgressEvents>): AsyncIterable<Provider> {
253-
options?.onProgress?.(new CustomProgressEvent<CID>('bitswap:network:find-providers', cid))
276+
options?.onProgress?.(new CustomProgressEvent<CID>('bitswap:find-providers', cid))
254277

255278
for await (const provider of this.routing.findProviders(cid, options)) {
256279
// make sure we can dial the provider
@@ -259,9 +282,17 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
259282
})
260283

261284
if (!dialable) {
285+
this.log('skipping peer %p as they are not dialable - %a[]', provider.id, provider.multiaddrs)
262286
continue
263287
}
264288

289+
options?.onProgress?.(new CustomProgressEvent('bitswap:found-provider', {
290+
type: 'bitswap',
291+
cid,
292+
provider,
293+
routing: provider.routing
294+
}))
295+
265296
yield provider
266297
}
267298
}

packages/bitswap/test/network.spec.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ describe('network', () => {
158158
id: peerId,
159159
multiaddrs: [
160160
multiaddr('/ip4/127.0.0.1/tcp/4001')
161-
]
161+
],
162+
routing: 'test-routing'
162163
}]
163164

164165
components.routing.findProviders.withArgs(cid).returns((async function * () {
@@ -181,7 +182,8 @@ describe('network', () => {
181182
id: peerId,
182183
multiaddrs: [
183184
multiaddr('/ip4/127.0.0.1/tcp/4001/p2p-circuit')
184-
]
185+
],
186+
routing: 'test-routing'
185187
}]
186188

187189
components.routing.findProviders.withArgs(cid).returns((async function * () {
@@ -216,7 +218,8 @@ describe('network', () => {
216218
id: peerId,
217219
multiaddrs: [
218220
multiaddr('/ip4/127.0.0.1/tcp/4001/p2p-circuit')
219-
]
221+
],
222+
routing: 'test-routing'
220223
}]
221224

222225
components.routing.findProviders.withArgs(cid).returns((async function * () {
@@ -239,7 +242,8 @@ describe('network', () => {
239242
id: peerId,
240243
multiaddrs: [
241244
multiaddr('/ip4/127.0.0.1/tcp/4001')
242-
]
245+
],
246+
routing: 'test-routing'
243247
}]
244248

245249
components.routing.findProviders.withArgs(cid).returns((async function * () {
@@ -298,7 +302,8 @@ describe('network', () => {
298302
id: peerId,
299303
multiaddrs: [
300304
multiaddr('/ip4/127.0.0.1/tcp/4001/p2p-circuit')
301-
]
305+
],
306+
routing: 'test-routing'
302307
}]
303308

304309
components.routing.findProviders.withArgs(cid).returns((async function * () {

packages/bitswap/test/session.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ describe('session', () => {
5252
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
5353
multiaddrs: [
5454
multiaddr(`/ip4/4${i}.4${i}.4${i}.4${i}/tcp/${1234 + i}`)
55-
]
55+
],
56+
routing: 'test-routing'
5657
}
5758
})
5859
)
@@ -125,7 +126,8 @@ describe('session', () => {
125126
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
126127
multiaddrs: [
127128
multiaddr('/ip4/41.41.41.41/tcp/1234')
128-
]
129+
],
130+
routing: 'test-routing'
129131
}]
130132

131133
components.network.findProviders.withArgs(cid).returns((async function * () {
@@ -150,12 +152,14 @@ describe('session', () => {
150152
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
151153
multiaddrs: [
152154
multiaddr('/ip4/41.41.41.41/tcp/1234')
153-
]
155+
],
156+
routing: 'test-routing'
154157
}, {
155158
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
156159
multiaddrs: [
157160
multiaddr('/ip4/41.41.41.41/tcp/1235')
158-
]
161+
],
162+
routing: 'test-routing'
159163
}]
160164

161165
components.network.findProviders.withArgs(cid).returns((async function * () {

packages/block-brokers/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@
5050
export { bitswap } from './bitswap.js'
5151
export type { BitswapBlockBrokerInit, BitswapBlockBrokerComponents } from './bitswap.js'
5252
export { trustlessGateway } from './trustless-gateway/index.js'
53-
export type { TrustlessGatewayBlockBrokerInit, TrustlessGatewayBlockBrokerComponents, TrustlessGatewayGetBlockProgressEvents } from './trustless-gateway/index.js'
53+
export type { TrustlessGatewayBlockBrokerInit, TrustlessGatewayBlockBrokerComponents, TrustlessGatewayGetBlockProgressEvents, TrustlessGatewayProvider } from './trustless-gateway/index.js'
54+
export type { BitswapProvider } from '@helia/bitswap'

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TrustlessGatewayBlockBroker } from './broker.js'
22
import type { TransformRequestInit } from './trustless-gateway.js'
33
import type { Routing, BlockBroker } from '@helia/interface'
44
import type { ComponentLogger } from '@libp2p/interface'
5+
import type { CID } from 'multiformats'
56
import type { ProgressEvent } from 'progress-events'
67

78
export const DEFAULT_ALLOW_INSECURE = false
@@ -13,8 +14,31 @@ export const DEFAULT_ALLOW_LOCAL = false
1314
*/
1415
export const DEFAULT_MAX_SIZE = 2_097_152
1516

17+
export interface TrustlessGatewayProvider {
18+
/**
19+
* The type of provider
20+
*/
21+
type: 'trustless-gateway'
22+
23+
/**
24+
* The CID that the provider can provide the block for
25+
*/
26+
cid: CID
27+
28+
/**
29+
* The provider's URL
30+
*/
31+
url: string
32+
33+
/**
34+
* Which routing implementation found the provider
35+
*/
36+
routing: string
37+
}
38+
1639
export type TrustlessGatewayGetBlockProgressEvents =
17-
ProgressEvent<'trustless-gateway:get-block:fetch', URL>
40+
ProgressEvent<'trustless-gateway:get-block:fetch', URL> |
41+
ProgressEvent<'trustless-gateway:found-provider', TrustlessGatewayProvider>
1842

1943
export interface TrustlessGatewayBlockBrokerInit {
2044
/**

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { getNetConfig, isPrivate } from '@libp2p/utils'
22
import { DNS, HTTP, HTTPS } from '@multiformats/multiaddr-matcher'
33
import { multiaddrToUri } from '@multiformats/multiaddr-to-uri'
4+
import { CustomProgressEvent } from 'progress-events'
45
import { Uint8ArrayList } from 'uint8arraylist'
56
import { TrustlessGateway } from './trustless-gateway.js'
7+
import type { TrustlessGatewayGetBlockProgressEvents, TrustlessGatewayProvider } from './index.ts'
68
import type { TransformRequestInit } from './trustless-gateway.js'
79
import type { Routing } from '@helia/interface'
810
import type { ComponentLogger, Logger, AbortOptions } from '@libp2p/interface'
911
import type { Multiaddr } from '@multiformats/multiaddr'
1012
import type { CID } from 'multiformats/cid'
13+
import type { ProgressOptions } from 'progress-events'
1114

1215
export function filterNonHTTPMultiaddrs (multiaddrs: Multiaddr[], allowInsecure: boolean, allowLocal: boolean): Multiaddr[] {
1316
return multiaddrs.filter(ma => {
@@ -36,7 +39,7 @@ export function filterNonHTTPMultiaddrs (multiaddrs: Multiaddr[], allowInsecure:
3639
})
3740
}
3841

39-
export interface FindHttpGatewayProvidersOptions extends AbortOptions {
42+
export interface FindHttpGatewayProvidersOptions extends AbortOptions, ProgressOptions<TrustlessGatewayGetBlockProgressEvents> {
4043
transformRequestInit?: TransformRequestInit
4144
}
4245

@@ -55,6 +58,15 @@ export async function * findHttpGatewayProviders (cid: CID, routing: Routing, lo
5558
// etc
5659
const uri = multiaddrToUri(httpAddresses[0])
5760

61+
const prov: TrustlessGatewayProvider = {
62+
type: 'trustless-gateway',
63+
cid,
64+
url: uri.toString(),
65+
routing: provider.routing
66+
}
67+
68+
options?.onProgress?.(new CustomProgressEvent('trustless-gateway:found-provider', prov))
69+
5870
yield new TrustlessGateway(uri, { logger, transformRequestInit: options.transformRequestInit })
5971
}
6072
}

packages/block-brokers/test/trustless-gateway-sessions.spec.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { StubbedInstance } from 'sinon-ts'
1818

1919
interface StubbedTrustlessGatewaySessionComponents {
2020
logger: ComponentLogger
21-
routing: StubbedInstance<Routing>
21+
routing: StubbedInstance<Required<Routing>>
2222
}
2323

2424
describe('trustless-gateway sessions', () => {
@@ -27,7 +27,7 @@ describe('trustless-gateway sessions', () => {
2727
beforeEach(async () => {
2828
components = {
2929
logger: defaultLogger(),
30-
routing: stubInterface<Routing>()
30+
routing: stubInterface()
3131
}
3232
})
3333

@@ -45,7 +45,8 @@ describe('trustless-gateway sessions', () => {
4545
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
4646
multiaddrs: [
4747
uriToMultiaddr(process.env.TRUSTLESS_GATEWAY ?? '')
48-
]
48+
],
49+
routing: 'test-routing'
4950
}
5051
}())
5152

@@ -66,19 +67,22 @@ describe('trustless-gateway sessions', () => {
6667
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
6768
multiaddrs: [
6869
multiaddr('/ip4/127.0.0.1/tcp/1234')
69-
]
70+
],
71+
routing: 'test-routing'
7072
}
7173
yield {
7274
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
7375
multiaddrs: [
7476
multiaddr('/ip4/127.0.0.1/udp/1234/quic-v1')
75-
]
77+
],
78+
routing: 'test-routing'
7679
}
7780
yield {
7881
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
7982
multiaddrs: [
8083
uriToMultiaddr(process.env.TRUSTLESS_GATEWAY ?? '')
81-
]
84+
],
85+
routing: 'test-routing'
8286
}
8387
}())
8488

@@ -100,7 +104,8 @@ describe('trustless-gateway sessions', () => {
100104
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
101105
multiaddrs: [
102106
uriToMultiaddr(process.env.TRUSTLESS_GATEWAY ?? '')
103-
]
107+
],
108+
routing: 'test-routing'
104109
}
105110

106111
components.routing.findProviders.returns(async function * () {
@@ -133,7 +138,8 @@ describe('trustless-gateway sessions', () => {
133138
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
134139
multiaddrs: [
135140
uriToMultiaddr(process.env.BAD_TRUSTLESS_GATEWAY ?? '')
136-
]
141+
],
142+
routing: 'test-routing'
137143
}
138144
}())
139145

@@ -153,13 +159,15 @@ describe('trustless-gateway sessions', () => {
153159
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
154160
multiaddrs: [
155161
uriToMultiaddr(process.env.BAD_TRUSTLESS_GATEWAY ?? '')
156-
]
162+
],
163+
routing: 'test-routing'
157164
},
158165
{
159166
id: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
160167
multiaddrs: [
161168
uriToMultiaddr(process.env.TRUSTLESS_GATEWAY ?? '')
162-
]
169+
],
170+
routing: 'test-routing'
163171
}]
164172

165173
components.routing.findProviders.returns(async function * () {

0 commit comments

Comments
 (0)