Remove old tx code from rest (#6939)

* Cleanup rest txs

* Remove nolint

* Update x/ibc to use WriteGeneratedTxResponse

* Update marshaler usages

* Fix PostProcessResponse test

* Update PostProcessResponse
This commit is contained in:
SaReN 2020-08-05 23:38:13 +05:30 committed by GitHub
parent d2661a4692
commit 95cf3c95a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 74 additions and 671 deletions

View File

@ -273,22 +273,12 @@ func PostProcessResponseBare(w http.ResponseWriter, ctx client.Context, body int
err error
)
// TODO: Remove once client-side Protobuf migration has been completed.
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
var marshaler codec.JSONMarshaler
if ctx.JSONMarshaler != nil {
marshaler = ctx.JSONMarshaler
} else {
marshaler = ctx.Codec
}
switch b := body.(type) {
case []byte:
resp = b
default:
resp, err = marshaler.MarshalJSON(body)
resp, err = ctx.JSONMarshaler.MarshalJSON(body)
if CheckInternalServerError(w, err) {
return
}
@ -312,8 +302,8 @@ func PostProcessResponse(w http.ResponseWriter, ctx client.Context, resp interfa
return
}
// TODO: Remove once client-side Protobuf migration has been completed.
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
// TODO: Remove once PubKey Protobuf migration has been completed.
// ref: https://github.com/cosmos/cosmos-sdk/issues/6886
var marshaler codec.JSONMarshaler
if ctx.JSONMarshaler != nil {

View File

@ -19,6 +19,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
)
@ -306,8 +307,11 @@ func TestParseQueryParamBool(t *testing.T) {
func TestPostProcessResponseBare(t *testing.T) {
t.Parallel()
encodingConfig := simappparams.MakeEncodingConfig()
clientCtx := client.Context{}.
WithTxConfig(encodingConfig.TxConfig).
WithJSONMarshaler(encodingConfig.Marshaler)
// write bytes
clientCtx := client.Context{}
w := httptest.NewRecorder()
bs := []byte("text string")
@ -323,7 +327,6 @@ func TestPostProcessResponseBare(t *testing.T) {
require.Equal(t, "text string", string(got))
// write struct and indent response
clientCtx = client.Context{}.WithCodec(codec.New())
w = httptest.NewRecorder()
data := struct {
X int `json:"x"`
@ -342,7 +345,6 @@ func TestPostProcessResponseBare(t *testing.T) {
require.Equal(t, "{\"x\":\"10\",\"s\":\"test\"}", string(got))
// write struct, don't indent response
clientCtx = client.Context{}.WithCodec(codec.New())
w = httptest.NewRecorder()
data = struct {
X int `json:"x"`
@ -361,7 +363,6 @@ func TestPostProcessResponseBare(t *testing.T) {
require.Equal(t, `{"x":"10","s":"test"}`, string(got))
// test marshalling failure
clientCtx = client.Context{}.WithCodec(codec.New())
w = httptest.NewRecorder()
data2 := badJSONMarshaller{}

View File

@ -7,7 +7,6 @@ import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/bank/types"
@ -37,16 +36,6 @@ func QueryBalancesRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
route string
)
// TODO: Remove once client-side Protobuf migration has been completed.
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
var marshaler codec.JSONMarshaler
if ctx.JSONMarshaler != nil {
marshaler = ctx.JSONMarshaler
} else {
marshaler = ctx.Codec
}
denom := r.FormValue("denom")
if denom == "" {
params = types.NewQueryAllBalancesRequest(addr, nil)
@ -56,7 +45,7 @@ func QueryBalancesRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryBalance)
}
bz, err := marshaler.MarshalJSON(params)
bz, err := ctx.JSONMarshaler.MarshalJSON(params)
if rest.CheckBadRequestError(w, err) {
return
}

View File

@ -6,9 +6,8 @@ import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
govrest "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
@ -19,12 +18,6 @@ func RegisterHandlers(clientCtx client.Context, r *mux.Router) {
registerTxHandlers(clientCtx, r)
}
// RegisterRoutes register distribution REST routes.
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
registerQueryRoutes(clientCtx, r)
registerTxRoutes(clientCtx, r)
}
// TODO add proto compatible Handler after x/gov migration
// ProposalRESTHandler returns a ProposalRESTHandler that exposes the community pool spend REST handler with a given sub-route.
func ProposalRESTHandler(clientCtx client.Context) govrest.ProposalRESTHandler {
@ -56,6 +49,6 @@ func postProposalHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}

View File

@ -9,7 +9,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx"
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/distribution/client/common"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)
@ -202,191 +201,6 @@ func newFundCommunityPoolHandlerFn(clientCtx client.Context) http.HandlerFunc {
}
}
// ---------------------------------------------------------------------------
// Deprecated
//
// TODO: Remove once client-side Protobuf migration has been completed.
// ---------------------------------------------------------------------------
func registerTxRoutes(clientCtx client.Context, r *mux.Router) {
// Withdraw all delegator rewards
r.HandleFunc(
"/distribution/delegators/{delegatorAddr}/rewards",
withdrawDelegatorRewardsHandlerFn(clientCtx),
).Methods("POST")
// Withdraw delegation rewards
r.HandleFunc(
"/distribution/delegators/{delegatorAddr}/rewards/{validatorAddr}",
withdrawDelegationRewardsHandlerFn(clientCtx),
).Methods("POST")
// Replace the rewards withdrawal address
r.HandleFunc(
"/distribution/delegators/{delegatorAddr}/withdraw_address",
setDelegatorWithdrawalAddrHandlerFn(clientCtx),
).Methods("POST")
// Withdraw validator rewards and commission
r.HandleFunc(
"/distribution/validators/{validatorAddr}/rewards",
withdrawValidatorRewardsHandlerFn(clientCtx),
).Methods("POST")
// Fund the community pool
r.HandleFunc(
"/distribution/community_pool",
fundCommunityPoolHandlerFn(clientCtx),
).Methods("POST")
}
// Withdraw delegator rewards
func withdrawDelegatorRewardsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req withdrawRewardsReq
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
// read and validate URL's variables
delAddr, ok := checkDelegatorAddressVar(w, r)
if !ok {
return
}
msgs, err := common.WithdrawAllDelegatorRewards(clientCtx, delAddr)
if rest.CheckInternalServerError(w, err) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, msgs)
}
}
// Withdraw delegation rewards
func withdrawDelegationRewardsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req withdrawRewardsReq
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
// read and validate URL's variables
delAddr, ok := checkDelegatorAddressVar(w, r)
if !ok {
return
}
valAddr, ok := checkValidatorAddressVar(w, r)
if !ok {
return
}
msg := types.NewMsgWithdrawDelegatorReward(delAddr, valAddr)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}
// Replace the rewards withdrawal address
func setDelegatorWithdrawalAddrHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req setWithdrawalAddrReq
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
// read and validate URL's variables
delAddr, ok := checkDelegatorAddressVar(w, r)
if !ok {
return
}
msg := types.NewMsgSetWithdrawAddress(delAddr, req.WithdrawAddress)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}
// Withdraw validator rewards and commission
func withdrawValidatorRewardsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req withdrawRewardsReq
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
// read and validate URL's variable
valAddr, ok := checkValidatorAddressVar(w, r)
if !ok {
return
}
// prepare multi-message transaction
msgs, err := common.WithdrawValidatorRewardsAndCommission(valAddr)
if rest.CheckBadRequestError(w, err) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, msgs)
}
}
func fundCommunityPoolHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req fundCommunityPoolReq
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
msg := types.NewMsgFundCommunityPool(req.Amount, fromAddr)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}
// Auxiliary
func checkDelegatorAddressVar(w http.ResponseWriter, r *http.Request) (sdk.AccAddress, bool) {

View File

@ -33,12 +33,6 @@ func RegisterHandlers(clientCtx client.Context, r *mux.Router, phs []ProposalRES
registerTxHandlers(clientCtx, r, phs)
}
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(clientCtx client.Context, r *mux.Router, phs []ProposalRESTHandler) {
registerQueryRoutes(clientCtx, r)
registerTxRoutes(clientCtx, r, phs)
}
// PostProposalReq defines the properties of a proposal request's body.
type PostProposalReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`

View File

@ -8,9 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
@ -127,121 +125,3 @@ func newVoteHandlerFn(clientCtx client.Context) http.HandlerFunc {
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
// ---------------------------------------------------------------------------
// Deprecated
//
// TODO: Remove once client-side Protobuf migration has been completed.
// ---------------------------------------------------------------------------
func registerTxRoutes(clientCtx client.Context, r *mux.Router, phs []ProposalRESTHandler) {
propSubRtr := r.PathPrefix("/gov/proposals").Subrouter()
for _, ph := range phs {
propSubRtr.HandleFunc(fmt.Sprintf("/%s", ph.SubRoute), ph.Handler).Methods("POST")
}
r.HandleFunc("/gov/proposals", postProposalHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits", RestProposalID), depositHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), voteHandlerFn(clientCtx)).Methods("POST")
}
func postProposalHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req PostProposalReq
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
proposalType := gcutils.NormalizeProposalType(req.ProposalType)
content := types.ContentFromProposalType(req.Title, req.Description, proposalType)
msg, err := types.NewMsgSubmitProposal(content, req.InitialDeposit, req.Proposer)
if rest.CheckBadRequestError(w, err) {
return
}
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}
func depositHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
if len(strProposalID) == 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, "proposalId required but not specified")
return
}
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
var req DepositReq
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
// create the message
msg := types.NewMsgDeposit(req.Depositor, proposalID, req.Amount)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}
func voteHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
if len(strProposalID) == 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, "proposalId required but not specified")
return
}
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
var req VoteReq
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
voteOption, err := types.VoteOptionFromString(gcutils.NormalizeVoteOption(req.Option))
if rest.CheckBadRequestError(w, err) {
return
}
// create the message
msg := types.NewMsgVote(req.Voter, proposalID, voteOption)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}

View File

@ -15,7 +15,7 @@ const (
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
registerTxRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
// TransferTxReq defines the properties of a transfer tx request's body.

View File

@ -7,13 +7,13 @@ import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
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/ibc-transfer/types"
)
func registerTxRoutes(clientCtx client.Context, r *mux.Router) {
func registerTxHandlers(clientCtx client.Context, r *mux.Router) {
r.HandleFunc(fmt.Sprintf("/ibc/ports/{%s}/channels/{%s}/transfer", restPortID, restChannelID), transferHandlerFn(clientCtx)).Methods("POST")
}
@ -68,6 +68,6 @@ func transferHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}

View File

@ -16,7 +16,7 @@ const (
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
registerQueryRoutes(clientCtx, r)
registerTxRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
// ConnectionOpenInitReq defines the properties of a connection open init request's body.

View File

@ -7,13 +7,13 @@ import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
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/ibc/03-connection/types"
)
func registerTxRoutes(clientCtx client.Context, r *mux.Router) {
func registerTxHandlers(clientCtx client.Context, r *mux.Router) {
r.HandleFunc("/ibc/connections/open-init", connectionOpenInitHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc("/ibc/connections/open-try", connectionOpenTryHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/ibc/connections/{%s}/open-ack", RestConnectionID), connectionOpenAckHandlerFn(clientCtx)).Methods("POST")
@ -59,7 +59,7 @@ func connectionOpenInitHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -104,7 +104,7 @@ func connectionOpenTryHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -152,7 +152,7 @@ func connectionOpenAckHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -199,6 +199,6 @@ func connectionOpenConfirmHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}

View File

@ -16,7 +16,7 @@ const (
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
registerQueryRoutes(clientCtx, r)
registerTxRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
// ChannelOpenInitReq defines the properties of a channel open init request's body.

View File

@ -7,14 +7,14 @@ import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
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/ibc/04-channel/types"
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func registerTxRoutes(clientCtx client.Context, r *mux.Router) {
func registerTxHandlers(clientCtx client.Context, r *mux.Router) {
r.HandleFunc("/ibc/channels/open-init", channelOpenInitHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc("/ibc/channels/open-try", channelOpenTryHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/ibc/ports/{%s}/channels/{%s}/open-ack", RestPortID, RestChannelID), channelOpenAckHandlerFn(clientCtx)).Methods("POST")
@ -69,7 +69,7 @@ func channelOpenInitHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -121,7 +121,7 @@ func channelOpenTryHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -175,7 +175,7 @@ func channelOpenAckHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -228,7 +228,7 @@ func channelOpenConfirmHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -279,7 +279,7 @@ func channelCloseInitHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -332,7 +332,7 @@ func channelCloseConfirmHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -377,6 +377,6 @@ func recvPacketHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}

View File

@ -20,7 +20,7 @@ const (
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
registerTxRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
// CreateClientReq defines the properties of a create client request's body.

View File

@ -7,14 +7,14 @@ import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func registerTxRoutes(clientCtx client.Context, r *mux.Router) {
func registerTxHandlers(clientCtx client.Context, r *mux.Router) {
r.HandleFunc("/ibc/clients/tendermint", createClientHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/ibc/clients/{%s}/update", RestClientID), updateClientHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/ibc/clients/{%s}/misbehaviour", RestClientID), submitMisbehaviourHandlerFn(clientCtx)).Methods("POST")
@ -60,7 +60,7 @@ func createClientHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -109,7 +109,7 @@ func updateClientHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
@ -149,6 +149,6 @@ func submitMisbehaviourHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}

View File

@ -9,7 +9,7 @@ import (
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
registerTxRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
// CreateClientReq defines the properties of a create client request's body.

View File

@ -6,14 +6,14 @@ import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
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/ibc/09-localhost/types"
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func registerTxRoutes(clientCtx client.Context, r *mux.Router) {
func registerTxHandlers(clientCtx client.Context, r *mux.Router) {
r.HandleFunc("/ibc/clients/localhost", createClientHandlerFn(clientCtx)).Methods("POST")
}
@ -51,6 +51,6 @@ func createClientHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}

View File

@ -4,9 +4,8 @@ import (
"net/http"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
govrest "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramscutils "github.com/cosmos/cosmos-sdk/x/params/client/utils"
@ -44,6 +43,6 @@ func postProposalHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}

View File

@ -10,9 +10,3 @@ func RegisterHandlers(clientCtx client.Context, r *mux.Router) {
registerQueryRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
// RegisterRoutes registers staking-related REST handlers to a router
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
registerQueryRoutes(clientCtx, r)
registerTxRoutes(clientCtx, r)
}

View File

@ -10,7 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx"
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"
)
@ -62,56 +61,3 @@ func NewUnjailRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
tx.WriteGeneratedTxResponse(clientCtx, w, 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(clientCtx client.Context, r *mux.Router) {
r.HandleFunc(
"/slashing/validators/{validatorAddr}/unjail",
unjailRequestHandlerFn(clientCtx),
).Methods("POST")
}
func unjailRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bech32validator := vars["validatorAddr"]
var req UnjailReq
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
valAddr, err := sdk.ValAddressFromBech32(bech32validator)
if rest.CheckInternalServerError(w, err) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
if !bytes.Equal(fromAddr, valAddr) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own validator address")
return
}
msg := types.NewMsgUnjail(valAddr)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}

View File

@ -10,9 +10,3 @@ func RegisterHandlers(clientCtx client.Context, r *mux.Router) {
registerQueryRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
// RegisterRoutes registers staking-related REST handlers to a router
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
registerQueryRoutes(clientCtx, r)
registerTxRoutes(clientCtx, r)
}

View File

@ -10,7 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx"
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/staking/types"
)
@ -148,119 +147,3 @@ func newPostUnbondingDelegationsHandlerFn(clientCtx client.Context) http.Handler
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
// ---------------------------------------------------------------------------
// Deprecated
//
// TODO: Remove once client-side Protobuf migration has been completed.
// ---------------------------------------------------------------------------
func registerTxRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/delegations",
postDelegationsHandlerFn(clientCtx),
).Methods("POST")
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/unbonding_delegations",
postUnbondingDelegationsHandlerFn(clientCtx),
).Methods("POST")
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/redelegations",
postRedelegationsHandlerFn(clientCtx),
).Methods("POST")
}
func postDelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req DelegateRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgDelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}
func postRedelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req RedelegateRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgBeginRedelegate(req.DelegatorAddress, req.ValidatorSrcAddress, req.ValidatorDstAddress, req.Amount)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}
func postUnbondingDelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req UndelegateRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgUndelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}

View File

@ -12,11 +12,13 @@ import (
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)
// RegisterRoutes registers REST routes for the upgrade module under the path specified by routeName.
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc("/upgrade/current", getCurrentPlanHandler(clientCtx)).Methods("GET")
r.HandleFunc("/upgrade/applied/{name}", getDonePlanHandler(clientCtx)).Methods("GET")
registerTxRoutes(clientCtx, r)
func registerQueryRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc(
"/upgrade/current", getCurrentPlanHandler(clientCtx),
).Methods("GET")
r.HandleFunc(
"/upgrade/applied/{name}", getDonePlanHandler(clientCtx),
).Methods("GET")
}
func getCurrentPlanHandler(clientCtx client.Context) func(http.ResponseWriter, *http.Request) {

View File

@ -0,0 +1,13 @@
package rest
import (
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
)
// RegisterRoutes registers REST routes for the upgrade module under the path specified by routeName.
func RegisterRoutes(clientCtx client.Context, r *mux.Router) {
registerQueryRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}

View File

@ -8,29 +8,20 @@ import (
"github.com/gorilla/mux"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
govrest "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
gov "github.com/cosmos/cosmos-sdk/x/gov/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)
// nolint
func newRegisterTxRoutes(
func registerTxHandlers(
clientCtx client.Context,
txg client.TxConfig,
newMsgFn func() gov.MsgSubmitProposalI,
r *mux.Router) {
r.HandleFunc("/upgrade/plan", newPostPlanHandler(clientCtx, txg, newMsgFn)).Methods("POST")
r.HandleFunc("/upgrade/cancel", newCancelPlanHandler(clientCtx, txg, newMsgFn)).Methods("POST")
}
func registerTxRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc("/upgrade/plan", postPlanHandler(clientCtx)).Methods("POST")
r.HandleFunc("/upgrade/cancel", cancelPlanHandler(clientCtx)).Methods("POST")
r.HandleFunc("/upgrade/plan", newPostPlanHandler(clientCtx)).Methods("POST")
r.HandleFunc("/upgrade/cancel", newCancelPlanHandler(clientCtx)).Methods("POST")
}
// PlanRequest defines a proposal for a new upgrade plan.
@ -56,19 +47,18 @@ type CancelRequest struct {
func ProposalRESTHandler(clientCtx client.Context) govrest.ProposalRESTHandler {
return govrest.ProposalRESTHandler{
SubRoute: "upgrade",
Handler: postPlanHandler(clientCtx),
Handler: newPostPlanHandler(clientCtx),
}
}
func ProposalCancelRESTHandler(clientCtx client.Context) govrest.ProposalRESTHandler {
return govrest.ProposalRESTHandler{
SubRoute: "upgrade",
Handler: cancelPlanHandler(clientCtx),
Handler: newCancelPlanHandler(clientCtx),
}
}
// nolint
func newPostPlanHandler(clientCtx client.Context, txg client.TxConfig, newMsgFn func() gov.MsgSubmitProposalI) http.HandlerFunc {
func newPostPlanHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req PlanRequest
@ -96,14 +86,10 @@ func newPostPlanHandler(clientCtx client.Context, txg client.TxConfig, newMsgFn
plan := types.Plan{Name: req.UpgradeName, Time: t, Height: req.UpgradeHeight, Info: req.UpgradeInfo}
content := types.NewSoftwareUpgradeProposal(req.Title, req.Description, plan)
msg := newMsgFn()
err = msg.SetContent(content)
msg, err := govtypes.NewMsgSubmitProposal(content, req.Deposit, fromAddr)
if rest.CheckBadRequestError(w, err) {
return
}
msg.SetInitialDeposit(req.Deposit)
msg.SetProposer(fromAddr)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
@ -112,8 +98,7 @@ func newPostPlanHandler(clientCtx client.Context, txg client.TxConfig, newMsgFn
}
}
// nolint
func newCancelPlanHandler(clientCtx client.Context, txg client.TxConfig, newMsgFn func() gov.MsgSubmitProposalI) http.HandlerFunc {
func newCancelPlanHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req CancelRequest
@ -133,13 +118,10 @@ func newCancelPlanHandler(clientCtx client.Context, txg client.TxConfig, newMsgF
content := types.NewCancelSoftwareUpgradeProposal(req.Title, req.Description)
msg := newMsgFn()
err = msg.SetContent(content)
msg, err := govtypes.NewMsgSubmitProposal(content, req.Deposit, fromAddr)
if rest.CheckBadRequestError(w, err) {
return
}
msg.SetInitialDeposit(req.Deposit)
msg.SetProposer(fromAddr)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
@ -147,74 +129,3 @@ func newCancelPlanHandler(clientCtx client.Context, txg client.TxConfig, newMsgF
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
func postPlanHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req PlanRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
var t time.Time
if req.UpgradeTime != "" {
t, err = time.Parse(time.RFC3339, req.UpgradeTime)
if rest.CheckBadRequestError(w, err) {
return
}
}
plan := types.Plan{Name: req.UpgradeName, Time: t, Height: req.UpgradeHeight, Info: req.UpgradeInfo}
content := types.NewSoftwareUpgradeProposal(req.Title, req.Description, plan)
msg, err := gov.NewMsgSubmitProposal(content, req.Deposit, fromAddr)
if rest.CheckBadRequestError(w, err) {
return
}
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}
func cancelPlanHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req CancelRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
content := types.NewCancelSoftwareUpgradeProposal(req.Title, req.Description)
msg, err := gov.NewMsgSubmitProposal(content, req.Deposit, fromAddr)
if rest.CheckBadRequestError(w, err) {
return
}
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}