Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement verge spec feature #5164

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/beacon-node/test/spec/presets/fork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CachedBeaconStateAltair,
CachedBeaconStatePhase0,
CachedBeaconStateCapella,
CachedBeaconStateDeneb,
} from "@lodestar/state-transition";
import * as slotFns from "@lodestar/state-transition/slot";
import {phase0, ssz} from "@lodestar/types";
Expand Down Expand Up @@ -32,6 +33,8 @@ export const fork: TestRunnerFn<ForkStateCase, BeaconStateAllForks> = (forkNext)
return slotFns.upgradeStateToCapella(preState as CachedBeaconStateBellatrix);
case ForkName.deneb:
return slotFns.upgradeStateToDeneb(preState as CachedBeaconStateCapella);
case ForkName.verge:
return slotFns.upgradeStateToVerge(preState as CachedBeaconStateDeneb);
}
},
options: {
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/test/spec/presets/genesis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from "chai";
import {phase0, Root, ssz, TimeSeconds, allForks, deneb} from "@lodestar/types";
import {phase0, Root, ssz, TimeSeconds, allForks} from "@lodestar/types";
import {InputType} from "@lodestar/spec-test-util";
import {
BeaconStateAllForks,
Expand Down Expand Up @@ -41,7 +41,7 @@ const genesisInitialization: TestRunnerFn<GenesisInitSpecTest, BeaconStateAllFor
genesisValidatorsRoot: Buffer.alloc(32, 0),
});

const executionPayloadHeaderType =
const executionPayloadHeaderType: allForks.AllForksExecutionSSZTypes["ExecutionPayloadHeader"] =
fork !== ForkName.phase0 && fork !== ForkName.altair
? ssz.allForksExecution[fork as ExecutionFork].ExecutionPayloadHeader
: ssz.bellatrix.ExecutionPayloadHeader;
Expand All @@ -54,7 +54,7 @@ const genesisInitialization: TestRunnerFn<GenesisInitSpecTest, BeaconStateAllFor
deposits,
undefined,
testcase["execution_payload_header"] &&
executionPayloadHeaderType.toViewDU(testcase["execution_payload_header"] as deneb.ExecutionPayloadHeader)
executionPayloadHeaderType.toViewDU(testcase["execution_payload_header"])
);
},
// eth1.yaml
Expand Down
8 changes: 8 additions & 0 deletions packages/beacon-node/test/spec/presets/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ function getTransitionConfig(fork: ForkName, forkEpoch: number): Partial<IChainC
return {ALTAIR_FORK_EPOCH: 0, BELLATRIX_FORK_EPOCH: 0, CAPELLA_FORK_EPOCH: forkEpoch};
case ForkName.deneb:
return {ALTAIR_FORK_EPOCH: 0, BELLATRIX_FORK_EPOCH: 0, CAPELLA_FORK_EPOCH: 0, EIP4844_FORK_EPOCH: forkEpoch};
case ForkName.verge:
return {
ALTAIR_FORK_EPOCH: 0,
BELLATRIX_FORK_EPOCH: 0,
CAPELLA_FORK_EPOCH: 0,
EIP4844_FORK_EPOCH: 0,
VERGE_FORK_EPOCH: forkEpoch,
};
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from "chai";
import {ssz, capella, altair, deneb, allForks} from "@lodestar/types";
import {ssz, capella, altair, deneb, allForks, verge} from "@lodestar/types";
import {ForkName, ForkSeq} from "@lodestar/params";
import {createIBeaconConfig, createIChainForkConfig, defaultChainConfig} from "@lodestar/config";
import {upgradeLightClientHeader} from "@lodestar/light-client/spec";
Expand Down Expand Up @@ -30,8 +30,8 @@ describe("UpgradeLightClientHeader", function () {
denebLCHeader = ssz.deneb.LightClientHeader.defaultValue();
bellatrixLCHeader = ssz.altair.LightClientHeader.defaultValue();

update = [altairLCHeader, altairLCHeader, bellatrixLCHeader, capellaLCHeader, denebLCHeader];
testSlots = [0, 10, 17, 25, 33];
update = [altairLCHeader, altairLCHeader, bellatrixLCHeader, capellaLCHeader, denebLCHeader, denebLCHeader];
testSlots = [0, 10, 17, 25, 33, 41];
});

