feat(slashing): autocli query config (backport #16659) (#17005)

This commit is contained in:
mergify[bot] 2023-07-15 09:15:20 +02:00 committed by GitHub
parent e1efdfef8b
commit fbe45d635c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 294 deletions

40
x/slashing/autocli.go Normal file
View File

@ -0,0 +1,40 @@
package slashing
import (
"fmt"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
slashingv1beta "cosmossdk.io/api/cosmos/slashing/v1beta1"
"github.com/cosmos/cosmos-sdk/version"
)
func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
return &autocliv1.ModuleOptions{
Query: &autocliv1.ServiceCommandDescriptor{
Service: slashingv1beta.Query_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Params",
Use: "params",
Short: "Query the current slashing parameters",
},
{
RpcMethod: "SigningInfo",
Use: "signing-info [validator-conspub/address]",
Short: "Query a validator's signing information",
Long: "Query a validator's signing information, with a pubkey ('<appd> comet show-validator') or a validator consensus address",
Example: fmt.Sprintf(`%s query slashing signing-info '{"@type":"/cosmos.crypto.ed25519.PubKey","key":"OauFcTKbN5Lx3fJL689cikXBqe+hcp6Y+x0rYUdR9Jk="}'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "cons_address"},
},
},
{
RpcMethod: "SigningInfos",
Use: "signing-infos",
Short: "Query signing information of all validators",
},
},
},
}
}

View File

@ -1,141 +0,0 @@
package cli
import (
"strings"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
// Group slashing queries under a subcommand
slashingQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the slashing module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
slashingQueryCmd.AddCommand(
GetCmdQuerySigningInfo(),
GetCmdQueryParams(),
GetCmdQuerySigningInfos(),
)
return slashingQueryCmd
}
// GetCmdQuerySigningInfo implements the command to query signing info.
func GetCmdQuerySigningInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "signing-info [validator-conspub]",
Short: "Query a validator's signing information",
Long: strings.TrimSpace(`Use a validators' consensus public key to find the signing-info for that validator:
$ <appd> query slashing signing-info '{"@type":"/cosmos.crypto.ed25519.PubKey","key":"OauFcTKbN5Lx3fJL689cikXBqe+hcp6Y+x0rYUdR9Jk="}'
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
var pk cryptotypes.PubKey
if err := clientCtx.Codec.UnmarshalInterfaceJSON([]byte(args[0]), &pk); err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
consAddr := sdk.ConsAddress(pk.Address())
params := &types.QuerySigningInfoRequest{ConsAddress: consAddr.String()}
res, err := queryClient.SigningInfo(cmd.Context(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(&res.ValSigningInfo)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQuerySigningInfos implements the command to query signing infos.
func GetCmdQuerySigningInfos() *cobra.Command {
cmd := &cobra.Command{
Use: "signing-infos",
Short: "Query signing information of all validators",
Long: strings.TrimSpace(`signing infos of validators:
$ <appd> query slashing signing-infos
`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
params := &types.QuerySigningInfosRequest{Pagination: pageReq}
res, err := queryClient.SigningInfos(cmd.Context(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "signing infos")
return cmd
}
// GetCmdQueryParams implements a command to fetch slashing parameters.
func GetCmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "Query the current slashing parameters",
Args: cobra.NoArgs,
Long: strings.TrimSpace(`Query genesis parameters for the slashing module:
$ <appd> query slashing params
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryParamsRequest{}
res, err := queryClient.Params(cmd.Context(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(&res.Params)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

View File

@ -1,148 +0,0 @@
package cli_test
import (
"fmt"
"io"
"testing"
abci "github.com/cometbft/cometbft/abci/types"
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/types"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
)
type CLITestSuite struct {
suite.Suite
kr keyring.Keyring
baseCtx client.Context
clientCtx client.Context
encCfg testutilmod.TestEncodingConfig
pub types.PubKey
addr sdk.AccAddress
}
func TestCLITestSuite(t *testing.T) {
suite.Run(t, new(CLITestSuite))
}
func (s *CLITestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
s.encCfg = testutilmod.MakeTestEncodingConfig(slashing.AppModuleBasic{})
s.kr = keyring.NewInMemory(s.encCfg.Codec)
s.baseCtx = client.Context{}.
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")
ctxGen := func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
c := clitestutil.NewMockCometRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
}
s.clientCtx = ctxGen()
k, _, err := s.clientCtx.Keyring.NewMnemonic("NewValidator", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
s.Require().NoError(err)
pub, err := k.GetPubKey()
s.Require().NoError(err)
s.pub = pub
s.addr = sdk.AccAddress(pub.Address())
}
func (s *CLITestSuite) TestGetCmdQuerySigningInfo() {
pubKeyBz, err := s.encCfg.Codec.MarshalInterfaceJSON(s.pub)
s.Require().NoError(err)
pubKeyStr := string(pubKeyBz)
testCases := []struct {
name string
args []string
expectErr bool
}{
{"invalid address", []string{"foo"}, true},
{
"valid address (json output)",
[]string{
pubKeyStr,
fmt.Sprintf("--%s=json", flags.FlagOutput),
fmt.Sprintf("--%s=1", flags.FlagHeight),
},
false,
},
{
"valid address (text output)",
[]string{
pubKeyStr,
fmt.Sprintf("--%s=text", flags.FlagOutput),
fmt.Sprintf("--%s=1", flags.FlagHeight),
},
false,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQuerySigningInfo()
clientCtx := s.clientCtx
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
}
})
}
}
func (s *CLITestSuite) TestGetCmdQueryParams() {
testCases := []struct {
name string
args []string
}{
{
"json output",
[]string{fmt.Sprintf("--%s=json", flags.FlagOutput)},
},
{
"text output",
[]string{fmt.Sprintf("--%s=text", flags.FlagOutput)},
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryParams()
clientCtx := s.clientCtx
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
s.Require().NoError(err)
})
}
}

View File

@ -2,15 +2,77 @@ package cli_test
import (
"fmt"
"io"
"testing"
abci "github.com/cometbft/cometbft/abci/types"
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
"github.com/stretchr/testify/suite"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/types"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
)
type CLITestSuite struct {
suite.Suite
kr keyring.Keyring
baseCtx client.Context
clientCtx client.Context
encCfg testutilmod.TestEncodingConfig
pub types.PubKey
addr sdk.AccAddress
}
func TestCLITestSuite(t *testing.T) {
suite.Run(t, new(CLITestSuite))
}
func (s *CLITestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
s.encCfg = testutilmod.MakeTestEncodingConfig(slashing.AppModuleBasic{})
s.kr = keyring.NewInMemory(s.encCfg.Codec)
s.baseCtx = client.Context{}.
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")
ctxGen := func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
c := clitestutil.NewMockCometRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
}
s.clientCtx = ctxGen()
k, _, err := s.clientCtx.Keyring.NewMnemonic("NewValidator", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
s.Require().NoError(err)
pub, err := k.GetPubKey()
s.Require().NoError(err)
s.pub = pub
s.addr = sdk.AccAddress(pub.Address())
}
func (s *CLITestSuite) TestNewUnjailTxCmd() {
val := s.addr
testCases := []struct {

View File

@ -88,11 +88,6 @@ func (AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.NewTxCmd()
}
// GetQueryCmd returns no root query command for the slashing module.
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd()
}
// AppModule implements an application module for the slashing module.
type AppModule struct {
AppModuleBasic