Skip to content

Commit

Permalink
feat: add EL triggered consolidation and remove ExecutionLayer pref…
Browse files Browse the repository at this point in the history
…ix (#6865)

* init commit

* Add consolidation request

* fix lint

* Remove ExecutionLayer prefix

* Address comment

* Fix verification logic

* lint

* Add todo
  • Loading branch information
ensi321 authored and g11tech committed Aug 27, 2024
1 parent 4dfe326 commit 39edbfd
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,6 @@ export async function produceBlockBody<T extends BlockType>(
}
}

if (ForkSeq[fork] >= ForkSeq.electra) {
(blockBody as electra.BeaconBlockBody).consolidations = [];
}

Object.assign(logMeta, {executionPayloadValue});
this.logger.verbose("Produced beacon block body", logMeta);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type DepositRequestV1 = {
index: QUANTITY;
};

export type ExecutionLayerWithdrawalRequestV1 = {
export type WithdrawalRequestV1 = {
sourceAddress: DATA;
validatorPubkey: DATA;
amount: QUANTITY;
Expand Down
33 changes: 12 additions & 21 deletions packages/beacon-node/src/execution/engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
quantityToBigint,
} from "../../eth1/provider/utils.js";
import {ExecutionPayloadStatus, BlobsBundle, PayloadAttributes, VersionedHashes} from "./interface.js";
import {WithdrawalV1, DepositRequestV1, ExecutionLayerWithdrawalRequestV1} from "./payloadIdCache.js";
import {WithdrawalV1, DepositRequestV1, WithdrawalRequestV1} from "./payloadIdCache.js";

/* eslint-disable @typescript-eslint/naming-convention */

Expand Down Expand Up @@ -132,14 +132,14 @@ export type ExecutionPayloadBodyRpc = {
// currently there is a discepancy between EL and CL field name references for deposit requests
// its likely CL receipt will be renamed to requests
depositRequests: DepositRequestV1[] | null | undefined;
withdrawalRequests: ExecutionLayerWithdrawalRequestV1[] | null | undefined;
withdrawalRequests: WithdrawalRequestV1[] | null | undefined;
};

export type ExecutionPayloadBody = {
transactions: bellatrix.Transaction[];
withdrawals: capella.Withdrawals | null;
depositRequests: electra.DepositRequests | null;
withdrawalRequests: electra.ExecutionLayerWithdrawalRequests | null;
withdrawalRequests: electra.WithdrawalRequests | null;
};

export type ExecutionPayloadRpc = {
Expand All @@ -162,7 +162,7 @@ export type ExecutionPayloadRpc = {
excessBlobGas?: QUANTITY; // DENEB
parentBeaconBlockRoot?: QUANTITY; // DENEB
depositRequests?: DepositRequestRpc[]; // ELECTRA
withdrawalRequests?: ExecutionLayerWithdrawalRequestRpc[]; // ELECTRA
withdrawalRequests?: WithdrawalRequestRpc[]; // ELECTRA
};

export type WithdrawalRpc = {
Expand All @@ -173,7 +173,7 @@ export type WithdrawalRpc = {
};

export type DepositRequestRpc = DepositRequestV1;
export type ExecutionLayerWithdrawalRequestRpc = ExecutionLayerWithdrawalRequestV1;
export type WithdrawalRequestRpc = WithdrawalRequestV1;

export type VersionedHashesRpc = DATA[];

Expand Down Expand Up @@ -241,7 +241,7 @@ export function serializeExecutionPayload(fork: ForkName, data: ExecutionPayload
if (ForkSeq[fork] >= ForkSeq.electra) {
const {depositRequests, withdrawalRequests} = data as electra.ExecutionPayload;
payload.depositRequests = depositRequests.map(serializeDepositRequest);
payload.withdrawalRequests = withdrawalRequests.map(serializeExecutionLayerWithdrawalRequest);
payload.withdrawalRequests = withdrawalRequests.map(serializeWithdrawalRequest);
}

return payload;
Expand Down Expand Up @@ -345,9 +345,8 @@ export function parseExecutionPayload(
`withdrawalRequests missing for ${fork} >= electra executionPayload number=${executionPayload.blockNumber} hash=${data.blockHash}`
);
}
(executionPayload as electra.ExecutionPayload).withdrawalRequests = withdrawalRequests.map(
deserializeExecutionLayerWithdrawalRequest
);
(executionPayload as electra.ExecutionPayload).withdrawalRequests =
withdrawalRequests.map(deserializeWithdrawalRequest);
}

return {executionPayload, executionPayloadValue, blobsBundle, shouldOverrideBuilder};
Expand Down Expand Up @@ -436,19 +435,15 @@ export function deserializeDepositRequest(serialized: DepositRequestRpc): electr
} as electra.DepositRequest;
}

