Skip to content

Commit

Permalink
fix: improve sync pubkeys (#7010)
Browse files Browse the repository at this point in the history
* fix: improve sync pubkeys

* fix: lint
  • Loading branch information
twoeths authored and philknows committed Sep 3, 2024
1 parent ac50648 commit c0d73ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
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
}
}

0 comments on commit c0d73ae

Please sign in to comment.