Skip to content

Commit baeb740

Browse files
matthewkeilg11tech
authored andcommitted
test: fix balance spec tests (ChainSafe#6777)
* fix: remove epochCache.balances and invalid MAX_EFFECTIVE_BALANCE check * fix: update rewardsAndPenalties balance updates * docs: add comment to check epochTransitionCache
1 parent d1b5769 commit baeb740

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

packages/state-transition/src/epoch/processEffectiveBalanceUpdates.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ export function processEffectiveBalanceUpdates(
4141

4242
// epochTransitionCache.balances is set in processRewardsAndPenalties(), so it's recycled here for performance.
4343
// It defaults to `state.balances.getAll()` to make Typescript happy and for spec tests
44-
const balances = cache.balances ?? state.balances.getAll();
44+
const balances = state.balances.getAll();
45+
46+
// TODO: (@matthewkeil) This was causing additional failures but should not. Check the EpochTransitionCache for why
47+
// const balances = cache.balances ?? state.balances.getAll();
4548

4649
for (let i = 0, len = balances.length; i < len; i++) {
4750
const balance = balances[i];
@@ -55,7 +58,7 @@ export function processEffectiveBalanceUpdates(
5558
// Too big
5659
effectiveBalance > balance + DOWNWARD_THRESHOLD ||
5760
// Too small. Check effectiveBalance < MAX_EFFECTIVE_BALANCE to prevent unnecessary updates
58-
(effectiveBalance < MAX_EFFECTIVE_BALANCE && effectiveBalance < balance - UPWARD_THRESHOLD)
61+
effectiveBalance + UPWARD_THRESHOLD < balance
5962
) {
6063
// Update the state tree
6164
// Should happen rarely, so it's fine to update the tree

packages/state-transition/src/epoch/processRewardsAndPenalties.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export function processRewardsAndPenalties(
2828
const balances = state.balances.getAll();
2929

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

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

0 commit comments

Comments
 (0)