chore: peer-exchange uses error codes

This commit is contained in:
danisharora099 2024-03-11 19:14:59 +05:30
parent b308d71cd4
commit 0100ded4e8
No known key found for this signature in database
GPG Key ID: FBD2BF500037F135
4 changed files with 43 additions and 23 deletions

View File

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

View File

@ -1,10 +1,11 @@
import { BaseProtocol } from "@waku/core/lib/base_protocol";
import { EnrDecoder } from "@waku/enr";
import type {
import {
IPeerExchange,
Libp2pComponents,
PeerExchangeQueryParams,
PeerInfo,
PeerExchangeResult,
ProtocolError,
PubsubTopic
} from "@waku/interfaces";
import { isDefined } from "@waku/utils";
@ -34,18 +35,18 @@ export class WakuPeerExchange extends BaseProtocol implements IPeerExchange {
/**
* Make a peer exchange query to a peer
*/
async query(
params: PeerExchangeQueryParams
): Promise<PeerInfo[] | undefined> {
async query(params: PeerExchangeQueryParams): Promise<PeerExchangeResult> {
const { numPeers } = params;
const rpcQuery = PeerExchangeRPC.createRequest({
numPeers: BigInt(numPeers)
});
const peer = await this.peerStore.get(params.peerId);
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);
@ -65,15 +66,17 @@ export class WakuPeerExchange extends BaseProtocol implements IPeerExchange {
});
const { response } = PeerExchangeRPC.decode(bytes);
if (!response) {
log.error(
"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
.map((peerInfo) => peerInfo.enr)
.filter(isDefined)
@ -81,9 +84,17 @@ export class WakuPeerExchange extends BaseProtocol implements IPeerExchange {
return { ENR: await EnrDecoder.fromRLP(enr) };
})
);
return {
peerInfos,
error: null
};
} catch (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,
PeerInfo
} 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 { PeerExchangeCodec, WakuPeerExchange } from "./waku_peer_exchange.js";
@ -160,15 +165,15 @@ export class PeerExchangeDiscovery
}, queryInterval * currentAttempt);
};
private async query(peerId: PeerId): Promise<void> {
const peerInfos = await this.peerExchange.query({
private async query(peerId: PeerId): Promise<PeerExchangeResult> {
const { error, peerInfos } = await this.peerExchange.query({
numPeers: DEFAULT_PEER_EXCHANGE_REQUEST_NODES,
peerId
});
if (!peerInfos) {
log.error("Peer exchange query failed, no peer info returned");
return;
if (error) {
log.error("Peer exchange query failed", error);
return { error, peerInfos: null };
}
for (const _peerInfo of peerInfos) {
@ -214,6 +219,8 @@ export class PeerExchangeDiscovery
})
);
}
return { error: null, peerInfos };
}
private abortQueriesForPeer(peerIdStr: string): void {

View File

@ -20,8 +20,8 @@ import {
IRelay,
Libp2p,
ProtocolCreateOptions,
ProtocolError,
PubsubTopic,
SendError,
SendResult
} from "@waku/interfaces";
import { isWireSizeUnderCap, toAsyncIterator } from "@waku/utils";
@ -107,7 +107,7 @@ class Relay implements IRelay {
log.error("Failed to send waku relay: topic not configured");
return {
recipients,
errors: [SendError.TOPIC_NOT_CONFIGURED]
errors: [ProtocolError.TOPIC_NOT_CONFIGURED]
};
}
@ -116,7 +116,7 @@ class Relay implements IRelay {
log.error("Failed to encode message, aborting publish");
return {
recipients,
errors: [SendError.ENCODE_FAILED]
errors: [ProtocolError.ENCODE_FAILED]
};
}
@ -124,7 +124,7 @@ class Relay implements IRelay {
log.error("Failed to send waku relay: message is bigger that 1MB");
return {
recipients,
errors: [SendError.SIZE_TOO_BIG]
errors: [ProtocolError.SIZE_TOO_BIG]
};
}