From 74be5b243671c90f5b2c66f36e8d83e2b5d884dc Mon Sep 17 00:00:00 2001 From: Mark Mackey Date: Thu, 16 May 2024 15:29:32 +0300 Subject: [PATCH 1/3] Electra: Properly Calculate Proposer Probabilities --- specs/electra/beacon-chain.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/specs/electra/beacon-chain.md b/specs/electra/beacon-chain.md index 551a41af1..5c3c67dd2 100644 --- a/specs/electra/beacon-chain.md +++ b/specs/electra/beacon-chain.md @@ -41,6 +41,7 @@ - [`BeaconState`](#beaconstate) - [Helper functions](#helper-functions) - [Predicates](#predicates) + - [Updated `compute_proposer_index`](#updated-compute_proposer_index) - [Updated `is_eligible_for_activation_queue`](#updated-is_eligible_for_activation_queue) - [New `is_compounding_withdrawal_credential`](#new-is_compounding_withdrawal_credential) - [New `has_compounding_withdrawal_credential`](#new-has_compounding_withdrawal_credential) @@ -431,6 +432,26 @@ class BeaconState(Container): ### Predicates +#### Updated `compute_proposer_index` + +```python +def compute_proposer_index(state: BeaconState, indices: Sequence[ValidatorIndex], seed: Bytes32) -> ValidatorIndex: + """ + Return from ``indices`` a random index sampled by effective balance. + """ + assert len(indices) > 0 + MAX_RANDOM_BYTE = 2**8 - 1 + i = uint64(0) + total = uint64(len(indices)) + while True: + candidate_index = indices[compute_shuffled_index(i % total, total, seed)] + random_byte = hash(seed + uint_to_bytes(uint64(i // 32)))[i % 32] + effective_balance = state.validators[candidate_index].effective_balance + if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE_ELECTRA * random_byte: #[Modified in Electra:EIP7251] + return candidate_index + i += 1 +``` + #### Updated `is_eligible_for_activation_queue` ```python From 222e980b7ef4eba949f9b1d3ba8814e5ce1224b0 Mon Sep 17 00:00:00 2001 From: Mikhail Kalinin Date: Thu, 16 May 2024 16:55:23 +0300 Subject: [PATCH 2/3] Fix lint --- specs/electra/beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/electra/beacon-chain.md b/specs/electra/beacon-chain.md index 5c3c67dd2..309e8a1b7 100644 --- a/specs/electra/beacon-chain.md +++ b/specs/electra/beacon-chain.md @@ -447,7 +447,7 @@ def compute_proposer_index(state: BeaconState, indices: Sequence[ValidatorIndex] candidate_index = indices[compute_shuffled_index(i % total, total, seed)] random_byte = hash(seed + uint_to_bytes(uint64(i // 32)))[i % 32] effective_balance = state.validators[candidate_index].effective_balance - if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE_ELECTRA * random_byte: #[Modified in Electra:EIP7251] + if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE_ELECTRA * random_byte: # [Modified in Electra:EIP7251] return candidate_index i += 1 ``` From 32b441d381d45f84ce30d289f02b36c3353ad9a8 Mon Sep 17 00:00:00 2001 From: Mikhail Kalinin Date: Thu, 16 May 2024 17:03:10 +0300 Subject: [PATCH 3/3] Fix lint 2 --- specs/electra/beacon-chain.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/specs/electra/beacon-chain.md b/specs/electra/beacon-chain.md index 309e8a1b7..b892acf82 100644 --- a/specs/electra/beacon-chain.md +++ b/specs/electra/beacon-chain.md @@ -447,7 +447,8 @@ def compute_proposer_index(state: BeaconState, indices: Sequence[ValidatorIndex] candidate_index = indices[compute_shuffled_index(i % total, total, seed)] random_byte = hash(seed + uint_to_bytes(uint64(i // 32)))[i % 32] effective_balance = state.validators[candidate_index].effective_balance - if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE_ELECTRA * random_byte: # [Modified in Electra:EIP7251] + # [Modified in Electra:EIP7251] + if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE_ELECTRA * random_byte: return candidate_index i += 1 ```