Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dot/rpc) Add system_localPeerId rpc call #1690

Merged
merged 5 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions dot/rpc/modules/mocks/network_api.go

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

12 changes: 12 additions & 0 deletions dot/rpc/modules/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
28 changes: 28 additions & 0 deletions dot/rpc/modules/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}
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 := 10
qtySystemMethods := 11
qtyRPCMethods := 1
qtyAuthorMethods := 7

Expand Down