Skip to content

Commit

Permalink
test: fix balance spec tests (#6777)
Browse files Browse the repository at this point in the history
* fix: remove epochCache.balances and invalid MAX_EFFECTIVE_BALANCE check

* fix: update rewardsAndPenalties balance updates

* docs: add comment to check epochTransitionCache
  • Loading branch information
matthewkeil authored and g11tech committed Aug 9, 2024
1 parent 33c6216 commit 3c87abb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ export function processEffectiveBalanceUpdates(

// epochTransitionCache.balances is set in processRewardsAndPenalties(), so it's recycled here for performance.
// It defaults to `state.balances.getAll()` to make Typescript happy and for spec tests
const balances = cache.balances ?? state.balances.getAll();
const balances = state.balances.getAll();

// TODO: (@matthewkeil) This was causing additional failures but should not. Check the EpochTransitionCache for why
// const balances = cache.balances ?? state.balances.getAll();

for (let i = 0, len = balances.length; i < len; i++) {
const balance = balances[i];
Expand All @@ -55,7 +58,7 @@ export function processEffectiveBalanceUpdates(
// Too big
effectiveBalance > balance + DOWNWARD_THRESHOLD ||
// Too small. Check effectiveBalance < MAX_EFFECTIVE_BALANCE to prevent unnecessary updates
(effectiveBalance < MAX_EFFECTIVE_BALANCE && effectiveBalance < balance - UPWARD_THRESHOLD)
effectiveBalance + UPWARD_THRESHOLD < balance
) {
// Update the state tree
// Should happen rarely, so it's fine to update the tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export function processRewardsAndPenalties(
const balances = state.balances.getAll();

for (let i = 0, len = rewards.length; i < len; i++) {
balances[i] += rewards[i] - penalties[i] - (slashingPenalties[i] ?? 0);
const result = balances[i] + rewards[i] - penalties[i] - (slashingPenalties[i] ?? 0)
balances[i] = Math.max(result, 0);
}

// important: do not change state one balance at a time. Set them all at once, constructing the tree in one go
Expand Down

0 comments on commit 3c87abb

Please sign in to comment.