Skip to content

Commit

Permalink
fix: balancesTreeCache to store one balances tree per epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Oct 17, 2024
1 parent cc5cfe1 commit 550c700
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/beacon-node/src/chain/balancesTreeCache.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import {ListBasicTreeViewDU, UintNumberType} from "@chainsafe/ssz";
import {IBalancesTreeCache, CachedBeaconStateAllForks} from "@lodestar/state-transition";
import {Metrics} from "../metrics/index.js";
import {Epoch} from "@lodestar/types";

const MAX_ITEMS = 2;

/**
* A cached of unused balances tree
* States in the same epoch share the same balances tree so we only want to cache max once per epoch
*/
export class BalancesTreeCache implements IBalancesTreeCache {
private readonly unusedBalancesTrees: ListBasicTreeViewDU<UintNumberType>[] = [];
private readonly unusedBalancesTrees: Map<Epoch, ListBasicTreeViewDU<UintNumberType>> = new Map();

constructor(private readonly metrics: Metrics | null = null) {
if (metrics) {
metrics.balancesTreeCache.size.addCollect(() => {
metrics.balancesTreeCache.size.set(this.unusedBalancesTrees.length);
metrics.balancesTreeCache.size.set(this.unusedBalancesTrees.size);
});
}
}
Expand All @@ -19,20 +24,28 @@ export class BalancesTreeCache implements IBalancesTreeCache {
if (state === undefined) {
return;
}
const stateEpoch = state.epochCtx.epoch;
if (this.unusedBalancesTrees.has(stateEpoch)) {
return;
}

this.unusedBalancesTrees.push(state.balances);
while (this.unusedBalancesTrees.length > MAX_ITEMS) {
this.unusedBalancesTrees.shift();
this.unusedBalancesTrees.set(stateEpoch, state.balances);
while (this.unusedBalancesTrees.size > MAX_ITEMS) {
const firstEpoch = Array.from(this.unusedBalancesTrees.keys())[0];
this.unusedBalancesTrees.delete(firstEpoch);
}
}

getUnusedBalances(): ListBasicTreeViewDU<UintNumberType> | undefined {
if (this.unusedBalancesTrees.length === 0) {
if (this.unusedBalancesTrees.size === 0) {
this.metrics?.balancesTreeCache.miss.inc();
return undefined;
}

this.metrics?.balancesTreeCache.hit.inc();
return this.unusedBalancesTrees.shift();
const firstEpoch = Array.from(this.unusedBalancesTrees.keys())[0];
const unusedBalances = this.unusedBalancesTrees.get(firstEpoch);
this.unusedBalancesTrees.delete(firstEpoch);
return unusedBalances;
}
}

0 comments on commit 550c700

Please sign in to comment.