Skip to content

Commit

Permalink
addFinalizedPubkey on finalized checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ensi321 committed Oct 26, 2023
1 parent fb97206 commit 4c60789
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
13 changes: 13 additions & 0 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,19 @@ export class BeaconChain implements IBeaconChain {
if (headState) {
this.opPool.pruneAll(headState, finalizedState);
}

// Populate finalized pubkey cache
if (finalizedState?.epochCtx.isAfterEIP6110()) {
const pivotValidatorIndex = finalizedState.validators.length;
// TODO 6110: If we are not considering EIP-6914 see if there is any
// data structure like OrderedMap in immutabe-js so we can do slicing instead of filter
const newFinalizedValidators = finalizedState.epochCtx.unfinalizedPubkey2index.filter((index, _pubkey) => index < pivotValidatorIndex);

newFinalizedValidators.forEach((index, pubkey) => {
finalizedState.epochCtx.addFinalizedPubkey(index, pubkey);
})

}
}

async updateBeaconProposerData(epoch: Epoch, proposers: ProposerPreparationData[]): Promise<void> {
Expand Down
17 changes: 5 additions & 12 deletions packages/state-transition/src/cache/epochCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,14 +573,6 @@ export class EpochCache {
this.epoch = computeEpochAtSlot(state.slot);
this.syncPeriod = computeSyncPeriodAtEpoch(this.epoch);

// To populate finalized cache and prune unfinalized cache with validators that are just initialized
if (this.isAfterEIP6110()) {
const validators = state.validators;
for (const index of epochTransitionCache.indicesEligibleForActivationQueue) {
const validator = validators.getReadonly(index);
this.addFinalizedPubkey(validator.pubkey, index);
}
}
}

beforeEpochTransition(): void {
Expand Down Expand Up @@ -803,9 +795,10 @@ export class EpochCache {
/**
*
* Given a validator whose activationEligibilityEpoch has just been set, we move its pubkey from unfinalized cache to finalized cache
* Since addFinalizedPubkey() primarily takes pubkeys from unfinalized cache, it can take pubkey hex string directly
*
*/
addFinalizedPubkey(pubkey: Uint8Array, index: ValidatorIndex): void {
addFinalizedPubkey(index: ValidatorIndex, pubkey: PubkeyHex): void {
if (!this.isAfterEIP6110()) {
throw new Error("addFInalizedPubkey is not available pre EIP-6110");
}
Expand All @@ -816,7 +809,7 @@ export class EpochCache {
if (existingIndex === index) {
// Repeated insert. Should not happen except during the first few epochs of 6110 activation
// Unfinalized validator added to finalizedPubkey2index pre-6110 by calling addPubkey()
// when it become finalized in post-6110, addFinalizedPubkey() is called to cause repeated insert
// when it becomes finalized in post-6110, addFinalizedPubkey() is called to cause repeated insert
return;
} else {
// attempt to insert the same pubkey with different index, should never happen.
Expand All @@ -825,9 +818,9 @@ export class EpochCache {
}

this.pubkey2index.set(pubkey, index);
this.index2pubkey[index] = bls.PublicKey.fromBytes(pubkey, CoordType.jacobian);
this.index2pubkey[index] = bls.PublicKey.fromHex(pubkey);

this.unfinalizedPubkey2index = this.unfinalizedPubkey2index.delete(toMemoryEfficientHexStr(pubkey));
this.unfinalizedPubkey2index = this.unfinalizedPubkey2index.delete(pubkey);
}

getShufflingAtSlot(slot: Slot): EpochShuffling {
Expand Down
2 changes: 1 addition & 1 deletion packages/state-transition/src/cache/pubkeyCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class PubkeyIndexMap {
return this.map.get(toMemoryEfficientHexStr(key));
}

set(key: Uint8Array, value: ValidatorIndex): void {
set(key: Uint8Array | PubkeyHex, value: ValidatorIndex): void {
this.map.set(toMemoryEfficientHexStr(key), value);
}
}
Expand Down

0 comments on commit 4c60789

Please sign in to comment.