Skip to content

Commit

Permalink
Merge pull request ethereum#3999 from jtraglia/simplify-apply-deposit
Browse files Browse the repository at this point in the history
Deduplicate code in `apply_deposit`
  • Loading branch information
jtraglia authored Nov 4, 2024
2 parents 1b408e9 + 410b14f commit 17feed7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
31 changes: 12 additions & 19 deletions specs/electra/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,6 @@ def apply_pending_deposit(state: BeaconState, deposit: PendingDeposit) -> None:
add_validator_to_registry(state, deposit.pubkey, deposit.withdrawal_credentials, deposit.amount)
else:
validator_index = ValidatorIndex(validator_pubkeys.index(deposit.pubkey))
# Increase balance
increase_balance(state, validator_index, deposit.amount)
```

Expand Down Expand Up @@ -1356,24 +1355,18 @@ def apply_deposit(state: BeaconState,
# Verify the deposit signature (proof of possession) which is not checked by the deposit contract
if is_valid_deposit_signature(pubkey, withdrawal_credentials, amount, signature):
add_validator_to_registry(state, pubkey, withdrawal_credentials, Gwei(0)) # [Modified in Electra:EIP7251]
# [New in Electra:EIP7251]
state.pending_deposits.append(PendingDeposit(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
amount=amount,
signature=signature,
slot=GENESIS_SLOT, # Use GENESIS_SLOT to distinguish from a pending deposit request
))
else:
# Increase balance by deposit amount
# [Modified in Electra:EIP7251]
state.pending_deposits.append(PendingDeposit(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
amount=amount,
signature=signature,
slot=GENESIS_SLOT # Use GENESIS_SLOT to distinguish from a pending deposit request
))
else:
return

# Increase balance by deposit amount
# [Modified in Electra:EIP7251]
state.pending_deposits.append(PendingDeposit(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
amount=amount,
signature=signature,
slot=GENESIS_SLOT # Use GENESIS_SLOT to distinguish from a pending deposit request
))
```

###### New `is_valid_deposit_signature`
Expand Down
16 changes: 9 additions & 7 deletions tests/core/pyspec/eth2spec/test/helpers/deposits.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def run_deposit_processing(spec, state, deposit, validator_index, valid=True, ef
pre_effective_balance = state.validators[validator_index].effective_balance

if is_post_electra(spec):
pre_pending_deposits = len(state.pending_deposits)
pre_pending_deposits_count = len(state.pending_deposits)

yield 'pre', state
yield 'deposit', deposit
Expand All @@ -290,6 +290,8 @@ def run_deposit_processing(spec, state, deposit, validator_index, valid=True, ef
assert len(state.balances) == pre_validator_count
if is_top_up:
assert get_balance(state, validator_index) == pre_balance
if is_post_electra(spec):
assert len(state.pending_deposits) == pre_pending_deposits_count
else:
if is_top_up:
# Top-ups don't add validators
Expand All @@ -313,13 +315,13 @@ def run_deposit_processing(spec, state, deposit, validator_index, valid=True, ef
assert get_balance(state, validator_index) == pre_balance
assert state.validators[validator_index].effective_balance == pre_effective_balance
# new correct balance deposit queued up
assert len(state.pending_deposits) == pre_pending_deposits + 1
assert state.pending_deposits[pre_pending_deposits].pubkey == deposit.data.pubkey
assert len(state.pending_deposits) == pre_pending_deposits_count + 1
assert state.pending_deposits[pre_pending_deposits_count].pubkey == deposit.data.pubkey
assert state.pending_deposits[
pre_pending_deposits].withdrawal_credentials == deposit.data.withdrawal_credentials
assert state.pending_deposits[pre_pending_deposits].amount == deposit.data.amount
assert state.pending_deposits[pre_pending_deposits].signature == deposit.data.signature
assert state.pending_deposits[pre_pending_deposits].slot == spec.GENESIS_SLOT
pre_pending_deposits_count].withdrawal_credentials == deposit.data.withdrawal_credentials
assert state.pending_deposits[pre_pending_deposits_count].amount == deposit.data.amount
assert state.pending_deposits[pre_pending_deposits_count].signature == deposit.data.signature
assert state.pending_deposits[pre_pending_deposits_count].slot == spec.GENESIS_SLOT

assert state.eth1_deposit_index == state.eth1_data.deposit_count

Expand Down

0 comments on commit 17feed7

Please sign in to comment.