From 4ef5c9ad159e0fd7b390b365dad1d7ae05ff7717 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 12 May 2021 15:59:26 +0800 Subject: [PATCH] add it in (#8885) --- beacon-chain/rpc/beacon/validators.go | 17 ++++++--- beacon-chain/rpc/beacon/validators_test.go | 41 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/beacon-chain/rpc/beacon/validators.go b/beacon-chain/rpc/beacon/validators.go index 0c36fdc07..4ce1b96af 100644 --- a/beacon-chain/rpc/beacon/validators.go +++ b/beacon-chain/rpc/beacon/validators.go @@ -2,7 +2,6 @@ package beacon import ( "context" - "errors" "sort" "strconv" "time" @@ -870,10 +869,20 @@ func (bs *Server) isSlotCanonical(ctx context.Context, slot types.Slot) (bool, e if !hasBlockRoots { return false, nil } - if len(roots) != 1 { - return false, errors.New("more than one block existed in slot") + + // Loop through all roots in slot, and + // check which one is canonical. + for _, rt := range roots { + canonical, err := bs.CanonicalFetcher.IsCanonical(ctx, rt) + if err != nil { + return false, err + } + if canonical { + return true, nil + } + } - return bs.CanonicalFetcher.IsCanonical(ctx, roots[0]) + return false, nil } // Determines whether a validator has already exited. diff --git a/beacon-chain/rpc/beacon/validators_test.go b/beacon-chain/rpc/beacon/validators_test.go index a9ed16124..a3aeafaa6 100644 --- a/beacon-chain/rpc/beacon/validators_test.go +++ b/beacon-chain/rpc/beacon/validators_test.go @@ -2204,6 +2204,47 @@ func TestServer_isSlotCanonical(t *testing.T) { } } +func TestServer_isSlotCanonical_MultipleBlocks(t *testing.T) { + beaconDB := dbTest.SetupDB(t) + ctx := context.Background() + var roots [][32]byte + cRoots := map[[32]byte]bool{} + for i := 1; i < 100; i++ { + b := testutil.NewBeaconBlock() + b.Block.Slot = types.Slot(i) + require.NoError(t, beaconDB.SaveBlock(ctx, b)) + br, err := b.Block.HashTreeRoot() + require.NoError(t, err) + if i%2 == 0 { + cRoots[br] = true + // Save a block in the same slot + b = testutil.NewBeaconBlock() + b.Block.Slot = types.Slot(i) + b.Block.ProposerIndex = 100 + require.NoError(t, beaconDB.SaveBlock(ctx, b)) + } + roots = append(roots, br) + } + + bs := &Server{ + BeaconDB: beaconDB, + CanonicalFetcher: &mock.ChainService{ + CanonicalRoots: cRoots, + }, + } + + for i := range roots { + slot := types.Slot(i + 1) + c, err := bs.isSlotCanonical(ctx, slot) + require.NoError(t, err) + if slot%2 == 0 { + require.Equal(t, true, c) + } else { + require.Equal(t, false, c) + } + } +} + func TestServer_isSlotCanonicalForSlot0(t *testing.T) { ctx := context.Background() bs := &Server{}