Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

electra: Misc beacon chain operations tests #3980

Merged
merged 5 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -1,12 +1,16 @@
from eth2spec.test.helpers.constants import MINIMAL
from eth2spec.test.context import (
always_bls,
spec_state_test,
with_electra_and_later,
with_presets,
)
from eth2spec.test.helpers.attestations import (
run_attestation_processing,
get_valid_attestation,
sign_attestation,
build_attestation_data,
fill_aggregate_attestation,
)
from eth2spec.test.helpers.state import (
next_slots,
Expand Down Expand Up @@ -79,3 +83,30 @@ def test_invalid_nonset_committe_bits(spec, state):
attestation.committee_bits[committee_index] = 0

yield from run_attestation_processing(spec, state, attestation, valid=False)


@with_electra_and_later
@spec_state_test
@with_presets([MINIMAL], "need multiple committees per slot")
@always_bls
def test_multiple_committees(spec, state):
attestation_data = build_attestation_data(spec, state, slot=state.slot, index=0)
attestation = spec.Attestation(data=attestation_data)

# fill the attestation with two committees
fill_aggregate_attestation(spec, state, attestation, signed=True, committee_index=0)
fill_aggregate_attestation(spec, state, attestation, signed=True, committee_index=1)

next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY)

yield from run_attestation_processing(spec, state, attestation)


@with_electra_and_later
@spec_state_test
@always_bls
def test_one_committee_with_gap(spec, state):
attestation = get_valid_attestation(spec, state, index=1, signed=True)
next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY)

yield from run_attestation_processing(spec, state, attestation)
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_basic_withdrawal_request_with_full_partial_withdrawal_queue(spec, state
)


# Invalid tests
# Tests that should fail


@with_electra_and_later
Expand Down Expand Up @@ -237,6 +237,31 @@ def test_activation_epoch_less_than_shard_committee_period(spec, state):
)


@with_electra_and_later
@spec_state_test
def test_unknown_pubkey(spec, state):
rng = random.Random(1344)
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH

current_epoch = spec.get_current_epoch(state)
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
address = b"\x22" * 20
pubkey = spec.BLSPubkey(b"\x23" * 48)
set_eth1_withdrawal_credential_with_balance(
spec, state, validator_index, address=address
)
withdrawal_request = spec.WithdrawalRequest(
source_address=address,
validator_pubkey=pubkey,
amount=spec.FULL_EXIT_REQUEST_AMOUNT,
)

yield from run_withdrawal_request_processing(
spec, state, withdrawal_request, success=False
)


# Partial withdrawals tests

@with_electra_and_later
Expand Down Expand Up @@ -896,10 +921,6 @@ def run_withdrawal_request_processing(
If ``valid == False``, run expecting ``AssertionError``
If ``success == False``, it doesn't initiate exit successfully
"""
validator_index = get_validator_index_by_pubkey(
state, withdrawal_request.validator_pubkey
)

yield "pre", state
yield "withdrawal_request", withdrawal_request

Expand All @@ -912,14 +933,7 @@ def run_withdrawal_request_processing(
yield "post", None
return

pre_exit_epoch = state.validators[validator_index].exit_epoch
pre_pending_partial_withdrawals = state.pending_partial_withdrawals.copy()
pre_balance = state.balances[validator_index]
pre_effective_balance = state.validators[validator_index].effective_balance
pre_state = state.copy()
expected_amount_to_withdraw = compute_amount_to_withdraw(
spec, state, validator_index, withdrawal_request.amount
)

spec.process_withdrawal_request(
state, withdrawal_request
Expand All @@ -931,6 +945,13 @@ def run_withdrawal_request_processing(
# No-op
assert pre_state == state
else:
validator_index = get_validator_index_by_pubkey(
state, withdrawal_request.validator_pubkey
)
pre_exit_epoch = pre_state.validators[validator_index].exit_epoch
pre_pending_partial_withdrawals = pre_state.pending_partial_withdrawals.copy()
pre_balance = pre_state.balances[validator_index]
pre_effective_balance = pre_state.validators[validator_index].effective_balance
assert state.balances[validator_index] == pre_balance
assert (
state.validators[validator_index].effective_balance == pre_effective_balance
Expand All @@ -943,6 +964,9 @@ def run_withdrawal_request_processing(
assert state.pending_partial_withdrawals == pre_pending_partial_withdrawals
# Partial withdrawal request
else:
expected_amount_to_withdraw = compute_amount_to_withdraw(
spec, pre_state, validator_index, withdrawal_request.amount
)
assert state.validators[validator_index].exit_epoch == spec.FAR_FUTURE_EPOCH
expected_withdrawable_epoch = (
state.earliest_exit_epoch
Expand Down