From c5db6dfcfb509462d9bc870847e0cbab196d7174 Mon Sep 17 00:00:00 2001 From: james-prysm Date: Tue, 6 Aug 2024 14:36:20 -0500 Subject: [PATCH] adding fork version tests --- .../test_process_pending_deposits.py | 65 +++++++++++++++++++ .../pyspec/eth2spec/test/helpers/deposits.py | 13 ++-- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/electra/epoch_processing/test_process_pending_deposits.py b/tests/core/pyspec/eth2spec/test/electra/epoch_processing/test_process_pending_deposits.py index 6d4b90354..8f27c7db1 100644 --- a/tests/core/pyspec/eth2spec/test/electra/epoch_processing/test_process_pending_deposits.py +++ b/tests/core/pyspec/eth2spec/test/electra/epoch_processing/test_process_pending_deposits.py @@ -871,3 +871,68 @@ def test_key_validate_invalid_decompression(spec, state): signed=True) state.pending_deposits.append(pd) yield from run_process_pending_deposits(spec, state) + + +@with_electra_and_later +@spec_state_test +@always_bls +def test_apply_pending_deposit_with_previous_fork_version(spec, state): + # Since deposits are valid across forks, the domain is always set with `GENESIS_FORK_VERSION`. + assert state.fork.previous_version != state.fork.current_version + amount = spec.MAX_EFFECTIVE_BALANCE + index = 0 + withdrawal_credentials = ( + spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + + spec.hash(pubkeys[index])[1:] + ) + wc = withdrawal_credentials + pd = build_pending_deposit(spec, index, + amount=amount, + withdrawal_credentials=wc, + fork_version=state.fork.previous_version, + signed=True) + state.pending_deposits.append(pd) + yield from run_process_pending_deposits(spec, state) + + +@with_electra_and_later +@spec_state_test +@always_bls +def test_apply_pending_deposit_with_genesis_fork_version(spec, state): + assert spec.config.GENESIS_FORK_VERSION not in (state.fork.previous_version, state.fork.current_version) + + assert state.fork.previous_version != state.fork.current_version + amount = spec.MAX_EFFECTIVE_BALANCE + index = 0 + withdrawal_credentials = ( + spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + + spec.hash(pubkeys[index])[1:] + ) + wc = withdrawal_credentials + pd = build_pending_deposit(spec, index, + amount=amount, + withdrawal_credentials=wc, + fork_version=spec.GENESIS_FORK_VERSION, + signed=True) + state.pending_deposits.append(pd) + yield from run_process_pending_deposits(spec, state) + + +@with_electra_and_later +@spec_state_test +@always_bls +def test_apply_pending_deposit_with_bad_fork_version(spec, state): + amount = spec.MAX_EFFECTIVE_BALANCE + index = 0 + withdrawal_credentials = ( + spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + + spec.hash(pubkeys[index])[1:] + ) + wc = withdrawal_credentials + pd = build_pending_deposit(spec, index, + amount=amount, + withdrawal_credentials=wc, + fork_version=spec.Version('0xAaBbCcDd'), + signed=True) + state.pending_deposits.append(pd) + yield from run_process_pending_deposits(spec, state) diff --git a/tests/core/pyspec/eth2spec/test/helpers/deposits.py b/tests/core/pyspec/eth2spec/test/helpers/deposits.py index fc6e6b5d2..72311be22 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/deposits.py +++ b/tests/core/pyspec/eth2spec/test/helpers/deposits.py @@ -23,23 +23,26 @@ def mock_deposit(spec, state, index): assert not spec.is_active_validator(state.validators[index], spec.get_current_epoch(state)) -def build_deposit_data(spec, pubkey, privkey, amount, withdrawal_credentials, signed=False): +def build_deposit_data(spec, pubkey, privkey, amount, withdrawal_credentials, fork_version, signed=False): deposit_data = spec.DepositData( pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount, ) if signed: - sign_deposit_data(spec, deposit_data, privkey) + sign_deposit_data(spec, deposit_data, privkey, fork_version) return deposit_data -def sign_deposit_data(spec, deposit_data, privkey): +def sign_deposit_data(spec, deposit_data, privkey, fork_version): deposit_message = spec.DepositMessage( pubkey=deposit_data.pubkey, withdrawal_credentials=deposit_data.withdrawal_credentials, amount=deposit_data.amount) - domain = spec.compute_domain(spec.DOMAIN_DEPOSIT) + if fork_version is not None: + domain = spec.compute_domain(domain_type=spec.DOMAIN_DEPOSIT, fork_version=fork_version) + else: + domain = spec.compute_domain(spec.DOMAIN_DEPOSIT) signing_root = spec.compute_signing_root(deposit_message, domain) deposit_data.signature = bls.Sign(privkey, signing_root) @@ -241,6 +244,7 @@ def build_pending_deposit(spec, validator_index, amount, pubkey=None, privkey=None, withdrawal_credentials=None, + fork_version=None, slot=None, signed=False): if index is None: @@ -267,6 +271,7 @@ def build_pending_deposit(spec, validator_index, amount, privkeys[index], amount, withdrawal_credentials, + fork_version, signed=True) pending_deposit.signature = deposit_data.signature return pending_deposit