Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dot/rpc, dot/network: Implement RPC system_nodeRoles #880

Merged
merged 6 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,8 @@ func (s *Service) Peers() []common.PeerInfo {
}
return peers
}

// NodeRoles Returns the roles the node is running as.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be part of the core service rather than the network service? maybe? we pass this configuration value down from the dot configuration to the core and network services and the network service is less the source of truth for this sort of thing than core but probably ok as is - kinda just thinking this out loud.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to leave this as is for now. I agree it makes sense to include this in core, however the RPC module that's using this doesn't have reference to coreAPI, it currently has access to networkAPI and systemAPI.

func (s *Service) NodeRoles() byte {
return s.cfg.Roles
}
13 changes: 13 additions & 0 deletions dot/network/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ChainSafe/gossamer/lib/common/variadic"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/require"
)

var TestProtocolID = "/gossamer/test/0"
Expand Down Expand Up @@ -259,3 +260,15 @@ func TestHandleMessage_BlockResponse(t *testing.T) {
t.Error("timeout waiting for message")
}
}

func TestService_NodeRoles(t *testing.T) {
dataDir := utils.NewTestDataDir(t, "node")
cfg := &Config{
DataDir: dataDir,
Roles: 1,
}
svc := createTestService(t, cfg)

role := svc.NodeRoles()
require.Equal(t, cfg.Roles, role)
}
1 change: 1 addition & 0 deletions dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type NetworkAPI interface {
Health() common.Health
NetworkState() common.NetworkState
Peers() []common.PeerInfo
NodeRoles() byte
}

// TransactionQueueAPI ...
Expand Down
23 changes: 23 additions & 0 deletions dot/rpc/modules/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,26 @@ func (sm *SystemModule) Peers(r *http.Request, req *EmptyRequest, res *SystemPee
res.Peers = peers
return nil
}

// NodeRoles Returns the roles the node is running as.
func (sm *SystemModule) NodeRoles(r *http.Request, req *EmptyRequest, res *[]interface{}) error {
resultArray := []interface{}{}

role := sm.networkAPI.NodeRoles()
switch role {
case 1:
resultArray = append(resultArray, "Full")
case 2:
resultArray = append(resultArray, "LightClient")
case 4:
resultArray = append(resultArray, "Authority")
default:
resultArray = append(resultArray, "UnknownRole")
uknrole := []interface{}{}
uknrole = append(uknrole, role)
resultArray = append(resultArray, uknrole)
}

*res = resultArray
return nil
}
21 changes: 18 additions & 3 deletions dot/rpc/modules/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/stretchr/testify/require"
)

var (
Expand Down Expand Up @@ -62,7 +63,8 @@ func TestSystemModule_Health(t *testing.T) {
sys := NewSystemModule(net, nil)

res := &SystemHealthResponse{}
sys.Health(nil, nil, res)
err := sys.Health(nil, nil, res)
require.NoError(t, err)

if res.Health != testHealth {
t.Errorf("System.Health.: expected: %+v got: %+v\n", testHealth, res.Health)
Expand All @@ -75,7 +77,8 @@ func TestSystemModule_NetworkState(t *testing.T) {
sys := NewSystemModule(net, nil)

res := &SystemNetworkStateResponse{}
sys.NetworkState(nil, nil, res)
err := sys.NetworkState(nil, nil, res)
require.NoError(t, err)

testNetworkState := net.NetworkState()

Expand All @@ -90,9 +93,21 @@ func TestSystemModule_Peers(t *testing.T) {
sys := NewSystemModule(net, nil)

res := &SystemPeersResponse{}
sys.Peers(nil, nil, res)
err := sys.Peers(nil, nil, res)
require.NoError(t, err)

if len(res.Peers) != len(testPeers) {
t.Errorf("System.Peers: expected: %+v got: %+v\n", testPeers, res.Peers)
}
}

func TestSystemModule_NodeRoles(t *testing.T) {
net := newNetworkService(t)
sys := NewSystemModule(net, nil)
expected := []interface{}{"Full"}

var res []interface{}
err := sys.NodeRoles(nil, nil, &res)
require.NoError(t, err)
require.Equal(t, expected, res)
}
2 changes: 1 addition & 1 deletion dot/rpc/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestNewService(t *testing.T) {
}

func TestService_Methods(t *testing.T) {
qtySystemMethods := 7
qtySystemMethods := 8
qtyRPCMethods := 1
qtyAuthorMethods := 6

Expand Down