Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 49 additions & 18 deletions packages/reqresp/src/ReqResp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {Logger} from "@lodestar/utils";
import {getMetrics, Metrics, MetricsRegister} from "./metrics.js";
import {RequestError, RequestErrorCode, sendRequest, SendRequestOpts} from "./request/index.js";
import {handleRequest} from "./response/index.js";
import {Encoding, ProtocolDefinition, ReqRespRateLimiterOpts} from "./types.js";
import {
DialOnlyProtocolDefinition,
Encoding,
MixedProtocolDefinition,
ReqRespRateLimiterOpts,
ProtocolDefinition,
} from "./types.js";
import {formatProtocolID} from "./utils/protocolId.js";
import {ReqRespRateLimiter} from "./rate_limiter/ReqRespRateLimiter.js";

Expand Down Expand Up @@ -50,7 +56,11 @@ export class ReqResp {
private readonly protocolPrefix: string;

/** `${protocolPrefix}/${method}/${version}/${encoding}` */
private readonly registeredProtocols = new Map<ProtocolID, ProtocolDefinition>();
// Use any to avoid TS error on registering protocol
// Type 'unknown' is not assignable to type 'Resp'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private readonly registeredProtocols = new Map<ProtocolID, MixedProtocolDefinition<any, any>>();
private readonly dialOnlyProtocols = new Map<ProtocolID, boolean>();

constructor(modules: ReqRespProtocolModules, private readonly opts: ReqRespOpts = {}) {
this.libp2p = modules.libp2p;
Expand All @@ -60,6 +70,27 @@ export class ReqResp {
this.rateLimiter = new ReqRespRateLimiter(opts);
}

/**
* Register protocol which will be used only to dial to other peers
* The libp2p instance will not handle that protocol
*
* Made it explicit method to avoid any developer mistake
*/
registerDialOnlyProtocol<Req, Resp>(
protocol: DialOnlyProtocolDefinition<Req, Resp>,
opts?: ReqRespRegisterOpts
): void {
const protocolID = this.formatProtocolID(protocol);

// libp2p will throw on error on duplicates, allow to overwrite behavior
if (opts?.ignoreIfDuplicate && this.registeredProtocols.has(protocolID)) {
return;
}

this.registeredProtocols.set(protocolID, protocol);
this.dialOnlyProtocols.set(protocolID, true);
}

/**
* Register protocol as supported and to libp2p.
* async because libp2p registar persists the new protocol list in the peer-store.
Expand All @@ -71,16 +102,12 @@ export class ReqResp {
opts?: ReqRespRegisterOpts
): Promise<void> {
const protocolID = this.formatProtocolID(protocol);
const {handler: _handler, renderRequestBody: _renderRequestBody, inboundRateLimits, ...rest} = protocol;
this.registerDialOnlyProtocol(rest, opts);
this.dialOnlyProtocols.set(protocolID, false);

// libp2p will throw on error on duplicates, allow to overwrite behaviour
if (opts?.ignoreIfDuplicate && this.registeredProtocols.has(protocolID)) {
return;
}

this.registeredProtocols.set(protocolID, protocol as ProtocolDefinition);

if (protocol.inboundRateLimits) {
this.rateLimiter.initRateLimits(protocolID, protocol.inboundRateLimits);
if (inboundRateLimits) {
this.rateLimiter.initRateLimits(protocolID, inboundRateLimits);
}

return this.libp2p.handle(protocolID, this.getRequestHandler(protocol, protocolID));
Expand Down Expand Up @@ -135,7 +162,7 @@ export class ReqResp {
this.metrics?.outgoingRequests.inc({method});
const timer = this.metrics?.outgoingRequestRoundtripTime.startTimer({method});

const protocols: ProtocolDefinition[] = [];
const protocols: (MixedProtocolDefinition | DialOnlyProtocolDefinition)[] = [];
const protocolIDs: string[] = [];

for (const version of versions) {
Expand Down Expand Up @@ -176,23 +203,27 @@ export class ReqResp {
}
}

private getRequestHandler<Req, Resp>(protocol: ProtocolDefinition<Req, Resp>, protocolID: string) {
private getRequestHandler<Req, Resp>(protocol: MixedProtocolDefinition<Req, Resp>, protocolID: string) {
return async ({connection, stream}: {connection: Connection; stream: Stream}) => {
if (this.dialOnlyProtocols.get(protocolID)) {
throw new Error(`Received request on dial only protocol '${protocolID}'`);
}

const peerId = connection.remotePeer;
const peerClient = this.opts.getPeerLogMetadata?.(peerId.toString());
const {method} = protocol;

this.metrics?.incomingRequests.inc({method});
const timer = this.metrics?.incomingRequestHandlerTime.startTimer({method});

this.onIncomingRequest?.(peerId, protocol as ProtocolDefinition);
this.onIncomingRequest?.(peerId, protocol as MixedProtocolDefinition);

try {
await handleRequest<Req, Resp>({
logger: this.logger,
stream,
peerId,
protocol,
protocol: protocol as ProtocolDefinition<Req, Resp>,
protocolID,
rateLimiter: this.rateLimiter,
signal: this.controller.signal,
Expand All @@ -216,12 +247,12 @@ export class ReqResp {
};
}

protected onIncomingRequest(_peerId: PeerId, _protocol: ProtocolDefinition): void {
protected onIncomingRequest(_peerId: PeerId, _protocol: MixedProtocolDefinition): void {
// Override
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected onIncomingRequestError(_protocol: ProtocolDefinition<any, any>, _error: RequestError): void {
protected onIncomingRequestError(_protocol: MixedProtocolDefinition<any, any>, _error: RequestError): void {
// Override
}

Expand All @@ -235,7 +266,7 @@ export class ReqResp {
* ```
* https://github.com/ethereum/consensus-specs/blob/v1.2.0/specs/phase0/p2p-interface.md#protocol-identification
*/
protected formatProtocolID(protocol: Pick<ProtocolDefinition, "method" | "version" | "encoding">): string {
protected formatProtocolID(protocol: Pick<MixedProtocolDefinition, "method" | "version" | "encoding">): string {
return formatProtocolID(this.protocolPrefix, protocol.method, protocol.version, protocol.encoding);
}
}
4 changes: 2 additions & 2 deletions packages/reqresp/src/encoders/requestDecode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Sink} from "it-stream-types";
import {Uint8ArrayList} from "uint8arraylist";
import {ForkName} from "@lodestar/params";
import {ProtocolDefinition} from "../types.js";
import {MixedProtocolDefinition} from "../types.js";
import {BufferedSource} from "../utils/index.js";
import {readEncodedPayload} from "../encodingStrategies/index.js";
/**
Expand All @@ -11,7 +11,7 @@ import {readEncodedPayload} from "../encodingStrategies/index.js";
* ```
*/
export function requestDecode<Req, Resp>(
protocol: ProtocolDefinition<Req, Resp>
protocol: MixedProtocolDefinition<Req, Resp>
): Sink<Uint8Array | Uint8ArrayList, Promise<Req>> {
return async function requestDecodeSink(source) {
const type = protocol.requestType(ForkName.phase0);
Expand Down
7 changes: 5 additions & 2 deletions packages/reqresp/src/encoders/requestEncode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ForkName} from "@lodestar/params";
import {EncodedPayloadType, ProtocolDefinition} from "../types.js";
import {EncodedPayloadType, MixedProtocolDefinition} from "../types.js";
import {writeEncodedPayload} from "../encodingStrategies/index.js";

/**
Expand All @@ -10,7 +10,10 @@ import {writeEncodedPayload} from "../encodingStrategies/index.js";
* Requests may contain no payload (e.g. /eth2/beacon_chain/req/metadata/1/)
* if so, it would yield no byte chunks
*/
export async function* requestEncode<Req>(protocol: ProtocolDefinition<Req>, requestBody: Req): AsyncGenerator<Buffer> {
export async function* requestEncode<Req>(
protocol: MixedProtocolDefinition<Req>,
requestBody: Req
): AsyncGenerator<Buffer> {
const type = protocol.requestType(ForkName.phase0);

if (type && requestBody !== null) {
Expand Down
10 changes: 5 additions & 5 deletions packages/reqresp/src/encoders/responseDecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
ContextBytesType,
CONTEXT_BYTES_FORK_DIGEST_LENGTH,
ContextBytesFactory,
ProtocolDefinition,
TypeSerializer,
MixedProtocolDefinition,
} from "../types.js";
import {RespStatus} from "../interface.js";

Expand All @@ -28,7 +28,7 @@ enum StreamStatus {
* ```
*/
export function responseDecode<Resp>(
protocol: ProtocolDefinition,
protocol: MixedProtocolDefinition,
cbs: {
onFirstHeader: () => void;
onFirstResponseChunk: () => void;
Expand Down Expand Up @@ -62,7 +62,7 @@ export function responseDecode<Resp>(
throw new ResponseError(status, errorMessage);
}

const forkName = await readContextBytes<Resp>(protocol.contextBytes, bufferedSource);
const forkName = await readContextBytes(protocol.contextBytes, bufferedSource);
const type = protocol.responseType(forkName) as TypeSerializer<Resp>;

yield await readEncodedPayload<Resp>(bufferedSource, protocol.encoding, type);
Expand Down Expand Up @@ -132,8 +132,8 @@ export async function readErrorMessage(bufferedSource: BufferedSource): Promise<
* While `<context-bytes>` has a single type of `ForkDigest`, this function only parses the `ForkName`
* of the `ForkDigest` or defaults to `phase0`
*/
export async function readContextBytes<Resp>(
contextBytes: ContextBytesFactory<Resp>,
export async function readContextBytes(
contextBytes: ContextBytesFactory<unknown>,
bufferedSource: BufferedSource
): Promise<ForkName> {
switch (contextBytes.type) {
Expand Down
5 changes: 3 additions & 2 deletions packages/reqresp/src/encoders/responseEncode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {encodeErrorMessage} from "../utils/index.js";
import {
ContextBytesType,
ContextBytesFactory,
ProtocolDefinition,
MixedProtocolDefinition,
EncodedPayload,
EncodedPayloadType,
ProtocolDefinition,
} from "../types.js";
import {RespStatus, RpcResponseStatusError} from "../interface.js";

Expand Down Expand Up @@ -52,7 +53,7 @@ export function responseEncodeSuccess<Req, Resp>(
* fn yields exactly one `<error_response>` and afterwards the stream must be terminated
*/
export async function* responseEncodeError(
protocol: Pick<ProtocolDefinition, "encoding">,
protocol: Pick<MixedProtocolDefinition, "encoding">,
status: RpcResponseStatusError,
errorMessage: string
): AsyncGenerator<Buffer> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {deneb, ssz} from "@lodestar/types";
import {toHex} from "@lodestar/utils";
import {ContextBytesType, Encoding, ProtocolDefinitionGenerator} from "../types.js";
import {ContextBytesType, DuplexProtocolDefinitionGenerator, Encoding} from "../types.js";

// eslint-disable-next-line @typescript-eslint/naming-convention
export const BeaconBlockAndBlobsSidecarByRoot: ProtocolDefinitionGenerator<
export const BeaconBlockAndBlobsSidecarByRoot: DuplexProtocolDefinitionGenerator<
deneb.BeaconBlockAndBlobsSidecarByRootRequest,
deneb.SignedBeaconBlockAndBlobsSidecar
> = (modules, handler) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/reqresp/src/protocols/BeaconBlocksByRange.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {MAX_REQUEST_BLOCKS} from "@lodestar/params";
import {allForks, phase0, ssz} from "@lodestar/types";
import {ContextBytesType, Encoding, ProtocolDefinition, ProtocolDefinitionGenerator} from "../types.js";
import {ContextBytesType, Encoding, MixedProtocolDefinition, DuplexProtocolDefinitionGenerator} from "../types.js";

/* eslint-disable @typescript-eslint/naming-convention */
const BeaconBlocksByRangeCommon: Pick<
ProtocolDefinition<phase0.BeaconBlocksByRangeRequest, allForks.SignedBeaconBlock>,
MixedProtocolDefinition<phase0.BeaconBlocksByRangeRequest, allForks.SignedBeaconBlock>,
"method" | "encoding" | "requestType" | "renderRequestBody" | "inboundRateLimits"
> = {
method: "beacon_blocks_by_range",
Expand All @@ -18,7 +18,7 @@ const BeaconBlocksByRangeCommon: Pick<
},
};

export const BeaconBlocksByRange: ProtocolDefinitionGenerator<
export const BeaconBlocksByRange: DuplexProtocolDefinitionGenerator<
phase0.BeaconBlocksByRangeRequest,
allForks.SignedBeaconBlock
> = (_modules, handler) => {
Expand All @@ -31,7 +31,7 @@ export const BeaconBlocksByRange: ProtocolDefinitionGenerator<
};
};

export const BeaconBlocksByRangeV2: ProtocolDefinitionGenerator<
export const BeaconBlocksByRangeV2: DuplexProtocolDefinitionGenerator<
phase0.BeaconBlocksByRangeRequest,
allForks.SignedBeaconBlock
> = (modules, handler) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/reqresp/src/protocols/BeaconBlocksByRoot.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {allForks, phase0, ssz} from "@lodestar/types";
import {toHex} from "@lodestar/utils";
import {ContextBytesType, Encoding, ProtocolDefinitionGenerator, ProtocolDefinition} from "../types.js";
import {ContextBytesType, DuplexProtocolDefinitionGenerator, Encoding, MixedProtocolDefinition} from "../types.js";

/* eslint-disable @typescript-eslint/naming-convention */
const BeaconBlocksByRootCommon: Pick<
ProtocolDefinition<phase0.BeaconBlocksByRootRequest, allForks.SignedBeaconBlock>,
MixedProtocolDefinition<phase0.BeaconBlocksByRootRequest, allForks.SignedBeaconBlock>,
"method" | "encoding" | "requestType" | "renderRequestBody" | "inboundRateLimits"
> = {
method: "beacon_blocks_by_root",
Expand All @@ -18,7 +18,7 @@ const BeaconBlocksByRootCommon: Pick<
},
};

export const BeaconBlocksByRoot: ProtocolDefinitionGenerator<
export const BeaconBlocksByRoot: DuplexProtocolDefinitionGenerator<
phase0.BeaconBlocksByRootRequest,
allForks.SignedBeaconBlock
> = (_modules, handler) => {
Expand All @@ -31,7 +31,7 @@ export const BeaconBlocksByRoot: ProtocolDefinitionGenerator<
};
};

export const BeaconBlocksByRootV2: ProtocolDefinitionGenerator<
export const BeaconBlocksByRootV2: DuplexProtocolDefinitionGenerator<
phase0.BeaconBlocksByRootRequest,
allForks.SignedBeaconBlock
> = (modules, handler) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/reqresp/src/protocols/BlobsSidecarsByRange.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {MAX_REQUEST_BLOCKS} from "@lodestar/params";
import {deneb, ssz} from "@lodestar/types";
import {ContextBytesType, Encoding, ProtocolDefinitionGenerator} from "../types.js";
import {ContextBytesType, DuplexProtocolDefinitionGenerator, Encoding} from "../types.js";

// eslint-disable-next-line @typescript-eslint/naming-convention
export const BlobsSidecarsByRange: ProtocolDefinitionGenerator<
export const BlobsSidecarsByRange: DuplexProtocolDefinitionGenerator<
deneb.BlobsSidecarsByRangeRequest,
deneb.BlobsSidecar
> = (modules, handler) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/reqresp/src/protocols/Goodbye.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {phase0, ssz} from "@lodestar/types";
import {ContextBytesType, Encoding, ProtocolDefinitionGenerator} from "../types.js";
import {ContextBytesType, DuplexProtocolDefinitionGenerator, Encoding} from "../types.js";

// eslint-disable-next-line @typescript-eslint/naming-convention
export const Goodbye: ProtocolDefinitionGenerator<phase0.Goodbye, phase0.Goodbye> = (_modules, handler) => {
export const Goodbye: DuplexProtocolDefinitionGenerator<phase0.Goodbye, phase0.Goodbye> = (_modules, handler) => {
return {
method: "goodbye",
version: 1,
Expand Down
18 changes: 12 additions & 6 deletions packages/reqresp/src/protocols/LightClientBootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
import {Root, ssz, allForks} from "@lodestar/types";
import {isForkLightClient} from "@lodestar/params";
import {toHex} from "@lodestar/utils";
import {Encoding, ProtocolDefinitionGenerator} from "../types.js";
import {DialOnlyProtocolDefinition, Encoding, MixedProtocolDefinitionGenerator} from "../types.js";
import {getContextBytesLightclient} from "./utils.js";

// eslint-disable-next-line @typescript-eslint/naming-convention
export const LightClientBootstrap: ProtocolDefinitionGenerator<Root, allForks.LightClientBootstrap> = (
export const LightClientBootstrap: MixedProtocolDefinitionGenerator<Root, allForks.LightClientBootstrap> = ((
modules,
handler
) => {
return {
const dialProtocol: DialOnlyProtocolDefinition<Root, allForks.LightClientBootstrap> = {
method: "light_client_bootstrap",
version: 1,
encoding: Encoding.SSZ_SNAPPY,
handler,
requestType: () => ssz.Root,
responseType: (forkName) =>
isForkLightClient(forkName)
? ssz.allForksLightClient[forkName].LightClientBootstrap
: ssz.altair.LightClientBootstrap,
renderRequestBody: (req) => toHex(req),
contextBytes: getContextBytesLightclient(
(bootstrap) => modules.config.getForkName(bootstrap.header.beacon.slot),
modules
),
};

if (!handler) return dialProtocol;

return {
...dialProtocol,
handler,
renderRequestBody: (req) => toHex(req),
inboundRateLimits: {
// As similar in the nature of `Status` protocol so we use the same rate limits.
byPeer: {quota: 5, quotaTimeMs: 15_000},
},
};
};
}) as MixedProtocolDefinitionGenerator<Root, allForks.LightClientBootstrap>;
Loading