@lodestar/beacon-node
Version:
A Typescript implementation of the beacon chain
33 lines • 1.58 kB
JavaScript
import { computeStartSlotAtEpoch } from "@lodestar/state-transition";
import { toRootHex } from "@lodestar/utils";
/** The number of validator balance sets that are cached within `CheckpointBalancesCache`. */
const MAX_BALANCE_CACHE_SIZE = 4;
/**
* Cache EffectiveBalanceIncrements of checkpoint blocks
*/
export class CheckpointBalancesCache {
items = [];
/**
* 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, state) {
const epoch = state.epoch;
const epochBoundarySlot = computeStartSlotAtEpoch(epoch);
const epochBoundaryRoot = epochBoundarySlot === state.slot ? blockRootHex : toRootHex(state.getBlockRootAtSlot(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: state.getEffectiveBalanceIncrementsZeroInactive() });
}
}
get(checkpoint) {
const { rootHex, epoch } = checkpoint;
return this.items.find((item) => item.epoch === epoch && item.rootHex === rootHex)?.balances;
}
}
//# sourceMappingURL=balancesCache.js.map