Skip to content

Commit

Permalink
adding fork version tests
Browse files Browse the repository at this point in the history
  • Loading branch information
james-prysm committed Aug 6, 2024
1 parent 56fe310 commit c5db6df
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 9 additions & 4 deletions tests/core/pyspec/eth2spec/test/helpers/deposits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit c5db6df

Please sign in to comment.