2024-01-15 16:12:01 -08:00
import type { PeerId } from "@libp2p/interface" ;
2022-12-05 17:00:24 +11:00
2025-01-31 00:16:00 +01:00
import type { ConnectionManagerOptions } from "./connection_manager.js" ;
2025-08-15 00:14:32 +02:00
import type { DiscoveryOptions , PeerCache } from "./discovery.js" ;
2025-01-31 00:16:00 +01:00
import type { FilterProtocolOptions } from "./filter.js" ;
2023-11-28 00:40:59 +01:00
import type { CreateLibp2pOptions } from "./libp2p.js" ;
2025-02-25 22:40:03 +01:00
import type { LightPushProtocolOptions } from "./light_push.js" ;
2023-02-24 23:22:04 +11:00
import type { IDecodedMessage } from "./message.js" ;
2025-02-25 22:40:03 +01:00
import type { ThisAndThat , ThisOrThat } from "./misc.js" ;
feat!: Introduce routing info concept
Concepts are being mixed up between the global network config (static vs auto sharding), that needs to be the same of all nodes in the network, individual node configuration (eg relay node subscribing to a given shard), and the routing characteristic of a specific message (eg pubsub topic, shard).
This stops proper configuration of nwaku post 0.36.0 because we know need to be deliberate on whether nwaku nodes are running with auto or static sharding.
It also included various back and forth conversions between shards, pubsub topics, etc.
With this change, we tidy up the network configuration, and make it explicit whether it is static or auto sharded.
We also introduce the concept of routing info, which is specific to a message, and tied to the overall network configuration.
Routing info abstract pubsub topic, shard, and autosharding needs. Which should lead to easier tidy up of the pubsub concept at a later stage.
# Conflicts:
# packages/core/src/lib/connection_manager/connection_manager.ts
# packages/core/src/lib/metadata/metadata.ts
# packages/interfaces/src/metadata.ts
# packages/interfaces/src/sharding.ts
# packages/relay/src/create.ts
# packages/sdk/src/filter/filter.ts
# packages/sdk/src/filter/types.ts
# packages/sdk/src/light_push/light_push.spec.ts
# packages/tests/tests/sharding/auto_sharding.spec.ts
# packages/tests/tests/sharding/static_sharding.spec.ts
# Conflicts:
# packages/sdk/src/store/store.ts
2025-07-11 13:33:45 +10:00
import { NetworkConfig } from "./sharding.js" ;
2025-01-31 00:16:00 +01:00
import type { StoreProtocolOptions } from "./store.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
}
2025-01-31 00:16:00 +01:00
export type CreateNodeOptions = {
2024-07-03 12:09:34 +05:30
/ * *
2025-01-31 00:16:00 +01:00
* Set the user agent string to be used in identification of the node .
2024-07-26 01:21:52 +03:00
*
2025-01-31 00:16:00 +01:00
* @default "js-waku"
2024-07-26 01:21:52 +03:00
* /
2025-01-31 00:16:00 +01:00
userAgent? : string ;
2025-02-27 14:00:28 +01:00
/ * *
* Starts Waku node automatically upon creations .
* Calls { @link @waku / sdk ! WakuNode . start } before returning { @link @waku / sdk ! WakuNode }
*
* @default true
* /
autoStart? : boolean ;
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 ;
2025-01-31 00:16:00 +01:00
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 > ;
2025-01-31 00:16:00 +01:00
2024-01-24 18:24:03 +05:30
/ * *
* Number of peers to connect to , for the usage of the protocol .
2025-02-25 22:40:03 +01:00
* This is used by Filter to retrieve messages .
2025-01-31 00:16:00 +01:00
*
* @default 2 .
2024-01-24 18:24:03 +05:30
* /
numPeersToUse? : number ;
2025-01-31 00:16:00 +01:00
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 ;
2025-01-31 00:16:00 +01:00
2023-02-09 13:15:23 +05:30
/ * *
* Use recommended bootstrap method to discovery and connect to new nodes .
* /
defaultBootstrap? : boolean ;
2025-01-31 00:16:00 +01:00
2025-05-30 16:27:00 +05:30
/ * *
* Enable or disable specific discovery methods .
*
2025-08-15 00:14:32 +02:00
* @default { peerExchange : true , dns : true , peerCache : true }
2025-05-30 16:27:00 +05:30
* /
2025-08-15 00:14:32 +02:00
discovery? : Partial < DiscoveryOptions > ;
/ * *
* Peer cache to use for storing and retrieving peer information .
* If present , enables peer cache discovery .
*
* @default browser ' s localStorage
* /
peerCache? : PeerCache ;
2025-05-30 16:27:00 +05:30
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 [ ] ;
2025-01-31 00:16:00 +01:00
/ * *
* Configuration for connection manager .
* If not specified - default values are applied .
* /
connectionManager? : Partial < ConnectionManagerOptions > ;
/ * *
* Configuration for Filter protocol .
* If not specified - default values are applied .
* /
filter? : Partial < FilterProtocolOptions > ;
2025-01-28 17:57:49 +05:30
/ * *
* Options for the Store protocol .
2025-02-25 22:40:03 +01:00
* If not specified - default values are applied .
2025-01-28 17:57:49 +05:30
* /
store? : Partial < StoreProtocolOptions > ;
2025-02-25 22:40:03 +01:00
/ * *
* Options for the LightPush protocol .
* If not specified - default values are applied .
* /
lightPush? : Partial < LightPushProtocolOptions > ;
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
2025-09-04 15:52:37 -07:00
export enum LightPushError {
2023-05-17 23:40:52 +02:00
GENERIC_FAIL = "Generic error" ,
DECODE_FAILED = "Failed to decode" ,
2023-09-20 15:56:47 +10:00
NO_PEER_AVAILABLE = "No peer available" ,
2024-05-14 16:31:38 +05:30
NO_STREAM_AVAILABLE = "No stream available" ,
2024-08-29 11:20:19 +02:00
NO_RESPONSE = "No response received" ,
2025-09-04 15:52:37 -07:00
STREAM_ABORTED = "Stream aborted" ,
2025-02-25 22:40:03 +01:00
ENCODE_FAILED = "Failed to encode" ,
EMPTY_PAYLOAD = "Payload is empty" ,
SIZE_TOO_BIG = "Size is too big" ,
TOPIC_NOT_CONFIGURED = "Topic not configured" ,
2025-09-04 15:52:37 -07:00
RLN_PROOF_GENERATION = "Proof generation failed" ,
REMOTE_PEER_REJECTED = "Remote peer rejected" ,
2025-02-25 22:40:03 +01:00
2025-09-04 15:52:37 -07:00
BAD_REQUEST = "Bad request format" ,
PAYLOAD_TOO_LARGE = "Message payload exceeds maximum size" ,
INVALID_MESSAGE = "Message validation failed" ,
UNSUPPORTED_TOPIC = "Unsupported pubsub topic" ,
TOO_MANY_REQUESTS = "Rate limit exceeded" ,
INTERNAL_ERROR = "Internal server error" ,
UNAVAILABLE = "Service temporarily unavailable" ,
NO_RLN_PROOF = "RLN proof generation failed" ,
NO_PEERS = "No relay peers available"
}
2025-02-25 22:40:03 +01:00
2025-09-04 15:52:37 -07:00
export enum FilterError {
// General errors
GENERIC_FAIL = "Generic error" ,
DECODE_FAILED = "Failed to decode" ,
NO_PEER_AVAILABLE = "No peer available" ,
NO_STREAM_AVAILABLE = "No stream available" ,
NO_RESPONSE = "No response received" ,
STREAM_ABORTED = "Stream aborted" ,
2025-02-25 22:40:03 +01:00
2025-09-04 15:52:37 -07:00
// Filter specific errors
REMOTE_PEER_REJECTED = "Remote peer rejected" ,
TOPIC_NOT_CONFIGURED = "Topic not configured" ,
SUBSCRIPTION_FAILED = "Subscription failed" ,
UNSUBSCRIBE_FAILED = "Unsubscribe failed" ,
PING_FAILED = "Ping failed" ,
2025-02-25 22:40:03 +01:00
TOPIC_DECODER_MISMATCH = "Topic decoder mismatch" ,
2025-09-04 15:52:37 -07:00
INVALID_DECODER_TOPICS = "Invalid decoder topics" ,
SUBSCRIPTION_LIMIT_EXCEEDED = "Subscription limit exceeded" ,
INVALID_CONTENT_TOPIC = "Invalid content topic" ,
PUSH_MESSAGE_FAILED = "Push message failed" ,
EMPTY_MESSAGE = "Empty message received" ,
MISSING_PUBSUB_TOPIC = "Pubsub topic missing from push message"
}
2025-02-25 22:40:03 +01:00
2025-09-04 15:52:37 -07:00
export interface LightPushFailure {
error : LightPushError ;
peerId? : PeerId ;
2023-05-17 23:40:52 +02:00
}
2025-09-04 15:52:37 -07:00
export interface FilterFailure {
error : FilterError ;
2024-03-11 18:50:34 +05:30
peerId? : PeerId ;
}
2025-09-04 15:52:37 -07:00
export type LightPushCoreResult = ThisOrThat <
2024-05-09 16:51:08 +05:30
"success" ,
PeerId ,
"failure" ,
2025-09-04 15:52:37 -07:00
LightPushFailure
2024-05-09 16:51:08 +05:30
> ;
2025-09-04 15:52:37 -07:00
export type FilterCoreResult = ThisOrThat <
"success" ,
PeerId ,
"failure" ,
FilterFailure
> ;
export type LightPushSDKResult = ThisAndThat <
"successes" ,
PeerId [ ] ,
"failures" ,
LightPushFailure [ ]
> ;
export type FilterSDKResult = ThisAndThat <
"successes" ,
PeerId [ ] ,
"failures" ,
FilterFailure [ ]
> ;
/ * *
* @deprecated replace usage by specific result types
* /
2024-05-09 16:51:08 +05:30
export type SDKProtocolResult = ThisAndThat <
"successes" ,
PeerId [ ] ,
"failures" ,
2025-09-04 15:52:37 -07:00
Array < {
error : ProtocolError ;
peerId? : PeerId ;
} >
2024-05-09 16:51:08 +05:30
> ;
2025-09-04 15:52:37 -07:00
/ * *
* @deprecated replace usage by specific result types
* /
export enum ProtocolError {
GENERIC_FAIL = "Generic error" ,
REMOTE_PEER_REJECTED = "Remote peer rejected" ,
DECODE_FAILED = "Failed to decode" ,
NO_PEER_AVAILABLE = "No peer available" ,
NO_STREAM_AVAILABLE = "No stream available" ,
NO_RESPONSE = "No response received" ,
ENCODE_FAILED = "Failed to encode" ,
EMPTY_PAYLOAD = "Payload is empty" ,
SIZE_TOO_BIG = "Size is too big" ,
TOPIC_NOT_CONFIGURED = "Topic not configured" ,
STREAM_ABORTED = "Stream aborted" ,
RLN_PROOF_GENERATION = "Proof generation failed" ,
TOPIC_DECODER_MISMATCH = "Topic decoder mismatch" ,
INVALID_DECODER_TOPICS = "Invalid decoder topics"
}