Skip to content

Commit

Permalink
Merge pull request #18 from james-prysm/deposit-queue
Browse files Browse the repository at this point in the history
updating spectests for pending deposit changes
  • Loading branch information
mkalinin authored Aug 7, 2024
2 parents 85b752b + c5db6df commit 698367e
Show file tree
Hide file tree
Showing 4 changed files with 1,015 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
)
from eth2spec.test.helpers.state import next_epoch_via_block
from eth2spec.test.helpers.withdrawals import set_validator_fully_withdrawable
from eth2spec.test.helpers.epoch_processing import run_epoch_processing_with


def run_process_pending_deposits(spec, state):
yield from run_epoch_processing_with(
spec, state, 'process_pending_deposits')


@with_electra_and_later
Expand All @@ -18,6 +24,7 @@ def test_new_deposit_under_max(spec, state):
deposit_request = prepare_deposit_request(spec, validator_index, amount, signed=True)

yield from run_deposit_request_processing(spec, state, deposit_request, validator_index)
yield from run_process_pending_deposits(spec, state)


@with_electra_and_later
Expand All @@ -30,6 +37,7 @@ def test_new_deposit_max(spec, state):
deposit_request = prepare_deposit_request(spec, validator_index, amount, signed=True)

yield from run_deposit_request_processing(spec, state, deposit_request, validator_index)
yield from run_process_pending_deposits(spec, state)


@with_electra_and_later
Expand All @@ -42,6 +50,7 @@ def test_new_deposit_over_max(spec, state):
deposit_request = prepare_deposit_request(spec, validator_index, amount, signed=True)

yield from run_deposit_request_processing(spec, state, deposit_request, validator_index)
yield from run_process_pending_deposits(spec, state)


@with_electra_and_later
Expand All @@ -64,6 +73,7 @@ def test_new_deposit_eth1_withdrawal_credentials(spec, state):
)

yield from run_deposit_request_processing(spec, state, deposit_request, validator_index)
yield from run_process_pending_deposits(spec, state)


@with_electra_and_later
Expand All @@ -85,6 +95,7 @@ def test_new_deposit_non_versioned_withdrawal_credentials(spec, state):
)

yield from run_deposit_request_processing(spec, state, deposit_request, validator_index)
yield from run_process_pending_deposits(spec, state)


@with_electra_and_later
Expand Down Expand Up @@ -177,6 +188,7 @@ def test_incorrect_sig_top_up(spec, state):

# invalid signatures, in top-ups, are allowed!
yield from run_deposit_request_processing(spec, state, deposit_request, validator_index)
yield from run_process_pending_deposits(spec, state)


@with_electra_and_later
Expand All @@ -194,6 +206,7 @@ def test_incorrect_withdrawal_credentials_top_up(spec, state):

# inconsistent withdrawal credentials, in top-ups, are allowed!
yield from run_deposit_request_processing(spec, state, deposit_request, validator_index)
yield from run_process_pending_deposits(spec, state)


@with_electra_and_later
Expand All @@ -208,6 +221,7 @@ def test_key_validate_invalid_subgroup(spec, state):
deposit_request = prepare_deposit_request(spec, validator_index, amount, pubkey=pubkey, signed=True)

yield from run_deposit_request_processing(spec, state, deposit_request, validator_index)
yield from run_process_pending_deposits(spec, state)


@with_electra_and_later
Expand All @@ -224,6 +238,7 @@ def test_key_validate_invalid_decompression(spec, state):
deposit_request = prepare_deposit_request(spec, validator_index, amount, pubkey=pubkey, signed=True)

yield from run_deposit_request_processing(spec, state, deposit_request, validator_index)
yield from run_process_pending_deposits(spec, state)


@with_electra_and_later
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
from eth2spec.test.helpers.epoch_processing import run_epoch_processing_with
from eth2spec.test.context import (
spec_state_test,
with_electra_and_later,
)
from eth2spec.test.helpers.keys import privkeys, pubkeys
from tests.core.pyspec.eth2spec.test.helpers.deposits import build_deposit_data
from eth2spec.test.helpers.state import next_epoch_via_block


