Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,16 @@ def test_random_full_withdrawals_3(spec, state):
def test_success_no_max_effective_balance(spec, state):
validator_index = len(state.validators) // 2
# To be partially withdrawable, the validator's effective balance must be maxed out
set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, spec.MAX_EFFECTIVE_BALANCE - 1)
set_eth1_withdrawal_credential_with_balance(
spec,
state,
validator_index,
effective_balance=spec.MAX_EFFECTIVE_BALANCE - spec.EFFECTIVE_BALANCE_INCREMENT,
balance=spec.MAX_EFFECTIVE_BALANCE - 1,
)
validator = state.validators[validator_index]

assert validator.effective_balance < spec.MAX_EFFECTIVE_BALANCE
assert validator.effective_balance == spec.MAX_EFFECTIVE_BALANCE - spec.EFFECTIVE_BALANCE_INCREMENT
assert not spec.is_partially_withdrawable_validator(validator, state.balances[validator_index])

execution_payload = build_empty_execution_payload(spec, state)
Expand All @@ -614,7 +620,7 @@ def test_success_no_max_effective_balance(spec, state):
def test_success_no_excess_balance(spec, state):
validator_index = len(state.validators) // 2
# To be partially withdrawable, the validator needs an excess balance
set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, spec.MAX_EFFECTIVE_BALANCE)
set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, balance=spec.MAX_EFFECTIVE_BALANCE)
validator = state.validators[validator_index]

assert validator.effective_balance == spec.MAX_EFFECTIVE_BALANCE
Expand Down Expand Up @@ -823,6 +829,7 @@ def test_partially_withdrawable_validator_legacy_max_plus_one(spec, state):
set_eth1_withdrawal_credential_with_balance(
spec, state,
validator_index,
effective_balance=spec.MAX_EFFECTIVE_BALANCE,
balance=spec.MAX_EFFECTIVE_BALANCE + 1
)
assert spec.is_partially_withdrawable_validator(
Expand Down Expand Up @@ -872,6 +879,7 @@ def test_partially_withdrawable_validator_legacy_max_minus_one(spec, state):
set_eth1_withdrawal_credential_with_balance(
spec, state,
validator_index,
effective_balance=spec.MAX_EFFECTIVE_BALANCE - spec.EFFECTIVE_BALANCE_INCREMENT,
balance=spec.MAX_EFFECTIVE_BALANCE - 1
)
assert not spec.is_partially_withdrawable_validator(
Expand Down
9 changes: 7 additions & 2 deletions tests/core/pyspec/eth2spec/test/capella/sanity/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def test_top_up_and_partial_withdrawable_validator(spec, state):
next_withdrawal_validator_index = 0
validator_index = next_withdrawal_validator_index + 1

set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, spec.MAX_EFFECTIVE_BALANCE)
set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, balance=spec.MAX_EFFECTIVE_BALANCE)
validator = state.validators[validator_index]
balance = state.balances[validator_index]
assert not spec.is_partially_withdrawable_validator(validator, balance)
Expand Down Expand Up @@ -439,7 +439,12 @@ def test_top_up_to_fully_withdrawn_validator(spec, state):


def _insert_validator(spec, state, balance):
effective_balance = balance if balance < spec.MAX_EFFECTIVE_BALANCE else spec.MAX_EFFECTIVE_BALANCE
effective_balance = (
balance - balance % spec.EFFECTIVE_BALANCE_INCREMENT
if balance < spec.MAX_EFFECTIVE_BALANCE
else spec.MAX_EFFECTIVE_BALANCE
)

