2023-08-16 20:18:13 +05:30
import type { Libp2p } from "@libp2p/interface" ;
2024-01-15 16:12:01 -08:00
import type { PeerId } from "@libp2p/interface" ;
import type { Peer , PeerStore } from "@libp2p/interface" ;
2022-12-05 17:00:24 +11:00
2023-11-28 00:40:59 +01:00
import type { CreateLibp2pOptions } from "./libp2p.js" ;
2023-02-24 23:22:04 +11:00
import type { IDecodedMessage } from "./message.js" ;
2024-08-13 05:23:20 +05:30
import { ThisAndThat , ThisOrThat } from "./misc.js" ;
import { AutoSharding , StaticSharding } from "./sharding.js" ;
2022-12-05 17:00:24 +11:00
export enum Protocols {
Relay = "relay" ,
Store = "store" ,
LightPush = "lightpush" ,
2023-08-16 20:18:13 +05:30
Filter = "filter"
2022-12-05 17:00:24 +11:00
}
2024-03-11 18:50:34 +05:30
export type IBaseProtocolCore = {
2023-01-25 16:36:30 +11:00
multicodec : string ;
2022-12-05 17:00:24 +11:00
peerStore : PeerStore ;
2024-01-19 20:42:52 +05:30
allPeers : ( ) = > Promise < Peer [ ] > ;
connectedPeers : ( ) = > Promise < Peer [ ] > ;
2023-07-25 02:17:52 +02:00
addLibp2pEventListener : Libp2p [ "addEventListener" ] ;
removeLibp2pEventListener : Libp2p [ "removeEventListener" ] ;
2024-03-11 18:50:34 +05:30
} ;
export type IBaseProtocolSDK = {
2024-06-19 01:52:16 -04:00
readonly connectedPeers : Peer [ ] ;
2024-07-27 18:27:54 +05:30
renewPeer : ( peerToDisconnect : PeerId ) = > Promise < Peer > ;
2024-06-19 01:52:16 -04:00
readonly numPeersToUse : number ;
2024-03-11 18:50:34 +05:30
} ;
2022-12-05 17:00:24 +11:00
2024-08-13 05:23:20 +05:30
export type NetworkConfig = StaticSharding | AutoSharding ;
2023-11-16 15:17:17 +03:00
2024-07-03 12:09:34 +05:30
//TODO: merge this with ProtocolCreateOptions or establish distinction: https://github.com/waku-org/js-waku/issues/2048
/ * *
* Options for using LightPush and Filter
* /
export type ProtocolUseOptions = {
/ * *
* Optional flag to enable auto - retry with exponential backoff
* /
autoRetry? : boolean ;
/ * *
* Optional flag to force using all available peers
* /
forceUseAllPeers? : boolean ;
/ * *
* Optional maximum number of attempts for exponential backoff
* /
maxAttempts? : number ;
/ * *
* Optional initial delay in milliseconds for exponential backoff
* /
initialDelay? : number ;
/ * *
* Optional maximum delay in milliseconds for exponential backoff
* /
maxDelay? : number ;
} ;
2023-02-02 08:02:06 +05:30
export type ProtocolCreateOptions = {
2024-01-09 23:34:30 -08:00
/ * *
2024-08-13 05:23:20 +05:30
* Configuration for determining the network in use .
2024-07-26 01:21:52 +03:00
*
2024-08-13 05:23:20 +05:30
* If using Static Sharding :
* Default value is configured for The Waku Network .
* The format to specify a shard is : clusterId : number , shards : number [ ]
* To learn more about the sharding specification , see [ Relay Sharding ] ( https : //rfc.vac.dev/spec/51/).
2024-07-26 01:21:52 +03:00
*
2024-08-13 05:23:20 +05:30
* If using Auto Sharding :
* See [ Waku v2 Topic Usage Recommendations ] ( https : //github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md#content-topics) for details.
* You cannot add or remove content topics after initialization of the node .
2024-07-26 01:21:52 +03:00
* /
/ * *
2024-08-13 05:23:20 +05:30
* Configuration for determining the network in use .
* Network configuration refers to the shards and clusters used in the network .
2024-07-26 01:21:52 +03:00
*
2024-08-13 05:23:20 +05:30
* If using Static Sharding :
* Cluster ID and shards are specified in the format : clusterId : number , shards : number [ ]
* The default value is configured for The Waku Network = > clusterId : 0 , shards : [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
2024-07-26 01:21:52 +03:00
* To learn more about the sharding specification , see [ Relay Sharding ] ( https : //rfc.vac.dev/spec/51/).
*
2024-08-13 05:23:20 +05:30
* If using Auto Sharding :
* Cluster ID and content topics are specified in the format : clusterId : number , contentTopics : string [ ]
* Content topics are used to determine the shards to be configured for the network .
* Cluster ID is optional , and defaults to The Waku Network ' s cluster ID = > 0
* To specify content topics , see [ Waku v2 Topic Usage Recommendations ] ( https : //github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md#content-topics) for details
*
* @default { clusterId : 1 , shards : [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ] }
2024-04-30 01:47:45 +02:00
* /
2024-08-13 05:23:20 +05:30
networkConfig? : NetworkConfig ;
2023-02-09 13:15:23 +05:30
/ * *
2024-02-08 12:09:10 -08:00
* You can pass options to the ` Libp2p ` instance used by { @link @waku / sdk ! WakuNode } using the ` libp2p ` property .
2023-02-09 13:15:23 +05:30
* This property is the same type as the one passed to [ ` Libp2p.create ` ] ( https : //github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
* apart that we made the ` modules ` property optional and partial ,
* allowing its omission and letting Waku set good defaults .
2024-02-08 12:09:10 -08:00
* Notes that some values are overridden by { @link @waku / sdk ! WakuNode } to ensure it implements the Waku protocol .
2023-02-09 13:15:23 +05:30
* /
2023-11-28 00:40:59 +01:00
libp2p? : Partial < CreateLibp2pOptions > ;
2024-01-24 18:24:03 +05:30
/ * *
* Number of peers to connect to , for the usage of the protocol .
* This is used by :
* - Light Push to send messages ,
* - Filter to retrieve messages .
2024-10-04 10:50:58 +02:00
* Defaults to 2 .
2024-01-24 18:24:03 +05:30
* /
numPeersToUse? : number ;
2023-02-09 13:15:23 +05:30
/ * *
* Byte array used as key for the noise protocol used for connection encryption
* by [ ` Libp2p.create ` ] ( https : //github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
* This is only used for test purposes to not run out of entropy during CI runs .
* /
staticNoiseKey? : Uint8Array ;
/ * *
* Use recommended bootstrap method to discovery and connect to new nodes .
* /
defaultBootstrap? : boolean ;
2024-03-04 10:56:20 +01:00
/ * *
* List of peers to use to bootstrap the node . Ignored if defaultBootstrap is set to true .
* /
bootstrapPeers? : string [ ] ;
2023-02-02 08:02:06 +05:30
} ;
2023-02-24 23:22:04 +11:00
export type Callback < T extends IDecodedMessage > = (
2023-08-16 20:18:13 +05:30
msg : T
2023-02-24 23:22:04 +11:00
) = > void | Promise < void > ;
2022-12-05 17:00:24 +11:00
2024-03-12 16:40:08 +05:30
export enum ProtocolError {
2023-09-20 16:18:22 +10:00
/** Could not determine the origin of the fault. Best to check connectivity and try again */
2023-05-17 23:40:52 +02:00
GENERIC_FAIL = "Generic error" ,
2023-10-17 09:14:45 +11:00
/ * *
* Failure to protobuf encode the message . This is not recoverable and needs
* further investigation .
* /
2023-05-17 23:40:52 +02:00
ENCODE_FAILED = "Failed to encode" ,
2023-10-17 09:14:45 +11:00
/ * *
* Failure to protobuf decode the message . May be due to a remote peer issue ,
* ensuring that messages are sent via several peer enable mitigation of this error .
* /
2023-05-17 23:40:52 +02:00
DECODE_FAILED = "Failed to decode" ,
2023-10-17 09:14:45 +11:00
/ * *
* The message payload is empty , making the message invalid . Ensure that a non - empty
* payload is set on the outgoing message .
* /
EMPTY_PAYLOAD = "Payload is empty" ,
/ * *
* The message size is above the maximum message size allowed on the Waku Network .
2023-09-20 16:18:22 +10:00
* Compressing the message or using an alternative strategy for large messages is recommended .
* /
2023-05-17 23:40:52 +02:00
SIZE_TOO_BIG = "Size is too big" ,
2023-09-27 15:28:07 +05:30
/ * *
2023-11-14 21:22:52 +05:30
* The PubsubTopic passed to the send function is not configured on the Waku node .
* Please ensure that the PubsubTopic is used when initializing the Waku node .
2023-09-27 15:28:07 +05:30
* /
TOPIC_NOT_CONFIGURED = "Topic not configured" ,
2024-05-09 16:51:08 +05:30
/ * *
* The pubsub topic configured on the decoder does not match the pubsub topic setup on the protocol .
* Ensure that the pubsub topic used for decoder creation is the same as the one used for protocol .
* /
TOPIC_DECODER_MISMATCH = "Topic decoder mismatch" ,
2024-08-05 15:52:58 +05:30
/ * *
* The topics passed in the decoders do not match each other , or don ' t exist at all .
* Ensure that all the pubsub topics used in the decoders are valid and match each other .
* /
INVALID_DECODER_TOPICS = "Invalid decoder topics" ,
2023-09-20 16:18:22 +10:00
/ * *
* Failure to find a peer with suitable protocols . This may due to a connection issue .
* Mitigation can be : retrying after a given time period , display connectivity issue
* to user or listening for ` peer:connected:bootstrap ` or ` peer:connected:peer-exchange `
* on the connection manager before retrying .
* /
2023-09-20 15:56:47 +10:00
NO_PEER_AVAILABLE = "No peer available" ,
2024-05-14 16:31:38 +05:30
/ * *
* Failure to find a stream to the peer . This may be because the connection with the peer is not still alive .
* Mitigation can be : retrying after a given time period , or mitigation for ` NO_PEER_AVAILABLE ` can be used .
* /
NO_STREAM_AVAILABLE = "No stream available" ,
2023-09-20 16:18:22 +10:00
/ * *
2023-09-21 11:32:34 +10:00
* The remote peer did not behave as expected . Mitigation for ` NO_PEER_AVAILABLE `
2023-09-20 16:18:22 +10:00
* or ` DECODE_FAILED ` can be used .
* /
2024-08-29 11:20:19 +02:00
NO_RESPONSE = "No response received" ,
2023-09-21 11:32:34 +10:00
/ * *
* The remote peer rejected the message . Information provided by the remote peer
* is logged . Review message validity , or mitigation for ` NO_PEER_AVAILABLE `
* or ` DECODE_FAILED ` can be used .
* /
2024-03-13 19:33:50 +05:30
REMOTE_PEER_REJECTED = "Remote peer rejected" ,
/ * *
* The protocol request timed out without a response . This may be due to a connection issue .
* Mitigation can be : retrying after a given time period
* /
2024-08-29 11:20:19 +02:00
REQUEST_TIMEOUT = "Request timeout" ,
/ * *
* Missing credentials info message .
* nwaku : https : //github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/group_manager/group_manager_base.nim#L186
* /
RLN_IDENTITY_MISSING = "Identity credentials are not set" ,
/ * *
* Membership index missing info message .
* nwaku : https : //github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/group_manager/group_manager_base.nim#L188
* /
RLN_MEMBERSHIP_INDEX = "Membership index is not set" ,
/ * *
* Message limit is missing .
* nwaku : https : //github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/group_manager/group_manager_base.nim#L190
* /
RLN_LIMIT_MISSING = "User message limit is not set" ,
/ * *
* General proof generation error message .
* nwaku : https : //github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/group_manager/group_manager_base.nim#L201C19-L201C42
* /
RLN_PROOF_GENERATION = "Proof generation failed"
2023-05-17 23:40:52 +02:00
}
2024-03-11 18:50:34 +05:30
export interface Failure {
2024-03-12 16:40:08 +05:30
error : ProtocolError ;
2024-03-11 18:50:34 +05:30
peerId? : PeerId ;
}
2024-05-09 16:51:08 +05:30
export type CoreProtocolResult = ThisOrThat <
"success" ,
PeerId ,
"failure" ,
Failure
> ;
export type SDKProtocolResult = ThisAndThat <
"successes" ,
PeerId [ ] ,
"failures" ,
Failure [ ]
> ;