2019-08-21 19:50:27 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-08-23 01:13:56 +00:00
|
|
|
"reflect"
|
2019-08-21 19:50:27 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-08-23 01:13:56 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
2019-08-29 15:34:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-08-21 19:50:27 +00:00
|
|
|
)
|
|
|
|
|
2019-09-07 02:39:14 +00:00
|
|
|
// Ensure Service implements chain info interface.
|
2019-09-09 21:13:50 +00:00
|
|
|
var _ = ChainInfoFetcher(&Service{})
|
2019-09-16 20:45:03 +00:00
|
|
|
var _ = GenesisTimeFetcher(&Service{})
|
2019-08-21 19:50:27 +00:00
|
|
|
|
2019-08-29 15:34:26 +00:00
|
|
|
func TestFinalizedCheckpt_Nil(t *testing.T) {
|
|
|
|
c := setupBeaconChain(t, nil)
|
|
|
|
if !bytes.Equal(c.FinalizedCheckpt().Root, params.BeaconConfig().ZeroHash[:]) {
|
|
|
|
t.Error("Incorrect pre chain start value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadRoot_Nil(t *testing.T) {
|
|
|
|
c := setupBeaconChain(t, nil)
|
|
|
|
if !bytes.Equal(c.HeadRoot(), params.BeaconConfig().ZeroHash[:]) {
|
|
|
|
t.Error("Incorrect pre chain start value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 19:50:27 +00:00
|
|
|
func TestFinalizedCheckpt_CanRetrieve(t *testing.T) {
|
|
|
|
db := testDB.SetupDB(t)
|
|
|
|
defer testDB.TeardownDB(t, db)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-08-22 01:14:24 +00:00
|
|
|
c := setupBeaconChain(t, db)
|
2019-08-21 19:50:27 +00:00
|
|
|
|
2019-08-30 13:58:02 +00:00
|
|
|
if err := c.forkChoiceStore.GenesisStore(ctx, ðpb.Checkpoint{}, ðpb.Checkpoint{}); err != nil {
|
2019-08-21 19:50:27 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.FinalizedCheckpt().Epoch != 0 {
|
|
|
|
t.Errorf("Finalized epoch at genesis should be 0, got: %d", c.FinalizedCheckpt().Epoch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadSlot_CanRetrieve(t *testing.T) {
|
2019-09-07 02:39:14 +00:00
|
|
|
c := &Service{}
|
2019-08-21 19:50:27 +00:00
|
|
|
c.headSlot = 100
|
|
|
|
if c.HeadSlot() != 100 {
|
|
|
|
t.Errorf("Wanted head slot: %d, got: %d", 100, c.HeadSlot())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadRoot_CanRetrieve(t *testing.T) {
|
2019-09-07 02:39:14 +00:00
|
|
|
c := &Service{canonicalRoots: make(map[uint64][]byte)}
|
2019-08-21 19:50:27 +00:00
|
|
|
c.headSlot = 100
|
|
|
|
c.canonicalRoots[c.headSlot] = []byte{'A'}
|
|
|
|
if !bytes.Equal([]byte{'A'}, c.HeadRoot()) {
|
|
|
|
t.Errorf("Wanted head root: %v, got: %d", []byte{'A'}, c.HeadRoot())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 01:13:56 +00:00
|
|
|
func TestHeadBlock_CanRetrieve(t *testing.T) {
|
|
|
|
b := ðpb.BeaconBlock{Slot: 1}
|
2019-09-07 02:39:14 +00:00
|
|
|
c := &Service{headBlock: b}
|
2019-08-23 01:13:56 +00:00
|
|
|
if !reflect.DeepEqual(b, c.HeadBlock()) {
|
|
|
|
t.Error("incorrect head block received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadState_CanRetrieve(t *testing.T) {
|
|
|
|
s := &pb.BeaconState{Slot: 2}
|
2019-09-07 02:39:14 +00:00
|
|
|
c := &Service{headState: s}
|
2019-08-23 01:13:56 +00:00
|
|
|
if !reflect.DeepEqual(s, c.HeadState()) {
|
|
|
|
t.Error("incorrect head state received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 19:50:27 +00:00
|
|
|
func TestCanonicalRoot_CanRetrieve(t *testing.T) {
|
2019-09-07 02:39:14 +00:00
|
|
|
c := &Service{canonicalRoots: make(map[uint64][]byte)}
|
2019-08-21 19:50:27 +00:00
|
|
|
slot := uint64(123)
|
2019-08-23 01:13:56 +00:00
|
|
|
r := []byte{'B'}
|
|
|
|
c.canonicalRoots[slot] = r
|
|
|
|
if !bytes.Equal(r, c.CanonicalRoot(slot)) {
|
2019-08-21 19:50:27 +00:00
|
|
|
t.Errorf("Wanted head root: %v, got: %d", []byte{'A'}, c.CanonicalRoot(slot))
|
|
|
|
}
|
|
|
|
}
|