2024-10-04 10:50:58 +02:00
|
|
|
import type { Peer, PeerId } from "@libp2p/interface";
|
|
|
|
|
import { ConnectionManager, LightPushCodec, LightPushCore } from "@waku/core";
|
2024-03-11 18:50:34 +05:30
|
|
|
import {
|
|
|
|
|
Failure,
|
|
|
|
|
type IEncoder,
|
2024-10-04 10:50:58 +02:00
|
|
|
ILightPush,
|
2024-03-11 18:50:34 +05:30
|
|
|
type IMessage,
|
|
|
|
|
type Libp2p,
|
|
|
|
|
type ProtocolCreateOptions,
|
2024-03-12 16:40:08 +05:30
|
|
|
ProtocolError,
|
2024-07-03 12:09:34 +05:30
|
|
|
ProtocolUseOptions,
|
|
|
|
|
SDKProtocolResult
|
2024-03-11 18:50:34 +05:30
|
|
|
} from "@waku/interfaces";
|
|
|
|
|
import { ensurePubsubTopicIsConfigured, Logger } from "@waku/utils";
|
|
|
|
|
|
2024-09-17 11:34:59 +05:30
|
|
|
import { ReliabilityMonitorManager } from "../../reliability_monitor/index.js";
|
|
|
|
|
import { SenderReliabilityMonitor } from "../../reliability_monitor/sender.js";
|
|
|
|
|
import { BaseProtocolSDK } from "../base_protocol.js";
|
2024-03-11 18:50:34 +05:30
|
|
|
|
|
|
|
|
const log = new Logger("sdk:light-push");
|
|
|
|
|
|
2024-10-04 10:50:58 +02:00
|
|
|
class LightPush extends BaseProtocolSDK implements ILightPush {
|
2024-03-11 18:50:34 +05:30
|
|
|
public readonly protocol: LightPushCore;
|
|
|
|
|
|
2024-09-17 11:34:59 +05:30
|
|
|
private readonly reliabilityMonitor: SenderReliabilityMonitor;
|
|
|
|
|
|
2024-07-19 15:58:17 +05:30
|
|
|
public constructor(
|
2024-06-19 01:52:16 -04:00
|
|
|
connectionManager: ConnectionManager,
|
2024-10-04 10:50:58 +02:00
|
|
|
private libp2p: Libp2p,
|
2024-06-19 01:52:16 -04:00
|
|
|
options?: ProtocolCreateOptions
|
|
|
|
|
) {
|
2024-08-13 05:23:20 +05:30
|
|
|
super(
|
|
|
|
|
new LightPushCore(connectionManager.configuredPubsubTopics, libp2p),
|
|
|
|
|
connectionManager,
|
|
|
|
|
{
|
|
|
|
|
numPeersToUse: options?.numPeersToUse
|
|
|
|
|
}
|
|
|
|
|
);
|
2024-06-19 01:52:16 -04:00
|
|
|
|
2024-09-17 11:34:59 +05:30
|
|
|
this.reliabilityMonitor = ReliabilityMonitorManager.createSenderMonitor(
|
|
|
|
|
this.renewPeer.bind(this)
|
|
|
|
|
);
|
|
|
|
|
|
2024-06-19 01:52:16 -04:00
|
|
|
this.protocol = this.core as LightPushCore;
|
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-07-03 12:09:34 +05:30
|
|
|
_options?: ProtocolUseOptions
|
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
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-04 10:50:58 +02:00
|
|
|
const peers = await this.getConnectedPeers();
|
|
|
|
|
if (peers.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(
|
|
|
|
|
peers.map((peer) => this.protocol.send(encoder, message, peer))
|
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);
|
|
|
|
|
|
|
|
|
|
const connectedPeer = this.connectedPeers.find((connectedPeer) =>
|
|
|
|
|
connectedPeer.id.equals(failure.peerId)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (connectedPeer) {
|
|
|
|
|
void this.reliabilityMonitor.attemptRetriesOrRenew(
|
|
|
|
|
connectedPeer.id,
|
|
|
|
|
() => this.protocol.send(encoder, message, connectedPeer)
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-03-11 18:50:34 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-04 10:50:58 +02:00
|
|
|
this.healthManager.updateProtocolHealth(LightPushCodec, successes.length);
|
|
|
|
|
|
2024-03-11 18:50:34 +05:30
|
|
|
return {
|
|
|
|
|
successes,
|
|
|
|
|
failures
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-10-04 10:50:58 +02:00
|
|
|
|
|
|
|
|
private async getConnectedPeers(): Promise<Peer[]> {
|
|
|
|
|
const peerIDs = this.libp2p.getPeers();
|
|
|
|
|
|
|
|
|
|
if (peerIDs.length === 0) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const peers = await Promise.all(
|
|
|
|
|
peerIDs.map(async (id) => {
|
|
|
|
|
try {
|
|
|
|
|
return await this.libp2p.peerStore.get(id);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return peers
|
|
|
|
|
.filter((p) => !!p)
|
|
|
|
|
.filter((p) => (p as Peer).protocols.includes(LightPushCodec))
|
|
|
|
|
.slice(0, this.numPeersToUse) as Peer[];
|
|
|
|
|
}
|
2024-03-11 18:50:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function wakuLightPush(
|
2024-06-19 01:52:16 -04:00
|
|
|
connectionManager: ConnectionManager,
|
2024-03-11 18:50:34 +05:30
|
|
|
init: Partial<ProtocolCreateOptions> = {}
|
2024-10-04 10:50:58 +02:00
|
|
|
): (libp2p: Libp2p) => ILightPush {
|
|
|
|
|
return (libp2p: Libp2p) => new LightPush(connectionManager, libp2p, init);
|
2024-03-11 18:50:34 +05:30
|
|
|
}
|