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
  • Loading branch information
g11tech committed Nov 23, 2023
1 parent a8767da commit de9d20d
Show file tree
Hide file tree
Showing 29 changed files with 588 additions and 28 deletions.
32 changes: 31 additions & 1 deletion packages/beacon-node/src/execution/engine/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {allForks, capella, deneb, Wei, bellatrix, Root} from "@lodestar/types";
import * as util from "node:util";
import {allForks, capella, deneb, Wei, bellatrix, Root, verge, ssz} from "@lodestar/types";
import {
BYTES_PER_LOGS_BLOOM,
FIELD_ELEMENTS_PER_BLOB,
Expand Down Expand Up @@ -133,6 +134,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 @@ -193,6 +195,20 @@ export function serializeExecutionPayload(fork: ForkName, data: allForks.Executi
payload.excessBlobGas = numToQuantity(excessBlobGas);
}

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

return payload;
}

Expand Down Expand Up @@ -269,6 +285,20 @@ export function parseExecutionPayload(
(executionPayload as deneb.ExecutionPayload).excessBlobGas = quantityToBigint(excessBlobGas);
}

// VERGE adds execution witness to the payload
if (ForkSeq[fork] >= ForkSeq.verge) {
// 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;
console.log(
"parse executionWitness from EL",
util.inspect(executionWitness, false, null, true /* enable colors */),
{blockNumber: data.blockNumber}
);
(executionPayload as verge.ExecutionPayload).executionWitness =
ssz.verge.ExecutionWitness.fromJson(executionWitness);
}

return {executionPayload, executionPayloadValue, blobsBundle};
}

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.verge:
return slotFns.upgradeStateToVerge(preState as CachedBeaconStateDeneb);
}
},
options: {
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/test/spec/presets/genesis.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "node:path";
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 @@ -47,7 +47,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 @@ -60,7 +60,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.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.verge:
return {
ALTAIR_FORK_EPOCH: 0,
BELLATRIX_FORK_EPOCH: 0,
CAPELLA_FORK_EPOCH: 0,
DENEB_FORK_EPOCH: 0,
ELECTRA_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 @@ -133,9 +143,10 @@ const testScenarios = [
for (const testScenario of testScenarios) {
const {phase0, altair, bellatrix, capella, testCases} = testScenario;
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 createChainForkConfig({
ALTAIR_FORK_EPOCH: 0,
BELLATRIX_FORK_EPOCH: 0,
CAPELLA_FORK_EPOCH: 0,
DENEB_FORK_EPOCH: 0,
ELECTRA_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
ELECTRA_FORK_VERSION: b("0x05000000"),
ELECTRA_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
ELECTRA_FORK_VERSION: b("0x05000001"),
ELECTRA_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
ELECTRA_FORK_VERSION: Uint8Array;
ELECTRA_FORK_EPOCH: number;

// Time parameters
SECONDS_PER_SLOT: number;
Expand Down Expand Up @@ -93,6 +96,9 @@ export const chainConfigTypes: SpecTypes<ChainConfig> = {
// DENEB
DENEB_FORK_VERSION: "bytes",
DENEB_FORK_EPOCH: "number",
// VERGE
ELECTRA_FORK_VERSION: "bytes",
ELECTRA_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: ForkInfo = {
name: ForkName.verge,
seq: ForkSeq.verge,
epoch: config.ELECTRA_FORK_EPOCH,
version: config.ELECTRA_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, verge, deneb};

// Prevents allocating an array on every getForkInfo() call
const forksAscendingEpochOrder = Object.values(forks);
Expand Down
15 changes: 12 additions & 3 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 @@ -17,7 +18,9 @@ export enum ForkSeq {
altair = 1,
bellatrix = 2,
capella = 3,
deneb = 4,
// Verge is scheduled after capella for now
verge = 4,
deneb = 5,
}

export type ForkPreLightClient = ForkName.phase0;
Expand All @@ -38,8 +41,14 @@ 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 function isForkVerge(fork: ForkName): fork is ForkVerge {
return isForkWithdrawals(fork) && fork !== ForkName.capella;
}

export type ForkPreBlobs = ForkPreVerge | ForkName.verge;
export type ForkBlobs = Exclude<ForkName, ForkPreBlobs>;
export function isForkBlobs(fork: ForkName): fork is ForkBlobs {
return isForkWithdrawals(fork) && fork !== ForkName.capella;
return isForkVerge(fork) && fork !== ForkName.verge;
}
7 changes: 7 additions & 0 deletions packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,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;
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 @@ -11,6 +11,7 @@ import {
BeaconStateBellatrix,
BeaconStateCapella,
BeaconStateDeneb,
BeaconStateVerge,
} from "./types.js";

export type BeaconStateCache = {
Expand Down Expand Up @@ -129,6 +130,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 @@ -7,6 +7,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 @@ -18,8 +19,9 @@ export type BeaconStateAllForks =
| BeaconStateAltair
| BeaconStateBellatrix
| BeaconStateCapella
| BeaconStateDeneb;
| BeaconStateDeneb
| BeaconStateVerge;

export type BeaconStateExecutions = BeaconStateBellatrix | BeaconStateCapella | BeaconStateDeneb;
export type BeaconStateExecutions = BeaconStateBellatrix | BeaconStateCapella | BeaconStateDeneb | BeaconStateVerge;

export type ShufflingGetter = (shufflingEpoch: Epoch, dependentRoot: RootHex) => EpochShuffling | null;
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
26 changes: 26 additions & 0 deletions packages/state-transition/src/slot/upgradeStateToVerge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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.DENEB_FORK_VERSION,
epoch: stateDeneb.epochCtx.epoch,
});

// latestExecutionPayloadHeader's executionWitnessRoot will have default zero root

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.ELECTRA_FORK_EPOCH) {
postState = upgradeStateToVerge(postState as CachedBeaconStateDeneb) as CachedBeaconStateAllForks;
}
} else {
postState.slot++;
}
Expand Down
Loading

0 comments on commit de9d20d

Please sign in to comment.