for (let i = ForkSeq.altair; i < Object.values(ForkName).length; i++) {
Expand Down
13 changes: 12 additions & 1 deletion packages/beacon-node/test/unit/network/fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ function getForkConfig({
bellatrix,
capella,
deneb,
verge,
}: {
phase0: number;
altair: number;
bellatrix: number;
capella: number;
deneb: number;
verge: number;
}): IBeaconConfig {
const forks: Record<ForkName, IForkInfo> = {
phase0: {
Expand Down Expand Up @@ -57,6 +59,14 @@ function getForkConfig({
prevVersion: Buffer.from([0, 0, 0, 3]),
prevForkName: ForkName.capella,
},
verge: {
name: ForkName.verge,
seq: ForkSeq.verge,
epoch: verge,
version: Buffer.from([0, 0, 0, 4]),
prevVersion: Buffer.from([0, 0, 0, 3]),
prevForkName: ForkName.capella,
},
};
const forksAscendingEpochOrder = Object.values(forks);
const forksDescendingEpochOrder = Object.values(forks).reverse();
Expand Down Expand Up @@ -134,9 +144,10 @@ for (const testScenario of testScenarios) {
const {phase0, altair, bellatrix, capella, testCases} = testScenario;
// TODO DENEB: Is it necessary to test?
const deneb = Infinity;
const verge = Infinity;

describe(`network / fork: phase0: ${phase0}, altair: ${altair}, bellatrix: ${bellatrix} capella: ${capella}`, () => {
const forkConfig = getForkConfig({phase0, altair, bellatrix, capella, deneb});
const forkConfig = getForkConfig({phase0, altair, bellatrix, capella, deneb, verge});
const forks = forkConfig.forks;
for (const testCase of testCases) {
const {epoch, currentFork, nextFork, activeForks} = testCase;
Expand Down
8 changes: 8 additions & 0 deletions packages/beacon-node/test/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ export function getConfig(fork: ForkName, forkEpoch = 0): IChainForkConfig {
CAPELLA_FORK_EPOCH: 0,
EIP4844_FORK_EPOCH: forkEpoch,
});
case ForkName.verge:
return createIChainForkConfig({
ALTAIR_FORK_EPOCH: 0,
BELLATRIX_FORK_EPOCH: 0,
CAPELLA_FORK_EPOCH: 0,
EIP4844_FORK_EPOCH: 0,
VERGE_FORK_EPOCH: forkEpoch,
});
}
}
4 changes: 4 additions & 0 deletions packages/config/src/chainConfig/presets/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export const chainConfig: IChainConfig = {
EIP4844_FORK_VERSION: b("0x04000000"),
EIP4844_FORK_EPOCH: Infinity,

// VERGE
VERGE_FORK_VERSION: b("0x05000000"),
VERGE_FORK_EPOCH: Infinity,

// Time parameters
// ---------------------------------------------------------------
// 12 seconds
Expand Down
3 changes: 3 additions & 0 deletions packages/config/src/chainConfig/presets/minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export const chainConfig: IChainConfig = {
// Deneb
EIP4844_FORK_VERSION: b("0x04000001"),
EIP4844_FORK_EPOCH: Infinity,
// Verge
VERGE_FORK_VERSION: b("0x05000001"),
VERGE_FORK_EPOCH: Infinity,

// Time parameters
// ---------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions packages/config/src/chainConfig/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export type IChainConfig = {
// DENEB
EIP4844_FORK_VERSION: Uint8Array;
EIP4844_FORK_EPOCH: number;
// VERGE
VERGE_FORK_VERSION: Uint8Array;
VERGE_FORK_EPOCH: number;

// Time parameters
SECONDS_PER_SLOT: number;
Expand Down Expand Up @@ -98,6 +101,9 @@ export const chainConfigTypes: SpecTypes<IChainConfig> = {
// DENEB
EIP4844_FORK_VERSION: "bytes",
EIP4844_FORK_EPOCH: "number",
// VERGE
VERGE_FORK_VERSION: "bytes",
VERGE_FORK_EPOCH: "number",

// Time parameters
SECONDS_PER_SLOT: "number",
Expand Down
10 changes: 9 additions & 1 deletion packages/config/src/forkConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ export function createIForkConfig(config: IChainConfig): IForkConfig {
prevVersion: config.CAPELLA_FORK_VERSION,
prevForkName: ForkName.capella,
};
const verge: IForkInfo = {
name: ForkName.verge,
seq: ForkSeq.verge,
epoch: config.VERGE_FORK_EPOCH,
version: config.VERGE_FORK_VERSION,
prevVersion: config.EIP4844_FORK_VERSION,
prevForkName: ForkName.deneb,
};

/** Forks in order order of occurence, `phase0` first */
// Note: Downstream code relies on proper ordering.
const forks = {phase0, altair, bellatrix, capella, deneb};
const forks = {phase0, altair, bellatrix, capella, deneb, verge};

// Prevents allocating an array on every getForkInfo() call
const forksAscendingEpochOrder = Object.values(forks);
Expand Down
2 changes: 2 additions & 0 deletions packages/params/src/forkName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum ForkName {
bellatrix = "bellatrix",
capella = "capella",
deneb = "deneb",
verge = "verge",
}

/**
Expand All @@ -18,6 +19,7 @@ export enum ForkSeq {
bellatrix = 2,
capella = 3,
deneb = 4,
verge = 5,
}

export type ForkLightClient = Exclude<ForkName, ForkName.phase0>;
Expand Down
7 changes: 7 additions & 0 deletions packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,10 @@ export const INTERVALS_PER_SLOT = 3;
export const BYTES_PER_FIELD_ELEMENT = 32;
export const BLOB_TX_TYPE = 0x05;
export const VERSIONED_HASH_VERSION_KZG = 0x01;

// TODO: Verge spec notes these as preset but there's only one value
// https://github.com/ethereum/consensus-specs/blob/db74090c1e8dc1fb2c052bae268e22dc63061e32/specs/verge/beacon-chain.md#preset
export const MAX_STEMS = 2 ** 16;
export const MAX_COMMITMENTS_PER_STEM = 33;
export const VERKLE_WIDTH = 256;
export const IPA_PROOF_DEPTH = 8;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ssz, allForks, capella, deneb} from "@lodestar/types";
import {ssz, allForks, capella, deneb, verge} from "@lodestar/types";
import {toHexString, byteArrayEquals} from "@chainsafe/ssz";
import {ForkSeq} from "@lodestar/params";
import {CachedBeaconStateBellatrix, CachedBeaconStateCapella} from "../types.js";
Expand Down Expand Up @@ -106,5 +106,10 @@ export function executionPayloadToPayloadHeader(
| deneb.ExecutionPayload).excessDataGas;
}

if (fork >= ForkSeq.verge) {
// https://github.com/ethereum/consensus-specs/blob/db74090c1e8dc1fb2c052bae268e22dc63061e32/specs/verge/beacon-chain.md#process_execution_payload
(bellatrixPayloadFields as verge.ExecutionPayloadHeader).executionWitness = (payload as verge.ExecutionPayload).executionWitness;
}

return bellatrixPayloadFields;
}
2 changes: 2 additions & 0 deletions packages/state-transition/src/cache/stateCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BeaconStateBellatrix,
BeaconStateCapella,
BeaconStateDeneb,
BeaconStateVerge,
} from "./types.js";

export type BeaconStateCache = {
Expand Down Expand Up @@ -118,6 +119,7 @@ export type CachedBeaconStateAltair = CachedBeaconState<BeaconStateAltair>;
export type CachedBeaconStateBellatrix = CachedBeaconState<BeaconStateBellatrix>;
export type CachedBeaconStateCapella = CachedBeaconState<BeaconStateCapella>;
export type CachedBeaconStateDeneb = CachedBeaconState<BeaconStateDeneb>;
export type CachedBeaconStateVerge = CachedBeaconState<BeaconStateVerge>;

export type CachedBeaconStateAllForks = CachedBeaconState<BeaconStateAllForks>;
export type CachedBeaconStateExecutions = CachedBeaconState<BeaconStateExecutions>;
Expand Down
6 changes: 4 additions & 2 deletions packages/state-transition/src/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type BeaconStateAltair = CompositeViewDU<typeof ssz.altair.BeaconState>;
export type BeaconStateBellatrix = CompositeViewDU<typeof ssz.bellatrix.BeaconState>;
export type BeaconStateCapella = CompositeViewDU<typeof ssz.capella.BeaconState>;
export type BeaconStateDeneb = CompositeViewDU<typeof ssz.deneb.BeaconState>;
export type BeaconStateVerge = CompositeViewDU<typeof ssz.verge.BeaconState>;

// Union at the TreeViewDU level
// - Works well as function argument and as generic type for allForks functions
Expand All @@ -17,6 +18,7 @@ export type BeaconStateAllForks =
| BeaconStateAltair
| BeaconStateBellatrix
| BeaconStateCapella
| BeaconStateDeneb;
| BeaconStateDeneb
| BeaconStateVerge;

export type BeaconStateExecutions = BeaconStateBellatrix | BeaconStateCapella | BeaconStateDeneb;
export type BeaconStateExecutions = BeaconStateBellatrix | BeaconStateCapella | BeaconStateDeneb | BeaconStateVerge;
1 change: 1 addition & 0 deletions packages/state-transition/src/slot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {upgradeStateToAltair} from "./upgradeStateToAltair.js";
export {upgradeStateToBellatrix} from "./upgradeStateToBellatrix.js";
export {upgradeStateToCapella} from "./upgradeStateToCapella.js";
export {upgradeStateToDeneb} from "./upgradeStateToDeneb.js";
export {upgradeStateToVerge} from "./upgradeStateToVerge.js";

/**
* Dial state to next slot. Common for all forks
Expand Down
28 changes: 28 additions & 0 deletions packages/state-transition/src/slot/upgradeStateToVerge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {ssz} from "@lodestar/types";
import {CachedBeaconStateDeneb, CachedBeaconStateVerge} from "../types.js";
import {getCachedBeaconState} from "../cache/stateCache.js";

/**
* Upgrade a state from Deneb to Verge.
*/
export function upgradeStateToVerge(stateDeneb: CachedBeaconStateDeneb): CachedBeaconStateVerge {
const {config} = stateDeneb;

const stateDenebNode = ssz.deneb.BeaconState.commitViewDU(stateDeneb);
const stateVergeView = ssz.verge.BeaconState.getViewDU(stateDenebNode);

const stateVerge = getCachedBeaconState(stateVergeView, stateDeneb);

stateVerge.fork = ssz.phase0.Fork.toViewDU({
previousVersion: stateDeneb.fork.currentVersion,
currentVersion: config.EIP4844_FORK_VERSION,
epoch: stateDeneb.epochCtx.epoch,
});

// Initialize ExecutionWitness empty List
stateVerge.latestExecutionPayloadHeader.executionWitness = ssz.verge.ExecutionWitness.defaultViewDU();

stateVerge.commit();

return stateVerge;
}
5 changes: 5 additions & 0 deletions packages/state-transition/src/stateTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CachedBeaconStateAltair,
CachedBeaconStateBellatrix,
CachedBeaconStateCapella,
CachedBeaconStateDeneb,
} from "./types.js";
import {computeEpochAtSlot} from "./util/index.js";
import {verifyProposerSignature} from "./signatureSets/index.js";
Expand All @@ -23,6 +24,7 @@ import {processBlock} from "./block/index.js";
import {processEpoch} from "./epoch/index.js";
import {BlockExternalData, DataAvailableStatus, ExecutionPayloadStatus} from "./block/externalData.js";
import {ProcessBlockOpts} from "./block/types.js";
import {upgradeStateToVerge} from "./slot/upgradeStateToVerge.js";

// Multifork capable state transition

Expand Down Expand Up @@ -179,6 +181,9 @@ function processSlotsWithTransientCache(
if (stateSlot === config.EIP4844_FORK_EPOCH) {
postState = upgradeStateToDeneb(postState as CachedBeaconStateCapella) as CachedBeaconStateAllForks;
}
if (stateSlot === config.VERGE_FORK_EPOCH) {
postState = upgradeStateToVerge(postState as CachedBeaconStateDeneb) as CachedBeaconStateAllForks;
}
} else {
postState.slot++;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/state-transition/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
CachedBeaconStateBellatrix,
CachedBeaconStateCapella,
CachedBeaconStateDeneb,
CachedBeaconStateVerge,
} from "./cache/stateCache.js";

export {
Expand All @@ -19,4 +20,5 @@ export {
BeaconStateBellatrix,
BeaconStateCapella,
BeaconStateDeneb,
BeaconStateVerge,
} from "./cache/types.js";
1 change: 1 addition & 0 deletions packages/state-transition/src/util/genesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export function initializeBeaconStateFromEth1(
| typeof ssz.bellatrix.ExecutionPayloadHeader
| typeof ssz.capella.ExecutionPayloadHeader
| typeof ssz.deneb.ExecutionPayloadHeader
| typeof ssz.verge.ExecutionPayloadHeader
>
): CachedBeaconStateAllForks {
const stateView = getGenesisBeaconState(
Expand Down
Loading