Skip to content

Commit

Permalink
refactor: rename EpochContext and EpochProcess (#5558)
Browse files Browse the repository at this point in the history
Rename EpochContext and EpochProcess
  • Loading branch information
dapplion authored May 30, 2023
1 parent bf52a7f commit a27322e
Show file tree
Hide file tree
Showing 46 changed files with 311 additions and 296 deletions.
4 changes: 2 additions & 2 deletions packages/beacon-node/src/api/impl/beacon/state/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
CachedBeaconStateAllForks,
BeaconStateAllForks,
createCachedBeaconState,
createEmptyEpochContextImmutableData,
createEmptyEpochCacheImmutableData,
PubkeyIndexMap,
ExecutionPayloadStatus,
DataAvailableStatus,
Expand Down Expand Up @@ -237,7 +237,7 @@ async function getNearestArchivedState(
const state = (await states[Symbol.asyncIterator]().next()).value as BeaconStateAllForks;
// TODO - PENDING: Don't create new immutable caches here
// see https://github.com/ChainSafe/lodestar/issues/3683
return createCachedBeaconState(state, createEmptyEpochContextImmutableData(config, state));
return createCachedBeaconState(state, createEmptyEpochCacheImmutableData(config, state));
}

async function getFinalizedState(
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/src/chain/genesis/genesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
CachedBeaconStateAllForks,
createCachedBeaconState,
BeaconStateAllForks,
createEmptyEpochContextImmutableData,
createEmptyEpochCacheImmutableData,
getActiveValidatorIndices,
} from "@lodestar/state-transition";
import {Logger} from "@lodestar/utils";
Expand Down Expand Up @@ -85,8 +85,8 @@ export class GenesisBuilder implements IGenesisBuilder {
this.fromBlock = this.eth1Provider.deployBlock;
}

// TODO - PENDING: Ensure EpochContextImmutableData is created only once
this.state = createCachedBeaconState(stateView, createEmptyEpochContextImmutableData(config, stateView));
// TODO - PENDING: Ensure EpochCacheImmutableData is created only once
this.state = createCachedBeaconState(stateView, createEmptyEpochCacheImmutableData(config, stateView));
this.config = this.state.config;
this.activatedValidatorCount = getActiveValidatorIndices(stateView, GENESIS_EPOCH).length;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/chain/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type BlockProcessOpts = {
*/
safeSlotsToImportOptimistically: number;
/**
* Assert progressive balances the same to EpochProcess
* Assert progressive balances the same to EpochTransitionCache
*/
assertCorrectProgressiveBalances?: boolean;
/** Used for fork_choice spec tests */
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/node/utils/interop/state.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Bytes32, phase0, ssz, TimeSeconds} from "@lodestar/types";
import {ChainForkConfig} from "@lodestar/config";
import {BeaconStateAllForks, initializeBeaconStateFromEth1} from "@lodestar/state-transition";
import {createEmptyEpochContextImmutableData} from "@lodestar/state-transition";
import {createEmptyEpochCacheImmutableData} from "@lodestar/state-transition";
import {ForkName, GENESIS_SLOT} from "@lodestar/params";

import {DepositTree} from "../../../db/repositories/depositDataRoot.js";
Expand Down Expand Up @@ -45,7 +45,7 @@ export function getInteropState(
latestPayloadHeader.baseFeePerGas = GENESIS_BASE_FEE_PER_GAS;
const state = initializeBeaconStateFromEth1(
config,
createEmptyEpochContextImmutableData(config, {genesisValidatorsRoot: Buffer.alloc(32, 0)}),
createEmptyEpochCacheImmutableData(config, {genesisValidatorsRoot: Buffer.alloc(32, 0)}),
eth1BlockHash,
eth1Timestamp,
deposits,
Expand Down
34 changes: 17 additions & 17 deletions packages/beacon-node/test/spec/presets/epoch_processing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect} from "chai";
import {
CachedBeaconStateAllForks,
EpochProcess,
EpochTransitionCache,
BeaconStateAllForks,
beforeProcessEpoch,
} from "@lodestar/state-transition";
Expand All @@ -13,63 +13,63 @@ import {getConfig} from "../../utils/config.js";
import {TestRunnerFn} from "../utils/types.js";
import {assertCorrectProgressiveBalances} from "../config.js";

export type EpochProcessFn = (state: CachedBeaconStateAllForks, epochProcess: EpochProcess) => void;
export type EpochTransitionFn = (state: CachedBeaconStateAllForks, epochTransitionCache: EpochTransitionCache) => void;

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

const epochProcessFns: Record<string, EpochProcessFn> = {
const epochTransitionFns: Record<string, EpochTransitionFn> = {
effective_balance_updates: epochFns.processEffectiveBalanceUpdates,
eth1_data_reset: epochFns.processEth1DataReset,
historical_roots_update: epochFns.processHistoricalRootsUpdate,
inactivity_updates: epochFns.processInactivityUpdates as EpochProcessFn,
inactivity_updates: epochFns.processInactivityUpdates as EpochTransitionFn,
justification_and_finalization: epochFns.processJustificationAndFinalization,
participation_flag_updates: epochFns.processParticipationFlagUpdates as EpochProcessFn,
participation_record_updates: epochFns.processParticipationRecordUpdates as EpochProcessFn,
participation_flag_updates: epochFns.processParticipationFlagUpdates as EpochTransitionFn,
participation_record_updates: epochFns.processParticipationRecordUpdates as EpochTransitionFn,
randao_mixes_reset: epochFns.processRandaoMixesReset,
registry_updates: epochFns.processRegistryUpdates,
rewards_and_penalties: epochFns.processRewardsAndPenalties,
slashings: epochFns.processSlashings,
slashings_reset: epochFns.processSlashingsReset,
sync_committee_updates: epochFns.processSyncCommitteeUpdates as EpochProcessFn,
historical_summaries_update: epochFns.processHistoricalSummariesUpdate as EpochProcessFn,
sync_committee_updates: epochFns.processSyncCommitteeUpdates as EpochTransitionFn,
historical_summaries_update: epochFns.processHistoricalSummariesUpdate as EpochTransitionFn,
};

/**
* https://github.com/ethereum/consensus-specs/blob/dev/tests/formats/epoch_processing/README.md
*/
type EpochProcessingTestCase = {
type EpochTransitionCacheingTestCase = {
meta?: {bls_setting?: bigint};
pre: BeaconStateAllForks;
post: BeaconStateAllForks;
};

/**
* @param fork
* @param epochProcessFns Describe with which function to run each directory of tests
* @param epochTransitionFns Describe with which function to run each directory of tests
*/
export const epochProcessing =
(skipTestNames?: string[]): TestRunnerFn<EpochProcessingTestCase, BeaconStateAllForks> =>
(skipTestNames?: string[]): TestRunnerFn<EpochTransitionCacheingTestCase, BeaconStateAllForks> =>
(fork, testName) => {
const config = getConfig(fork);

const epochProcessFn = epochProcessFns[testName];
if (epochProcessFn === undefined) {
throw Error(`No epochProcessFn for ${testName}`);
const epochTransitionFn = epochTransitionFns[testName];
if (epochTransitionFn === undefined) {
throw Error(`No epochTransitionFn for ${testName}`);
}

return {
testFunction: (testcase) => {
const stateTB = testcase.pre.clone();
const state = createCachedBeaconStateTest(stateTB, config);

const epochProcess = beforeProcessEpoch(state, {assertCorrectProgressiveBalances});
const epochTransitionCache = beforeProcessEpoch(state, {assertCorrectProgressiveBalances});

if (testcase.post === undefined) {
// If post.ssz_snappy is not value, the sub-transition processing is aborted
// https://github.com/ethereum/consensus-specs/blob/dev/tests/formats/epoch_processing/README.md#postssz_snappy
expect(() => epochProcessFn(state, epochProcess)).to.throw();
expect(() => epochTransitionFn(state, epochTransitionCache)).to.throw();
} else {
epochProcessFn(state, epochProcess);
epochTransitionFn(state, epochTransitionCache);
}

state.commit();
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/test/spec/presets/genesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {phase0, Root, ssz, TimeSeconds, allForks, deneb} from "@lodestar/types";
import {InputType} from "@lodestar/spec-test-util";
import {
BeaconStateAllForks,
createEmptyEpochContextImmutableData,
createEmptyEpochCacheImmutableData,
initializeBeaconStateFromEth1,
isValidGenesisState,
} from "@lodestar/state-transition";
Expand Down Expand Up @@ -36,7 +36,7 @@ const genesisInitialization: TestRunnerFn<GenesisInitSpecTest, BeaconStateAllFor
}

const config = getConfig(fork);
const immutableData = createEmptyEpochContextImmutableData(config, {
const immutableData = createEmptyEpochCacheImmutableData(config, {
// TODO: Should the genesisValidatorsRoot be random here?
genesisValidatorsRoot: Buffer.alloc(32, 0),
});
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/test/spec/presets/rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const rewards: TestRunnerFn<RewardTestCase, Deltas> = (fork) => {
testFunction: (testcase) => {
const config = getConfig(fork);
const wrappedState = createCachedBeaconStateTest(testcase.pre, config);
const epochProcess = beforeProcessEpoch(wrappedState, {assertCorrectProgressiveBalances});
const epochTransitionCache = beforeProcessEpoch(wrappedState, {assertCorrectProgressiveBalances});

// To debug this test and get granular results you can tweak inputs to get more granular results
//
Expand All @@ -32,7 +32,7 @@ export const rewards: TestRunnerFn<RewardTestCase, Deltas> = (fork) => {
// + set all inactivityScores to zero
// - To get inactivity_penalty_deltas set TIMELY_HEAD_FLAG_INDEX | TIMELY_SOURCE_FLAG_INDEX to false
// + set PARTICIPATION_FLAG_WEIGHTS[TIMELY_TARGET_FLAG_INDEX] to zero
return getRewardsAndPenalties(wrappedState, epochProcess);
return getRewardsAndPenalties(wrappedState, epochTransitionCache);
},
options: {
inputTypes: inputTypeSszTreeViewDU,
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/test/utils/cachedBeaconState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import {
BeaconStateAllForks,
BeaconStateCache,
createCachedBeaconState,
createEmptyEpochContextImmutableData,
createEmptyEpochCacheImmutableData,
} from "@lodestar/state-transition";
import {ChainForkConfig} from "@lodestar/config";

export function createCachedBeaconStateTest<T extends BeaconStateAllForks>(
state: T,
chainConfig: ChainForkConfig
): T & BeaconStateCache {
return createCachedBeaconState<T>(state, createEmptyEpochContextImmutableData(chainConfig, state));
return createCachedBeaconState<T>(state, createEmptyEpochCacheImmutableData(chainConfig, state));
}
7 changes: 4 additions & 3 deletions packages/beacon-node/test/utils/node/simTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ export function simTestInfoTracker(bn: BeaconNode, logger: Logger): () => void {
function logParticipation(state: CachedBeaconStateAllForks): void {
// Compute participation (takes 5ms with 64 validators)
// Need a CachedBeaconStateAllForks where (state.slot + 1) % SLOTS_EPOCH == 0
const epochProcess = beforeProcessEpoch(state);
const epochTransitionCache = beforeProcessEpoch(state);
const epoch = computeEpochAtSlot(state.slot);

const prevParticipation =
epochProcess.prevEpochUnslashedStake.targetStakeByIncrement / epochProcess.totalActiveStakeByIncrement;
epochTransitionCache.prevEpochUnslashedStake.targetStakeByIncrement /
epochTransitionCache.totalActiveStakeByIncrement;
const currParticipation =
epochProcess.currEpochUnslashedTargetStakeByIncrement / epochProcess.totalActiveStakeByIncrement;
epochTransitionCache.currEpochUnslashedTargetStakeByIncrement / epochTransitionCache.totalActiveStakeByIncrement;
prevParticipationPerEpoch.set(epoch - 1, prevParticipation);
currParticipationPerEpoch.set(epoch, currParticipation);
logger.info("> Participation", {
Expand Down
Loading

0 comments on commit a27322e

Please sign in to comment.