Skip to content

Commit

Permalink
Merge PR #2543: Split GET delegator endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes authored Oct 23, 2018
2 parents 45c9d82 + 64f2235 commit 5476b1b
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 64 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ BREAKING CHANGES
* Gaia REST API (`gaiacli advanced rest-server`)
* [x/stake] Validator.Owner renamed to Validator.Operator
* [\#595](https://github.com/cosmos/cosmos-sdk/issues/595) Connections to the REST server are now secured using Transport Layer Security by default. The --insecure flag is provided to switch back to insecure HTTP.
* [gaia-lite] [\#2258](https://github.com/cosmos/cosmos-sdk/issues/2258) Split `GET stake/delegators/{delegatorAddr}` into `GET stake/delegators/{delegatorAddr}/delegations`, `GET stake/delegators/{delegatorAddr}/unbonding_delegations` and `GET stake/delegators/{delegatorAddr}/redelegations`

* Gaia CLI (`gaiacli`)
* [x/stake] Validator.Owner renamed to Validator.Operator
Expand Down
64 changes: 42 additions & 22 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,9 @@ func TestBonding(t *testing.T) {
bond := getDelegation(t, port, addr, operAddrs[0])
require.Equal(t, amt, bond.Shares)

summary := getDelegationSummary(t, port, addr)

require.Len(t, summary.Delegations, 1, "Delegation summary holds all delegations")
require.Equal(t, amt, summary.Delegations[0].Shares)
require.Len(t, summary.UnbondingDelegations, 0, "Delegation summary holds all unbonding-delegations")
delegatorDels := getDelegatorDelegations(t, port, addr)
require.Len(t, delegatorDels, 1)
require.Equal(t, amt, delegatorDels[0].Shares)

bondedValidators := getDelegatorValidators(t, port, addr)
require.Len(t, bondedValidators, 1)
Expand All @@ -557,8 +555,6 @@ func TestBonding(t *testing.T) {
require.Equal(t, operAddrs[0], bondedValidator.OperatorAddr)

// testing unbonding

// create unbond TX
resultTx = doBeginUnbonding(t, port, seed, name, password, addr, operAddrs[0], 30)
tests.WaitForHeight(resultTx.Height+1, port)

Expand All @@ -580,15 +576,18 @@ func TestBonding(t *testing.T) {
require.Equal(t, uint32(0), resultTx.CheckTx.Code)
require.Equal(t, uint32(0), resultTx.DeliverTx.Code)

summary = getDelegationSummary(t, port, addr)
// query delegations, unbondings and redelegations from validator and delegator
delegatorDels = getDelegatorDelegations(t, port, addr)
require.Len(t, delegatorDels, 1)
require.Equal(t, "30.0000000000", delegatorDels[0].GetShares().String())

require.Len(t, summary.Delegations, 1, "Delegation summary holds all delegations")
require.Len(t, summary.UnbondingDelegations, 1, "Delegation summary holds all unbonding-delegations")
require.Len(t, summary.Redelegations, 1, "Delegation summary holds all redelegations")
delegatorUbds := getDelegatorUnbondingDelegations(t, port, addr)
require.Len(t, delegatorUbds, 1)
require.Equal(t, "30", delegatorUbds[0].Balance.Amount.String())

require.Equal(t, "30.0000000000", summary.Delegations[0].GetShares().String())
require.Equal(t, "30", summary.UnbondingDelegations[0].Balance.Amount.String())
require.Equal(t, "30", summary.Redelegations[0].Balance.Amount.String())
delegatorReds := getDelegatorRedelegations(t, port, addr)
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)
Expand All @@ -598,9 +597,6 @@ func TestBonding(t *testing.T) {
require.Len(t, validatorReds, 1)
require.Equal(t, "30", validatorReds[0].Balance.Amount.String())

bondedValidators = getDelegatorValidators(t, port, addr)
require.Len(t, bondedValidators, 1, "There's a delegation as the user only withdraw half of the funds")

// TODO Undonding status not currently implemented
// require.Equal(t, sdk.Unbonding, bondedValidators[0].Status)

Expand Down Expand Up @@ -997,16 +993,40 @@ func getUndelegation(t *testing.T, port string, delegatorAddr sdk.AccAddress, va
return unbond
}

func getDelegationSummary(t *testing.T, port string, delegatorAddr sdk.AccAddress) stake.DelegationSummary {
res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s", delegatorAddr), nil)
func getDelegatorDelegations(t *testing.T, port string, delegatorAddr sdk.AccAddress) []stake.Delegation {
res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/delegations", delegatorAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var dels []stake.Delegation

err := cdc.UnmarshalJSON([]byte(body), &dels)
require.Nil(t, err)

return dels
}

func getDelegatorUnbondingDelegations(t *testing.T, port string, delegatorAddr sdk.AccAddress) []stake.UnbondingDelegation {
res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/unbonding_delegations", delegatorAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var summary stake.DelegationSummary
var ubds []stake.UnbondingDelegation

err := cdc.UnmarshalJSON([]byte(body), &summary)
err := cdc.UnmarshalJSON([]byte(body), &ubds)
require.Nil(t, err)

return summary
return ubds
}

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)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var reds []stake.Redelegation

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

return reds
}

func getBondingTxs(t *testing.T, port string, delegatorAddr sdk.AccAddress, query string) []tx.Info {
Expand Down
32 changes: 27 additions & 5 deletions x/stake/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ const storeName = "stake"

func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {

// Get all delegations (delegation, undelegation and redelegation) from a delegator
// Get all delegations from a delegator
r.HandleFunc(
"/stake/delegators/{delegatorAddr}",
delegatorHandlerFn(cliCtx, cdc),
"/stake/delegators/{delegatorAddr}/delegations",
delegatorDelegationsHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get all unbonding delegations from a delegator
r.HandleFunc(
"/stake/delegators/{delegatorAddr}/unbonding_delegations",
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
Expand Down Expand Up @@ -93,8 +105,18 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Co
}

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

// HTTP request handler to query a delegator unbonding delegations
func delegatorUnbondingDelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
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
Expand Down
57 changes: 47 additions & 10 deletions x/stake/querier/queryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
const (
QueryValidators = "validators"
QueryValidator = "validator"
QueryDelegatorDelegations = "delegatorDelegations"
QueryDelegatorUnbondingDelegations = "delegatorUnbondingDelegations"
QueryDelegatorRedelegations = "delegatorRedelegations"
QueryValidatorUnbondingDelegations = "validatorUnbondingDelegations"
QueryValidatorRedelegations = "validatorRedelegations"
QueryDelegator = "delegator"
Expand All @@ -35,12 +38,16 @@ func NewQuerier(k keep.Keeper, cdc *codec.Codec) sdk.Querier {
return queryValidatorUnbondingDelegations(ctx, cdc, req, k)
case QueryValidatorRedelegations:
return queryValidatorRedelegations(ctx, cdc, req, k)
case QueryDelegator:
return queryDelegator(ctx, cdc, req, k)
case QueryDelegation:
return queryDelegation(ctx, cdc, req, k)
case QueryUnbondingDelegation:
return queryUnbondingDelegation(ctx, cdc, req, k)
case QueryDelegatorDelegations:
return queryDelegatorDelegations(ctx, cdc, req, k)
case QueryDelegatorUnbondingDelegations:
return queryDelegatorUnbondingDelegations(ctx, cdc, req, k)
case QueryDelegatorRedelegations:
return queryDelegatorRedelegations(ctx, cdc, req, k)
case QueryDelegatorValidators:
return queryDelegatorValidators(ctx, cdc, req, k)
case QueryDelegatorValidator:
Expand All @@ -56,7 +63,9 @@ func NewQuerier(k keep.Keeper, cdc *codec.Codec) sdk.Querier {
}

// defines the params for the following queries:
// - 'custom/stake/delegator'
// - 'custom/stake/delegatorDelegations'
// - 'custom/stake/delegatorUnbondingDelegations'
// - 'custom/stake/delegatorRedelegations'
// - 'custom/stake/delegatorValidators'
type QueryDelegatorParams struct {
DelegatorAddr sdk.AccAddress
Expand Down Expand Up @@ -144,23 +153,51 @@ func queryValidatorRedelegations(ctx sdk.Context, cdc *codec.Codec, req abci.Req
return res, nil
}

func queryDelegator(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
func queryDelegatorDelegations(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
var params QueryDelegatorParams

errRes := cdc.UnmarshalJSON(req.Data, &params)
if errRes != nil {
return []byte{}, sdk.ErrUnknownAddress("")
}

delegations := k.GetAllDelegatorDelegations(ctx, params.DelegatorAddr)

res, errRes = codec.MarshalJSONIndent(cdc, delegations)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}

func queryDelegatorUnbondingDelegations(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
var params QueryDelegatorParams

errRes := cdc.UnmarshalJSON(req.Data, &params)
if errRes != nil {
return []byte{}, sdk.ErrUnknownAddress("")
}

unbondingDelegations := k.GetAllUnbondingDelegations(ctx, params.DelegatorAddr)
redelegations := k.GetAllRedelegations(ctx, params.DelegatorAddr)

summary := types.DelegationSummary{
Delegations: delegations,
UnbondingDelegations: unbondingDelegations,
Redelegations: redelegations,
res, errRes = codec.MarshalJSONIndent(cdc, unbondingDelegations)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
return res, nil
}

func queryDelegatorRedelegations(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
var params QueryDelegatorParams

res, errRes = codec.MarshalJSONIndent(cdc, summary)
errRes := cdc.UnmarshalJSON(req.Data, &params)
if errRes != nil {
return []byte{}, sdk.ErrUnknownAddress("")
}

redelegations := k.GetAllRedelegations(ctx, params.DelegatorAddr)

res, errRes = codec.MarshalJSONIndent(cdc, redelegations)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}
Expand Down
Loading

0 comments on commit 5476b1b

Please sign in to comment.