This commit is contained in:
Nishant Das 2021-05-12 15:59:26 +08:00 committed by GitHub
parent ddab82e52a
commit 4ef5c9ad15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 4 deletions

View File

@ -2,7 +2,6 @@ package beacon
import ( import (
"context" "context"
"errors"
"sort" "sort"
"strconv" "strconv"
"time" "time"
@ -870,10 +869,20 @@ func (bs *Server) isSlotCanonical(ctx context.Context, slot types.Slot) (bool, e
if !hasBlockRoots { if !hasBlockRoots {
return false, nil 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. // Determines whether a validator has already exited.

View File

@ -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) { func TestServer_isSlotCanonicalForSlot0(t *testing.T) {
ctx := context.Background() ctx := context.Background()
bs := &Server{} bs := &Server{}