diff --git a/dot/rpc/modules/mocks/system_api.go b/dot/rpc/modules/mocks/system_api.go new file mode 100644 index 0000000000..c4983a93f4 --- /dev/null +++ b/dot/rpc/modules/mocks/system_api.go @@ -0,0 +1,82 @@ +// Code generated by mockery v2.8.0. DO NOT EDIT. + +package mocks + +import mock "github.com/stretchr/testify/mock" + +// MockSystemAPI is an autogenerated mock type for the SystemAPI type +type MockSystemAPI struct { + mock.Mock +} + +// ChainName provides a mock function with given fields: +func (_m *MockSystemAPI) ChainName() string { + ret := _m.Called() + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// ChainType provides a mock function with given fields: +func (_m *MockSystemAPI) ChainType() string { + ret := _m.Called() + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// Properties provides a mock function with given fields: +func (_m *MockSystemAPI) Properties() map[string]interface{} { + ret := _m.Called() + + var r0 map[string]interface{} + if rf, ok := ret.Get(0).(func() map[string]interface{}); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(map[string]interface{}) + } + } + + return r0 +} + +// SystemName provides a mock function with given fields: +func (_m *MockSystemAPI) SystemName() string { + ret := _m.Called() + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// SystemVersion provides a mock function with given fields: +func (_m *MockSystemAPI) SystemVersion() string { + ret := _m.Called() + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} diff --git a/dot/rpc/modules/system.go b/dot/rpc/modules/system.go index 96d9457dd5..b7a46225c2 100644 --- a/dot/rpc/modules/system.go +++ b/dot/rpc/modules/system.go @@ -228,6 +228,24 @@ func (sm *SystemModule) AccountNextIndex(r *http.Request, req *StringRequest, re return nil } +// LocalListenAddresses Returns the libp2p multiaddresses that the local node is listening on +func (sm *SystemModule) LocalListenAddresses(r *http.Request, req *EmptyRequest, res *[]string) error { + netstate := sm.networkAPI.NetworkState() + + if len(netstate.Multiaddrs) < 1 { + return errors.New("multiaddress list is empty") + } + + addrs := make([]string, len(netstate.Multiaddrs)) + + for i, ma := range netstate.Multiaddrs { + addrs[i] = ma.String() + } + + *res = addrs + 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() diff --git a/dot/rpc/modules/system_test.go b/dot/rpc/modules/system_test.go index e0fe81acdb..fcc4cbfa75 100644 --- a/dot/rpc/modules/system_test.go +++ b/dot/rpc/modules/system_test.go @@ -39,7 +39,11 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" log "github.com/ChainSafe/log15" "github.com/btcsuite/btcutil/base58" + "github.com/multiformats/go-multiaddr" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" + + coremocks "github.com/ChainSafe/gossamer/dot/core/mocks" ) var ( @@ -149,35 +153,15 @@ var testGenesisData = &genesis.Data{ ChainType: "Local", } -type mockSystemAPI struct { - info *types.SystemInfo - genData *genesis.Data -} - -func newMockSystemAPI() *mockSystemAPI { - return &mockSystemAPI{ - info: testSystemInfo, - genData: testGenesisData, - } -} - -func (api *mockSystemAPI) SystemName() string { - return api.info.SystemName -} - -func (api *mockSystemAPI) SystemVersion() string { - return api.info.SystemVersion -} +func newMockSystemAPI() *mocks.MockSystemAPI { + sysapimock := new(mocks.MockSystemAPI) + sysapimock.On("SystemName").Return(testSystemInfo.SystemName) + sysapimock.On("SystemVersion").Return(testSystemInfo.SystemVersion) + sysapimock.On("ChainName").Return(testGenesisData.Name) + sysapimock.On("Properties").Return(nil) + sysapimock.On("ChainType").Return(testGenesisData.ChainType) -func (api *mockSystemAPI) ChainName() string { - return api.genData.Name -} -func (api *mockSystemAPI) Properties() map[string]interface{} { - return nil -} - -func (api *mockSystemAPI) ChainType() string { - return api.genData.ChainType + return sysapimock } func TestSystemModule_Chain(t *testing.T) { @@ -196,9 +180,8 @@ func TestSystemModule_ChainType(t *testing.T) { res := new(string) sys.ChainType(nil, nil, res) - require.Equal(t, api.genData.ChainType, *res) + require.Equal(t, testGenesisData.ChainType, *res) } - func TestSystemModule_Name(t *testing.T) { sys := NewSystemModule(nil, newMockSystemAPI(), nil, nil, nil) @@ -338,10 +321,6 @@ func setupSystemModule(t *testing.T) *SystemModule { return NewSystemModule(net, nil, core, chain.Storage, txQueue) } -type mockNetwork struct{} - -func (n *mockNetwork) SendMessage(_ network.NotificationsMessage) {} - func newCoreService(t *testing.T, srvc *state.Service) *core.Service { // setup service tt := trie.NewEmptyTrie() @@ -360,6 +339,9 @@ func newCoreService(t *testing.T, srvc *state.Service) *core.Service { srvc = newTestStateService(t) } + mocknet := new(coremocks.MockNetwork) + mocknet.On("SendMessage", mock.AnythingOfType("network.NotificationsMessage")) + cfg := &core.Config{ Runtime: rt, Keystore: ks, @@ -367,13 +349,37 @@ func newCoreService(t *testing.T, srvc *state.Service) *core.Service { BlockState: srvc.Block, StorageState: srvc.Storage, EpochState: srvc.Epoch, - Network: &mockNetwork{}, + Network: mocknet, CodeSubstitutedState: srvc.Base, } return core.NewTestService(t, cfg) } +func TestLocalListenAddresses(t *testing.T) { + ma, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/7001/p2p/12D3KooWCYyh5xoAc5oRyiGU4d9ktcqFQ23JjitNFR6bEcbw7YdN") + require.NoError(t, err) + + mockedNetState := common.NetworkState{ + PeerID: "fake-peer-id", + Multiaddrs: []multiaddr.Multiaddr{ma}, + } + + mockNetAPI := new(mocks.MockNetworkAPI) + mockNetAPI.On("NetworkState").Return(mockedNetState) + + res := make([]string, 0) + + sysmodule := new(SystemModule) + sysmodule.networkAPI = mockNetAPI + + err = sysmodule.LocalListenAddresses(nil, nil, &res) + require.NoError(t, err) + + require.Len(t, res, 1) + require.Equal(t, res[0], ma.String()) +} + func TestLocalPeerId(t *testing.T) { peerID := "12D3KooWBrwpqLE9Z23NEs59m2UHUs9sGYWenxjeCk489Xq7SG2h" encoded := base58.Encode([]byte(peerID)) diff --git a/dot/rpc/service_test.go b/dot/rpc/service_test.go index b6ffad9425..c72a6a607c 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 := 11 + qtySystemMethods := 12 qtyRPCMethods := 1 qtyAuthorMethods := 7 diff --git a/go.sum b/go.sum index dc3f2bccbb..69581e72c2 100644 --- a/go.sum +++ b/go.sum @@ -273,7 +273,6 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= -github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=