Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve sync pubkeys #7010

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/state-transition/src/cache/epochCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,6 @@ export class EpochCache {
{config, pubkey2index, index2pubkey}: EpochCacheImmutableData,
opts?: EpochCacheOpts
): EpochCache {
// syncPubkeys here to ensure EpochCacheImmutableData is popualted before computing the rest of caches
// - computeSyncCommitteeCache() needs a fully populated pubkey2index cache
if (!opts?.skipSyncPubkeys) {
syncPubkeys(state, pubkey2index, index2pubkey);
}

const currentEpoch = computeEpochAtSlot(state.slot);
const isGenesis = currentEpoch === GENESIS_EPOCH;
const previousEpoch = isGenesis ? GENESIS_EPOCH : currentEpoch - 1;
Expand All @@ -282,6 +276,12 @@ export class EpochCache {
const validators = state.validators.getAllReadonlyValues();
const validatorCount = validators.length;

// syncPubkeys here to ensure EpochCacheImmutableData is popualted before computing the rest of caches
// - computeSyncCommitteeCache() needs a fully populated pubkey2index cache
if (!opts?.skipSyncPubkeys) {
syncPubkeys(validators, pubkey2index, index2pubkey);
}

const effectiveBalanceIncrements = getEffectiveBalanceIncrementsWithLen(validatorCount);
const totalSlashingsByIncrement = getTotalSlashingsByIncrement(state);
const previousActiveIndices: ValidatorIndex[] = [];
Expand Down
15 changes: 6 additions & 9 deletions packages/state-transition/src/cache/pubkeyCache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {PublicKey} from "@chainsafe/blst";
import {ValidatorIndex} from "@lodestar/types";
import {BeaconStateAllForks} from "./types.js";
import {ValidatorIndex, phase0} from "@lodestar/types";

export type Index2PubkeyCache = PublicKey[];

Expand Down Expand Up @@ -53,24 +52,22 @@ export class PubkeyIndexMap {
* If pubkey caches are empty: SLOW CODE - 🐢
*/
export function syncPubkeys(
state: BeaconStateAllForks,
validators: phase0.Validator[],
pubkey2index: PubkeyIndexMap,
index2pubkey: Index2PubkeyCache
): void {
if (pubkey2index.size !== index2pubkey.length) {
throw new Error(`Pubkey indices have fallen out of sync: ${pubkey2index.size} != ${index2pubkey.length}`);
}

// Get the validators sub tree once for all the loop
const validators = state.validators;

const newCount = state.validators.length;
const newCount = validators.length;
index2pubkey.length = newCount;
for (let i = pubkey2index.size; i < newCount; i++) {
const pubkey = validators.getReadonly(i).pubkey;
const pubkey = validators[i].pubkey;
pubkey2index.set(pubkey, i);
// Pubkeys must be checked for group + inf. This must be done only once when the validator deposit is processed.
// Afterwards any public key is the state consider validated.
// > Do not do any validation here
index2pubkey.push(PublicKey.fromBytes(pubkey)); // Optimize for aggregation
index2pubkey[i] = PublicKey.fromBytes(pubkey); // Optimize for aggregation
}
}
Loading