Skip to content

Commit

Permalink
Merge branch 'development' into db/cname
Browse files Browse the repository at this point in the history
  • Loading branch information
dutterbutter authored Jul 14, 2021
2 parents 933e2df + 7f51e4c commit 1b5cf1d
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
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

0 comments on commit 1b5cf1d

Please sign in to comment.