From 13d6448d116a3e95a47865a48328366a9c943b82 Mon Sep 17 00:00:00 2001 From: NC <17676176+ensi321@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:50:03 -0800 Subject: [PATCH 01/21] Init --- packages/config/src/chainConfig/configs/mainnet.ts | 6 +++++- packages/config/src/chainConfig/configs/minimal.ts | 4 ++++ packages/config/src/chainConfig/types.ts | 6 ++++++ packages/params/src/presets/minimal.ts | 4 ++-- .../state-transition/src/block/processExecutionPayload.ts | 6 ++++-- packages/validator/src/util/params.ts | 3 +++ 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/config/src/chainConfig/configs/mainnet.ts b/packages/config/src/chainConfig/configs/mainnet.ts index a4adc04a175..7b332f94b1a 100644 --- a/packages/config/src/chainConfig/configs/mainnet.ts +++ b/packages/config/src/chainConfig/configs/mainnet.ts @@ -109,6 +109,10 @@ export const chainConfig: ChainConfig = { // Electra // 2**8 * 10**9 (= 256,000,000,000) MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 256000000000, - // 2*7 * 10**9 (= 128,000,000,000) + // 2**7 * 10**9 (= 128,000,000,000) MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA: 128000000000, + BLOB_SIDECAR_SUBNET_COUNT_ELECTRA: 9, + MAX_BLOBS_PER_BLOCK_ELECTRA: 9, + // MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK_ELECTRA + MAX_REQUEST_BLOB_SIDECARS_ELECTRA: 1152, }; diff --git a/packages/config/src/chainConfig/configs/minimal.ts b/packages/config/src/chainConfig/configs/minimal.ts index d16b03e82c2..0902742277e 100644 --- a/packages/config/src/chainConfig/configs/minimal.ts +++ b/packages/config/src/chainConfig/configs/minimal.ts @@ -108,4 +108,8 @@ export const chainConfig: ChainConfig = { MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 128000000000, // 2**6 * 10**9 (= 64,000,000,000) MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA: 64000000000, + BLOB_SIDECAR_SUBNET_COUNT_ELECTRA: 9, + MAX_BLOBS_PER_BLOCK_ELECTRA: 9, + // MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK_ELECTRA + MAX_REQUEST_BLOB_SIDECARS_ELECTRA: 1152, }; diff --git a/packages/config/src/chainConfig/types.ts b/packages/config/src/chainConfig/types.ts index 291dcc8601a..8ca338c89a2 100644 --- a/packages/config/src/chainConfig/types.ts +++ b/packages/config/src/chainConfig/types.ts @@ -75,6 +75,9 @@ export type ChainConfig = { BLOB_SIDECAR_SUBNET_COUNT: number; MAX_BLOBS_PER_BLOCK: number; MAX_REQUEST_BLOB_SIDECARS: number; + BLOB_SIDECAR_SUBNET_COUNT_ELECTRA: number; + MAX_BLOBS_PER_BLOCK_ELECTRA: number; + MAX_REQUEST_BLOB_SIDECARS_ELECTRA: number; }; export const chainConfigTypes: SpecTypes = { @@ -142,6 +145,9 @@ export const chainConfigTypes: SpecTypes = { BLOB_SIDECAR_SUBNET_COUNT: "number", MAX_BLOBS_PER_BLOCK: "number", MAX_REQUEST_BLOB_SIDECARS: "number", + BLOB_SIDECAR_SUBNET_COUNT_ELECTRA: "number", + MAX_BLOBS_PER_BLOCK_ELECTRA: "number", + MAX_REQUEST_BLOB_SIDECARS_ELECTRA: "number", }; /** Allows values in a Spec file */ diff --git a/packages/params/src/presets/minimal.ts b/packages/params/src/presets/minimal.ts index 4c2a56a11d8..4595ee9ee87 100644 --- a/packages/params/src/presets/minimal.ts +++ b/packages/params/src/presets/minimal.ts @@ -115,8 +115,8 @@ export const minimalPreset: BeaconPreset = { // DENEB /////////// FIELD_ELEMENTS_PER_BLOB: 4096, - MAX_BLOB_COMMITMENTS_PER_BLOCK: 16, - KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: 9, + MAX_BLOB_COMMITMENTS_PER_BLOCK: 32, + KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: 10, // ELECTRA MAX_DEPOSIT_REQUESTS_PER_PAYLOAD: 4, diff --git a/packages/state-transition/src/block/processExecutionPayload.ts b/packages/state-transition/src/block/processExecutionPayload.ts index 0ea2fc7a16f..628c5b28831 100644 --- a/packages/state-transition/src/block/processExecutionPayload.ts +++ b/packages/state-transition/src/block/processExecutionPayload.ts @@ -48,9 +48,11 @@ export function processExecutionPayload( } if (fork >= ForkSeq.deneb) { + const maxBlobsPerBlock = + fork >= ForkSeq.electra ? state.config.MAX_BLOBS_PER_BLOCK_ELECTRA : state.config.MAX_BLOBS_PER_BLOCK; const blobKzgCommitmentsLen = (body as deneb.BeaconBlockBody).blobKzgCommitments?.length ?? 0; - if (blobKzgCommitmentsLen > state.config.MAX_BLOBS_PER_BLOCK) { - throw Error(`blobKzgCommitmentsLen exceeds limit=${state.config.MAX_BLOBS_PER_BLOCK}`); + if (blobKzgCommitmentsLen > maxBlobsPerBlock) { + throw Error(`blobKzgCommitmentsLen exceeds limit=${maxBlobsPerBlock}`); } } diff --git a/packages/validator/src/util/params.ts b/packages/validator/src/util/params.ts index 825f60e8c7f..ea19bb3a90c 100644 --- a/packages/validator/src/util/params.ts +++ b/packages/validator/src/util/params.ts @@ -138,6 +138,7 @@ function getSpecCriticalParams(localConfig: ChainConfig): Record Date: Mon, 16 Dec 2024 17:20:27 -0800 Subject: [PATCH 02/21] Add reqresp v2 definition --- .../src/network/reqresp/ReqRespBeaconNode.ts | 7 +++++++ .../beacon-node/src/network/reqresp/protocols.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts b/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts index 96b5c6c0a77..83c6f40bc2e 100644 --- a/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts +++ b/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts @@ -257,6 +257,13 @@ export class ReqRespBeaconNode extends ReqResp { ); } + if (ForkSeq[fork] >= ForkSeq.electra) { + protocolsAtFork.push( + [protocols.BlobSidecarsByRootV2(this.config), this.getHandler(ReqRespMethod.BlobSidecarsByRoot)], + [protocols.BlobSidecarsByRangeV2(this.config), this.getHandler(ReqRespMethod.BlobSidecarsByRange)] + ); + } + return protocolsAtFork; } diff --git a/packages/beacon-node/src/network/reqresp/protocols.ts b/packages/beacon-node/src/network/reqresp/protocols.ts index b254db02210..c84b1669578 100644 --- a/packages/beacon-node/src/network/reqresp/protocols.ts +++ b/packages/beacon-node/src/network/reqresp/protocols.ts @@ -63,12 +63,24 @@ export const BlobSidecarsByRange = toProtocol({ contextBytesType: ContextBytesType.ForkDigest, }); +export const BlobSidecarsByRangeV2 = toProtocol({ + method: ReqRespMethod.BlobSidecarsByRange, + version: Version.V2, + contextBytesType: ContextBytesType.ForkDigest, +}); + export const BlobSidecarsByRoot = toProtocol({ method: ReqRespMethod.BlobSidecarsByRoot, version: Version.V1, contextBytesType: ContextBytesType.ForkDigest, }); +export const BlobSidecarsByRootV2 = toProtocol({ + method: ReqRespMethod.BlobSidecarsByRoot, + version: Version.V2, + contextBytesType: ContextBytesType.ForkDigest, +}); + export const LightClientBootstrap = toProtocol({ method: ReqRespMethod.LightClientBootstrap, version: Version.V1, From e6984f41d5e3f6ef2bc2f778da095aac0c0061cb Mon Sep 17 00:00:00 2001 From: NC <17676176+ensi321@users.noreply.github.com> Date: Mon, 16 Dec 2024 22:25:02 -0800 Subject: [PATCH 03/21] Update validateGossipBlock --- packages/beacon-node/src/chain/validation/block.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/beacon-node/src/chain/validation/block.ts b/packages/beacon-node/src/chain/validation/block.ts index b2623aa4f79..921319439fd 100644 --- a/packages/beacon-node/src/chain/validation/block.ts +++ b/packages/beacon-node/src/chain/validation/block.ts @@ -1,5 +1,5 @@ import {ChainForkConfig} from "@lodestar/config"; -import {ForkName, isForkBlobs} from "@lodestar/params"; +import {ForkName, isForkBlobs, isForkPostElectra} from "@lodestar/params"; import { computeStartSlotAtEpoch, computeTimeAtSlot, @@ -113,7 +113,10 @@ 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; - if (blobKzgCommitmentsLen > chain.config.MAX_BLOBS_PER_BLOCK) { + const maxBlobsPerBlock = isForkPostElectra(fork) + ? chain.config.MAX_BLOBS_PER_BLOCK_ELECTRA + : chain.config.MAX_BLOBS_PER_BLOCK; + if (blobKzgCommitmentsLen > maxBlobsPerBlock) { throw new BlockGossipError(GossipAction.REJECT, { code: BlockErrorCode.TOO_MANY_KZG_COMMITMENTS, blobKzgCommitmentsLen, From 6b485889d173c8bc065f1b8bf3539ae9cfdb216b Mon Sep 17 00:00:00 2001 From: NC <17676176+ensi321@users.noreply.github.com> Date: Mon, 16 Dec 2024 22:25:30 -0800 Subject: [PATCH 04/21] Partial commit --- packages/beacon-node/src/network/network.ts | 2 +- .../src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts | 4 ++-- packages/beacon-node/src/network/reqresp/handlers/index.ts | 4 ++-- packages/beacon-node/src/network/reqresp/rateLimit.ts | 2 +- packages/beacon-node/src/network/reqresp/types.ts | 2 +- packages/beacon-node/src/util/types.ts | 5 +++-- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/beacon-node/src/network/network.ts b/packages/beacon-node/src/network/network.ts index 2faf2371e5c..56fcde7630a 100644 --- a/packages/beacon-node/src/network/network.ts +++ b/packages/beacon-node/src/network/network.ts @@ -511,7 +511,7 @@ export class Network implements INetwork { async sendBlobSidecarsByRoot(peerId: PeerIdStr, request: BlobSidecarsByRootRequest): Promise { return collectMaxResponseTyped( - this.sendReqRespRequest(peerId, ReqRespMethod.BlobSidecarsByRoot, [Version.V1], request), + this.sendReqRespRequest(peerId, ReqRespMethod.BlobSidecarsByRoot, [Version.V1], request), // TODO request.length, responseSszTypeByMethod[ReqRespMethod.BlobSidecarsByRoot] ); diff --git a/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts b/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts index 3bbe00bfc56..00a2744b44d 100644 --- a/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts +++ b/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts @@ -52,7 +52,7 @@ export async function beaconBlocksMaybeBlobsByRoot( let allBlobSidecars: deneb.BlobSidecar[]; if (blobIdentifiers.length > 0) { - allBlobSidecars = await network.sendBlobSidecarsByRoot(peerId, blobIdentifiers); + allBlobSidecars = await network.sendBlobSidecarsByRoot(peerId, blobIdentifiers); // TODO } else { allBlobSidecars = []; } @@ -228,7 +228,7 @@ export async function unavailableBeaconBlobsByRoot( } if (networkReqIdentifiers.length > 0) { - networkResBlobSidecars = await network.sendBlobSidecarsByRoot(peerId, networkReqIdentifiers); + networkResBlobSidecars = await network.sendBlobSidecarsByRoot(peerId, networkReqIdentifiers); // TODO metrics?.blockInputFetchStats.dataPromiseBlobsFinallyAvailableFromNetwork.inc(networkResBlobSidecars.length); if (blockTriedBefore) { metrics?.blockInputFetchStats.dataPromiseBlobsRetriedAvailableFromNetwork.inc(networkResBlobSidecars.length); diff --git a/packages/beacon-node/src/network/reqresp/handlers/index.ts b/packages/beacon-node/src/network/reqresp/handlers/index.ts index 83f6620dbbd..8567915c7fb 100644 --- a/packages/beacon-node/src/network/reqresp/handlers/index.ts +++ b/packages/beacon-node/src/network/reqresp/handlers/index.ts @@ -38,8 +38,8 @@ export function getReqRespHandlers({db, chain}: {db: IBeaconDb; chain: IBeaconCh return onBeaconBlocksByRoot(body, chain, db); }, [ReqRespMethod.BlobSidecarsByRoot]: (req) => { - const body = BlobSidecarsByRootRequestType(chain.config).deserialize(req.data); - return onBlobSidecarsByRoot(body, chain, db); + const body = BlobSidecarsByRootRequestType(fork, chain.config).deserialize(req.data); + return onBlobSidecarsByRoot(body, chain, db) }, [ReqRespMethod.BlobSidecarsByRange]: (req) => { const body = ssz.deneb.BlobSidecarsByRangeRequest.deserialize(req.data); diff --git a/packages/beacon-node/src/network/reqresp/rateLimit.ts b/packages/beacon-node/src/network/reqresp/rateLimit.ts index 771d01f6c33..a875d332b52 100644 --- a/packages/beacon-node/src/network/reqresp/rateLimit.ts +++ b/packages/beacon-node/src/network/reqresp/rateLimit.ts @@ -39,7 +39,7 @@ export const rateLimitQuotas: (config: ChainConfig) => Record req.length), }, [ReqRespMethod.LightClientBootstrap]: { diff --git a/packages/beacon-node/src/network/reqresp/types.ts b/packages/beacon-node/src/network/reqresp/types.ts index b7c18ebdfeb..f2178ce7a03 100644 --- a/packages/beacon-node/src/network/reqresp/types.ts +++ b/packages/beacon-node/src/network/reqresp/types.ts @@ -80,7 +80,7 @@ export const requestSszTypeByMethod: (config: ChainConfig) => { [ReqRespMethod.BeaconBlocksByRange]: ssz.phase0.BeaconBlocksByRangeRequest, [ReqRespMethod.BeaconBlocksByRoot]: ssz.phase0.BeaconBlocksByRootRequest, [ReqRespMethod.BlobSidecarsByRange]: ssz.deneb.BlobSidecarsByRangeRequest, - [ReqRespMethod.BlobSidecarsByRoot]: BlobSidecarsByRootRequestType(config), + [ReqRespMethod.BlobSidecarsByRoot]: BlobSidecarsByRootRequestType(fork, config), [ReqRespMethod.LightClientBootstrap]: ssz.Root, [ReqRespMethod.LightClientUpdatesByRange]: ssz.altair.LightClientUpdatesByRange, [ReqRespMethod.LightClientFinalityUpdate]: null, diff --git a/packages/beacon-node/src/util/types.ts b/packages/beacon-node/src/util/types.ts index 5b9c7a78427..d022125991d 100644 --- a/packages/beacon-node/src/util/types.ts +++ b/packages/beacon-node/src/util/types.ts @@ -1,5 +1,6 @@ import {ContainerType, ListCompositeType, ValueOf} from "@chainsafe/ssz"; import {ChainConfig} from "@lodestar/config"; +import { ForkName, isForkPostElectra } from "@lodestar/params"; import {ssz} from "@lodestar/types"; // Misc SSZ types used only in the beacon-node package, no need to upstream to types @@ -14,6 +15,6 @@ export const signedBLSToExecutionChangeVersionedType = new ContainerType( ); export type SignedBLSToExecutionChangeVersioned = ValueOf; -export const BlobSidecarsByRootRequestType = (config: ChainConfig) => - new ListCompositeType(ssz.deneb.BlobIdentifier, config.MAX_REQUEST_BLOB_SIDECARS); +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); // TODO export type BlobSidecarsByRootRequest = ValueOf>; From 172297155f956673f3c7abbd008bea236f7b5256 Mon Sep 17 00:00:00 2001 From: NC <17676176+ensi321@users.noreply.github.com> Date: Tue, 17 Dec 2024 00:30:49 -0800 Subject: [PATCH 05/21] Reqresp. Add todos --- packages/beacon-node/src/network/network.ts | 21 +++++++++++++++---- .../src/network/reqresp/ReqRespBeaconNode.ts | 1 + .../reqresp/beaconBlocksMaybeBlobsByRoot.ts | 2 +- .../reqresp/handlers/beaconBlocksByRange.ts | 1 + .../src/network/reqresp/handlers/index.ts | 6 ++++-- .../src/network/reqresp/rateLimit.ts | 4 +++- .../beacon-node/src/network/reqresp/types.ts | 3 ++- packages/beacon-node/src/util/types.ts | 7 +++++-- 8 files changed, 34 insertions(+), 11 deletions(-) diff --git a/packages/beacon-node/src/network/network.ts b/packages/beacon-node/src/network/network.ts index 56fcde7630a..d1de2d4109a 100644 --- a/packages/beacon-node/src/network/network.ts +++ b/packages/beacon-node/src/network/network.ts @@ -4,7 +4,7 @@ import {PeerId} from "@libp2p/interface"; import {routes} from "@lodestar/api"; import {BeaconConfig} from "@lodestar/config"; import {LoggerNode} from "@lodestar/logger/node"; -import {ForkSeq} from "@lodestar/params"; +import {ForkName, ForkSeq, isForkPostElectra} from "@lodestar/params"; import {ResponseIncoming} from "@lodestar/reqresp"; import {computeStartSlotAtEpoch, computeTimeAtSlot} from "@lodestar/state-transition"; import { @@ -501,17 +501,30 @@ export class Network implements INetwork { peerId: PeerIdStr, request: deneb.BlobSidecarsByRangeRequest ): Promise { + const fork = this.config.getForkName(this.clock.currentSlot); return collectMaxResponseTyped( - this.sendReqRespRequest(peerId, ReqRespMethod.BlobSidecarsByRange, [Version.V1], request), + this.sendReqRespRequest( + peerId, + ReqRespMethod.BlobSidecarsByRange, + [isForkPostElectra(fork) ? Version.V2 : 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.MAX_BLOBS_PER_BLOCK, + request.count * + (isForkPostElectra(fork) ? this.config.MAX_BLOBS_PER_BLOCK_ELECTRA : this.config.MAX_BLOBS_PER_BLOCK), responseSszTypeByMethod[ReqRespMethod.BlobSidecarsByRange] ); } async sendBlobSidecarsByRoot(peerId: PeerIdStr, request: BlobSidecarsByRootRequest): Promise { + const fork = this.config.getForkName(this.clock.currentSlot); return collectMaxResponseTyped( - this.sendReqRespRequest(peerId, ReqRespMethod.BlobSidecarsByRoot, [Version.V1], request), // TODO + this.sendReqRespRequest( + peerId, + ReqRespMethod.BlobSidecarsByRoot, + [isForkPostElectra(fork) ? Version.V2 : Version.V1], + request + ), request.length, responseSszTypeByMethod[ReqRespMethod.BlobSidecarsByRoot] ); diff --git a/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts b/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts index 83c6f40bc2e..3bca2cee609 100644 --- a/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts +++ b/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts @@ -251,6 +251,7 @@ export class ReqRespBeaconNode extends ReqResp { } if (ForkSeq[fork] >= ForkSeq.deneb) { + // TODO Electra: Consider deprecating BlobSidecarsByRootV1 and BlobSidecarsByRangeV1 at fork boundary or after Electra is stable protocolsAtFork.push( [protocols.BlobSidecarsByRoot(this.config), this.getHandler(ReqRespMethod.BlobSidecarsByRoot)], [protocols.BlobSidecarsByRange(this.config), this.getHandler(ReqRespMethod.BlobSidecarsByRange)] diff --git a/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts b/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts index 00a2744b44d..df24d0473b7 100644 --- a/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts +++ b/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts @@ -228,7 +228,7 @@ export async function unavailableBeaconBlobsByRoot( } if (networkReqIdentifiers.length > 0) { - networkResBlobSidecars = await network.sendBlobSidecarsByRoot(peerId, networkReqIdentifiers); // TODO + networkResBlobSidecars = await network.sendBlobSidecarsByRoot(peerId, networkReqIdentifiers); metrics?.blockInputFetchStats.dataPromiseBlobsFinallyAvailableFromNetwork.inc(networkResBlobSidecars.length); if (blockTriedBefore) { metrics?.blockInputFetchStats.dataPromiseBlobsRetriedAvailableFromNetwork.inc(networkResBlobSidecars.length); diff --git a/packages/beacon-node/src/network/reqresp/handlers/beaconBlocksByRange.ts b/packages/beacon-node/src/network/reqresp/handlers/beaconBlocksByRange.ts index e8c19fb4962..620b98be63d 100644 --- a/packages/beacon-node/src/network/reqresp/handlers/beaconBlocksByRange.ts +++ b/packages/beacon-node/src/network/reqresp/handlers/beaconBlocksByRange.ts @@ -85,6 +85,7 @@ 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; } diff --git a/packages/beacon-node/src/network/reqresp/handlers/index.ts b/packages/beacon-node/src/network/reqresp/handlers/index.ts index 8567915c7fb..49b552b5817 100644 --- a/packages/beacon-node/src/network/reqresp/handlers/index.ts +++ b/packages/beacon-node/src/network/reqresp/handlers/index.ts @@ -1,9 +1,10 @@ +import {ForkName} from "@lodestar/params"; import {ProtocolHandler} from "@lodestar/reqresp"; import {ssz} from "@lodestar/types"; import {IBeaconChain} from "../../../chain/index.js"; import {IBeaconDb} from "../../../db/index.js"; import {BlobSidecarsByRootRequestType} from "../../../util/types.js"; -import {GetReqRespHandlerFn, ReqRespMethod} from "../types.js"; +import {GetReqRespHandlerFn, ReqRespMethod, Version} from "../types.js"; import {onBeaconBlocksByRange} from "./beaconBlocksByRange.js"; import {onBeaconBlocksByRoot} from "./beaconBlocksByRoot.js"; import {onBlobSidecarsByRange} from "./blobSidecarsByRange.js"; @@ -38,8 +39,9 @@ export function getReqRespHandlers({db, chain}: {db: IBeaconDb; chain: IBeaconCh return onBeaconBlocksByRoot(body, chain, db); }, [ReqRespMethod.BlobSidecarsByRoot]: (req) => { + const fork = req.version === Version.V2 ? ForkName.electra : ForkName.deneb; const body = BlobSidecarsByRootRequestType(fork, chain.config).deserialize(req.data); - return onBlobSidecarsByRoot(body, chain, db) + return onBlobSidecarsByRoot(body, chain, db); }, [ReqRespMethod.BlobSidecarsByRange]: (req) => { const body = ssz.deneb.BlobSidecarsByRangeRequest.deserialize(req.data); diff --git a/packages/beacon-node/src/network/reqresp/rateLimit.ts b/packages/beacon-node/src/network/reqresp/rateLimit.ts index a875d332b52..ce8a996a49a 100644 --- a/packages/beacon-node/src/network/reqresp/rateLimit.ts +++ b/packages/beacon-node/src/network/reqresp/rateLimit.ts @@ -34,12 +34,14 @@ export const rateLimitQuotas: (config: ChainConfig) => Record req.count), }, [ReqRespMethod.BlobSidecarsByRoot]: { // Rationale: quota of BeaconBlocksByRoot * MAX_BLOBS_PER_BLOCK - byPeer: {quota: config.MAX_REQUEST_BLOB_SIDECARS, quotaTimeMs: 10_000}, // TODO + // TODO Electra: Stays as `MAX_REQUEST_BLOB_SIDECARS` until we have fork-aware `byPeer` and set it to `MAX_REQUEST_BLOB_SIDECARS_ELECTRA` + byPeer: {quota: config.MAX_REQUEST_BLOB_SIDECARS, quotaTimeMs: 10_000}, getRequestCount: getRequestCountFn(config, ReqRespMethod.BlobSidecarsByRoot, (req) => req.length), }, [ReqRespMethod.LightClientBootstrap]: { diff --git a/packages/beacon-node/src/network/reqresp/types.ts b/packages/beacon-node/src/network/reqresp/types.ts index f2178ce7a03..392d4d0d1dd 100644 --- a/packages/beacon-node/src/network/reqresp/types.ts +++ b/packages/beacon-node/src/network/reqresp/types.ts @@ -70,6 +70,7 @@ type ResponseBodyByMethod = { }; /** Request SSZ type for each method and ForkName */ +// TODO Electra: Make requestSszTypeByMethod fork-aware so we can get the correct BlobSidecarsByRootRequestType export const requestSszTypeByMethod: (config: ChainConfig) => { [K in ReqRespMethod]: RequestBodyByMethod[K] extends null ? null : Type; } = (config) => ({ @@ -80,7 +81,7 @@ export const requestSszTypeByMethod: (config: ChainConfig) => { [ReqRespMethod.BeaconBlocksByRange]: ssz.phase0.BeaconBlocksByRangeRequest, [ReqRespMethod.BeaconBlocksByRoot]: ssz.phase0.BeaconBlocksByRootRequest, [ReqRespMethod.BlobSidecarsByRange]: ssz.deneb.BlobSidecarsByRangeRequest, - [ReqRespMethod.BlobSidecarsByRoot]: BlobSidecarsByRootRequestType(fork, config), + [ReqRespMethod.BlobSidecarsByRoot]: BlobSidecarsByRootRequestType(ForkName.deneb, config), [ReqRespMethod.LightClientBootstrap]: ssz.Root, [ReqRespMethod.LightClientUpdatesByRange]: ssz.altair.LightClientUpdatesByRange, [ReqRespMethod.LightClientFinalityUpdate]: null, diff --git a/packages/beacon-node/src/util/types.ts b/packages/beacon-node/src/util/types.ts index d022125991d..2a99fc34bcc 100644 --- a/packages/beacon-node/src/util/types.ts +++ b/packages/beacon-node/src/util/types.ts @@ -1,6 +1,6 @@ import {ContainerType, ListCompositeType, ValueOf} from "@chainsafe/ssz"; import {ChainConfig} from "@lodestar/config"; -import { ForkName, isForkPostElectra } from "@lodestar/params"; +import {ForkName, isForkPostElectra} from "@lodestar/params"; import {ssz} from "@lodestar/types"; // Misc SSZ types used only in the beacon-node package, no need to upstream to types @@ -16,5 +16,8 @@ export const signedBLSToExecutionChangeVersionedType = new ContainerType( export type SignedBLSToExecutionChangeVersioned = ValueOf; 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); // TODO + new ListCompositeType( + ssz.deneb.BlobIdentifier, + isForkPostElectra(fork) ? config.MAX_REQUEST_BLOB_SIDECARS_ELECTRA : config.MAX_REQUEST_BLOB_SIDECARS + ); export type BlobSidecarsByRootRequest = ValueOf>; From f031dc35ab2c73e64acde87efc2a49466d3bac78 Mon Sep 17 00:00:00 2001 From: NC <17676176+ensi321@users.noreply.github.com> Date: Tue, 17 Dec 2024 00:39:12 -0800 Subject: [PATCH 06/21] polish --- .../src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts b/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts index df24d0473b7..3bbe00bfc56 100644 --- a/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts +++ b/packages/beacon-node/src/network/reqresp/beaconBlocksMaybeBlobsByRoot.ts @@ -52,7 +52,7 @@ export async function beaconBlocksMaybeBlobsByRoot( let allBlobSidecars: deneb.BlobSidecar[]; if (blobIdentifiers.length > 0) { - allBlobSidecars = await network.sendBlobSidecarsByRoot(peerId, blobIdentifiers); // TODO + allBlobSidecars = await network.sendBlobSidecarsByRoot(peerId, blobIdentifiers); } else { allBlobSidecars = []; } From a8f34b89ad57cd31f87de0bf3e1239c1a71025a8 Mon Sep 17 00:00:00 2001 From: NC <17676176+ensi321@users.noreply.github.com> Date: Tue, 17 Dec 2024 22:31:51 -0800 Subject: [PATCH 07/21] Fork-aware requestSszTypeByMethod --- packages/beacon-node/src/network/network.ts | 3 ++- .../src/network/reqresp/ReqRespBeaconNode.ts | 3 ++- packages/beacon-node/src/network/reqresp/types.ts | 11 +++++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/beacon-node/src/network/network.ts b/packages/beacon-node/src/network/network.ts index d1de2d4109a..1d4bad2b832 100644 --- a/packages/beacon-node/src/network/network.ts +++ b/packages/beacon-node/src/network/network.ts @@ -536,7 +536,8 @@ export class Network implements INetwork { versions: number[], request: Req ): AsyncIterable { - const requestType = requestSszTypeByMethod(this.config)[method]; + const fork = this.config.getForkName(this.clock.currentSlot); + const requestType = requestSszTypeByMethod(this.config, fork)[method]; const requestData = requestType ? requestType.serialize(request as never) : new Uint8Array(); // ReqResp outgoing request, emit from main thread to worker diff --git a/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts b/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts index 3bca2cee609..1098f0de145 100644 --- a/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts +++ b/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts @@ -207,7 +207,8 @@ export class ReqRespBeaconNode extends ReqResp { versions: number[], request: Req ): AsyncIterable { - const requestType = requestSszTypeByMethod(this.config)[method]; + const fork = ForkName[ForkSeq[this.currentRegisteredFork] as keyof typeof ForkName]; + const requestType = requestSszTypeByMethod(this.config, fork)[method]; const requestData = requestType ? requestType.serialize(request as never) : new Uint8Array(); return this.sendRequestWithoutEncoding(peerId, method, versions, requestData); } diff --git a/packages/beacon-node/src/network/reqresp/types.ts b/packages/beacon-node/src/network/reqresp/types.ts index 392d4d0d1dd..ac14b24eaf2 100644 --- a/packages/beacon-node/src/network/reqresp/types.ts +++ b/packages/beacon-node/src/network/reqresp/types.ts @@ -70,10 +70,13 @@ type ResponseBodyByMethod = { }; /** Request SSZ type for each method and ForkName */ -// TODO Electra: Make requestSszTypeByMethod fork-aware so we can get the correct BlobSidecarsByRootRequestType -export const requestSszTypeByMethod: (config: ChainConfig) => { +// TODO Electra: Currently setting default fork to deneb because not every caller of requestSszTypeByMethod can provide fork info +export const requestSszTypeByMethod: ( + config: ChainConfig, + fork?: ForkName +) => { [K in ReqRespMethod]: RequestBodyByMethod[K] extends null ? null : Type; -} = (config) => ({ +} = (config, fork = ForkName.deneb) => ({ [ReqRespMethod.Status]: ssz.phase0.Status, [ReqRespMethod.Goodbye]: ssz.phase0.Goodbye, [ReqRespMethod.Ping]: ssz.phase0.Ping, @@ -81,7 +84,7 @@ export const requestSszTypeByMethod: (config: ChainConfig) => { [ReqRespMethod.BeaconBlocksByRange]: ssz.phase0.BeaconBlocksByRangeRequest, [ReqRespMethod.BeaconBlocksByRoot]: ssz.phase0.BeaconBlocksByRootRequest, [ReqRespMethod.BlobSidecarsByRange]: ssz.deneb.BlobSidecarsByRangeRequest, - [ReqRespMethod.BlobSidecarsByRoot]: BlobSidecarsByRootRequestType(ForkName.deneb, config), + [ReqRespMethod.BlobSidecarsByRoot]: BlobSidecarsByRootRequestType(fork, config), [ReqRespMethod.LightClientBootstrap]: ssz.Root, [ReqRespMethod.LightClientUpdatesByRange]: ssz.altair.LightClientUpdatesByRange, [ReqRespMethod.LightClientFinalityUpdate]: null, From d3361b4f5b4893f722f69b1db957805fa3c7d0d6 Mon Sep 17 00:00:00 2001 From: NC <17676176+ensi321@users.noreply.github.com> Date: Wed, 18 Dec 2024 13:50:04 -0800 Subject: [PATCH 08/21] Fixed minimal constants --- packages/params/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/params/src/index.ts b/packages/params/src/index.ts index 64d3b64dbd6..757667abada 100644 --- a/packages/params/src/index.ts +++ b/packages/params/src/index.ts @@ -255,11 +255,11 @@ export const BLOB_TX_TYPE = 0x03; export const VERSIONED_HASH_VERSION_KZG = 0x01; // ssz.deneb.BeaconBlockBody.getPathInfo(['blobKzgCommitments',0]).gindex -export const KZG_COMMITMENT_GINDEX0 = ACTIVE_PRESET === PresetName.minimal ? 864 : 221184; +export const KZG_COMMITMENT_GINDEX0 = ACTIVE_PRESET === PresetName.minimal ? 1728 : 221184; export const KZG_COMMITMENT_SUBTREE_INDEX0 = KZG_COMMITMENT_GINDEX0 - 2 ** KZG_COMMITMENT_INCLUSION_PROOF_DEPTH; // ssz.deneb.BlobSidecars.elementType.fixedSize -export const BLOBSIDECAR_FIXED_SIZE = ACTIVE_PRESET === PresetName.minimal ? 131672 : 131928; +export const BLOBSIDECAR_FIXED_SIZE = ACTIVE_PRESET === PresetName.minimal ? 131704 : 131928; // Electra Misc export const UNSET_DEPOSIT_REQUESTS_START_INDEX = 2n ** 64n - 1n; From ebc2aaefdafe208dfa128b1670e6c2cfdca9ff33 Mon Sep 17 00:00:00 2001 From: NC <17676176+ensi321@users.noreply.github.com> Date: Wed, 18 Dec 2024 13:51:29 -0800 Subject: [PATCH 09/21] Bump config test version --- packages/params/test/e2e/ensure-config-is-synced.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/params/test/e2e/ensure-config-is-synced.test.ts b/packages/params/test/e2e/ensure-config-is-synced.test.ts index b1d9c05a54b..d09ea85e016 100644 --- a/packages/params/test/e2e/ensure-config-is-synced.test.ts +++ b/packages/params/test/e2e/ensure-config-is-synced.test.ts @@ -8,7 +8,7 @@ import {loadConfigYaml} from "../yaml.js"; // Not e2e, but slow. Run with e2e tests /** https://github.com/ethereum/consensus-specs/releases */ -const specConfigCommit = "v1.5.0-alpha.8"; +const specConfigCommit = "v1.5.0-alpha.10"; /** * Fields that we filter from local config when doing comparison. * Ideally this should be empty as it is not spec compliant From 13ad885d6b946066324bd135a5fbf8b0046b9680 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Thu, 19 Dec 2024 12:27:52 +0000 Subject: [PATCH 10/21] Update blob sidecar subnet computation --- .../src/chain/validation/blobSidecar.ts | 18 +++++++++++++----- .../src/network/processor/gossipHandlers.ts | 3 ++- packages/validator/src/util/params.ts | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/beacon-node/src/chain/validation/blobSidecar.ts b/packages/beacon-node/src/chain/validation/blobSidecar.ts index b79db77e393..4ce6d29c76c 100644 --- a/packages/beacon-node/src/chain/validation/blobSidecar.ts +++ b/packages/beacon-node/src/chain/validation/blobSidecar.ts @@ -1,5 +1,10 @@ import {ChainConfig} from "@lodestar/config"; -import {KZG_COMMITMENT_INCLUSION_PROOF_DEPTH, KZG_COMMITMENT_SUBTREE_INDEX0} from "@lodestar/params"; +import { + ForkName, + isForkPostElectra, + KZG_COMMITMENT_INCLUSION_PROOF_DEPTH, + KZG_COMMITMENT_SUBTREE_INDEX0, +} from "@lodestar/params"; import {computeStartSlotAtEpoch, getBlockHeaderProposerSignatureSet} from "@lodestar/state-transition"; import {BlobIndex, Root, Slot, deneb, ssz} from "@lodestar/types"; import {toRootHex, verifyMerkleBranch} from "@lodestar/utils"; @@ -14,7 +19,8 @@ import {RegenCaller} from "../regen/index.js"; export async function validateGossipBlobSidecar( chain: IBeaconChain, blobSidecar: deneb.BlobSidecar, - subnet: number + subnet: number, + fork: ForkName ): Promise { const blobSlot = blobSidecar.signedBlockHeader.message.slot; @@ -28,7 +34,7 @@ export async function validateGossipBlobSidecar( } // [REJECT] The sidecar is for the correct subnet -- i.e. `compute_subnet_for_blob_sidecar(sidecar.index) == subnet_id`. - if (computeSubnetForBlobSidecar(blobSidecar.index, chain.config) !== subnet) { + if (computeSubnetForBlobSidecar(blobSidecar.index, chain.config, fork) !== subnet) { throw new BlobSidecarGossipError(GossipAction.REJECT, { code: BlobSidecarErrorCode.INVALID_INDEX, blobIdx: blobSidecar.index, @@ -236,6 +242,8 @@ function validateInclusionProof(blobSidecar: deneb.BlobSidecar): boolean { ); } -function computeSubnetForBlobSidecar(blobIndex: BlobIndex, config: ChainConfig): number { - return blobIndex % config.BLOB_SIDECAR_SUBNET_COUNT; +function computeSubnetForBlobSidecar(blobIndex: BlobIndex, config: ChainConfig, fork: ForkName): number { + return ( + blobIndex % (isForkPostElectra(fork) ? config.BLOB_SIDECAR_SUBNET_COUNT_ELECTRA : config.BLOB_SIDECAR_SUBNET_COUNT) + ); } diff --git a/packages/beacon-node/src/network/processor/gossipHandlers.ts b/packages/beacon-node/src/network/processor/gossipHandlers.ts index 906d16b33d7..91eaca7c86f 100644 --- a/packages/beacon-node/src/network/processor/gossipHandlers.ts +++ b/packages/beacon-node/src/network/processor/gossipHandlers.ts @@ -186,6 +186,7 @@ function getSequentialHandlers(modules: ValidatorFnsModules, options: GossipHand ): Promise { const blobBlockHeader = blobSidecar.signedBlockHeader.message; const slot = blobBlockHeader.slot; + const fork = config.getForkName(slot); const blockRoot = ssz.phase0.BeaconBlockHeader.hashTreeRoot(blobBlockHeader); const blockHex = prettyBytes(blockRoot); @@ -203,7 +204,7 @@ function getSequentialHandlers(modules: ValidatorFnsModules, options: GossipHand ); try { - await validateGossipBlobSidecar(chain, blobSidecar, subnet); + await validateGossipBlobSidecar(chain, blobSidecar, subnet, fork); const recvToValidation = Date.now() / 1000 - seenTimestampSec; const validationTime = recvToValidation - recvToValLatency; diff --git a/packages/validator/src/util/params.ts b/packages/validator/src/util/params.ts index ea19bb3a90c..383275bfb47 100644 --- a/packages/validator/src/util/params.ts +++ b/packages/validator/src/util/params.ts @@ -137,6 +137,7 @@ function getSpecCriticalParams(localConfig: ChainConfig): Record Date: Thu, 19 Dec 2024 12:30:58 +0000 Subject: [PATCH 11/21] Pass proper commitment limit to block gossip error --- packages/beacon-node/src/chain/validation/block.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/beacon-node/src/chain/validation/block.ts b/packages/beacon-node/src/chain/validation/block.ts index 921319439fd..9057e101321 100644 --- a/packages/beacon-node/src/chain/validation/block.ts +++ b/packages/beacon-node/src/chain/validation/block.ts @@ -120,7 +120,7 @@ export async function validateGossipBlock( throw new BlockGossipError(GossipAction.REJECT, { code: BlockErrorCode.TOO_MANY_KZG_COMMITMENTS, blobKzgCommitmentsLen, - commitmentLimit: chain.config.MAX_BLOBS_PER_BLOCK, + commitmentLimit: maxBlobsPerBlock, }); } } From c22f78b87a616440c88fc6a1ea5d4ca1273527b9 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Thu, 19 Dec 2024 12:31:36 +0000 Subject: [PATCH 12/21] Update blob sidecar index check --- packages/beacon-node/src/chain/validation/blobSidecar.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/beacon-node/src/chain/validation/blobSidecar.ts b/packages/beacon-node/src/chain/validation/blobSidecar.ts index 4ce6d29c76c..e102ad4fa83 100644 --- a/packages/beacon-node/src/chain/validation/blobSidecar.ts +++ b/packages/beacon-node/src/chain/validation/blobSidecar.ts @@ -25,11 +25,14 @@ 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`. - if (blobSidecar.index >= chain.config.MAX_BLOBS_PER_BLOCK) { + const maxBlobsPerBlock = isForkPostElectra(fork) + ? chain.config.MAX_BLOBS_PER_BLOCK_ELECTRA + : chain.config.MAX_BLOBS_PER_BLOCK; + if (blobSidecar.index >= maxBlobsPerBlock) { throw new BlobSidecarGossipError(GossipAction.REJECT, { code: BlobSidecarErrorCode.INDEX_TOO_LARGE, blobIdx: blobSidecar.index, - maxBlobsPerBlock: chain.config.MAX_BLOBS_PER_BLOCK, + maxBlobsPerBlock, }); } From aa8a2186d2d53db84949ddefbc3a3dda79bd1cac Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Thu, 19 Dec 2024 12:38:54 +0000 Subject: [PATCH 13/21] Lint --- packages/beacon-node/src/chain/validation/blobSidecar.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/beacon-node/src/chain/validation/blobSidecar.ts b/packages/beacon-node/src/chain/validation/blobSidecar.ts index e102ad4fa83..83c39c33b48 100644 --- a/packages/beacon-node/src/chain/validation/blobSidecar.ts +++ b/packages/beacon-node/src/chain/validation/blobSidecar.ts @@ -1,9 +1,9 @@ import {ChainConfig} from "@lodestar/config"; import { ForkName, - isForkPostElectra, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH, KZG_COMMITMENT_SUBTREE_INDEX0, + isForkPostElectra, } from "@lodestar/params"; import {computeStartSlotAtEpoch, getBlockHeaderProposerSignatureSet} from "@lodestar/state-transition"; import {BlobIndex, Root, Slot, deneb, ssz} from "@lodestar/types"; From 41be26164e4e2aba3f19afcfa1508c7958217ce5 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Thu, 19 Dec 2024 12:43:34 +0000 Subject: [PATCH 14/21] Update kzg unit test --- packages/beacon-node/test/unit/util/kzg.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/beacon-node/test/unit/util/kzg.test.ts b/packages/beacon-node/test/unit/util/kzg.test.ts index 616505268ae..681587f4a4a 100644 --- a/packages/beacon-node/test/unit/util/kzg.test.ts +++ b/packages/beacon-node/test/unit/util/kzg.test.ts @@ -45,6 +45,7 @@ describe("C-KZG", () => { afterEachCallbacks.push(() => chain.close()); const slot = 0; + const fork = config.getForkName(slot); const blobs = [generateRandomBlob(), generateRandomBlob()]; const kzgCommitments = blobs.map((blob) => ckzg.blobToKzgCommitment(blob)); @@ -65,7 +66,7 @@ describe("C-KZG", () => { for (const blobSidecar of blobSidecars) { try { - await validateGossipBlobSidecar(chain, blobSidecar, blobSidecar.index); + await validateGossipBlobSidecar(chain, blobSidecar, blobSidecar.index, fork); } catch (_e) { // We expect some error from here // console.log(error); From 1c2b5eded363553b7f0de4c5061e99fdfcb9890e Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Thu, 19 Dec 2024 12:49:32 +0000 Subject: [PATCH 15/21] Subscribe to correct number of blob sidecar subnets --- packages/beacon-node/src/network/gossip/topic.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/beacon-node/src/network/gossip/topic.ts b/packages/beacon-node/src/network/gossip/topic.ts index 83603226a94..773a831510d 100644 --- a/packages/beacon-node/src/network/gossip/topic.ts +++ b/packages/beacon-node/src/network/gossip/topic.ts @@ -230,7 +230,10 @@ export function getCoreTopicsAtFork( // After Deneb also track blob_sidecar_{subnet_id} if (ForkSeq[fork] >= ForkSeq.deneb) { - for (let subnet = 0; subnet < config.BLOB_SIDECAR_SUBNET_COUNT; subnet++) { + const subnetCount = + ForkSeq[fork] >= ForkSeq.electra ? config.BLOB_SIDECAR_SUBNET_COUNT_ELECTRA : config.BLOB_SIDECAR_SUBNET_COUNT; + + for (let subnet = 0; subnet < subnetCount; subnet++) { topics.push({type: GossipType.blob_sidecar, subnet}); } } From a267c34f7f8fd63d6ac91ec05a660a5f623a7c47 Mon Sep 17 00:00:00 2001 From: NC <17676176+ensi321@users.noreply.github.com> Date: Thu, 19 Dec 2024 16:06:57 -0800 Subject: [PATCH 16/21] Refactor constants getter to constantsHelper --- .../beacon-node/src/chain/validation/blobSidecar.ts | 10 +++------- packages/beacon-node/src/chain/validation/block.ts | 6 ++---- packages/beacon-node/src/network/gossip/topic.ts | 5 ++--- packages/beacon-node/src/network/network.ts | 5 ++--- packages/config/src/constantsHelper.ts | 13 +++++++++++++ packages/config/src/index.ts | 1 + .../src/block/processExecutionPayload.ts | 9 +++++---- 7 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 packages/config/src/constantsHelper.ts diff --git a/packages/beacon-node/src/chain/validation/blobSidecar.ts b/packages/beacon-node/src/chain/validation/blobSidecar.ts index 83c39c33b48..53bd1a492bc 100644 --- a/packages/beacon-node/src/chain/validation/blobSidecar.ts +++ b/packages/beacon-node/src/chain/validation/blobSidecar.ts @@ -1,4 +1,4 @@ -import {ChainConfig} from "@lodestar/config"; +import {ChainConfig, getBlobSidecarSubnetCount, getMaxBlobsPerBlock} from "@lodestar/config"; import { ForkName, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH, @@ -25,9 +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 = isForkPostElectra(fork) - ? chain.config.MAX_BLOBS_PER_BLOCK_ELECTRA - : chain.config.MAX_BLOBS_PER_BLOCK; + const maxBlobsPerBlock = getMaxBlobsPerBlock(fork, chain.config); if (blobSidecar.index >= maxBlobsPerBlock) { throw new BlobSidecarGossipError(GossipAction.REJECT, { code: BlobSidecarErrorCode.INDEX_TOO_LARGE, @@ -246,7 +244,5 @@ function validateInclusionProof(blobSidecar: deneb.BlobSidecar): boolean { } function computeSubnetForBlobSidecar(blobIndex: BlobIndex, config: ChainConfig, fork: ForkName): number { - return ( - blobIndex % (isForkPostElectra(fork) ? config.BLOB_SIDECAR_SUBNET_COUNT_ELECTRA : config.BLOB_SIDECAR_SUBNET_COUNT) - ); + return blobIndex % getBlobSidecarSubnetCount(fork, config); } diff --git a/packages/beacon-node/src/chain/validation/block.ts b/packages/beacon-node/src/chain/validation/block.ts index 9057e101321..600f4b8b49f 100644 --- a/packages/beacon-node/src/chain/validation/block.ts +++ b/packages/beacon-node/src/chain/validation/block.ts @@ -1,4 +1,4 @@ -import {ChainForkConfig} from "@lodestar/config"; +import {ChainForkConfig, getMaxBlobsPerBlock} from "@lodestar/config"; import {ForkName, isForkBlobs, isForkPostElectra} from "@lodestar/params"; import { computeStartSlotAtEpoch, @@ -113,9 +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 = isForkPostElectra(fork) - ? chain.config.MAX_BLOBS_PER_BLOCK_ELECTRA - : chain.config.MAX_BLOBS_PER_BLOCK; + const maxBlobsPerBlock = getMaxBlobsPerBlock(fork, chain.config); if (blobKzgCommitmentsLen > maxBlobsPerBlock) { throw new BlockGossipError(GossipAction.REJECT, { code: BlockErrorCode.TOO_MANY_KZG_COMMITMENTS, diff --git a/packages/beacon-node/src/network/gossip/topic.ts b/packages/beacon-node/src/network/gossip/topic.ts index 773a831510d..1f907cf6587 100644 --- a/packages/beacon-node/src/network/gossip/topic.ts +++ b/packages/beacon-node/src/network/gossip/topic.ts @@ -1,4 +1,4 @@ -import {ChainConfig, ForkDigestContext} from "@lodestar/config"; +import {ChainConfig, ForkDigestContext, getBlobSidecarSubnetCount} from "@lodestar/config"; import { ATTESTATION_SUBNET_COUNT, ForkName, @@ -230,8 +230,7 @@ export function getCoreTopicsAtFork( // After Deneb also track blob_sidecar_{subnet_id} if (ForkSeq[fork] >= ForkSeq.deneb) { - const subnetCount = - ForkSeq[fork] >= ForkSeq.electra ? config.BLOB_SIDECAR_SUBNET_COUNT_ELECTRA : config.BLOB_SIDECAR_SUBNET_COUNT; + const subnetCount = getBlobSidecarSubnetCount(fork, config); for (let subnet = 0; subnet < subnetCount; subnet++) { topics.push({type: GossipType.blob_sidecar, subnet}); diff --git a/packages/beacon-node/src/network/network.ts b/packages/beacon-node/src/network/network.ts index 1d4bad2b832..90af4931853 100644 --- a/packages/beacon-node/src/network/network.ts +++ b/packages/beacon-node/src/network/network.ts @@ -2,7 +2,7 @@ import {PeerScoreStatsDump} from "@chainsafe/libp2p-gossipsub/score"; import {PublishOpts} from "@chainsafe/libp2p-gossipsub/types"; import {PeerId} from "@libp2p/interface"; import {routes} from "@lodestar/api"; -import {BeaconConfig} from "@lodestar/config"; +import {BeaconConfig, getMaxBlobsPerBlock} from "@lodestar/config"; import {LoggerNode} from "@lodestar/logger/node"; import {ForkName, ForkSeq, isForkPostElectra} from "@lodestar/params"; import {ResponseIncoming} from "@lodestar/reqresp"; @@ -510,8 +510,7 @@ export class Network implements INetwork { request ), // request's count represent the slots, so the actual max count received could be slots * blobs per slot - request.count * - (isForkPostElectra(fork) ? this.config.MAX_BLOBS_PER_BLOCK_ELECTRA : this.config.MAX_BLOBS_PER_BLOCK), + request.count * getMaxBlobsPerBlock(fork, this.config), responseSszTypeByMethod[ReqRespMethod.BlobSidecarsByRange] ); } diff --git a/packages/config/src/constantsHelper.ts b/packages/config/src/constantsHelper.ts new file mode 100644 index 00000000000..a7b2596cacd --- /dev/null +++ b/packages/config/src/constantsHelper.ts @@ -0,0 +1,13 @@ +import {ForkName, isForkPostElectra} from "@lodestar/params"; +import {ChainConfig} from "./index.js"; +/** + * A collection of functions that retrieve the correct config/preset constants from the given hard fork + */ + +export function getMaxBlobsPerBlock(fork: ForkName, config: ChainConfig): number { + return isForkPostElectra(fork) ? config.MAX_BLOBS_PER_BLOCK_ELECTRA : config.MAX_BLOBS_PER_BLOCK; +} + +export function getBlobSidecarSubnetCount(fork: ForkName, config: ChainConfig): number { + return isForkPostElectra(fork) ? config.BLOB_SIDECAR_SUBNET_COUNT_ELECTRA : config.BLOB_SIDECAR_SUBNET_COUNT; +} diff --git a/packages/config/src/index.ts b/packages/config/src/index.ts index 2743f3ba7cf..aede07a9412 100644 --- a/packages/config/src/index.ts +++ b/packages/config/src/index.ts @@ -2,3 +2,4 @@ export * from "./chainConfig/index.js"; export * from "./genesisConfig/index.js"; export * from "./forkConfig/index.js"; export * from "./beaconConfig.js"; +export * from "./constantsHelper.js"; diff --git a/packages/state-transition/src/block/processExecutionPayload.ts b/packages/state-transition/src/block/processExecutionPayload.ts index 628c5b28831..6164141f494 100644 --- a/packages/state-transition/src/block/processExecutionPayload.ts +++ b/packages/state-transition/src/block/processExecutionPayload.ts @@ -1,5 +1,6 @@ import {byteArrayEquals} from "@chainsafe/ssz"; -import {ForkSeq} from "@lodestar/params"; +import {getMaxBlobsPerBlock} from "@lodestar/config"; +import {ForkName, ForkSeq, isForkBlobs} from "@lodestar/params"; import {BeaconBlockBody, BlindedBeaconBlockBody, deneb, isExecutionPayload} from "@lodestar/types"; import {toHex, toRootHex} from "@lodestar/utils"; import {CachedBeaconStateBellatrix, CachedBeaconStateCapella} from "../types.js"; @@ -18,6 +19,7 @@ export function processExecutionPayload( externalData: Omit ): void { const payload = getFullOrBlindedPayloadFromBody(body); + const forkName = ForkName[ForkSeq[fork] as keyof typeof ForkName]; // Verify consistency of the parent hash, block number, base fee per gas and gas limit // with respect to the previous execution payload header if (isMergeTransitionComplete(state)) { @@ -47,9 +49,8 @@ export function processExecutionPayload( throw Error(`Invalid timestamp ${payload.timestamp} genesisTime=${state.genesisTime} slot=${state.slot}`); } - if (fork >= ForkSeq.deneb) { - const maxBlobsPerBlock = - fork >= ForkSeq.electra ? state.config.MAX_BLOBS_PER_BLOCK_ELECTRA : state.config.MAX_BLOBS_PER_BLOCK; + if (isForkBlobs(forkName)) { + const maxBlobsPerBlock = getMaxBlobsPerBlock(forkName, state.config); const blobKzgCommitmentsLen = (body as deneb.BeaconBlockBody).blobKzgCommitments?.length ?? 0; if (blobKzgCommitmentsLen > maxBlobsPerBlock) { throw Error(`blobKzgCommitmentsLen exceeds limit=${maxBlobsPerBlock}`); From e8132948422591adcf0b048184fc02295b422af6 Mon Sep 17 00:00:00 2001 From: NC <17676176+ensi321@users.noreply.github.com> Date: Thu, 19 Dec 2024 16:10:49 -0800 Subject: [PATCH 17/21] address comment --- packages/beacon-node/src/network/network.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/beacon-node/src/network/network.ts b/packages/beacon-node/src/network/network.ts index 90af4931853..11e91afe326 100644 --- a/packages/beacon-node/src/network/network.ts +++ b/packages/beacon-node/src/network/network.ts @@ -501,7 +501,7 @@ export class Network implements INetwork { peerId: PeerIdStr, request: deneb.BlobSidecarsByRangeRequest ): Promise { - const fork = this.config.getForkName(this.clock.currentSlot); + const fork = this.config.getForkName(request.startSlot); return collectMaxResponseTyped( this.sendReqRespRequest( peerId, From 744c0becd2d9cc039a4d9c1f2634ebb9df7a92b8 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Fri, 20 Dec 2024 10:07:01 +0000 Subject: [PATCH 18/21] Pass fork as first arg --- packages/beacon-node/src/chain/validation/blobSidecar.ts | 8 ++++---- .../beacon-node/src/network/processor/gossipHandlers.ts | 2 +- packages/beacon-node/test/unit/util/kzg.test.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/beacon-node/src/chain/validation/blobSidecar.ts b/packages/beacon-node/src/chain/validation/blobSidecar.ts index 53bd1a492bc..f23063ee82f 100644 --- a/packages/beacon-node/src/chain/validation/blobSidecar.ts +++ b/packages/beacon-node/src/chain/validation/blobSidecar.ts @@ -17,10 +17,10 @@ import {IBeaconChain} from "../interface.js"; import {RegenCaller} from "../regen/index.js"; export async function validateGossipBlobSidecar( + fork: ForkName, chain: IBeaconChain, blobSidecar: deneb.BlobSidecar, - subnet: number, - fork: ForkName + subnet: number ): Promise { const blobSlot = blobSidecar.signedBlockHeader.message.slot; @@ -35,7 +35,7 @@ export async function validateGossipBlobSidecar( } // [REJECT] The sidecar is for the correct subnet -- i.e. `compute_subnet_for_blob_sidecar(sidecar.index) == subnet_id`. - if (computeSubnetForBlobSidecar(blobSidecar.index, chain.config, fork) !== subnet) { + if (computeSubnetForBlobSidecar(fork, chain.config, blobSidecar.index) !== subnet) { throw new BlobSidecarGossipError(GossipAction.REJECT, { code: BlobSidecarErrorCode.INVALID_INDEX, blobIdx: blobSidecar.index, @@ -243,6 +243,6 @@ function validateInclusionProof(blobSidecar: deneb.BlobSidecar): boolean { ); } -function computeSubnetForBlobSidecar(blobIndex: BlobIndex, config: ChainConfig, fork: ForkName): number { +function computeSubnetForBlobSidecar(fork: ForkName, config: ChainConfig, blobIndex: BlobIndex): number { return blobIndex % getBlobSidecarSubnetCount(fork, config); } diff --git a/packages/beacon-node/src/network/processor/gossipHandlers.ts b/packages/beacon-node/src/network/processor/gossipHandlers.ts index 91eaca7c86f..4646797c242 100644 --- a/packages/beacon-node/src/network/processor/gossipHandlers.ts +++ b/packages/beacon-node/src/network/processor/gossipHandlers.ts @@ -204,7 +204,7 @@ function getSequentialHandlers(modules: ValidatorFnsModules, options: GossipHand ); try { - await validateGossipBlobSidecar(chain, blobSidecar, subnet, fork); + await validateGossipBlobSidecar(fork, chain, blobSidecar, subnet); const recvToValidation = Date.now() / 1000 - seenTimestampSec; const validationTime = recvToValidation - recvToValLatency; diff --git a/packages/beacon-node/test/unit/util/kzg.test.ts b/packages/beacon-node/test/unit/util/kzg.test.ts index 681587f4a4a..589e8d28d83 100644 --- a/packages/beacon-node/test/unit/util/kzg.test.ts +++ b/packages/beacon-node/test/unit/util/kzg.test.ts @@ -66,7 +66,7 @@ describe("C-KZG", () => { for (const blobSidecar of blobSidecars) { try { - await validateGossipBlobSidecar(chain, blobSidecar, blobSidecar.index, fork); + await validateGossipBlobSidecar(fork, chain, blobSidecar, blobSidecar.index); } catch (_e) { // We expect some error from here // console.log(error); From 760fbb891eae26bfea444d6b29a74ea5c3b0fe52 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Fri, 20 Dec 2024 10:08:06 +0000 Subject: [PATCH 19/21] Update packages/state-transition/src/block/processExecutionPayload.ts --- packages/state-transition/src/block/processExecutionPayload.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/state-transition/src/block/processExecutionPayload.ts b/packages/state-transition/src/block/processExecutionPayload.ts index 6164141f494..cc21975673b 100644 --- a/packages/state-transition/src/block/processExecutionPayload.ts +++ b/packages/state-transition/src/block/processExecutionPayload.ts @@ -19,7 +19,7 @@ export function processExecutionPayload( externalData: Omit ): void { const payload = getFullOrBlindedPayloadFromBody(body); - const forkName = ForkName[ForkSeq[fork] as keyof typeof ForkName]; + const forkName = ForkName[ForkSeq[fork] as ForkName]; // Verify consistency of the parent hash, block number, base fee per gas and gas limit // with respect to the previous execution payload header if (isMergeTransitionComplete(state)) { From 8a49542b418197cc59424d4991416106fa7f9e60 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Mon, 6 Jan 2025 17:00:56 +0000 Subject: [PATCH 20/21] refactor: move helper to get max blobs per block to fork config (#7322) --- .../beacon-node/src/chain/validation/blobSidecar.ts | 8 +++++--- packages/beacon-node/src/chain/validation/block.ts | 6 +++--- packages/beacon-node/src/network/gossip/topic.ts | 6 ++++-- packages/beacon-node/src/network/network.ts | 6 +++--- packages/config/src/constantsHelper.ts | 13 ------------- packages/config/src/forkConfig/index.ts | 4 ++++ packages/config/src/forkConfig/types.ts | 2 ++ packages/config/src/index.ts | 1 - .../src/block/processExecutionPayload.ts | 3 +-- 9 files changed, 22 insertions(+), 27 deletions(-) delete mode 100644 packages/config/src/constantsHelper.ts diff --git a/packages/beacon-node/src/chain/validation/blobSidecar.ts b/packages/beacon-node/src/chain/validation/blobSidecar.ts index f23063ee82f..78f40e40792 100644 --- a/packages/beacon-node/src/chain/validation/blobSidecar.ts +++ b/packages/beacon-node/src/chain/validation/blobSidecar.ts @@ -1,4 +1,4 @@ -import {ChainConfig, getBlobSidecarSubnetCount, getMaxBlobsPerBlock} from "@lodestar/config"; +import {ChainConfig} from "@lodestar/config"; import { ForkName, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH, @@ -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 = getMaxBlobsPerBlock(fork, chain.config); + const maxBlobsPerBlock = chain.config.getMaxBlobsPerBlock(fork); if (blobSidecar.index >= maxBlobsPerBlock) { throw new BlobSidecarGossipError(GossipAction.REJECT, { code: BlobSidecarErrorCode.INDEX_TOO_LARGE, @@ -244,5 +244,7 @@ function validateInclusionProof(blobSidecar: deneb.BlobSidecar): boolean { } function computeSubnetForBlobSidecar(fork: ForkName, config: ChainConfig, blobIndex: BlobIndex): number { - return blobIndex % getBlobSidecarSubnetCount(fork, config); + return ( + blobIndex % (isForkPostElectra(fork) ? config.BLOB_SIDECAR_SUBNET_COUNT_ELECTRA : config.BLOB_SIDECAR_SUBNET_COUNT) + ); } diff --git a/packages/beacon-node/src/chain/validation/block.ts b/packages/beacon-node/src/chain/validation/block.ts index 600f4b8b49f..2b18999db40 100644 --- a/packages/beacon-node/src/chain/validation/block.ts +++ b/packages/beacon-node/src/chain/validation/block.ts @@ -1,5 +1,5 @@ -import {ChainForkConfig, getMaxBlobsPerBlock} from "@lodestar/config"; -import {ForkName, isForkBlobs, isForkPostElectra} from "@lodestar/params"; +import {ChainForkConfig} from "@lodestar/config"; +import {ForkName, isForkBlobs} from "@lodestar/params"; import { computeStartSlotAtEpoch, computeTimeAtSlot, @@ -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 = getMaxBlobsPerBlock(fork, chain.config); + const maxBlobsPerBlock = chain.config.getMaxBlobsPerBlock(fork); if (blobKzgCommitmentsLen > maxBlobsPerBlock) { throw new BlockGossipError(GossipAction.REJECT, { code: BlockErrorCode.TOO_MANY_KZG_COMMITMENTS, diff --git a/packages/beacon-node/src/network/gossip/topic.ts b/packages/beacon-node/src/network/gossip/topic.ts index 1f907cf6587..bf44dd90b8a 100644 --- a/packages/beacon-node/src/network/gossip/topic.ts +++ b/packages/beacon-node/src/network/gossip/topic.ts @@ -1,4 +1,4 @@ -import {ChainConfig, ForkDigestContext, getBlobSidecarSubnetCount} from "@lodestar/config"; +import {ChainConfig, ForkDigestContext} from "@lodestar/config"; import { ATTESTATION_SUBNET_COUNT, ForkName, @@ -230,7 +230,9 @@ export function getCoreTopicsAtFork( // After Deneb also track blob_sidecar_{subnet_id} if (ForkSeq[fork] >= ForkSeq.deneb) { - const subnetCount = getBlobSidecarSubnetCount(fork, config); + const subnetCount = isForkPostElectra(fork) + ? config.BLOB_SIDECAR_SUBNET_COUNT_ELECTRA + : config.BLOB_SIDECAR_SUBNET_COUNT; for (let subnet = 0; subnet < subnetCount; subnet++) { topics.push({type: GossipType.blob_sidecar, subnet}); diff --git a/packages/beacon-node/src/network/network.ts b/packages/beacon-node/src/network/network.ts index 11e91afe326..ae06cbab715 100644 --- a/packages/beacon-node/src/network/network.ts +++ b/packages/beacon-node/src/network/network.ts @@ -2,9 +2,9 @@ import {PeerScoreStatsDump} from "@chainsafe/libp2p-gossipsub/score"; import {PublishOpts} from "@chainsafe/libp2p-gossipsub/types"; import {PeerId} from "@libp2p/interface"; import {routes} from "@lodestar/api"; -import {BeaconConfig, getMaxBlobsPerBlock} from "@lodestar/config"; +import {BeaconConfig} from "@lodestar/config"; import {LoggerNode} from "@lodestar/logger/node"; -import {ForkName, ForkSeq, isForkPostElectra} from "@lodestar/params"; +import {ForkSeq, isForkPostElectra} from "@lodestar/params"; import {ResponseIncoming} from "@lodestar/reqresp"; import {computeStartSlotAtEpoch, computeTimeAtSlot} from "@lodestar/state-transition"; import { @@ -510,7 +510,7 @@ export class Network implements INetwork { request ), // request's count represent the slots, so the actual max count received could be slots * blobs per slot - request.count * getMaxBlobsPerBlock(fork, this.config), + request.count * this.config.getMaxBlobsPerBlock(fork), responseSszTypeByMethod[ReqRespMethod.BlobSidecarsByRange] ); } diff --git a/packages/config/src/constantsHelper.ts b/packages/config/src/constantsHelper.ts deleted file mode 100644 index a7b2596cacd..00000000000 --- a/packages/config/src/constantsHelper.ts +++ /dev/null @@ -1,13 +0,0 @@ -import {ForkName, isForkPostElectra} from "@lodestar/params"; -import {ChainConfig} from "./index.js"; -/** - * A collection of functions that retrieve the correct config/preset constants from the given hard fork - */ - -export function getMaxBlobsPerBlock(fork: ForkName, config: ChainConfig): number { - return isForkPostElectra(fork) ? config.MAX_BLOBS_PER_BLOCK_ELECTRA : config.MAX_BLOBS_PER_BLOCK; -} - -export function getBlobSidecarSubnetCount(fork: ForkName, config: ChainConfig): number { - return isForkPostElectra(fork) ? config.BLOB_SIDECAR_SUBNET_COUNT_ELECTRA : config.BLOB_SIDECAR_SUBNET_COUNT; -} diff --git a/packages/config/src/forkConfig/index.ts b/packages/config/src/forkConfig/index.ts index 725e0a3af57..6540106bb88 100644 --- a/packages/config/src/forkConfig/index.ts +++ b/packages/config/src/forkConfig/index.ts @@ -10,6 +10,7 @@ import { isForkBlobs, isForkExecution, isForkLightClient, + isForkPostElectra, } from "@lodestar/params"; import {Epoch, SSZTypesFor, Slot, Version, sszTypesFor} from "@lodestar/types"; import {ChainConfig} from "../chainConfig/index.js"; @@ -129,5 +130,8 @@ 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; + }, }; } diff --git a/packages/config/src/forkConfig/types.ts b/packages/config/src/forkConfig/types.ts index ebb2899a2a2..420cd6bcd24 100644 --- a/packages/config/src/forkConfig/types.ts +++ b/packages/config/src/forkConfig/types.ts @@ -39,4 +39,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; }; diff --git a/packages/config/src/index.ts b/packages/config/src/index.ts index aede07a9412..2743f3ba7cf 100644 --- a/packages/config/src/index.ts +++ b/packages/config/src/index.ts @@ -2,4 +2,3 @@ export * from "./chainConfig/index.js"; export * from "./genesisConfig/index.js"; export * from "./forkConfig/index.js"; export * from "./beaconConfig.js"; -export * from "./constantsHelper.js"; diff --git a/packages/state-transition/src/block/processExecutionPayload.ts b/packages/state-transition/src/block/processExecutionPayload.ts index cc21975673b..ddc24e884d9 100644 --- a/packages/state-transition/src/block/processExecutionPayload.ts +++ b/packages/state-transition/src/block/processExecutionPayload.ts @@ -1,5 +1,4 @@ import {byteArrayEquals} from "@chainsafe/ssz"; -import {getMaxBlobsPerBlock} from "@lodestar/config"; import {ForkName, ForkSeq, isForkBlobs} from "@lodestar/params"; import {BeaconBlockBody, BlindedBeaconBlockBody, deneb, isExecutionPayload} from "@lodestar/types"; import {toHex, toRootHex} from "@lodestar/utils"; @@ -50,7 +49,7 @@ export function processExecutionPayload( } if (isForkBlobs(forkName)) { - const maxBlobsPerBlock = getMaxBlobsPerBlock(forkName, state.config); + const maxBlobsPerBlock = state.config.getMaxBlobsPerBlock(forkName); const blobKzgCommitmentsLen = (body as deneb.BeaconBlockBody).blobKzgCommitments?.length ?? 0; if (blobKzgCommitmentsLen > maxBlobsPerBlock) { throw Error(`blobKzgCommitmentsLen exceeds limit=${maxBlobsPerBlock}`); From d5b003888b5725811022d0a3898221007e1c3187 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Wed, 8 Jan 2025 11:39:06 +0000 Subject: [PATCH 21/21] Simplify type cast --- packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts b/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts index 1098f0de145..72d9892faec 100644 --- a/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts +++ b/packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts @@ -207,7 +207,7 @@ export class ReqRespBeaconNode extends ReqResp { versions: number[], request: Req ): AsyncIterable { - const fork = ForkName[ForkSeq[this.currentRegisteredFork] as keyof typeof ForkName]; + const fork = ForkName[ForkSeq[this.currentRegisteredFork] as ForkName]; const requestType = requestSszTypeByMethod(this.config, fork)[method]; const requestData = requestType ? requestType.serialize(request as never) : new Uint8Array(); return this.sendRequestWithoutEncoding(peerId, method, versions, requestData);