2019-11-12 17:01:27 +00:00
|
|
|
package node
|
2019-07-23 02:19:55 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-16 20:45:03 +00:00
|
|
|
"bytes"
|
2019-07-23 02:19:55 +00:00
|
|
|
"context"
|
|
|
|
"testing"
|
2019-09-16 20:45:03 +00:00
|
|
|
"time"
|
2019-07-23 02:19:55 +00:00
|
|
|
|
2019-09-16 20:45:03 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-07-23 02:19:55 +00:00
|
|
|
ptypes "github.com/gogo/protobuf/types"
|
2019-09-16 20:45:03 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
|
|
dbutil "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2019-11-12 17:01:27 +00:00
|
|
|
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
|
2019-07-23 02:19:55 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/version"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeServer_GetSyncStatus(t *testing.T) {
|
2019-11-12 17:01:27 +00:00
|
|
|
mSync := &mockSync.Sync{IsSyncing: false}
|
|
|
|
ns := &Server{
|
|
|
|
SyncChecker: mSync,
|
2019-07-23 02:19:55 +00:00
|
|
|
}
|
|
|
|
res, err := ns.GetSyncStatus(context.Background(), &ptypes.Empty{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-12 17:01:27 +00:00
|
|
|
if res.Syncing {
|
|
|
|
t.Errorf("Wanted GetSyncStatus() = %v, received %v", false, res.Syncing)
|
2019-07-23 02:19:55 +00:00
|
|
|
}
|
2019-11-12 17:01:27 +00:00
|
|
|
ns.SyncChecker = &mockSync.Sync{IsSyncing: true}
|
2019-07-23 02:19:55 +00:00
|
|
|
res, err = ns.GetSyncStatus(context.Background(), &ptypes.Empty{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-12 17:01:27 +00:00
|
|
|
if !res.Syncing {
|
|
|
|
t.Errorf("Wanted GetSyncStatus() = %v, received %v", true, res.Syncing)
|
2019-07-23 02:19:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:45:03 +00:00
|
|
|
func TestNodeServer_GetGenesis(t *testing.T) {
|
|
|
|
db := dbutil.SetupDB(t)
|
|
|
|
defer dbutil.TeardownDB(t, db)
|
|
|
|
ctx := context.Background()
|
|
|
|
addr := common.Address{1, 2, 3}
|
|
|
|
if err := db.SaveDepositContractAddress(ctx, addr); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-12 17:01:27 +00:00
|
|
|
ns := &Server{
|
|
|
|
BeaconDB: db,
|
|
|
|
GenesisTimeFetcher: &mock.ChainService{Genesis: time.Unix(0, 0)},
|
2019-09-16 20:45:03 +00:00
|
|
|
}
|
|
|
|
res, err := ns.GetGenesis(context.Background(), &ptypes.Empty{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(res.DepositContractAddress, addr.Bytes()) {
|
|
|
|
t.Errorf("Wanted DepositContractAddress() = %#x, received %#x", addr.Bytes(), res.DepositContractAddress)
|
|
|
|
}
|
|
|
|
pUnix, err := ptypes.TimestampProto(time.Unix(0, 0))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !res.GenesisTime.Equal(pUnix) {
|
|
|
|
t.Errorf("Wanted GenesisTime() = %v, received %v", pUnix, res.GenesisTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 02:19:55 +00:00
|
|
|
func TestNodeServer_GetVersion(t *testing.T) {
|
|
|
|
v := version.GetVersion()
|
2019-11-12 17:01:27 +00:00
|
|
|
ns := &Server{}
|
2019-07-23 02:19:55 +00:00
|
|
|
res, err := ns.GetVersion(context.Background(), &ptypes.Empty{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if res.Version != v {
|
|
|
|
t.Errorf("Wanted GetVersion() = %s, received %s", v, res.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeServer_GetImplementedServices(t *testing.T) {
|
|
|
|
server := grpc.NewServer()
|
2019-11-12 17:01:27 +00:00
|
|
|
ns := &Server{
|
|
|
|
Server: server,
|
2019-07-23 02:19:55 +00:00
|
|
|
}
|
|
|
|
ethpb.RegisterNodeServer(server, ns)
|
|
|
|
reflection.Register(server)
|
|
|
|
|
|
|
|
res, err := ns.ListImplementedServices(context.Background(), &ptypes.Empty{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// We verify the services include the node service + the registered reflection service.
|
|
|
|
if len(res.Services) != 2 {
|
|
|
|
t.Errorf("Expected 2 services, received %d: %v", len(res.Services), res.Services)
|
|
|
|
}
|
|
|
|
}
|