def run_process_pending_deposits(spec, state):
yield from run_epoch_processing_with(
spec, state, 'process_pending_deposits')


@with_electra_and_later
@spec_state_test
def test_apply_pending_deposit_add_validator_to_registry(spec, state):
amount = spec.MIN_ACTIVATION_BALANCE

# select validator set outside of the mainnet preset of 256
index = len(state.validators)
withdrawal_credentials = (
spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkeys[index])[1:]
)
deposit_data = build_deposit_data(spec,
pubkeys[index],
privkeys[index],
amount,
withdrawal_credentials,
signed=True)
deposit = spec.PendingDeposit(
pubkey=pubkeys[index],
withdrawal_credentials=withdrawal_credentials,
amount=amount,
slot=spec.GENESIS_SLOT,
signature=deposit_data.signature,
)
old_validator_count = len(state.validators)
state.pending_deposits.append(deposit)
yield from run_process_pending_deposits(spec, state)
# validator count should increase by 1
assert len(state.validators) == old_validator_count + 1


@with_electra_and_later
@spec_state_test
def test_apply_pending_deposit_increases_balance(spec, state):
amount = 100
# signature doesn't matter here as it's interpreted as a top-up
deposit = spec.PendingDeposit(
pubkey=state.validators[0].pubkey,
withdrawal_credentials=state.validators[0].withdrawal_credentials,
amount=amount,
slot=spec.GENESIS_SLOT
)
# run test
spec.apply_pending_deposit(state, deposit)
assert state.balances[0] == amount


@with_electra_and_later
@spec_state_test
def test_apply_pending_deposit_switch_to_compounding(spec, state):
amount = 100

# choose a value public key that's in the validator set
index = 0
withdrawal_credentials = (
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX +
spec.hash(pubkeys[index])[1:]
)
compounding_credentials = (
spec.COMPOUNDING_WITHDRAWAL_PREFIX +
spec.hash(pubkeys[index])[1:]
)
# advance the state
next_epoch_via_block(spec, state)
state.validators[index].withdrawal_credentials = withdrawal_credentials
# set validator to be exited by current epoch
state.validators[index].exit_epoch = spec.get_current_epoch(state) - 1
deposit_data = build_deposit_data(spec,
pubkeys[index],
privkeys[index],
amount,
compounding_credentials,
signed=True)
deposit = spec.PendingDeposit(
pubkey=pubkeys[index],
withdrawal_credentials=compounding_credentials,
amount=amount,
slot=spec.GENESIS_SLOT,
signature=deposit_data.signature,
)
state.balances[index] = 0
# run test
spec.apply_pending_deposit(state, deposit)
# validator balance should increase
assert state.balances[index] == amount
current_credentials = state.validators[0].withdrawal_credentials
assert current_credentials == compounding_credentials


@with_electra_and_later
@spec_state_test
def test_apply_pending_deposit_switch_to_compounding_no_compounding(spec, state):
amount = 100

# choose a value public key that's in the validator set
index = 0
withdrawal_credentials = (
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX +
spec.hash(pubkeys[index])[1:]
)
# wrong compounding
compounding_credentials = (
spec.hash(b"wrong compounding address")[:]
)
# advance the state
next_epoch_via_block(spec, state)
state.validators[index].withdrawal_credentials = withdrawal_credentials
# set validator to be exited by current epoch
state.validators[index].exit_epoch = spec.get_current_epoch(state) - 1
deposit_data = build_deposit_data(spec,
pubkeys[index],
privkeys[index],
amount,
compounding_credentials,
signed=True)
deposit = spec.PendingDeposit(
pubkey=pubkeys[index],
withdrawal_credentials=compounding_credentials,
amount=amount,
slot=spec.GENESIS_SLOT,
signature=deposit_data.signature,
)
state.balances[index] = 0
# run test
spec.apply_pending_deposit(state, deposit)
# validator balance should increase
assert state.balances[index] == amount
current_credentials = state.validators[0].withdrawal_credentials
assert current_credentials == withdrawal_credentials


