From 35183a37e6eda8ee461035c41d90ef36772f99cd Mon Sep 17 00:00:00 2001 From: Amaury Date: Wed, 18 Nov 2020 22:44:13 +0100 Subject: [PATCH] Fix StatusCommand (#7974) Co-authored-by: SaReN --- client/rpc/rpc_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++ client/rpc/status.go | 43 ++++++++++++++++++++++++++++++++------ 2 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 client/rpc/rpc_test.go diff --git a/client/rpc/rpc_test.go b/client/rpc/rpc_test.go new file mode 100644 index 000000000..c1c233e73 --- /dev/null +++ b/client/rpc/rpc_test.go @@ -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)) +} diff --git a/client/rpc/status.go b/client/rpc/status.go index b23442748..f390a1ab9 100644 --- a/client/rpc/status.go +++ b/client/rpc/status.go @@ -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{ @@ -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 }, }