Skip to content

Commit

Permalink
chore: rebase fixes
Browse files Browse the repository at this point in the history
lint

lint and type fix

update spec version

fix tests

fix spec test

Update packages/beacon-node/src/chain/stateCache/types.ts

Co-authored-by: Cayman <[email protected]>

Update packages/beacon-node/src/chain/stateCache/types.ts

Co-authored-by: Cayman <[email protected]>

Update packages/beacon-node/src/chain/blocks/types.ts

Co-authored-by: Cayman <[email protected]>

Update packages/types/src/utils/typeguards.ts

Co-authored-by: Cayman <[email protected]>

Variable naming

fix the ForkElectra

fix build

fix spec test

Co-authored-by: Cayman <[email protected]>
  • Loading branch information
g11tech and wemeetagain committed Aug 9, 2024
1 parent 2bb60c7 commit bb9d811
Show file tree
Hide file tree
Showing 23 changed files with 99 additions and 55 deletions.
5 changes: 5 additions & 0 deletions packages/api/test/unit/beacon/oapiSpec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ const ignoredTopics = [
topic block_gossip not implemented
*/
"block_gossip",

// Modified in electra to include version
// should be removed from the ignore list after spec update
"attestation",
"attester_slashing",
];

// eventstream types are defined as comments in the description of "examples".
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/api/impl/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
VERSIONED_HASH_VERSION_KZG,
COMPOUNDING_WITHDRAWAL_PREFIX,
DOMAIN_CONSOLIDATION,
UNSET_DEPOSIT_RECEIPTS_START_INDEX,
UNSET_DEPOSIT_REQUESTS_START_INDEX,
FULL_EXIT_REQUEST_AMOUNT,
} from "@lodestar/params";

Expand Down Expand Up @@ -108,6 +108,6 @@ export const specConstants = {
VERSIONED_HASH_VERSION_KZG,

// electra
UNSET_DEPOSIT_RECEIPTS_START_INDEX,
UNSET_DEPOSIT_REQUESTS_START_INDEX,
FULL_EXIT_REQUEST_AMOUNT,
};
4 changes: 2 additions & 2 deletions packages/beacon-node/src/chain/blocks/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {CachedBeaconStateAllForks, computeEpochAtSlot} from "@lodestar/state-transition";
import {MaybeValidExecutionStatus, DataAvailabilityStatus} from "@lodestar/fork-choice";
import {deneb, Slot, RootHex, SignedBeaconBlock} from "@lodestar/types";
import {ForkSeq, ForkName} from "@lodestar/params";
import {ForkSeq, ForkName, ForkBlobs} from "@lodestar/params";
import {ChainForkConfig} from "@lodestar/config";