@with_electra_and_later
@spec_state_test
def test_apply_pending_deposit_switch_to_compounding_has_bls(spec, state):
amount = 100

# choose a value public key that's in the validator set
index = 0
compounding_credentials = (
spec.COMPOUNDING_WITHDRAWAL_PREFIX +
spec.hash(pubkeys[index])[1:]
)
# advance the state
next_epoch_via_block(spec, state)
bls_credentials = state.validators[index].withdrawal_credentials
# set validator to be exited by current epoch
state.validators[index].exit_epoch = spec.get_current_epoch(state) - 1
deposit_data = build_deposit_data(spec,
pubkeys[index],
privkeys[index],
amount,
compounding_credentials,
signed=True)
deposit = spec.PendingDeposit(
pubkey=pubkeys[index],
withdrawal_credentials=compounding_credentials,
amount=amount,
slot=spec.GENESIS_SLOT,
signature=deposit_data.signature,
)
state.balances[index] = 0
# run test
spec.apply_pending_deposit(state, deposit)
# validator balance should increase
assert state.balances[index] == amount
current_credentials = state.validators[index].withdrawal_credentials
# does not switch to compounding
assert current_credentials == bls_credentials


@with_electra_and_later
@spec_state_test
def test_apply_pending_deposit_switch_to_compounding_invalid_sig(spec, state):
amount = 100

# choose a value public key that's in the validator set
index = 0
withdrawal_credentials = (
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX +
spec.hash(pubkeys[index])[1:]
)
compounding_credentials = (
spec.COMPOUNDING_WITHDRAWAL_PREFIX +
spec.hash(pubkeys[index])[1:]
)
# advance the state
next_epoch_via_block(spec, state)
state.validators[index].withdrawal_credentials = withdrawal_credentials
# set validator to be exited by current epoch
state.validators[index].exit_epoch = spec.get_current_epoch(state) - 1
# creates wrong signature
deposit_data = build_deposit_data(spec,
pubkeys[index],
privkeys[index],
amount,
withdrawal_credentials,
signed=True)
deposit = spec.PendingDeposit(
pubkey=pubkeys[index],
withdrawal_credentials=compounding_credentials,
amount=amount,
slot=spec.GENESIS_SLOT,
signature=deposit_data.signature,
)
state.balances[index] = 0
# run test
spec.apply_pending_deposit(state, deposit)
# validator balance should increase
assert state.balances[index] == amount
current_credentials = state.validators[0].withdrawal_credentials
assert current_credentials == withdrawal_credentials


@with_electra_and_later
@spec_state_test
def test_apply_pending_deposit_switch_to_compounding_not_exited(spec, state):
amount = 100

# choose a value public key that's in the validator set
index = 0
withdrawal_credentials = (
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + spec.hash(pubkeys[index])[1:]
)
compounding_credentials = (
spec.COMPOUNDING_WITHDRAWAL_PREFIX + spec.hash(pubkeys[index])[1:]
)
state.validators[index].withdrawal_credentials = withdrawal_credentials
deposit_data = build_deposit_data(spec,
pubkeys[index],
privkeys[index],
amount,
compounding_credentials,
signed=True)
deposit = spec.PendingDeposit(
pubkey=pubkeys[index],
withdrawal_credentials=compounding_credentials,
amount=amount,
slot=spec.GENESIS_SLOT,
signature=deposit_data.signature,
)
state.balances[index] = 0
# run test
spec.apply_pending_deposit(state, deposit)
# validator balance should increase
assert state.balances[index] == amount
# make sure validator did not switch to compounding if not exited
current_credentials = state.validators[0].withdrawal_credentials
assert current_credentials == withdrawal_credentials
# postpone pending_deposit
assert len(state.pending_deposits) == 0
Loading

0 comments on commit 698367e

Please sign in to comment.