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

rebase fixes

get the types to match current kaustinen network

insert verge between capella and deneb

add payload to execution witness

working local build with kaunstinen v2 geth build

use electra in config

few corrections

fix build

integrated ssz with optional

deep log witness

fix serialization for new payload

rename verge to electra and fix upgrade issues and transition banner

fix type issues

add parent stateroot to witness

rebase fixes

rename electra to verkle
  • Loading branch information
g11tech committed Sep 5, 2024
1 parent 5d2e1a7 commit 343e718
Show file tree
Hide file tree
Showing 29 changed files with 570 additions and 12 deletions.
20 changes: 20 additions & 0 deletions packages/beacon-node/src/chain/blocks/utils/elephantWithWings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const VERKLE_ELEPHANTWITHWINGS_BANNER = String.raw`
:~~.
:!:^::^^!!^
:!^^ !:J.
.!!:! .^7^
.?7~: ^#! !!!^!~.
:!^ GP~..^^?^ ^!:
!^ Y5 #7:
.:^^~7~. ! ~~6800~~^ ^6800^ ~~6800 68 00 68~~ 00~ ||
.~! 55J ~~6800~~ :! !^ .YP :JP 7B. ^7#
^7 :: 5 #:::^::~~6800~~^^?!?.~:: :! !^ 6800 .^PJ JG ~!B
:G ^7 ^ !^68 00^ ~~6800~~ :! !: .&~ ^#! ^!55~~~6800~~7G~ !7B
7G ~!5J ^? !.----. ~! ~~6800~~ .7#: 6800 ~
?G :^?G. ...... ^?^.!G. ?G :^?G. ...... ^?^.!G. !
~! ~~6800~~ .7#: 6800 G~ 7G ~!5J ^? !.----. ::
:! !: .&~ ^#! ^!55~~~6800~~7G~ !7B :G ^7 ^ !^68 00^ ~~6800~~ ::
:! !^ 6800 .^PJ JG ~!B ^7 :: 5 ^^ !!!! #:::^::~~6800~~^^?!?.~
:! !^ .YP :JP 7B. ^7# .~!~ ~~6800 ^6800^ ~~6800~~
6800 68 00 68~~ 00~ 6800
`;
6 changes: 6 additions & 0 deletions packages/beacon-node/src/chain/blocks/verifyBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {BlockInput, ImportBlockOpts, BlockInputType} from "./types.js";
import {POS_PANDA_MERGE_TRANSITION_BANNER} from "./utils/pandaMergeTransitionBanner.js";
import {CAPELLA_OWL_BANNER} from "./utils/ownBanner.js";
import {DENEB_BLOWFISH_BANNER} from "./utils/blowfishBanner.js";
import {VERKLE_ELEPHANTWITHWINGS_BANNER} from "./utils/elephantWithWings.js";
import {verifyBlocksStateTransitionOnly} from "./verifyBlocksStateTransitionOnly.js";
import {verifyBlocksSignatures} from "./verifyBlocksSignatures.js";
import {verifyBlocksExecutionPayload, SegmentExecStatus} from "./verifyBlocksExecutionPayloads.js";
Expand Down Expand Up @@ -152,6 +153,11 @@ export async function verifyBlocksInEpoch(
this.logger.info("Activating withdrawals", {epoch: this.config.CAPELLA_FORK_EPOCH});
break;

case ForkName.verkle:
this.logger.info(VERKLE_ELEPHANTWITHWINGS_BANNER);
this.logger.info("Activating verkle", {epoch: this.config.VERKLE_FORK_EPOCH});
break;

case ForkName.deneb:
this.logger.info(DENEB_BLOWFISH_BANNER);
this.logger.info("Activating blobs", {epoch: this.config.DENEB_FORK_EPOCH});
Expand Down
27 changes: 25 additions & 2 deletions packages/beacon-node/src/execution/engine/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {capella, deneb, Wei, bellatrix, Root, ExecutionPayload} from "@lodestar/types";
import {capella, deneb, Wei, bellatrix, Root, verkle, ExecutionPayload, ssz} from "@lodestar/types";
import {
BYTES_PER_LOGS_BLOOM,
FIELD_ELEMENTS_PER_BLOB,
Expand All @@ -18,7 +18,6 @@ import {
} from "../../eth1/provider/utils.js";
import {ExecutionPayloadStatus, BlobsBundle, PayloadAttributes, VersionedHashes} from "./interface.js";
import {WithdrawalV1} from "./payloadIdCache.js";

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

export type EngineApiRpcParamTypes = {
Expand Down Expand Up @@ -141,6 +140,7 @@ export type ExecutionPayloadRpc = {
blobGasUsed?: QUANTITY; // DENEB
excessBlobGas?: QUANTITY; // DENEB
parentBeaconBlockRoot?: QUANTITY; // DENEB
executionWitness?: Record<string, unknown>; // DENEB
};

export type WithdrawalRpc = {
Expand Down Expand Up @@ -212,6 +212,20 @@ export function serializeExecutionPayload(fork: ForkName, data: ExecutionPayload
payload.excessBlobGas = numToQuantity(excessBlobGas);
}

// VERKLE adds executionWitness to the ExecutionPayload
if (ForkSeq[fork] >= ForkSeq.verkle) {
const {executionWitness} = data as verkle.ExecutionPayload;
// right now the caseMap of ssz ExecutionWitness is camel cased and can
// directly be used to serialize tojson
payload.executionWitness = ssz.verkle.ExecutionWitness.toJson(executionWitness);
// serialization with ssz serialize suffix diff's suffix to a string while geth expects num
(payload.executionWitness as verkle.ExecutionWitness).stateDiff.forEach((sDiff) => {
sDiff.suffixDiffs.forEach((sfDiff) => {
sfDiff.suffix = Number(sfDiff.suffix);
});
});
}

return payload;
}

Expand Down Expand Up @@ -297,6 +311,15 @@ export function parseExecutionPayload(
(executionPayload as deneb.ExecutionPayload).excessBlobGas = quantityToBigint(excessBlobGas);
}

// VERKLE adds execution witness to the payload
if (ForkSeq[fork] >= ForkSeq.verkle) {
// right now the casing of executionWitness is camel case in the ssz caseMap
// we can directly use fromJson to read the serialized data from payload
const {executionWitness} = data;
(executionPayload as verkle.ExecutionPayload).executionWitness =
ssz.verkle.ExecutionWitness.fromJson(executionWitness);
}

return {executionPayload, executionPayloadValue, blobsBundle, shouldOverrideBuilder};
}

Expand Down
3 changes: 3 additions & 0 deletions packages/beacon-node/test/spec/presets/fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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 @@ -35,6 +36,8 @@ const fork: TestRunnerFn<ForkStateCase, BeaconStateAllForks> = (forkNext) => {
return slotFns.upgradeStateToCapella(preState as CachedBeaconStateBellatrix);
case ForkName.deneb:
return slotFns.upgradeStateToDeneb(preState as CachedBeaconStateCapella);
case ForkName.verkle:
return slotFns.upgradeStateToVerkle(preState as CachedBeaconStateDeneb);
}
},
options: {
Expand Down
8 changes: 8 additions & 0 deletions packages/beacon-node/test/spec/presets/transition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ function getTransitionConfig(fork: ForkName, forkEpoch: number): Partial<ChainCo
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};
case ForkName.verkle:
return {
ALTAIR_FORK_EPOCH: 0,
BELLATRIX_FORK_EPOCH: 0,
CAPELLA_FORK_EPOCH: 0,
DENEB_FORK_EPOCH: 0,
VERKLE_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(),
verkle: ssz.verkle.LightClientHeader.defaultValue(),
};

testSlots = {
Expand All @@ -35,6 +36,7 @@ describe("UpgradeLightClientHeader", function () {
bellatrix: 17,
capella: 25,
deneb: 33,
verkle: 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,
verkle,
}: {
phase0: number;
altair: number;
bellatrix: number;
capella: number;
deneb: number;
verkle: 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,
},
verkle: {
name: ForkName.verkle,
seq: ForkSeq.verkle,
epoch: verkle,
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 @@ -133,9 +143,10 @@ const testScenarios = [
for (const testScenario of testScenarios) {
const {phase0, altair, bellatrix, capella, testCases} = testScenario;
const deneb = Infinity;
const verkle = 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, verkle});
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.verkle:
return createChainForkConfig({
ALTAIR_FORK_EPOCH: 0,
BELLATRIX_FORK_EPOCH: 0,
CAPELLA_FORK_EPOCH: 0,
DENEB_FORK_EPOCH: 0,
VERKLE_FORK_EPOCH: forkEpoch,
});
}
}
4 changes: 4 additions & 0 deletions packages/config/src/chainConfig/configs/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export const chainConfig: ChainConfig = {
DENEB_FORK_VERSION: b("0x04000000"),
DENEB_FORK_EPOCH: 269568, // March 13, 2024, 01:55:35pm UTC

// VERKLE
VERKLE_FORK_VERSION: b("0x05000000"),
VERKLE_FORK_EPOCH: Infinity,

// Time parameters
// ---------------------------------------------------------------
// 12 seconds
Expand Down
3 changes: 3 additions & 0 deletions packages/config/src/chainConfig/configs/minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export const chainConfig: ChainConfig = {
// Deneb
DENEB_FORK_VERSION: b("0x04000001"),
DENEB_FORK_EPOCH: Infinity,
// Verkle
VERKLE_FORK_VERSION: b("0x05000001"),
VERKLE_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;
// VERKLE
VERKLE_FORK_VERSION: Uint8Array;
VERKLE_FORK_EPOCH: number;

// Time parameters
SECONDS_PER_SLOT: number;
Expand Down Expand Up @@ -99,6 +102,9 @@ export const chainConfigTypes: SpecTypes<ChainConfig> = {
// DENEB
DENEB_FORK_VERSION: "bytes",
DENEB_FORK_EPOCH: "number",
// VERKLE
VERKLE_FORK_VERSION: "bytes",
VERKLE_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 @@ -59,10 +59,18 @@ export function createForkConfig(config: ChainConfig): ForkConfig {
prevVersion: config.CAPELLA_FORK_VERSION,
prevForkName: ForkName.capella,
};
const verkle: ForkInfo = {
name: ForkName.verkle,
seq: ForkSeq.verkle,
epoch: config.VERKLE_FORK_EPOCH,
version: config.VERKLE_FORK_VERSION,
prevVersion: config.CAPELLA_FORK_VERSION,
prevForkName: ForkName.capella,
};

/** 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, verkle, deneb};

// Prevents allocating an array on every getForkInfo() call
const forksAscendingEpochOrder = Object.values(forks);
Expand Down
24 changes: 20 additions & 4 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",
verkle = "verkle",
}

/**
Expand All @@ -17,7 +18,9 @@ export enum ForkSeq {
altair = 1,
bellatrix = 2,
capella = 3,
deneb = 4,
// Verkle is scheduled after capella for now
verkle = 4,
deneb = 5,
}

function exclude<T extends ForkName, U extends T>(coll: T[], val: U[]): Exclude<T, U>[] {
Expand Down Expand Up @@ -72,9 +75,22 @@ export function isForkWithdrawals(fork: ForkName): fork is ForkWithdrawals {
return isForkExecution(fork) && fork !== ForkName.bellatrix;
}

export type ForkPreBlobs = ForkPreWithdrawals | ForkName.capella;
export type ForkPreVerge = ForkPreWithdrawals | ForkName.capella;
export type ForkVerge = Exclude<ForkName, ForkPreVerge>;
export const forkVerge = exclude(forkAll, [ForkName.phase0, ForkName.altair, ForkName.bellatrix, ForkName.capella]);
export function isForkVerge(fork: ForkName): fork is ForkVerge {
return isForkWithdrawals(fork) && fork !== ForkName.capella;
}

export type ForkPreBlobs = ForkPreVerge | ForkName.verkle;
export type ForkBlobs = Exclude<ForkName, ForkPreBlobs>;
export const forkBlobs = exclude(forkAll, [ForkName.phase0, ForkName.altair, ForkName.bellatrix, ForkName.capella]);
export const forkBlobs = exclude(forkAll, [
ForkName.phase0,
ForkName.altair,
ForkName.bellatrix,
ForkName.capella,
ForkName.verkle,
]);
export function isForkBlobs(fork: ForkName): fork is ForkBlobs {
return isForkWithdrawals(fork) && fork !== ForkName.capella;
return isForkVerge(fork) && fork !== ForkName.verkle;
}
7 changes: 7 additions & 0 deletions packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,10 @@ export const KZG_COMMITMENT_SUBTREE_INDEX0 = KZG_COMMITMENT_GINDEX0 - 2 ** KZG_C

// ssz.deneb.BlobSidecars.elementType.fixedSize
export const BLOBSIDECAR_FIXED_SIZE = ACTIVE_PRESET === PresetName.minimal ? 131672 : 131928;

// TODO: Verkle 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;
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 @@ -10,6 +10,7 @@ import {
BeaconStateBellatrix,
BeaconStateCapella,
BeaconStateDeneb,
BeaconStateVerkle,
} from "./types.js";
import {RewardCache, createEmptyRewardCache} from "./rewardCache.js";

Expand Down Expand Up @@ -130,6 +131,7 @@ export type CachedBeaconStateAltair = CachedBeaconState<BeaconStateAltair>;
export type CachedBeaconStateBellatrix = CachedBeaconState<BeaconStateBellatrix>;
export type CachedBeaconStateCapella = CachedBeaconState<BeaconStateCapella>;
export type CachedBeaconStateDeneb = CachedBeaconState<BeaconStateDeneb>;
export type CachedBeaconStateVerkle = CachedBeaconState<BeaconStateVerkle>;

export type CachedBeaconStateAllForks = CachedBeaconState<BeaconStateAllForks>;
export type CachedBeaconStateExecutions = CachedBeaconState<BeaconStateExecutions>;
Expand Down
1 change: 1 addition & 0 deletions packages/state-transition/src/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type BeaconStatePhase0 = CompositeViewDU<SSZTypesFor<ForkName.phase0, "Be
export type BeaconStateAltair = CompositeViewDU<SSZTypesFor<ForkName.altair, "BeaconState">>;
export type BeaconStateBellatrix = CompositeViewDU<SSZTypesFor<ForkName.bellatrix, "BeaconState">>;
export type BeaconStateCapella = CompositeViewDU<SSZTypesFor<ForkName.capella, "BeaconState">>;
export type BeaconStateVerkle = CompositeViewDU<SSZTypesFor<ForkName.verkle, "BeaconState">>;
export type BeaconStateDeneb = CompositeViewDU<SSZTypesFor<ForkName.deneb, "BeaconState">>;

export type BeaconStateAllForks = CompositeViewDU<SSZTypesFor<ForkAll, "BeaconState">>;
Expand Down
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 {upgradeStateToVerkle} from "./upgradeStateToVerkle.js";

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

/**
* Upgrade a state from Capella (Eventualy DENEB) to Verkle.
*/
export function upgradeStateToVerkle(stateCapella: CachedBeaconStateCapella): CachedBeaconStateVerkle {
const {config} = stateCapella;

const stateCapellaNode = ssz.capella.BeaconState.commitViewDU(stateCapella);
const stateVerkleView = ssz.verkle.BeaconState.getViewDU(stateCapellaNode);

const stateVerkle = getCachedBeaconState(stateVerkleView, stateCapella);

stateVerkle.fork = ssz.phase0.Fork.toViewDU({
previousVersion: stateCapella.fork.currentVersion,
currentVersion: config.VERKLE_FORK_VERSION,
epoch: stateCapella.epochCtx.epoch,
});

// latestExecutionPayloadHeader's executionWitnessRoot will have default zero root

stateVerkle.commit();
return stateVerkle;
}
Loading

0 comments on commit 343e718

Please sign in to comment.