Skip to content

Commit

Permalink
Increase logging to track sources of balance changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
etan-status committed Feb 28, 2024
1 parent 90b586e commit 92a735f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 22 additions & 4 deletions beacon_chain/spec/beaconstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@ from ./datatypes/capella import BeaconState, ExecutionPayloadHeader, Withdrawal

export extras, forks, validator, chronicles

var debugTrackedValidator* = ValidatorIndex.high

# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#increase_balance
func increase_balance*(balance: var Gwei, delta: Gwei) =
balance += delta

func increase_balance*(
state: var ForkyBeaconState, index: ValidatorIndex, delta: Gwei) =
func increase_balance(
state: var ForkyBeaconState, index: ValidatorIndex, delta: Gwei,
info: typeof(instantiationInfo())) =
## Increase the validator balance at index ``index`` by ``delta``.
if delta != 0: # avoid dirtying the balance cache if not needed
{.noSideEffect.}:
if index == debugTrackedValidator:
debugEcho "+", $delta, " ", $info
increase_balance(state.balances.mitem(index), delta)

template increase_balance*(
state: var ForkyBeaconState, index: ValidatorIndex, delta: Gwei) =
increase_balance(state, index, delta, instantiationInfo())

# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#decrease_balance
func decrease_balance*(balance: var Gwei, delta: Gwei) =
balance =
Expand All @@ -40,13 +50,21 @@ func decrease_balance*(balance: var Gwei, delta: Gwei) =
else:
balance - delta

func decrease_balance*(
state: var ForkyBeaconState, index: ValidatorIndex, delta: Gwei) =
func decrease_balance(
state: var ForkyBeaconState, index: ValidatorIndex, delta: Gwei,
info: typeof(instantiationInfo())) =
## Decrease the validator balance at index ``index`` by ``delta``, with
## underflow protection.
if delta != 0: # avoid dirtying the balance cache if not needed
{.noSideEffect.}:
if index == debugTrackedValidator:
debugEcho "-", $delta, " ", $info
decrease_balance(state.balances.mitem(index), delta)

template decrease_balance*(
state: var ForkyBeaconState, index: ValidatorIndex, delta: Gwei) =
decrease_balance(state, index, delta, instantiationInfo())

# https://github.com/ethereum/consensus-specs/blob/v1.4.0-alpha.3/specs/phase0/beacon-chain.md#deposits
# https://github.com/ethereum/consensus-specs/blob/v1.3.0/specs/altair/beacon-chain.md#modified-apply_deposit
func get_validator_from_deposit*(deposit: DepositData):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_block_rewards.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import
chronicles,
../../beacon_chain/spec/beaconstate,
../../beacon_chain/spec/forks,
../../beacon_chain/spec/state_transition,
../../beacon_chain/validators/rewards,
Expand Down Expand Up @@ -68,6 +69,8 @@ proc runTest(consensusFork: static ConsensusFork,
state_slot = stateSlot,
expected_balance = preStateBalance + blockValue

debugTrackedValidator = ValidatorIndex.high

block:
let res =
process_slots(defaultRuntimeConfig, fhPreState[],
Expand All @@ -80,6 +83,7 @@ proc runTest(consensusFork: static ConsensusFork,
let advanceBalance =
withState(fhPreState[]):
forkyState.data.balances.item(proposerIndex)
debugTrackedValidator = ValidatorIndex(proposerIndex)

block:
let res =
Expand Down

0 comments on commit 92a735f

Please sign in to comment.