Skip to content

Commit

Permalink
Add consolidation tests for comp creds
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalinin committed Oct 21, 2024
1 parent aa5ebb3 commit 44c1c31
Showing 1 changed file with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from eth2spec.test.helpers.state import (
next_epoch_with_full_participation,
)
from eth2spec.test.helpers.withdrawals import (
set_eth1_withdrawal_credential_with_balance,
set_compounding_withdrawal_credential_with_balance,
)

# ***********************
# * CONSOLIDATION TESTS *
Expand Down Expand Up @@ -316,11 +320,9 @@ def test_pending_consolidation_source_balance_less_than_max_effective(spec, stat
)
# Set withdrawable epoch to current epoch to allow processing
state.validators[source_index].withdrawable_epoch = current_epoch
# Set the target withdrawal credential to eth1
eth1_withdrawal_credential = (
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b"\x00" * 11 + b"\x11" * 20
)
state.validators[target_index].withdrawal_credentials = eth1_withdrawal_credential
# Set source and target withdrawal credential to eth1
set_eth1_withdrawal_credential_with_balance(spec, state, source_index)
set_eth1_withdrawal_credential_with_balance(spec, state, target_index)
# Set the source balance to be less than effective_balance
pre_balance_source = state.validators[source_index].effective_balance - spec.EFFECTIVE_BALANCE_INCREMENT // 8
state.balances[source_index] = pre_balance_source
Expand Down Expand Up @@ -349,11 +351,73 @@ def test_pending_consolidation_source_balance_greater_than_max_effective(spec, s
)
# Set withdrawable epoch to current epoch to allow processing
state.validators[source_index].withdrawable_epoch = current_epoch
# Set the target withdrawal credential to eth1
eth1_withdrawal_credential = (
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b"\x00" * 11 + b"\x11" * 20
# Set source and target withdrawal credential to eth1
set_eth1_withdrawal_credential_with_balance(spec, state, source_index)
set_eth1_withdrawal_credential_with_balance(spec, state, target_index)
# Set the source balance to be greater than effective_balance
excess_source_balance = spec.EFFECTIVE_BALANCE_INCREMENT // 8
pre_balance_source = state.validators[source_index].effective_balance + excess_source_balance
state.balances[source_index] = pre_balance_source

pre_balance_target = state.balances[target_index]

source_max_effective_balance = spec.get_max_effective_balance(state.validators[source_index])
assert state.balances[source_index] > source_max_effective_balance

yield from run_epoch_processing_with(spec, state, "process_pending_consolidations")

# Pending consolidation was successfully processed
assert state.balances[target_index] == pre_balance_target + source_max_effective_balance
assert state.balances[source_index] == excess_source_balance
assert state.pending_consolidations == []


@with_electra_and_later
@spec_state_test
def test_pending_consolidation_source_balance_less_than_max_effective_compounding(spec, state):
current_epoch = spec.get_current_epoch(state)
source_index = spec.get_active_validator_indices(state, current_epoch)[0]
target_index = spec.get_active_validator_indices(state, current_epoch)[1]
# append pending consolidation
state.pending_consolidations.append(
spec.PendingConsolidation(source_index=source_index, target_index=target_index)
)
state.validators[target_index].withdrawal_credentials = eth1_withdrawal_credential
# Set withdrawable epoch to current epoch to allow processing
state.validators[source_index].withdrawable_epoch = current_epoch
# Set source and target withdrawal credential to compounding
set_compounding_withdrawal_credential_with_balance(spec, state, source_index)
set_compounding_withdrawal_credential_with_balance(spec, state, target_index)
# Set the source balance to be less than effective_balance
pre_balance_source = state.validators[source_index].effective_balance - spec.EFFECTIVE_BALANCE_INCREMENT // 8
state.balances[source_index] = pre_balance_source

pre_balance_target = state.balances[target_index]

assert state.balances[source_index] < spec.get_max_effective_balance(state.validators[source_index])

yield from run_epoch_processing_with(spec, state, "process_pending_consolidations")

# Pending consolidation was successfully processed
assert state.balances[target_index] == pre_balance_target + pre_balance_source
assert state.balances[source_index] == 0
assert state.pending_consolidations == []


@with_electra_and_later
@spec_state_test
def test_pending_consolidation_source_balance_greater_than_max_effective_compounding(spec, state):
current_epoch = spec.get_current_epoch(state)
source_index = spec.get_active_validator_indices(state, current_epoch)[0]
target_index = spec.get_active_validator_indices(state, current_epoch)[1]
# append pending consolidation
state.pending_consolidations.append(
spec.PendingConsolidation(source_index=source_index, target_index=target_index)
)
# Set withdrawable epoch to current epoch to allow processing
state.validators[source_index].withdrawable_epoch = current_epoch
# Set source and target withdrawal credential to compounding
set_compounding_withdrawal_credential_with_balance(spec, state, source_index)
set_compounding_withdrawal_credential_with_balance(spec, state, target_index)
# Set the source balance to be greater than effective_balance
excess_source_balance = spec.EFFECTIVE_BALANCE_INCREMENT // 8
pre_balance_source = state.validators[source_index].effective_balance + excess_source_balance
Expand Down

0 comments on commit 44c1c31

Please sign in to comment.