Skip to content

Commit

Permalink
use EF consensus spec v1.5.0-alpha.10 test vectors (#6761)
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec authored Dec 15, 2024
1 parent 2bf0df7 commit 7cb0a61
Show file tree
Hide file tree
Showing 21 changed files with 353 additions and 222 deletions.
18 changes: 9 additions & 9 deletions AllTests-mainnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
OK: 2/2 Fail: 0/2 Skip: 0/2
## EF - EIP7594 - Networking [Preset: mainnet]
```diff
+ Networking - Get Custody Columns - mainnet/eip7594/networking/get_custody_columns/pyspec_t OK
+ Networking - Get Custody Columns - mainnet/eip7594/networking/get_custody_columns/pyspec_t OK
+ Networking - Get Custody Columns - mainnet/eip7594/networking/get_custody_columns/pyspec_t OK
+ Networking - Get Custody Columns - mainnet/eip7594/networking/get_custody_columns/pyspec_t OK
+ Networking - Get Custody Columns - mainnet/eip7594/networking/get_custody_columns/pyspec_t OK
+ Networking - Get Custody Columns - mainnet/eip7594/networking/get_custody_columns/pyspec_t OK
+ Networking - Get Custody Columns - mainnet/eip7594/networking/get_custody_columns/pyspec_t OK
+ Networking - Get Custody Columns - mainnet/eip7594/networking/get_custody_columns/pyspec_t OK
+ Networking - Get Custody Columns - mainnet/eip7594/networking/get_custody_columns/pyspec_t OK
+ Networking - Get Custody Columns - mainnet/fulu/networking/get_custody_columns/pyspec_test OK
+ Networking - Get Custody Columns - mainnet/fulu/networking/get_custody_columns/pyspec_test OK
+ Networking - Get Custody Columns - mainnet/fulu/networking/get_custody_columns/pyspec_test OK
+ Networking - Get Custody Columns - mainnet/fulu/networking/get_custody_columns/pyspec_test OK
+ Networking - Get Custody Columns - mainnet/fulu/networking/get_custody_columns/pyspec_test OK
+ Networking - Get Custody Columns - mainnet/fulu/networking/get_custody_columns/pyspec_test OK
+ Networking - Get Custody Columns - mainnet/fulu/networking/get_custody_columns/pyspec_test OK
+ Networking - Get Custody Columns - mainnet/fulu/networking/get_custody_columns/pyspec_test OK
+ Networking - Get Custody Columns - mainnet/fulu/networking/get_custody_columns/pyspec_test OK
```
OK: 9/9 Fail: 0/9 Skip: 0/9
## EF - KZG
Expand Down
148 changes: 80 additions & 68 deletions ConsensusSpecPreset-mainnet.md

Large diffs are not rendered by default.

152 changes: 82 additions & 70 deletions ConsensusSpecPreset-minimal.md

Large diffs are not rendered by default.

62 changes: 49 additions & 13 deletions beacon_chain/spec/beaconstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1212,11 +1212,9 @@ proc process_attestation*(
ok(proposer_reward)

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.9/specs/altair/beacon-chain.md#get_next_sync_committee_indices
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/electra/beacon-chain.md#modified-get_next_sync_committee_indices
func get_next_sync_committee_keys(
state: altair.BeaconState | bellatrix.BeaconState | capella.BeaconState |
deneb.BeaconState | electra.BeaconState | fulu.BeaconState):
array[SYNC_COMMITTEE_SIZE, ValidatorPubKey] =
deneb.BeaconState): array[SYNC_COMMITTEE_SIZE, ValidatorPubKey] =
## Return the sequence of sync committee indices, with possible duplicates,
## for the next sync committee.
# The sync committe depends on seed and effective balance - it can
Expand Down Expand Up @@ -1244,13 +1242,51 @@ func get_next_sync_committee_keys(
candidate_index = active_validator_indices[shuffled_index]
random_byte = eth2digest(hash_buffer).data[i mod 32]
effective_balance = state.validators[candidate_index].effective_balance
const meb =
when typeof(state).kind >= ConsensusFork.Electra:
MAX_EFFECTIVE_BALANCE_ELECTRA.Gwei # [Modified in Electra:EIP7251]
else:
MAX_EFFECTIVE_BALANCE.Gwei
if effective_balance * MAX_RANDOM_BYTE >=
MAX_EFFECTIVE_BALANCE.Gwei * random_byte:
res[index] = state.validators[candidate_index].pubkey
inc index
i += 1'u64
res

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.10/specs/electra/beacon-chain.md#modified-get_next_sync_committee_indices
func get_next_sync_committee_keys(
state: electra.BeaconState | fulu.BeaconState):
array[SYNC_COMMITTEE_SIZE, ValidatorPubKey] =
## Return the sequence of sync committee indices, with possible duplicates,
## for the next sync committee.
# The sync committe depends on seed and effective balance - it can
# thus only be computed for the current epoch of the state, after balance
# updates have been performed

let epoch = get_current_epoch(state) + 1

if effective_balance * MAX_RANDOM_BYTE >= meb * random_byte:
const MAX_RANDOM_VALUE = 65536 - 1 # [Modified in Electra]
let
active_validator_indices = get_active_validator_indices(state, epoch)
active_validator_count = uint64(len(active_validator_indices))
seed = get_seed(state, epoch, DOMAIN_SYNC_COMMITTEE)
var
i = 0'u64
index = 0
res: array[SYNC_COMMITTEE_SIZE, ValidatorPubKey]
hash_buffer: array[40, byte]
rv_buf: array[8, byte]
hash_buffer[0..31] = seed.data
while index < SYNC_COMMITTEE_SIZE:
hash_buffer[32..39] = uint_to_bytes(uint64(i div 16)) # [Modified in Electra]
let
shuffled_index = compute_shuffled_index(
uint64(i mod active_validator_count), active_validator_count, seed)
candidate_index = active_validator_indices[shuffled_index]
random_bytes = eth2digest(hash_buffer).data
offset = (i mod 16) * 2
effective_balance = state.validators[candidate_index].effective_balance
rv_buf[0 .. 1] = random_bytes.toOpenArray(offset, offset + 1)
let random_value = bytes_to_uint64(rv_buf)
# [Modified in Electra:EIP7251]
if effective_balance * MAX_RANDOM_VALUE >=
MAX_EFFECTIVE_BALANCE_ELECTRA.Gwei * random_value:
res[index] = state.validators[candidate_index].pubkey
inc index
i += 1'u64
Expand Down Expand Up @@ -1336,7 +1372,7 @@ func get_pending_balance_to_withdraw*(
validator_index: ValidatorIndex): Gwei =
var pending_balance: Gwei
for withdrawal in state.pending_partial_withdrawals:
if withdrawal.index == validator_index:
if withdrawal.validator_index == validator_index:
pending_balance += withdrawal.amount

pending_balance
Expand Down Expand Up @@ -1444,10 +1480,10 @@ template get_expected_withdrawals_with_partial_count_aux*(
break

let
validator = state.validators.item(withdrawal.index)
validator = state.validators.item(withdrawal.validator_index)

# Keep a uniform variable name available for injected code
validator_index {.inject.} = withdrawal.index
validator_index {.inject.} = withdrawal.validator_index

# Here, can't use the pre-stored effective balance because this template
# might be called on the next slot and therefore next epoch, after which
Expand All @@ -1471,7 +1507,7 @@ template get_expected_withdrawals_with_partial_count_aux*(
withdrawal.amount)
var w = Withdrawal(
index: withdrawal_index,
validator_index: withdrawal.index,
validator_index: withdrawal.validator_index,
amount: withdrawable_balance)
w.address.data[0..19] = validator.withdrawal_credentials.data[12..^1]
withdrawals.add w
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/spec/datatypes/base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export
tables, results, endians2, json_serialization, sszTypes, beacon_time, crypto,
digest, presets

const SPEC_VERSION* = "1.5.0-alpha.9"
const SPEC_VERSION* = "1.5.0-alpha.10"
## Spec version we're aiming to be compatible with, right now

const
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/spec/datatypes/electra.nim
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ type

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.7/specs/electra/beacon-chain.md#pendingpartialwithdrawal
PendingPartialWithdrawal* = object
index*: uint64
validator_index*: uint64
amount*: Gwei
withdrawable_epoch*: Epoch

Expand Down
57 changes: 43 additions & 14 deletions beacon_chain/spec/presets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ type
MIN_PER_EPOCH_CHURN_LIMIT*: uint64
CHURN_LIMIT_QUOTIENT*: uint64
MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT*: uint64
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA*: uint64
MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT*: uint64

# Fork choice
# TODO PROPOSER_SCORE_BOOST*: uint64
Expand Down Expand Up @@ -116,6 +114,13 @@ type
MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS*: uint64
# TODO BLOB_SIDECAR_SUBNET_COUNT*: uint64

# Electra
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA*: uint64
MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT*: uint64
BLOB_SIDECAR_SUBNET_COUNT_ELECTRA*: uint64
MAX_BLOBS_PER_BLOCK_ELECTRA*: uint64
MAX_REQUEST_BLOB_SIDECARS_ELECTRA*: uint64

PresetFile* = object
values*: Table[string, string]
missingValues*: seq[string]
Expand Down Expand Up @@ -233,10 +238,6 @@ when const_preset == "mainnet":
CHURN_LIMIT_QUOTIENT: 65536,
# [New in Deneb:EIP7514] 2**3 (= 8)
MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT: 8,
# [New in Electra:EIP7251] 2**7 * 10**9 (= 128,000,000,000)
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA: 128000000000'u64,
# [New in Electra:EIP7251] 2**8 * 10**9 (= 256,000,000,000)
MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 256000000000'u64,

# Deposit contract
# ---------------------------------------------------------------
Expand Down Expand Up @@ -283,6 +284,18 @@ when const_preset == "mainnet":
MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS: 4096,
# `6`
# TODO BLOB_SIDECAR_SUBNET_COUNT: 6,

# Electra
# 2**7 * 10**9 (= 128,000,000,000)
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA: 128000000000'u64,
# 2**8 * 10**9 (= 256,000,000,000)
MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 256000000000'u64,
# `9`
BLOB_SIDECAR_SUBNET_COUNT_ELECTRA: 9,
# `uint64(9)`
MAX_BLOBS_PER_BLOCK_ELECTRA: 9,
# MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK_ELECTRA
MAX_REQUEST_BLOB_SIDECARS_ELECTRA: 1152
)

elif const_preset == "gnosis":
Expand Down Expand Up @@ -385,10 +398,6 @@ elif const_preset == "gnosis":
CHURN_LIMIT_QUOTIENT: 4096,
# [New in Deneb:EIP7514] 2**3 (= 8)
MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT: 8,
# [New in Electra:EIP7251] 2**7 * 10**9 (= 128,000,000,000) (copied from EF mainnet)
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA: 128000000000'u64,
# [New in Electra:EIP7251] 2**8 * 10**9 (= 256,000,000,000) (copied from EF mainnet)
MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 256000000000'u64,

# Deposit contract
# ---------------------------------------------------------------
Expand Down Expand Up @@ -435,6 +444,18 @@ elif const_preset == "gnosis":
MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS: 16384,
# `6`
# TODO BLOB_SIDECAR_SUBNET_COUNT: 6,

# Electra
# 2**7 * 10**9 (= 128,000,000,000)
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA: 128000000000'u64,
# 2**8 * 10**9 (= 256,000,000,000)
MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 256000000000'u64,
# `9`
BLOB_SIDECAR_SUBNET_COUNT_ELECTRA: 9,
# `uint64(9)`
MAX_BLOBS_PER_BLOCK_ELECTRA: 9,
# MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK_ELECTRA
MAX_REQUEST_BLOB_SIDECARS_ELECTRA: 1152
)

elif const_preset == "minimal":
Expand Down Expand Up @@ -532,10 +553,6 @@ elif const_preset == "minimal":
CHURN_LIMIT_QUOTIENT: 32,
# [New in Deneb:EIP7514] [customized]
MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT: 4,
# [New in Electra:EIP7251] 2**6 * 10**9 (= 64,000,000,000)
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA: 64000000000'u64,
# [New in Electra:EIP7251] 2**7 * 10**9 (= 128,000,000,000)
MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 128000000000'u64,


# Deposit contract
Expand Down Expand Up @@ -584,6 +601,18 @@ elif const_preset == "minimal":
MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS: 4096,
# `6`
# TODO BLOB_SIDECAR_SUBNET_COUNT: 6,

# Electra
# [customized] 2**6 * 10**9 (= 64,000,000,000)
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA: 64000000000'u64,
# [customized] 2**7 * 10**9 (= 128,000,000,000)
MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 128000000000'u64,
# `9`
BLOB_SIDECAR_SUBNET_COUNT_ELECTRA: 9,
# `uint64(9)`
MAX_BLOBS_PER_BLOCK_ELECTRA: 9,
# MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK_ELECTRA
MAX_REQUEST_BLOB_SIDECARS_ELECTRA: 1152,
)

