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

refactor: improve types package to use forks as generics #6825

Merged
merged 40 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fdd6428
Update the types to use generics
nazarhussain May 24, 2024
50774c6
Update the types with default generics
nazarhussain May 28, 2024
dc4a23f
Fix the block type
nazarhussain May 28, 2024
fbbc279
Add missing type
nazarhussain May 28, 2024
5b8e573
Add missing type
nazarhussain May 28, 2024
ccf59cd
Add ssz types
nazarhussain May 28, 2024
33caf6e
Remove allForks namespace
nazarhussain May 28, 2024
8a33570
Make ssz types generic
nazarhussain May 28, 2024
bbd076a
Update config package
nazarhussain May 28, 2024
92f6aae
Fix blok fork types
nazarhussain May 28, 2024
046af93
Fix api package types
nazarhussain May 28, 2024
930e577
Update types
nazarhussain May 29, 2024
175c8c3
Update state-transition package
nazarhussain May 29, 2024
57feebe
Update config package
nazarhussain May 29, 2024
ab52f1a
Fix types
nazarhussain Jun 3, 2024
c56c7de
Fix types for all packages
nazarhussain Jun 3, 2024
d417c3b
Update api package
nazarhussain May 29, 2024
efa70a4
Fix types for tests files
nazarhussain Jun 5, 2024
7ca3416
Fix ssz types
nazarhussain Jun 5, 2024
5b9c388
Fix build error
nazarhussain Jun 5, 2024
037050d
Fix lint errors
nazarhussain Jun 5, 2024
a396cd9
Fix lint errors
nazarhussain Jun 5, 2024
ccaa449
Fix type issues after rebase
nazarhussain Jun 12, 2024
4d30310
Update redundant generics
nazarhussain Jun 13, 2024
947016e
Fix the type
nazarhussain Jun 13, 2024
d59b127
Update types to use single generic
nazarhussain Jun 14, 2024
a4d07eb
Fix all types
nazarhussain Jun 14, 2024
4a36622
Fix the code as per feedback
nazarhussain Jun 15, 2024
4e17424
Merge branch 'unstable' into nh/types-refactor
nazarhussain Jun 15, 2024
3c075ba
Merge branch 'nh/types-refactor' of github.com:ChainSafe/lodestar int…
nazarhussain Jun 15, 2024
5319f1c
Update the code with feedback
nazarhussain Jun 15, 2024
dbfea50
Update packages/types/README.md
nazarhussain Jun 15, 2024
f33b835
Merge branch 'nh/types-refactor' of github.com:ChainSafe/lodestar int…
nazarhussain Jun 15, 2024
04c38a1
chore: review #6825 (#6887)
nflaig Jun 16, 2024
d6d7eb9
Fix publishBlockWrapper input type
nflaig Jun 16, 2024
6374b0a
Updated the type guard
nazarhussain Jun 21, 2024
2661efc
Merge branch 'unstable' into nh/types-refactor
nflaig Jun 21, 2024
2ea3be7
Remove ssz instance type
nazarhussain Jun 22, 2024
68ff948
Rename BlindedExecutionPayload to ExecutionPayloadheader
nazarhussain Jun 22, 2024
536d8c9
Update code as per feedback
nazarhussain Jun 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions packages/api/src/beacon/routes/beacon/block.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {ContainerType, ListCompositeType, ValueOf} from "@chainsafe/ssz";
import {ChainForkConfig} from "@lodestar/config";
import {allForks, Slot, ssz, RootHex, deneb, isSignedBlockContents} from "@lodestar/types";
import {
Slot,
ssz,
RootHex,
deneb,
isSignedBlockContents,
SignedBeaconBlock,
BeaconBlockBody,
SignedBeaconBlockOrContents,
SignedBlindedBeaconBlock,
} from "@lodestar/types";
import {ForkName, ForkSeq} from "@lodestar/params";
import {Endpoint, RequestCodec, RouteDefinitions, Schema} from "../../../utils/index.js";
import {EmptyMeta, EmptyResponseCodec, EmptyResponseData, WithVersion} from "../../../utils/codecs.js";
Expand Down Expand Up @@ -74,7 +84,7 @@ export type Endpoints = {
"GET",
BlockArgs,
{params: {block_id: string}},
allForks.SignedBeaconBlock,
SignedBeaconBlock,
ExecutionOptimisticFinalizedAndVersionMeta
>;

Expand All @@ -86,7 +96,7 @@ export type Endpoints = {
"GET",
BlockArgs,
{params: {block_id: string}},
allForks.BeaconBlockBody["attestations"],
BeaconBlockBody["attestations"],
ExecutionOptimisticAndFinalizedMeta
>;

Expand Down Expand Up @@ -139,7 +149,7 @@ export type Endpoints = {
*/
publishBlock: Endpoint<
"POST",
{signedBlockOrContents: allForks.SignedBeaconBlockOrContents},
{signedBlockOrContents: SignedBeaconBlockOrContents},
{body: unknown; headers: {[MetaHeader.Version]: string}},
EmptyResponseData,
EmptyMeta
Expand All @@ -148,7 +158,7 @@ export type Endpoints = {
publishBlockV2: Endpoint<
"POST",
{
signedBlockOrContents: allForks.SignedBeaconBlockOrContents;
signedBlockOrContents: SignedBeaconBlockOrContents;
broadcastValidation?: BroadcastValidation;
},
{body: unknown; headers: {[MetaHeader.Version]: string}; query: {broadcast_validation?: string}},
Expand All @@ -162,7 +172,7 @@ export type Endpoints = {
*/
publishBlindedBlock: Endpoint<
"POST",
{signedBlindedBlock: allForks.SignedBlindedBeaconBlock},
{signedBlindedBlock: SignedBlindedBeaconBlock},
{body: unknown; headers: {[MetaHeader.Version]: string}},
EmptyResponseData,
EmptyMeta
Expand All @@ -171,7 +181,7 @@ export type Endpoints = {
publishBlindedBlockV2: Endpoint<
"POST",
{
signedBlindedBlock: allForks.SignedBlindedBeaconBlock;
signedBlindedBlock: SignedBlindedBeaconBlock;
broadcastValidation?: BroadcastValidation;
},
{body: unknown; headers: {[MetaHeader.Version]: string}; query: {broadcast_validation?: string}},
Expand Down Expand Up @@ -267,9 +277,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
return {
body:
config.getForkSeq(slot) < ForkSeq.deneb
? config
.getForkTypes(slot)
.SignedBeaconBlock.toJson(signedBlockOrContents as allForks.SignedBeaconBlock)
? config.getForkTypes(slot).SignedBeaconBlock.toJson(signedBlockOrContents as SignedBeaconBlock)
: SignedBlockContentsType.toJson(signedBlockOrContents as SignedBlockContents),
headers: {
[MetaHeader.Version]: config.getForkName(slot),
Expand All @@ -286,8 +294,8 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
// Determine fork from slot in JSON payload
forkName = config.getForkName(
(body as {signed_block: unknown}).signed_block !== undefined
? (body as {signed_block: allForks.SignedBeaconBlock}).signed_block.message.slot
: (body as allForks.SignedBeaconBlock).message.slot
? (body as {signed_block: SignedBeaconBlock}).signed_block.message.slot
: (body as SignedBeaconBlock).message.slot
);
}
const forkSeq = config.forks[forkName].seq;
Expand All @@ -305,9 +313,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
return {
body:
config.getForkSeq(slot) < ForkSeq.deneb
? config
.getForkTypes(slot)
.SignedBeaconBlock.serialize(signedBlockOrContents as allForks.SignedBeaconBlock)
? config.getForkTypes(slot).SignedBeaconBlock.serialize(signedBlockOrContents as SignedBeaconBlock)
: SignedBlockContentsType.serialize(signedBlockOrContents as SignedBlockContents),
headers: {
[MetaHeader.Version]: config.getForkName(slot),
Expand Down Expand Up @@ -345,9 +351,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
return {
body:
config.getForkSeq(slot) < ForkSeq.deneb
? config
.getForkTypes(slot)
.SignedBeaconBlock.toJson(signedBlockOrContents as allForks.SignedBeaconBlock)
? config.getForkTypes(slot).SignedBeaconBlock.toJson(signedBlockOrContents as SignedBeaconBlock)
: SignedBlockContentsType.toJson(signedBlockOrContents as SignedBlockContents),
headers: {
[MetaHeader.Version]: config.getForkName(slot),
Expand All @@ -373,9 +377,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
return {
body:
config.getForkSeq(slot) < ForkSeq.deneb
? config
.getForkTypes(slot)
.SignedBeaconBlock.serialize(signedBlockOrContents as allForks.SignedBeaconBlock)
? config.getForkTypes(slot).SignedBeaconBlock.serialize(signedBlockOrContents as SignedBeaconBlock)
: SignedBlockContentsType.serialize(signedBlockOrContents as SignedBlockContents),
headers: {
[MetaHeader.Version]: config.getForkName(slot),
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/beacon/routes/debug.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {ContainerType, Type, ValueOf} from "@chainsafe/ssz";
import {ChainForkConfig} from "@lodestar/config";
import {allForks, ssz, StringType} from "@lodestar/types";
import {ssz, StringType, BeaconState} from "@lodestar/types";
import {
ArrayOf,
EmptyArgs,
Expand Down Expand Up @@ -98,7 +98,7 @@ export type Endpoints = {
"GET",
StateArgs,
{params: {state_id: string}},
allForks.BeaconState,
BeaconState,
ExecutionOptimisticFinalizedAndVersionMeta
>;
};
Expand Down Expand Up @@ -136,7 +136,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
},
},
resp: {
data: WithVersion((fork) => ssz[fork].BeaconState as Type<allForks.BeaconState>),
data: WithVersion((fork) => ssz[fork].BeaconState as Type<BeaconState>),
meta: ExecutionOptimisticFinalizedAndVersionCodec,
},
init: {
Expand Down
21 changes: 17 additions & 4 deletions packages/api/src/beacon/routes/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import {ContainerType, ValueOf} from "@chainsafe/ssz";
import {ChainForkConfig} from "@lodestar/config";
import {Epoch, phase0, capella, Slot, ssz, StringType, RootHex, altair, UintNum64, allForks} from "@lodestar/types";
import {
Epoch,
phase0,
capella,
Slot,
ssz,
StringType,
RootHex,
altair,
UintNum64,
LightClientOptimisticUpdate,
LightClientFinalityUpdate,
SSEPayloadAttributes,
} from "@lodestar/types";
import {ForkName} from "@lodestar/params";

import {Endpoint, RouteDefinitions, Schema} from "../../utils/index.js";
Expand Down Expand Up @@ -113,9 +126,9 @@ export type EventData = {
executionOptimistic: boolean;
};
[EventType.contributionAndProof]: altair.SignedContributionAndProof;
[EventType.lightClientOptimisticUpdate]: {version: ForkName; data: allForks.LightClientOptimisticUpdate};
[EventType.lightClientFinalityUpdate]: {version: ForkName; data: allForks.LightClientFinalityUpdate};
[EventType.payloadAttributes]: {version: ForkName; data: allForks.SSEPayloadAttributes};
[EventType.lightClientOptimisticUpdate]: {version: ForkName; data: LightClientOptimisticUpdate};
[EventType.lightClientFinalityUpdate]: {version: ForkName; data: LightClientFinalityUpdate};
[EventType.payloadAttributes]: {version: ForkName; data: SSEPayloadAttributes};
[EventType.blobSidecar]: BlobSidecarSSE;
};

Expand Down
23 changes: 15 additions & 8 deletions packages/api/src/beacon/routes/lightclient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {ListCompositeType, ValueOf} from "@chainsafe/ssz";
import {ssz, SyncPeriod, allForks} from "@lodestar/types";
import {
LightClientBootstrap,
LightClientFinalityUpdate,
LightClientOptimisticUpdate,
LightClientUpdate,
ssz,
SyncPeriod,
} from "@lodestar/types";
import {ForkName} from "@lodestar/params";
import {ChainForkConfig} from "@lodestar/config";
import {Endpoint, RouteDefinitions, Schema} from "../../utils/index.js";
Expand Down Expand Up @@ -33,7 +40,7 @@ export type Endpoints = {
"GET",
{startPeriod: SyncPeriod; count: number},
{query: {start_period: number; count: number}},
allForks.LightClientUpdate[],
LightClientUpdate[],
{versions: ForkName[]}
>;

Expand All @@ -46,7 +53,7 @@ export type Endpoints = {
"GET",
EmptyArgs,
EmptyRequest,
allForks.LightClientOptimisticUpdate,
LightClientOptimisticUpdate,
VersionMeta
>;

Expand All @@ -55,7 +62,7 @@ export type Endpoints = {
"GET",
EmptyArgs,
EmptyRequest,
allForks.LightClientFinalityUpdate,
LightClientFinalityUpdate,
VersionMeta
>;

Expand All @@ -68,7 +75,7 @@ export type Endpoints = {
"GET",
{blockRoot: string},
{params: {block_root: string}},
allForks.LightClientBootstrap,
LightClientBootstrap,
VersionMeta
>;

Expand Down Expand Up @@ -105,7 +112,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
},
fromJson: (data, meta) => {
const updates = data as unknown[];
const value: allForks.LightClientUpdate[] = [];
const value: LightClientUpdate[] = [];
for (let i = 0; i < updates.length; i++) {
const version = meta.versions[i];
value.push(getLightClientForkTypes(version).LightClientUpdate.fromJson(updates[i]));
Expand All @@ -132,9 +139,9 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
if (!Array.isArray(resp)) {
throw Error("JSON is not an array");
}
const updates: allForks.LightClientUpdate[] = [];
const updates: LightClientUpdate[] = [];
const meta: {versions: ForkName[]} = {versions: []};
for (const {data, version} of resp as {data: allForks.LightClientUpdate; version: string}[]) {
for (const {data, version} of resp as {data: LightClientUpdate; version: string}[]) {
updates.push(data);
meta.versions.push(toForkName(version));
}
Expand Down
14 changes: 7 additions & 7 deletions packages/api/src/beacon/routes/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {ContainerType, fromHexString, toHexString, Type, ValueOf} from "@chainsa
import {ChainForkConfig} from "@lodestar/config";
import {isForkBlobs} from "@lodestar/params";
import {
allForks,
altair,
BLSSignature,
CommitteeIndex,
Expand All @@ -16,6 +15,8 @@ import {
ValidatorIndex,
ProducedBlockSource,
stringType,
BeaconBlockOrContents,
BlindedBeaconBlock,
} from "@lodestar/types";
import {Endpoint, RouteDefinitions, Schema} from "../../utils/index.js";
import {fromGraffitiHex, toBoolean, toGraffitiHex} from "../../utils/serdes.js";
Expand Down Expand Up @@ -315,7 +316,7 @@ export type Endpoints = {
strict_fee_recipient_check?: boolean;
};
},
allForks.BeaconBlockOrContents,
BeaconBlockOrContents,
VersionMeta
>;

Expand Down Expand Up @@ -349,7 +350,7 @@ export type Endpoints = {
blinded_local?: boolean;
};
},
allForks.FullOrBlindedBeaconBlockOrContents,
BeaconBlockOrContents | BlindedBeaconBlock,
ProduceBlockV3Meta
>;

Expand All @@ -361,7 +362,7 @@ export type Endpoints = {
graffiti: string;
},
{params: {slot: number}; query: {randao_reveal: string; graffiti: string}},
allForks.BlindedBeaconBlock,
BlindedBeaconBlock,
VersionMeta
>;

Expand Down Expand Up @@ -623,8 +624,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
},
resp: {
data: WithVersion(
(fork) =>
(isForkBlobs(fork) ? BlockContentsType : ssz[fork].BeaconBlock) as Type<allForks.BeaconBlockOrContents>
(fork) => (isForkBlobs(fork) ? BlockContentsType : ssz[fork].BeaconBlock) as Type<BeaconBlockOrContents>
),
meta: VersionCodec,
},
Expand Down Expand Up @@ -688,7 +688,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
? getBlindedForkTypes(version).BeaconBlock
: isForkBlobs(version)
? BlockContentsType
: ssz[version].BeaconBlock) as Type<allForks.FullOrBlindedBeaconBlockOrContents>
: ssz[version].BeaconBlock) as Type<BeaconBlockOrContents | BlindedBeaconBlock>
),
meta: {
toJson: (meta) => ({
Expand Down
30 changes: 19 additions & 11 deletions packages/api/src/builder/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {fromHexString, toHexString} from "@chainsafe/ssz";
import {ssz, allForks, bellatrix, Slot, Root, BLSPubkey} from "@lodestar/types";
import {
ssz,
bellatrix,
Slot,
Root,
BLSPubkey,
ExecutionPayload,
ExecutionPayloadAndBlobsBundle,
SignedBlindedBeaconBlock,
SignedBuilderBid,
} from "@lodestar/types";
import {ForkName, isForkBlobs} from "@lodestar/params";
import {ChainForkConfig} from "@lodestar/config";

Expand All @@ -26,7 +36,7 @@ import {fromHeaders} from "../utils/headers.js";
// In this case, we receive a success response (204) which is not handled as an error. The generic response
// handler already checks the status code and will not attempt to parse the body, but it will return no value.
// It is important that this type indicates that there might be no value to ensure it is properly handled downstream.
export type MaybeSignedBuilderBid = allForks.SignedBuilderBid | undefined;
export type MaybeSignedBuilderBid = SignedBuilderBid | undefined;

const RegistrationsType = ArrayOf(ssz.bellatrix.SignedValidatorRegistrationV1);

Expand Down Expand Up @@ -62,9 +72,9 @@ export type Endpoints = {

submitBlindedBlock: Endpoint<
"POST",
{signedBlindedBlock: allForks.SignedBlindedBeaconBlock},
{signedBlindedBlock: SignedBlindedBeaconBlock},
{body: unknown; headers: {[MetaHeader.Version]: string}},
allForks.ExecutionPayload | allForks.ExecutionPayloadAndBlobsBundle,
ExecutionPayload | ExecutionPayloadAndBlobsBundle,
VersionMeta
>;
};
Expand Down Expand Up @@ -138,13 +148,11 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
},
}),
resp: {
data: WithVersion<allForks.ExecutionPayload | allForks.ExecutionPayloadAndBlobsBundle, VersionMeta>(
(fork: ForkName) => {
return isForkBlobs(fork)
? ssz.allForksBlobs[fork].ExecutionPayloadAndBlobsBundle
: getExecutionForkTypes(fork).ExecutionPayload;
}
),
data: WithVersion<ExecutionPayload | ExecutionPayloadAndBlobsBundle, VersionMeta>((fork: ForkName) => {
return isForkBlobs(fork)
? ssz.allForksBlobs[fork].ExecutionPayloadAndBlobsBundle
: getExecutionForkTypes(fork).ExecutionPayload;
}),
meta: VersionCodec,
},
},
Expand Down
Loading
Loading