feat: peer-exchange uses error codes (#1907)

* setup a generic protocol result type (DRY)

* metadata: use generic

* lightpush: use generic

* peer-exchange: use error codes + generic + update tests

* add issue link to skipped test

* tests: improve while loop readability
This commit is contained in:
Danish Arora 2024-03-13 19:33:50 +05:30 committed by GitHub
parent 5296bfbad8
commit 877fe1dc1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 131 additions and 97 deletions

View File

@ -6,7 +6,8 @@ import {
IMessage, IMessage,
Libp2p, Libp2p,
ProtocolCreateOptions, ProtocolCreateOptions,
ProtocolError ProtocolError,
ProtocolResult
} from "@waku/interfaces"; } from "@waku/interfaces";
import { PushResponse } from "@waku/proto"; import { PushResponse } from "@waku/proto";
import { isMessageSizeUnderCap } from "@waku/utils"; import { isMessageSizeUnderCap } from "@waku/utils";
@ -25,25 +26,9 @@ const log = new Logger("light-push");
export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1"; export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
export { PushResponse }; export { PushResponse };
type PreparePushMessageResult = type PreparePushMessageResult = ProtocolResult<"query", PushRpc>;
| {
query: PushRpc;
error: null;
}
| {
query: null;
error: ProtocolError;
};
type CoreSendResult = type CoreSendResult = ProtocolResult<"success", PeerId, "failure", Failure>;
| {
success: null;
failure: Failure;
}
| {
success: PeerId;
failure: null;
};
/** /**
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/). * Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).

View File

@ -1,10 +1,11 @@
import { BaseProtocol } from "@waku/core/lib/base_protocol"; import { BaseProtocol } from "@waku/core/lib/base_protocol";
import { EnrDecoder } from "@waku/enr"; import { EnrDecoder } from "@waku/enr";
import type { import {
IPeerExchange, IPeerExchange,
Libp2pComponents, Libp2pComponents,
PeerExchangeQueryParams, PeerExchangeQueryParams,
PeerInfo, PeerExchangeResult,
ProtocolError,
PubsubTopic PubsubTopic
} from "@waku/interfaces"; } from "@waku/interfaces";
import { isDefined } from "@waku/utils"; import { isDefined } from "@waku/utils";
@ -34,18 +35,18 @@ export class WakuPeerExchange extends BaseProtocol implements IPeerExchange {
/** /**
* Make a peer exchange query to a peer * Make a peer exchange query to a peer
*/ */
async query( async query(params: PeerExchangeQueryParams): Promise<PeerExchangeResult> {
params: PeerExchangeQueryParams
): Promise<PeerInfo[] | undefined> {
const { numPeers } = params; const { numPeers } = params;
const rpcQuery = PeerExchangeRPC.createRequest({ const rpcQuery = PeerExchangeRPC.createRequest({
numPeers: BigInt(numPeers) numPeers: BigInt(numPeers)
}); });
const peer = await this.peerStore.get(params.peerId); const peer = await this.peerStore.get(params.peerId);
if (!peer) { if (!peer) {
throw new Error(`Peer ${params.peerId.toString()} not found`); return {
peerInfos: null,
error: ProtocolError.NO_PEER_AVAILABLE
};
} }
const stream = await this.getStream(peer); const stream = await this.getStream(peer);
@ -65,15 +66,17 @@ export class WakuPeerExchange extends BaseProtocol implements IPeerExchange {
}); });
const { response } = PeerExchangeRPC.decode(bytes); const { response } = PeerExchangeRPC.decode(bytes);
if (!response) { if (!response) {
log.error( log.error(
"PeerExchangeRPC message did not contains a `response` field" "PeerExchangeRPC message did not contains a `response` field"
); );
return; return {
peerInfos: null,
error: ProtocolError.EMPTY_PAYLOAD
};
} }
return Promise.all( const peerInfos = await Promise.all(
response.peerInfos response.peerInfos
.map((peerInfo) => peerInfo.enr) .map((peerInfo) => peerInfo.enr)
.filter(isDefined) .filter(isDefined)
@ -81,9 +84,17 @@ export class WakuPeerExchange extends BaseProtocol implements IPeerExchange {
return { ENR: await EnrDecoder.fromRLP(enr) }; return { ENR: await EnrDecoder.fromRLP(enr) };
}) })
); );
return {
peerInfos,
error: null
};
} catch (err) { } catch (err) {
log.error("Failed to decode push reply", err); log.error("Failed to decode push reply", err);
return; return {
peerInfos: null,
error: ProtocolError.DECODE_FAILED
};
} }
} }
} }