export enum BlockInputType {
Expand Down Expand Up @@ -36,7 +36,7 @@ export enum GossipedInputType {

type BlobsCacheMap = Map<number, {blobSidecar: deneb.BlobSidecar; blobBytes: Uint8Array | null}>;

type ForkBlobsInfo = {fork: ForkName.deneb | ForkName.electra};
type ForkBlobsInfo = {fork: ForkBlobs};
type BlobsData = {blobs: deneb.BlobSidecars; blobsBytes: (Uint8Array | null)[]; blobsSource: BlobsSource};
export type BlockInputDataBlobs = ForkBlobsInfo & BlobsData;
export type BlockInputData = BlockInputDataBlobs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {aggregateSignatures} from "@chainsafe/blst";
import {Signature} from "@chainsafe/bls/types";
import {aggregateSignatures, Signature} from "@chainsafe/blst";
import {BitArray, toHexString} from "@chainsafe/ssz";
import {
ForkName,
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/chain/opPools/opPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class OpPool {

const endAttesterSlashings = stepsMetrics?.startTimer();
const attesterSlashings: AttesterSlashing[] = [];
const maxAttesterSlashing = stateFork >= ForkSeq.electra ? MAX_ATTESTER_SLASHINGS_ELECTRA : MAX_ATTESTER_SLASHINGS;
const maxAttesterSlashings = stateFork >= ForkSeq.electra ? MAX_ATTESTER_SLASHINGS_ELECTRA : MAX_ATTESTER_SLASHINGS;
attesterSlashing: for (const attesterSlashing of this.attesterSlashings.values()) {
/** Indices slashable in this attester slashing */
const slashableIndices = new Set<ValidatorIndex>();
Expand All @@ -224,7 +224,7 @@ export class OpPool {
if (isSlashableAtEpoch(validator, stateEpoch)) {
slashableIndices.add(index);
}
if (attesterSlashings.length >= maxAttesterSlashing) {
if (attesterSlashings.length >= maxAttesterSlashings) {
break attesterSlashing;
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/beacon-node/src/chain/stateCache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export interface BlockStateCache {
prune(headStateRootHex: RootHex): void;
deleteAllBeforeEpoch(finalizedEpoch: Epoch): void;
dumpSummary(): routes.lodestar.StateCacheItem[];
getStates(): IterableIterator<CachedBeaconStateAllForks>; // Expose beacon states stored in cache. Use with caution
/** Expose beacon states stored in cache. Use with caution */
getStates(): IterableIterator<CachedBeaconStateAllForks>;
}

/**
Expand Down Expand Up @@ -75,7 +76,8 @@ export interface CheckpointStateCache {
processState(blockRootHex: RootHex, state: CachedBeaconStateAllForks): Promise<number>;
clear(): void;
dumpSummary(): routes.lodestar.StateCacheItem[];
getStates(): IterableIterator<CachedBeaconStateAllForks>; // Expose beacon states stored in cache. Use with caution
/** Expose beacon states stored in cache. Use with caution */
getStates(): IterableIterator<CachedBeaconStateAllForks>;
}

export enum CacheItemType {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {PublicKey} from "@chainsafe/bls/types";
import {PublicKey} from "@chainsafe/blst";
import {DOMAIN_AGGREGATE_AND_PROOF, ForkSeq} from "@lodestar/params";
import {ssz, SignedAggregateAndProof} from "@lodestar/types";
import {Epoch} from "@lodestar/types";
Expand Down
2 changes: 0 additions & 2 deletions packages/beacon-node/src/execution/engine/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,4 @@ export interface IExecutionEngine {
getPayloadBodiesByHash(fork: ForkName, blockHash: DATA[]): Promise<(ExecutionPayloadBody | null)[]>;

getPayloadBodiesByRange(fork: ForkName, start: number, count: number): Promise<(ExecutionPayloadBody | null)[]>;

getState(): ExecutionEngineState;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {itBench, setBenchOpts} from "@dapplion/benchmark";
import {Map} from "immutable";
import {toBufferBE} from "bigint-buffer";
import {digest} from "@chainsafe/as-sha256";
import type {SecretKey} from "@chainsafe/bls/types";
import bls from "@chainsafe/bls";
import {SecretKey} from "@chainsafe/blst";
import {ssz} from "@lodestar/types";
import {type CachedBeaconStateAllForks, PubkeyIndexMap} from "@lodestar/state-transition";
import {bytesToBigInt, intToBytes} from "@lodestar/utils";
Expand Down Expand Up @@ -106,6 +105,7 @@ describe("updateUnfinalizedPubkeys perf tests", function () {

function generatePrivateKey(index: number): SecretKey {
const secretKeyBytes = toBufferBE(bytesToBigInt(digest(intToBytes(index, 32))) % BigInt("38581184513"), 32);
return bls.SecretKey.fromBytes(secretKeyBytes);
const secret: SecretKey = SecretKey.fromBytes(secretKeyBytes);
return secret;
}
});
4 changes: 2 additions & 2 deletions packages/beacon-node/test/sim/electra-interop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from "node:assert";
import {describe, it, vi, afterAll, afterEach} from "vitest";

import {LogLevel, sleep} from "@lodestar/utils";
import {ForkName, SLOTS_PER_EPOCH, UNSET_DEPOSIT_RECEIPTS_START_INDEX} from "@lodestar/params";
import {ForkName, SLOTS_PER_EPOCH, UNSET_DEPOSIT_REQUESTS_START_INDEX} from "@lodestar/params";
import {electra, Epoch, Slot} from "@lodestar/types";
import {ValidatorProposerConfig} from "@lodestar/validator";

Expand Down Expand Up @@ -431,7 +431,7 @@ describe("executionEngine / ExecutionEngineHttp", function () {
throw Error("Historical validator length for epoch 1 or 2 is not dropped properly");
}

if (headState.depositRequestsStartIndex === UNSET_DEPOSIT_RECEIPTS_START_INDEX) {
if (headState.depositRequestsStartIndex === UNSET_DEPOSIT_REQUESTS_START_INDEX) {
throw Error("state.depositRequestsStartIndex is not set upon processing new deposit receipt");
}

Expand Down
26 changes: 18 additions & 8 deletions packages/beacon-node/test/spec/presets/fork_choice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ import {toHexString} from "@chainsafe/ssz";
import {BeaconStateAllForks, isExecutionStateType, signedBlockToSignedHeader} from "@lodestar/state-transition";
import {InputType} from "@lodestar/spec-test-util";
import {CheckpointWithHex, ForkChoice} from "@lodestar/fork-choice";
import {phase0, bellatrix, ssz, RootHex, deneb, BeaconBlock, SignedBeaconBlock} from "@lodestar/types";
import {
bellatrix,
ssz,
RootHex,
deneb,
BeaconBlock,
SignedBeaconBlock,
sszTypesFor,
Attestation,
AttesterSlashing,
} from "@lodestar/types";
import {bnToNum, fromHex} from "@lodestar/utils";
import {createBeaconConfig} from "@lodestar/config";
import {ACTIVE_PRESET, ForkSeq, isForkBlobs, ForkName} from "@lodestar/params";
Expand Down Expand Up @@ -136,7 +146,7 @@ const forkChoiceTest =
const attestation = testcase.attestations.get(step.attestation);
if (!attestation) throw Error(`No attestation ${step.attestation}`);
const headState = chain.getHeadState();
const attDataRootHex = toHexString(ssz.phase0.AttestationData.hashTreeRoot(attestation.data));
const attDataRootHex = toHexString(sszTypesFor(fork).AttestationData.hashTreeRoot(attestation.data));
chain.forkChoice.onAttestation(
headState.epochCtx.getIndexedAttestation(ForkSeq[fork], attestation),
attDataRootHex
Expand Down Expand Up @@ -343,16 +353,16 @@ const forkChoiceTest =
[BLOCK_FILE_NAME]: ssz[fork].SignedBeaconBlock,
[BLOBS_FILE_NAME]: ssz.deneb.Blobs,
[POW_BLOCK_FILE_NAME]: ssz.bellatrix.PowBlock,
[ATTESTATION_FILE_NAME]: ssz.phase0.Attestation,
[ATTESTER_SLASHING_FILE_NAME]: ssz.phase0.AttesterSlashing,
[ATTESTATION_FILE_NAME]: sszTypesFor(fork).Attestation,
[ATTESTER_SLASHING_FILE_NAME]: sszTypesFor(fork).AttesterSlashing,
},
mapToTestCase: (t: Record<string, any>) => {
// t has input file name as key
const blocks = new Map<string, SignedBeaconBlock>();
const blobs = new Map<string, deneb.Blobs>();
const powBlocks = new Map<string, bellatrix.PowBlock>();
const attestations = new Map<string, phase0.Attestation>();
const attesterSlashings = new Map<string, phase0.AttesterSlashing>();
const attestations = new Map<string, Attestation>();
const attesterSlashings = new Map<string, AttesterSlashing>();
for (const key in t) {
const blockMatch = key.match(BLOCK_FILE_NAME);
if (blockMatch) {
Expand Down Expand Up @@ -495,8 +505,8 @@ type ForkChoiceTestCase = {
blocks: Map<string, SignedBeaconBlock>;
blobs: Map<string, deneb.Blobs>;
powBlocks: Map<string, bellatrix.PowBlock>;
attestations: Map<string, phase0.Attestation>;
attesterSlashings: Map<string, phase0.AttesterSlashing>;
attestations: Map<string, Attestation>;
attesterSlashings: Map<string, AttesterSlashing>;
};

function isTick(step: Step): step is OnTick {
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/test/spec/presets/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ const operations: TestRunnerFn<OperationsTestCase, BeaconStateAllForks> = (fork,
sszTypes: {
pre: ssz[fork].BeaconState,
post: ssz[fork].BeaconState,
attestation: ssz.phase0.Attestation,
attester_slashing: ssz.phase0.AttesterSlashing,
attestation: sszTypesFor(fork).Attestation,
attester_slashing: sszTypesFor(fork).AttesterSlashing,
block: ssz[fork].BeaconBlock,
body: ssz[fork].BeaconBlockBody,
deposit: ssz.phase0.Deposit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {BitArray, fromHexString, toHexString} from "@chainsafe/ssz";
import {describe, it, expect, beforeEach, beforeAll, afterEach, vi} from "vitest";
import {SecretKey, Signature, fastAggregateVerify} from "@chainsafe/blst";
import {SecretKey, Signature, fastAggregateVerify, aggregateSignatures} from "@chainsafe/blst";
import {CachedBeaconStateAllForks, newFilledArray} from "@lodestar/state-transition";
import {
FAR_FUTURE_EPOCH,
Expand Down Expand Up @@ -331,9 +331,9 @@ describe("MatchingDataAttestationGroup aggregateInto", function () {
});

describe("aggregateConsolidation", function () {
const sk0 = bls.SecretKey.fromBytes(Buffer.alloc(32, 1));
const sk1 = bls.SecretKey.fromBytes(Buffer.alloc(32, 2));
const sk2 = bls.SecretKey.fromBytes(Buffer.alloc(32, 3));
const sk0 = SecretKey.fromBytes(Buffer.alloc(32, 1));
const sk1 = SecretKey.fromBytes(Buffer.alloc(32, 2));
const sk2 = SecretKey.fromBytes(Buffer.alloc(32, 3));
const skArr = [sk0, sk1, sk2];
const testCases: {
name: string;
Expand Down Expand Up @@ -397,7 +397,7 @@ describe("aggregateConsolidation", function () {
expect(finalAttestation.aggregationBits.uint8Array).toEqual(new Uint8Array(expectedAggregationBits));
expect(finalAttestation.committeeBits.toBoolArray()).toEqual(expectedCommitteeBits);
expect(finalAttestation.data).toEqual(attData);
expect(finalAttestation.signature).toEqual(bls.Signature.aggregate(sigArr).toBytes());
expect(finalAttestation.signature).toEqual(aggregateSignatures(sigArr).toBytes());
});
}
});
13 changes: 13 additions & 0 deletions packages/params/src/forkName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,16 @@ export const forkBlobs = exclude(forkAll, [ForkName.phase0, ForkName.altair, For
export function isForkBlobs(fork: ForkName): fork is ForkBlobs {
return isForkWithdrawals(fork) && fork !== ForkName.capella;
}

export type ForkPreElectra = ForkPreBlobs | ForkName.deneb;
export type ForkElectra = Exclude<ForkName, ForkPreElectra>;
export const forkElectra = exclude(forkAll, [
ForkName.phase0,
ForkName.altair,
ForkName.bellatrix,
ForkName.capella,
ForkName.deneb,
]);
export function isForkElectra(fork: ForkName): fork is ForkElectra {
return isForkBlobs(fork) && fork !== ForkName.deneb;
}
2 changes: 1 addition & 1 deletion packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export const KZG_COMMITMENT_SUBTREE_INDEX0 = KZG_COMMITMENT_GINDEX0 - 2 ** KZG_C
export const BLOBSIDECAR_FIXED_SIZE = ACTIVE_PRESET === PresetName.minimal ? 131672 : 131928;

// Electra Misc
export const UNSET_DEPOSIT_RECEIPTS_START_INDEX = 2n ** 64n - 1n;
export const UNSET_DEPOSIT_REQUESTS_START_INDEX = 2n ** 64n - 1n;
export const FULL_EXIT_REQUEST_AMOUNT = 0;
export const NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA = 87;
export const NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA = 6;
Expand Down
5 changes: 5 additions & 0 deletions packages/params/test/unit/__snapshots__/forkName.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ exports[`forkName > should have valid allForks 1`] = `
"bellatrix",
"capella",
"deneb",
"electra",
]
`;

exports[`forkName > should have valid blobs forks 1`] = `
[
"deneb",
"electra",
]
`;

Expand All @@ -21,6 +23,7 @@ exports[`forkName > should have valid execution forks 1`] = `
"bellatrix",
"capella",
"deneb",
"electra",
]
`;

Expand All @@ -30,12 +33,14 @@ exports[`forkName > should have valid lightclient forks 1`] = `
"bellatrix",
"capella",
"deneb",
"electra",
]
`;

exports[`forkName > should have valid withdrawal forks 1`] = `
[
"capella",
"deneb",
"electra",
]
`;
2 changes: 1 addition & 1 deletion packages/state-transition/src/block/processDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function isValidDepositSignature(
const publicKey = PublicKey.fromBytes(pubkey, true);
const signature = Signature.fromBytes(depositSignature, true);

return verify(signingRoot, publicKey, signature)
return verify(signingRoot, publicKey, signature);
} catch (e) {
return false; // Catch all BLS errors: failed key validation, failed signature validation, invalid signature
}
Expand Down
4 changes: 2 additions & 2 deletions packages/state-transition/src/block/processDepositRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {electra} from "@lodestar/types";
import {ForkSeq, UNSET_DEPOSIT_RECEIPTS_START_INDEX} from "@lodestar/params";
import {ForkSeq, UNSET_DEPOSIT_REQUESTS_START_INDEX} from "@lodestar/params";

import {CachedBeaconStateElectra} from "../types.js";
import {applyDeposit} from "./processDeposit.js";
Expand All @@ -9,7 +9,7 @@ export function processDepositRequest(
state: CachedBeaconStateElectra,
depositRequest: electra.DepositRequest
): void {
if (state.depositRequestsStartIndex === UNSET_DEPOSIT_RECEIPTS_START_INDEX) {
if (state.depositRequestsStartIndex === UNSET_DEPOSIT_REQUESTS_START_INDEX) {
state.depositRequestsStartIndex = BigInt(depositRequest.index);
}

Expand Down
10 changes: 5 additions & 5 deletions packages/state-transition/src/slot/upgradeStateToElectra.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Epoch, ValidatorIndex, ssz} from "@lodestar/types";
import {FAR_FUTURE_EPOCH, UNSET_DEPOSIT_RECEIPTS_START_INDEX} from "@lodestar/params";
import {FAR_FUTURE_EPOCH, UNSET_DEPOSIT_REQUESTS_START_INDEX} from "@lodestar/params";
import {CachedBeaconStateDeneb} from "../types.js";
import {CachedBeaconStateElectra, getCachedBeaconState} from "../cache/stateCache.js";
import {
Expand Down Expand Up @@ -59,8 +59,8 @@ export function upgradeStateToElectra(stateDeneb: CachedBeaconStateDeneb): Cache
stateElectraView.historicalSummaries = stateElectraCloned.historicalSummaries;

// latestExecutionPayloadHeader's depositRequestsRoot and withdrawalRequestsRoot set to zeros by default
// default value of depositRequestsStartIndex is UNSET_DEPOSIT_RECEIPTS_START_INDEX
stateElectraView.depositRequestsStartIndex = UNSET_DEPOSIT_RECEIPTS_START_INDEX;
// default value of depositRequestsStartIndex is UNSET_DEPOSIT_REQUESTS_START_INDEX
stateElectraView.depositRequestsStartIndex = UNSET_DEPOSIT_REQUESTS_START_INDEX;
stateElectraView.depositBalanceToConsume = BigInt(0);
stateElectraView.exitBalanceToConsume = BigInt(0);

Expand Down Expand Up @@ -138,8 +138,8 @@ export function upgradeStateToElectraOriginal(stateDeneb: CachedBeaconStateDeneb
});

// latestExecutionPayloadHeader's depositRequestsRoot and withdrawalRequestsRoot set to zeros by default
// default value of depositRequestsStartIndex is UNSET_DEPOSIT_RECEIPTS_START_INDEX
stateElectra.depositRequestsStartIndex = UNSET_DEPOSIT_RECEIPTS_START_INDEX;
// default value of depositRequestsStartIndex is UNSET_DEPOSIT_REQUESTS_START_INDEX
stateElectra.depositRequestsStartIndex = UNSET_DEPOSIT_REQUESTS_START_INDEX;

const validatorsArr = stateElectra.validators.getAllReadonly();

Expand Down
Loading

0 comments on commit bb9d811

Please sign in to comment.