diff --git a/x/bank/client/cli/tx.go b/x/bank/client/cli/tx.go index 92341a371a..3925da6379 100644 --- a/x/bank/client/cli/tx.go +++ b/x/bank/client/cli/tx.go @@ -9,7 +9,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" - clientx "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" @@ -57,7 +56,7 @@ func NewSendTxCmd(m codec.Marshaler, txg tx.Generator, ar tx.AccountRetriever) * } msg := types.NewMsgSend(cliCtx.GetFromAddress(), toAddr, coins) - return clientx.GenerateOrBroadcastTx(cliCtx, txf, msg) + return tx.GenerateOrBroadcastTx(cliCtx, txf, msg) }, } diff --git a/x/slashing/client/cli/tx.go b/x/slashing/client/cli/tx.go index 4233d5c7a0..9d8ea841d2 100644 --- a/x/slashing/client/cli/tx.go +++ b/x/slashing/client/cli/tx.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" @@ -15,7 +16,55 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing/types" ) +// NewTxCmd returns a root CLI command handler for all x/slashing transaction commands. +func NewTxCmd(m codec.Marshaler, txg tx.Generator, ar tx.AccountRetriever) *cobra.Command { + slashingTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: "Bank transaction subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + slashingTxCmd.AddCommand(NewUnjailTxCmd(m, txg, ar)) + return slashingTxCmd +} + +func NewUnjailTxCmd(m codec.Marshaler, txg tx.Generator, ar tx.AccountRetriever) *cobra.Command { + cmd := &cobra.Command{ + Use: "unjail", + Args: cobra.NoArgs, + Short: "unjail validator previously jailed for downtime", + Long: `unjail a jailed validator: + +$ tx slashing unjail --from mykey +`, + RunE: func(cmd *cobra.Command, args []string) error { + inBuf := bufio.NewReader(cmd.InOrStdin()) + txf := tx.NewFactoryFromCLI(inBuf). + WithTxGenerator(txg). + WithAccountRetriever(ar) + + cliCtx := context.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithMarshaler(m) + + valAddr := cliCtx.GetFromAddress() + msg := types.NewMsgUnjail(sdk.ValAddress(valAddr)) + return tx.GenerateOrBroadcastTx(cliCtx, txf, msg) + }, + } + return flags.PostCommands(cmd)[0] +} + +// --------------------------------------------------------------------------- +// Deprecated +// +// TODO: Remove once client-side Protobuf migration has been completed. +// --------------------------------------------------------------------------- + // GetTxCmd returns the transaction commands for this module +// +// TODO: Remove once client-side Protobuf migration has been completed. +// ref: https://github.com/cosmos/cosmos-sdk/issues/5864 func GetTxCmd(cdc *codec.Codec) *cobra.Command { slashingTxCmd := &cobra.Command{ Use: types.ModuleName, @@ -33,6 +82,9 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { } // GetCmdUnjail implements the create unjail validator command. +// +// TODO: Remove once client-side Protobuf migration has been completed. +// ref: https://github.com/cosmos/cosmos-sdk/issues/5864 func GetCmdUnjail(cdc *codec.Codec) *cobra.Command { return &cobra.Command{ Use: "unjail", diff --git a/x/slashing/client/rest/rest.go b/x/slashing/client/rest/rest.go index 4237d59e25..d4c500f84c 100644 --- a/x/slashing/client/rest/rest.go +++ b/x/slashing/client/rest/rest.go @@ -4,8 +4,15 @@ import ( "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/cosmos/cosmos-sdk/codec" ) +func RegisterHandlers(ctx context.CLIContext, m codec.Marshaler, txg tx.Generator, r *mux.Router) { + registerQueryRoutes(ctx, r) + registerTxHandlers(ctx, m, txg, r) +} + // RegisterRoutes registers staking-related REST handlers to a router func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { registerQueryRoutes(cliCtx, r) diff --git a/x/slashing/client/rest/tx.go b/x/slashing/client/rest/tx.go index 9498ea8ba5..f0ce72c6ae 100644 --- a/x/slashing/client/rest/tx.go +++ b/x/slashing/client/rest/tx.go @@ -7,17 +7,16 @@ import ( "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" authclient "github.com/cosmos/cosmos-sdk/x/auth/client" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) -func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { - r.HandleFunc( - "/slashing/validators/{validatorAddr}/unjail", - unjailRequestHandlerFn(cliCtx), - ).Methods("POST") +func registerTxHandlers(ctx context.CLIContext, m codec.Marshaler, txg tx.Generator, r *mux.Router) { + r.HandleFunc("/slashing/validators/{validatorAddr}/unjail", NewUnjailRequestHandlerFn(ctx, m, txg)).Methods("POST") } // Unjail TX body @@ -25,6 +24,64 @@ type UnjailReq struct { BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` } +// NewUnjailRequestHandlerFn returns an HTTP REST handler for creating a MsgUnjail +// transaction. +func NewUnjailRequestHandlerFn(ctx context.CLIContext, m codec.Marshaler, txg tx.Generator) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + ctx = ctx.WithMarshaler(m) + vars := mux.Vars(r) + bech32Validator := vars["validatorAddr"] + + var req UnjailReq + if !rest.ReadRESTReq(w, r, ctx.Marshaler, &req) { + return + } + + req.BaseReq = req.BaseReq.Sanitize() + if !req.BaseReq.ValidateBasic(w) { + return + } + + fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) + if err != nil { + rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + return + } + + valAddr, err := sdk.ValAddressFromBech32(bech32Validator) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + if !bytes.Equal(fromAddr, valAddr) { + rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own validator address") + return + } + + msg := types.NewMsgUnjail(valAddr) + err = msg.ValidateBasic() + if err != nil { + rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + return + } + tx.WriteGeneratedTxResponse(ctx, w, txg, req.BaseReq, msg) + } +} + +// --------------------------------------------------------------------------- +// Deprecated +// +// TODO: Remove once client-side Protobuf migration has been completed. +// --------------------------------------------------------------------------- +// ref: https://github.com/cosmos/cosmos-sdk/issues/5864 +func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc( + "/slashing/validators/{validatorAddr}/unjail", + unjailRequestHandlerFn(cliCtx), + ).Methods("POST") +} + func unjailRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r)