Skip to content

Commit

Permalink
feat(x/slashing): add autocli options for tx (#17967)
Browse files Browse the repository at this point in the history
(cherry picked from commit 3690d9f)

# Conflicts:
#	client/v2/autocli/msg.go
#	x/slashing/client/cli/tx_test.go
#	x/slashing/module.go
  • Loading branch information
julienrbrt authored and mergify[bot] committed Oct 9, 2023
1 parent daaff08 commit dfc4432
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 70 deletions.
25 changes: 19 additions & 6 deletions client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import (
"github.com/cosmos/cosmos-sdk/runtime"
)

const (
AddressStringScalarType = "cosmos.AddressString"
ValidatorAddressStringScalarType = "cosmos.ValidatorAddressString"
ConsensusAddressStringScalarType = "cosmos.ConsensusAddressString"
)

// Builder manages options for building pflag flags for protobuf messages.
type Builder struct {
// TypeResolver specifies how protobuf types will be resolved. If it is
Expand Down Expand Up @@ -61,9 +67,9 @@ func (b *Builder) init() {

if b.scalarFlagTypes == nil {
b.scalarFlagTypes = map[string]Type{}
b.scalarFlagTypes["cosmos.AddressString"] = addressStringType{}
b.scalarFlagTypes["cosmos.ValidatorAddressString"] = validatorAddressStringType{}
b.scalarFlagTypes["cosmos.ConsensusAddressString"] = consensusAddressStringType{}
b.scalarFlagTypes[AddressStringScalarType] = addressStringType{}
b.scalarFlagTypes[ValidatorAddressStringScalarType] = validatorAddressStringType{}
b.scalarFlagTypes[ConsensusAddressStringScalarType] = consensusAddressStringType{}
}
}

Expand Down Expand Up @@ -387,10 +393,10 @@ func (b *Builder) resolveFlagType(field protoreflect.FieldDescriptor) Type {
}

func (b *Builder) resolveFlagTypeBasic(field protoreflect.FieldDescriptor) Type {
scalar := proto.GetExtension(field.Options(), cosmos_proto.E_Scalar)
if scalar != nil {
scalar, ok := GetScalarType(field)
if ok {
b.init()
if typ, ok := b.scalarFlagTypes[scalar.(string)]; ok {
if typ, ok := b.scalarFlagTypes[scalar]; ok {
return typ
}
}
Expand All @@ -413,6 +419,13 @@ func (b *Builder) resolveFlagTypeBasic(field protoreflect.FieldDescriptor) Type
}
}

// GetScalarType gets scalar type of a field.
func GetScalarType(field protoreflect.FieldDescriptor) (string, bool) {
scalar := proto.GetExtension(field.Options(), cosmos_proto.E_Scalar)
scalarStr, ok := scalar.(string)
return scalarStr, ok
}

// GetSignerFieldName gets signer field name of a message.
// AutoCLI supports only one signer field per message.
func GetSignerFieldName(descriptor protoreflect.MessageDescriptor) string {
Expand Down
17 changes: 17 additions & 0 deletions client/v2/autocli/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,25 @@ func (b *Builder) BuildMsgMethodCommand(descriptor protoreflect.MethodDescriptor
// set signer to signer field if empty
fd := input.Descriptor().Fields().ByName(protoreflect.Name(flag.GetSignerFieldName(input.Descriptor())))
if addr := input.Get(fd).String(); addr == "" {
addressCodec := b.Builder.AddressCodec

scalarType, ok := flag.GetScalarType(fd)
if ok {
// override address codec if validator or consensus address
switch scalarType {
case flag.ValidatorAddressStringScalarType:
addressCodec = b.Builder.ValidatorAddressCodec
case flag.ConsensusAddressStringScalarType:
addressCodec = b.Builder.ConsensusAddressCodec
}
}

signerFromFlag := clientCtx.GetFromAddress()
<<<<<<< HEAD
signer, err := b.AddressCodec.BytesToString(signerFromFlag.Bytes())
=======
signer, err := addressCodec.BytesToString(signerFromFlag.Bytes())
>>>>>>> 3690d9f7f (feat(x/slashing): add autocli options for tx (#17967))
if err != nil {
return fmt.Errorf("failed to set signer on message, got %v: %w", signerFromFlag, err)
}
Expand Down
15 changes: 15 additions & 0 deletions x/slashing/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,20 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
},
},
},
Tx: &autocliv1.ServiceCommandDescriptor{
Service: slashingv1beta.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Unjail",
Use: "unjail",
Short: "Unjail a jailed validator",
Example: fmt.Sprintf("%s tx slashing unjail --from [validator]", version.AppName),
},
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
},
},
},
}
}
5 changes: 0 additions & 5 deletions x/slashing/client/cli/flags.go

This file was deleted.

58 changes: 0 additions & 58 deletions x/slashing/client/cli/tx.go

This file was deleted.

7 changes: 6 additions & 1 deletion x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"

Check failure on line 8 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / dependency-review

could not import github.com/grpc-ecosystem/grpc-gateway/runtime (no metadata for github.com/grpc-ecosystem/grpc-gateway/runtime)
"github.com/spf13/cobra"

modulev1 "cosmossdk.io/api/cosmos/slashing/module/v1"
"cosmossdk.io/core/appmodule"
Expand All @@ -21,8 +20,11 @@ import (
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
<<<<<<< HEAD

Check failure on line 23 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / split-test-files

missing import path

Check failure on line 23 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path

Check failure on line 23 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path

Check failure on line 23 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path
"github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
"github.com/cosmos/cosmos-sdk/x/slashing/exported"
=======

Check failure on line 26 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path
>>>>>>> 3690d9f7f (feat(x/slashing): add autocli options for tx (#17967))

Check failure on line 27 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected ';', found ':'

Check failure on line 27 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected declaration, found 'for'

Check failure on line 27 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / dependency-review

illegal character U+0023 '#'
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/simulation"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
Expand Down Expand Up @@ -85,11 +87,14 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *g
}
}

<<<<<<< HEAD

Check failure on line 90 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected declaration, found '<<'
// GetTxCmd returns the root tx command for the slashing module.
func (b AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.NewTxCmd(b.cdc.InterfaceRegistry().SigningContext().ValidatorAddressCodec())
}

=======
>>>>>>> 3690d9f7f (feat(x/slashing): add autocli options for tx (#17967))

Check failure on line 97 in x/slashing/module.go

View workflow job for this annotation

GitHub Actions / dependency-review

illegal character U+0023 '#'
// AppModule implements an application module for the slashing module.
type AppModule struct {
AppModuleBasic
Expand Down

0 comments on commit dfc4432

Please sign in to comment.