From a9bcdb2a0a96b35e8cfb5bcf1f8425b842f401b6 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Fri, 25 May 2018 00:41:17 +0900 Subject: [PATCH] Addressed comments --- x/stake/client/rest/query.go | 2 +- x/stake/client/rest/tx.go | 52 +++++++++++++++++++----------------- x/stake/msg.go | 8 +++--- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 02cc732c9d..cbcf5f5e8e 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -20,7 +20,7 @@ func registerQueryRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec ).Methods("GET") } -// bondingStatusHandlerFn - http request handler to query delegator bonding status +// http request handler to query delegator bonding status func bondingStatusHandlerFn(storeName string, cdc *wire.Codec, ctx context.CoreContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // read parameters diff --git a/x/stake/client/rest/tx.go b/x/stake/client/rest/tx.go index 75e0f8d75d..2560fcc9f0 100644 --- a/x/stake/client/rest/tx.go +++ b/x/stake/client/rest/tx.go @@ -1,6 +1,7 @@ package rest import ( + "bytes" "encoding/json" "io/ioutil" "net/http" @@ -16,33 +17,26 @@ import ( ) func registerTxRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { - r.HandleFunc("/stake/bondunbond", bondUnbondRequestHandlerFn(cdc, kb, ctx)).Methods("POST") + r.HandleFunc( + "/stake/delegations", + editDelegationsRequestHandlerFn(cdc, kb, ctx), + ).Methods("POST") } -type bond struct { - Amount sdk.Coin `json:"amount"` - Candidate sdk.Address `json:"candidate"` -} - -type unbond struct { - Shares string `json:"shares"` - Candidate sdk.Address `json:"candidate"` -} - -type bondUnbondBody struct { +type editDelegationsBody struct { // fees is not used currently // Fees sdk.Coin `json="fees"` - LocalAccountName string `json:"name"` - Password string `json:"password"` - ChainID string `json:"chain_id"` - Sequence int64 `json:"sequence"` - Bond []bond `json:"bond"` - Unbond []unbond `json:"unbond"` + LocalAccountName string `json:"name"` + Password string `json:"password"` + ChainID string `json:"chain_id"` + Sequence int64 `json:"sequence"` + Delegate []stake.MsgDelegate `json:"delegate"` + Unbond []stake.MsgUnbond `json:"unbond"` } -func bondUnbondRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) http.HandlerFunc { +func editDelegationsRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var m bondUnbondBody + var m editDelegationsBody body, err := ioutil.ReadAll(r.Body) if err != nil { w.WriteHeader(http.StatusBadRequest) @@ -64,13 +58,21 @@ func bondUnbondRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.Co } // build messages - messages := make([]sdk.Msg, 0, len(m.Bond)+len(m.Unbond)) - for _, bond := range m.Bond { - msg := stake.NewMsgDelegate(info.Address(), bond.Candidate, bond.Amount) + messages := make([]sdk.Msg, 0, len(m.Delegate)+len(m.Unbond)) + for _, msg := range m.Delegate { + if !bytes.Equal(info.Address(), msg.DelegatorAddr) { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte("Must use own delegator address")) + return + } messages = append(messages, msg) } - for _, unbond := range m.Unbond { - msg := stake.NewMsgUnbond(info.Address(), unbond.Candidate, unbond.Shares) + for _, msg := range m.Unbond { + if !bytes.Equal(info.Address(), msg.DelegatorAddr) { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte("Must use own delegator address")) + return + } messages = append(messages, msg) } diff --git a/x/stake/msg.go b/x/stake/msg.go index 4bfc496dee..2d1757947e 100644 --- a/x/stake/msg.go +++ b/x/stake/msg.go @@ -117,8 +117,8 @@ func (msg MsgEditCandidacy) ValidateBasic() sdk.Error { // MsgDelegate - struct for bonding transactions type MsgDelegate struct { - DelegatorAddr sdk.Address `json:"delegator"` - ValidatorAddr sdk.Address `json:"candidate"` + DelegatorAddr sdk.Address `json:"delegator_addr"` + ValidatorAddr sdk.Address `json:"validator_addr"` Bond sdk.Coin `json:"bond"` } @@ -164,8 +164,8 @@ func (msg MsgDelegate) ValidateBasic() sdk.Error { // MsgUnbond - struct for unbonding transactions type MsgUnbond struct { - DelegatorAddr sdk.Address `json:"delegator"` - ValidatorAddr sdk.Address `json:"candidate"` + DelegatorAddr sdk.Address `json:"delegator_addr"` + ValidatorAddr sdk.Address `json:"validator_addr"` Shares string `json:"shares"` }