Skip to content

Commit

Permalink
feat(dot/rpc): Add system_localListenAddresses RPC call (#1689)
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior authored Jul 15, 2021
1 parent bb8021b commit c981d2e
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 37 deletions.
82 changes: 82 additions & 0 deletions dot/rpc/modules/mocks/system_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions dot/rpc/modules/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
76 changes: 41 additions & 35 deletions dot/rpc/modules/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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) {
Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand All @@ -360,20 +339,47 @@ 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,
TransactionState: srvc.Transaction,
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))
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestNewService(t *testing.T) {
}

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

Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit c981d2e

Please sign in to comment.