else:
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/spec/presets/gnosis/electra_preset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const
MAX_ATTESTER_SLASHINGS_ELECTRA*: uint64 = 1
# `uint64(2**3)` (= 8)
MAX_ATTESTATIONS_ELECTRA*: uint64 = 8
# `uint64(2**0)` (= 1)
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD*: uint64 = 1
# `uint64(2**1)` (= 2)
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD*: uint64 = 2

# Execution
# ---------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/spec/presets/mainnet/electra_preset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const
MAX_ATTESTER_SLASHINGS_ELECTRA*: uint64 = 1
# `uint64(2**3)` (= 8)
MAX_ATTESTATIONS_ELECTRA*: uint64 = 8
# `uint64(2**0)` (= 1)
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD*: uint64 = 1
# `uint64(2**1)` (= 2)
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD*: uint64 = 2

# Execution
# ---------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions beacon_chain/spec/presets/minimal/deneb_preset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const
# `uint64(4096)`
FIELD_ELEMENTS_PER_BLOB*: uint64 = 4096
# [customized]
MAX_BLOB_COMMITMENTS_PER_BLOCK*: uint64 = 16
MAX_BLOB_COMMITMENTS_PER_BLOCK*: uint64 = 32
# `uint64(6)`
MAX_BLOBS_PER_BLOCK*: uint64 = 6
# [customized] `floorlog2(get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 4 = 9
KZG_COMMITMENT_INCLUSION_PROOF_DEPTH* = 9
# [customized] `floorlog2(get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 5 = 10
KZG_COMMITMENT_INCLUSION_PROOF_DEPTH* = 10
4 changes: 2 additions & 2 deletions beacon_chain/spec/presets/minimal/electra_preset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const
MAX_ATTESTER_SLASHINGS_ELECTRA*: uint64 = 1
# `uint64(2**3)` (= 8)
MAX_ATTESTATIONS_ELECTRA*: uint64 = 8
# `uint64(2**0)` (= 1)
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD*: uint64 = 1
# `uint64(2**1)` (= 2)
MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD*: uint64 = 2

