Apply suggestions from code review

Co-authored-by: Caspar Schwarz-Schilling <31305984+casparschwa@users.noreply.github.com>
This commit is contained in:
Hsiao-Wei Wang 2021-12-11 09:01:16 +08:00 committed by GitHub
parent 11a42f8d06
commit 4cea972cc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,14 +33,19 @@ def _apply_base_block_a(spec, state, store, test_steps):
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_ex_ante_vanilla_with_boost(spec, state): def test_ex_ante_vanilla(spec, state):
""" """
With a single adversarial attestation With a single adversarial attestation
Objects:
Block A - slot N Block A - slot N
Block B (parent A) - slot N+1 Block B (parent A) - slot N+1
Block C (parent A) - slot N+2 Block C (parent A) - slot N+2
Attestation_1 (Block B) - slot N+1 size 1 Attestation_1 (Block B); size `1` - slot N+1
Steps:
Block A received at N A is head
Block C received at N+2 C is head
Block B received at N+2 C is head
Attestation_1 received at N+2 C is head
""" """
test_steps = [] test_steps = []
# Initialization # Initialization
@ -65,7 +70,7 @@ def test_ex_ante_vanilla_with_boost(spec, state):
block = build_empty_block(spec, state_c, slot=state_a.slot + 2) block = build_empty_block(spec, state_c, slot=state_a.slot + 2)
signed_block_c = state_transition_and_sign_block(spec, state_c, block) signed_block_c = state_transition_and_sign_block(spec, state_c, block)
# Attestation_1 received at N+2 — B is head due to boost proposer # Attestation_1 at slot `N + 1` voting for block B
def _filter_participant_set(participants): def _filter_participant_set(participants):
return [next(iter(participants))] return [next(iter(participants))]
@ -82,7 +87,7 @@ def test_ex_ante_vanilla_with_boost(spec, state):
yield from add_block(spec, store, signed_block_c, test_steps) yield from add_block(spec, store, signed_block_c, test_steps)
assert spec.get_head(store) == signed_block_c.message.hash_tree_root() assert spec.get_head(store) == signed_block_c.message.hash_tree_root()
# Block B received at N+2 — C is head that has higher proposer score boost # Block B received at N+2 — C is head due to proposer score boost
yield from add_block(spec, store, signed_block_b, test_steps) yield from add_block(spec, store, signed_block_b, test_steps)
assert spec.get_head(store) == signed_block_c.message.hash_tree_root() assert spec.get_head(store) == signed_block_c.message.hash_tree_root()
@ -119,11 +124,16 @@ def _get_greater_than_proposer_boost_score(spec, store, state, proposer_boost_ro
def test_ex_ante_attestations_is_greater_than_proposer_boost_with_boost(spec, state): def test_ex_ante_attestations_is_greater_than_proposer_boost_with_boost(spec, state):
""" """
Adversarial attestations > proposer boost Adversarial attestations > proposer boost
Objects:
Block A - slot N Block A - slot N
Block B (parent A) - slot N+1 Block B (parent A) - slot N+1
Block C (parent A) - slot N+2 Block C (parent A) - slot N+2
Attestation_1 (Block B) - slot N+1 proposer_boost + 1 participants Attestation_set_1 (Block B); size `proposer_boost + 1` - slot N+1
Steps:
Block A received at N A is head
Block C received at N+2 C is head
Block B received at N+2 C is head
Attestation_1 received at N+2 B is head
""" """
test_steps = [] test_steps = []
# Initialization # Initialization
@ -154,11 +164,11 @@ def test_ex_ante_attestations_is_greater_than_proposer_boost_with_boost(spec, st
yield from add_block(spec, store, signed_block_c, test_steps) yield from add_block(spec, store, signed_block_c, test_steps)
assert spec.get_head(store) == signed_block_c.message.hash_tree_root() assert spec.get_head(store) == signed_block_c.message.hash_tree_root()
# Block B received at N+2 — C is head that has higher proposer score boost # Block B received at N+2 — C is head due to proposer score boost
yield from add_block(spec, store, signed_block_b, test_steps) yield from add_block(spec, store, signed_block_b, test_steps)
assert spec.get_head(store) == signed_block_c.message.hash_tree_root() assert spec.get_head(store) == signed_block_c.message.hash_tree_root()
# Attestation of proposer_boost + 1 participants # Attestation_set_1 at slot `N + 1` voting for block B
proposer_boost_root = signed_block_b.message.hash_tree_root() proposer_boost_root = signed_block_b.message.hash_tree_root()
root = signed_block_b.message.hash_tree_root() root = signed_block_b.message.hash_tree_root()
participant_num = _get_greater_than_proposer_boost_score(spec, store, state, proposer_boost_root, root) participant_num = _get_greater_than_proposer_boost_score(spec, store, state, proposer_boost_root, root)
@ -173,7 +183,7 @@ def test_ex_ante_attestations_is_greater_than_proposer_boost_with_boost(spec, st
assert len([i for i in attestation.aggregation_bits if i == 1]) == participant_num assert len([i for i in attestation.aggregation_bits if i == 1]) == participant_num
sign_attestation(spec, state_b, attestation) sign_attestation(spec, state_b, attestation)
# Attestation_1 received at N+2 — B is head because B's attestation_score > C's proposer_score. # Attestation_set_1 received at N+2 — B is head because B's attestation_score > C's proposer_score.
# (B's proposer_score = C's attestation_score = 0) # (B's proposer_score = C's attestation_score = 0)
yield from add_attestation(spec, store, attestation, test_steps) yield from add_attestation(spec, store, attestation, test_steps)
assert spec.get_head(store) == signed_block_b.message.hash_tree_root() assert spec.get_head(store) == signed_block_b.message.hash_tree_root()
@ -183,7 +193,7 @@ def test_ex_ante_attestations_is_greater_than_proposer_boost_with_boost(spec, st
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_ex_ante_sandwich_without_attestations_with_boost(spec, state): def test_ex_ante_sandwich_without_attestations(spec, state):
""" """
Simple Sandwich test with boost and no attestations. Simple Sandwich test with boost and no attestations.
Obejcts: Obejcts:
@ -246,7 +256,7 @@ def test_ex_ante_sandwich_without_attestations_with_boost(spec, state):
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_ex_ante_sandwich_with_honest_attestation_with_boost(spec, state): def test_ex_ante_sandwich_with_honest_attestation(spec, state):
""" """
Boosting necessary to sandwich attack. Boosting necessary to sandwich attack.
Objects: Objects: