Skip to content

Commit

Permalink
Redelegation Querier (cosmos#2559)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 authored and jackzampolin committed Dec 18, 2018
1 parent 26fc516 commit f4fb360
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 169 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ test_cover:

test_lint:
gometalinter --config=tools/gometalinter.json ./...
!(gometalinter --exclude /usr/lib/go/src/ --exclude client/lcd/statik/statik.go --exclude 'vendor/*' --disable-all --enable='errcheck' --vendor ./... | grep -v "client/")
!(gometalinter --exclude /usr/lib/go/src/ --exclude client/lcd/statik/statik.go --exclude 'vendor/*' --exclude 'cmd/logjack/*' --disable-all --enable='errcheck' --vendor ./... | grep -v "client/")
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s
dep status >> /dev/null
!(grep -n branch Gopkg.toml)
Expand Down
5 changes: 5 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ BREAKING CHANGES
FEATURES

* Gaia REST API (`gaiacli advanced rest-server`)
* [gaia-lite] [\#2182] Added LCD endpoint for querying redelegations
* [gov] [\#2479](https://github.com/cosmos/cosmos-sdk/issues/2479) Added governance parameter
query REST endpoints.

* Gaia CLI (`gaiacli`)

* Gaia

* [\#2182] [x/stake] Added querier for querying a single redelegation

* SDK

* Tendermint
Expand Down
8 changes: 6 additions & 2 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,23 @@ func TestBonding(t *testing.T) {
require.Len(t, delegatorDels, 1)
require.Equal(t, "30.0000000000", delegatorDels[0].GetShares().String())

redelegation := getRedelegations(t, port, addr, operAddrs[0], operAddrs[1])
require.Len(t, redelegation, 1)
require.Equal(t, "30", redelegation[0].Balance.Amount.String())

delegatorUbds := getDelegatorUnbondingDelegations(t, port, addr)
require.Len(t, delegatorUbds, 1)
require.Equal(t, "30", delegatorUbds[0].Balance.Amount.String())

delegatorReds := getDelegatorRedelegations(t, port, addr)
delegatorReds := getRedelegations(t, port, addr, nil, nil)
require.Len(t, delegatorReds, 1)
require.Equal(t, "30", delegatorReds[0].Balance.Amount.String())

validatorUbds := getValidatorUnbondingDelegations(t, port, operAddrs[0])
require.Len(t, validatorUbds, 1)
require.Equal(t, "30", validatorUbds[0].Balance.Amount.String())

validatorReds := getValidatorRedelegations(t, port, operAddrs[0])
validatorReds := getRedelegations(t, port, nil, operAddrs[0], nil)
require.Len(t, validatorReds, 1)
require.Equal(t, "30", validatorReds[0].Balance.Amount.String())

Expand Down
59 changes: 25 additions & 34 deletions client/lcd/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -803,15 +803,25 @@ paths:
description: Invalid delegator address or validator address
500:
description: Internal Server Error
/stake/delegators/{delegatorAddr}/redelegations:
/stake/redelegations:
parameters:
- in: path
name: delegatorAddr
description: Bech32 AccAddress of Delegator
required: true
type: string
- in: query
name: delegator
description: Bech32 AccAddress of Delegator
required: false
type: string
- in: query
name: validator_from
description: Bech32 ValAddress of SrcValidator
required: false
type: string
- in: query
name: validator_to
description: Bech32 ValAddress of DstValidator
required: false
type: string
get:
summary: Get all redelegations from a delegator
summary: Get all redelegations (filter by query params)
tags:
- ICS21
produces:
Expand All @@ -823,10 +833,15 @@ paths:
type: array
items:
$ref: "#/definitions/Redelegation"
400:
description: Invalid delegator address
500:
description: Internal Server Error
description: Internal Server Error
/stake/delegators/{delegatorAddr}/redelegations:
parameters:
- in: path
name: delegatorAddr
description: Bech32 AccAddress of Delegator
required: true
type: string
post:
summary: Submit a redelegation
parameters:
Expand Down Expand Up @@ -1037,30 +1052,6 @@ paths:
description: Invalid validator address
500:
description: Internal Server Error
/stake/validators/{validatorAddr}/redelegations:
parameters:
- in: path
name: validatorAddr
description: Bech32 OperatorAddress of validator
required: true
type: string
get:
summary: Get all outgoing redelegations from a validator
tags:
- ICS21
produces:
- application/json
responses:
200:
description: OK
schema:
type: array
items:
$ref: "#/definitions/Redelegation"
400:
description: Invalid validator address
500:
description: Internal Server Error
/stake/pool:
get:
summary: Get the current state of the staking pool
Expand Down
39 changes: 18 additions & 21 deletions client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,17 +886,26 @@ func getDelegatorUnbondingDelegations(t *testing.T, port string, delegatorAddr s
return ubds
}

// GET /stake/delegators/{delegatorAddr}/redelegations Get all redelegations from a delegator
func getDelegatorRedelegations(t *testing.T, port string, delegatorAddr sdk.AccAddress) []stake.Redelegation {
res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/redelegations", delegatorAddr), nil)
// GET /stake/redelegations?delegator=0xdeadbeef&validator_from=0xdeadbeef&validator_to=0xdeadbeef& Get redelegations filters by params passed in
func getRedelegations(t *testing.T, port string, delegatorAddr sdk.AccAddress, srcValidatorAddr sdk.ValAddress, dstValidatorAddr sdk.ValAddress) []stake.Redelegation {
var res *http.Response
var body string
endpoint := "/stake/redelegations?"
if !delegatorAddr.Empty() {
endpoint += fmt.Sprintf("delegator=%s&", delegatorAddr)
}
if !srcValidatorAddr.Empty() {
endpoint += fmt.Sprintf("validator_from=%s&", srcValidatorAddr)
}
if !dstValidatorAddr.Empty() {
endpoint += fmt.Sprintf("validator_to=%s&", dstValidatorAddr)
}
res, body = Request(t, port, "GET", endpoint, nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var reds []stake.Redelegation

err := cdc.UnmarshalJSON([]byte(body), &reds)
var redels []stake.Redelegation
err := cdc.UnmarshalJSON([]byte(body), &redels)
require.Nil(t, err)

return reds
return redels
}

// GET /stake/delegators/{delegatorAddr}/validators Query all validators that a delegator is bonded to
Expand Down Expand Up @@ -1016,18 +1025,6 @@ func getValidatorUnbondingDelegations(t *testing.T, port string, validatorAddr s
return ubds
}

// GET /stake/validators/{validatorAddr}/redelegations Get all outgoing redelegations from a validator
func getValidatorRedelegations(t *testing.T, port string, validatorAddr sdk.ValAddress) []stake.Redelegation {
res, body := Request(t, port, "GET", fmt.Sprintf("/stake/validators/%s/redelegations", validatorAddr.String()), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var reds []stake.Redelegation
err := cdc.UnmarshalJSON([]byte(body), &reds)
require.Nil(t, err)

return reds
}

// GET /stake/pool Get the current state of the staking pool
func getStakePool(t *testing.T, port string) stake.Pool {
res, body := Request(t, port, "GET", "/stake/pool", nil)
Expand Down
1 change: 1 addition & 0 deletions cmd/logjack/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// nolint
package main

import (
Expand Down
82 changes: 58 additions & 24 deletions x/stake/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"net/http"
"strings"

"github.com/cosmos/cosmos-sdk/x/stake"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/cosmos/cosmos-sdk/x/stake/tags"

"github.com/gorilla/mux"
Expand All @@ -32,12 +31,6 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Co
delegatorUnbondingDelegationsHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get all redelegations from a delegator
r.HandleFunc(
"/stake/delegators/{delegatorAddr}/redelegations",
delegatorRedelegationsHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get all staking txs (i.e msgs) from a delegator
r.HandleFunc(
"/stake/delegators/{delegatorAddr}/txs",
Expand Down Expand Up @@ -68,6 +61,12 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Co
unbondingDelegationHandlerFn(cliCtx, cdc),
).Methods("GET")

// Query redelegations (filters in query params)
r.HandleFunc(
"/stake/redelegations",
redelegationsHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get all validators
r.HandleFunc(
"/stake/validators",
Expand All @@ -92,12 +91,6 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Co
validatorUnbondingDelegationsHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get all outgoing redelegations from a validator
r.HandleFunc(
"/stake/validators/{validatorAddr}/redelegations",
validatorRedelegationsHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get the current state of the staking pool
r.HandleFunc(
"/stake/pool",
Expand All @@ -122,11 +115,6 @@ func delegatorUnbondingDelegationsHandlerFn(cliCtx context.CLIContext, cdc *code
return queryDelegator(cliCtx, cdc, "custom/stake/delegatorUnbondingDelegations")
}

// HTTP request handler to query a delegator redelegations
func delegatorRedelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return queryDelegator(cliCtx, cdc, "custom/stake/delegatorRedelegations")
}

// HTTP request handler to query all staking txs (msgs) from a delegator
func delegatorTxsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -203,6 +191,57 @@ func unbondingDelegationHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) h
return queryBonds(cliCtx, cdc, "custom/stake/unbondingDelegation")
}

// HTTP request handler to query redelegations
func redelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var params stake.QueryRedelegationParams

bechDelegatorAddr := r.URL.Query().Get("delegator")
bechSrcValidatorAddr := r.URL.Query().Get("validator_from")
bechDstValidatorAddr := r.URL.Query().Get("validator_to")

if len(bechDelegatorAddr) != 0 {
delegatorAddr, err := sdk.AccAddressFromBech32(bechDelegatorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.DelegatorAddr = delegatorAddr
}

if len(bechSrcValidatorAddr) != 0 {
srcValidatorAddr, err := sdk.ValAddressFromBech32(bechSrcValidatorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.SrcValidatorAddr = srcValidatorAddr
}

if len(bechDstValidatorAddr) != 0 {
dstValidatorAddr, err := sdk.ValAddressFromBech32(bechDstValidatorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.DstValidatorAddr = dstValidatorAddr
}

bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

res, err := cliCtx.QueryWithData("custom/stake/redelegations", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

// HTTP request handler to query a delegation
func delegationHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return queryBonds(cliCtx, cdc, "custom/stake/delegation")
Expand Down Expand Up @@ -245,11 +284,6 @@ func validatorUnbondingDelegationsHandlerFn(cliCtx context.CLIContext, cdc *code
return queryValidator(cliCtx, cdc, "custom/stake/validatorUnbondingDelegations")
}

// HTTP request handler to query all redelegations from a source validator
func validatorRedelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return queryValidator(cliCtx, cdc, "custom/stake/validatorRedelegations")
}

// HTTP request handler to query the pool information
func poolHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand Down
44 changes: 44 additions & 0 deletions x/stake/client/rest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,50 @@ func queryTxs(node rpcclient.Client, cliCtx context.CLIContext, cdc *codec.Codec
return tx.FormatTxResults(cdc, res.Txs)
}

func queryRedelegations(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bech32delegator := vars["delegatorAddr"]
bech32srcValidator := vars["srcValidatorAddr"]
bech32dstValidator := vars["dstValidatorAddr"]

delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
srcValidatorAddr, err := sdk.ValAddressFromBech32(bech32srcValidator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
dstValidatorAddr, err := sdk.ValAddressFromBech32(bech32dstValidator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

params := stake.QueryRedelegationParams{
DelegatorAddr: delegatorAddr,
SrcValidatorAddr: srcValidatorAddr,
DstValidatorAddr: dstValidatorAddr,
}

bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

res, err := cliCtx.QueryWithData(endpoint, bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

func queryBonds(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
Expand Down
4 changes: 2 additions & 2 deletions x/stake/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func TestRedelegation(t *testing.T) {
require.Equal(t, 1, len(redelegations))
require.True(t, redelegations[0].Equal(resRed))

redelegations = keeper.GetAllRedelegations(ctx, addrDels[0])
redelegations = keeper.GetAllRedelegations(ctx, addrDels[0], nil, nil)
require.Equal(t, 1, len(redelegations))
require.True(t, redelegations[0].Equal(resRed))

Expand Down Expand Up @@ -580,7 +580,7 @@ func TestRedelegation(t *testing.T) {
redelegations = keeper.GetRedelegations(ctx, addrDels[0], 5)
require.Equal(t, 0, len(redelegations))

redelegations = keeper.GetAllRedelegations(ctx, addrDels[0])
redelegations = keeper.GetAllRedelegations(ctx, addrDels[0], nil, nil)
require.Equal(t, 0, len(redelegations))
}

Expand Down
Loading

0 comments on commit f4fb360

Please sign in to comment.