Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove all* types objects #6904

Merged
merged 16 commits into from
Jun 27, 2024
4 changes: 2 additions & 2 deletions packages/api/src/builder/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
JsonOnlyReq,
WithVersion,
} from "../utils/codecs.js";
import {getExecutionForkTypes, toForkName} from "../utils/fork.js";
import {getBlobsForkTypes, getExecutionForkTypes, toForkName} from "../utils/fork.js";
import {fromHeaders} from "../utils/headers.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes
Expand Down Expand Up @@ -150,7 +150,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
resp: {
data: WithVersion<ExecutionPayload | ExecutionPayloadAndBlobsBundle, VersionMeta>((fork: ForkName) => {
return isForkBlobs(fork)
? ssz.allForksBlobs[fork].ExecutionPayloadAndBlobsBundle
? getBlobsForkTypes(fork).ExecutionPayloadAndBlobsBundle
: getExecutionForkTypes(fork).ExecutionPayload;
}),
meta: VersionCodec,
Expand Down
5 changes: 4 additions & 1 deletion packages/beacon-node/src/chain/lightClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
RootHex,
Slot,
ssz,
sszTypesFor,
SSZTypesFor,
SyncPeriod,
} from "@lodestar/types";
Expand All @@ -39,6 +40,8 @@ import {
ForkSeq,
ForkExecution,
ForkLightClient,
highestFork,
forkLightClient,
} from "@lodestar/params";

