Skip to content
Merged
2 changes: 1 addition & 1 deletion cmd/gossamer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func gossamerAction(ctx *cli.Context) error {
return err
}

node, err := dot.NewNode(cfg, ks)
node, err := dot.NewNode(cfg, ks, ctx)
if err != nil {
log.Error("[cmd] failed to create node services", "error", err)
return err
Expand Down
6 changes: 4 additions & 2 deletions dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"path"
"syscall"

"github.com/urfave/cli"

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/lib/common"
Expand Down Expand Up @@ -170,7 +172,7 @@ func NodeInitialized(datadir string, expected bool) bool {
}

// NewNode creates a new dot node from a dot node configuration
func NewNode(cfg *Config, ks *keystore.Keystore) (*Node, error) {
func NewNode(cfg *Config, ks *keystore.Keystore, ctx *cli.Context) (*Node, error) {
Comment thread
edwardmack marked this conversation as resolved.
Outdated

// if authority node, should have at least 1 key in keystore
if cfg.Core.Authority && ks.NumSr25519Keys() == 0 {
Expand Down Expand Up @@ -241,7 +243,7 @@ func NewNode(cfg *Config, ks *keystore.Keystore) (*Node, error) {
if enabled := RPCServiceEnabled(cfg); enabled {

// create rpc service and append rpc service to node services
rpcSrvc := createRPCService(cfg, stateSrvc, coreSrvc, networkSrvc, rt)
rpcSrvc := createRPCService(cfg, stateSrvc, coreSrvc, networkSrvc, rt, ctx)
nodeSrvcs = append(nodeSrvcs, rpcSrvc)

} else {
Expand Down
8 changes: 4 additions & 4 deletions dot/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestNewNode(t *testing.T) {
// TODO: improve dot tests #687
cfg.Core.Authority = false

_, err = NewNode(cfg, ks)
_, err = NewNode(cfg, ks, nil)
require.Nil(t, err)
}

Expand All @@ -121,7 +121,7 @@ func TestStartNode(t *testing.T) {
// TODO: improve dot tests #687
cfg.Core.Authority = false

node, err := NewNode(cfg, ks)
node, err := NewNode(cfg, ks, nil)
require.Nil(t, err)

go node.Start()
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestInitNode_LoadStorageRoot(t *testing.T) {
ks := keystore.NewKeystore()
require.NotNil(t, ks)

node, err := NewNode(cfg, ks)
node, err := NewNode(cfg, ks, nil)
require.Nil(t, err)

if reflect.TypeOf(node) != reflect.TypeOf(&Node{}) {
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestInitNode_LoadBalances(t *testing.T) {
ks := keystore.NewKeystore()
require.NotNil(t, ks)

node, err := NewNode(cfg, ks)
node, err := NewNode(cfg, ks, nil)
require.Nil(t, err)

if reflect.TypeOf(node) != reflect.TypeOf(&Node{}) {
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (h *HTTPServer) RegisterModules(mods []string) {
var srvc interface{}
switch mod {
case "system":
srvc = modules.NewSystemModule(h.serverConfig.NetworkAPI)
srvc = modules.NewSystemModule(h.serverConfig.NetworkAPI, h.serverConfig.RPCAPI)
case "author":
srvc = modules.NewAuthorModule(h.serverConfig.CoreAPI, h.serverConfig.RuntimeAPI, h.serverConfig.TransactionQueueAPI)
case "chain":
Expand Down
9 changes: 8 additions & 1 deletion dot/rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ import (
"time"

"github.com/stretchr/testify/require"
"github.com/urfave/cli"
)

func TestNewHTTPServer(t *testing.T) {
ctx := &cli.Context{
App: &cli.App{
Name: "gossamer",
Version: "0.0.1",
},
}

cfg := &HTTPServerConfig{
Modules: []string{"system"},
RPCPort: 8545,
RPCAPI: NewService(),
RPCAPI: NewService(ctx, "gssmr"),
}
s := NewHTTPServer(cfg)
err := s.Start()
Expand Down
3 changes: 3 additions & 0 deletions dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type CoreAPI interface {
type RPCAPI interface {
Methods() []string
BuildMethodNames(rcvr interface{}, name string)
SystemName() string
SystemVersion() string
NodeName() string
}

// RuntimeAPI is the interface for runtime methods
Expand Down
16 changes: 9 additions & 7 deletions dot/rpc/modules/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const NOT_IMPLEMENTED = "not yet implemented"
// SystemModule is an RPC module providing access to core API points
type SystemModule struct {
networkAPI NetworkAPI
rpcAPI RPCAPI
}

// EmptyRequest represents an RPC request with no fields
Expand Down Expand Up @@ -59,21 +60,22 @@ type SystemPropertiesResponse struct {
}

// NewSystemModule creates a new API instance
func NewSystemModule(net NetworkAPI) *SystemModule {
func NewSystemModule(net NetworkAPI, rpc RPCAPI) *SystemModule {
return &SystemModule{
networkAPI: net, // TODO: migrate to network state
rpcAPI: rpc,
}
}

// Chain returns the runtime chain
func (sm *SystemModule) Chain(r *http.Request, req *EmptyRequest, res *StringResponse) error {
*res = NOT_IMPLEMENTED
func (sm *SystemModule) Chain(r *http.Request, req *EmptyRequest, res *string) error {
*res = sm.rpcAPI.NodeName()
return nil
}

// Name returns the runtime name
func (sm *SystemModule) Name(r *http.Request, req *EmptyRequest, res *StringResponse) error {
*res = "gossamer v0.0"
func (sm *SystemModule) Name(r *http.Request, req *EmptyRequest, res *string) error {
*res = sm.rpcAPI.SystemName()
return nil
}

Expand All @@ -84,8 +86,8 @@ func (sm *SystemModule) Properties(r *http.Request, req *EmptyRequest, res *Stri
}

// Version returns the runtime version
func (sm *SystemModule) Version(r *http.Request, req *EmptyRequest, res *StringResponse) error {
*res = NOT_IMPLEMENTED
func (sm *SystemModule) Version(r *http.Request, req *EmptyRequest, res *string) error {
*res = sm.rpcAPI.SystemVersion()
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions dot/rpc/modules/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func newNetworkService(t *testing.T) *network.Service {
// Test RPC's System.Health() response
func TestSystemModule_Health(t *testing.T) {
net := newNetworkService(t)
sys := NewSystemModule(net)
sys := NewSystemModule(net, nil)

res := &SystemHealthResponse{}
sys.Health(nil, nil, res)
Expand All @@ -72,7 +72,7 @@ func TestSystemModule_Health(t *testing.T) {
// Test RPC's System.NetworkState() response
func TestSystemModule_NetworkState(t *testing.T) {
net := newNetworkService(t)
sys := NewSystemModule(net)
sys := NewSystemModule(net, nil)

res := &SystemNetworkStateResponse{}
sys.NetworkState(nil, nil, res)
Expand All @@ -87,7 +87,7 @@ func TestSystemModule_NetworkState(t *testing.T) {
// Test RPC's System.Peers() response
func TestSystemModule_Peers(t *testing.T) {
net := newNetworkService(t)
sys := NewSystemModule(net)
sys := NewSystemModule(net, nil)

res := &SystemPeersResponse{}
sys.Peers(nil, nil, res)
Expand Down
36 changes: 34 additions & 2 deletions dot/rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,55 @@ import (
"strings"
"unicode"
"unicode/utf8"

"github.com/urfave/cli"
)

// Service struct to hold rpc service data
type Service struct {
rpcMethods []string // list of method names offered by rpc
systemInfo *systemInfo
}

type systemInfo struct {
systemName string
systemVersion string
nodeName string
}

// NewService create a new instance of Service
func NewService() *Service {
return &Service{rpcMethods: []string{}}
func NewService(ctx *cli.Context, nodeName string) *Service {
si := &systemInfo{
systemName: ctx.App.Name,
systemVersion: ctx.App.Version,
nodeName: nodeName,
}
return &Service{
rpcMethods: []string{},
systemInfo: si,
}
}

// Methods returns list of methods available via RPC call
func (s *Service) Methods() []string {
return s.rpcMethods
}

// SystemName returns the app name
func (s *Service) SystemName() string {
return s.systemInfo.systemName
}

// SystemVersion returns the app version
func (s *Service) SystemVersion() string {
return s.systemInfo.systemVersion
}

// NodeName returns the nodeName (chain name)
func (s *Service) NodeName() string {
return s.systemInfo.nodeName
}

var (
// Precompute the reflect.Type of error and http.Request
typeOfError = reflect.TypeOf((*error)(nil)).Elem()
Expand Down
58 changes: 55 additions & 3 deletions dot/rpc/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,32 @@ import (

"github.com/ChainSafe/gossamer/dot/rpc/modules"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
)

func TestNewService(t *testing.T) {
NewService()
ctx := &cli.Context{
App: &cli.App{
Name: "gossamer",
Version: "0.0.1",
},
}
NewService(ctx, "gssmr")
}

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

rpcService := NewService()
sysMod := modules.NewSystemModule(nil)
ctx := &cli.Context{
App: &cli.App{
Name: "gossamer",
Version: "0.0.1",
},
}
rpcService := NewService(ctx, "gssmr")
sysMod := modules.NewSystemModule(nil, nil)
rpcService.BuildMethodNames(sysMod, "system")
m := rpcService.Methods()
require.Equal(t, qtySystemMethods, len(m)) // check to confirm quantity for methods is correct
Expand All @@ -47,3 +60,42 @@ func TestService_Methods(t *testing.T) {
m = rpcService.Methods()
require.Equal(t, qtySystemMethods+qtyRPCMethods+qtyAuthorMethods, len(m))
}

func TestService_NodeName(t *testing.T) {
ctx := &cli.Context{
App: &cli.App{
Name: "gossamer",
Version: "0.0.1",
},
}
rpcService := NewService(ctx, "gssmr")

name := rpcService.NodeName()
require.Equal(t, "gssmr", name)
}

func TestService_SystemName(t *testing.T) {
ctx := &cli.Context{
App: &cli.App{
Name: "gossamer",
Version: "0.0.1",
},
}
rpcService := NewService(ctx, "gssmr")

name := rpcService.SystemName()
require.Equal(t, "gossamer", name)
}

func TestService_SystemVersion(t *testing.T) {
ctx := &cli.Context{
App: &cli.App{
Name: "gossamer",
Version: "0.0.1",
},
}
rpcService := NewService(ctx, "gssmr")

ver := rpcService.SystemVersion()
require.Equal(t, "0.0.1", ver)
}
12 changes: 9 additions & 3 deletions dot/rpc/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,31 @@ import (

"github.com/gorilla/websocket"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
)

var addr = flag.String("addr", "localhost:8546", "http service address")
var testCalls = []struct {
call []byte
expected []byte
}{
{[]byte(`{"jsonrpc":"2.0","method":"system_name","params":[],"id":1}`), []byte(`{"id":1,"jsonrpc":"2.0","result":"gossamer v0.0"}` + "\n")}, // working request
{[]byte(`{"jsonrpc":"2.0","method":"system_name","params":[],"id":1}`), []byte(`{"id":1,"jsonrpc":"2.0","result":"gossamer"}` + "\n")}, // working request
{[]byte(`{"jsonrpc":"2.0","method":"unknown","params":[],"id":1}`), []byte(`{"error":{"code":-32000,"data":null,"message":"rpc error method unknown not found"},"id":1,"jsonrpc":"2.0"}` + "\n")}, // unknown method
{[]byte{}, []byte(`{"error":{"code":-32700,"data":{"id":null,"jsonrpc":"","method":"","params":null},"message":"EOF"},"id":null,"jsonrpc":"2.0"}` + "\n")}, // empty request
}

func TestNewWebSocketServer(t *testing.T) {

ctx := &cli.Context{
App: &cli.App{
Name: "gossamer",
Version: "0.0.1",
},
}
cfg := &HTTPServerConfig{
Modules: []string{"system"},
RPCPort: 8545,
WSPort: 8546,
RPCAPI: NewService(),
RPCAPI: NewService(ctx, "gssmr"),
}
s := NewHTTPServer(cfg)
err := s.Start()
Expand Down
6 changes: 4 additions & 2 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"math/big"

"github.com/urfave/cli"

"github.com/ChainSafe/gossamer/dot/core"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/rpc"
Expand Down Expand Up @@ -144,15 +146,15 @@ func createNetworkService(cfg *Config, stateSrvc *state.Service, coreMsgs chan n
// RPC Service

// createRPCService creates the RPC service from the provided core configuration
func createRPCService(cfg *Config, stateSrvc *state.Service, coreSrvc *core.Service, networkSrvc *network.Service, rt *runtime.Runtime) *rpc.HTTPServer {
func createRPCService(cfg *Config, stateSrvc *state.Service, coreSrvc *core.Service, networkSrvc *network.Service, rt *runtime.Runtime, ctx *cli.Context) *rpc.HTTPServer {
log.Info(
"[dot] creating rpc service...",
"host", cfg.RPC.Host,
"rpc port", cfg.RPC.Port,
"mods", cfg.RPC.Modules,
"ws port", cfg.RPC.WSPort,
)
rpcService := rpc.NewService()
rpcService := rpc.NewService(ctx, cfg.Global.Name)

rpcConfig := &rpc.HTTPServerConfig{
BlockAPI: stateSrvc.Block,
Expand Down
Loading