From 9eb662786adebce7c3e41852a525458895e8bedc Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Thu, 1 Apr 2021 06:29:01 -0700 Subject: [PATCH 1/7] Add consistency checks in on_block tests --- .../test/phase0/unittests/fork_choice/test_on_block.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py index 7fa32b86c..bb5aec578 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py +++ b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py @@ -234,6 +234,9 @@ def test_on_block_outside_safe_slots_and_multiple_better_justified(spec, state): just_block.slot = spec.compute_start_slot_at_epoch(store.justified_checkpoint.epoch) store.blocks[just_block.hash_tree_root()] = just_block + just_block_parent = store.blocks[just_block.parent_root] + assert just_block_parent.slot < just_block.slot, f"just_block_parent.slot: {just_block_parent.slot}, just_block.slot: {just_block.slot}" + # Step time past safe slots spec.on_tick(store, store.time + spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED * spec.SECONDS_PER_SLOT) assert spec.get_current_slot(store) % spec.SLOTS_PER_EPOCH >= spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED @@ -284,6 +287,9 @@ def test_on_block_outside_safe_slots_but_finality(spec, state): just_block.slot = spec.compute_start_slot_at_epoch(store.justified_checkpoint.epoch) store.blocks[just_block.hash_tree_root()] = just_block + just_block_parent = store.blocks[just_block.parent_root] + assert just_block_parent.slot < just_block.slot, f"just_block_parent.slot: {just_block_parent.slot}, just_block.slot: {just_block.slot}" + # Step time past safe slots spec.on_tick(store, store.time + spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED * spec.SECONDS_PER_SLOT) assert spec.get_current_slot(store) % spec.SLOTS_PER_EPOCH >= spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED From 2478e1526a464dd9d93dbce88d98452452ec01dc Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Sat, 3 Apr 2021 16:50:43 -0700 Subject: [PATCH 2/7] Fix block hierarchy consistency in tests --- .../unittests/fork_choice/test_on_block.py | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py index bb5aec578..79c55b2eb 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py +++ b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py @@ -224,14 +224,19 @@ def test_on_block_outside_safe_slots_and_multiple_better_justified(spec, state): next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) state, store, last_signed_block = apply_next_epoch_with_attestations(spec, state, store) + last_block_root = hash_tree_root(last_signed_block.message) + + # Mock ficticious justified checkpoint in store + store.justified_checkpoint = spec.Checkpoint( + epoch=spec.compute_epoch_at_slot(last_signed_block.message.slot), + root=spec.Root("0x4a55535449464945440000000000000000000000000000000000000000000000") + ) + next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) - last_block_root = hash_tree_root(last_signed_block.message) - # Mock justified block in store + # Create new higher justified checkpoint not in branch of store's justified checkpoint just_block = build_empty_block_for_next_slot(spec, state) - # Slot is same as justified checkpoint so does not trigger an override in the store - just_block.slot = spec.compute_start_slot_at_epoch(store.justified_checkpoint.epoch) store.blocks[just_block.hash_tree_root()] = just_block just_block_parent = store.blocks[just_block.parent_root] @@ -271,20 +276,25 @@ def test_on_block_outside_safe_slots_and_multiple_better_justified(spec, state): def test_on_block_outside_safe_slots_but_finality(spec, state): # Initialization store = get_genesis_forkchoice_store(spec, state) - time = 100 + time = 0 spec.on_tick(store, time) next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) state, store, last_signed_block = apply_next_epoch_with_attestations(spec, state, store) + last_block_root = hash_tree_root(last_signed_block.message) + + # Mock ficticious justified checkpoint in store + store.justified_checkpoint = spec.Checkpoint( + epoch=spec.compute_epoch_at_slot(last_signed_block.message.slot), + root=spec.Root("0x4a55535449464945440000000000000000000000000000000000000000000000") + ) + next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) - last_block_root = hash_tree_root(last_signed_block.message) - # Mock justified block in store + # Create new higher justified checkpoint not in branch of store's justified checkpoint just_block = build_empty_block_for_next_slot(spec, state) - # Slot is same as justified checkpoint so does not trigger an override in the store - just_block.slot = spec.compute_start_slot_at_epoch(store.justified_checkpoint.epoch) store.blocks[just_block.hash_tree_root()] = just_block just_block_parent = store.blocks[just_block.parent_root] From 21b878364a615ad0a5938ae566a416b10e98393d Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Sat, 3 Apr 2021 17:09:08 -0700 Subject: [PATCH 3/7] Fix checkpoint hierarchy consistency in tests --- .../test/phase0/unittests/fork_choice/test_on_block.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py index 79c55b2eb..536241bf8 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py +++ b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py @@ -307,13 +307,15 @@ def test_on_block_outside_safe_slots_but_finality(spec, state): # Mock justified and finalized update in state just_fin_state = store.block_states[last_block_root] new_justified = spec.Checkpoint( - epoch=store.justified_checkpoint.epoch + 1, + epoch=spec.compute_epoch_at_slot(just_block.slot) + 1, root=just_block.hash_tree_root(), ) + assert new_justified.epoch > store.justified_checkpoint.epoch new_finalized = spec.Checkpoint( - epoch=store.finalized_checkpoint.epoch + 1, + epoch=spec.compute_epoch_at_slot(just_block.slot), root=just_block.parent_root, ) + assert new_finalized.epoch > store.finalized_checkpoint.epoch just_fin_state.current_justified_checkpoint = new_justified just_fin_state.finalized_checkpoint = new_finalized From 6e6afac86f819d2237cb349c8ee51af3a4503c9f Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Sat, 3 Apr 2021 17:17:43 -0700 Subject: [PATCH 4/7] Remove unnecessary asserts --- .../test/phase0/unittests/fork_choice/test_on_block.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py index 536241bf8..00ea5f172 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py +++ b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py @@ -239,9 +239,6 @@ def test_on_block_outside_safe_slots_and_multiple_better_justified(spec, state): just_block = build_empty_block_for_next_slot(spec, state) store.blocks[just_block.hash_tree_root()] = just_block - just_block_parent = store.blocks[just_block.parent_root] - assert just_block_parent.slot < just_block.slot, f"just_block_parent.slot: {just_block_parent.slot}, just_block.slot: {just_block.slot}" - # Step time past safe slots spec.on_tick(store, store.time + spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED * spec.SECONDS_PER_SLOT) assert spec.get_current_slot(store) % spec.SLOTS_PER_EPOCH >= spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED @@ -297,9 +294,6 @@ def test_on_block_outside_safe_slots_but_finality(spec, state): just_block = build_empty_block_for_next_slot(spec, state) store.blocks[just_block.hash_tree_root()] = just_block - just_block_parent = store.blocks[just_block.parent_root] - assert just_block_parent.slot < just_block.slot, f"just_block_parent.slot: {just_block_parent.slot}, just_block.slot: {just_block.slot}" - # Step time past safe slots spec.on_tick(store, store.time + spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED * spec.SECONDS_PER_SLOT) assert spec.get_current_slot(store) % spec.SLOTS_PER_EPOCH >= spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED From 9ec252e6f2244e6f0722335869250df6bf87f64d Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Sat, 3 Apr 2021 17:22:33 -0700 Subject: [PATCH 5/7] Fix linter --- .../test/phase0/unittests/fork_choice/test_on_block.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py index 00ea5f172..3c1802ca2 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py +++ b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py @@ -225,13 +225,13 @@ def test_on_block_outside_safe_slots_and_multiple_better_justified(spec, state): spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) state, store, last_signed_block = apply_next_epoch_with_attestations(spec, state, store) last_block_root = hash_tree_root(last_signed_block.message) - + # Mock ficticious justified checkpoint in store store.justified_checkpoint = spec.Checkpoint( epoch=spec.compute_epoch_at_slot(last_signed_block.message.slot), root=spec.Root("0x4a55535449464945440000000000000000000000000000000000000000000000") ) - + next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) @@ -280,13 +280,13 @@ def test_on_block_outside_safe_slots_but_finality(spec, state): spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) state, store, last_signed_block = apply_next_epoch_with_attestations(spec, state, store) last_block_root = hash_tree_root(last_signed_block.message) - + # Mock ficticious justified checkpoint in store store.justified_checkpoint = spec.Checkpoint( epoch=spec.compute_epoch_at_slot(last_signed_block.message.slot), root=spec.Root("0x4a55535449464945440000000000000000000000000000000000000000000000") ) - + next_epoch(spec, state) spec.on_tick(store, store.time + state.slot * spec.SECONDS_PER_SLOT) From 5194dd123cdb52c1616cd11a0eeee39ed7330f27 Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Sat, 3 Apr 2021 17:26:12 -0700 Subject: [PATCH 6/7] TIL fictitious --- .../test/phase0/unittests/fork_choice/test_on_block.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py index 3c1802ca2..0cc8c33c9 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py +++ b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py @@ -226,7 +226,7 @@ def test_on_block_outside_safe_slots_and_multiple_better_justified(spec, state): state, store, last_signed_block = apply_next_epoch_with_attestations(spec, state, store) last_block_root = hash_tree_root(last_signed_block.message) - # Mock ficticious justified checkpoint in store + # Mock fictitious justified checkpoint in store store.justified_checkpoint = spec.Checkpoint( epoch=spec.compute_epoch_at_slot(last_signed_block.message.slot), root=spec.Root("0x4a55535449464945440000000000000000000000000000000000000000000000") @@ -281,7 +281,7 @@ def test_on_block_outside_safe_slots_but_finality(spec, state): state, store, last_signed_block = apply_next_epoch_with_attestations(spec, state, store) last_block_root = hash_tree_root(last_signed_block.message) - # Mock ficticious justified checkpoint in store + # Mock fictitious justified checkpoint in store store.justified_checkpoint = spec.Checkpoint( epoch=spec.compute_epoch_at_slot(last_signed_block.message.slot), root=spec.Root("0x4a55535449464945440000000000000000000000000000000000000000000000") From 9e5ee0a083c9df9c4643e2220e7de26dbe5c6cd5 Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Sat, 3 Apr 2021 17:43:18 -0700 Subject: [PATCH 7/7] Remove unnecesssary change --- .../eth2spec/test/phase0/unittests/fork_choice/test_on_block.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py index 0cc8c33c9..48ea78e39 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py +++ b/tests/core/pyspec/eth2spec/test/phase0/unittests/fork_choice/test_on_block.py @@ -273,7 +273,7 @@ def test_on_block_outside_safe_slots_and_multiple_better_justified(spec, state): def test_on_block_outside_safe_slots_but_finality(spec, state): # Initialization store = get_genesis_forkchoice_store(spec, state) - time = 0 + time = 100 spec.on_tick(store, time) next_epoch(spec, state)