From 3e2890bb3e7b586f54d71b0958caef4347b0e644 Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Wed, 1 Dec 2021 18:03:40 -0800 Subject: [PATCH 1/5] 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 279a08f8ca..281bf4e4d3 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 2/5] 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 281bf4e4d3..497c06dc6d 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 3/5] 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 d2c84fce79..a524cbd565 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 4/5] 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 497c06dc6d..fe546142b4 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 5/5] 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 fe546142b4..d3b0e2dc54 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``