2019-11-12 17:01:27 +00:00
|
|
|
package node
|
2019-07-23 02:19:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-16 17:22:34 +00:00
|
|
|
"errors"
|
2019-07-23 02:19:55 +00:00
|
|
|
"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"
|
2023-03-17 18:52:56 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/v4/beacon-chain/blockchain/testing"
|
|
|
|
dbutil "github.com/prysmaticlabs/prysm/v4/beacon-chain/db/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p"
|
|
|
|
mockP2p "github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/testutil"
|
|
|
|
mockSync "github.com/prysmaticlabs/prysm/v4/beacon-chain/sync/initial-sync/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/runtime/version"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/util"
|
2021-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2019-07-23 02:19:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2021-05-17 18:32:04 +00:00
|
|
|
res, err := ns.GetSyncStatus(context.Background(), &emptypb.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}
|
2021-05-17 18:32:04 +00:00
|
|
|
res, err = ns.GetSyncStatus(context.Background(), &emptypb.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-12-16 16:56:21 +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))
|
2021-09-23 18:53:46 +00:00
|
|
|
st, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-27 06:31:50 +00:00
|
|
|
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
|
|
|
}
|
2021-05-17 18:32:04 +00:00
|
|
|
res, err := ns.GetGenesis(context.Background(), &emptypb.Empty{})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, addr.Bytes(), res.DepositContractAddress)
|
2021-05-17 18:32:04 +00:00
|
|
|
pUnix := timestamppb.New(time.Unix(0, 0))
|
|
|
|
assert.Equal(t, res.GenesisTime.Seconds, pUnix.Seconds)
|
2020-07-20 02:15:51 +00:00
|
|
|
assert.DeepEqual(t, genValRoot[:], res.GenesisValidatorsRoot)
|
2020-05-29 01:57:03 +00:00
|
|
|
|
|
|
|
ns.GenesisTimeFetcher = &mock.ChainService{Genesis: time.Unix(10, 0)}
|
2021-05-17 18:32:04 +00:00
|
|
|
res, err = ns.GetGenesis(context.Background(), &emptypb.Empty{})
|
2020-07-20 02:15:51 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-17 18:32:04 +00:00
|
|
|
pUnix = timestamppb.New(time.Unix(10, 0))
|
|
|
|
assert.Equal(t, res.GenesisTime.Seconds, pUnix.Seconds)
|
2019-09-16 20:45:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 02:19:55 +00:00
|
|
|
func TestNodeServer_GetVersion(t *testing.T) {
|
2021-01-25 21:27:30 +00:00
|
|
|
v := version.Version()
|
2019-11-12 17:01:27 +00:00
|
|
|
ns := &Server{}
|
2021-05-17 18:32:04 +00:00
|
|
|
res, err := ns.GetVersion(context.Background(), &emptypb.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)
|
|
|
|
|
2021-05-17 18:32:04 +00:00
|
|
|
res, err := ns.ListImplementedServices(context.Background(), &emptypb.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()
|
2020-10-01 18:53:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-18 03:53:46 +00:00
|
|
|
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)
|
2021-05-17 18:32:04 +00:00
|
|
|
h, err := ns.GetHost(context.Background(), &emptypb.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)
|
|
|
|
|
2021-05-17 18:32:04 +00:00
|
|
|
res, err := ns.ListPeers(context.Background(), &emptypb.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
|
|
|
}
|
2022-01-14 16:13:04 +00:00
|
|
|
|
|
|
|
func TestNodeServer_GetETH1ConnectionStatus(t *testing.T) {
|
|
|
|
server := grpc.NewServer()
|
2022-08-16 17:22:34 +00:00
|
|
|
ep := "foo"
|
|
|
|
err := errors.New("error1")
|
|
|
|
errStr := "error1"
|
2022-08-01 14:43:47 +00:00
|
|
|
mockFetcher := &testutil.MockExecutionChainInfoFetcher{
|
2022-08-16 17:22:34 +00:00
|
|
|
CurrEndpoint: ep,
|
|
|
|
CurrError: err,
|
2022-01-14 16:13:04 +00:00
|
|
|
}
|
|
|
|
ns := &Server{
|
|
|
|
POWChainInfoFetcher: mockFetcher,
|
|
|
|
}
|
|
|
|
ethpb.RegisterNodeServer(server, ns)
|
|
|
|
reflection.Register(server)
|
|
|
|
|
|
|
|
res, err := ns.GetETH1ConnectionStatus(context.Background(), &emptypb.Empty{})
|
|
|
|
require.NoError(t, err)
|
2022-08-16 17:22:34 +00:00
|
|
|
assert.Equal(t, ep, res.CurrentAddress)
|
|
|
|
assert.Equal(t, errStr, res.CurrentConnectionError)
|
2022-01-14 16:13:04 +00:00
|
|
|
}
|