diff --git a/dot/rpc/modules/mocks/network_api.go b/dot/rpc/modules/mocks/network_api.go new file mode 100644 index 0000000000..dd9fa4fa40 --- /dev/null +++ b/dot/rpc/modules/mocks/network_api.go @@ -0,0 +1,113 @@ +// Code generated by mockery v2.8.0. DO NOT EDIT. + +package mocks + +import ( + common "github.com/ChainSafe/gossamer/lib/common" + mock "github.com/stretchr/testify/mock" +) + +// MockNetworkAPI is an autogenerated mock type for the NetworkAPI type +type MockNetworkAPI struct { + mock.Mock +} + +// Health provides a mock function with given fields: +func (_m *MockNetworkAPI) Health() common.Health { + ret := _m.Called() + + var r0 common.Health + if rf, ok := ret.Get(0).(func() common.Health); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(common.Health) + } + + return r0 +} + +// IsStopped provides a mock function with given fields: +func (_m *MockNetworkAPI) IsStopped() bool { + ret := _m.Called() + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// NetworkState provides a mock function with given fields: +func (_m *MockNetworkAPI) NetworkState() common.NetworkState { + ret := _m.Called() + + var r0 common.NetworkState + if rf, ok := ret.Get(0).(func() common.NetworkState); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(common.NetworkState) + } + + return r0 +} + +// NodeRoles provides a mock function with given fields: +func (_m *MockNetworkAPI) NodeRoles() byte { + ret := _m.Called() + + var r0 byte + if rf, ok := ret.Get(0).(func() byte); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(byte) + } + + return r0 +} + +// Peers provides a mock function with given fields: +func (_m *MockNetworkAPI) Peers() []common.PeerInfo { + ret := _m.Called() + + var r0 []common.PeerInfo + if rf, ok := ret.Get(0).(func() []common.PeerInfo); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]common.PeerInfo) + } + } + + return r0 +} + +// Start provides a mock function with given fields: +func (_m *MockNetworkAPI) Start() error { + ret := _m.Called() + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Stop provides a mock function with given fields: +func (_m *MockNetworkAPI) Stop() error { + ret := _m.Called() + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} diff --git a/dot/rpc/modules/system.go b/dot/rpc/modules/system.go index 46a9b3e23a..96d9457dd5 100644 --- a/dot/rpc/modules/system.go +++ b/dot/rpc/modules/system.go @@ -25,6 +25,7 @@ import ( "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto" "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/btcsuite/btcutil/base58" ctypes "github.com/centrifuge/go-substrate-rpc-client/v3/types" ) @@ -226,3 +227,14 @@ func (sm *SystemModule) AccountNextIndex(r *http.Request, req *StringRequest, re *res = U64Response(accountInfo.Nonce) return nil } + +// LocalPeerId Returns the base58-encoded PeerId fo the node. +func (sm *SystemModule) LocalPeerId(r *http.Request, req *EmptyRequest, res *string) error { //nolint + netstate := sm.networkAPI.NetworkState() + if netstate.PeerID == "" { + return errors.New("peer id cannot be empty") + } + + *res = base58.Encode([]byte(netstate.PeerID)) + return nil +} diff --git a/dot/rpc/modules/system_test.go b/dot/rpc/modules/system_test.go index cd0c41fe46..e0fe81acdb 100644 --- a/dot/rpc/modules/system_test.go +++ b/dot/rpc/modules/system_test.go @@ -26,6 +26,7 @@ import ( "github.com/ChainSafe/gossamer/dot/core" "github.com/ChainSafe/gossamer/dot/network" + "github.com/ChainSafe/gossamer/dot/rpc/modules/mocks" "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" @@ -37,6 +38,7 @@ import ( "github.com/ChainSafe/gossamer/lib/trie" "github.com/ChainSafe/gossamer/pkg/scale" log "github.com/ChainSafe/log15" + "github.com/btcsuite/btcutil/base58" "github.com/stretchr/testify/require" ) @@ -371,3 +373,29 @@ func newCoreService(t *testing.T, srvc *state.Service) *core.Service { return core.NewTestService(t, cfg) } + +func TestLocalPeerId(t *testing.T) { + peerID := "12D3KooWBrwpqLE9Z23NEs59m2UHUs9sGYWenxjeCk489Xq7SG2h" + encoded := base58.Encode([]byte(peerID)) + + state := common.NetworkState{ + PeerID: peerID, + } + + mocknetAPI := new(mocks.MockNetworkAPI) + mocknetAPI.On("NetworkState").Return(state).Once() + + sysmodules := new(SystemModule) + sysmodules.networkAPI = mocknetAPI + + var res string + err := sysmodules.LocalPeerId(nil, nil, &res) + require.NoError(t, err) + + require.Equal(t, res, encoded) + + state.PeerID = "" + mocknetAPI.On("NetworkState").Return(state).Once() + err = sysmodules.LocalPeerId(nil, nil, &res) + require.Error(t, err) +} diff --git a/dot/rpc/service_test.go b/dot/rpc/service_test.go index d6c6cbed3a..b6ffad9425 100644 --- a/dot/rpc/service_test.go +++ b/dot/rpc/service_test.go @@ -33,7 +33,7 @@ func TestNewService(t *testing.T) { } func TestService_Methods(t *testing.T) { - qtySystemMethods := 10 + qtySystemMethods := 11 qtyRPCMethods := 1 qtyAuthorMethods := 7