Skip to content

Commit

Permalink
fix: use correct fork constants to limit max request blocks/blobs cou…
Browse files Browse the repository at this point in the history
…nt (#7368)

* fix: use correct fork constants to limit max request blocks/blobs count

* Add helper to get max request blob sidecars by fork

* Add a more generic getter to fork config

* Formatting

* Revert "Formatting"

This reverts commit 28af071.

* Revert "Add a more generic getter to fork config"

This reverts commit 40b51d2.
  • Loading branch information
nflaig authored Jan 17, 2025
1 parent 717f02a commit eb2ee60
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {GENESIS_SLOT, MAX_REQUEST_BLOCKS} from "@lodestar/params";
import {BeaconConfig} from "@lodestar/config";
import {GENESIS_SLOT, MAX_REQUEST_BLOCKS, MAX_REQUEST_BLOCKS_DENEB, isForkBlobs} from "@lodestar/params";
import {RespStatus, ResponseError, ResponseOutgoing} from "@lodestar/reqresp";
import {deneb, phase0} from "@lodestar/types";
import {fromHex} from "@lodestar/utils";
Expand All @@ -12,7 +13,7 @@ export async function* onBeaconBlocksByRange(
chain: IBeaconChain,
db: IBeaconDb
): AsyncIterable<ResponseOutgoing> {
const {startSlot, count} = validateBeaconBlocksByRangeRequest(request);
const {startSlot, count} = validateBeaconBlocksByRangeRequest(chain.config, request);
const endSlot = startSlot + count;

const finalized = db.blockArchive;
Expand Down Expand Up @@ -69,6 +70,7 @@ export async function* onBeaconBlocksByRange(
}

export function validateBeaconBlocksByRangeRequest(
config: BeaconConfig,
request: deneb.BlobSidecarsByRangeRequest
): deneb.BlobSidecarsByRangeRequest {
const {startSlot} = request;
Expand All @@ -84,9 +86,10 @@ export function validateBeaconBlocksByRangeRequest(

// step > 1 is deprecated, see https://github.com/ethereum/consensus-specs/pull/2856

if (count > MAX_REQUEST_BLOCKS) {
// TODO: This is probably not right as `BeaconBlocksByRangeV2` takes at most `MAX_REQUEST_BLOCKS_DENEB`
count = MAX_REQUEST_BLOCKS;
const maxRequestBlocks = isForkBlobs(config.getForkName(startSlot)) ? MAX_REQUEST_BLOCKS_DENEB : MAX_REQUEST_BLOCKS;

if (count > maxRequestBlocks) {
count = maxRequestBlocks;
}

return {startSlot, count};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {BLOBSIDECAR_FIXED_SIZE, GENESIS_SLOT, MAX_REQUEST_BLOCKS_DENEB} from "@lodestar/params";
import {BeaconConfig} from "@lodestar/config";
import {BLOBSIDECAR_FIXED_SIZE, GENESIS_SLOT} from "@lodestar/params";
import {RespStatus, ResponseError, ResponseOutgoing} from "@lodestar/reqresp";
import {Slot, deneb} from "@lodestar/types";
import {fromHex} from "@lodestar/utils";
Expand All @@ -12,7 +13,7 @@ export async function* onBlobSidecarsByRange(
db: IBeaconDb
): AsyncIterable<ResponseOutgoing> {
// Non-finalized range of blobs
const {startSlot, count} = validateBlobSidecarsByRangeRequest(request);
const {startSlot, count} = validateBlobSidecarsByRangeRequest(chain.config, request);
const endSlot = startSlot + count;

const finalized = db.blobSidecarsArchive;
Expand Down Expand Up @@ -90,6 +91,7 @@ export function* iterateBlobBytesFromWrapper(
}

export function validateBlobSidecarsByRangeRequest(
config: BeaconConfig,
request: deneb.BlobSidecarsByRangeRequest
): deneb.BlobSidecarsByRangeRequest {
const {startSlot} = request;
Expand All @@ -103,8 +105,10 @@ export function validateBlobSidecarsByRangeRequest(
throw new ResponseError(RespStatus.INVALID_REQUEST, "startSlot < genesis");
}

if (count > MAX_REQUEST_BLOCKS_DENEB) {
count = MAX_REQUEST_BLOCKS_DENEB;
const maxRequestBlobSidecars = config.getMaxRequestBlobSidecars(config.getForkName(startSlot));

if (count > maxRequestBlobSidecars) {
count = maxRequestBlobSidecars;
}

return {startSlot, count};
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/src/network/reqresp/rateLimit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {ChainConfig} from "@lodestar/config";
import {BeaconConfig} from "@lodestar/config";
import {MAX_REQUEST_BLOCKS, MAX_REQUEST_LIGHT_CLIENT_UPDATES} from "@lodestar/params";
import {InboundRateLimitQuota} from "@lodestar/reqresp";
import {ReqRespMethod, RequestBodyByMethod} from "./types.js";
import {requestSszTypeByMethod} from "./types.js";

export const rateLimitQuotas: (config: ChainConfig) => Record<ReqRespMethod, InboundRateLimitQuota> = (config) => ({
export const rateLimitQuotas: (config: BeaconConfig) => Record<ReqRespMethod, InboundRateLimitQuota> = (config) => ({
[ReqRespMethod.Status]: {
// Rationale: https://github.com/sigp/lighthouse/blob/bf533c8e42cc73c35730e285c21df8add0195369/beacon_node/lighthouse_network/src/rpc/mod.rs#L118-L130
byPeer: {quota: 5, quotaTimeMs: 15_000},
Expand Down Expand Up @@ -67,7 +67,7 @@ export const rateLimitQuotas: (config: ChainConfig) => Record<ReqRespMethod, Inb

// Helper to produce a getRequestCount function
function getRequestCountFn<T extends ReqRespMethod>(
config: ChainConfig,
config: BeaconConfig,
method: T,
fn: (req: RequestBodyByMethod[T]) => number
): (reqData: Uint8Array) => number {
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/network/reqresp/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Type} from "@chainsafe/ssz";
import {ChainConfig} from "@lodestar/config";
import {BeaconConfig} from "@lodestar/config";
import {ForkLightClient, ForkName, isForkLightClient} from "@lodestar/params";
import {Protocol, ProtocolHandler, ReqRespRequest} from "@lodestar/reqresp";
import {
Expand Down Expand Up @@ -72,7 +72,7 @@ type ResponseBodyByMethod = {
/** Request SSZ type for each method and ForkName */
// TODO Electra: Currently setting default fork to deneb because not every caller of requestSszTypeByMethod can provide fork info
export const requestSszTypeByMethod: (
config: ChainConfig,
config: BeaconConfig,
fork?: ForkName
) => {
[K in ReqRespMethod]: RequestBodyByMethod[K] extends null ? null : Type<RequestBodyByMethod[K]>;
Expand Down
11 changes: 4 additions & 7 deletions packages/beacon-node/src/util/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ContainerType, ListCompositeType, ValueOf} from "@chainsafe/ssz";
import {ChainConfig} from "@lodestar/config";
import {ForkName, isForkPostElectra} from "@lodestar/params";
import {BeaconConfig} from "@lodestar/config";
import {ForkName} from "@lodestar/params";
import {ssz} from "@lodestar/types";

// Misc SSZ types used only in the beacon-node package, no need to upstream to types
Expand All @@ -15,9 +15,6 @@ export const signedBLSToExecutionChangeVersionedType = new ContainerType(
);
export type SignedBLSToExecutionChangeVersioned = ValueOf<typeof signedBLSToExecutionChangeVersionedType>;

export const BlobSidecarsByRootRequestType = (fork: ForkName, config: ChainConfig) =>
new ListCompositeType(
ssz.deneb.BlobIdentifier,
isForkPostElectra(fork) ? config.MAX_REQUEST_BLOB_SIDECARS_ELECTRA : config.MAX_REQUEST_BLOB_SIDECARS
);
export const BlobSidecarsByRootRequestType = (fork: ForkName, config: BeaconConfig) =>
new ListCompositeType(ssz.deneb.BlobIdentifier, config.getMaxRequestBlobSidecars(fork));
export type BlobSidecarsByRootRequest = ValueOf<ReturnType<typeof BlobSidecarsByRootRequestType>>;
3 changes: 3 additions & 0 deletions packages/config/src/forkConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,8 @@ export function createForkConfig(config: ChainConfig): ForkConfig {
getMaxBlobsPerBlock(fork: ForkName): number {
return isForkPostElectra(fork) ? config.MAX_BLOBS_PER_BLOCK_ELECTRA : config.MAX_BLOBS_PER_BLOCK;
},
getMaxRequestBlobSidecars(fork: ForkName): number {
return isForkPostElectra(fork) ? config.MAX_REQUEST_BLOB_SIDECARS_ELECTRA : config.MAX_REQUEST_BLOB_SIDECARS;
},
};
}
2 changes: 2 additions & 0 deletions packages/config/src/forkConfig/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ export type ForkConfig = {
getBlobsForkTypes(slot: Slot): SSZTypesFor<ForkBlobs>;
/** Get max blobs per block by hard-fork */
getMaxBlobsPerBlock(fork: ForkName): number;
/** Get max request blob sidecars by hard-fork */
getMaxRequestBlobSidecars(fork: ForkName): number;
};

0 comments on commit eb2ee60

Please sign in to comment.