Skip to content

Commit

Permalink
Review PR
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Jul 30, 2024
1 parent d17902b commit c6e1cca
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 84 deletions.
78 changes: 32 additions & 46 deletions packages/api/src/beacon/routes/beacon/pool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {ValueOf} from "@chainsafe/ssz";
import {ChainForkConfig} from "@lodestar/config";
import {ForkPreElectra, ForkSeq, isForkElectra} from "@lodestar/params";
import {isForkElectra} from "@lodestar/params";
import {phase0, capella, CommitteeIndex, Slot, ssz, electra, AttesterSlashing} from "@lodestar/types";
import {Schema, Endpoint, RouteDefinitions} from "../../../utils/index.js";
import {
Expand Down Expand Up @@ -171,7 +171,7 @@ export type Endpoints = {
*/
submitPoolAttesterSlashings: Endpoint<
"POST",
{attesterSlashing: AttesterSlashing<ForkPreElectra>},
{attesterSlashing: phase0.AttesterSlashing},
{body: unknown},
EmptyResponseData,
EmptyMeta
Expand Down Expand Up @@ -334,44 +334,37 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
method: "POST",
req: {
writeReqJson: ({signedAttestations}) => {
const fork = config.getForkName(signedAttestations[0].data.slot);
const fork = config.getForkName(signedAttestations[0]?.data.slot ?? 0);
return {
body:
ForkSeq[fork] >= ForkSeq.electra
? AttestationListTypeElectra.toJson(signedAttestations as AttestationListElectra)
: AttestationListTypePhase0.toJson(signedAttestations as AttestationListPhase0),
body: isForkElectra(fork)
? AttestationListTypeElectra.toJson(signedAttestations as AttestationListElectra)
: AttestationListTypePhase0.toJson(signedAttestations as AttestationListPhase0),
headers: {[MetaHeader.Version]: fork},
};
},
parseReqJson: ({body, headers}) => {
const versionHeader = fromHeaders(headers, MetaHeader.Version, true);
const fork = toForkName(versionHeader);

const fork = toForkName(fromHeaders(headers, MetaHeader.Version));
return {
signedAttestations:
ForkSeq[fork] >= ForkSeq.electra
? AttestationListTypeElectra.fromJson(body)
: AttestationListTypePhase0.fromJson(body),
signedAttestations: isForkElectra(fork)
? AttestationListTypeElectra.fromJson(body)
: AttestationListTypePhase0.fromJson(body),
};
},
writeReqSsz: ({signedAttestations}) => {
const fork = config.getForkName(signedAttestations[0].data.slot);
const fork = config.getForkName(signedAttestations[0]?.data.slot ?? 0);
return {
body:
ForkSeq[fork] >= ForkSeq.electra
? AttestationListTypeElectra.serialize(signedAttestations as AttestationListElectra)
: AttestationListTypePhase0.serialize(signedAttestations as AttestationListPhase0),
body: isForkElectra(fork)
? AttestationListTypeElectra.serialize(signedAttestations as AttestationListElectra)
: AttestationListTypePhase0.serialize(signedAttestations as AttestationListPhase0),
headers: {[MetaHeader.Version]: fork},
};
},
parseReqSsz: ({body, headers}) => {
const versionHeader = fromHeaders(headers, MetaHeader.Version, true);
const fork = toForkName(versionHeader);
const fork = toForkName(fromHeaders(headers, MetaHeader.Version));
return {
signedAttestations:
ForkSeq[fork] >= ForkSeq.electra
? AttestationListTypeElectra.deserialize(body)
: AttestationListTypePhase0.deserialize(body),
signedAttestations: isForkElectra(fork)
? AttestationListTypeElectra.deserialize(body)
: AttestationListTypePhase0.deserialize(body),
};
},
schema: {
Expand Down Expand Up @@ -402,42 +395,35 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
writeReqJson: ({attesterSlashing}) => {
const fork = config.getForkName(Number(attesterSlashing.attestation1.data.slot));
return {
body:
ForkSeq[fork] >= ForkSeq.electra
? ssz.electra.AttesterSlashing.toJson(attesterSlashing)
: ssz.phase0.AttesterSlashing.toJson(attesterSlashing),
body: isForkElectra(fork)
? ssz.electra.AttesterSlashing.toJson(attesterSlashing)
: ssz.phase0.AttesterSlashing.toJson(attesterSlashing),
headers: {[MetaHeader.Version]: fork},
};
},
parseReqJson: ({body, headers}) => {
const versionHeader = fromHeaders(headers, MetaHeader.Version, true);
const fork = toForkName(versionHeader);

const fork = toForkName(fromHeaders(headers, MetaHeader.Version));
return {
attesterSlashing:
ForkSeq[fork] >= ForkSeq.electra
? ssz.electra.AttesterSlashing.fromJson(body)
: ssz.phase0.AttesterSlashing.fromJson(body),
attesterSlashing: isForkElectra(fork)
? ssz.electra.AttesterSlashing.fromJson(body)
: ssz.phase0.AttesterSlashing.fromJson(body),
};
},
writeReqSsz: ({attesterSlashing}) => {
const fork = config.getForkName(Number(attesterSlashing.attestation1.data.slot));
return {
body:
ForkSeq[fork] >= ForkSeq.electra
? ssz.electra.AttesterSlashing.serialize(attesterSlashing as electra.AttesterSlashing)
: ssz.electra.AttesterSlashing.serialize(attesterSlashing as phase0.AttesterSlashing),
body: isForkElectra(fork)
? ssz.electra.AttesterSlashing.serialize(attesterSlashing as electra.AttesterSlashing)
: ssz.electra.AttesterSlashing.serialize(attesterSlashing as phase0.AttesterSlashing),
headers: {[MetaHeader.Version]: fork},
};
},
parseReqSsz: ({body, headers}) => {
const versionHeader = fromHeaders(headers, MetaHeader.Version, true);
const fork = toForkName(versionHeader);
const fork = toForkName(fromHeaders(headers, MetaHeader.Version));
return {
attesterSlashing:
ForkSeq[fork] >= ForkSeq.electra
? ssz.electra.AttesterSlashing.deserialize(body)
: ssz.phase0.AttesterSlashing.deserialize(body),
attesterSlashing: isForkElectra(fork)
? ssz.electra.AttesterSlashing.deserialize(body)
: ssz.phase0.AttesterSlashing.deserialize(body),
};
},
schema: {
Expand Down
63 changes: 27 additions & 36 deletions packages/api/src/beacon/routes/validator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {ContainerType, fromHexString, toHexString, Type, ValueOf} from "@chainsafe/ssz";
import {ChainForkConfig} from "@lodestar/config";
import {isForkBlobs, ForkSeq} from "@lodestar/params";
import {isForkBlobs, isForkElectra} from "@lodestar/params";
import {
altair,
BLSSignature,
Expand Down Expand Up @@ -202,7 +202,7 @@ export const AttesterDutyListType = ArrayOf(AttesterDutyType);
export const ProposerDutyListType = ArrayOf(ProposerDutyType);
export const SyncDutyListType = ArrayOf(SyncDutyType);
export const SignedAggregateAndProofListPhase0Type = ArrayOf(ssz.phase0.SignedAggregateAndProof);
export const SignedAggregateAndProofListElectaType = ArrayOf(ssz.electra.SignedAggregateAndProof);
export const SignedAggregateAndProofListElectraType = ArrayOf(ssz.electra.SignedAggregateAndProof);
export const SignedContributionAndProofListType = ArrayOf(ssz.altair.SignedContributionAndProof);
export const BeaconCommitteeSubscriptionListType = ArrayOf(BeaconCommitteeSubscriptionType);
export const SyncCommitteeSubscriptionListType = ArrayOf(SyncCommitteeSubscriptionType);
Expand All @@ -220,8 +220,8 @@ export type ProposerDutyList = ValueOf<typeof ProposerDutyListType>;
export type SyncDuty = ValueOf<typeof SyncDutyType>;
export type SyncDutyList = ValueOf<typeof SyncDutyListType>;
export type SignedAggregateAndProofListPhase0 = ValueOf<typeof SignedAggregateAndProofListPhase0Type>;
export type SignedAggregateAndProofListElecta = ValueOf<typeof SignedAggregateAndProofListElectaType>;
export type SignedAggregateAndProofList = SignedAggregateAndProofListPhase0 | SignedAggregateAndProofListElecta;
export type SignedAggregateAndProofListElectra = ValueOf<typeof SignedAggregateAndProofListElectraType>;
export type SignedAggregateAndProofList = SignedAggregateAndProofListPhase0 | SignedAggregateAndProofListElectra;
export type SignedContributionAndProofList = ValueOf<typeof SignedContributionAndProofListType>;
export type BeaconCommitteeSubscription = ValueOf<typeof BeaconCommitteeSubscriptionType>;
export type BeaconCommitteeSubscriptionList = ValueOf<typeof BeaconCommitteeSubscriptionListType>;
Expand Down Expand Up @@ -870,9 +870,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
},
},
resp: {
data: WithVersion((fork) =>
ForkSeq[fork] >= ForkSeq.electra ? ssz.electra.Attestation : ssz.phase0.Attestation
),
data: WithVersion((fork) => (isForkElectra(fork) ? ssz.electra.Attestation : ssz.phase0.Attestation)),
meta: VersionCodec,
},
},
Expand Down Expand Up @@ -901,50 +899,43 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
writeReqJson: ({signedAggregateAndProofs}) => {
const fork = config.getForkName(signedAggregateAndProofs[0]?.message.aggregate.data.slot ?? 0);
return {
body:
ForkSeq[fork] >= ForkSeq.electra
? SignedAggregateAndProofListElectaType.toJson(
signedAggregateAndProofs as SignedAggregateAndProofListElecta
)
: SignedAggregateAndProofListPhase0Type.toJson(
signedAggregateAndProofs as SignedAggregateAndProofListPhase0
),
body: isForkElectra(fork)
? SignedAggregateAndProofListElectraType.toJson(
signedAggregateAndProofs as SignedAggregateAndProofListElectra
)
: SignedAggregateAndProofListPhase0Type.toJson(
signedAggregateAndProofs as SignedAggregateAndProofListPhase0
),
headers: {[MetaHeader.Version]: fork},
};
},
parseReqJson: ({body, headers}) => {
const versionHeader = fromHeaders(headers, MetaHeader.Version, true);
const fork = toForkName(versionHeader);

const fork = toForkName(fromHeaders(headers, MetaHeader.Version));
return {
signedAggregateAndProofs:
ForkSeq[fork] >= ForkSeq.electra
? SignedAggregateAndProofListElectaType.fromJson(body)
: SignedAggregateAndProofListPhase0Type.fromJson(body),
signedAggregateAndProofs: isForkElectra(fork)
? SignedAggregateAndProofListElectraType.fromJson(body)
: SignedAggregateAndProofListPhase0Type.fromJson(body),
};
},
writeReqSsz: ({signedAggregateAndProofs}) => {
const fork = config.getForkName(signedAggregateAndProofs[0]?.message.aggregate.data.slot ?? 0);
return {
body:
ForkSeq[fork] >= ForkSeq.electra
? SignedAggregateAndProofListElectaType.serialize(
signedAggregateAndProofs as SignedAggregateAndProofListElecta
)
: SignedAggregateAndProofListPhase0Type.serialize(
signedAggregateAndProofs as SignedAggregateAndProofListPhase0
),
body: isForkElectra(fork)
? SignedAggregateAndProofListElectraType.serialize(
signedAggregateAndProofs as SignedAggregateAndProofListElectra
)
: SignedAggregateAndProofListPhase0Type.serialize(
signedAggregateAndProofs as SignedAggregateAndProofListPhase0
),
headers: {[MetaHeader.Version]: fork},
};
},
parseReqSsz: ({body, headers}) => {
const versionHeader = fromHeaders(headers, MetaHeader.Version, true);
const fork = toForkName(versionHeader);
const fork = toForkName(fromHeaders(headers, MetaHeader.Version));
return {
signedAggregateAndProofs:
ForkSeq[fork] >= ForkSeq.electra
? SignedAggregateAndProofListElectaType.deserialize(body)
: SignedAggregateAndProofListPhase0Type.deserialize(body),
signedAggregateAndProofs: isForkElectra(fork)
? SignedAggregateAndProofListElectraType.deserialize(body)
: SignedAggregateAndProofListPhase0Type.deserialize(body),
};
},
schema: {
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/api/impl/beacon/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function getBeaconPoolApi({
},

async getPoolAttesterSlashingsV2() {
// TODO Electra: Determine fork based on data returned by api
return {data: chain.opPool.getAllAttesterSlashings(), meta: {version: ForkName.phase0}};
},

Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/api/impl/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ export function getValidatorApi({
if (!aggregate) {
throw new ApiError(
404,
`No aggregated attestation for slot=${slot} committeeIndex=${committeeIndex}, dataRoot=${dataRootHex}`
`No aggregated attestation for slot=${slot}, committeeIndex=${committeeIndex}, dataRoot=${dataRootHex}`
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/chain/opPools/attestationPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function attestationToAggregate(attestation: Attestation): AggregateFast {
}

/**
* Unwrap AggregateFast to phase0.Attestation
* Unwrap AggregateFast to Attestation
*/
function fastToAttestation(aggFast: AggregateFast): Attestation {
return {...aggFast, signature: aggFast.signature.toBytes(PointFormat.compressed)};
Expand Down

0 comments on commit c6e1cca

Please sign in to comment.