Skip to content

Commit

Permalink
add EF consensus spec test Electra deposits operations fixture (#6243)
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec committed Apr 26, 2024
1 parent 5d5517b commit 34ba05f
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 27 deletions.
27 changes: 26 additions & 1 deletion ConsensusSpecPreset-mainnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -2613,6 +2613,31 @@ OK: 6/6 Fail: 0/6 Skip: 0/6
+ [Invalid] EF - Electra - Operations - Consolidation - invalid_target_signature OK
```
OK: 11/11 Fail: 0/11 Skip: 0/11
## EF - Electra - Operations - Deposit [Preset: mainnet]
```diff
+ [Invalid] EF - Electra - Operations - Deposit - invalid_bad_merkle_proof OK
+ [Invalid] EF - Electra - Operations - Deposit - invalid_wrong_deposit_for_deposit_count OK
+ [Valid] EF - Electra - Operations - Deposit - correct_sig_but_forked_state OK
+ [Valid] EF - Electra - Operations - Deposit - effective_deposit_with_genesis_fork_versio OK
+ [Valid] EF - Electra - Operations - Deposit - incorrect_sig_new_deposit OK
+ [Valid] EF - Electra - Operations - Deposit - incorrect_sig_top_up OK
+ [Valid] EF - Electra - Operations - Deposit - incorrect_withdrawal_credentials_top_up OK
+ [Valid] EF - Electra - Operations - Deposit - ineffective_deposit_with_bad_fork_version OK
+ [Valid] EF - Electra - Operations - Deposit - ineffective_deposit_with_current_fork_vers OK
+ [Valid] EF - Electra - Operations - Deposit - ineffective_deposit_with_previous_fork_ver OK
+ [Valid] EF - Electra - Operations - Deposit - key_validate_invalid_decompression OK
+ [Valid] EF - Electra - Operations - Deposit - key_validate_invalid_subgroup OK
+ [Valid] EF - Electra - Operations - Deposit - new_deposit_eth1_withdrawal_credentials OK
+ [Valid] EF - Electra - Operations - Deposit - new_deposit_max OK
+ [Valid] EF - Electra - Operations - Deposit - new_deposit_non_versioned_withdrawal_crede OK
+ [Valid] EF - Electra - Operations - Deposit - new_deposit_over_max OK
+ [Valid] EF - Electra - Operations - Deposit - new_deposit_under_max OK
+ [Valid] EF - Electra - Operations - Deposit - success_top_up_to_withdrawn_validator OK
+ [Valid] EF - Electra - Operations - Deposit - top_up__less_effective_balance OK
+ [Valid] EF - Electra - Operations - Deposit - top_up__max_effective_balance OK
+ [Valid] EF - Electra - Operations - Deposit - top_up__zero_balance OK
```
OK: 21/21 Fail: 0/21 Skip: 0/21
## EF - Electra - Operations - Execution Layer Withdrawal Request [Preset: mainnet]
```diff
+ [Valid] EF - Electra - Operations - Execution Layer Withdrawal Request - activation_epoc OK
Expand Down Expand Up @@ -3397,4 +3422,4 @@ OK: 69/88 Fail: 0/88 Skip: 19/88
OK: 3/3 Fail: 0/3 Skip: 0/3

---TOTAL---
OK: 2716/2735 Fail: 0/2735 Skip: 19/2735
OK: 2737/2756 Fail: 0/2756 Skip: 19/2756
27 changes: 26 additions & 1 deletion ConsensusSpecPreset-minimal.md
Original file line number Diff line number Diff line change
Expand Up @@ -2749,6 +2749,31 @@ OK: 6/6 Fail: 0/6 Skip: 0/6
+ [Valid] EF - Electra - Operations - Consolidation - multiple_consolidations_above_churn OK
```
OK: 20/20 Fail: 0/20 Skip: 0/20
## EF - Electra - Operations - Deposit [Preset: minimal]
```diff
+ [Invalid] EF - Electra - Operations - Deposit - invalid_bad_merkle_proof OK
+ [Invalid] EF - Electra - Operations - Deposit - invalid_wrong_deposit_for_deposit_count OK
+ [Valid] EF - Electra - Operations - Deposit - correct_sig_but_forked_state OK
+ [Valid] EF - Electra - Operations - Deposit - effective_deposit_with_genesis_fork_versio OK
+ [Valid] EF - Electra - Operations - Deposit - incorrect_sig_new_deposit OK
+ [Valid] EF - Electra - Operations - Deposit - incorrect_sig_top_up OK
+ [Valid] EF - Electra - Operations - Deposit - incorrect_withdrawal_credentials_top_up OK
+ [Valid] EF - Electra - Operations - Deposit - ineffective_deposit_with_bad_fork_version OK
+ [Valid] EF - Electra - Operations - Deposit - ineffective_deposit_with_current_fork_vers OK
+ [Valid] EF - Electra - Operations - Deposit - ineffective_deposit_with_previous_fork_ver OK
+ [Valid] EF - Electra - Operations - Deposit - key_validate_invalid_decompression OK
+ [Valid] EF - Electra - Operations - Deposit - key_validate_invalid_subgroup OK
+ [Valid] EF - Electra - Operations - Deposit - new_deposit_eth1_withdrawal_credentials OK
+ [Valid] EF - Electra - Operations - Deposit - new_deposit_max OK
+ [Valid] EF - Electra - Operations - Deposit - new_deposit_non_versioned_withdrawal_crede OK
+ [Valid] EF - Electra - Operations - Deposit - new_deposit_over_max OK
+ [Valid] EF - Electra - Operations - Deposit - new_deposit_under_max OK
+ [Valid] EF - Electra - Operations - Deposit - success_top_up_to_withdrawn_validator OK
+ [Valid] EF - Electra - Operations - Deposit - top_up__less_effective_balance OK
+ [Valid] EF - Electra - Operations - Deposit - top_up__max_effective_balance OK
+ [Valid] EF - Electra - Operations - Deposit - top_up__zero_balance OK
```
OK: 21/21 Fail: 0/21 Skip: 0/21
## EF - Electra - Operations - Execution Layer Withdrawal Request [Preset: minimal]
```diff
+ [Valid] EF - Electra - Operations - Execution Layer Withdrawal Request - activation_epoc OK
Expand Down Expand Up @@ -3699,4 +3724,4 @@ OK: 185/207 Fail: 0/207 Skip: 22/207
OK: 3/3 Fail: 0/3 Skip: 0/3

---TOTAL---
OK: 2987/3009 Fail: 0/3009 Skip: 22/3009
OK: 3008/3030 Fail: 0/3030 Skip: 22/3030
25 changes: 20 additions & 5 deletions beacon_chain/spec/beaconstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ func decrease_balance*(

# https://github.com/ethereum/consensus-specs/blob/v1.4.0-alpha.3/specs/phase0/beacon-chain.md#deposits
# https://github.com/ethereum/consensus-specs/blob/v1.3.0/specs/altair/beacon-chain.md#modified-apply_deposit
func get_validator_from_deposit*(deposit: DepositData):
Validator =
func get_validator_from_deposit*(
state: phase0.BeaconState | altair.BeaconState | bellatrix.BeaconState |
capella.BeaconState | deneb.BeaconState,
deposit: DepositData): Validator =
let
amount = deposit.amount
effective_balance = min(
Expand All @@ -65,6 +67,19 @@ func get_validator_from_deposit*(deposit: DepositData):
effective_balance: effective_balance
)

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/specs/electra/beacon-chain.md#updated-get_validator_from_deposit
func get_validator_from_deposit*(
state: electra.BeaconState, deposit: DepositData): Validator =
Validator(
pubkeyData: HashedValidatorPubKey.init(deposit.pubkey),
withdrawal_credentials: deposit.withdrawal_credentials,
activation_eligibility_epoch: FAR_FUTURE_EPOCH,
activation_epoch: FAR_FUTURE_EPOCH,
exit_epoch: FAR_FUTURE_EPOCH,
withdrawable_epoch: FAR_FUTURE_EPOCH,
effective_balance: 0.Gwei # [Modified in Electra:EIP7251]
)

# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/phase0/beacon-chain.md#compute_activation_exit_epoch
func compute_activation_exit_epoch*(epoch: Epoch): Epoch =
## Return the epoch during which validator activations and exits initiated in
Expand Down Expand Up @@ -1020,7 +1035,7 @@ func is_partially_withdrawable_validator(
has_max_effective_balance and has_excess_balance

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/specs/electra/beacon-chain.md#new-is_compounding_withdrawal_credential
func is_compounding_withdrawal_credential(
func is_compounding_withdrawal_credential*(
withdrawal_credentials: Eth2Digest): bool =
withdrawal_credentials.data[0] == COMPOUNDING_WITHDRAWAL_PREFIX

Expand Down Expand Up @@ -1198,7 +1213,7 @@ proc initialize_beacon_state_from_eth1(
if skipBlsValidation in flags or
verify_deposit_signature(cfg, deposit):
pubkeyToIndex[pubkey] = ValidatorIndex(state.validators.len)
if not state.validators.add(get_validator_from_deposit(deposit)):
if not state.validators.add(get_validator_from_deposit(state, deposit)):
raiseAssert "too many validators"
if not state.balances.add(amount):
raiseAssert "same as validators"
Expand Down Expand Up @@ -1303,7 +1318,7 @@ proc initialize_beacon_state_from_eth1*(
if skipBlsValidation in flags or
verify_deposit_signature(cfg, deposit):
pubkeyToIndex[pubkey] = ValidatorIndex(state.validators.len)
if not state.validators.add(get_validator_from_deposit(deposit)):
if not state.validators.add get_validator_from_deposit(state, deposit):
raiseAssert "too many validators"
if not state.balances.add(amount):
raiseAssert "same as validators"
Expand Down
1 change: 1 addition & 0 deletions beacon_chain/spec/signatures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ proc verify_attestation_signature*(

blsFastAggregateVerify(pubkeys, signing_root.data, signature)

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/specs/electra/beacon-chain.md#new-is_valid_deposit_signature
func compute_deposit_signing_root(
version: Version,
deposit_message: DepositMessage): Eth2Digest =
Expand Down
32 changes: 29 additions & 3 deletions beacon_chain/spec/state_transition_block.nim
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ func findValidatorIndex*(state: ForkyBeaconState, pubkey: ValidatorPubKey):
from ".."/bloomfilter import
PubkeyBloomFilter, constructBloomFilter, incl, mightContain

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/specs/phase0/beacon-chain.md#deposits
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/specs/electra/beacon-chain.md#updated--apply_deposit
proc process_deposit*(
cfg: RuntimeConfig, state: var ForkyBeaconState,
bloom_filter: var PubkeyBloomFilter, deposit: Deposit, flags: UpdateFlags):
Expand Down Expand Up @@ -333,15 +335,34 @@ proc process_deposit*(

if index.isSome():
# Increase balance by deposit amount
increase_balance(state, index.get(), amount)
when typeof(state).kind < ConsensusFork.Electra:
increase_balance(state, index.get(), amount)
else:
debugRaiseAssert "check hashlist add return"
discard state.pending_balance_deposits.add PendingBalanceDeposit(
index: index.get.uint64, amount: amount) # [Modified in Electra:EIP-7251]

# Check if valid deposit switch to compounding credentials
if is_compounding_withdrawal_credential(
deposit.data.withdrawal_credentials) and
has_eth1_withdrawal_credential(state.validators.item(index.get)) and
verify_deposit_signature(cfg, deposit.data):
switch_to_compounding_validator(state, index.get)
else:
# Verify the deposit signature (proof of possession) which is not checked
# by the deposit contract
if verify_deposit_signature(cfg, deposit.data):
# New validator! Add validator and balance entries
if not state.validators.add(get_validator_from_deposit(deposit.data)):
if not state.validators.add(
get_validator_from_deposit(state, deposit.data)):
return err("process_deposit: too many validators")
if not state.balances.add(amount):

let initial_balance =
when typeof(state).kind >= ConsensusFork.Electra:
0.Gwei
else:
amount
if not state.balances.add(initial_balance):
static: doAssert state.balances.maxLen == state.validators.maxLen
raiseAssert "adding validator succeeded, so should balances"

Expand All @@ -352,7 +373,12 @@ proc process_deposit*(
return err("process_deposit: too many validators (current_epoch_participation)")
if not state.inactivity_scores.add(0'u64):
return err("process_deposit: too many validators (inactivity_scores)")
when typeof(state).kind >= ConsensusFork.Electra:
debugRaiseAssert "check hashlist add return"

# [New in Electra:EIP7251]
discard state.pending_balance_deposits.add PendingBalanceDeposit(
index: state.validators.lenu64 - 1, amount: amount)
doAssert state.validators.len == state.balances.len
bloom_filter.incl pubkey
else:
Expand Down
34 changes: 17 additions & 17 deletions tests/consensus_spec/electra/test_fixture_operations.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ proc runTest[T, U](
when false:
debugRaiseAssert "when these are fixed..."
suite baseDescription & "Attestation " & preset():
proc applyAttestation(
func applyAttestation(
preState: var electra.BeaconState, attestation: Attestation):
Result[void, cstring] =
var cache: StateCache
Expand Down Expand Up @@ -149,7 +149,7 @@ suite baseDescription & "BLS to execution change " & preset():
applyBlsToExecutionChange, path)

suite baseDescription & "Consolidation " & preset():
proc applyConsolidation(
func applyConsolidation(
preState: var electra.BeaconState,
signed_consolidation: SignedConsolidation):
Result[void, cstring] =
Expand All @@ -169,23 +169,23 @@ suite baseDescription & "Consolidation " & preset():
OpConsolidationDir, suiteName, "Consolidation", "consolidation",
applyConsolidation, path)

when false:
from ".."/".."/".."/beacon_chain/bloomfilter import constructBloomFilter
from ".."/".."/".."/beacon_chain/bloomfilter import constructBloomFilter

suite baseDescription & "Deposit " & preset():
proc applyDeposit(
preState: var electra.BeaconState, deposit: Deposit):
Result[void, cstring] =
process_deposit(
defaultRuntimeConfig, preState,
constructBloomFilter(preState.validators.asSeq)[], deposit, {})
suite baseDescription & "Deposit " & preset():
func applyDeposit(
preState: var electra.BeaconState, deposit: Deposit):
Result[void, cstring] =
process_deposit(
defaultRuntimeConfig, preState,
constructBloomFilter(preState.validators.asSeq)[], deposit, {})

for path in walkTests(OpDepositsDir):
runTest[Deposit, typeof applyDeposit](
OpDepositsDir, suiteName, "Deposit", "deposit", applyDeposit, path)
for path in walkTests(OpDepositsDir):
runTest[Deposit, typeof applyDeposit](
OpDepositsDir, suiteName, "Deposit", "deposit", applyDeposit, path)

when false:
suite baseDescription & "Execution Payload " & preset():
proc makeApplyExecutionPayloadCb(path: string): auto =
func makeApplyExecutionPayloadCb(path: string): auto =
return proc(
preState: var electra.BeaconState, body: electra.BeaconBlockBody):
Result[void, cstring] {.raises: [IOError].} =
Expand All @@ -204,7 +204,7 @@ when false:
debugRaiseAssert "deposit receipts"

suite baseDescription & "Execution Layer Withdrawal Request " & preset():
proc applyExecutionLayerWithdrawalRequest(
func applyExecutionLayerWithdrawalRequest(
preState: var electra.BeaconState,
executionLayerWithdrawalRequest: ExecutionLayerWithdrawalRequest):
Result[void, cstring] =
Expand Down Expand Up @@ -269,7 +269,7 @@ suite baseDescription & "Voluntary Exit " & preset():
applyVoluntaryExit, path)

suite baseDescription & "Withdrawals " & preset():
proc applyWithdrawals(
func applyWithdrawals(
preState: var electra.BeaconState,
executionPayload: electra.ExecutionPayload): Result[void, cstring] =
process_withdrawals(preState, executionPayload)
Expand Down

0 comments on commit 34ba05f

Please sign in to comment.