# Execution
# ---------------------------------------------------------------
Expand Down
24 changes: 11 additions & 13 deletions beacon_chain/spec/state_transition_block.nim
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func process_withdrawal_request*(

# In theory can fail, but failing/early returning here is indistinguishable
discard state.pending_partial_withdrawals.add(PendingPartialWithdrawal(
index: index.uint64,
validator_index: index.uint64,
amount: to_withdraw,
withdrawable_epoch: withdrawable_epoch,
))
Expand Down Expand Up @@ -647,8 +647,8 @@ func process_consolidation_request*(
if not (has_correct_credential and is_correct_source_address):
return

# Verify that target has execution withdrawal credentials
if not has_execution_withdrawal_credential(target_validator):
# Verify that target has compounding withdrawal credentials
if not has_compounding_withdrawal_credential(target_validator):
return

# Verify the source and the target are active
Expand Down Expand Up @@ -682,10 +682,6 @@ func process_consolidation_request*(
discard state.pending_consolidations.add(PendingConsolidation(
source_index: source_index.uint64, target_index: target_index.uint64))

# Churn any target excess active balance of target and raise its max
if has_eth1_withdrawal_credential(target_validator):
switch_to_compounding_validator(state, target_index)

type
# https://ethereum.github.io/beacon-APIs/?urls.primaryName=v2.5.0#/Rewards/getBlockRewards
BlockRewards* = object
Expand Down Expand Up @@ -1015,7 +1011,8 @@ type SomeElectraBeaconBlockBody =

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/electra/beacon-chain.md#modified-process_execution_payload
proc process_execution_payload*(
state: var electra.BeaconState, body: SomeElectraBeaconBlockBody,
cfg: RuntimeConfig, state: var electra.BeaconState,
body: SomeElectraBeaconBlockBody,
notify_new_payload: electra.ExecutePayload): Result[void, cstring] =
template payload: auto = body.execution_payload

Expand All @@ -1034,7 +1031,7 @@ proc process_execution_payload*(
return err("process_execution_payload: invalid timestamp")

# [New in Deneb] Verify commitments are under limit
if not (lenu64(body.blob_kzg_commitments) <= MAX_BLOBS_PER_BLOCK):
if not (lenu64(body.blob_kzg_commitments) <= cfg.MAX_BLOBS_PER_BLOCK_ELECTRA):
return err("process_execution_payload: too many KZG commitments")

# Verify the execution payload is valid
Expand Down Expand Up @@ -1070,7 +1067,8 @@ type SomeFuluBeaconBlockBody =

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/electra/beacon-chain.md#modified-process_execution_payload
proc process_execution_payload*(
state: var fulu.BeaconState, body: SomeFuluBeaconBlockBody,
cfg: RuntimeConfig, state: var fulu.BeaconState,
body: SomeFuluBeaconBlockBody,
notify_new_payload: fulu.ExecutePayload): Result[void, cstring] =
template payload: auto = body.execution_payload

Expand All @@ -1089,7 +1087,7 @@ proc process_execution_payload*(
return err("process_execution_payload: invalid timestamp")

# [New in Deneb] Verify commitments are under limit
if not (lenu64(body.blob_kzg_commitments) <= MAX_BLOBS_PER_BLOCK):
if not (lenu64(body.blob_kzg_commitments) <= cfg.MAX_BLOBS_PER_BLOCK_ELECTRA):
return err("process_execution_payload: too many KZG commitments")

# Verify the execution payload is valid
Expand Down Expand Up @@ -1364,7 +1362,7 @@ proc process_block*(
if is_execution_enabled(state, blck.body):
? process_withdrawals(state, blck.body.execution_payload)
? process_execution_payload(
state, blck.body,
cfg, state, blck.body,
func(_: electra.ExecutionPayload): bool = true)
? process_randao(state, blck.body, flags, cache)
? process_eth1_data(state, blck.body)
Expand Down Expand Up @@ -1397,7 +1395,7 @@ proc process_block*(
if is_execution_enabled(state, blck.body):
? process_withdrawals(state, blck.body.execution_payload)
? process_execution_payload(
state, blck.body,
cfg, state, blck.body,
func(_: fulu.ExecutionPayload): bool = true)
? process_randao(state, blck.body, flags, cache)
? process_eth1_data(state, blck.body)
Expand Down
Loading

0 comments on commit 7cb0a61

Please sign in to comment.