Skip to content

Commit

Permalink
Merge branch 'unstable' into mkeil/dedup-beacon-block-3
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Sep 6, 2024
2 parents ac4cf60 + b05c93e commit fd6319e
Show file tree
Hide file tree
Showing 233 changed files with 6,555 additions and 1,096 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "root",
"private": true,
"engines": {
"node": ">=20.1.0 <21 || >=22 <22.5"
"node": ">=20.1.0 <21 || >=22 <23"
},
"packageManager": "yarn@1.22.22+sha256.c17d3797fb9a9115bf375e31bfd30058cac6bc9c3b8807a3d8cb2094794b51ca",
"workspaces": [
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"dependencies": {
"@chainsafe/persistent-merkle-tree": "^0.8.0",
"@chainsafe/ssz": "^0.17.0",
"@chainsafe/ssz": "^0.17.1",
"@lodestar/config": "^1.21.0",
"@lodestar/params": "^1.21.0",
"@lodestar/types": "^1.21.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/beacon/client/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ApiClient = ApiClientMethods<Endpoints>;
*/
export function getClient(config: ChainForkConfig, baseUrl: string): ApiClient {
const definitions = getDefinitions(config);
const eventSerdes = getEventSerdes();
const eventSerdes = getEventSerdes(config);

return {
eventstream: async ({topics, signal, onEvent, onError, onClose}) => {
Expand Down
25 changes: 23 additions & 2 deletions packages/api/src/beacon/routes/beacon/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
SignedBlockContents,
sszTypesFor,
} from "@lodestar/types";
import {ForkName, ForkPreExecution, isForkBlobs, isForkExecution} from "@lodestar/params";
import {ForkName, ForkPreElectra, ForkPreExecution, isForkBlobs, isForkExecution} from "@lodestar/params";
import {Endpoint, RequestCodec, RouteDefinitions, Schema} from "../../../utils/index.js";
import {EmptyMeta, EmptyResponseCodec, EmptyResponseData, WithVersion} from "../../../utils/codecs.js";
import {
Expand Down Expand Up @@ -101,10 +101,22 @@ export type Endpoints = {
"GET",
BlockArgs,
{params: {block_id: string}},
BeaconBlockBody["attestations"],
BeaconBlockBody<ForkPreElectra>["attestations"],
ExecutionOptimisticAndFinalizedMeta
>;

/**
* Get block attestations
* Retrieves attestation included in requested block.
*/
getBlockAttestationsV2: Endpoint<
"GET",
BlockArgs,
{params: {block_id: string}},
BeaconBlockBody["attestations"],
ExecutionOptimisticFinalizedAndVersionMeta
>;

/**
* Get block header
* Retrieves block header for given block id.
Expand Down Expand Up @@ -251,6 +263,15 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
meta: ExecutionOptimisticAndFinalizedCodec,
},
},
getBlockAttestationsV2: {
url: "/eth/v2/beacon/blocks/{block_id}/attestations",
method: "GET",
req: blockIdOnlyReq,
resp: {
data: WithVersion((fork) => ssz[fork].BeaconBlockBody.fields.attestations),
meta: ExecutionOptimisticFinalizedAndVersionCodec,
},
},
getBlockHeader: {
url: "/eth/v1/beacon/headers/{block_id}",
method: "GET",
Expand Down
210 changes: 195 additions & 15 deletions packages/api/src/beacon/routes/beacon/pool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {ValueOf} from "@chainsafe/ssz";
import {ChainForkConfig} from "@lodestar/config";
import {phase0, capella, CommitteeIndex, Slot, ssz} from "@lodestar/types";
import {isForkPostElectra} from "@lodestar/params";
import {phase0, capella, CommitteeIndex, Slot, ssz, electra, AttesterSlashing} from "@lodestar/types";
import {Schema, Endpoint, RouteDefinitions} from "../../../utils/index.js";
import {
ArrayOf,
Expand All @@ -12,19 +13,31 @@ import {
EmptyRequest,
EmptyResponseCodec,
EmptyResponseData,
WithVersion,
} from "../../../utils/codecs.js";
import {MetaHeader, VersionCodec, VersionMeta} from "../../../utils/metadata.js";
import {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

const AttestationListType = ArrayOf(ssz.phase0.Attestation);
const AttesterSlashingListType = ArrayOf(ssz.phase0.AttesterSlashing);
const AttestationListTypePhase0 = ArrayOf(ssz.phase0.Attestation);
const AttestationListTypeElectra = ArrayOf(ssz.electra.Attestation);
const AttesterSlashingListTypePhase0 = ArrayOf(ssz.phase0.AttesterSlashing);
const AttesterSlashingListTypeElectra = ArrayOf(ssz.electra.AttesterSlashing);
const ProposerSlashingListType = ArrayOf(ssz.phase0.ProposerSlashing);
const SignedVoluntaryExitListType = ArrayOf(ssz.phase0.SignedVoluntaryExit);
const SignedBLSToExecutionChangeListType = ArrayOf(ssz.capella.SignedBLSToExecutionChange);
const SyncCommitteeMessageListType = ArrayOf(ssz.altair.SyncCommitteeMessage);

type AttestationList = ValueOf<typeof AttestationListType>;
type AttesterSlashingList = ValueOf<typeof AttesterSlashingListType>;
type AttestationListPhase0 = ValueOf<typeof AttestationListTypePhase0>;
type AttestationListElectra = ValueOf<typeof AttestationListTypeElectra>;
type AttestationList = AttestationListPhase0 | AttestationListElectra;

type AttesterSlashingListPhase0 = ValueOf<typeof AttesterSlashingListTypePhase0>;
type AttesterSlashingListElectra = ValueOf<typeof AttesterSlashingListTypeElectra>;
type AttesterSlashingList = AttesterSlashingListPhase0 | AttesterSlashingListElectra;

type ProposerSlashingList = ValueOf<typeof ProposerSlashingListType>;
type SignedVoluntaryExitList = ValueOf<typeof SignedVoluntaryExitListType>;
type SignedBLSToExecutionChangeList = ValueOf<typeof SignedBLSToExecutionChangeListType>;
Expand All @@ -39,10 +52,22 @@ export type Endpoints = {
"GET",
{slot?: Slot; committeeIndex?: CommitteeIndex},
{query: {slot?: number; committee_index?: number}},
AttestationList,
AttestationListPhase0,
EmptyMeta
>;

/**
* Get Attestations from operations pool
* Retrieves attestations known by the node but not necessarily incorporated into any block
*/
getPoolAttestationsV2: Endpoint<
"GET",
{slot?: Slot; committeeIndex?: CommitteeIndex},
{query: {slot?: number; committee_index?: number}},
AttestationList,
VersionMeta
>;

/**
* Get AttesterSlashings from operations pool
* Retrieves attester slashings known by the node but not necessarily incorporated into any block
Expand All @@ -52,10 +77,23 @@ export type Endpoints = {
"GET",
EmptyArgs,
EmptyRequest,
AttesterSlashingList,
AttesterSlashingListPhase0,
EmptyMeta
>;

/**
* Get AttesterSlashings from operations pool
* Retrieves attester slashings known by the node but not necessarily incorporated into any block
*/
getPoolAttesterSlashingsV2: Endpoint<
// ⏎
"GET",
EmptyArgs,
EmptyRequest,
AttesterSlashingList,
VersionMeta
>;

/**
* Get ProposerSlashings from operations pool
* Retrieves proposer slashings known by the node but not necessarily incorporated into any block
Expand Down Expand Up @@ -105,12 +143,28 @@ export type Endpoints = {
*/
submitPoolAttestations: Endpoint<
"POST",
{signedAttestations: AttestationList},
{signedAttestations: AttestationListPhase0},
{body: unknown},
EmptyResponseData,
EmptyMeta
>;

/**
* Submit Attestation objects to node
* Submits Attestation objects to the node. Each attestation in the request body is processed individually.
*
* If an attestation is validated successfully the node MUST publish that attestation on the appropriate subnet.
*
* If one or more attestations fail validation the node MUST return a 400 error with details of which attestations have failed, and why.
*/
submitPoolAttestationsV2: Endpoint<
"POST",
{signedAttestations: AttestationList},
{body: unknown; headers: {[MetaHeader.Version]: string}},
EmptyResponseData,
EmptyMeta
>;

/**
* Submit AttesterSlashing object to node's pool
* Submits AttesterSlashing object to node's pool and if passes validation node MUST broadcast it to network.
Expand All @@ -123,6 +177,18 @@ export type Endpoints = {
EmptyMeta
>;

/**
* Submit AttesterSlashing object to node's pool
* Submits AttesterSlashing object to node's pool and if passes validation node MUST broadcast it to network.
*/
submitPoolAttesterSlashingsV2: Endpoint<
"POST",
{attesterSlashing: AttesterSlashing},
{body: unknown; headers: {[MetaHeader.Version]: string}},
EmptyResponseData,
EmptyMeta
>;

/**
* Submit ProposerSlashing object to node's pool
* Submits ProposerSlashing object to node's pool and if passes validation node MUST broadcast it to network.
Expand Down Expand Up @@ -172,7 +238,7 @@ export type Endpoints = {
>;
};

export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpoints> {
export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoints> {
return {
getPoolAttestations: {
url: "/eth/v1/beacon/pool/attestations",
Expand All @@ -183,19 +249,43 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
schema: {query: {slot: Schema.Uint, committee_index: Schema.Uint}},
},
resp: {
data: AttestationListType,
data: AttestationListTypePhase0,
meta: EmptyMetaCodec,
},
},
getPoolAttestationsV2: {
url: "/eth/v2/beacon/pool/attestations",
method: "GET",
req: {
writeReq: ({slot, committeeIndex}) => ({query: {slot, committee_index: committeeIndex}}),
parseReq: ({query}) => ({slot: query.slot, committeeIndex: query.committee_index}),
schema: {query: {slot: Schema.Uint, committee_index: Schema.Uint}},
},
resp: {
data: WithVersion((fork) => (isForkPostElectra(fork) ? AttestationListTypeElectra : AttestationListTypePhase0)),
meta: VersionCodec,
},
},
getPoolAttesterSlashings: {
url: "/eth/v1/beacon/pool/attester_slashings",
method: "GET",
req: EmptyRequestCodec,
resp: {
data: AttesterSlashingListType,
data: AttesterSlashingListTypePhase0,
meta: EmptyMetaCodec,
},
},
getPoolAttesterSlashingsV2: {
url: "/eth/v2/beacon/pool/attester_slashings",
method: "GET",
req: EmptyRequestCodec,
resp: {
data: WithVersion((fork) =>
isForkPostElectra(fork) ? AttesterSlashingListTypeElectra : AttesterSlashingListTypePhase0
),
meta: VersionCodec,
},
},
getPoolProposerSlashings: {
url: "/eth/v1/beacon/pool/proposer_slashings",
method: "GET",
Expand Down Expand Up @@ -227,16 +317,61 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
url: "/eth/v1/beacon/pool/attestations",
method: "POST",
req: {
writeReqJson: ({signedAttestations}) => ({body: AttestationListType.toJson(signedAttestations)}),
parseReqJson: ({body}) => ({signedAttestations: AttestationListType.fromJson(body)}),
writeReqSsz: ({signedAttestations}) => ({body: AttestationListType.serialize(signedAttestations)}),
parseReqSsz: ({body}) => ({signedAttestations: AttestationListType.deserialize(body)}),
writeReqJson: ({signedAttestations}) => ({body: AttestationListTypePhase0.toJson(signedAttestations)}),
parseReqJson: ({body}) => ({signedAttestations: AttestationListTypePhase0.fromJson(body)}),
writeReqSsz: ({signedAttestations}) => ({body: AttestationListTypePhase0.serialize(signedAttestations)}),
parseReqSsz: ({body}) => ({signedAttestations: AttestationListTypePhase0.deserialize(body)}),
schema: {
body: Schema.ObjectArray,
},
},
resp: EmptyResponseCodec,
},
submitPoolAttestationsV2: {
url: "/eth/v2/beacon/pool/attestations",
method: "POST",
req: {
writeReqJson: ({signedAttestations}) => {
const fork = config.getForkName(signedAttestations[0]?.data.slot ?? 0);
return {
body: isForkPostElectra(fork)
? AttestationListTypeElectra.toJson(signedAttestations as AttestationListElectra)
: AttestationListTypePhase0.toJson(signedAttestations as AttestationListPhase0),
headers: {[MetaHeader.Version]: fork},
};
},
parseReqJson: ({body, headers}) => {
const fork = toForkName(fromHeaders(headers, MetaHeader.Version));
return {
signedAttestations: isForkPostElectra(fork)
? AttestationListTypeElectra.fromJson(body)
: AttestationListTypePhase0.fromJson(body),
};
},
writeReqSsz: ({signedAttestations}) => {
const fork = config.getForkName(signedAttestations[0]?.data.slot ?? 0);
return {
body: isForkPostElectra(fork)
? AttestationListTypeElectra.serialize(signedAttestations as AttestationListElectra)
: AttestationListTypePhase0.serialize(signedAttestations as AttestationListPhase0),
headers: {[MetaHeader.Version]: fork},
};
},
parseReqSsz: ({body, headers}) => {
const fork = toForkName(fromHeaders(headers, MetaHeader.Version));
return {
signedAttestations: isForkPostElectra(fork)
? AttestationListTypeElectra.deserialize(body)
: AttestationListTypePhase0.deserialize(body),
};
},
schema: {
body: Schema.ObjectArray,
headers: {[MetaHeader.Version]: Schema.String},
},
},
resp: EmptyResponseCodec,
},
submitPoolAttesterSlashings: {
url: "/eth/v1/beacon/pool/attester_slashings",
method: "POST",
Expand All @@ -251,6 +386,51 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
},
resp: EmptyResponseCodec,
},
submitPoolAttesterSlashingsV2: {
url: "/eth/v2/beacon/pool/attester_slashings",
method: "POST",
req: {
writeReqJson: ({attesterSlashing}) => {
const fork = config.getForkName(Number(attesterSlashing.attestation1.data.slot));
return {
body: isForkPostElectra(fork)
? ssz.electra.AttesterSlashing.toJson(attesterSlashing)
: ssz.phase0.AttesterSlashing.toJson(attesterSlashing),
headers: {[MetaHeader.Version]: fork},
};
},
parseReqJson: ({body, headers}) => {
const fork = toForkName(fromHeaders(headers, MetaHeader.Version));
return {
attesterSlashing: isForkPostElectra(fork)
? ssz.electra.AttesterSlashing.fromJson(body)
: ssz.phase0.AttesterSlashing.fromJson(body),
};
},
writeReqSsz: ({attesterSlashing}) => {
const fork = config.getForkName(Number(attesterSlashing.attestation1.data.slot));
return {
body: isForkPostElectra(fork)
? ssz.electra.AttesterSlashing.serialize(attesterSlashing as electra.AttesterSlashing)
: ssz.phase0.AttesterSlashing.serialize(attesterSlashing as phase0.AttesterSlashing),
headers: {[MetaHeader.Version]: fork},
};
},
parseReqSsz: ({body, headers}) => {
const fork = toForkName(fromHeaders(headers, MetaHeader.Version));
return {
attesterSlashing: isForkPostElectra(fork)
? ssz.electra.AttesterSlashing.deserialize(body)
: ssz.phase0.AttesterSlashing.deserialize(body),
};
},
schema: {
body: Schema.Object,
headers: {[MetaHeader.Version]: Schema.String},
},
},
resp: EmptyResponseCodec,
},
submitPoolProposerSlashings: {
url: "/eth/v1/beacon/pool/proposer_slashings",
method: "POST",
Expand Down
Loading

0 comments on commit fd6319e

Please sign in to comment.