mirror of https://github.com/waku-org/js-waku.git
chore: peer-exchange uses error codes
This commit is contained in:
parent
b308d71cd4
commit
0100ded4e8
|
@ -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 { IBaseProtocol } from "./protocols.js";
|
import { IBaseProtocol, ProtocolResult } from "./protocols.js";
|
||||||
|
|
||||||
export interface IPeerExchange extends IBaseProtocol {
|
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 {
|
export interface PeerExchangeQueryParams {
|
||||||
numPeers: number;
|
numPeers: number;
|
||||||
peerId: PeerId;
|
peerId: PeerId;
|
||||||
|
|
|
@ -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
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -20,8 +20,8 @@ import {
|
||||||
IRelay,
|
IRelay,
|
||||||
Libp2p,
|
Libp2p,
|
||||||
ProtocolCreateOptions,
|
ProtocolCreateOptions,
|
||||||
|
ProtocolError,
|
||||||
PubsubTopic,
|
PubsubTopic,
|
||||||
SendError,
|
|
||||||
SendResult
|
SendResult
|
||||||
} from "@waku/interfaces";
|
} from "@waku/interfaces";
|
||||||
import { isWireSizeUnderCap, toAsyncIterator } from "@waku/utils";
|
import { isWireSizeUnderCap, toAsyncIterator } from "@waku/utils";
|
||||||
|
@ -107,7 +107,7 @@ class Relay implements IRelay {
|
||||||
log.error("Failed to send waku relay: topic not configured");
|
log.error("Failed to send waku relay: topic not configured");
|
||||||
return {
|
return {
|
||||||
recipients,
|
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");
|
log.error("Failed to encode message, aborting publish");
|
||||||
return {
|
return {
|
||||||
recipients,
|
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");
|
log.error("Failed to send waku relay: message is bigger that 1MB");
|
||||||
return {
|
return {
|
||||||
recipients,
|
recipients,
|
||||||
errors: [SendError.SIZE_TOO_BIG]
|
errors: [ProtocolError.SIZE_TOO_BIG]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue