Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc fork choice improvements #4744

Merged
merged 4 commits into from Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions beacon-chain/blockchain/process_block.go
Expand Up @@ -120,6 +120,10 @@ func (s *Service) onBlock(ctx context.Context, signed *ethpb.SignedBeaconBlock)

s.prevFinalizedCheckpt = s.finalizedCheckpt
s.finalizedCheckpt = postState.FinalizedCheckpoint()

if err := s.finalizedNewJustified(ctx, postState); err != nil {
return nil, errors.Wrap(err, "could not save new justified")
}
}

// Update validator indices in database as needed.
Expand Down Expand Up @@ -222,6 +226,10 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed

s.prevFinalizedCheckpt = s.finalizedCheckpt
s.finalizedCheckpt = postState.FinalizedCheckpoint()

if err := s.finalizedNewJustified(ctx, postState); err != nil {
return nil, errors.Wrap(err, "could not save new justified")
}
}

// Update validator indices in database as needed.
Expand Down
29 changes: 29 additions & 0 deletions beacon-chain/blockchain/process_block_helpers.go
Expand Up @@ -356,3 +356,32 @@ func (s *Service) ancestor(ctx context.Context, root []byte, slot uint64) ([]byt

return s.ancestor(ctx, b.ParentRoot, slot)
}

// This updates justified check point in store, if the new justified is later than stored justified or
// the store's justified is not in chain with finalized check point.
//
// Spec definition:
// if (
// state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch
// or get_ancestor(store, store.justified_checkpoint.root, finalized_slot) != store.finalized_checkpoint.root
// ):
// store.justified_checkpoint = state.current_justified_checkpoint
func (s *Service) finalizedNewJustified(ctx context.Context, state *stateTrie.BeaconState) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rename to something more informative? finalizedImpliesNewJustifiedCheckpoint or something like that

finalizedBlkSigned, err := s.beaconDB.Block(ctx, bytesutil.ToBytes32(s.finalizedCheckpt.Root))
if err != nil || finalizedBlkSigned == nil || finalizedBlkSigned.Block == nil {
return errors.Wrap(err, "could not get finalized block")
}
finalizedBlk := finalizedBlkSigned.Block

anc, err := s.ancestor(ctx, s.justifiedCheckpt.Root, finalizedBlk.Slot)
if err != nil {
return err
}

// Either the new justified is later than stored justified or not in chain with finalized check pint.
if cpt := state.CurrentJustifiedCheckpoint(); cpt != nil && cpt.Epoch > s.justifiedCheckpt.Epoch || !bytes.Equal(anc, s.finalizedCheckpt.Root) {
s.justifiedCheckpt = state.CurrentJustifiedCheckpoint()
}

return nil
}
4 changes: 4 additions & 0 deletions beacon-chain/blockchain/receive_block.go
Expand Up @@ -131,6 +131,10 @@ func (s *Service) ReceiveBlockNoPubsub(ctx context.Context, block *ethpb.SignedB
} else {
headRoot := make([]byte, 0)
if featureconfig.Get().ProtoArrayForkChoice {
if s.bestJustifiedCheckpt.Epoch > s.justifiedCheckpt.Epoch {
s.justifiedCheckpt = s.bestJustifiedCheckpt
}

f := s.finalizedCheckpt
j := s.justifiedCheckpt
headRootProtoArray, err := s.forkChoiceStore.Head(
Expand Down