-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
387 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {CachedBeaconStateAllForks, EpochShuffling, getShufflingDecisionBlock} from "@lodestar/state-transition"; | ||
import {Epoch, RootHex} from "@lodestar/types"; | ||
import {Metrics} from "../metrics/metrics.js"; | ||
|
||
/** | ||
* Same value to CheckpointBalancesCache, with the assumption that we don't have to use it for old epochs. In the worse case: | ||
* - when loading state bytes from disk, we need to compute shuffling for all epochs (~1s as of Sep 2023) | ||
* - don't have shuffling to verify attestations, need to do 1 epoch transition to add shuffling to this cache. This never happens | ||
* with default chain option of maxSkipSlots = 32 | ||
**/ | ||
const MAX_SHUFFLING_CACHE_SIZE = 4; | ||
|
||
type ShufflingCacheItem = { | ||
decisionBlockHex: RootHex; | ||
shuffling: EpochShuffling; | ||
}; | ||
|
||
/** | ||
* A shuffling cache to help: | ||
* - get committee quickly for attestation verification | ||
* - skip computing shuffling when loading state bytes from disk | ||
*/ | ||
export class ShufflingCache { | ||
private readonly items: ShufflingCacheItem[] = []; | ||
|
||
constructor(metrics: Metrics | null = null) { | ||
if (metrics) { | ||
metrics.shufflingCache.size.addCollect(() => metrics.shufflingCache.size.set(this.items.length)); | ||
} | ||
} | ||
|
||
processState(state: CachedBeaconStateAllForks, shufflingEpoch: Epoch): void { | ||
const decisionBlockHex = getShufflingDecisionBlock(state, shufflingEpoch); | ||
const index = this.items.findIndex( | ||
(item) => item.shuffling.epoch === shufflingEpoch && item.decisionBlockHex === decisionBlockHex | ||
); | ||
if (index === -1) { | ||
if (this.items.length === MAX_SHUFFLING_CACHE_SIZE) { | ||
this.items.shift(); | ||
} | ||
let shuffling: EpochShuffling; | ||
if (shufflingEpoch === state.epochCtx.nextShuffling.epoch) { | ||
shuffling = state.epochCtx.nextShuffling; | ||
} else if (shufflingEpoch === state.epochCtx.currentShuffling.epoch) { | ||
shuffling = state.epochCtx.currentShuffling; | ||
} else if (shufflingEpoch === state.epochCtx.previousShuffling.epoch) { | ||
shuffling = state.epochCtx.previousShuffling; | ||
} else { | ||
throw new Error(`Shuffling not found from state ${state.slot} for epoch ${shufflingEpoch}`); | ||
} | ||
this.items.push({decisionBlockHex, shuffling}); | ||
} | ||
} | ||
|
||
get(shufflingEpoch: Epoch, dependentRootHex: RootHex): EpochShuffling | null { | ||
return ( | ||
this.items.find((item) => item.shuffling.epoch === shufflingEpoch && item.decisionBlockHex === dependentRootHex) | ||
?.shuffling ?? null | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.