From ebb3fa71f1f3ce1334a8dc8581cf6b194b7b47eb Mon Sep 17 00:00:00 2001 From: terence tsao Date: Fri, 23 Oct 2020 12:02:20 -0700 Subject: [PATCH] VerifyFinalizedConsistency - return early when canonical (#7628) Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- .../blockchain/process_attestation_test.go | 30 +++++++++++++++++++ .../blockchain/receive_attestation.go | 6 ++++ 2 files changed, 36 insertions(+) diff --git a/beacon-chain/blockchain/process_attestation_test.go b/beacon-chain/blockchain/process_attestation_test.go index 0f28ca682..adf9da8c9 100644 --- a/beacon-chain/blockchain/process_attestation_test.go +++ b/beacon-chain/blockchain/process_attestation_test.go @@ -545,6 +545,36 @@ func TestVerifyFinalizedConsistency_OK(t *testing.T) { require.NoError(t, err) } +func TestVerifyFinalizedConsistency_IsCanonical(t *testing.T) { + ctx := context.Background() + db, _ := testDB.SetupDB(t) + + cfg := &Config{BeaconDB: db, ForkChoiceStore: protoarray.New(0, 0, [32]byte{})} + service, err := NewService(ctx, cfg) + require.NoError(t, err) + + b32 := testutil.NewBeaconBlock() + b32.Block.Slot = 32 + r32, err := b32.Block.HashTreeRoot() + require.NoError(t, err) + + service.finalizedCheckpt = ðpb.Checkpoint{Epoch: 1, Root: r32[:]} + + b33 := testutil.NewBeaconBlock() + b33.Block.Slot = 33 + b33.Block.ParentRoot = r32[:] + r33, err := b33.Block.HashTreeRoot() + require.NoError(t, err) + + require.NoError(t, service.forkChoiceStore.ProcessBlock(ctx, b32.Block.Slot, r32, [32]byte{}, [32]byte{}, 0, 0)) + require.NoError(t, service.forkChoiceStore.ProcessBlock(ctx, b33.Block.Slot, r33, r32, [32]byte{}, 0, 0)) + + _, err = service.forkChoiceStore.Head(ctx, 0, r32, []uint64{}, 0) + require.NoError(t, err) + err = service.VerifyFinalizedConsistency(context.Background(), r33[:]) + require.NoError(t, err) +} + func TestGetAttCheckptInfo(t *testing.T) { ctx := context.Background() db, _ := testDB.SetupDB(t) diff --git a/beacon-chain/blockchain/receive_attestation.go b/beacon-chain/blockchain/receive_attestation.go index 6d2816973..5d71969dd 100644 --- a/beacon-chain/blockchain/receive_attestation.go +++ b/beacon-chain/blockchain/receive_attestation.go @@ -102,6 +102,12 @@ func (s *Service) VerifyLmdFfgConsistency(ctx context.Context, a *ethpb.Attestat // When the input root is not be consistent with finalized store then we know it is not // on the finalized check point that leads to current canonical chain and should be rejected accordingly. func (s *Service) VerifyFinalizedConsistency(ctx context.Context, root []byte) error { + // A canonical root implies the root to has an ancestor that aligns with finalized check point. + // In this case, we could exit early to save on additional computation. + if s.forkChoiceStore.IsCanonical(bytesutil.ToBytes32(root)) { + return nil + } + f := s.FinalizedCheckpt() ss, err := helpers.StartSlot(f.Epoch) if err != nil {