From 4fad8635c66d0023d2330c6766aa21a856b3e698 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Fri, 20 Dec 2024 10:25:45 +0000 Subject: [PATCH] refactor: move helper to get max blobs per block to fork config --- .../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 f23063ee82fb..78f40e407925 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 600f4b8b49fc..2b18999db402 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 1f907cf65874..bf44dd90b8af 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 11e91afe3260..ae06cbab715f 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 a7b2596cacd8..000000000000 --- 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 725e0a3af572..6540106bb886 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 ebb2899a2a21..420cd6bcd244 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 aede07a9412f..2743f3ba7cfc 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 cc21975673bd..ddc24e884d98 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}`);