Merge PR #5885: Added proto compatible x/slashing clients

This commit is contained in:
Alexander Bezobchuk 2020-03-27 14:41:15 -04:00 committed by GitHub
commit 77dd247a57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 7 deletions

View File

@ -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)
},
}

View File

@ -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:
$ <appcli> 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",

View File

@ -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)

View File

@ -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)