prysm-pulse/beacon-chain/sync/utils_test.go
Nishant Das caf9bdbc6f
Use Block Interface Across Prysm (#8918)
* commit initial work

* checkpoint current work

* gaz

* checkpoint

* req/resp changes

* initial-sync

* finally works

* fix error

* fix bugs

* fix issue

* fix issues

* fix refs

* tests

* more text fixes

* more text fixes

* more text fixes

* fix tests

* fix tests

* tests

* finally fix builds

* finally

* comments

* fix fuzz

* share common library

* fix

* fix

* add in more defensive nil checks

* add in more defensive nil checks

* imports

* Apply suggestions from code review

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Apply suggestions from code review

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Update shared/interfaces/block_interface.go

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Update shared/interfaces/block_wrapper.go

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Update shared/interfaces/block_interface.go

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* imports

* fix bad changes

* fix

* terence's review

* terence's review

* fmt

* Update beacon-chain/rpc/beacon/blocks.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* fix tests

* fix

* fix all tests

Co-authored-by: terence tsao <terence@prysmaticlabs.com>
Co-authored-by: Radosław Kapka <rkapka@wp.pl>
2021-05-26 16:19:54 +00:00

80 lines
2.3 KiB
Go

package sync
import (
"math/rand"
"testing"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/interfaces"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestSortedObj_SortBlocksRoots(t *testing.T) {
source := rand.NewSource(33)
randGen := rand.New(source)
var blks []interfaces.SignedBeaconBlock
var roots [][32]byte
randFunc := func() int64 {
return randGen.Int63n(50)
}
for i := 0; i < 10; i++ {
slot := types.Slot(randFunc())
newBlk := interfaces.NewWrappedSignedBeaconBlock(&ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{Slot: slot}})
blks = append(blks, newBlk)
root := bytesutil.ToBytes32(bytesutil.Bytes32(uint64(slot)))
roots = append(roots, root)
}
r := &Service{}
newBlks, newRoots := r.sortBlocksAndRoots(blks, roots)
previousSlot := types.Slot(0)
for i, b := range newBlks {
if b.Block().Slot() < previousSlot {
t.Errorf("Block list is not sorted as %d is smaller than previousSlot %d", b.Block().Slot(), previousSlot)
}
if bytesutil.FromBytes8(newRoots[i][:]) != uint64(b.Block().Slot()) {
t.Errorf("root doesn't match stored slot in block: wanted %d but got %d", b.Block().Slot(), bytesutil.FromBytes8(newRoots[i][:]))
}
previousSlot = b.Block().Slot()
}
}
func TestSortedObj_NoDuplicates(t *testing.T) {
source := rand.NewSource(33)
randGen := rand.New(source)
var blks []interfaces.SignedBeaconBlock
var roots [][32]byte
randFunc := func() int64 {
return randGen.Int63n(50)
}
for i := 0; i < 10; i++ {
slot := types.Slot(randFunc())
newBlk := &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{Slot: slot}}
// append twice
blks = append(blks, interfaces.NewWrappedSignedBeaconBlock(newBlk), interfaces.NewWrappedSignedBeaconBlock(newBlk))
// append twice
root := bytesutil.ToBytes32(bytesutil.Bytes32(uint64(slot)))
roots = append(roots, root, root)
}
r := &Service{}
newBlks, newRoots, err := r.dedupBlocksAndRoots(blks, roots)
require.NoError(t, err)
rootMap := make(map[[32]byte]bool)
for i, b := range newBlks {
if rootMap[newRoots[i]] {
t.Errorf("Duplicated root exists %#x with block %v", newRoots[i], b)
}
rootMap[newRoots[i]] = true
}
}