Skip to content

Commit

Permalink
Fix StatusCommand (#7974)
Browse files Browse the repository at this point in the history
Co-authored-by: SaReN <sahithnarahari@gmail.com>
  • Loading branch information
amaury1093 and sahith-narahari authored Nov 18, 2020
1 parent 87315a6 commit 35183a3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
47 changes: 47 additions & 0 deletions client/rpc/rpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package rpc_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/client/rpc"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
)

type IntegrationTestSuite struct {
suite.Suite

network *network.Network
}

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

s.network = network.New(s.T(), network.DefaultConfig())
s.Require().NotNil(s.network)

s.Require().NoError(s.network.WaitForNextBlock())
}

func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}

func (s *IntegrationTestSuite) TestStatusCommand() {
val0 := s.network.Validators[0]
cmd := rpc.StatusCommand()

out, err := clitestutil.ExecTestCLICmd(val0.ClientCtx, cmd, []string{})
s.Require().NoError(err)

// Make sure the output has the validator moniker.
s.Require().Contains(out.String(), fmt.Sprintf("\"moniker\":\"%s\"", val0.Moniker))
}

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
43 changes: 37 additions & 6 deletions client/rpc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@ package rpc

import (
"context"
"fmt"
"net/http"

"github.com/spf13/cobra"

"github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/p2p"
ctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec/legacy"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/version"

"github.com/tendermint/tendermint/p2p"
)

// ValidatorInfo is info about the node's validator, same as Tendermint,
// except that we use our own PubKey.
type validatorInfo struct {
Address bytes.HexBytes
PubKey cryptotypes.PubKey
VotingPower int64
}

// ResultStatus is node's info, same as Tendermint, except that we use our own
// PubKey.
type resultStatus struct {
NodeInfo p2p.DefaultNodeInfo
SyncInfo ctypes.SyncInfo
ValidatorInfo validatorInfo
}

// StatusCommand returns the command to return the status of the network.
func StatusCommand() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -31,12 +47,27 @@ func StatusCommand() *cobra.Command {
return err
}

output, err := legacy.Cdc.MarshalJSON(status)
// `status` has TM pubkeys, we need to convert them to our pubkeys.
pk, err := cryptocodec.FromTmPubKeyInterface(status.ValidatorInfo.PubKey)
if err != nil {
return err
}
statusWithPk := resultStatus{
NodeInfo: status.NodeInfo,
SyncInfo: status.SyncInfo,
ValidatorInfo: validatorInfo{
Address: status.ValidatorInfo.Address,
PubKey: pk,
VotingPower: status.ValidatorInfo.VotingPower,
},
}

output, err := clientCtx.LegacyAmino.MarshalJSON(statusWithPk)
if err != nil {
return err
}

fmt.Println(string(output))
cmd.Println(string(output))
return nil
},
}
Expand Down

0 comments on commit 35183a3

Please sign in to comment.