import {IBeaconDb} from "../../db/index.js";
Expand Down Expand Up @@ -220,7 +223,7 @@ export class LightClientServer {

this.zero = {
// Assign the hightest fork's default value because it can always be typecasted down to correct fork
finalizedHeader: Object.values(ssz.allForksLightClient).slice(-1)[0].LightClientHeader.defaultValue(),
finalizedHeader: sszTypesFor(highestFork(forkLightClient)).LightClientHeader.defaultValue(),
finalityBranch: ssz.altair.LightClientUpdate.fields["finalityBranch"].defaultValue(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ExecutionPayloadHeader,
BlindedBeaconBlockBody,
BlindedBeaconBlock,
sszTypesFor,
} from "@lodestar/types";
import {
CachedBeaconStateAllForks,
Expand Down Expand Up @@ -240,7 +241,7 @@ export async function produceBlockBody<T extends BlockType>(

if (prepareRes.isPremerge) {
(blockBody as BeaconBlockBody<ForkExecution>).executionPayload =
ssz.allForksExecution[fork].ExecutionPayload.defaultValue();
sszTypesFor(fork).ExecutionPayload.defaultValue();
blobsResult = {type: BlobsResultType.preDeneb};
executionPayloadValue = BigInt(0);
} else {
Expand Down Expand Up @@ -310,7 +311,7 @@ export async function produceBlockBody<T extends BlockType>(
e as Error
);
(blockBody as BeaconBlockBody<ForkExecution>).executionPayload =
ssz.allForksExecution[fork].ExecutionPayload.defaultValue();
sszTypesFor(fork).ExecutionPayload.defaultValue();
blobsResult = {type: BlobsResultType.preDeneb};
executionPayloadValue = BigInt(0);
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/src/network/gossip/topic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {phase0, ssz} from "@lodestar/types";
import {phase0, ssz, sszTypesFor} from "@lodestar/types";
import {ForkDigestContext} from "@lodestar/config";
import {
ATTESTATION_SUBNET_COUNT,
Expand Down Expand Up @@ -102,11 +102,11 @@ export function getGossipSSZType(topic: GossipTopic) {
return ssz.altair.SyncCommitteeMessage;
case GossipType.light_client_optimistic_update:
return isForkLightClient(topic.fork)
? ssz.allForksLightClient[topic.fork].LightClientOptimisticUpdate
? sszTypesFor(topic.fork).LightClientOptimisticUpdate
: ssz.altair.LightClientOptimisticUpdate;
case GossipType.light_client_finality_update:
return isForkLightClient(topic.fork)
? ssz.allForksLightClient[topic.fork].LightClientFinalityUpdate
? sszTypesFor(topic.fork).LightClientFinalityUpdate
: ssz.altair.LightClientFinalityUpdate;
case GossipType.bls_to_execution_change:
return ssz.capella.SignedBLSToExecutionChange;
Expand Down
13 changes: 5 additions & 8 deletions packages/beacon-node/src/network/reqresp/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Type} from "@chainsafe/ssz";
import {ForkLightClient, ForkName, isForkLightClient} from "@lodestar/params";
import {Protocol, ProtocolHandler, ReqRespRequest} from "@lodestar/reqresp";
import {Metadata, Root, SignedBeaconBlock, altair, deneb, phase0, ssz} from "@lodestar/types";
import {Metadata, Root, SignedBeaconBlock, altair, deneb, phase0, ssz, sszTypesFor} from "@lodestar/types";

export type ProtocolNoHandler = Omit<Protocol, "handler">;

Expand Down Expand Up @@ -91,14 +91,11 @@ export const responseSszTypeByMethod: {[K in ReqRespMethod]: ResponseTypeGetter<
[ReqRespMethod.BeaconBlocksByRoot]: blocksResponseType,
[ReqRespMethod.BlobSidecarsByRange]: () => ssz.deneb.BlobSidecar,
[ReqRespMethod.BlobSidecarsByRoot]: () => ssz.deneb.BlobSidecar,
[ReqRespMethod.LightClientBootstrap]: (fork) =>
ssz.allForksLightClient[onlyLightclientFork(fork)].LightClientBootstrap,
[ReqRespMethod.LightClientUpdatesByRange]: (fork) =>
ssz.allForksLightClient[onlyLightclientFork(fork)].LightClientUpdate,
[ReqRespMethod.LightClientFinalityUpdate]: (fork) =>
ssz.allForksLightClient[onlyLightclientFork(fork)].LightClientFinalityUpdate,
[ReqRespMethod.LightClientBootstrap]: (fork) => sszTypesFor(onlyLightclientFork(fork)).LightClientBootstrap,
[ReqRespMethod.LightClientUpdatesByRange]: (fork) => sszTypesFor(onlyLightclientFork(fork)).LightClientUpdate,
[ReqRespMethod.LightClientFinalityUpdate]: (fork) => sszTypesFor(onlyLightclientFork(fork)).LightClientFinalityUpdate,
[ReqRespMethod.LightClientOptimisticUpdate]: (fork) =>
ssz.allForksLightClient[onlyLightclientFork(fork)].LightClientOptimisticUpdate,
sszTypesFor(onlyLightclientFork(fork)).LightClientOptimisticUpdate,
};

function onlyLightclientFork(fork: ForkName): ForkLightClient {
Expand Down
5 changes: 2 additions & 3 deletions packages/beacon-node/src/node/utils/interop/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Bytes32, phase0, ssz, TimeSeconds} from "@lodestar/types";
import {Bytes32, phase0, ssz, sszTypesFor, TimeSeconds} from "@lodestar/types";
import {ChainForkConfig} from "@lodestar/config";
import {BeaconStateAllForks, initializeBeaconStateFromEth1} from "@lodestar/state-transition";
import {createEmptyEpochCacheImmutableData} from "@lodestar/state-transition";
Expand All @@ -12,7 +12,6 @@ export const INTEROP_TIMESTAMP = Math.pow(2, 40);
// Note: These configuration settings do not apply to the mainnet and are utilized only by pure Merge testing.
export const GENESIS_GAS_LIMIT = 30000000;
export const GENESIS_BASE_FEE_PER_GAS = BigInt(1000000000);
type ExecutionFork = Exclude<ForkName, ForkName.phase0 | ForkName.altair>;

export type InteropStateOpts = {
genesisTime?: number;
Expand All @@ -34,7 +33,7 @@ export function getInteropState(
const fork = config.getForkName(GENESIS_SLOT);
const executionPayloadHeaderType =
fork !== ForkName.phase0 && fork !== ForkName.altair
? ssz.allForksExecution[fork as ExecutionFork].ExecutionPayloadHeader
? sszTypesFor(fork).ExecutionPayloadHeader
: ssz.bellatrix.ExecutionPayloadHeader;
const latestPayloadHeader = executionPayloadHeaderType.defaultViewDU();
// TODO: when having different test options, consider modifying these values
Expand Down
8 changes: 3 additions & 5 deletions packages/beacon-node/test/spec/presets/genesis.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "node:path";
import {expect} from "vitest";
import {phase0, Root, ssz, TimeSeconds, ExecutionPayloadHeader} from "@lodestar/types";
import {phase0, Root, ssz, TimeSeconds, ExecutionPayloadHeader, sszTypesFor} from "@lodestar/types";
import {InputType} from "@lodestar/spec-test-util";
import {
BeaconStateAllForks,
Expand Down Expand Up @@ -49,7 +49,7 @@ const genesisInitialization: TestRunnerFn<GenesisInitSpecTest, BeaconStateAllFor

const executionPayloadHeaderType =
fork !== ForkName.phase0 && fork !== ForkName.altair
? ssz.allForksExecution[fork as ExecutionFork].ExecutionPayloadHeader
? sszTypesFor(fork).ExecutionPayloadHeader
: ssz.bellatrix.ExecutionPayloadHeader;

return initializeBeaconStateFromEth1(
Expand Down Expand Up @@ -82,7 +82,7 @@ const genesisInitialization: TestRunnerFn<GenesisInitSpecTest, BeaconStateAllFor
// for merge/post merge genesis, no affect on other phases
execution_payload_header:
fork !== ForkName.phase0 && fork !== ForkName.altair
? ssz.allForksExecution[fork as ExecutionFork].ExecutionPayloadHeader
? sszTypesFor(fork).ExecutionPayloadHeader
: ssz.bellatrix.ExecutionPayloadHeader,
...generateDepositSSZTypeMapping(192),
},
Expand Down Expand Up @@ -152,8 +152,6 @@ type GenesisInitCase = {
eth1_timestamp: bigint;
};

type ExecutionFork = Exclude<ForkName, ForkName.phase0 | ForkName.altair>;

specTestIterator(path.join(ethereumConsensusSpecsTests.outputDir, "tests", ACTIVE_PRESET), {
genesis: {type: RunnerType.default, fn: genesis},
});
6 changes: 2 additions & 4 deletions packages/beacon-node/test/spec/presets/light_client/sync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect} from "vitest";
import {init} from "@chainsafe/bls/switchable";
import {isForkLightClient} from "@lodestar/params";
import {altair, phase0, RootHex, Slot, ssz} from "@lodestar/types";
import {altair, phase0, RootHex, Slot, ssz, sszTypesFor} from "@lodestar/types";
import {InputType} from "@lodestar/spec-test-util";
import {createBeaconConfig, ChainConfig} from "@lodestar/config";
import {fromHex, toHex} from "@lodestar/utils";
Expand Down Expand Up @@ -170,9 +170,7 @@ export const sync: TestRunnerFn<SyncTestCase, void> = (fork) => {
config: InputType.YAML,
},
sszTypes: {
bootstrap: isForkLightClient(fork)
? ssz.allForksLightClient[fork].LightClientBootstrap
: ssz.altair.LightClientBootstrap,
bootstrap: isForkLightClient(fork) ? sszTypesFor(fork).LightClientBootstrap : ssz.altair.LightClientBootstrap,
// The updates are multifork and need config and step info to be deserialized within the test
[UPDATE_FILE_NAME]: {typeName: "LightClientUpdate", deserialize: (bytes: Uint8Array) => bytes},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from "vitest";
import {LightClientUpdate, altair, ssz} from "@lodestar/types";
import {LightClientUpdate, altair, ssz, sszTypesFor} from "@lodestar/types";
import {isForkLightClient} from "@lodestar/params";
import {InputType} from "@lodestar/spec-test-util";
import {isBetterUpdate, LightClientUpdateSummary, toLightClientUpdateSummary} from "@lodestar/light-client/spec";
Expand Down Expand Up @@ -54,7 +54,7 @@ newUpdate = ${renderUpdate(newUpdate)}
},
sszTypes: {
[UPDATES_FILE_NAME]: isForkLightClient(fork)
? ssz.allForksLightClient[fork].LightClientUpdate
? sszTypesFor(fork).LightClientUpdate
: ssz.altair.LightClientUpdate,
},
expectFunc: () => {},
Expand Down
6 changes: 2 additions & 4 deletions packages/beacon-node/test/spec/presets/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getBlockRootAtSlot,
} from "@lodestar/state-transition";
import * as blockFns from "@lodestar/state-transition/block";
import {ssz, phase0, altair, bellatrix, capella} from "@lodestar/types";
import {ssz, phase0, altair, bellatrix, capella, sszTypesFor} from "@lodestar/types";
import {InputType} from "@lodestar/spec-test-util";
import {ACTIVE_PRESET, ForkName} from "@lodestar/params";

Expand Down Expand Up @@ -128,7 +128,7 @@ const operations: TestRunnerFn<OperationsTestCase, BeaconStateAllForks> = (fork,
// Bellatrix
execution_payload:
fork !== ForkName.phase0 && fork !== ForkName.altair
? ssz.allForksExecution[fork as ExecutionFork].ExecutionPayload
? sszTypesFor(fork).ExecutionPayload
: ssz.bellatrix.ExecutionPayload,
// Capella
address_change: ssz.capella.SignedBLSToExecutionChange,
Expand All @@ -143,8 +143,6 @@ const operations: TestRunnerFn<OperationsTestCase, BeaconStateAllForks> = (fork,
};
};

type ExecutionFork = Exclude<ForkName, ForkName.phase0 | ForkName.altair>;

specTestIterator(path.join(ethereumConsensusSpecsTests.outputDir, "tests", ACTIVE_PRESET), {
operations: {type: RunnerType.default, fn: operations},
});
9 changes: 3 additions & 6 deletions packages/beacon-node/test/spec/presets/ssz_static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import fs from "node:fs";
import path from "node:path";
import {it, vi} from "vitest";
import {Type} from "@chainsafe/ssz";
import {ssz} from "@lodestar/types";
import {ACTIVE_PRESET, ForkName, ForkLightClient} from "@lodestar/params";
import {ssz, sszTypesFor} from "@lodestar/types";
import {ACTIVE_PRESET, ForkName} from "@lodestar/params";
import {replaceUintTypeWithUintBigintType} from "../utils/replaceUintTypeWithUintBigintType.js";
import {parseSszStaticTestcase} from "../utils/sszTestCaseParser.js";
import {runValidSszTest} from "../utils/runValidSszTest.js";
Expand Down Expand Up @@ -44,10 +44,7 @@ const sszStatic =

/* eslint-disable @typescript-eslint/strict-boolean-expressions */
const sszType =
// Since lightclient types are not updated/declared at all forks, this allForksLightClient
// will help us get the right type for lightclient objects
((ssz.allForksLightClient[fork as ForkLightClient] || {}) as Types)[typeName] ||
(ssz[fork] as Types)[typeName] ||
(sszTypesFor(fork) as Types)[typeName] ||
(ssz.capella as Types)[typeName] ||
(ssz.bellatrix as Types)[typeName] ||
(ssz.altair as Types)[typeName] ||
Expand Down
8 changes: 4 additions & 4 deletions packages/config/src/forkConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ForkLightClient,
ForkBlobs,
} from "@lodestar/params";
import {Slot, Version, ssz, SSZTypesFor, sszTypesFor} from "@lodestar/types";
import {Slot, Version, SSZTypesFor, sszTypesFor} from "@lodestar/types";
import {ChainConfig} from "../chainConfig/index.js";
import {ForkConfig, ForkInfo} from "./types.js";

Expand Down Expand Up @@ -99,21 +99,21 @@ export function createForkConfig(config: ChainConfig): ForkConfig {
if (!isForkExecution(forkName)) {
throw Error(`Invalid slot=${slot} fork=${forkName} for execution fork types`);
}
return ssz.allForksExecution[forkName];
return sszTypesFor(forkName);
},
getLightClientForkTypes(slot: Slot): SSZTypesFor<ForkLightClient> {
const forkName = this.getForkName(slot);
if (!isForkLightClient(forkName)) {
throw Error(`Invalid slot=${slot} fork=${forkName} for lightclient fork types`);
}
return ssz.allForksLightClient[forkName];
return sszTypesFor(forkName);
},
getBlobsForkTypes(slot: Slot): SSZTypesFor<ForkBlobs> {
const forkName = this.getForkName(slot);
if (!isForkBlobs(forkName)) {
throw Error(`Invalid slot=${slot} fork=${forkName} for blobs fork types`);
}
return ssz.allForksBlobs[forkName];
return sszTypesFor(forkName);
},
};
}
33 changes: 33 additions & 0 deletions packages/params/src/forkName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,61 @@ export enum ForkSeq {
deneb = 4,
}

function exclude<T extends ForkName, U extends T>(coll: T[], val: U[]): Exclude<T, U>[] {
return coll.filter((f) => !val.includes(f as U)) as Exclude<T, U>[];
}

export function highestFork<F extends ForkName>(forkNames: F[]): F {
let highest = forkNames[0];

for (const forkName of forkNames) {
if (ForkSeq[forkName] > ForkSeq[highest]) {
highest = forkName;
}
}

return highest;
}

export function lowestFork<F extends ForkName>(forkNames: F[]): F {
let lowest = forkNames[0];

for (const forkName of forkNames) {
if (ForkSeq[forkName] < ForkSeq[lowest]) {
lowest = forkName;
}
}

return lowest;
}

export type ForkAll = ForkName;
export const forkAll = Object.values(ForkName);

export type ForkPreLightClient = ForkName.phase0;
export type ForkLightClient = Exclude<ForkName, ForkPreLightClient>;
export const forkLightClient = exclude(forkAll, [ForkName.phase0]);
export function isForkLightClient(fork: ForkName): fork is ForkLightClient {
return fork !== ForkName.phase0;
}

export type ForkPreExecution = ForkPreLightClient | ForkName.altair;
export type ForkExecution = Exclude<ForkName, ForkPreExecution>;
export const forkExecution = exclude(forkAll, [ForkName.phase0, ForkName.altair]);
nazarhussain marked this conversation as resolved.
Show resolved Hide resolved
export function isForkExecution(fork: ForkName): fork is ForkExecution {
return isForkLightClient(fork) && fork !== ForkName.altair;
}

export type ForkPreWithdrawals = ForkPreExecution | ForkName.bellatrix;
export type ForkWithdrawals = Exclude<ForkName, ForkPreWithdrawals>;
export const forkWithdrawals = exclude(forkAll, [ForkName.phase0, ForkName.altair, ForkName.bellatrix]);
export function isForkWithdrawals(fork: ForkName): fork is ForkWithdrawals {
return isForkExecution(fork) && fork !== ForkName.bellatrix;
}

export type ForkPreBlobs = ForkPreWithdrawals | ForkName.capella;
export type ForkBlobs = Exclude<ForkName, ForkPreBlobs>;
export const forkBlobs = exclude(forkAll, [ForkName.phase0, ForkName.altair, ForkName.bellatrix, ForkName.capella]);
export function isForkBlobs(fork: ForkName): fork is ForkBlobs {
return isForkWithdrawals(fork) && fork !== ForkName.capella;
}
41 changes: 41 additions & 0 deletions packages/params/test/unit/__snapshots__/forkName.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
nflaig marked this conversation as resolved.
Show resolved Hide resolved

exports[`forkName > should have valid allForks 1`] = `
[
"phase0",
"altair",
"bellatrix",
"capella",
"deneb",
]
`;

exports[`forkName > should have valid blobs forks 1`] = `
[
"deneb",
]
`;

exports[`forkName > should have valid execution forks 1`] = `
[
"bellatrix",
"capella",
"deneb",
]
`;

exports[`forkName > should have valid lightclient forks 1`] = `
[
"altair",
"bellatrix",
"capella",
"deneb",
]
`;

exports[`forkName > should have valid withdrawal forks 1`] = `
[
"capella",
"deneb",
]
`;
Loading
Loading