2023-08-16 20:18:13 +05:30
|
|
|
import type { Libp2p } from "@libp2p/interface";
|
|
|
|
|
import type { PeerId } from "@libp2p/interface/peer-id";
|
|
|
|
|
import type { Peer, PeerStore } from "@libp2p/interface/peer-store";
|
2022-12-05 17:00:24 +11:00
|
|
|
|
2023-11-28 15:57:18 +05:30
|
|
|
import type { ShardInfo } from "./enr.js";
|
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";
|
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
|
|
|
}
|
|
|
|
|
|
2023-07-25 02:17:52 +02:00
|
|
|
export interface IBaseProtocol {
|
2023-01-25 16:36:30 +11:00
|
|
|
multicodec: string;
|
2022-12-05 17:00:24 +11:00
|
|
|
peerStore: PeerStore;
|
|
|
|
|
peers: () => Promise<Peer[]>;
|
2023-07-25 02:17:52 +02:00
|
|
|
addLibp2pEventListener: Libp2p["addEventListener"];
|
|
|
|
|
removeLibp2pEventListener: Libp2p["removeEventListener"];
|
2022-12-05 17:00:24 +11:00
|
|
|
}
|
|
|
|
|
|
2023-11-16 15:17:17 +03:00
|
|
|
export type ContentTopicInfo = {
|
|
|
|
|
clusterId: number;
|
|
|
|
|
contentTopics: string[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type ShardingParams = ShardInfo | ContentTopicInfo;
|
|
|
|
|
|
2023-02-02 08:02:06 +05:30
|
|
|
export type ProtocolCreateOptions = {
|
|
|
|
|
/**
|
2023-11-28 15:57:18 +05:30
|
|
|
* Waku supports usage of multiple pubsub topics. This is achieved through static sharding for now, and auto-sharding in the future.
|
|
|
|
|
* The format to specify a shard is:
|
|
|
|
|
* clusterId: number, shards: number[]
|
2023-09-27 15:28:07 +05:30
|
|
|
* To learn more about the sharding specifications implemented, see [Relay Sharding](https://rfc.vac.dev/spec/51/).
|
2023-11-14 21:22:52 +05:30
|
|
|
* The Pubsub Topic to use. Defaults to {@link @waku/core!DefaultPubsubTopic }.
|
2023-02-02 08:02:06 +05:30
|
|
|
*
|
2023-09-27 15:28:07 +05:30
|
|
|
* If no pubsub topic is specified, the default pubsub topic is used.
|
|
|
|
|
* The set of pubsub topics that are used to initialize the Waku node, will need to be used by the protocols as well
|
|
|
|
|
* You cannot currently add or remove pubsub topics after initialization.
|
|
|
|
|
* This is used by:
|
2023-02-02 08:02:06 +05:30
|
|
|
* - WakuRelay to receive, route and send messages,
|
|
|
|
|
* - WakuLightPush to send messages,
|
|
|
|
|
* - WakuStore to retrieve messages.
|
|
|
|
|
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2023-11-16 15:17:17 +03:00
|
|
|
shardInfo?: ShardingParams;
|
2023-02-09 13:15:23 +05:30
|
|
|
/**
|
2023-09-21 10:57:37 +02:00
|
|
|
* You can pass options to the `Libp2p` instance used by {@link @waku/core!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.
|
2023-09-21 10:57:37 +02:00
|
|
|
* Notes that some values are overridden by {@link @waku/core!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>;
|
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;
|
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
|
|
|
|
2023-05-17 23:40:52 +02:00
|
|
|
export enum SendError {
|
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",
|
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",
|
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.
|
|
|
|
|
*/
|
2023-09-21 11:32:34 +10:00
|
|
|
REMOTE_PEER_FAULT = "Remote peer fault",
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
REMOTE_PEER_REJECTED = "Remote peer rejected"
|
2023-05-17 23:40:52 +02:00
|
|
|
}
|
|
|
|
|
|
2022-12-05 17:00:24 +11:00
|
|
|
export interface SendResult {
|
2023-09-07 13:15:49 +05:30
|
|
|
errors?: SendError[];
|
2022-12-05 17:00:24 +11:00
|
|
|
recipients: PeerId[];
|
|
|
|
|
}
|