Skip to content

Commit f8a61d1

Browse files
committed
fix(cli): show zero-value fields in slashing signing-info output
1 parent 59de456 commit f8a61d1

File tree

4 files changed

+224
-3
lines changed

4 files changed

+224
-3
lines changed

app/app.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
customante "github.com/classic-terra/core/v4/custom/auth/ante"
4747
custompost "github.com/classic-terra/core/v4/custom/auth/post"
4848
customauthtx "github.com/classic-terra/core/v4/custom/auth/tx"
49+
customslashing "github.com/classic-terra/core/v4/custom/slashing"
4950
customserver "github.com/classic-terra/core/v4/server"
5051
abci "github.com/cometbft/cometbft/abci/types"
5152
tmjson "github.com/cometbft/cometbft/libs/json"
@@ -465,9 +466,11 @@ func (app *TerraApp) SimulationManager() *module.SimulationManager {
465466
// (e.g., with codecs) to construct tx/query commands safely.
466467
func (app *TerraApp) BasicModuleManager() module.BasicManager {
467468
// Use the SDK helper which extracts module basics (with initialized codecs)
468-
// from the module manager, ensuring CLI commands from upstream modules are
469-
// wired correctly.
470-
return module.NewBasicManagerFromManager(app.mm, nil)
469+
// from the module manager, while overriding select modules with our custom
470+
// CLI basics where we intentionally diverge from upstream behavior.
471+
return module.NewBasicManagerFromManager(app.mm, map[string]module.AppModuleBasic{
472+
"slashing": customslashing.AppModuleBasic{},
473+
})
471474
}
472475

473476
// RegisterAPIRoutes registers all application module routes with the provided
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/cosmos/cosmos-sdk/client"
8+
"github.com/cosmos/cosmos-sdk/client/flags"
9+
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
10+
sdk "github.com/cosmos/cosmos-sdk/types"
11+
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
// GetQueryCmd returns the slashing query commands.
16+
func GetQueryCmd() *cobra.Command {
17+
slashingQueryCmd := &cobra.Command{
18+
Use: slashingtypes.ModuleName,
19+
Short: fmt.Sprintf("Querying commands for the %s module", slashingtypes.ModuleName),
20+
DisableFlagParsing: true,
21+
SuggestionsMinimumDistance: 2,
22+
RunE: client.ValidateCmd,
23+
}
24+
25+
slashingQueryCmd.AddCommand(
26+
GetCmdQueryParams(),
27+
GetCmdQuerySigningInfo(),
28+
GetCmdQuerySigningInfos(),
29+
)
30+
31+
return slashingQueryCmd
32+
}
33+
34+
// GetCmdQueryParams returns the current slashing parameters.
35+
func GetCmdQueryParams() *cobra.Command {
36+
cmd := &cobra.Command{
37+
Use: "params",
38+
Short: "Query the current slashing parameters",
39+
Args: cobra.NoArgs,
40+
RunE: func(cmd *cobra.Command, _ []string) error {
41+
clientCtx, err := client.GetClientQueryContext(cmd)
42+
if err != nil {
43+
return err
44+
}
45+
46+
queryClient := slashingtypes.NewQueryClient(clientCtx)
47+
res, err := queryClient.Params(cmd.Context(), &slashingtypes.QueryParamsRequest{})
48+
if err != nil {
49+
return err
50+
}
51+
52+
return clientCtx.PrintProto(res)
53+
},
54+
}
55+
56+
flags.AddQueryFlagsToCmd(cmd)
57+
58+
return cmd
59+
}
60+
61+
// GetCmdQuerySigningInfo queries a validator's signing information.
62+
func GetCmdQuerySigningInfo() *cobra.Command {
63+
cmd := &cobra.Command{
64+
Use: "signing-info [validator-conspub/address]",
65+
Short: "Query a validator's signing information",
66+
Long: "Query a validator's signing information, with a pubkey ('<appd> comet show-validator') or a validator consensus address",
67+
Args: cobra.ExactArgs(1),
68+
RunE: func(cmd *cobra.Command, args []string) error {
69+
clientCtx, err := client.GetClientQueryContext(cmd)
70+
if err != nil {
71+
return err
72+
}
73+
74+
consAddr, err := normalizeConsAddressArg(clientCtx, args[0])
75+
if err != nil {
76+
return err
77+
}
78+
79+
queryClient := slashingtypes.NewQueryClient(clientCtx)
80+
res, err := queryClient.SigningInfo(
81+
cmd.Context(),
82+
&slashingtypes.QuerySigningInfoRequest{ConsAddress: consAddr},
83+
)
84+
if err != nil {
85+
return err
86+
}
87+
88+
return clientCtx.PrintProto(res)
89+
},
90+
}
91+
92+
flags.AddQueryFlagsToCmd(cmd)
93+
94+
return cmd
95+
}
96+
97+
// GetCmdQuerySigningInfos queries signing information for all validators.
98+
func GetCmdQuerySigningInfos() *cobra.Command {
99+
cmd := &cobra.Command{
100+
Use: "signing-infos",
101+
Short: "Query signing information of all validators",
102+
Args: cobra.NoArgs,
103+
RunE: func(cmd *cobra.Command, _ []string) error {
104+
clientCtx, err := client.GetClientQueryContext(cmd)
105+
if err != nil {
106+
return err
107+
}
108+
109+
pageReq, err := client.ReadPageRequest(cmd.Flags())
110+
if err != nil {
111+
return err
112+
}
113+
114+
queryClient := slashingtypes.NewQueryClient(clientCtx)
115+
res, err := queryClient.SigningInfos(
116+
cmd.Context(),
117+
&slashingtypes.QuerySigningInfosRequest{Pagination: pageReq},
118+
)
119+
if err != nil {
120+
return err
121+
}
122+
123+
return clientCtx.PrintProto(res)
124+
},
125+
}
126+
127+
flags.AddQueryFlagsToCmd(cmd)
128+
flags.AddPaginationFlagsToCmd(cmd, "signing infos")
129+
130+
return cmd
131+
}
132+
133+
func normalizeConsAddressArg(clientCtx client.Context, arg string) (string, error) {
134+
arg = strings.TrimSpace(arg)
135+
136+
if _, err := sdk.ConsAddressFromBech32(arg); err == nil {
137+
return arg, nil
138+
}
139+
140+
var pubKey cryptotypes.PubKey
141+
if err := clientCtx.Codec.UnmarshalInterfaceJSON([]byte(arg), &pubKey); err == nil && pubKey != nil {
142+
return sdk.GetConsAddress(pubKey).String(), nil
143+
}
144+
145+
return "", fmt.Errorf("invalid consensus address or pubkey JSON: %q", arg)
146+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cli
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/cosmos/cosmos-sdk/client"
8+
"github.com/cosmos/cosmos-sdk/codec"
9+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
10+
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
11+
"github.com/cosmos/cosmos-sdk/std"
12+
sdk "github.com/cosmos/cosmos-sdk/types"
13+
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestNormalizeConsAddressArg_AcceptsConsensusAddress(t *testing.T) {
18+
clientCtx := makeTestClientContext()
19+
pubKey := ed25519.GenPrivKey().PubKey()
20+
consAddr := sdk.GetConsAddress(pubKey).String()
21+
22+
got, err := normalizeConsAddressArg(clientCtx, consAddr)
23+
require.NoError(t, err)
24+
require.Equal(t, consAddr, got)
25+
}
26+
27+
func TestNormalizeConsAddressArg_AcceptsConsensusPubKeyJSON(t *testing.T) {
28+
clientCtx := makeTestClientContext()
29+
pubKey := ed25519.GenPrivKey().PubKey()
30+
31+
bz, err := clientCtx.Codec.MarshalInterfaceJSON(pubKey)
32+
require.NoError(t, err)
33+
34+
got, err := normalizeConsAddressArg(clientCtx, string(bz))
35+
require.NoError(t, err)
36+
require.Equal(t, sdk.GetConsAddress(pubKey).String(), got)
37+
}
38+
39+
func TestPrintProtoIncludesZeroValueSigningFields(t *testing.T) {
40+
clientCtx := makeTestClientContext()
41+
var out bytes.Buffer
42+
43+
clientCtx = clientCtx.WithOutput(&out).WithOutputFormat("json")
44+
45+
res := &slashingtypes.QuerySigningInfoResponse{
46+
ValSigningInfo: slashingtypes.ValidatorSigningInfo{
47+
Address: sdk.GetConsAddress(ed25519.GenPrivKey().PubKey()).String(),
48+
StartHeight: 27267533,
49+
IndexOffset: 2826250,
50+
},
51+
}
52+
53+
err := clientCtx.PrintProto(res)
54+
require.NoError(t, err)
55+
require.Contains(t, out.String(), `"tombstoned":false`)
56+
require.Contains(t, out.String(), `"missed_blocks_counter":"0"`)
57+
}
58+
59+
func makeTestClientContext() client.Context {
60+
ir := codectypes.NewInterfaceRegistry()
61+
std.RegisterInterfaces(ir)
62+
63+
return client.Context{}.WithCodec(codec.NewProtoCodec(ir))
64+
}

custom/slashing/module.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package slashing
22

33
import (
4+
"github.com/classic-terra/core/v4/custom/slashing/client/cli"
45
customtypes "github.com/classic-terra/core/v4/custom/slashing/types"
56
"github.com/cosmos/cosmos-sdk/codec"
67
"github.com/cosmos/cosmos-sdk/types/module"
78
"github.com/cosmos/cosmos-sdk/x/slashing"
9+
"github.com/spf13/cobra"
810
)
911

1012
var _ module.AppModuleBasic = AppModuleBasic{}
@@ -18,3 +20,9 @@ type AppModuleBasic struct {
1820
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
1921
customtypes.RegisterLegacyAminoCodec(cdc)
2022
}
23+
24+
// GetQueryCmd returns a manual query command so slashing queries use PrintProto
25+
// output instead of AutoCLI aminojson output, which omits zero-value fields.
26+
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
27+
return cli.GetQueryCmd()
28+
}

0 commit comments

Comments
 (0)