Skip to content

Commit

Permalink
Fix ssz_static
Browse files Browse the repository at this point in the history
  • Loading branch information
ensi321 authored and g11tech committed Jul 1, 2024
1 parent 170d2d5 commit 8614094
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ export class BeaconChain implements IBeaconChain {
// Will resolve this later
// if (cpEpoch >= (this.config.ELECTRA_FORK_EPOCH ?? Infinity)) {
// // finalizedState can be safely casted to Electra state since cp is already post-Electra
// if (finalizedState.eth1DepositIndex >= (finalizedState as CachedBeaconStateElectra).depositRequestsStartIndex) {
// if (finalizedState.eth1DepositIndex >= (finalizedState as CachedBeaconStateElectra).depositReceiptsStartIndex) {
// // Signal eth1 to stop polling eth1Data
// this.eth1.stopPollingEth1Data();
// }
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/eth1/eth1DepositDataTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ export class Eth1DepositDataTracker {
async getEth1DataAndDeposits(state: CachedBeaconStateAllForks): Promise<Eth1DataAndDeposits> {
if (
state.epochCtx.isAfterElectra() &&
state.eth1DepositIndex >= (state as CachedBeaconStateElectra).depositRequestsStartIndex
state.eth1DepositIndex >= (state as CachedBeaconStateElectra).depositReceiptsStartIndex
) {
// No need to poll eth1Data since Electra deprecates the mechanism after depositRequestsStartIndex is reached
// No need to poll eth1Data since Electra deprecates the mechanism after depositReceiptsStartIndex is reached
return {eth1Data: state.eth1Data, deposits: []};
}
const eth1Data = this.forcedEth1DataVote ?? (await this.getEth1Data(state));
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/src/execution/engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ export function serializeExecutionPayload(fork: ForkName, data: ExecutionPayload

// ELECTRA adds depositRequests/depositRequests to the ExecutionPayload
if (ForkSeq[fork] >= ForkSeq.electra) {
const {depositRequests, withdrawalRequests} = data as electra.ExecutionPayload;
payload.depositRequests = depositRequests.map(serializeDepositRequest);
const {depositReceipts, withdrawalRequests} = data as electra.ExecutionPayload;
payload.depositRequests = depositReceipts.map(serializeDepositRequest);
payload.withdrawalRequests = withdrawalRequests.map(serializeExecutionLayerWithdrawalRequest);
}

Expand Down Expand Up @@ -316,7 +316,7 @@ export function parseExecutionPayload(
`depositRequests missing for ${fork} >= electra executionPayload number=${executionPayload.blockNumber} hash=${data.blockHash}`
);
}
(executionPayload as electra.ExecutionPayload).depositRequests = depositRequests.map(deserializeDepositRequest);
(executionPayload as electra.ExecutionPayload).depositReceipts = depositRequests.map(deserializeDepositRequest);

if (withdrawalRequests == null) {
throw Error(
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/test/sim/electra-interop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ describe("executionEngine / ExecutionEngineHttp", function () {
throw Error("Historical validator length for epoch 1 or 2 is not dropped properly");
}

if (headState.depositRequestsStartIndex === UNSET_DEPOSIT_RECEIPTS_START_INDEX) {
throw Error("state.depositRequestsStartIndex is not set upon processing new deposit receipt");
if (headState.depositReceiptsStartIndex === UNSET_DEPOSIT_RECEIPTS_START_INDEX) {
throw Error("state.depositReceiptsStartIndex is not set upon processing new deposit receipt");
}

// wait for 1 slot to print current epoch stats
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/test/spec/presets/ssz_static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const sszStatic =
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
const sszType =
(sszTypesFor(fork) as Types)[typeName] ||
(ssz.electra as Types)[typeName] ||
(ssz.deneb as Types)[typeName] ||
(ssz.capella as Types)[typeName] ||
(ssz.bellatrix as Types)[typeName] ||
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/utils/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function generateState(

if (forkSeq >= ForkSeq.electra) {
const stateElectra = state as electra.BeaconState;
stateElectra.depositRequestsStartIndex = 2023n;
stateElectra.depositReceiptsStartIndex = 2023n;
stateElectra.latestExecutionPayloadHeader = ssz.electra.ExecutionPayloadHeader.defaultValue();
}

Expand Down
6 changes: 3 additions & 3 deletions packages/light-client/src/spec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ export function upgradeLightClientHeader(

// eslint-disable-next-line no-fallthrough
case ForkName.electra:
(upgradedHeader as LightClientHeader<ForkName.electra>).execution.depositRequestsRoot =
ssz.electra.LightClientHeader.fields.execution.fields.depositRequestsRoot.defaultValue();
(upgradedHeader as LightClientHeader<ForkName.electra>).execution.depositReceiptsRoot =
ssz.electra.LightClientHeader.fields.execution.fields.depositReceiptsRoot.defaultValue();
(upgradedHeader as LightClientHeader<ForkName.electra>).execution.withdrawalRequestsRoot =
ssz.electra.LightClientHeader.fields.execution.fields.withdrawalRequestsRoot.defaultValue();

Expand Down Expand Up @@ -157,7 +157,7 @@ export function isValidLightClientHeader(config: ChainForkConfig, header: LightC

if (epoch < config.ELECTRA_FORK_EPOCH) {
if (
(header as LightClientHeader<ForkName.electra>).execution.depositRequestsRoot !== undefined ||
(header as LightClientHeader<ForkName.electra>).execution.depositReceiptsRoot !== undefined ||
(header as LightClientHeader<ForkName.electra>).execution.withdrawalRequestsRoot !== undefined
) {
return false;
Expand Down
4 changes: 4 additions & 0 deletions packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,7 @@ export const BLOBSIDECAR_FIXED_SIZE = ACTIVE_PRESET === PresetName.minimal ? 131
// Electra Misc
export const UNSET_DEPOSIT_RECEIPTS_START_INDEX = 2n ** 64n - 1n;
export const FULL_EXIT_REQUEST_AMOUNT = 0;
export const NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA = 87;
export const NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA = 6;
export const FINALIZED_ROOT_DEPTH_ELECTRA = 7;
export const FINALIZED_ROOT_INDEX_ELECTRA = 169;
4 changes: 2 additions & 2 deletions packages/state-transition/src/block/processDepositRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export function processDepositRequest(
state: CachedBeaconStateElectra,
depositRequest: electra.DepositRequest
): void {
if (state.depositRequestsStartIndex === UNSET_DEPOSIT_RECEIPTS_START_INDEX) {
state.depositRequestsStartIndex = BigInt(depositRequest.index);
if (state.depositReceiptsStartIndex === UNSET_DEPOSIT_RECEIPTS_START_INDEX) {
state.depositReceiptsStartIndex = BigInt(depositRequest.index);
}

applyDeposit(fork, state, depositRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function processExecutionLayerWithdrawalRequest(
const exitQueueEpoch = computeExitEpochAndUpdateChurn(state, amountToWithdraw);
const withdrawableEpoch = exitQueueEpoch + config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY;

const pendingPartialWithdrawal = ssz.electra.PartialWithdrawal.toViewDU({
const pendingPartialWithdrawal = ssz.electra.PendingPartialWithdrawal.toViewDU({
index: validatorIndex,
amount: amountToWithdraw,
withdrawableEpoch,
Expand Down
2 changes: 1 addition & 1 deletion packages/state-transition/src/block/processOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function processOperations(
processExecutionLayerWithdrawalRequest(fork, state as CachedBeaconStateElectra, elWithdrawalRequest);
}

for (const depositRequest of bodyElectra.executionPayload.depositRequests) {
for (const depositRequest of bodyElectra.executionPayload.depositReceipts) {
processDepositRequest(fork, stateElectra, depositRequest);
}

Expand Down
14 changes: 7 additions & 7 deletions packages/state-transition/src/slot/upgradeStateToElectra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ export function upgradeStateToElectra(stateDeneb: CachedBeaconStateDeneb): Cache
stateElectraView.nextSyncCommittee = stateElectraCloned.nextSyncCommittee;
stateElectraView.latestExecutionPayloadHeader = ssz.electra.BeaconState.fields.latestExecutionPayloadHeader.toViewDU({
...stateElectraCloned.latestExecutionPayloadHeader.toValue(),
depositRequestsRoot: ssz.Root.defaultValue(),
depositReceiptsRoot: ssz.Root.defaultValue(),
withdrawalRequestsRoot: ssz.Root.defaultValue(),
});
stateElectraView.nextWithdrawalIndex = stateDeneb.nextWithdrawalIndex;
stateElectraView.nextWithdrawalValidatorIndex = stateDeneb.nextWithdrawalValidatorIndex;
stateElectraView.historicalSummaries = stateElectraCloned.historicalSummaries;

// latestExecutionPayloadHeader's depositRequestsRoot and withdrawalRequestsRoot set to zeros by default
// default value of depositRequestsStartIndex is UNSET_DEPOSIT_RECEIPTS_START_INDEX
stateElectraView.depositRequestsStartIndex = UNSET_DEPOSIT_RECEIPTS_START_INDEX;
// latestExecutionPayloadHeader's depositReceiptsRoot and withdrawalRequestsRoot set to zeros by default
// default value of depositReceiptsStartIndex is UNSET_DEPOSIT_RECEIPTS_START_INDEX
stateElectraView.depositReceiptsStartIndex = UNSET_DEPOSIT_RECEIPTS_START_INDEX;

const validatorsArr = stateElectraView.validators.getAllReadonly();

Expand Down Expand Up @@ -99,9 +99,9 @@ export function upgradeStateToElectraOriginal(stateDeneb: CachedBeaconStateDeneb
epoch: stateDeneb.epochCtx.epoch,
});

// latestExecutionPayloadHeader's depositRequestsRoot and withdrawalRequestsRoot set to zeros by default
// default value of depositRequestsStartIndex is UNSET_DEPOSIT_RECEIPTS_START_INDEX
stateElectra.depositRequestsStartIndex = UNSET_DEPOSIT_RECEIPTS_START_INDEX;
// latestExecutionPayloadHeader's depositReceiptsRoot and withdrawalRequestsRoot set to zeros by default
// default value of depositReceiptsStartIndex is UNSET_DEPOSIT_RECEIPTS_START_INDEX
stateElectra.depositReceiptsStartIndex = UNSET_DEPOSIT_RECEIPTS_START_INDEX;

const validatorsArr = stateElectra.validators.getAllReadonly();

Expand Down
4 changes: 2 additions & 2 deletions packages/state-transition/src/util/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export function getEth1DepositCount(state: CachedBeaconStateAllForks, eth1Data?:
// eth1DataIndexLimit = min(UintNum64, UintBn64) can be safely casted as UintNum64
// since the result lies within upper and lower bound of UintNum64
const eth1DataIndexLimit: UintNum64 =
eth1DataToUse.depositCount < electraState.depositRequestsStartIndex
eth1DataToUse.depositCount < electraState.depositReceiptsStartIndex
? eth1DataToUse.depositCount
: Number(electraState.depositRequestsStartIndex);
: Number(electraState.depositReceiptsStartIndex);

if (state.eth1DepositIndex < eth1DataIndexLimit) {
return Math.min(MAX_DEPOSITS, eth1DataIndexLimit - state.eth1DepositIndex);
Expand Down
4 changes: 2 additions & 2 deletions packages/state-transition/src/util/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ export function executionPayloadToPayloadHeader(fork: ForkSeq, payload: Executio
}

if (fork >= ForkSeq.electra) {
(bellatrixPayloadFields as electra.ExecutionPayloadHeader).depositRequestsRoot =
ssz.electra.DepositRequests.hashTreeRoot((payload as electra.ExecutionPayload).depositRequests);
(bellatrixPayloadFields as electra.ExecutionPayloadHeader).depositReceiptsRoot =
ssz.electra.DepositReceipts.hashTreeRoot((payload as electra.ExecutionPayload).depositReceipts);
(bellatrixPayloadFields as electra.ExecutionPayloadHeader).withdrawalRequestsRoot =
ssz.electra.ExecutionLayerWithdrawalRequests.hashTreeRoot(
(payload as electra.ExecutionPayload).withdrawalRequests
Expand Down
2 changes: 1 addition & 1 deletion packages/state-transition/src/util/genesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export function initializeBeaconStateFromEth1(
stateElectra.latestExecutionPayloadHeader =
(executionPayloadHeader as CompositeViewDU<typeof ssz.electra.ExecutionPayloadHeader>) ??
ssz.electra.ExecutionPayloadHeader.defaultViewDU();
stateElectra.depositRequestsStartIndex = UNSET_DEPOSIT_RECEIPTS_START_INDEX;
stateElectra.depositReceiptsStartIndex = UNSET_DEPOSIT_RECEIPTS_START_INDEX;
}

state.commit();
Expand Down
4 changes: 2 additions & 2 deletions packages/state-transition/test/unit/util/deposit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("getEth1DepositCount", () => {
throw Error("Not a post-Electra state");
}

postElectraState.depositRequestsStartIndex = 1000n;
postElectraState.depositReceiptsStartIndex = 1000n;
postElectraState.eth1Data.depositCount = 995;

// 1. Should get less than MAX_DEPOSIT
Expand Down Expand Up @@ -77,7 +77,7 @@ describe("getEth1DepositCount", () => {
throw Error("Not a post-Electra state");
}

postElectraState.depositRequestsStartIndex = 1000n;
postElectraState.depositReceiptsStartIndex = 1000n;
postElectraState.eth1Data.depositCount = 1005;

// Before eth1DepositIndex reaching the start index
Expand Down
28 changes: 15 additions & 13 deletions packages/types/src/electra/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
PENDING_BALANCE_DEPOSITS_LIMIT,
PENDING_PARTIAL_WITHDRAWALS_LIMIT,
PENDING_CONSOLIDATIONS_LIMIT,
FINALIZED_ROOT_DEPTH_ELECTRA,
NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA,
} from "@lodestar/params";
import {ssz as primitiveSsz} from "../primitive/index.js";
import {ssz as phase0Ssz} from "../phase0/index.js";
Expand Down Expand Up @@ -110,18 +112,18 @@ export const SignedAggregateAndProof = new ContainerType(
{typeName: "SignedAggregateAndProof", jsonCase: "eth2"}
);

export const DepositRequest = new ContainerType(
export const DepositReceipt = new ContainerType(
{
pubkey: BLSPubkey,
withdrawalCredentials: Bytes32,
amount: UintNum64,
signature: BLSSignature,
index: DepositIndex,
},
{typeName: "DepositRequest", jsonCase: "eth2"}
{typeName: "DepositReceipt", jsonCase: "eth2"}
);

export const DepositRequests = new ListCompositeType(DepositRequest, MAX_DEPOSIT_RECEIPTS_PER_PAYLOAD);
export const DepositReceipts = new ListCompositeType(DepositReceipt, MAX_DEPOSIT_RECEIPTS_PER_PAYLOAD);

export const ExecutionLayerWithdrawalRequest = new ContainerType(
{
Expand All @@ -139,7 +141,7 @@ export const ExecutionLayerWithdrawalRequests = new ListCompositeType(
export const ExecutionPayload = new ContainerType(
{
...denebSsz.ExecutionPayload.fields,
depositRequests: DepositRequests, // New in ELECTRA
depositReceipts: DepositReceipts, // New in ELECTRA
withdrawalRequests: ExecutionLayerWithdrawalRequests, // New in ELECTRA
},
{typeName: "ExecutionPayload", jsonCase: "eth2"}
Expand All @@ -148,7 +150,7 @@ export const ExecutionPayload = new ContainerType(
export const ExecutionPayloadHeader = new ContainerType(
{
...denebSsz.ExecutionPayloadHeader.fields,
depositRequestsRoot: Root, // New in ELECTRA
depositReceiptsRoot: Root, // New in ELECTRA
withdrawalRequestsRoot: Root, // New in ELECTRA
},
{typeName: "ExecutionPayloadHeader", jsonCase: "eth2"}
Expand Down Expand Up @@ -280,13 +282,13 @@ export const PendingBalanceDeposits = new ListCompositeType(
Number(PENDING_BALANCE_DEPOSITS_LIMIT)
);

export const PartialWithdrawal = new ContainerType(
export const PendingPartialWithdrawal = new ContainerType(
{
index: ValidatorIndex,
amount: Gwei,
withdrawableEpoch: Epoch,
},
{typeName: "PartialWithdrawal", jsonCase: "eth2"}
{typeName: "PendingPartialWithdrawal", jsonCase: "eth2"}
);

export const PendingConsolidation = new ContainerType(
Expand Down Expand Up @@ -340,14 +342,14 @@ export const BeaconState = new ContainerType(
nextWithdrawalValidatorIndex: capellaSsz.BeaconState.fields.nextWithdrawalValidatorIndex,
// Deep history valid from Capella onwards
historicalSummaries: capellaSsz.BeaconState.fields.historicalSummaries,
depositRequestsStartIndex: UintBn64, // New in ELECTRA:EIP6110
depositReceiptsStartIndex: UintBn64, // New in ELECTRA:EIP6110
depositBalanceToConsume: Gwei, // New in Electra:EIP7251
exitBalanceToConsume: Gwei, // New in Electra:EIP7251
earliestExitEpoch: Epoch, // New in Electra:EIP7251
consolidationBalanceToConsume: Gwei, // New in Electra:EIP7251
earliestConsolidationEpoch: Epoch, // New in Electra:EIP7251
pendingBalanceDeposits: PendingBalanceDeposits, // new in electra:eip7251
pendingPartialWithdrawals: new ListCompositeType(PartialWithdrawal, Number(PENDING_PARTIAL_WITHDRAWALS_LIMIT)), // New in Electra:EIP7251
pendingPartialWithdrawals: new ListCompositeType(PendingPartialWithdrawal, Number(PENDING_PARTIAL_WITHDRAWALS_LIMIT)), // New in Electra:EIP7251
pendingConsolidations: new ListCompositeType(PendingConsolidation, Number(PENDING_CONSOLIDATIONS_LIMIT)), // new in electra:eip7251
},
{typeName: "BeaconState", jsonCase: "eth2"}
Expand All @@ -366,7 +368,7 @@ export const LightClientBootstrap = new ContainerType(
{
header: LightClientHeader,
currentSyncCommittee: altairSsz.SyncCommittee,
currentSyncCommitteeBranch: altairSsz.LightClientBootstrap.fields.currentSyncCommitteeBranch,
currentSyncCommitteeBranch: new VectorCompositeType(Bytes32, NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA),
},
{typeName: "LightClientBootstrap", jsonCase: "eth2"}
);
Expand All @@ -375,9 +377,9 @@ export const LightClientUpdate = new ContainerType(
{
attestedHeader: LightClientHeader,
nextSyncCommittee: altairSsz.SyncCommittee,
nextSyncCommitteeBranch: altairSsz.LightClientUpdate.fields.nextSyncCommitteeBranch,
nextSyncCommitteeBranch: new VectorCompositeType(Bytes32, NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA),
finalizedHeader: LightClientHeader,
finalityBranch: altairSsz.LightClientUpdate.fields.finalityBranch,
finalityBranch: new VectorCompositeType(Bytes32, FINALIZED_ROOT_DEPTH_ELECTRA),
syncAggregate: altairSsz.SyncAggregate,
signatureSlot: Slot,
},
Expand All @@ -388,7 +390,7 @@ export const LightClientFinalityUpdate = new ContainerType(
{
attestedHeader: LightClientHeader,
finalizedHeader: LightClientHeader,
finalityBranch: altairSsz.LightClientFinalityUpdate.fields.finalityBranch,
finalityBranch: new VectorCompositeType(Bytes32, FINALIZED_ROOT_DEPTH_ELECTRA),
syncAggregate: altairSsz.SyncAggregate,
signatureSlot: Slot,
},
Expand Down
6 changes: 3 additions & 3 deletions packages/types/src/electra/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export type AttesterSlashing = ValueOf<typeof ssz.AttesterSlashing>;
export type AggregateAndProof = ValueOf<typeof ssz.AggregateAndProof>;
export type SignedAggregateAndProof = ValueOf<typeof ssz.SignedAggregateAndProof>;

export type DepositRequest = ValueOf<typeof ssz.DepositRequest>;
export type DepositRequests = ValueOf<typeof ssz.DepositRequests>;
export type DepositRequest = ValueOf<typeof ssz.DepositReceipt>;
export type DepositRequests = ValueOf<typeof ssz.DepositReceipts>;

export type ExecutionLayerWithdrawalRequest = ValueOf<typeof ssz.ExecutionLayerWithdrawalRequest>;
export type ExecutionLayerWithdrawalRequests = ValueOf<typeof ssz.ExecutionLayerWithdrawalRequests>;
Expand Down Expand Up @@ -47,5 +47,5 @@ export type Consolidation = ValueOf<typeof ssz.Consolidation>;
export type SignedConsolidation = ValueOf<typeof ssz.SignedConsolidation>;

export type PendingBalanceDeposit = ValueOf<typeof ssz.PendingBalanceDeposit>;
export type PartialWithdrawal = ValueOf<typeof ssz.PartialWithdrawal>;
export type PendingPartialWithdrawal = ValueOf<typeof ssz.PendingPartialWithdrawal>;
export type PendingConsolidation = ValueOf<typeof ssz.PendingConsolidation>;

0 comments on commit 8614094

Please sign in to comment.