From 8f18920ac7ff4203f508bf2dbf49a21a3f6f75af Mon Sep 17 00:00:00 2001 From: Potuz Date: Wed, 1 Jun 2022 11:13:47 -0300 Subject: [PATCH] Update forkchoice justified on init sync (#10801) * Update forkchoice justified on init sync * handle err * add regression test Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- beacon-chain/blockchain/process_block.go | 3 +++ beacon-chain/blockchain/process_block_helpers.go | 3 +-- beacon-chain/blockchain/process_block_test.go | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index f7dc28cd2..7b64c20b4 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -518,6 +518,9 @@ func (s *Service) handleBlockAfterBatchVerify(ctx context.Context, signed interf return err } s.store.SetFinalizedCheckptAndPayloadHash(fCheckpoint, h) + if err := s.cfg.ForkChoiceStore.UpdateFinalizedCheckpoint(fCheckpoint); err != nil { + return err + } if err := s.cfg.ForkChoiceStore.Prune(ctx, bytesutil.ToBytes32(fCheckpoint.Root)); err != nil { return errors.Wrap(err, "could not prune proto array fork choice nodes") } diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index ba3131a50..031ce31c9 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -242,8 +242,7 @@ func (s *Service) updateJustifiedInitSync(ctx context.Context, cp *ethpb.Checkpo return err } s.store.SetJustifiedCheckptAndPayloadHash(cp, h) - - return nil + return s.cfg.ForkChoiceStore.UpdateJustifiedCheckpoint(cp) } func (s *Service) updateFinalized(ctx context.Context, cp *ethpb.Checkpoint) error { diff --git a/beacon-chain/blockchain/process_block_test.go b/beacon-chain/blockchain/process_block_test.go index 0bb583971..3c3d82398 100644 --- a/beacon-chain/blockchain/process_block_test.go +++ b/beacon-chain/blockchain/process_block_test.go @@ -305,7 +305,7 @@ func TestStore_OnBlockBatch_ProtoArray(t *testing.T) { var blks []interfaces.SignedBeaconBlock var blkRoots [][32]byte var firstState state.BeaconState - for i := 1; i < 10; i++ { + for i := 1; i < 97; i++ { b, err := util.GenerateFullBlock(bState, keys, util.DefaultBlockGenConfig(), types.Slot(i)) require.NoError(t, err) wsb, err := wrapper.WrappedSignedBeaconBlock(b) @@ -335,6 +335,11 @@ func TestStore_OnBlockBatch_ProtoArray(t *testing.T) { require.ErrorIs(t, errWrongBlockCount, err) err = service.onBlockBatch(ctx, blks[1:], blkRoots[1:]) require.NoError(t, err) + jcp, err := service.store.JustifiedCheckpt() + require.NoError(t, err) + jroot := bytesutil.ToBytes32(jcp.Root) + require.Equal(t, blkRoots[63], jroot) + require.Equal(t, types.Epoch(2), service.cfg.ForkChoiceStore.JustifiedEpoch()) } func TestStore_OnBlockBatch_DoublyLinkedTree(t *testing.T) { @@ -370,7 +375,7 @@ func TestStore_OnBlockBatch_DoublyLinkedTree(t *testing.T) { var blks []interfaces.SignedBeaconBlock var blkRoots [][32]byte var firstState state.BeaconState - for i := 1; i < 10; i++ { + for i := 1; i < 97; i++ { b, err := util.GenerateFullBlock(bState, keys, util.DefaultBlockGenConfig(), types.Slot(i)) require.NoError(t, err) wsb, err = wrapper.WrappedSignedBeaconBlock(b) @@ -400,6 +405,11 @@ func TestStore_OnBlockBatch_DoublyLinkedTree(t *testing.T) { require.ErrorIs(t, errWrongBlockCount, err) err = service.onBlockBatch(ctx, blks[1:], blkRoots[1:]) require.NoError(t, err) + jcp, err := service.store.JustifiedCheckpt() + require.NoError(t, err) + jroot := bytesutil.ToBytes32(jcp.Root) + require.Equal(t, blkRoots[63], jroot) + require.Equal(t, types.Epoch(2), service.cfg.ForkChoiceStore.JustifiedEpoch()) } func TestStore_OnBlockBatch_NotifyNewPayload(t *testing.T) {