Skip to content

Commit

Permalink
Add types
Browse files Browse the repository at this point in the history
Update state transition

Fix tests

Add fork to LC tests
  • Loading branch information
dapplion authored and g11tech committed Aug 1, 2023
1 parent 67b6e79 commit 83799df
Show file tree
Hide file tree
Showing 28 changed files with 460 additions and 25 deletions.
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
10 changes: 9 additions & 1 deletion packages/beacon-node/test/spec/presets/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ function getTransitionConfig(fork: ForkName, forkEpoch: number): Partial<ChainCo
case ForkName.capella:
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, DENEB_FORK_EPOCH: forkEpoch};
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,
DENEB_FORK_EPOCH: 0,
VERGE_FORK_EPOCH: forkEpoch,
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("UpgradeLightClientHeader", function () {
capella: ssz.capella.LightClientHeader.defaultValue(),
bellatrix: ssz.altair.LightClientHeader.defaultValue(),
deneb: ssz.deneb.LightClientHeader.defaultValue(),
verge: ssz.verge.LightClientHeader.defaultValue(),
};

testSlots = {
Expand All @@ -35,6 +36,7 @@ describe("UpgradeLightClientHeader", function () {
bellatrix: 17,
capella: 25,
deneb: 33,
verge: 41,
};
});

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;
}): BeaconConfig {
const forks: Record<ForkName, ForkInfo> = {
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): ChainForkConfig {
CAPELLA_FORK_EPOCH: 0,
DENEB_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: ChainConfig = {
DENEB_FORK_VERSION: b("0x04000000"),
DENEB_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: ChainConfig = {
// Deneb
DENEB_FORK_VERSION: b("0x04000001"),
DENEB_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 ChainConfig = {
// DENEB
DENEB_FORK_VERSION: Uint8Array;
DENEB_FORK_EPOCH: number;
// VERGE
VERGE_FORK_VERSION: Uint8Array;
VERGE_FORK_EPOCH: number;

// Time parameters
SECONDS_PER_SLOT: number;
Expand Down Expand Up @@ -92,6 +95,9 @@ export const chainConfigTypes: SpecTypes<ChainConfig> = {
// DENEB
DENEB_FORK_VERSION: "bytes",
DENEB_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 createForkConfig(config: ChainConfig): ForkConfig {
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 @@ -245,3 +245,10 @@ export const INTERVALS_PER_SLOT = 3;
export const BYTES_PER_FIELD_ELEMENT = 32;
export const BLOB_TX_TYPE = 0x03;
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,5 +1,5 @@
import {toHexString, byteArrayEquals} from "@chainsafe/ssz";
import {ssz, allForks, capella, deneb} from "@lodestar/types";
import {ssz, allForks, capella, deneb, verge} from "@lodestar/types";
import {ForkSeq, MAX_BLOBS_PER_BLOCK} from "@lodestar/params";
import {CachedBeaconStateBellatrix, CachedBeaconStateCapella} from "../types.js";
import {getRandaoMix} from "../util/index.js";
Expand Down Expand Up @@ -117,5 +117,10 @@ export function executionPayloadToPayloadHeader(
).excessBlobGas;
}

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 @@ -126,6 +127,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 @@ -196,6 +198,9 @@ function processSlotsWithTransientCache(
if (stateSlot === config.DENEB_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 @@ -214,6 +214,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

0 comments on commit 83799df

Please sign in to comment.