validator_index = len(state.validators)
validator = spec.Validator(
pubkey=pubkeys[validator_index],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,12 @@ def test_incorrect_source_has_pending_withdrawal(spec, state):
source_address = b"\x22" * 20
excess_balance = spec.EFFECTIVE_BALANCE_INCREMENT // 4
set_eth1_withdrawal_credential_with_balance(
spec, state, source_index, address=source_address, balance=spec.MIN_ACTIVATION_BALANCE + excess_balance
spec,
state,
source_index,
address=source_address,
effective_balance=spec.MIN_ACTIVATION_BALANCE,
balance=spec.MIN_ACTIVATION_BALANCE + excess_balance,
)
consolidation = spec.ConsolidationRequest(
source_address=source_address,
Expand Down Expand Up @@ -1118,7 +1123,12 @@ def test_incorrect_source_not_active_long_enough(spec, state):
source_address = b"\x22" * 20
excess_balance = spec.EFFECTIVE_BALANCE_INCREMENT // 4
set_eth1_withdrawal_credential_with_balance(
spec, state, source_index, address=source_address, balance=spec.MIN_ACTIVATION_BALANCE + excess_balance
spec,
state,
source_index,
address=source_address,
effective_balance=spec.MIN_ACTIVATION_BALANCE,
balance=spec.MIN_ACTIVATION_BALANCE + excess_balance,
)
consolidation = spec.ConsolidationRequest(
source_address=source_address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def run_test_activation_queue_eligibility(spec, state, validator_index, balance)
next_epoch(spec, state)

state.balances[validator_index] = balance
state.validators[validator_index].effective_balance = balance
state.validators[validator_index].effective_balance = balance - balance % spec.EFFECTIVE_BALANCE_INCREMENT

# ready for entrance into activation queue
mock_deposit(spec, state, validator_index)
Expand All @@ -23,7 +23,7 @@ def run_test_activation_queue_eligibility(spec, state, validator_index, balance)

# validator moved into activation queue if eligible
validator = state.validators[validator_index]
if validator.effective_balance < spec.MIN_ACTIVATION_BALANCE:
if validator.effective_balance <= (spec.MIN_ACTIVATION_BALANCE - spec.EFFECTIVE_BALANCE_INCREMENT):
assert validator.activation_eligibility_epoch == spec.FAR_FUTURE_EPOCH
else:
assert validator.activation_eligibility_epoch < spec.FAR_FUTURE_EPOCH
Expand Down Expand Up @@ -70,3 +70,12 @@ def test_activation_queue_eligibility__greater_than_min_activation_balance(spec,
balance = spec.MIN_ACTIVATION_BALANCE + spec.EFFECTIVE_BALANCE_INCREMENT
set_compounding_withdrawal_credential_with_balance(spec, state, index)
yield from run_test_activation_queue_eligibility(spec, state, index, balance)


@with_electra_and_later
@spec_state_test
def test_activation_queue_eligibility__fractional_greater_than_min_activation_balance(spec, state):
index = 15
balance = spec.MIN_ACTIVATION_BALANCE + (spec.EFFECTIVE_BALANCE_INCREMENT * 0.6)
set_compounding_withdrawal_credential_with_balance(spec, state, index)
yield from run_test_activation_queue_eligibility(spec, state, index, balance)
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,14 @@ def test_withdrawal_and_withdrawal_request_same_validator(spec, state):
excess_balance = 200000
balance = spec.MAX_EFFECTIVE_BALANCE + excess_balance
address = b'\x22' * 20
set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, balance, address)
set_eth1_withdrawal_credential_with_balance(
spec,
state,
validator_index,
effective_balance=spec.MAX_EFFECTIVE_BALANCE,
balance=balance,
address=address,
)

# Ensure the validator has an upcoming withdrawal
# This will happen before the withdrawal request
Expand Down Expand Up @@ -301,7 +308,14 @@ def test_withdrawal_and_switch_to_compounding_request_same_validator(spec, state
excess_balance = 200000
balance = spec.MAX_EFFECTIVE_BALANCE + excess_balance
address = b'\x22' * 20
set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, balance, address)
set_eth1_withdrawal_credential_with_balance(
spec,
state,
validator_index,
effective_balance=spec.MAX_EFFECTIVE_BALANCE,
balance=balance,
address=address,
)

# Ensure the validator has an upcoming withdrawal
# This will happen before the withdrawal request
Expand Down
22 changes: 17 additions & 5 deletions tests/core/pyspec/eth2spec/test/helpers/withdrawals.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@ def set_validator_fully_withdrawable(spec, state, index, withdrawable_epoch=None
assert spec.is_fully_withdrawable_validator(validator, state.balances[index], withdrawable_epoch)


def set_eth1_withdrawal_credential_with_balance(spec, state, index, balance=None, address=None):
if balance is None:
def set_eth1_withdrawal_credential_with_balance(spec, state, index, effective_balance=None, balance=None, address=None):
if balance is None and effective_balance is None:
balance = spec.MAX_EFFECTIVE_BALANCE
effective_balance = spec.MAX_EFFECTIVE_BALANCE
elif balance is None:
balance = effective_balance
elif effective_balance is None:
effective_balance = min(balance - balance % spec.EFFECTIVE_BALANCE_INCREMENT, spec.MAX_EFFECTIVE_BALANCE)

if address is None:
address = b'\x11' * 20

validator = state.validators[index]
validator.withdrawal_credentials = spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b'\x00' * 11 + address
validator.effective_balance = min(balance, spec.MAX_EFFECTIVE_BALANCE)
validator.effective_balance = effective_balance
state.balances[index] = balance


Expand All @@ -47,7 +53,13 @@ def set_validator_partially_withdrawable(spec, state, index, excess_balance=1000
validator.effective_balance = spec.MAX_EFFECTIVE_BALANCE_ELECTRA
state.balances[index] = validator.effective_balance + excess_balance
else:
set_eth1_withdrawal_credential_with_balance(spec, state, index, spec.MAX_EFFECTIVE_BALANCE + excess_balance)
set_eth1_withdrawal_credential_with_balance(
spec,
state,
index,
effective_balance=spec.MAX_EFFECTIVE_BALANCE,
balance=spec.MAX_EFFECTIVE_BALANCE + excess_balance,
)

assert spec.is_partially_withdrawable_validator(state.validators[index], state.balances[index])

Expand Down Expand Up @@ -104,7 +116,7 @@ def set_compounding_withdrawal_credential_with_balance(spec, state, index,
if balance is None:
balance = effective_balance

state.validators[index].effective_balance = effective_balance
state.validators[index].effective_balance = effective_balance - effective_balance % spec.EFFECTIVE_BALANCE_INCREMENT
state.balances[index] = balance


Expand Down
Loading