From 8d123fb5045942b08334c2e9065279c182453996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Mon, 12 Jul 2021 15:12:39 -0400 Subject: [PATCH 1/5] chore: add local peer id rpc call --- dot/rpc/modules/mocks/network_api.go | 113 +++++++++++++++++++++++++++ dot/rpc/modules/system.go | 11 +++ dot/rpc/modules/system_test.go | 23 ++++++ 3 files changed, 147 insertions(+) create mode 100644 dot/rpc/modules/mocks/network_api.go 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..0c661e93f0 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,13 @@ func (sm *SystemModule) AccountNextIndex(r *http.Request, req *StringRequest, re *res = U64Response(accountInfo.Nonce) return nil } + +func (sm *SystemModule) LocalPeerId(r *http.Request, req *EmptyRequest, res *string) error { + 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..0cfc76aeb2 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,24 @@ 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) + + sysmodules := new(SystemModule) + sysmodules.networkAPI = mocknetAPI + + var res string + err := sysmodules.LocalPeerId(nil, nil, &res) + require.NoError(t, err) + + require.Equal(t, res, encoded) +} From 52d934927fa1845d72c7394b0ddfe7a60d64a21b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Mon, 12 Jul 2021 15:13:27 -0400 Subject: [PATCH 2/5] chore: add func desc --- dot/rpc/modules/system.go | 1 + 1 file changed, 1 insertion(+) diff --git a/dot/rpc/modules/system.go b/dot/rpc/modules/system.go index 0c661e93f0..d37db4086c 100644 --- a/dot/rpc/modules/system.go +++ b/dot/rpc/modules/system.go @@ -228,6 +228,7 @@ func (sm *SystemModule) AccountNextIndex(r *http.Request, req *StringRequest, re return nil } +// LocalPeerId Returns the base58-encoded PeerId fo the node. func (sm *SystemModule) LocalPeerId(r *http.Request, req *EmptyRequest, res *string) error { netstate := sm.networkAPI.NetworkState() if netstate.PeerID == "" { From 423b8b0e15e911801ff03e0a0862f6d90e2deff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Mon, 12 Jul 2021 15:16:00 -0400 Subject: [PATCH 3/5] cover empty test case --- dot/rpc/modules/system_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dot/rpc/modules/system_test.go b/dot/rpc/modules/system_test.go index 0cfc76aeb2..e0fe81acdb 100644 --- a/dot/rpc/modules/system_test.go +++ b/dot/rpc/modules/system_test.go @@ -383,7 +383,7 @@ func TestLocalPeerId(t *testing.T) { } mocknetAPI := new(mocks.MockNetworkAPI) - mocknetAPI.On("NetworkState").Return(state) + mocknetAPI.On("NetworkState").Return(state).Once() sysmodules := new(SystemModule) sysmodules.networkAPI = mocknetAPI @@ -393,4 +393,9 @@ func TestLocalPeerId(t *testing.T) { 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) } From a2208bca9e98f259593bfaccaaf8f52bed5164f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Mon, 12 Jul 2021 15:33:14 -0400 Subject: [PATCH 4/5] chore: ignore Id on func name --- dot/rpc/modules/system.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dot/rpc/modules/system.go b/dot/rpc/modules/system.go index d37db4086c..96d9457dd5 100644 --- a/dot/rpc/modules/system.go +++ b/dot/rpc/modules/system.go @@ -229,7 +229,7 @@ func (sm *SystemModule) AccountNextIndex(r *http.Request, req *StringRequest, re } // LocalPeerId Returns the base58-encoded PeerId fo the node. -func (sm *SystemModule) LocalPeerId(r *http.Request, req *EmptyRequest, res *string) error { +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") From 8268d7cad3cd20c5f0a7dc7b0559d1bb0cfc5110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20J=C3=BAnior?= Date: Mon, 12 Jul 2021 18:11:08 -0400 Subject: [PATCH 5/5] chore: adjusts tests --- dot/rpc/service_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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