View File

@ -7,7 +7,12 @@ import type {
PeerId, PeerId,
PeerInfo PeerInfo
} from "@libp2p/interface"; } from "@libp2p/interface";
import { Libp2pComponents, PubsubTopic, Tags } from "@waku/interfaces"; import {
Libp2pComponents,
PeerExchangeResult,
PubsubTopic,
Tags
} from "@waku/interfaces";
import { encodeRelayShard, Logger } from "@waku/utils"; import { encodeRelayShard, Logger } from "@waku/utils";
import { PeerExchangeCodec, WakuPeerExchange } from "./waku_peer_exchange.js"; import { PeerExchangeCodec, WakuPeerExchange } from "./waku_peer_exchange.js";
@ -160,15 +165,15 @@ export class PeerExchangeDiscovery
}, queryInterval * currentAttempt); }, queryInterval * currentAttempt);
}; };
private async query(peerId: PeerId): Promise<void> { private async query(peerId: PeerId): Promise<PeerExchangeResult> {
const peerInfos = await this.peerExchange.query({ const { error, peerInfos } = await this.peerExchange.query({
numPeers: DEFAULT_PEER_EXCHANGE_REQUEST_NODES, numPeers: DEFAULT_PEER_EXCHANGE_REQUEST_NODES,
peerId peerId
}); });
if (!peerInfos) { if (error) {
log.error("Peer exchange query failed, no peer info returned"); log.error("Peer exchange query failed", error);
return; return { error, peerInfos: null };
} }
for (const _peerInfo of peerInfos) { for (const _peerInfo of peerInfos) {
@ -214,6 +219,8 @@ export class PeerExchangeDiscovery
}) })
); );
} }
return { error: null, peerInfos };
} }
private abortQueriesForPeer(peerIdStr: string): void { private abortQueriesForPeer(peerIdStr: string): void {

View File

@ -1,21 +1,13 @@
import type { PeerId } from "@libp2p/interface"; import type { PeerId } from "@libp2p/interface";
import type { ShardInfo } from "./enr.js"; import { type ShardInfo } from "./enr.js";
import type { import type {
IBaseProtocolCore, IBaseProtocolCore,
ProtocolError, ProtocolResult,
ShardingParams ShardingParams
} from "./protocols.js"; } from "./protocols.js";
export type QueryResult = export type QueryResult = ProtocolResult<"shardInfo", ShardInfo>;
| {
shardInfo: ShardInfo;
error: null;
}
| {
shardInfo: null;
error: ProtocolError;
};
// IMetadata always has shardInfo defined while it is optionally undefined in IBaseProtocol // IMetadata always has shardInfo defined while it is optionally undefined in IBaseProtocol
export interface IMetadata extends Omit<IBaseProtocolCore, "shardInfo"> { export interface IMetadata extends Omit<IBaseProtocolCore, "shardInfo"> {

View File

@ -3,12 +3,14 @@ import type { PeerStore } from "@libp2p/interface";
import type { ConnectionManager } from "@libp2p/interface-internal"; import type { ConnectionManager } from "@libp2p/interface-internal";
import { IEnr } from "./enr.js"; import { IEnr } from "./enr.js";
import { IBaseProtocolCore } from "./protocols.js"; import { IBaseProtocolCore, ProtocolResult } from "./protocols.js";
export interface IPeerExchange extends IBaseProtocolCore { export interface IPeerExchange extends IBaseProtocolCore {
query(params: PeerExchangeQueryParams): Promise<PeerInfo[] | undefined>; query(params: PeerExchangeQueryParams): Promise<PeerExchangeResult>;
} }
export type PeerExchangeResult = ProtocolResult<"peerInfos", PeerInfo[]>;
export interface PeerExchangeQueryParams { export interface PeerExchangeQueryParams {
numPeers: number; numPeers: number;
peerId: PeerId; peerId: PeerId;

View File

@ -101,6 +101,27 @@ export type Callback<T extends IDecodedMessage> = (
msg: T msg: T
) => void | Promise<void>; ) => void | Promise<void>;
// SK = success key name
// SV = success value type
// EK = error key name (default: "error")
// EV = error value type (default: ProtocolError)
export type ProtocolResult<
SK extends string,
SV,
EK extends string = "error",
EV = ProtocolError
> =
| ({
[key in SK]: SV;
} & {
[key in EK]: null;
})
| ({
[key in SK]: null;
} & {
[key in EK]: EV;
});
export enum ProtocolError { export enum ProtocolError {
/** Could not determine the origin of the fault. Best to check connectivity and try again */ /** Could not determine the origin of the fault. Best to check connectivity and try again */
GENERIC_FAIL = "Generic error", GENERIC_FAIL = "Generic error",
@ -146,7 +167,12 @@ export enum ProtocolError {
* is logged. Review message validity, or mitigation for `NO_PEER_AVAILABLE` * is logged. Review message validity, or mitigation for `NO_PEER_AVAILABLE`
* or `DECODE_FAILED` can be used. * or `DECODE_FAILED` can be used.
*/ */
REMOTE_PEER_REJECTED = "Remote peer rejected" 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
*/
REQUEST_TIMEOUT = "Request timeout"
} }
export interface Failure { export interface Failure {

View File

@ -7,8 +7,8 @@ import {
WakuPeerExchange, WakuPeerExchange,
wakuPeerExchangeDiscovery wakuPeerExchangeDiscovery
} from "@waku/discovery"; } from "@waku/discovery";
import type { LightNode, PeerInfo } from "@waku/interfaces"; import type { LightNode, PeerExchangeResult } from "@waku/interfaces";
import { createLightNode, Libp2pComponents } from "@waku/sdk"; import { createLightNode, Libp2pComponents, ProtocolError } from "@waku/sdk";
import { Logger, singleShardInfoToPubsubTopic } from "@waku/utils"; import { Logger, singleShardInfoToPubsubTopic } from "@waku/utils";
import { expect } from "chai"; import { expect } from "chai";
@ -38,7 +38,7 @@ describe("Peer Exchange Query", function () {
let components: Libp2pComponents; let components: Libp2pComponents;
let peerExchange: WakuPeerExchange; let peerExchange: WakuPeerExchange;
let numPeersToRequest: number; let numPeersToRequest: number;
let peerInfos: PeerInfo[]; let queryResult: PeerExchangeResult;
beforeEachCustom( beforeEachCustom(
this, this,
@ -85,57 +85,77 @@ describe("Peer Exchange Query", function () {
peerExchange = new WakuPeerExchange(components, pubsubTopic); peerExchange = new WakuPeerExchange(components, pubsubTopic);
numPeersToRequest = 2; numPeersToRequest = 2;
// querying the connected peer
peerInfos = [];
const startTime = Date.now(); const startTime = Date.now();
while (!peerInfos || peerInfos.length != numPeersToRequest) {
if (Date.now() - startTime > 100000) { while (true) {
if (Date.now() - startTime > 100_000) {
log.error("Timeout reached, exiting the loop."); log.error("Timeout reached, exiting the loop.");
break; break;
} }
await delay(2000); await delay(2000);
try { try {
peerInfos = await Promise.race([ queryResult = await Promise.race([
peerExchange.query({ peerExchange.query({
peerId: nwaku3PeerId, peerId: nwaku3PeerId,
numPeers: numPeersToRequest numPeers: numPeersToRequest
}) as Promise<PeerInfo[]>, }),
new Promise<PeerInfo[]>((resolve) => new Promise<PeerExchangeResult>((resolve) =>
setTimeout(() => resolve([]), 5000) setTimeout(
() =>
resolve({
peerInfos: null,
error: ProtocolError.REQUEST_TIMEOUT
}),
5000
)
) )
]); ]);
const hasErrors = queryResult?.error !== null;
if (peerInfos.length === 0) { const hasPeerInfos =
log.warn("Query timed out, retrying..."); queryResult?.peerInfos &&
queryResult.peerInfos.length === numPeersToRequest;
if (hasErrors) {
if (queryResult.error === ProtocolError.REQUEST_TIMEOUT) {
log.warn("Query timed out, retrying...");
} else {
log.error("Error encountered, retrying...", queryResult.error);
}
continue; continue;
} }
if (!hasPeerInfos) {
log.warn(
"Peer info not available or does not match the requested number of peers, retrying..."
);
continue;
}
break;
} catch (error) { } catch (error) {
log.warn("Error encountered, retrying..."); log.warn("Error encountered, retrying...", error);
} }
} }
}, },
120000 120_000
); );
afterEachCustom(this, async () => { afterEachCustom(this, async () => {
await tearDownNodes([nwaku1, nwaku2, nwaku3], waku); await tearDownNodes([nwaku1, nwaku2, nwaku3], waku);
}); });
// slow and flaky in CI // slow and flaky in CI: https://github.com/waku-org/js-waku/issues/1911
it.skip("connected peers and dial", async function () { it.skip("connected peers and dial", async function () {
expect(peerInfos[0].ENR).to.not.be.null; expect(queryResult.error).to.be.null;
expect(peerInfos[0].ENR?.peerInfo?.multiaddrs).to.not.be.null;
const peerWsMA = peerInfos[0].ENR?.peerInfo?.multiaddrs[2]; expect(queryResult.peerInfos?.[0].ENR).to.not.be.null;
expect(queryResult.peerInfos?.[0].ENR?.peerInfo?.multiaddrs).to.not.be.null;
const peerWsMA = queryResult.peerInfos?.[0].ENR?.peerInfo?.multiaddrs[2];
const localPeerWsMAAsString = peerWsMA const localPeerWsMAAsString = peerWsMA
?.toString() ?.toString()
.replace(/\/ip4\/[\d.]+\//, "/ip4/127.0.0.1/"); .replace(/\/ip4\/[\d.]+\//, "/ip4/127.0.0.1/");
const localPeerWsMA = multiaddr(localPeerWsMAAsString); const localPeerWsMA = multiaddr(localPeerWsMAAsString);
let foundNodePeerId: PeerId | undefined = undefined; let foundNodePeerId: PeerId | undefined = undefined;
const doesPeerIdExistInResponse = peerInfos.some(({ ENR }) => { const doesPeerIdExistInResponse = queryResult.peerInfos?.some(({ ENR }) => {
foundNodePeerId = ENR?.peerInfo?.id; foundNodePeerId = ENR?.peerInfo?.id;
return ENR?.peerInfo?.id.toString() === nwaku1PeerId.toString(); return ENR?.peerInfo?.id.toString() === nwaku1PeerId.toString();
}); });
@ -148,43 +168,34 @@ describe("Peer Exchange Query", function () {
await waitForRemotePeerWithCodec(waku, PeerExchangeCodec, foundNodePeerId); await waitForRemotePeerWithCodec(waku, PeerExchangeCodec, foundNodePeerId);
}); });
// slow and flaky in CI // slow and flaky in CI: https://github.com/waku-org/js-waku/issues/1911
it.skip("more peers than existing", async function () { it.skip("more peers than existing", async function () {
const peerInfo = await peerExchange.query({ const result = await peerExchange.query({
peerId: nwaku3PeerId, peerId: nwaku3PeerId,
numPeers: 5 numPeers: 5
}); });
expect(peerInfo?.length).to.be.eq(numPeersToRequest); expect(result.error).to.be.null;
expect(result.peerInfos?.length).to.be.eq(numPeersToRequest);
}); });
// slow and flaky in CI // slow and flaky in CI: https://github.com/waku-org/js-waku/issues/1911
it.skip("less peers than existing", async function () { it.skip("less peers than existing", async function () {
const peerInfo = await peerExchange.query({ const result = await peerExchange.query({
peerId: nwaku3PeerId, peerId: nwaku3PeerId,
numPeers: 1 numPeers: 1
}); });
expect(peerInfo?.length).to.be.eq(1); expect(result.error).to.be.null;
expect(result.peerInfos?.length).to.be.eq(1);
}); });
// slow and flaky in CI // slow and flaky in CI: https://github.com/waku-org/js-waku/issues/1911
it.skip("non connected peers", async function () { it.skip("non connected peers", async function () {
// querying the non connected peer // querying the non connected peer
try { const result = await peerExchange.query({
await peerExchange.query({ peerId: nwaku1PeerId,
peerId: nwaku1PeerId, numPeers: numPeersToRequest
numPeers: numPeersToRequest });
}); expect(result.error).to.be.eq(ProtocolError.NO_PEER_AVAILABLE);
throw new Error("Query on not connected peer succeeded unexpectedly."); expect(result.peerInfos).to.be.null;
} catch (error) {
if (
!(
error instanceof Error &&
(error.message === "Not Found" ||
error.message === "Failed to get a connection to the peer")
)
) {
throw error;
}
}
}); });
}); });