Skip to content

Commit

Permalink
Add test_justify_previous_epoch_with_late_block to improve coverage (
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Jan 18, 2023
1 parent 0edb2a0 commit 9345c02
Showing 1 changed file with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,58 @@ def test_proposer_boost_root_same_slot_untimely_block(spec, state):
})

yield 'steps', test_steps


@with_all_phases
@spec_state_test
@with_presets([MINIMAL], reason="too slow")
def test_justify_previous_epoch_with_late_block(spec, state):
test_steps = []
# Initialization
store, anchor_block = get_genesis_forkchoice_store_and_block(spec, state)
yield 'anchor_state', state
yield 'anchor_block', anchor_block
current_time = state.slot * spec.config.SECONDS_PER_SLOT + store.genesis_time
on_tick_and_append_step(spec, store, current_time, test_steps)
assert store.time == current_time

for _ in range(2):
next_epoch(spec, state)

for _ in range(2):
state, store, _ = yield from apply_next_epoch_with_attestations(
spec, state, store, True, True, test_steps=test_steps)

assert state.current_justified_checkpoint.epoch == store.justified_checkpoint.epoch == 3
assert spec.get_current_epoch(state) == 4

# Move forward time to later epoch, and such that
# `compute_slots_since_epoch_start(get_current_slot(store)) == SAFE_SLOTS_TO_UPDATE_JUSTIFIED`
slot = spec.SLOTS_PER_EPOCH * 6 + spec.SAFE_SLOTS_TO_UPDATE_JUSTIFIED
on_tick_and_append_step(spec, store, store.genesis_time + slot * spec.config.SECONDS_PER_SLOT, test_steps)

next_slots(spec, state, spec.SLOTS_PER_EPOCH // 2)

# Now try to justify the checkpoint at epoch 4
# At the second half of the epoch, attest the current justification checkpoint
state, store, _ = yield from apply_next_slots_with_attestations(
spec, state, store, spec.SLOTS_PER_EPOCH // 2 - 1, True, False, test_steps)

assert spec.get_current_epoch(state) == 4
# skip last slot
next_slots(spec, state, 1)
# ensure that it passed epoch boundary
assert spec.get_current_epoch(state) == 5
assert state.current_justified_checkpoint.epoch == 3
assert store.justified_checkpoint.epoch == 3

# At the first half of the epoch, attest the *previous* justification checkpoint
state, store, _ = yield from apply_next_slots_with_attestations(
spec, state, store, spec.SLOTS_PER_EPOCH // 2, False, True, test_steps)

# During the above execution, `should_update_justified_checkpoint` function returns `True`
# via the last line of `should_update_justified_checkpoint`.
assert state.current_justified_checkpoint.epoch == 3
assert store.justified_checkpoint.epoch == 4

yield 'steps', test_steps

0 comments on commit 9345c02

Please sign in to comment.