Skip to content

Commit

Permalink
Misc fork choice improvements (#4744)
Browse files Browse the repository at this point in the history
* Added missing spec implementation
* Use it
* Rename
* Merge branch 'master' into update-justified
  • Loading branch information
terencechain committed Feb 4, 2020
1 parent 061960c commit 9c1a294
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
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.finalizedImpliesNewJustified(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.finalizedImpliesNewJustified(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) finalizedImpliesNewJustified(ctx context.Context, state *stateTrie.BeaconState) error {
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

0 comments on commit 9c1a294

Please sign in to comment.