export function serializeExecutionLayerWithdrawalRequest(
withdrawalRequest: electra.ExecutionLayerWithdrawalRequest
): ExecutionLayerWithdrawalRequestRpc {
export function serializeWithdrawalRequest(withdrawalRequest: electra.WithdrawalRequest): WithdrawalRequestRpc {
return {
sourceAddress: bytesToData(withdrawalRequest.sourceAddress),
validatorPubkey: bytesToData(withdrawalRequest.validatorPubkey),
amount: numToQuantity(withdrawalRequest.amount),
};
}

export function deserializeExecutionLayerWithdrawalRequest(
withdrawalRequest: ExecutionLayerWithdrawalRequestRpc
): electra.ExecutionLayerWithdrawalRequest {
export function deserializeWithdrawalRequest(withdrawalRequest: WithdrawalRequestRpc): electra.WithdrawalRequest {
return {
sourceAddress: dataToBytes(withdrawalRequest.sourceAddress, 20),
validatorPubkey: dataToBytes(withdrawalRequest.validatorPubkey, 48),
Expand All @@ -462,9 +457,7 @@ export function deserializeExecutionPayloadBody(data: ExecutionPayloadBodyRpc |
transactions: data.transactions.map((tran) => dataToBytes(tran, null)),
withdrawals: data.withdrawals ? data.withdrawals.map(deserializeWithdrawal) : null,
depositRequests: data.depositRequests ? data.depositRequests.map(deserializeDepositRequest) : null,
withdrawalRequests: data.withdrawalRequests
? data.withdrawalRequests.map(deserializeExecutionLayerWithdrawalRequest)
: null,
withdrawalRequests: data.withdrawalRequests ? data.withdrawalRequests.map(deserializeWithdrawalRequest) : null,
}
: null;
}
Expand All @@ -475,9 +468,7 @@ export function serializeExecutionPayloadBody(data: ExecutionPayloadBody | null)
transactions: data.transactions.map((tran) => bytesToData(tran)),
withdrawals: data.withdrawals ? data.withdrawals.map(serializeWithdrawal) : null,
depositRequests: data.depositRequests ? data.depositRequests.map(serializeDepositRequest) : null,
withdrawalRequests: data.withdrawalRequests
? data.withdrawalRequests.map(serializeExecutionLayerWithdrawalRequest)
: null,
withdrawalRequests: data.withdrawalRequests ? data.withdrawalRequests.map(serializeWithdrawalRequest) : null,
}
: null;
}
Expand Down
20 changes: 7 additions & 13 deletions packages/beacon-node/test/spec/presets/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,14 @@ const operationFns: Record<string, BlockProcessFn<CachedBeaconStateAllForks>> =
blockFns.processWithdrawals(ForkSeq.capella, state as CachedBeaconStateCapella, testCase.execution_payload);
},

