2019-11-12 17:01:27 +00:00
|
|
|
package node
|
2019-07-23 02:19:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2020-06-18 03:53:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2019-07-23 02:19:55 +00:00
|
|
|
ptypes "github.com/gogo/protobuf/types"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
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"
|
2020-06-18 03:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2019-11-30 05:36:02 +00:00
|
|
|
mockP2p "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
2019-11-12 17:01:27 +00:00
|
|
|
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
|
2020-04-27 06:31:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-20 02:15:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-07-23 02:19:55 +00:00
|
|
|
"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{})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, res.Syncing)
|
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{})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 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) {
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := dbutil.SetupDB(t)
|
2019-09-16 20:45:03 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
addr := common.Address{1, 2, 3}
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, db.SaveDepositContractAddress(ctx, addr))
|
2020-04-27 06:31:50 +00:00
|
|
|
st := testutil.NewBeaconState()
|
|
|
|
genValRoot := bytesutil.ToBytes32([]byte("I am root"))
|
2019-11-12 17:01:27 +00:00
|
|
|
ns := &Server{
|
|
|
|
BeaconDB: db,
|
2020-05-29 01:57:03 +00:00
|
|
|
GenesisTimeFetcher: &mock.ChainService{},
|
2020-04-27 06:31:50 +00:00
|
|
|
GenesisFetcher: &mock.ChainService{
|
|
|
|
State: st,
|
|
|
|
ValidatorsRoot: genValRoot,
|
|
|
|
},
|
2019-09-16 20:45:03 +00:00
|
|
|
}
|
|
|
|
res, err := ns.GetGenesis(context.Background(), &ptypes.Empty{})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, addr.Bytes(), res.DepositContractAddress)
|
2019-09-16 20:45:03 +00:00
|
|
|
pUnix, err := ptypes.TimestampProto(time.Unix(0, 0))
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, res.GenesisTime.Equal(pUnix))
|
|
|
|
assert.DeepEqual(t, genValRoot[:], res.GenesisValidatorsRoot)
|
2020-05-29 01:57:03 +00:00
|
|
|
|
|
|
|
ns.GenesisTimeFetcher = &mock.ChainService{Genesis: time.Unix(10, 0)}
|
|
|
|
res, err = ns.GetGenesis(context.Background(), &ptypes.Empty{})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-29 01:57:03 +00:00
|
|
|
pUnix, err = ptypes.TimestampProto(time.Unix(10, 0))
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, res.GenesisTime.Equal(pUnix))
|
2019-09-16 20:45:03 +00:00
|
|
|
}
|
|
|
|
|
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{})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, v, res.Version)
|
2019-07-23 02:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
2019-07-23 02:19:55 +00:00
|
|
|
// We verify the services include the node service + the registered reflection service.
|
2020-07-20 02:15:51 +00:00
|
|
|
assert.Equal(t, 2, len(res.Services))
|
2019-07-23 02:19:55 +00:00
|
|
|
}
|
2019-11-30 05:36:02 +00:00
|
|
|
|
2020-06-18 03:53:46 +00:00
|
|
|
func TestNodeServer_GetHost(t *testing.T) {
|
|
|
|
server := grpc.NewServer()
|
|
|
|
peersProvider := &mockP2p.MockPeersProvider{}
|
|
|
|
mP2P := mockP2p.NewTestP2P(t)
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
db, err := enode.OpenDB("")
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-18 03:53:46 +00:00
|
|
|
lNode := enode.NewLocalNode(db, key)
|
|
|
|
record := lNode.Node().Record()
|
|
|
|
stringENR, err := p2p.SerializeENR(record)
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-18 03:53:46 +00:00
|
|
|
ns := &Server{
|
|
|
|
PeerManager: &mockP2p.MockPeerManager{BHost: mP2P.BHost, Enr: record, PID: mP2P.BHost.ID()},
|
|
|
|
PeersFetcher: peersProvider,
|
|
|
|
}
|
|
|
|
ethpb.RegisterNodeServer(server, ns)
|
|
|
|
reflection.Register(server)
|
|
|
|
h, err := ns.GetHost(context.Background(), &ptypes.Empty{})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, mP2P.PeerID().String(), h.PeerId)
|
|
|
|
assert.Equal(t, stringENR, h.Enr)
|
2020-06-18 03:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeServer_GetPeer(t *testing.T) {
|
|
|
|
server := grpc.NewServer()
|
|
|
|
peersProvider := &mockP2p.MockPeersProvider{}
|
|
|
|
ns := &Server{
|
|
|
|
PeersFetcher: peersProvider,
|
|
|
|
}
|
|
|
|
ethpb.RegisterNodeServer(server, ns)
|
|
|
|
reflection.Register(server)
|
|
|
|
firstPeer := peersProvider.Peers().All()[0]
|
|
|
|
|
|
|
|
res, err := ns.GetPeer(context.Background(), ðpb.PeerRequest{PeerId: firstPeer.String()})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, firstPeer.String(), res.PeerId, "Unexpected peer ID")
|
|
|
|
assert.Equal(t, int(ethpb.PeerDirection_INBOUND), int(res.Direction), "Expected 1st peer to be an inbound connection")
|
|
|
|
assert.Equal(t, ethpb.ConnectionState_CONNECTED, res.ConnectionState, "Expected peer to be connected")
|
2020-06-18 03:53:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 05:36:02 +00:00
|
|
|
func TestNodeServer_ListPeers(t *testing.T) {
|
|
|
|
server := grpc.NewServer()
|
|
|
|
peersProvider := &mockP2p.MockPeersProvider{}
|
|
|
|
ns := &Server{
|
|
|
|
PeersFetcher: peersProvider,
|
|
|
|
}
|
|
|
|
ethpb.RegisterNodeServer(server, ns)
|
|
|
|
reflection.Register(server)
|
|
|
|
|
|
|
|
res, err := ns.ListPeers(context.Background(), &ptypes.Empty{})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 2, len(res.Peers))
|
|
|
|
assert.Equal(t, int(ethpb.PeerDirection_INBOUND), int(res.Peers[0].Direction))
|
|
|
|
assert.Equal(t, ethpb.PeerDirection_OUTBOUND, res.Peers[1].Direction)
|
2019-11-30 05:36:02 +00:00
|
|
|
}
|