Skip to content

Commit

Permalink
Fix rebase issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Nov 26, 2022
1 parent 804887d commit 58c595a
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 59 deletions.
2 changes: 1 addition & 1 deletion packages/beacon-node/src/chain/blocks/importBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export async function importBlock(
if (headBlockHash !== ZERO_HASH_HEX) {
this.executionEngine
.notifyForkchoiceUpdate(
this.config.getForkSeq(this.forkChoice.getHead().slot),
this.config.getForkName(this.forkChoice.getHead().slot),
headBlockHash,
safeBlockHash,
finalizedBlockHash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export async function verifyBlockExecutionPayload(

// TODO: Handle better notifyNewPayload() returning error is syncing
const execResult = await chain.executionEngine.notifyNewPayload(
chain.config.getForkSeq(block.message.slot),
chain.config.getForkName(block.message.slot),
executionPayloadEnabled
);

Expand Down
35 changes: 15 additions & 20 deletions packages/beacon-node/src/chain/produceBlock/produceBlockBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ import {ForkName, ForkSeq} from "@lodestar/params";
import {toHex, sleep} from "@lodestar/utils";

import type {BeaconChain} from "../chain.js";
import {PayloadId, IExecutionEngine, IExecutionBuilder, ForkExecution, PayloadAttributes} from "../../execution/index.js";
import {
PayloadId,
IExecutionEngine,
IExecutionBuilder,
ForkExecution,
PayloadAttributes,
} from "../../execution/index.js";
import {ZERO_HASH, ZERO_HASH_HEX} from "../../constants/index.js";
import {IEth1ForBlockProduction} from "../../eth1/index.js";
import {numToQuantity} from "../../eth1/provider/utils.js";
Expand Down Expand Up @@ -168,20 +174,11 @@ export async function produceBlockBody<T extends BlockType>(
currentState as CachedBeaconStateBellatrix,
proposerPubKey
);
(blockBody as allForks.BlindedBeaconBlockBody).executionPayloadHeader = executionPayloadHeader;

// Capella and later forks have withdrawalRoot on their ExecutionPayloadHeader
// TODO Capella: Remove this. It will come from the execution client.
if (fork === ForkName.capella || fork === ForkName.eip4844) {
(blockBody as capella.BlindedBeaconBlockBody).executionPayloadHeader.withdrawalsRoot = Uint8Array.from(
Buffer.alloc(32, 0)
);
}

if (fork === ForkName.eip4844) {
// Empty blobs for now
(blockBody as eip4844.BeaconBlockBody).blobKzgCommitments = [];
blobs = {blobs: [], blockHash: toHex(executionPayloadHeader.blockHash)};
throw Error("Builder blinded blocks not supported for capella and eip4844");
}
}

Expand All @@ -194,7 +191,6 @@ export async function produceBlockBody<T extends BlockType>(
// https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/validator.md#constructing-the-beaconblockbody

const prepareRes = await prepareExecutionPayload(
forkInfo.seq,
this,
fork,
safeBlockHash,
Expand All @@ -205,7 +201,7 @@ export async function produceBlockBody<T extends BlockType>(

if (prepareRes.isPremerge) {
(blockBody as allForks.ExecutionBlockBody).executionPayload = ssz.allForksExecution[
forkInfo.name
fork
].ExecutionPayload.defaultValue();
} else {
const {prepType, payloadId} = prepareRes;
Expand Down Expand Up @@ -266,7 +262,7 @@ export async function produceBlockBody<T extends BlockType>(
e as Error
);
(blockBody as allForks.ExecutionBlockBody).executionPayload = ssz.allForksExecution[
forkInfo.name
fork
].ExecutionPayload.defaultValue();
} else {
// since merge transition is complete, we need a valid payload even if with an
Expand Down Expand Up @@ -358,19 +354,18 @@ export async function prepareExecutionPayload(
timestamp,
prevRandao,
suggestedFeeRecipient,
}

if (fork >= ForkSeq.capella) {
attributes.withdrawals = getExpectedWithdrawals(state as CachedBeaconStateCapella).withdrawals}
};

if (ForkSeq[fork] >= ForkSeq.capella) {
attributes.withdrawals = getExpectedWithdrawals(state as CachedBeaconStateCapella).withdrawals;
}

payloadId = await chain.executionEngine.notifyForkchoiceUpdate(
fork,
toHex(parentHash),
safeBlockHash,
finalizedBlockHash,
attributes,
attributes
);
}

Expand Down Expand Up @@ -399,7 +394,7 @@ async function prepareExecutionPayloadHeader(
if (!chain.executionBuilder) {
throw Error("executionBuilder required");
}
if (ForkName[ fork] >= ForkSeq.capella) {
if (ForkSeq[fork] >= ForkSeq.capella) {
throw Error("executionBuilder capella api not implemented");
}

Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/eth1/provider/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ export function bytesToData(bytes: Uint8Array): DATA {
/**
* DATA as defined in ethereum execution layer JSON RPC https://eth.wiki/json-rpc/API
*/
export function dataToBytes(hex: DATA, fixedLength?: number): Uint8Array {
export function dataToBytes(hex: DATA, fixedLength: number | null): Uint8Array {
try {
const bytes = fromHexString(hex);
if (fixedLength !== undefined && bytes.length !== fixedLength) {
if (fixedLength != null && bytes.length !== fixedLength) {
throw Error(`Wrong data length ${bytes.length} expected ${fixedLength}`);
}
return bytes;
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/execution/engine/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ export class ExecutionEngineHttp implements IExecutionEngine {
* If any of the above fails due to errors unrelated to the normal processing flow of the method, client software MUST respond with an error object.
*/
async notifyForkchoiceUpdate(
seq: ForkSeq,
fork: ForkName,
headBlockHash: RootHex,
safeBlockHash: RootHex,
finalizedBlockHash: RootHex,
payloadAttributes?: PayloadAttributes
): Promise<PayloadId | null> {
// Once on capella, should this need to be permanently switched to v2 when payload attrs
// not provided
const method = seq >= ForkSeq.capella ? "engine_forkchoiceUpdatedV2" : "engine_forkchoiceUpdatedV1";
const method = ForkSeq[fork] >= ForkSeq.capella ? "engine_forkchoiceUpdatedV2" : "engine_forkchoiceUpdatedV1";
const apiPayloadAttributes: ApiPayloadAttributes | undefined = payloadAttributes
? {
timestamp: numToQuantity(payloadAttributes.timestamp),
Expand Down
7 changes: 3 additions & 4 deletions packages/beacon-node/src/execution/engine/interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {ForkName} from "@lodestar/params";
import {KZGCommitment, Blob} from "@lodestar/types/eip4844";
import {RootHex, allForks, capella} from "@lodestar/types";
import {ForkSeq} from "@lodestar/params";

import {DATA, QUANTITY} from "../../eth1/provider/utils.js";
import {PayloadIdCache, PayloadId, ApiPayloadAttributes, WithdrawalV1} from "./payloadIdCache.js";
Expand Down Expand Up @@ -90,7 +89,7 @@ export interface IExecutionEngine {
*
* Should be called in advance before, after or in parallel to block processing
*/
notifyNewPayload(seq: ForkSeq, executionPayload: allForks.ExecutionPayload): Promise<ExecutePayloadResponse>;
notifyNewPayload(fork: ForkName, executionPayload: allForks.ExecutionPayload): Promise<ExecutePayloadResponse>;

/**
* Signal fork choice updates
Expand All @@ -105,7 +104,7 @@ export interface IExecutionEngine {
* Should be called in response to fork-choice head and finalized events
*/
notifyForkchoiceUpdate(
seq: ForkSeq,
fork: ForkName,
headBlockHash: RootHex,
safeBlockHash: RootHex,
finalizedBlockHash: RootHex,
Expand All @@ -119,7 +118,7 @@ export interface IExecutionEngine {
* Required for block producing
* https://github.com/ethereum/consensus-specs/blob/dev/specs/merge/validator.md#get_payload
*/
getPayload(seq: ForkSeq, payloadId: PayloadId): Promise<allForks.ExecutionPayload>;
getPayload(fork: ForkName, payloadId: PayloadId): Promise<allForks.ExecutionPayload>;

/**
* "After retrieving the execution payload from the execution engine as specified in Bellatrix,
Expand Down
11 changes: 7 additions & 4 deletions packages/beacon-node/src/execution/engine/mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from "node:crypto";
import {allForks, bellatrix, RootHex, ssz} from "@lodestar/types";
import {fromHex, toHex} from "@lodestar/utils";
import {ForkSeq} from "@lodestar/params";
import {ForkName} from "@lodestar/params";
import {ZERO_HASH_HEX} from "../../constants/index.js";
import {
ExecutePayloadStatus,
Expand Down Expand Up @@ -58,7 +58,10 @@ export class ExecutionEngineMock implements IExecutionEngine {
/**
* `engine_newPayloadV1`
*/
async notifyNewPayload(_seq: ForkSeq, executionPayload: bellatrix.ExecutionPayload): Promise<ExecutePayloadResponse> {
async notifyNewPayload(
_fork: ForkName,
executionPayload: bellatrix.ExecutionPayload
): Promise<ExecutePayloadResponse> {
const blockHash = toHex(executionPayload.blockHash);
const parentHash = toHex(executionPayload.parentHash);

Expand Down Expand Up @@ -109,7 +112,7 @@ export class ExecutionEngineMock implements IExecutionEngine {
* `engine_forkchoiceUpdatedV1`
*/
async notifyForkchoiceUpdate(
_seq: ForkSeq,
_fork: ForkName,
headBlockHash: RootHex,
safeBlockHash: RootHex,
finalizedBlockHash: RootHex,
Expand Down Expand Up @@ -216,7 +219,7 @@ export class ExecutionEngineMock implements IExecutionEngine {
* 2. The call MUST be responded with 5: Unavailable payload error if the building process identified by the payloadId doesn't exist.
* 3. Client software MAY stop the corresponding building process after serving this call.
*/
async getPayload(_seq: ForkSeq, payloadId: PayloadId): Promise<bellatrix.ExecutionPayload> {
async getPayload(_fork: ForkName, payloadId: PayloadId): Promise<bellatrix.ExecutionPayload> {
// 1. Given the payloadId client software MUST return the most recent version of the payload that is available in
// the corresponding build process at the time of receiving the call.
const payloadIdNbr = Number(payloadId);
Expand Down
10 changes: 5 additions & 5 deletions packages/beacon-node/test/sim/merge-interop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ describe("executionEngine / ExecutionEngineHttp", function () {
const preparePayloadParams: PayloadAttributes = {
// Note: this is created with a pre-defined genesis.json
timestamp: quantityToNum("0x5"),
prevRandao: dataToBytes("0x0000000000000000000000000000000000000000000000000000000000000000"),
prevRandao: dataToBytes("0x0000000000000000000000000000000000000000000000000000000000000000", 32),
suggestedFeeRecipient: "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
fork: ForkName.bellatrix,
};

const finalizedBlockHash = "0x0000000000000000000000000000000000000000000000000000000000000000";

const payloadId = await executionEngine.notifyForkchoiceUpdate(
ForkSeq.bellatrix,
ForkName.bellatrix,
genesisBlockHash,
//use finalizedBlockHash as safeBlockHash
finalizedBlockHash,
Expand All @@ -144,7 +144,7 @@ describe("executionEngine / ExecutionEngineHttp", function () {
* curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"engine_getPayloadV1","params":["0xa247243752eb10b4"],"id":67}' http://localhost:8550
**/

const payload = await executionEngine.getPayload(ForkSeq.bellatrix, payloadId);
const payload = await executionEngine.getPayload(ForkName.bellatrix, payloadId);
if (TX_SCENARIOS.includes("simple")) {
if (payload.transactions.length !== 1)
throw new Error("Expected a simple transaction to be in the fetched payload");
Expand All @@ -157,7 +157,7 @@ describe("executionEngine / ExecutionEngineHttp", function () {
* curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"engine_newPayloadV1","params":[{"parentHash":"0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a","coinbase":"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b","stateRoot":"0xca3149fa9e37db08d1cd49c9061db1002ef1cd58db2210f2115c8c989b2bdf45","receiptRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","prevRandao":"0x0000000000000000000000000000000000000000000000000000000000000000","blockNumber":"0x1","gasLimit":"0x1c9c380","gasUsed":"0x0","timestamp":"0x5","extraData":"0x","baseFeePerGas":"0x7","blockHash":"0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858","transactions":[]}],"id":67}' http://localhost:8550
**/

const payloadResult = await executionEngine.notifyNewPayload(ForkSeq.bellatrix, payload);
const payloadResult = await executionEngine.notifyNewPayload(ForkName.bellatrix, payload);
if (payloadResult.status !== ExecutePayloadStatus.VALID) {
throw Error("getPayload returned payload that notifyNewPayload deems invalid");
}
Expand All @@ -169,7 +169,7 @@ describe("executionEngine / ExecutionEngineHttp", function () {
**/

await executionEngine.notifyForkchoiceUpdate(
ForkSeq.bellatrix,
ForkName.bellatrix,
bytesToData(payload.blockHash),
genesisBlockHash,
genesisBlockHash
Expand Down
19 changes: 10 additions & 9 deletions packages/beacon-node/test/sim/withdrawal-interop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import fs from "node:fs";
import {Context} from "mocha";
import {fromHexString, toHexString} from "@chainsafe/ssz";
import {LogLevel, sleep, TimestampFormatCode} from "@lodestar/utils";
import {SLOTS_PER_EPOCH, ForkSeq} from "@lodestar/params";
import {SLOTS_PER_EPOCH, ForkName} from "@lodestar/params";
import {IChainConfig} from "@lodestar/config";
import {Epoch} from "@lodestar/types";
import {ValidatorProposerConfig} from "@lodestar/validator";

import {ExecutePayloadStatus} from "../../src/execution/engine/interface.js";
import {ExecutePayloadStatus, PayloadAttributes} from "../../src/execution/engine/interface.js";
import {ExecutionEngineHttp} from "../../src/execution/engine/http.js";
import {ChainEvent} from "../../src/chain/index.js";
import {testLogger, TestLoggerOpts} from "../utils/logger.js";
Expand Down Expand Up @@ -133,22 +133,23 @@ describe("executionEngine / ExecutionEngineHttp", function () {
const withdrawals = withdrawalsVector.map((testVec) => ({
index: testVec.Index,
validatorIndex: testVec.Validator,
address: dataToBytes(testVec.Recipient),
address: dataToBytes(testVec.Recipient, 20),
amount: BigInt(testVec.Amount) / GWEI_TO_WEI,
}));

const preparePayloadParams = {
const preparePayloadParams: PayloadAttributes = {
// Note: this is created with a pre-defined genesis.json
timestamp: 47,
prevRandao: dataToBytes("0xff00000000000000000000000000000000000000000000000000000000000000"),
prevRandao: dataToBytes("0xff00000000000000000000000000000000000000000000000000000000000000", 32),
suggestedFeeRecipient: "0xaa00000000000000000000000000000000000000",
withdrawals,
fork: ForkName.capella,
};
const finalizedBlockHash = "0xfe950635b1bd2a416ff6283b0bbd30176e1b1125ad06fa729da9f3f4c1c61710";

// 1. Prepare a payload
const payloadId = await executionEngine.notifyForkchoiceUpdate(
ForkSeq.capella,
ForkName.capella,
genesisBlockHash,
//use finalizedBlockHash as safeBlockHash
finalizedBlockHash,
Expand All @@ -158,22 +159,22 @@ describe("executionEngine / ExecutionEngineHttp", function () {
if (!payloadId) throw Error("InvalidPayloadId");

// 2. Get the payload
const payload = await executionEngine.getPayload(ForkSeq.capella, payloadId);
const payload = await executionEngine.getPayload(ForkName.capella, payloadId);
const blockHash = toHexString(payload.blockHash);
const expectedBlockHash = "0x64707e5574d14103a7f583e702f09e68ca1eb334e8eb0632a4272efe54f2fc7c";
if (blockHash !== expectedBlockHash) {
throw Error(`Invalid blockHash expected=${expectedBlockHash} actual=${blockHash}`);
}

// 3. Execute the payload
const payloadResult = await executionEngine.notifyNewPayload(ForkSeq.capella, payload);
const payloadResult = await executionEngine.notifyNewPayload(ForkName.capella, payload);
if (payloadResult.status !== ExecutePayloadStatus.VALID) {
throw Error("getPayload returned payload that notifyNewPayload deems invalid");
}

// 4. Update the fork choice
await executionEngine.notifyForkchoiceUpdate(
ForkSeq.capella,
ForkName.capella,
bytesToData(payload.blockHash),
genesisBlockHash,
genesisBlockHash
Expand Down
14 changes: 7 additions & 7 deletions packages/beacon-node/test/unit/executionEngine/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from "chai";
import {fastify} from "fastify";
import {ForkSeq} from "@lodestar/params";
import {ForkName} from "@lodestar/params";
import {
ExecutionEngineHttp,
parseExecutionPayload,
Expand Down Expand Up @@ -77,9 +77,9 @@ describe("ExecutionEngine / http", () => {
};
returnValue = response;

const payload = await executionEngine.getPayload(ForkSeq.bellatrix, "0x0");
const payload = await executionEngine.getPayload(ForkName.bellatrix, "0x0");

expect(serializeExecutionPayload(ForkSeq.bellatrix, payload)).to.deep.equal(
expect(serializeExecutionPayload(ForkName.bellatrix, payload)).to.deep.equal(
response.result,
"Wrong returned payload"
);
Expand Down Expand Up @@ -121,8 +121,8 @@ describe("ExecutionEngine / http", () => {
};

const {status} = await executionEngine.notifyNewPayload(
ForkSeq.bellatrix,
parseExecutionPayload(ForkSeq.bellatrix, request.params[0])
ForkName.bellatrix,
parseExecutionPayload(ForkName.bellatrix, request.params[0])
);

expect(status).to.equal("VALID", "Wrong returned execute payload result");
Expand Down Expand Up @@ -151,7 +151,7 @@ describe("ExecutionEngine / http", () => {
};

await executionEngine.notifyForkchoiceUpdate(
ForkSeq.bellatrix,
ForkName.bellatrix,
forkChoiceHeadData.headBlockHash,
forkChoiceHeadData.safeBlockHash,
forkChoiceHeadData.finalizedBlockHash
Expand All @@ -169,7 +169,7 @@ describe("ExecutionEngine / http", () => {
const response = {jsonrpc: "2.0", id: 67, error: {code: 5, message: "unknown payload"}};
returnValue = response;

await expect(executionEngine.getPayload(ForkSeq.bellatrix, request.params[0])).to.be.rejectedWith(
await expect(executionEngine.getPayload(ForkName.bellatrix, request.params[0])).to.be.rejectedWith(
"JSON RPC error: unknown payload, engine_getPayload"
);
});
Expand Down
Loading

0 comments on commit 58c595a

Please sign in to comment.