From 40b51d2acebbf04b5bba6e5cd0640af41248551b Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Thu, 16 Jan 2025 17:14:30 +0000 Subject: [PATCH] Add a more generic getter to fork config --- .../src/chain/validation/blobSidecar.ts | 2 +- .../beacon-node/src/chain/validation/block.ts | 2 +- packages/beacon-node/src/network/network.ts | 2 +- .../reqresp/handlers/blobSidecarsByRange.ts | 2 +- packages/beacon-node/src/util/types.ts | 2 +- packages/config/src/forkConfig/index.ts | 16 ++++++++++------ packages/config/src/forkConfig/types.ts | 12 ++++++++---- .../src/block/processExecutionPayload.ts | 2 +- 8 files changed, 24 insertions(+), 16 deletions(-) diff --git a/packages/beacon-node/src/chain/validation/blobSidecar.ts b/packages/beacon-node/src/chain/validation/blobSidecar.ts index 37fa3067b80..53e464d7070 100644 --- a/packages/beacon-node/src/chain/validation/blobSidecar.ts +++ b/packages/beacon-node/src/chain/validation/blobSidecar.ts @@ -25,7 +25,7 @@ export async function validateGossipBlobSidecar( const blobSlot = blobSidecar.signedBlockHeader.message.slot; // [REJECT] The sidecar's index is consistent with `MAX_BLOBS_PER_BLOCK` -- i.e. `blob_sidecar.index < MAX_BLOBS_PER_BLOCK`. - const maxBlobsPerBlock = chain.config.getMaxBlobsPerBlock(fork); + const maxBlobsPerBlock = chain.config.getValue(fork, "MAX_BLOBS_PER_BLOCK"); if (blobSidecar.index >= maxBlobsPerBlock) { throw new BlobSidecarGossipError(GossipAction.REJECT, { code: BlobSidecarErrorCode.INDEX_TOO_LARGE, diff --git a/packages/beacon-node/src/chain/validation/block.ts b/packages/beacon-node/src/chain/validation/block.ts index 2b18999db40..f1a9fdd601c 100644 --- a/packages/beacon-node/src/chain/validation/block.ts +++ b/packages/beacon-node/src/chain/validation/block.ts @@ -113,7 +113,7 @@ export async function validateGossipBlock( // [REJECT] The length of KZG commitments is less than or equal to the limitation defined in Consensus Layer -- i.e. validate that len(body.signed_beacon_block.message.blob_kzg_commitments) <= MAX_BLOBS_PER_BLOCK if (isForkBlobs(fork)) { const blobKzgCommitmentsLen = (block as deneb.BeaconBlock).body.blobKzgCommitments.length; - const maxBlobsPerBlock = chain.config.getMaxBlobsPerBlock(fork); + const maxBlobsPerBlock = chain.config.getValue(fork, "MAX_BLOBS_PER_BLOCK"); if (blobKzgCommitmentsLen > maxBlobsPerBlock) { throw new BlockGossipError(GossipAction.REJECT, { code: BlockErrorCode.TOO_MANY_KZG_COMMITMENTS, diff --git a/packages/beacon-node/src/network/network.ts b/packages/beacon-node/src/network/network.ts index 505da6718c3..5c488d30ccf 100644 --- a/packages/beacon-node/src/network/network.ts +++ b/packages/beacon-node/src/network/network.ts @@ -506,7 +506,7 @@ export class Network implements INetwork { return collectMaxResponseTyped( this.sendReqRespRequest(peerId, ReqRespMethod.BlobSidecarsByRange, [Version.V1], request), // request's count represent the slots, so the actual max count received could be slots * blobs per slot - request.count * this.config.getMaxBlobsPerBlock(fork), + request.count * this.config.getValue(fork, "MAX_BLOBS_PER_BLOCK"), responseSszTypeByMethod[ReqRespMethod.BlobSidecarsByRange] ); } diff --git a/packages/beacon-node/src/network/reqresp/handlers/blobSidecarsByRange.ts b/packages/beacon-node/src/network/reqresp/handlers/blobSidecarsByRange.ts index 557d01a2284..a967a472934 100644 --- a/packages/beacon-node/src/network/reqresp/handlers/blobSidecarsByRange.ts +++ b/packages/beacon-node/src/network/reqresp/handlers/blobSidecarsByRange.ts @@ -105,7 +105,7 @@ export function validateBlobSidecarsByRangeRequest( throw new ResponseError(RespStatus.INVALID_REQUEST, "startSlot < genesis"); } - const maxRequestBlobSidecars = config.getMaxRequestBlobSidecars(config.getForkName(startSlot)); + const maxRequestBlobSidecars = config.getValue(config.getForkName(startSlot), "MAX_REQUEST_BLOB_SIDECARS"); if (count > maxRequestBlobSidecars) { count = maxRequestBlobSidecars; diff --git a/packages/beacon-node/src/util/types.ts b/packages/beacon-node/src/util/types.ts index 4133d6db102..d918c89e73a 100644 --- a/packages/beacon-node/src/util/types.ts +++ b/packages/beacon-node/src/util/types.ts @@ -16,5 +16,5 @@ export const signedBLSToExecutionChangeVersionedType = new ContainerType( export type SignedBLSToExecutionChangeVersioned = ValueOf; export const BlobSidecarsByRootRequestType = (fork: ForkName, config: BeaconConfig) => - new ListCompositeType(ssz.deneb.BlobIdentifier, config.getMaxRequestBlobSidecars(fork)); + new ListCompositeType(ssz.deneb.BlobIdentifier, config.getValue(fork, "MAX_REQUEST_BLOB_SIDECARS")); export type BlobSidecarsByRootRequest = ValueOf>; diff --git a/packages/config/src/forkConfig/index.ts b/packages/config/src/forkConfig/index.ts index 9551627188d..78e4ba615bf 100644 --- a/packages/config/src/forkConfig/index.ts +++ b/packages/config/src/forkConfig/index.ts @@ -14,7 +14,7 @@ import { } from "@lodestar/params"; import {Epoch, SSZTypesFor, Slot, Version, sszTypesFor} from "@lodestar/types"; import {ChainConfig} from "../chainConfig/index.js"; -import {ForkConfig, ForkInfo} from "./types.js"; +import {ConfigValue, ForkConfig, ForkInfo} from "./types.js"; export * from "./types.js"; @@ -130,11 +130,15 @@ export function createForkConfig(config: ChainConfig): ForkConfig { } return sszTypesFor(forkName); }, - 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; + getValue(fork: ForkName, name: K): ConfigValue[K] { + switch (name) { + case "MAX_BLOBS_PER_BLOCK": + return isForkPostElectra(fork) ? config.MAX_BLOBS_PER_BLOCK_ELECTRA : config.MAX_BLOBS_PER_BLOCK; + case "MAX_REQUEST_BLOB_SIDECARS": + return isForkPostElectra(fork) ? config.MAX_REQUEST_BLOB_SIDECARS_ELECTRA : config.MAX_REQUEST_BLOB_SIDECARS; + default: + throw Error(`Config value "${name}" does not exist`); + } }, }; } diff --git a/packages/config/src/forkConfig/types.ts b/packages/config/src/forkConfig/types.ts index c7874482770..590f14ec4e5 100644 --- a/packages/config/src/forkConfig/types.ts +++ b/packages/config/src/forkConfig/types.ts @@ -10,6 +10,12 @@ export type ForkInfo = { prevForkName: ForkName; }; +/** Set of config values that frequently change across hard-forks */ +export type ConfigValue = { + MAX_BLOBS_PER_BLOCK: number; + MAX_REQUEST_BLOB_SIDECARS: number; +}; + /** * Fork schedule and helper methods */ @@ -39,8 +45,6 @@ export type ForkConfig = { getExecutionForkTypes(slot: Slot): SSZTypesFor; /** Get blobs SSZ types by hard-fork*/ getBlobsForkTypes(slot: Slot): SSZTypesFor; - /** Get max blobs per block by hard-fork */ - getMaxBlobsPerBlock(fork: ForkName): number; - /** Get max request blob sidecars by hard-fork */ - getMaxRequestBlobSidecars(fork: ForkName): number; + /** Get config value by hard-fork */ + getValue(fork: ForkName, name: K): ConfigValue[K]; }; diff --git a/packages/state-transition/src/block/processExecutionPayload.ts b/packages/state-transition/src/block/processExecutionPayload.ts index ddc24e884d9..c267c2f4e67 100644 --- a/packages/state-transition/src/block/processExecutionPayload.ts +++ b/packages/state-transition/src/block/processExecutionPayload.ts @@ -49,7 +49,7 @@ export function processExecutionPayload( } if (isForkBlobs(forkName)) { - const maxBlobsPerBlock = state.config.getMaxBlobsPerBlock(forkName); + const maxBlobsPerBlock = state.config.getValue(forkName, "MAX_BLOBS_PER_BLOCK"); const blobKzgCommitmentsLen = (body as deneb.BeaconBlockBody).blobKzgCommitments?.length ?? 0; if (blobKzgCommitmentsLen > maxBlobsPerBlock) { throw Error(`blobKzgCommitmentsLen exceeds limit=${maxBlobsPerBlock}`);