ensure the best better-justification is stored in fork choice

This commit is contained in:
Danny Ryan 2019-12-09 16:47:43 -07:00
parent cf5b48ff21
commit d4d17ecc4e
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
2 changed files with 27 additions and 16 deletions

View File

@ -281,6 +281,7 @@ def on_block(store: Store, signed_block: SignedBeaconBlock) -> None:
# Update justified checkpoint # Update justified checkpoint
if state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch: if state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch:
if state.current_justified_checkpoint.epoch > store.best_justified_checkpoint.epoch:
store.best_justified_checkpoint = state.current_justified_checkpoint store.best_justified_checkpoint = state.current_justified_checkpoint
if should_update_justified_checkpoint(store, state.current_justified_checkpoint): if should_update_justified_checkpoint(store, state.current_justified_checkpoint):
store.justified_checkpoint = state.current_justified_checkpoint store.justified_checkpoint = state.current_justified_checkpoint

View File

@ -168,7 +168,7 @@ def test_on_block_update_justified_checkpoint_within_safe_slots(spec, state):
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_on_block_outside_safe_slots_and_old_block(spec, state): def test_on_block_outside_safe_slots_and_multiple_better_justified(spec, state):
# Initialization # Initialization
store = spec.get_genesis_store(state) store = spec.get_genesis_store(state)
time = 100 time = 100
@ -187,20 +187,30 @@ def test_on_block_outside_safe_slots_and_old_block(spec, state):
just_block.slot = spec.compute_start_slot_at_epoch(store.justified_checkpoint.epoch) just_block.slot = spec.compute_start_slot_at_epoch(store.justified_checkpoint.epoch)
store.blocks[just_block.hash_tree_root()] = just_block store.blocks[just_block.hash_tree_root()] = just_block
# Mock the justified checkpoint # 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
previously_justified = store.justified_checkpoint
# Add a series of new blocks with "better" justifications
best_justified_checkpoint = spec.Checkpoint(epoch=0)
for i in range(3, 0, -1):
just_state = store.block_states[last_block_root] just_state = store.block_states[last_block_root]
new_justified = spec.Checkpoint( new_justified = spec.Checkpoint(
epoch=just_state.current_justified_checkpoint.epoch + 1, epoch=previously_justified.epoch + i,
root=just_block.hash_tree_root(), root=just_block.hash_tree_root(),
) )
if new_justified.epoch > best_justified_checkpoint.epoch:
best_justified_checkpoint = new_justified
just_state.current_justified_checkpoint = new_justified just_state.current_justified_checkpoint = new_justified
block = build_empty_block_for_next_slot(spec, just_state) block = build_empty_block_for_next_slot(spec, just_state)
signed_block = state_transition_and_sign_block(spec, deepcopy(just_state), block) signed_block = state_transition_and_sign_block(spec, deepcopy(just_state), block)
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
run_on_block(spec, store, signed_block) run_on_block(spec, store, signed_block)
assert store.justified_checkpoint != new_justified assert store.justified_checkpoint == previously_justified
assert store.best_justified_checkpoint == new_justified # ensure the best from the series was stored
assert store.best_justified_checkpoint == best_justified_checkpoint