From 0bc0769767287e33277b6e346463c6431249a761 Mon Sep 17 00:00:00 2001 From: realbigsean Date: Tue, 30 Nov 2021 15:44:56 -0500 Subject: [PATCH 1/7] Simplify logic in calculating proposer score --- specs/phase0/fork-choice.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/specs/phase0/fork-choice.md b/specs/phase0/fork-choice.md index d082ede30..279a08f8c 100644 --- a/specs/phase0/fork-choice.md +++ b/specs/phase0/fork-choice.md @@ -182,14 +182,12 @@ def get_latest_attesting_balance(store: Store, root: Root) -> Gwei: and get_ancestor(store, store.latest_messages[i].root, store.blocks[root].slot) == root) )) proposer_score = Gwei(0) - if store.proposer_boost_root != Root(): - block = store.blocks[root] - if get_ancestor(store, root, block.slot) == store.proposer_boost_root: - num_validators = len(get_active_validator_indices(state, get_current_epoch(state))) - avg_balance = get_total_active_balance(state) // num_validators - committee_size = num_validators // SLOTS_PER_EPOCH - committee_weight = committee_size * avg_balance - proposer_score = (committee_weight * PROPOSER_SCORE_BOOST) // 100 + if store.proposer_boost_root != Root() and root == store.proposer_boost_root: + num_validators = len(get_active_validator_indices(state, get_current_epoch(state))) + avg_balance = get_total_active_balance(state) // num_validators + committee_size = num_validators // SLOTS_PER_EPOCH + committee_weight = committee_size * avg_balance + proposer_score = (committee_weight * PROPOSER_SCORE_BOOST) // 100 return attestation_score + proposer_score ``` From ad588d79f6aa25e0974f7e47afff52311f1f188a Mon Sep 17 00:00:00 2001 From: dankrad Date: Tue, 30 Nov 2021 22:52:16 +0000 Subject: [PATCH 2/7] Update beacon-chain.md 5 is not a primitive root of the BLS field, it needs to be 7 --- specs/sharding/beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/sharding/beacon-chain.md b/specs/sharding/beacon-chain.md index 8190951b4..cf3d7bea1 100644 --- a/specs/sharding/beacon-chain.md +++ b/specs/sharding/beacon-chain.md @@ -101,7 +101,7 @@ The following values are (non-configurable) constants used throughout the specif | Name | Value | Notes | | - | - | - | -| `PRIMITIVE_ROOT_OF_UNITY` | `5` | Primitive root of unity of the BLS12_381 (inner) modulus | +| `PRIMITIVE_ROOT_OF_UNITY` | `7` | Primitive root of unity of the BLS12_381 (inner) modulus | | `DATA_AVAILABILITY_INVERSE_CODING_RATE` | `2**1` (= 2) | Factor by which samples are extended for data availability encoding | | `POINTS_PER_SAMPLE` | `uint64(2**3)` (= 8) | 31 * 8 = 248 bytes | | `MODULUS` | `0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001` (curve order of BLS12_381) | From 3e2890bb3e7b586f54d71b0958caef4347b0e644 Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Wed, 1 Dec 2021 18:03:40 -0800 Subject: [PATCH 3/7] Apply proposer boost to ancestors correctly --- specs/phase0/fork-choice.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/specs/phase0/fork-choice.md b/specs/phase0/fork-choice.md index 279a08f8c..281bf4e4d 100644 --- a/specs/phase0/fork-choice.md +++ b/specs/phase0/fork-choice.md @@ -182,12 +182,13 @@ def get_latest_attesting_balance(store: Store, root: Root) -> Gwei: and get_ancestor(store, store.latest_messages[i].root, store.blocks[root].slot) == root) )) proposer_score = Gwei(0) - if store.proposer_boost_root != Root() and root == store.proposer_boost_root: - num_validators = len(get_active_validator_indices(state, get_current_epoch(state))) - avg_balance = get_total_active_balance(state) // num_validators - committee_size = num_validators // SLOTS_PER_EPOCH - committee_weight = committee_size * avg_balance - proposer_score = (committee_weight * PROPOSER_SCORE_BOOST) // 100 + if store.proposer_boost_root != Root(): + if get_ancestor(store, store.proposer_boost_root, store.blocks[root].slot) == root: + num_validators = len(get_active_validator_indices(state, get_current_epoch(state))) + avg_balance = get_total_active_balance(state) // num_validators + committee_size = num_validators // SLOTS_PER_EPOCH + committee_weight = committee_size * avg_balance + proposer_score = (committee_weight * PROPOSER_SCORE_BOOST) // 100 return attestation_score + proposer_score ``` From 6f3379c7bee3da83caaa4da347e4ef8801e2a257 Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Wed, 1 Dec 2021 20:11:52 -0800 Subject: [PATCH 4/7] Apply HWW's suggestion Co-authored-by: Hsiao-Wei Wang --- specs/phase0/fork-choice.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/specs/phase0/fork-choice.md b/specs/phase0/fork-choice.md index 281bf4e4d..497c06dc6 100644 --- a/specs/phase0/fork-choice.md +++ b/specs/phase0/fork-choice.md @@ -182,13 +182,15 @@ def get_latest_attesting_balance(store: Store, root: Root) -> Gwei: and get_ancestor(store, store.latest_messages[i].root, store.blocks[root].slot) == root) )) proposer_score = Gwei(0) - if store.proposer_boost_root != Root(): - if get_ancestor(store, store.proposer_boost_root, store.blocks[root].slot) == root: - num_validators = len(get_active_validator_indices(state, get_current_epoch(state))) - avg_balance = get_total_active_balance(state) // num_validators - committee_size = num_validators // SLOTS_PER_EPOCH - committee_weight = committee_size * avg_balance - proposer_score = (committee_weight * PROPOSER_SCORE_BOOST) // 100 + if ( + store.proposer_boost_root != Root() + and get_ancestor(store, store.proposer_boost_root, store.blocks[root].slot) == root + ): + num_validators = len(get_active_validator_indices(state, get_current_epoch(state))) + avg_balance = get_total_active_balance(state) // num_validators + committee_size = num_validators // SLOTS_PER_EPOCH + committee_weight = committee_size * avg_balance + proposer_score = (committee_weight * PROPOSER_SCORE_BOOST) // 100 return attestation_score + proposer_score ``` From a399d953d3c2e32744c94eb6397a6454dbac7775 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 Dec 2021 17:21:42 +0800 Subject: [PATCH 5/7] Add assertion to `test_shorter_chain_but_heavier_weight` --- .../pyspec/eth2spec/test/phase0/fork_choice/test_get_head.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_get_head.py b/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_get_head.py index d2c84fce7..a524cbd56 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_get_head.py +++ b/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_get_head.py @@ -166,6 +166,9 @@ def test_shorter_chain_but_heavier_weight(spec, state): signed_short_block = state_transition_and_sign_block(spec, short_state, short_block) yield from tick_and_add_block(spec, store, signed_short_block, test_steps) + # Since the long chain has higher proposer_score at slot 1, the latest long block is the head + assert spec.get_head(store) == spec.hash_tree_root(long_block) + short_attestation = get_valid_attestation(spec, short_state, short_block.slot, signed=True) yield from tick_and_run_on_attestation(spec, store, short_attestation, test_steps) From 0d8fab3986e3338c76747e1582aa53b5707c345b Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Thu, 2 Dec 2021 09:57:28 -0800 Subject: [PATCH 6/7] Apply Danny's suggestion --- specs/phase0/fork-choice.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/specs/phase0/fork-choice.md b/specs/phase0/fork-choice.md index 497c06dc6..fe546142b 100644 --- a/specs/phase0/fork-choice.md +++ b/specs/phase0/fork-choice.md @@ -181,11 +181,13 @@ def get_latest_attesting_balance(store: Store, root: Root) -> Gwei: if (i in store.latest_messages and get_ancestor(store, store.latest_messages[i].root, store.blocks[root].slot) == root) )) + if store.proposer_boost_root == Root(): + # Return only attestation score if ``proposer_boost_root`` is not set + return attestation_score + # Calculate proposer score if ``proposer_boost_root`` is set proposer_score = Gwei(0) - if ( - store.proposer_boost_root != Root() - and get_ancestor(store, store.proposer_boost_root, store.blocks[root].slot) == root - ): + # Boost is applied if ``root`` is an ancestor of ``proposer_boost_root`` + if get_ancestor(store, store.proposer_boost_root, store.blocks[root].slot) == root: num_validators = len(get_active_validator_indices(state, get_current_epoch(state))) avg_balance = get_total_active_balance(state) // num_validators committee_size = num_validators // SLOTS_PER_EPOCH From 3a9777eebc0bc0aedf3f2286a5282785dad97341 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Thu, 2 Dec 2021 14:00:06 -0700 Subject: [PATCH 7/7] Update specs/phase0/fork-choice.md --- specs/phase0/fork-choice.md | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/phase0/fork-choice.md b/specs/phase0/fork-choice.md index fe546142b..d3b0e2dc5 100644 --- a/specs/phase0/fork-choice.md +++ b/specs/phase0/fork-choice.md @@ -184,6 +184,7 @@ def get_latest_attesting_balance(store: Store, root: Root) -> Gwei: if store.proposer_boost_root == Root(): # Return only attestation score if ``proposer_boost_root`` is not set return attestation_score + # Calculate proposer score if ``proposer_boost_root`` is set proposer_score = Gwei(0) # Boost is applied if ``root`` is an ancestor of ``proposer_boost_root``