From 90e78d5d47f615d71cb5cc961bb311bd4902326f Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Thu, 8 Aug 2024 13:36:14 +0700 Subject: [PATCH 1/2] fix: improve sync pubkeys --- packages/state-transition/src/cache/epochCache.ts | 12 ++++++------ packages/state-transition/src/cache/pubkeyCache.ts | 14 ++++++-------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/state-transition/src/cache/epochCache.ts b/packages/state-transition/src/cache/epochCache.ts index 8f15dab90535..191aa7f3985c 100644 --- a/packages/state-transition/src/cache/epochCache.ts +++ b/packages/state-transition/src/cache/epochCache.ts @@ -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; @@ -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[] = []; diff --git a/packages/state-transition/src/cache/pubkeyCache.ts b/packages/state-transition/src/cache/pubkeyCache.ts index 0fd7a80fe990..bd9be85aa9e8 100644 --- a/packages/state-transition/src/cache/pubkeyCache.ts +++ b/packages/state-transition/src/cache/pubkeyCache.ts @@ -1,5 +1,5 @@ import {PublicKey} from "@chainsafe/blst"; -import {ValidatorIndex} from "@lodestar/types"; +import {ValidatorIndex, phase0} from "@lodestar/types"; import {BeaconStateAllForks} from "./types.js"; export type Index2PubkeyCache = PublicKey[]; @@ -53,7 +53,7 @@ export class PubkeyIndexMap { * If pubkey caches are empty: SLOW CODE - 🐢 */ export function syncPubkeys( - state: BeaconStateAllForks, + validators: phase0.Validator[], pubkey2index: PubkeyIndexMap, index2pubkey: Index2PubkeyCache ): void { @@ -61,16 +61,14 @@ export function syncPubkeys( 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 } } From 38cd9e9a17e25f8e57ef4cc11728819cb461fb0b Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Thu, 8 Aug 2024 14:09:43 +0700 Subject: [PATCH 2/2] fix: lint --- packages/state-transition/src/cache/pubkeyCache.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/state-transition/src/cache/pubkeyCache.ts b/packages/state-transition/src/cache/pubkeyCache.ts index bd9be85aa9e8..b2b45ca09d25 100644 --- a/packages/state-transition/src/cache/pubkeyCache.ts +++ b/packages/state-transition/src/cache/pubkeyCache.ts @@ -1,6 +1,5 @@ import {PublicKey} from "@chainsafe/blst"; import {ValidatorIndex, phase0} from "@lodestar/types"; -import {BeaconStateAllForks} from "./types.js"; export type Index2PubkeyCache = PublicKey[];