-
-
Notifications
You must be signed in to change notification settings - Fork 266
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
16 changed files
with
488 additions
and
332 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
CachedBeaconStateAllForks, | ||
computeStartSlotAtEpoch, | ||
EffectiveBalanceIncrements, | ||
getBlockRootAtSlot, | ||
getEffectiveBalanceIncrementsZeroInactive, | ||
} from "@lodestar/state-transition"; | ||
import {CheckpointWithHex} from "@lodestar/fork-choice"; | ||
import {Epoch, RootHex} from "@lodestar/types"; | ||
import {toHexString} from "@lodestar/utils"; | ||
|
||
/** The number of validator balance sets that are cached within `CheckpointBalancesCache`. */ | ||
const MAX_BALANCE_CACHE_SIZE = 4; | ||
|
||
type BalancesCacheItem = { | ||
rootHex: RootHex; | ||
epoch: Epoch; | ||
balances: EffectiveBalanceIncrements; | ||
}; | ||
|
||
/** | ||
* Cache EffectiveBalanceIncrements of checkpoint blocks | ||
*/ | ||
export class CheckpointBalancesCache { | ||
private readonly items: BalancesCacheItem[] = []; | ||
|
||
/** | ||
* Inspect the given `state` and determine the root of the block at the first slot of | ||
* `state.current_epoch`. If there is not already some entry for the given block root, then | ||
* add the effective balances from the `state` to the cache. | ||
*/ | ||
processState(blockRootHex: RootHex, state: CachedBeaconStateAllForks): void { | ||
const epoch = state.epochCtx.currentShuffling.epoch; | ||
const epochBoundarySlot = computeStartSlotAtEpoch(epoch); | ||
const epochBoundaryRoot = | ||
epochBoundarySlot === state.slot ? blockRootHex : toHexString(getBlockRootAtSlot(state, epochBoundarySlot)); | ||
|
||
const index = this.items.findIndex((item) => item.epoch === epoch && item.rootHex == epochBoundaryRoot); | ||
if (index === -1) { | ||
if (this.items.length === MAX_BALANCE_CACHE_SIZE) { | ||
this.items.shift(); | ||
} | ||
// expect to reach this once per epoch | ||
this.items.push({epoch, rootHex: epochBoundaryRoot, balances: getEffectiveBalanceIncrementsZeroInactive(state)}); | ||
} | ||
} | ||
|
||
get(checkpoint: CheckpointWithHex): EffectiveBalanceIncrements | undefined { | ||
const {rootHex, epoch} = checkpoint; | ||
return this.items.find((item) => item.epoch === epoch && item.rootHex === rootHex)?.balances; | ||
} | ||
} |
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
Oops, something went wrong.