consolidation: (state, testCase: {consolidation: electra.SignedConsolidation}) => {
blockFns.processConsolidation(state as CachedBeaconStateElectra, testCase.consolidation);
withdrawal_request: (state, testCase: {withdrawal_request: electra.WithdrawalRequest}) => {
blockFns.processWithdrawalRequest(ForkSeq.electra, state as CachedBeaconStateElectra, testCase.withdrawal_request);
},

execution_layer_withdrawal_request: (
state,
testCase: {execution_layer_withdrawal_request: electra.ExecutionLayerWithdrawalRequest}
) => {
blockFns.processExecutionLayerWithdrawalRequest(
ForkSeq.electra,
state as CachedBeaconStateElectra,
testCase.execution_layer_withdrawal_request
);
consolidation_request: (state, testCase: {consolidation_request: electra.ConsolidationRequest}) => {
blockFns.processConsolidationRequest(state as CachedBeaconStateElectra, testCase.consolidation_request);
},

};

export type BlockProcessFn<T extends CachedBeaconStateAllForks> = (state: T, testCase: any) => void;
Expand Down Expand Up @@ -156,8 +150,8 @@ const operations: TestRunnerFn<OperationsTestCase, BeaconStateAllForks> = (fork,
// Capella
address_change: ssz.capella.SignedBLSToExecutionChange,
// Electra
consolidation: ssz.electra.SignedConsolidation,
execution_layer_withdrawal_request: ssz.electra.ExecutionLayerWithdrawalRequest,
withdrawal_request: ssz.electra.WithdrawalRequest,
consolidation_request: ssz.electra.ConsolidationRequest,
},
shouldError: (testCase) => testCase.post === undefined,
getExpected: (testCase) => testCase.post,
Expand Down
2 changes: 1 addition & 1 deletion packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const {
PENDING_PARTIAL_WITHDRAWALS_LIMIT,
PENDING_CONSOLIDATIONS_LIMIT,
MIN_SLASHING_PENALTY_QUOTIENT_ELECTRA,
MAX_CONSOLIDATIONS,
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD,

MAX_DEPOSIT_RECEIPTS_PER_PAYLOAD,
MAX_WITHDRAWAL_REQUESTS_PER_PAYLOAD,
Expand Down
2 changes: 1 addition & 1 deletion packages/params/src/presets/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,6 @@ export const mainnetPreset: BeaconPreset = {
PENDING_BALANCE_DEPOSITS_LIMIT: 134217728,
PENDING_PARTIAL_WITHDRAWALS_LIMIT: 134217728,
PENDING_CONSOLIDATIONS_LIMIT: 262144,
MAX_CONSOLIDATIONS: 1,
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD: 1,
WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA: 4096,
};
2 changes: 1 addition & 1 deletion packages/params/src/presets/minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ export const minimalPreset: BeaconPreset = {
PENDING_BALANCE_DEPOSITS_LIMIT: 134217728,
PENDING_PARTIAL_WITHDRAWALS_LIMIT: 64,
PENDING_CONSOLIDATIONS_LIMIT: 64,
MAX_CONSOLIDATIONS: 1,
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD: 1,
WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA: 4096,
};
4 changes: 2 additions & 2 deletions packages/params/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export type BeaconPreset = {
PENDING_BALANCE_DEPOSITS_LIMIT: number;
PENDING_PARTIAL_WITHDRAWALS_LIMIT: number;
PENDING_CONSOLIDATIONS_LIMIT: number;
MAX_CONSOLIDATIONS: number;
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD: number;
WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA: number;
};

Expand Down Expand Up @@ -195,7 +195,7 @@ export const beaconPresetTypes: BeaconPresetTypes = {
PENDING_BALANCE_DEPOSITS_LIMIT: "number",
PENDING_PARTIAL_WITHDRAWALS_LIMIT: "number",
PENDING_CONSOLIDATIONS_LIMIT: "number",
MAX_CONSOLIDATIONS: "number",
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD: "number",
WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA: "number",
};

Expand Down
111 changes: 0 additions & 111 deletions packages/state-transition/src/block/processConsolidation.ts

This file was deleted.

74 changes: 74 additions & 0 deletions packages/state-transition/src/block/processConsolidationRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {electra, ssz} from "@lodestar/types";
import {FAR_FUTURE_EPOCH, MIN_ACTIVATION_BALANCE, PENDING_CONSOLIDATIONS_LIMIT} from "@lodestar/params";

import {CachedBeaconStateElectra} from "../types.js";
import {getConsolidationChurnLimit, isActiveValidator} from "../util/validator.js";
import {hasExecutionWithdrawalCredential} from "../util/electra.js";
import {computeConsolidationEpochAndUpdateChurn} from "../util/epoch.js";

export function processConsolidationRequest(
state: CachedBeaconStateElectra,
consolidationRequest: electra.ConsolidationRequest
): void {

// If the pending consolidations queue is full, consolidation requests are ignored
if (state.pendingConsolidations.length >= PENDING_CONSOLIDATIONS_LIMIT) {
return;
}

// If there is too little available consolidation churn limit, consolidation requests are ignored
if (getConsolidationChurnLimit(state) <= MIN_ACTIVATION_BALANCE) {
return;
}

const {sourcePubkey, targetPubkey} = consolidationRequest;
const sourceIndex = state.epochCtx.getValidatorIndex(sourcePubkey);
const targetIndex = state.epochCtx.getValidatorIndex(targetPubkey);

if (sourceIndex === undefined || targetIndex === undefined) {
return;
}

// Verify that source != target, so a consolidation cannot be used as an exit.
if (sourceIndex === targetIndex){
return;
}

const sourceValidator = state.validators.getReadonly(sourceIndex);
const targetValidator = state.validators.getReadonly(targetIndex);
const sourceWithdrawalAddress = sourceValidator.withdrawalCredentials.subarray(12);
const currentEpoch = state.epochCtx.epoch;

// Verify withdrawal credentials
if (
!hasExecutionWithdrawalCredential(sourceValidator.withdrawalCredentials) ||
!hasExecutionWithdrawalCredential(targetValidator.withdrawalCredentials)
) {
return;
}

if (Buffer.compare(sourceWithdrawalAddress, consolidationRequest.sourceAddress) !== 0) {
return;
}

// Verify the source and the target are active
if (!isActiveValidator(sourceValidator, currentEpoch) || !isActiveValidator(targetValidator, currentEpoch)) {
return;
}

// Verify exits for source and target have not been initiated
if (sourceValidator.exitEpoch !== FAR_FUTURE_EPOCH || targetValidator.exitEpoch !== FAR_FUTURE_EPOCH) {
return;
}

// TODO Electra: See if we can get rid of big int
const exitEpoch = computeConsolidationEpochAndUpdateChurn(state, BigInt(sourceValidator.effectiveBalance));
sourceValidator.exitEpoch = exitEpoch;
sourceValidator.withdrawableEpoch = exitEpoch + state.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY;

const pendingConsolidation = ssz.electra.PendingConsolidation.toViewDU({
sourceIndex,
targetIndex,
});
state.pendingConsolidations.push(pendingConsolidation);
}
Loading

0 comments on commit 39edbfd

Please sign in to comment.