From 4df8b373fabeb3fe153c9819ee6ead7efb64deca Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Fri, 6 Oct 2023 11:52:12 +0200 Subject: [PATCH] feat(x/circuit): add autocli options for tx (#17956) --- x/circuit/autocli.go | 40 +++++++++- x/circuit/client/cli/tx.go | 140 --------------------------------- x/circuit/go.mod | 4 +- x/circuit/keeper/msg_server.go | 4 + x/circuit/module.go | 7 -- 5 files changed, 45 insertions(+), 150 deletions(-) delete mode 100644 x/circuit/client/cli/tx.go diff --git a/x/circuit/autocli.go b/x/circuit/autocli.go index 50254dbc1024..1bb272253ad1 100644 --- a/x/circuit/autocli.go +++ b/x/circuit/autocli.go @@ -1,8 +1,12 @@ package circuit import ( + "fmt" + autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" circuitv1 "cosmossdk.io/api/cosmos/circuit/v1" + + "github.com/cosmos/cosmos-sdk/version" ) func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { @@ -29,7 +33,41 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { }, }, Tx: &autocliv1.ServiceCommandDescriptor{ - Service: circuitv1.Query_ServiceDesc.ServiceName, + Service: circuitv1.Msg_ServiceDesc.ServiceName, + RpcCommandOptions: []*autocliv1.RpcCommandOptions{ + { + RpcMethod: "AuthorizeCircuitBreaker", + Use: "authorize [grantee] [permissions_json] --from [granter]", + Short: "Authorize an account to trip the circuit breaker.", + Long: `Authorize an account to trip the circuit breaker. + "SOME_MSGS" = 1, + "ALL_MSGS" = 2, + "SUPER_ADMIN" = 3,`, + Example: fmt.Sprintf(`%s circuit authorize [address] '{"level":1,"limit_type_urls":["cosmos.bank.v1beta1.MsgSend,cosmos.bank.v1beta1.MsgMultiSend"]}'"`, version.AppName), + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "grantee"}, + {ProtoField: "permissions"}, // TODO(@julienrbrt) Support flattening msg for setting each field as a positional arg + }, + }, + { + RpcMethod: "TripCircuitBreaker", + Use: "disable [msg_type_urls]", + Short: "Disable a message from being executed", + Example: fmt.Sprintf(`%s circuit disable "cosmos.bank.v1beta1.MsgSend cosmos.bank.v1beta1.MsgMultiSend"`, version.AppName), + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "msg_type_urls", Varargs: true}, + }, + }, + { + RpcMethod: "ResetCircuitBreaker", + Use: "reset [msg_type_urls]", + Short: "Enable a message to be executed", + Example: fmt.Sprintf(`%s circuit reset "cosmos.bank.v1beta1.MsgSend cosmos.bank.v1beta1.MsgMultiSend"`, version.AppName), + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "msg_type_urls", Varargs: true}, + }, + }, + }, }, } } diff --git a/x/circuit/client/cli/tx.go b/x/circuit/client/cli/tx.go deleted file mode 100644 index f74b2e9f1f07..000000000000 --- a/x/circuit/client/cli/tx.go +++ /dev/null @@ -1,140 +0,0 @@ -package cli - -import ( - "fmt" - "strings" - - "github.com/spf13/cobra" - - "cosmossdk.io/math" - "cosmossdk.io/x/circuit/types" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/tx" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/version" -) - -// NewTxCmd returns a root CLI command handler for all x/circuit transaction commands. -func NewTxCmd() *cobra.Command { - txCmd := &cobra.Command{ - Use: types.ModuleName, - Short: "Circuit transaction subcommands", - DisableFlagParsing: true, - SuggestionsMinimumDistance: 2, - RunE: client.ValidateCmd, - } - - txCmd.AddCommand( - AuthorizeCircuitBreakerCmd(), - TripCircuitBreakerCmd(), - ) - - return txCmd -} - -// AuthorizeCircuitBreakerCmd returns a CLI command handler for creating a MsgAuthorizeCircuitBreaker transaction. -func AuthorizeCircuitBreakerCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "authorize [grantee] [permission_level] [limit_type_urls] --from [granter]", - Short: "Authorize an account to trip the circuit breaker.", - Long: `Authorize an account to trip the circuit breaker. - "SOME_MSGS" = 1, - "ALL_MSGS" = 2, - "SUPER_ADMIN" = 3,`, - Example: fmt.Sprintf(`%s circuit authorize [address] 0 "cosmos.bank.v1beta1.MsgSend,cosmos.bank.v1beta1.MsgMultiSend"`, version.AppName), - Args: cobra.RangeArgs(2, 3), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - grantee, err := sdk.AccAddressFromBech32(args[0]) - if err != nil { - return err - } - - lvl, err := math.ParseUint(args[1]) - if err != nil { - return err - } - - var typeUrls []string - if len(args) == 3 { - typeUrls = strings.Split(args[2], ",") - } - - permission := types.Permissions{Level: types.Permissions_Level(lvl.Uint64()), LimitTypeUrls: typeUrls} - - msg := &types.MsgAuthorizeCircuitBreaker{ - Granter: clientCtx.GetFromAddress().String(), - Grantee: grantee.String(), - Permissions: &permission, - } - - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -// TripCircuitBreakerCmd returns a CLI command handler for creating a MsgTripCircuitBreaker transaction. -func TripCircuitBreakerCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "disable [type_url]", - Short: "disable a message from being executed", - Example: fmt.Sprintf(`%s circuit disable "cosmos.bank.v1beta1.MsgSend,cosmos.bank.v1beta1.MsgMultiSend"`, version.AppName), - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msg := &types.MsgTripCircuitBreaker{ - Authority: clientCtx.GetFromAddress().String(), - MsgTypeUrls: strings.Split(args[0], ","), - } - - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -// ResetCircuitBreakerCmd returns a CLI command handler for creating a MsgRestCircuitBreaker transaction. -func ResetCircuitBreakerCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "reset [type_url]", - Short: "Enable a message to be executed", - Example: fmt.Sprintf(`%s circuit reset "cosmos.bank.v1beta1.MsgSend,cosmos.bank.v1beta1.MsgMultiSend"`, version.AppName), - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - msgTypeUrls := strings.Split(args[0], ",") - - msg := &types.MsgResetCircuitBreaker{ - Authority: clientCtx.GetFromAddress().String(), - MsgTypeUrls: msgTypeUrls, - } - - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} diff --git a/x/circuit/go.mod b/x/circuit/go.mod index ed7e4c6fbfe7..a969060a241e 100644 --- a/x/circuit/go.mod +++ b/x/circuit/go.mod @@ -8,7 +8,6 @@ require ( cosmossdk.io/core v0.12.0 cosmossdk.io/depinject v1.0.0-alpha.4 cosmossdk.io/errors v1.0.0 - cosmossdk.io/math v1.1.3-rc.1 cosmossdk.io/store v1.0.0-rc.0 github.com/cockroachdb/errors v1.11.1 github.com/cometbft/cometbft v0.38.0 @@ -16,7 +15,6 @@ require ( github.com/cosmos/gogoproto v1.4.11 github.com/golang/protobuf v1.5.3 github.com/grpc-ecosystem/grpc-gateway v1.16.0 - github.com/spf13/cobra v1.7.0 github.com/stretchr/testify v1.8.4 google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb google.golang.org/grpc v1.58.2 @@ -24,6 +22,7 @@ require ( require ( cosmossdk.io/log v1.2.1 // indirect + cosmossdk.io/math v1.1.3-rc.1 // indirect cosmossdk.io/x/tx v0.10.0 // indirect filippo.io/edwards25519 v1.0.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect @@ -123,6 +122,7 @@ require ( github.com/sasha-s/go-deadlock v0.3.1 // indirect github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.1 // indirect + github.com/spf13/cobra v1.7.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.16.0 // indirect diff --git a/x/circuit/keeper/msg_server.go b/x/circuit/keeper/msg_server.go index 6fda4ed3a865..0c6bb07cbc40 100644 --- a/x/circuit/keeper/msg_server.go +++ b/x/circuit/keeper/msg_server.go @@ -37,6 +37,10 @@ func (srv msgServer) AuthorizeCircuitBreaker(ctx context.Context, msg *types.Msg // Check that the authorizer has the permission level of "super admin" perms, err := srv.Permissions.Get(ctx, address) if err != nil { + if errorsmod.IsOf(err, collections.ErrNotFound) { + return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "only super admins can authorize users") + } + return nil, err } diff --git a/x/circuit/module.go b/x/circuit/module.go index 39ff742190e6..2bd0b19ba60e 100644 --- a/x/circuit/module.go +++ b/x/circuit/module.go @@ -7,14 +7,12 @@ import ( "time" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" - "github.com/spf13/cobra" modulev1 "cosmossdk.io/api/cosmos/circuit/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" "cosmossdk.io/core/store" "cosmossdk.io/depinject" - "cosmossdk.io/x/circuit/client/cli" "cosmossdk.io/x/circuit/keeper" "cosmossdk.io/x/circuit/types" @@ -74,11 +72,6 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *g } } -// GetTxCmd returns the root tx command for the circuit module. -func (AppModuleBasic) GetTxCmd() *cobra.Command { - return cli.NewTxCmd() -} - // RegisterInterfaces registers interfaces and implementations of the circuit module. func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { types.RegisterInterfaces(registry)