Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

feat: add content and peer routing progress events #355

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion packages/interface-content-routing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
"dependencies": {
"@libp2p/interface-peer-info": "^1.0.0",
"@libp2p/interfaces": "^3.0.0",
"multiformats": "^11.0.0"
"multiformats": "^11.0.0",
"progress-events": "^1.0.0"
},
"devDependencies": {
"aegir": "^38.1.0"
Expand Down
16 changes: 11 additions & 5 deletions packages/interface-content-routing/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import type { CID } from 'multiformats/cid'
import type { AbortOptions } from '@libp2p/interfaces'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import type { ProgressEvent, ProgressOptions } from 'progress-events'

export interface ContentRouting {
export interface ContentRouting<
ProvideProgressEvents extends ProgressEvent = ProgressEvent,
FindProvidersProgressEvents extends ProgressEvent = ProgressEvent,
PutProgressEvents extends ProgressEvent = ProgressEvent,
GetProgressEvents extends ProgressEvent = ProgressEvent
> {
/**
* The implementation of this method should ensure that network peers know the
* caller can provide content that corresponds to the passed CID.
Expand All @@ -14,7 +20,7 @@ export interface ContentRouting {
* await contentRouting.provide(cid)
* ```
*/
provide: (cid: CID, options?: AbortOptions) => Promise<void>
provide: (cid: CID, options?: AbortOptions & ProgressOptions<ProvideProgressEvents>) => Promise<void>

/**
* Find the providers of the passed CID.
Expand All @@ -28,7 +34,7 @@ export interface ContentRouting {
* }
* ```
*/
findProviders: (cid: CID, options?: AbortOptions) => AsyncIterable<PeerInfo>
findProviders: (cid: CID, options?: AbortOptions & ProgressOptions<FindProvidersProgressEvents>) => AsyncIterable<PeerInfo>

/**
* Puts a value corresponding to the passed key in a way that can later be
Expand All @@ -44,7 +50,7 @@ export interface ContentRouting {
* await contentRouting.put(key, value)
* ```
*/
put: (key: Uint8Array, value: Uint8Array, options?: AbortOptions) => Promise<void>
put: (key: Uint8Array, value: Uint8Array, options?: AbortOptions & ProgressOptions<PutProgressEvents>) => Promise<void>

/**
* Retrieves a value from the network corresponding to the passed key.
Expand All @@ -58,5 +64,5 @@ export interface ContentRouting {
* const value = await contentRouting.get(key)
* ```
*/
get: (key: Uint8Array, options?: AbortOptions) => Promise<Uint8Array>
get: (key: Uint8Array, options?: AbortOptions & ProgressOptions<GetProgressEvents>) => Promise<Uint8Array>
}
3 changes: 2 additions & 1 deletion packages/interface-libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@
"@libp2p/interface-pubsub": "^3.0.0",
"@libp2p/interface-registrar": "^2.0.0",
"@libp2p/interfaces": "^3.0.0",
"@multiformats/multiaddr": "^12.0.0"
"@multiformats/multiaddr": "^12.0.0",
"progress-events": "^1.0.0"
},
"devDependencies": {
"aegir": "^38.1.0"
Expand Down
14 changes: 11 additions & 3 deletions packages/interface-libp2p/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type { StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/inte
import type { Metrics } from '@libp2p/interface-metrics'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import type { KeyChain } from '@libp2p/interface-keychain'
import type { ProgressEvent } from 'progress-events'

/**
* Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.
Expand Down Expand Up @@ -87,7 +88,14 @@ export interface LookupFunction {
/**
* Libp2p nodes implement this interface.
*/
export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
export interface Libp2p<
FindPeerProgressEvents extends ProgressEvent = ProgressEvent,
GetClosestPeersProgressEvents extends ProgressEvent = ProgressEvent,
ProvideProgressEvents extends ProgressEvent = ProgressEvent,
FindProvidersProgressEvents extends ProgressEvent = ProgressEvent,
PutProgressEvents extends ProgressEvent = ProgressEvent,
GetProgressEvents extends ProgressEvent = ProgressEvent
> extends Startable, EventEmitter<Libp2pEvents> {
Comment on lines +91 to +98
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should add generics to the extends ProgressEvent in order to appropriately populate progressEvent or else users won't be able to customize their data since it's always going to be set to ProgressEvent<any, unknown>

e.g.

Suggested change
export interface Libp2p<
FindPeerProgressEvents extends ProgressEvent = ProgressEvent,
GetClosestPeersProgressEvents extends ProgressEvent = ProgressEvent,
ProvideProgressEvents extends ProgressEvent = ProgressEvent,
FindProvidersProgressEvents extends ProgressEvent = ProgressEvent,
PutProgressEvents extends ProgressEvent = ProgressEvent,
GetProgressEvents extends ProgressEvent = ProgressEvent
> extends Startable, EventEmitter<Libp2pEvents> {
export interface Libp2p<
FindPeerT extends string = any, FindPeerD = unknown,
GetClosestPeersT extends string = any, GetClosestPeersD = unknown,
ProvideT extends string = any, ProvideD = unknown,
FindProvidersT extends string = any, FindProvidersD = unknown,
PutT extends string = any, PutD = unknown,
GetT extends string = any, GetD = unknown,
> extends Startable, EventEmitter<Libp2pEvents> {

this can obviously be improved, but without piping the generics through, you will lock types to any and unknown, and aren't getting much benefit

/**
* The PeerId is a unique identifier for a node on the network.
*
Expand Down Expand Up @@ -138,7 +146,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
* }
* ```
*/
peerRouting: PeerRouting
peerRouting: PeerRouting<FindPeerProgressEvents, GetClosestPeersProgressEvents>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
peerRouting: PeerRouting<FindPeerProgressEvents, GetClosestPeersProgressEvents>
peerRouting: PeerRouting<ProgressEvent<FindPeerT, FindPeerD>, ProgressEvent<GetClosestPeersT, GetClosestPeersD>>


/**
* The content routing subsystem allows the user to find providers for content,
Expand All @@ -154,7 +162,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
* }
* ```
*/
contentRouting: ContentRouting
contentRouting: ContentRouting<ProvideProgressEvents, FindProvidersProgressEvents, PutProgressEvents, GetProgressEvents>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
contentRouting: ContentRouting<ProvideProgressEvents, FindProvidersProgressEvents, PutProgressEvents, GetProgressEvents>
contentRouting: ContentRouting<ProgressEvent<ProvideT, ProvideD>, ProgressEvent<FindProvidersT, FindProvidersD>, ProgressEvent<PutT, PutD>, ProgressEvent<GetT, GetD>>


/**
* The keychain contains the keys used by the current node, and can create new
Expand Down
3 changes: 2 additions & 1 deletion packages/interface-peer-routing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
"dependencies": {
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-peer-info": "^1.0.0",
"@libp2p/interfaces": "^3.0.0"
"@libp2p/interfaces": "^3.0.0",
"progress-events": "^1.0.0"
},
"devDependencies": {
"aegir": "^38.1.0"
Expand Down
10 changes: 7 additions & 3 deletions packages/interface-peer-routing/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { PeerId } from '@libp2p/interface-peer-id'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import type { AbortOptions } from '@libp2p/interfaces'
import type { ProgressEvent, ProgressOptions } from 'progress-events'

export interface PeerRouting {
export interface PeerRouting<
FindPeerProgressEvents extends ProgressEvent = ProgressEvent,
GetClosestPeersProgressEvents extends ProgressEvent = ProgressEvent,
> {
Comment on lines +6 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

/**
* Searches the network for peer info corresponding to the passed peer id.
*
Expand All @@ -13,7 +17,7 @@ export interface PeerRouting {
* const peer = await peerRouting.findPeer(peerId, options)
* ```
*/
findPeer: (peerId: PeerId, options?: AbortOptions) => Promise<PeerInfo>
findPeer: (peerId: PeerId, options?: AbortOptions & ProgressOptions<FindPeerProgressEvents>) => Promise<PeerInfo>

/**
* Search the network for peers that are closer to the passed key. Peer
Expand All @@ -28,5 +32,5 @@ export interface PeerRouting {
* }
* ```
*/
getClosestPeers: (key: Uint8Array, options?: AbortOptions) => AsyncIterable<PeerInfo>
getClosestPeers: (key: Uint8Array, options?: AbortOptions & ProgressOptions<GetClosestPeersProgressEvents>) => AsyncIterable<PeerInfo>
}