Skip to content

Commit

Permalink
chore: use blocks to separate timers
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Dec 12, 2023
1 parent d863360 commit 172da6a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 41 deletions.
77 changes: 46 additions & 31 deletions packages/state-transition/src/epoch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,52 @@ export function processEpoch(
throw new Error("Lodestar does not support this network, parameters don't fit number value inside state.slashings");
}

const justificationAndFinalizationTimer = metrics?.epochTransitionStepTime.startTimer({
step: "processJustificationAndFinalization",
});
processJustificationAndFinalization(state, cache);
justificationAndFinalizationTimer?.();
{
const timer = metrics?.epochTransitionStepTime.startTimer({
step: "processJustificationAndFinalization",
});
processJustificationAndFinalization(state, cache);
timer?.();
}

if (fork >= ForkSeq.altair) {
const inactivityUpdatesTimer = metrics?.epochTransitionStepTime.startTimer({step: "processInactivityUpdates"});
const timer = metrics?.epochTransitionStepTime.startTimer({step: "processInactivityUpdates"});
processInactivityUpdates(state as CachedBeaconStateAltair, cache);
inactivityUpdatesTimer?.();
timer?.();
}

// processRewardsAndPenalties() is 2nd step in the specs, we optimize to do it
// after processSlashings() to update balances only once
// processRewardsAndPenalties(state, cache);
const registryUpdatesTimer = metrics?.epochTransitionStepTime.startTimer({step: "processRegistryUpdates"});
processRegistryUpdates(state, cache);
registryUpdatesTimer?.();
{
const timer = metrics?.epochTransitionStepTime.startTimer({step: "processRegistryUpdates"});
processRegistryUpdates(state, cache);
timer?.();
}

// accumulate slashing penalties and only update balances once in processRewardsAndPenalties()
const slashingsTimer = metrics?.epochTransitionStepTime.startTimer({step: "processSlashings"});
const slashingPenalties = processSlashings(state, cache, false);
slashingsTimer?.();
let slashingPenalties: number[];
{
const timer = metrics?.epochTransitionStepTime.startTimer({step: "processSlashings"});
slashingPenalties = processSlashings(state, cache, false);
timer?.();
}

const rewardsAndPenaltiesTimer = metrics?.epochTransitionStepTime.startTimer({step: "processRewardsAndPenalties"});
processRewardsAndPenalties(state, cache, slashingPenalties);
rewardsAndPenaltiesTimer?.();
{
const timer = metrics?.epochTransitionStepTime.startTimer({step: "processRewardsAndPenalties"});
processRewardsAndPenalties(state, cache, slashingPenalties);
timer?.();
}

processEth1DataReset(state, cache);

const effectiveBalanceUpdatesTimer = metrics?.epochTransitionStepTime.startTimer({
step: "processEffectiveBalanceUpdates",
});
processEffectiveBalanceUpdates(state, cache);
effectiveBalanceUpdatesTimer?.();
{
const timer = metrics?.epochTransitionStepTime.startTimer({
step: "processEffectiveBalanceUpdates",
});
processEffectiveBalanceUpdates(state, cache);
timer?.();
}

processSlashingsReset(state, cache);
processRandaoMixesReset(state, cache);
Expand All @@ -113,16 +124,20 @@ export function processEpoch(
if (fork === ForkSeq.phase0) {
processParticipationRecordUpdates(state as CachedBeaconStatePhase0);
} else {
const participationFlagUpdatesTimer = metrics?.epochTransitionStepTime.startTimer({
step: "processParticipationFlagUpdates",
});
processParticipationFlagUpdates(state as CachedBeaconStateAltair);
participationFlagUpdatesTimer?.();
{
const timer = metrics?.epochTransitionStepTime.startTimer({
step: "processParticipationFlagUpdates",
});
processParticipationFlagUpdates(state as CachedBeaconStateAltair);
timer?.();
}

const syncCommitteeUpdatesTimer = metrics?.epochTransitionStepTime.startTimer({
step: "processSyncCommitteeUpdates",
});
processSyncCommitteeUpdates(state as CachedBeaconStateAltair);
syncCommitteeUpdatesTimer?.();
{
const timer = metrics?.epochTransitionStepTime.startTimer({
step: "processSyncCommitteeUpdates",
});
processSyncCommitteeUpdates(state as CachedBeaconStateAltair);
timer?.();
}
}
}
31 changes: 21 additions & 10 deletions packages/state-transition/src/stateTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {toHexString} from "@chainsafe/ssz";
import {allForks, Slot, ssz} from "@lodestar/types";
import {SLOTS_PER_EPOCH} from "@lodestar/params";
import {BeaconStateTransitionMetrics, onPostStateMetrics, onStateCloneMetrics} from "./metrics.js";
import {beforeProcessEpoch, EpochTransitionCacheOpts} from "./cache/epochTransitionCache.js";
import {beforeProcessEpoch, EpochTransitionCache, EpochTransitionCacheOpts} from "./cache/epochTransitionCache.js";
import {
CachedBeaconStateAllForks,
CachedBeaconStatePhase0,
Expand Down Expand Up @@ -164,23 +164,34 @@ function processSlotsWithTransientCache(
const fork = postState.config.getForkSeq(postState.slot);

const epochTransitionTimer = metrics?.epochTransitionTime.startTimer();
const beforeProcessEpochTimer = metrics?.epochTransitionStepTime.startTimer({step: "beforeProcessEpoch"});
const epochTransitionCache = beforeProcessEpoch(postState, epochTransitionCacheOpts);
beforeProcessEpochTimer?.({step: "beforeProcessEpoch"});

let epochTransitionCache: EpochTransitionCache;
{
const timer = metrics?.epochTransitionStepTime.startTimer({step: "beforeProcessEpoch"});
epochTransitionCache = beforeProcessEpoch(postState, epochTransitionCacheOpts);
timer?.();
}

processEpoch(fork, postState, epochTransitionCache, metrics);

const {currentEpoch, statuses, balances} = epochTransitionCache;
metrics?.registerValidatorStatuses(currentEpoch, statuses, balances);

postState.slot++;
const afterProcessEpochTimer = metrics?.epochTransitionStepTime.startTimer({step: "afterProcessEpoch"});
postState.epochCtx.afterProcessEpoch(postState, epochTransitionCache);
afterProcessEpochTimer?.({step: "afterProcessEpoch"});

{
const timer = metrics?.epochTransitionStepTime.startTimer({step: "afterProcessEpoch"});
postState.epochCtx.afterProcessEpoch(postState, epochTransitionCache);
timer?.();
}

// Running commit here is not strictly necessary. The cost of running commit twice (here + after process block)
// Should be negligible but gives better metrics to differentiate the cost of it for block and epoch proc.
const epochTransitionCommitTimer = metrics?.epochTransitionCommitTime.startTimer();
postState.commit();
epochTransitionCommitTimer?.();
{
const timer = metrics?.epochTransitionCommitTime.startTimer();
postState.commit();
timer?.();
}

// Note: time only on success. Include beforeProcessEpoch, processEpoch, afterProcessEpoch, commit
epochTransitionTimer?.();
Expand Down

0 comments on commit 172da6a

Please sign in to comment.