mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-03 08:37:37 +00:00
add it in (#8885)
This commit is contained in:
parent
ddab82e52a
commit
4ef5c9ad15
@ -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
|
||||
}
|
||||
return bs.CanonicalFetcher.IsCanonical(ctx, roots[0])
|
||||
if canonical {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Determines whether a validator has already exited.
|
||||
|
@ -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{}
|
||||
|
Loading…
Reference in New Issue
Block a user