Merge PR #2997: Split POST delegations endpoint
This commit is contained in:
committed by
Christopher Goes
parent
e1f0767ba8
commit
4ecbf0dd5f
@@ -49,8 +49,11 @@ func SendRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIC
|
||||
return
|
||||
}
|
||||
|
||||
cliCtx = cliCtx.WithGenerateOnly(req.BaseReq.GenerateOnly)
|
||||
cliCtx = cliCtx.WithSimulation(req.BaseReq.Simulate)
|
||||
|
||||
baseReq := req.BaseReq.Sanitize()
|
||||
if !baseReq.ValidateBasic(w) {
|
||||
if !baseReq.ValidateBasic(w, cliCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -78,8 +78,11 @@ func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han
|
||||
return
|
||||
}
|
||||
|
||||
cliCtx = cliCtx.WithGenerateOnly(req.BaseReq.GenerateOnly)
|
||||
cliCtx = cliCtx.WithSimulation(req.BaseReq.Simulate)
|
||||
|
||||
baseReq := req.BaseReq.Sanitize()
|
||||
if !baseReq.ValidateBasic(w) {
|
||||
if !baseReq.ValidateBasic(w, cliCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -123,8 +126,11 @@ func depositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerF
|
||||
return
|
||||
}
|
||||
|
||||
cliCtx = cliCtx.WithGenerateOnly(req.BaseReq.GenerateOnly)
|
||||
cliCtx = cliCtx.WithSimulation(req.BaseReq.Simulate)
|
||||
|
||||
baseReq := req.BaseReq.Sanitize()
|
||||
if !baseReq.ValidateBasic(w) {
|
||||
if !baseReq.ValidateBasic(w, cliCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -162,8 +168,11 @@ func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc
|
||||
return
|
||||
}
|
||||
|
||||
cliCtx = cliCtx.WithGenerateOnly(req.BaseReq.GenerateOnly)
|
||||
cliCtx = cliCtx.WithSimulation(req.BaseReq.Simulate)
|
||||
|
||||
baseReq := req.BaseReq.Sanitize()
|
||||
if !baseReq.ValidateBasic(w) {
|
||||
if !baseReq.ValidateBasic(w, cliCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -43,8 +43,11 @@ func TransferRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.
|
||||
return
|
||||
}
|
||||
|
||||
cliCtx = cliCtx.WithGenerateOnly(req.BaseReq.GenerateOnly)
|
||||
cliCtx = cliCtx.WithSimulation(req.BaseReq.Simulate)
|
||||
|
||||
baseReq := req.BaseReq.Sanitize()
|
||||
if !baseReq.ValidateBasic(w) {
|
||||
if !baseReq.ValidateBasic(w, cliCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -38,8 +38,11 @@ func unjailRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CL
|
||||
return
|
||||
}
|
||||
|
||||
cliCtx = cliCtx.WithGenerateOnly(req.BaseReq.GenerateOnly)
|
||||
cliCtx = cliCtx.WithSimulation(req.BaseReq.Simulate)
|
||||
|
||||
baseReq := req.BaseReq.Sanitize()
|
||||
if !baseReq.ValidateBasic(w) {
|
||||
if !baseReq.ValidateBasic(w, cliCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+105
-198
@@ -2,81 +2,72 @@ package rest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client/utils"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
|
||||
"github.com/cosmos/cosmos-sdk/x/stake"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
)
|
||||
|
||||
func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) {
|
||||
r.HandleFunc(
|
||||
"/stake/delegators/{delegatorAddr}/delegations",
|
||||
delegationsRequestHandlerFn(cdc, kb, cliCtx),
|
||||
postDelegationsHandlerFn(cdc, kb, cliCtx),
|
||||
).Methods("POST")
|
||||
r.HandleFunc(
|
||||
"/stake/delegators/{delegatorAddr}/unbonding_delegations",
|
||||
postUnbondingDelegationsHandlerFn(cdc, kb, cliCtx),
|
||||
).Methods("POST")
|
||||
r.HandleFunc(
|
||||
"/stake/delegators/{delegatorAddr}/redelegations",
|
||||
postRedelegationsHandlerFn(cdc, kb, cliCtx),
|
||||
).Methods("POST")
|
||||
}
|
||||
|
||||
type (
|
||||
msgDelegationsInput struct {
|
||||
DelegatorAddr string `json:"delegator_addr"` // in bech32
|
||||
ValidatorAddr string `json:"validator_addr"` // in bech32
|
||||
Delegation sdk.Coin `json:"delegation"`
|
||||
BaseReq utils.BaseReq `json:"base_req"`
|
||||
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // in bech32
|
||||
ValidatorAddr sdk.ValAddress `json:"validator_addr"` // in bech32
|
||||
Delegation sdk.Coin `json:"delegation"`
|
||||
}
|
||||
|
||||
msgBeginRedelegateInput struct {
|
||||
DelegatorAddr string `json:"delegator_addr"` // in bech32
|
||||
ValidatorSrcAddr string `json:"validator_src_addr"` // in bech32
|
||||
ValidatorDstAddr string `json:"validator_dst_addr"` // in bech32
|
||||
SharesAmount string `json:"shares"`
|
||||
BaseReq utils.BaseReq `json:"base_req"`
|
||||
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // in bech32
|
||||
ValidatorSrcAddr sdk.ValAddress `json:"validator_src_addr"` // in bech32
|
||||
ValidatorDstAddr sdk.ValAddress `json:"validator_dst_addr"` // in bech32
|
||||
SharesAmount sdk.Dec `json:"shares"`
|
||||
}
|
||||
|
||||
msgBeginUnbondingInput struct {
|
||||
DelegatorAddr string `json:"delegator_addr"` // in bech32
|
||||
ValidatorAddr string `json:"validator_addr"` // in bech32
|
||||
SharesAmount string `json:"shares"`
|
||||
}
|
||||
|
||||
// the request body for edit delegations
|
||||
EditDelegationsReq struct {
|
||||
BaseReq utils.BaseReq `json:"base_req"`
|
||||
Delegations []msgDelegationsInput `json:"delegations"`
|
||||
BeginUnbondings []msgBeginUnbondingInput `json:"begin_unbondings"`
|
||||
BeginRedelegates []msgBeginRedelegateInput `json:"begin_redelegates"`
|
||||
BaseReq utils.BaseReq `json:"base_req"`
|
||||
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // in bech32
|
||||
ValidatorAddr sdk.ValAddress `json:"validator_addr"` // in bech32
|
||||
SharesAmount sdk.Dec `json:"shares"`
|
||||
}
|
||||
)
|
||||
|
||||
// TODO: Split this up into several smaller functions, and remove the above nolint
|
||||
// TODO: use sdk.ValAddress instead of sdk.AccAddress for validators in messages
|
||||
// TODO: Seriously consider how to refactor...do we need to make it multiple txs?
|
||||
// If not, we can just use CompleteAndBroadcastTxREST.
|
||||
func delegationsRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
|
||||
func postDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req EditDelegationsReq
|
||||
var req msgDelegationsInput
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
err := utils.ReadRESTReq(w, r, cdc, &req)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
err = cdc.UnmarshalJSON(body, &req)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
cliCtx = cliCtx.WithGenerateOnly(req.BaseReq.GenerateOnly)
|
||||
cliCtx = cliCtx.WithSimulation(req.BaseReq.Simulate)
|
||||
|
||||
baseReq := req.BaseReq.Sanitize()
|
||||
if !baseReq.ValidateBasic(w) {
|
||||
if !baseReq.ValidateBasic(w, cliCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -86,182 +77,98 @@ func delegationsRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx conte
|
||||
return
|
||||
}
|
||||
|
||||
// build messages
|
||||
messages := make([]sdk.Msg, len(req.Delegations)+
|
||||
len(req.BeginRedelegates)+
|
||||
len(req.BeginUnbondings))
|
||||
|
||||
i := 0
|
||||
for _, msg := range req.Delegations {
|
||||
delAddr, err := sdk.AccAddressFromBech32(msg.DelegatorAddr)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddr)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if !bytes.Equal(info.GetPubKey().Address(), delAddr) {
|
||||
utils.WriteErrorResponse(w, http.StatusUnauthorized, "Must use own delegator address")
|
||||
return
|
||||
}
|
||||
|
||||
messages[i] = stake.MsgDelegate{
|
||||
DelegatorAddr: delAddr,
|
||||
ValidatorAddr: valAddr,
|
||||
Delegation: msg.Delegation,
|
||||
}
|
||||
|
||||
i++
|
||||
if !bytes.Equal(info.GetPubKey().Address(), req.DelegatorAddr) {
|
||||
utils.WriteErrorResponse(w, http.StatusUnauthorized, "Must use own delegator address")
|
||||
return
|
||||
}
|
||||
|
||||
for _, msg := range req.BeginRedelegates {
|
||||
delAddr, err := sdk.AccAddressFromBech32(msg.DelegatorAddr)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if !bytes.Equal(info.GetPubKey().Address(), delAddr) {
|
||||
utils.WriteErrorResponse(w, http.StatusUnauthorized, "Must use own delegator address")
|
||||
return
|
||||
}
|
||||
|
||||
valSrcAddr, err := sdk.ValAddressFromBech32(msg.ValidatorSrcAddr)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
valDstAddr, err := sdk.ValAddressFromBech32(msg.ValidatorDstAddr)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
shares, err := sdk.NewDecFromStr(msg.SharesAmount)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
messages[i] = stake.MsgBeginRedelegate{
|
||||
DelegatorAddr: delAddr,
|
||||
ValidatorSrcAddr: valSrcAddr,
|
||||
ValidatorDstAddr: valDstAddr,
|
||||
SharesAmount: shares,
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
for _, msg := range req.BeginUnbondings {
|
||||
delAddr, err := sdk.AccAddressFromBech32(msg.DelegatorAddr)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if !bytes.Equal(info.GetPubKey().Address(), delAddr) {
|
||||
utils.WriteErrorResponse(w, http.StatusUnauthorized, "Must use own delegator address")
|
||||
return
|
||||
}
|
||||
|
||||
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddr)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
shares, err := sdk.NewDecFromStr(msg.SharesAmount)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
messages[i] = stake.MsgBeginUnbonding{
|
||||
DelegatorAddr: delAddr,
|
||||
ValidatorAddr: valAddr,
|
||||
SharesAmount: shares,
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
simulateGas, gas, err := client.ReadGasFlag(baseReq.Gas)
|
||||
msg := stake.NewMsgDelegate(req.DelegatorAddr, req.ValidatorAddr, req.Delegation)
|
||||
err = msg.ValidateBasic()
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
adjustment, ok := utils.ParseFloat64OrReturnBadRequest(w, baseReq.GasAdjustment, client.DefaultGasAdjustment)
|
||||
if !ok {
|
||||
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, baseReq, []sdk.Msg{msg}, cdc)
|
||||
}
|
||||
}
|
||||
|
||||
func postRedelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req msgBeginRedelegateInput
|
||||
|
||||
err := utils.ReadRESTReq(w, r, cdc, &req)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
txBldr := authtxb.TxBuilder{
|
||||
Codec: cdc,
|
||||
Gas: gas,
|
||||
GasAdjustment: adjustment,
|
||||
SimulateGas: simulateGas,
|
||||
ChainID: baseReq.ChainID,
|
||||
cliCtx = cliCtx.WithGenerateOnly(req.BaseReq.GenerateOnly)
|
||||
cliCtx = cliCtx.WithSimulation(req.BaseReq.Simulate)
|
||||
|
||||
baseReq := req.BaseReq.Sanitize()
|
||||
if !baseReq.ValidateBasic(w, cliCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
// sign messages
|
||||
signedTxs := make([][]byte, len(messages[:]))
|
||||
for i, msg := range messages {
|
||||
// increment sequence for each message
|
||||
txBldr = txBldr.WithAccountNumber(baseReq.AccountNumber)
|
||||
txBldr = txBldr.WithSequence(baseReq.Sequence)
|
||||
|
||||
baseReq.Sequence++
|
||||
|
||||
if baseReq.Simulate || txBldr.SimulateGas {
|
||||
newBldr, err := utils.EnrichCtxWithGas(txBldr, cliCtx, baseReq.Name, []sdk.Msg{msg})
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if baseReq.Simulate {
|
||||
utils.WriteSimulationResponse(w, newBldr.Gas)
|
||||
return
|
||||
}
|
||||
|
||||
txBldr = newBldr
|
||||
}
|
||||
|
||||
if baseReq.GenerateOnly {
|
||||
utils.WriteGenerateStdTxResponse(w, txBldr, []sdk.Msg{msg})
|
||||
return
|
||||
}
|
||||
|
||||
txBytes, err := txBldr.BuildAndSign(baseReq.Name, baseReq.Password, []sdk.Msg{msg})
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
signedTxs[i] = txBytes
|
||||
info, err := kb.Get(baseReq.Name)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// send
|
||||
// XXX the operation might not be atomic if a tx fails
|
||||
// should we have a sdk.MultiMsg type to make sending atomic?
|
||||
results := make([]*ctypes.ResultBroadcastTxCommit, len(signedTxs[:]))
|
||||
for i, txBytes := range signedTxs {
|
||||
res, err := cliCtx.BroadcastTx(txBytes)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
results[i] = res
|
||||
if !bytes.Equal(info.GetPubKey().Address(), req.DelegatorAddr) {
|
||||
utils.WriteErrorResponse(w, http.StatusUnauthorized, "Must use own delegator address")
|
||||
return
|
||||
}
|
||||
|
||||
utils.PostProcessResponse(w, cdc, results, cliCtx.Indent)
|
||||
msg := stake.NewMsgBeginRedelegate(req.DelegatorAddr, req.ValidatorSrcAddr, req.ValidatorDstAddr, req.SharesAmount)
|
||||
err = msg.ValidateBasic()
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, baseReq, []sdk.Msg{msg}, cdc)
|
||||
}
|
||||
}
|
||||
|
||||
func postUnbondingDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req msgBeginUnbondingInput
|
||||
|
||||
err := utils.ReadRESTReq(w, r, cdc, &req)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
cliCtx = cliCtx.WithGenerateOnly(req.BaseReq.GenerateOnly)
|
||||
cliCtx = cliCtx.WithSimulation(req.BaseReq.Simulate)
|
||||
|
||||
baseReq := req.BaseReq.Sanitize()
|
||||
if !baseReq.ValidateBasic(w, cliCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
info, err := kb.Get(baseReq.Name)
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if !bytes.Equal(info.GetPubKey().Address(), req.DelegatorAddr) {
|
||||
utils.WriteErrorResponse(w, http.StatusUnauthorized, "Must use own delegator address")
|
||||
return
|
||||
}
|
||||
|
||||
msg := stake.NewMsgBeginUnbonding(req.DelegatorAddr, req.ValidatorAddr, req.SharesAmount)
|
||||
err = msg.ValidateBasic()
|
||||
if err != nil {
|
||||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, baseReq, []sdk.Msg{msg}, cdc)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user