Skip to content

Commit

Permalink
Fix execution request parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ensi321 committed Dec 18, 2024
1 parent cd7b1b4 commit e22d637
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
6 changes: 6 additions & 0 deletions packages/beacon-node/src/api/impl/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
BLOB_TX_TYPE,
BLS_WITHDRAWAL_PREFIX,
COMPOUNDING_WITHDRAWAL_PREFIX,
CONSOLIDATION_REQUEST_TYPE,
DEPOSIT_CONTRACT_TREE_DEPTH,
DEPOSIT_REQUEST_TYPE,
DOMAIN_AGGREGATE_AND_PROOF,
DOMAIN_APPLICATION_BUILDER,
DOMAIN_APPLICATION_MASK,
Expand Down Expand Up @@ -40,6 +42,7 @@ import {
UNSET_DEPOSIT_REQUESTS_START_INDEX,
VERSIONED_HASH_VERSION_KZG,
WEIGHT_DENOMINATOR,
WITHDRAWAL_REQUEST_TYPE,
} from "@lodestar/params";

/**
Expand Down Expand Up @@ -108,4 +111,7 @@ export const specConstants = {
// electra
UNSET_DEPOSIT_REQUESTS_START_INDEX,
FULL_EXIT_REQUEST_AMOUNT,
DEPOSIT_REQUEST_TYPE,
WITHDRAWAL_REQUEST_TYPE,
CONSOLIDATION_REQUEST_TYPE,
};
12 changes: 6 additions & 6 deletions packages/beacon-node/src/execution/engine/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ForkName} from "@lodestar/params";
import {ForkName, DEPOSIT_REQUEST_TYPE, WITHDRAWAL_REQUEST_TYPE, CONSOLIDATION_REQUEST_TYPE} from "@lodestar/params";
import {ExecutionPayload, ExecutionRequests, Root, RootHex, Wei, capella} from "@lodestar/types";
import {Blob, BlobAndProof, KZGCommitment, KZGProof} from "@lodestar/types/deneb";

Expand Down Expand Up @@ -58,11 +58,11 @@ export enum ClientCode {
XX = "XX", // unknown
}

// Represents request type in ExecutionRequests defined in EIP-7685
export enum RequestType {
DEPOSIT_REQUEST_TYPE = 0, // 0x00
WITHDRAWAL_REQUEST_TYPE = 1, // 0x01
CONSOLIDATION_REQUEST_TYPE = 2, // 0x02
export type RequestType = typeof DEPOSIT_REQUEST_TYPE | typeof WITHDRAWAL_REQUEST_TYPE | typeof CONSOLIDATION_REQUEST_TYPE;


export function isRequestType(type: number): type is RequestType {
return type === DEPOSIT_REQUEST_TYPE || type === WITHDRAWAL_REQUEST_TYPE || type === CONSOLIDATION_REQUEST_TYPE;
}

export type ExecutePayloadResponse =
Expand Down
33 changes: 23 additions & 10 deletions packages/beacon-node/src/execution/engine/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {
BYTES_PER_FIELD_ELEMENT,
BYTES_PER_LOGS_BLOOM,
CONSOLIDATION_REQUEST_TYPE,
DEPOSIT_REQUEST_TYPE,
FIELD_ELEMENTS_PER_BLOB,
ForkName,
ForkSeq,
WITHDRAWAL_REQUEST_TYPE,
} from "@lodestar/params";
import {ExecutionPayload, ExecutionRequests, Root, Wei, bellatrix, capella, deneb, electra, ssz} from "@lodestar/types";
import {BlobAndProof} from "@lodestar/types/deneb";
Expand All @@ -17,7 +20,7 @@ import {
quantityToBigint,
quantityToNum,
} from "../../eth1/provider/utils.js";
import {BlobsBundle, ExecutionPayloadStatus, PayloadAttributes, RequestType, VersionedHashes} from "./interface.js";
import {BlobsBundle, ExecutionPayloadStatus, PayloadAttributes, RequestType, VersionedHashes, isRequestType} from "./interface.js";
import {WithdrawalV1} from "./payloadIdCache.js";

export type EngineApiRpcParamTypes = {
Expand Down Expand Up @@ -417,7 +420,7 @@ function prefixRequests(requestsBytes: Uint8Array, requestType: RequestType): Ui

function serializeDepositRequests(depositRequests: electra.DepositRequests): DepositRequestsRpc {
const requestsBytes = ssz.electra.DepositRequests.serialize(depositRequests);
return bytesToData(prefixRequests(requestsBytes, RequestType.DEPOSIT_REQUEST_TYPE));
return bytesToData(prefixRequests(requestsBytes, DEPOSIT_REQUEST_TYPE));
}

function deserializeDepositRequests(serialized: DepositRequestsRpc): electra.DepositRequests {
Expand All @@ -426,7 +429,7 @@ function deserializeDepositRequests(serialized: DepositRequestsRpc): electra.Dep

function serializeWithdrawalRequests(withdrawalRequests: electra.WithdrawalRequests): WithdrawalRequestsRpc {
const requestsBytes = ssz.electra.WithdrawalRequests.serialize(withdrawalRequests);
return bytesToData(prefixRequests(requestsBytes, RequestType.WITHDRAWAL_REQUEST_TYPE));
return bytesToData(prefixRequests(requestsBytes, WITHDRAWAL_REQUEST_TYPE));
}

function deserializeWithdrawalRequests(serialized: WithdrawalRequestsRpc): electra.WithdrawalRequests {
Expand All @@ -437,7 +440,7 @@ function serializeConsolidationRequests(
consolidationRequests: electra.ConsolidationRequests
): ConsolidationRequestsRpc {
const requestsBytes = ssz.electra.ConsolidationRequests.serialize(consolidationRequests);
return bytesToData(prefixRequests(requestsBytes, RequestType.CONSOLIDATION_REQUEST_TYPE));
return bytesToData(prefixRequests(requestsBytes, CONSOLIDATION_REQUEST_TYPE));
}

function deserializeConsolidationRequests(serialized: ConsolidationRequestsRpc): electra.ConsolidationRequests {
Expand Down Expand Up @@ -480,9 +483,19 @@ export function deserializeExecutionRequests(serialized: ExecutionRequestsRpc):

let prevRequestType: RequestType | undefined;

for (const prefixedRequests of serialized) {
const currentRequestType = RequestType[prefixedRequests[0] as keyof typeof RequestType];
const requests = prefixedRequests.slice(1);
for (let prefixedRequests of serialized) {
// Slice out 0x so it is easier to extract request type
if (prefixedRequests.startsWith("0x")) {
prefixedRequests = prefixedRequests.slice(2);
}

const currentRequestType = Number(prefixedRequests.substring(0, 2));

if (!isRequestType(currentRequestType)) {
throw Error(`Invalid request type currentRequestType=${prefixedRequests.substring(0, 2)}`);
}

const requests = prefixedRequests.slice(2);

if (prevRequestType !== undefined && prevRequestType >= currentRequestType) {
throw Error(
Expand All @@ -491,15 +504,15 @@ export function deserializeExecutionRequests(serialized: ExecutionRequestsRpc):
}

switch (currentRequestType) {
case RequestType.DEPOSIT_REQUEST_TYPE: {
case DEPOSIT_REQUEST_TYPE: {
result.deposits = deserializeDepositRequests(requests);
break;
}
case RequestType.WITHDRAWAL_REQUEST_TYPE: {
case WITHDRAWAL_REQUEST_TYPE: {
result.withdrawals = deserializeWithdrawalRequests(requests);
break;
}
case RequestType.CONSOLIDATION_REQUEST_TYPE: {
case CONSOLIDATION_REQUEST_TYPE: {
result.consolidations = deserializeConsolidationRequests(requests);
break;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,6 @@ export const FINALIZED_ROOT_INDEX_ELECTRA = 41;
export const NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA = 87;
export const NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA = 6;
export const NEXT_SYNC_COMMITTEE_INDEX_ELECTRA = 23;
export const DEPOSIT_REQUEST_TYPE = 0x00;
export const WITHDRAWAL_REQUEST_TYPE = 0x01;
export const CONSOLIDATION_REQUEST_TYPE = 0x02;

0 comments on commit e22d637

Please sign in to comment.