2025-02-05 11:21:22 +01:00
|
|
|
import type { PeerId } from "@libp2p/interface";
|
2025-02-05 13:24:50 +01:00
|
|
|
import { ConnectionManager, LightPushCore } from "@waku/core";
|
2024-03-11 18:50:34 +05:30
|
|
|
import {
|
2024-10-17 00:49:24 +02:00
|
|
|
type CoreProtocolResult,
|
2024-03-11 18:50:34 +05:30
|
|
|
Failure,
|
|
|
|
|
type IEncoder,
|
2024-10-04 10:50:58 +02:00
|
|
|
ILightPush,
|
2024-03-11 18:50:34 +05:30
|
|
|
type IMessage,
|
2024-10-17 00:49:24 +02:00
|
|
|
type ISenderOptions,
|
2024-03-11 18:50:34 +05:30
|
|
|
type Libp2p,
|
2024-03-12 16:40:08 +05:30
|
|
|
ProtocolError,
|
2024-07-03 12:09:34 +05:30
|
|
|
SDKProtocolResult
|
2024-03-11 18:50:34 +05:30
|
|
|
} from "@waku/interfaces";
|
|
|
|
|
import { ensurePubsubTopicIsConfigured, Logger } from "@waku/utils";
|
|
|
|
|
|
2025-01-31 00:16:00 +01:00
|
|
|
import { PeerManager } from "../peer_manager/index.js";
|
2024-03-11 18:50:34 +05:30
|
|
|
|
|
|
|
|
const log = new Logger("sdk:light-push");
|
|
|
|
|
|
2024-10-17 00:49:24 +02:00
|
|
|
const DEFAULT_MAX_ATTEMPTS = 3;
|
|
|
|
|
const DEFAULT_SEND_OPTIONS: ISenderOptions = {
|
|
|
|
|
autoRetry: false,
|
|
|
|
|
maxAttempts: DEFAULT_MAX_ATTEMPTS
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-05 11:21:22 +01:00
|
|
|
type RetryCallback = (peerId: PeerId) => Promise<CoreProtocolResult>;
|
2024-03-11 18:50:34 +05:30
|
|
|
|
2024-10-17 00:49:24 +02:00
|
|
|
export class LightPush implements ILightPush {
|
|
|
|
|
public readonly protocol: LightPushCore;
|
2024-09-17 11:34:59 +05:30
|
|
|
|
2024-07-19 15:58:17 +05:30
|
|
|
public constructor(
|
2024-06-19 01:52:16 -04:00
|
|
|
connectionManager: ConnectionManager,
|
2025-01-31 00:16:00 +01:00
|
|
|
private peerManager: PeerManager,
|
|
|
|
|
libp2p: Libp2p
|
2024-06-19 01:52:16 -04:00
|
|
|
) {
|
2025-01-31 00:16:00 +01:00
|
|
|
this.protocol = new LightPushCore(connectionManager.pubsubTopics, libp2p);
|
2024-03-11 18:50:34 +05:30
|
|
|
}
|
|
|
|
|
|
2024-07-19 15:58:17 +05:30
|
|
|
public async send(
|
2024-06-19 01:52:16 -04:00
|
|
|
encoder: IEncoder,
|
|
|
|
|
message: IMessage,
|
2024-10-17 00:49:24 +02:00
|
|
|
options: ISenderOptions = DEFAULT_SEND_OPTIONS
|
2024-06-19 01:52:16 -04:00
|
|
|
): Promise<SDKProtocolResult> {
|
2024-03-11 18:50:34 +05:30
|
|
|
const successes: PeerId[] = [];
|
|
|
|
|
const failures: Failure[] = [];
|
|
|
|
|
|
|
|
|
|
const { pubsubTopic } = encoder;
|
|
|
|
|
try {
|
|
|
|
|
ensurePubsubTopicIsConfigured(pubsubTopic, this.protocol.pubsubTopics);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log.error("Failed to send waku light push: pubsub topic not configured");
|
|
|
|
|
return {
|
2024-10-04 10:50:58 +02:00
|
|
|
successes,
|
2024-03-11 18:50:34 +05:30
|
|
|
failures: [
|
|
|
|
|
{
|
2024-03-12 16:40:08 +05:30
|
|
|
error: ProtocolError.TOPIC_NOT_CONFIGURED
|
2024-03-11 18:50:34 +05:30
|
|
|
}
|
2024-10-04 10:50:58 +02:00
|
|
|
]
|
2024-03-11 18:50:34 +05:30
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 11:21:22 +01:00
|
|
|
const peerIds = await this.peerManager.getPeers();
|
|
|
|
|
if (peerIds.length === 0) {
|
2024-03-11 18:50:34 +05:30
|
|
|
return {
|
|
|
|
|
successes,
|
2024-06-19 01:52:16 -04:00
|
|
|
failures: [
|
|
|
|
|
{
|
|
|
|
|
error: ProtocolError.NO_PEER_AVAILABLE
|
|
|
|
|
}
|
|
|
|
|
]
|
2024-03-11 18:50:34 +05:30
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-04 10:50:58 +02:00
|
|
|
const results = await Promise.allSettled(
|
2025-02-05 11:21:22 +01:00
|
|
|
peerIds.map((id) => this.protocol.send(encoder, message, id))
|
2024-03-11 18:50:34 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for (const result of results) {
|
2024-10-04 10:50:58 +02:00
|
|
|
if (result.status !== "fulfilled") {
|
2024-08-29 11:20:19 +02:00
|
|
|
log.error("Failed unexpectedly while sending:", result.reason);
|
2024-03-12 16:40:08 +05:30
|
|
|
failures.push({ error: ProtocolError.GENERIC_FAIL });
|
2024-10-04 10:50:58 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { failure, success } = result.value;
|
|
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
|
successes.push(success);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (failure) {
|
|
|
|
|
failures.push(failure);
|
|
|
|
|
|
2024-10-17 00:49:24 +02:00
|
|
|
if (options?.autoRetry) {
|
|
|
|
|
void this.attemptRetries(
|
2025-02-05 11:21:22 +01:00
|
|
|
(id: PeerId) => this.protocol.send(encoder, message, id),
|
2024-10-17 00:49:24 +02:00
|
|
|
options.maxAttempts
|
2024-10-04 10:50:58 +02:00
|
|
|
);
|
|
|
|
|
}
|
2024-03-11 18:50:34 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
successes,
|
|
|
|
|
failures
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-10-04 10:50:58 +02:00
|
|
|
|
2024-10-17 00:49:24 +02:00
|
|
|
private async attemptRetries(
|
|
|
|
|
fn: RetryCallback,
|
|
|
|
|
maxAttempts?: number
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
maxAttempts = maxAttempts || DEFAULT_MAX_ATTEMPTS;
|
2025-02-05 11:21:22 +01:00
|
|
|
const peerIds = await this.peerManager.getPeers();
|
2024-10-17 00:49:24 +02:00
|
|
|
|
2025-02-05 11:21:22 +01:00
|
|
|
if (peerIds.length === 0) {
|
2024-10-17 00:49:24 +02:00
|
|
|
log.warn("Cannot retry with no connected peers.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < maxAttempts; i++) {
|
2025-02-05 11:21:22 +01:00
|
|
|
const id = peerIds[i % peerIds.length]; // always present as we checked for the length already
|
|
|
|
|
const response = await fn(id);
|
2024-10-17 00:49:24 +02:00
|
|
|
|
|
|
|
|
if (response.success) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.info(
|
2025-02-05 11:21:22 +01:00
|
|
|
`Attempted retry for peer:${id} failed with:${response?.failure?.error}`
|
2024-10-17 00:49:24 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-11 18:50:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function wakuLightPush(
|
2024-06-19 01:52:16 -04:00
|
|
|
connectionManager: ConnectionManager,
|
2025-01-31 00:16:00 +01:00
|
|
|
peerManager: PeerManager
|
2024-10-04 10:50:58 +02:00
|
|
|
): (libp2p: Libp2p) => ILightPush {
|
2025-01-31 00:16:00 +01:00
|
|
|
return (libp2p: Libp2p) =>
|
|
|
|
|
new LightPush(connectionManager, peerManager, libp2p);
|
2024-03-11 18:50:34 +05:30
|
|
|
}
|