Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Split GET delegator endpoint #2543

Merged
merged 11 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
56 changes: 39 additions & 17 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,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 Down Expand Up @@ -575,21 +573,21 @@ func TestBonding(t *testing.T) {
unbonding := getUndelegation(t, port, addr, operAddrs[0])
require.Equal(t, "60", unbonding.Balance.Amount.String())

summary = getDelegationSummary(t, port, addr)
delegatorDels = getDelegatorDelegations(t, port, addr)
require.Len(t, delegatorDels, 0)

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

bondedValidators = getDelegatorValidators(t, port, addr)
require.Len(t, bondedValidators, 0, "There's no delegation as the user withdraw all funds")

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

// TODO add redelegation, need more complex capabilities such to mock context and
// TODO check summary for redelegation
// assert.Len(t, summary.Redelegations, 1, "Delegation summary holds all redelegations")
delegatorReds := getDelegatorRedelegations(t, port, addr)
require.Len(t, delegatorReds, 0)

// query txs
txs := getBondingTxs(t, port, addr, "")
Expand Down Expand Up @@ -981,16 +979,40 @@ func getUndelegation(t *testing.T, port string, delegatorAddr sdk.AccAddress, va
return unbondings
}

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 ubds []stake.UnbondingDelegation

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

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 summary stake.DelegationSummary
var reds []stake.Redelegation

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

return summary
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 @@ -19,10 +19,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 @@ -82,8 +94,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
73 changes: 55 additions & 18 deletions x/stake/querier/queryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (

// query endpoints supported by the staking Querier
const (
QueryValidators = "validators"
QueryValidator = "validator"
QueryDelegator = "delegator"
QueryDelegation = "delegation"
QueryUnbondingDelegation = "unbondingDelegation"
QueryDelegatorValidators = "delegatorValidators"
QueryDelegatorValidator = "delegatorValidator"
QueryPool = "pool"
QueryParameters = "parameters"
QueryValidators = "validators"
QueryValidator = "validator"
QueryDelegatorDelegations = "delegatorDelegations"
QueryDelegatorUnbondingDelegations = "delegatorUnbondingDelegations"
QueryDelegatorRedelegations = "delegatorRedelegations"
QueryDelegation = "delegation"
QueryUnbondingDelegation = "unbondingDelegation"
QueryDelegatorValidators = "delegatorValidators"
QueryDelegatorValidator = "delegatorValidator"
QueryPool = "pool"
QueryParameters = "parameters"
)

// creates a querier for staking REST endpoints
Expand All @@ -29,8 +31,12 @@ func NewQuerier(k keep.Keeper, cdc *codec.Codec) sdk.Querier {
return queryValidators(ctx, cdc, k)
case QueryValidator:
return queryValidator(ctx, cdc, req, k)
case QueryDelegator:
return queryDelegator(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 QueryDelegation:
return queryDelegation(ctx, cdc, req, k)
case QueryUnbondingDelegation:
Expand All @@ -51,6 +57,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 @@ -102,23 +111,51 @@ func queryValidator(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k
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

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

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

res, errRes = codec.MarshalJSONIndent(cdc, summary)
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
31 changes: 23 additions & 8 deletions x/stake/querier/queryable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,22 @@ func TestQueryDelegation(t *testing.T) {

require.Equal(t, delegation, delegationRes)

// Query Delegator Delegations

query = abci.RequestQuery{
Path: "/custom/stake/delegatorDelegations",
Data: bz,
}

res, err = queryDelegatorDelegations(ctx, cdc, query, keeper)
require.Nil(t, err)

var delegatorDelegations []types.Delegation
errRes = cdc.UnmarshalJSON(res, &delegatorDelegations)
require.Nil(t, errRes)
require.Len(t, delegatorDelegations, 1)
require.Equal(t, delegation, delegatorDelegations[0])

// error unknown request
query.Data = bz[:len(bz)-1]

Expand Down Expand Up @@ -273,25 +289,24 @@ func TestQueryDelegation(t *testing.T) {
_, err = queryUnbondingDelegation(ctx, cdc, query, keeper)
require.NotNil(t, err)

// Query Delegator Summary
// Query Delegator Delegations

query = abci.RequestQuery{
Path: "/custom/stake/delegator",
Path: "/custom/stake/delegatorUnbondingDelegations",
Data: bz,
}

res, err = queryDelegator(ctx, cdc, query, keeper)
res, err = queryDelegatorUnbondingDelegations(ctx, cdc, query, keeper)
require.Nil(t, err)

var summary types.DelegationSummary
errRes = cdc.UnmarshalJSON(res, &summary)
var delegatorUbds []types.UnbondingDelegation
errRes = cdc.UnmarshalJSON(res, &delegatorUbds)
require.Nil(t, errRes)

require.Equal(t, unbond, summary.UnbondingDelegations[0])
require.Equal(t, unbond, delegatorUbds[0])

// error unknown request
query.Data = bz[:len(bz)-1]

_, err = queryDelegator(ctx, cdc, query, keeper)
_, err = queryDelegatorUnbondingDelegations(ctx, cdc, query, keeper)
require.NotNil(t, err)
}
21 changes: 11 additions & 10 deletions x/stake/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type (
Description = types.Description
Commission = types.Commission
Delegation = types.Delegation
DelegationSummary = types.DelegationSummary
UnbondingDelegation = types.UnbondingDelegation
Redelegation = types.Redelegation
Params = types.Params
Expand Down Expand Up @@ -85,15 +84,17 @@ var (
)

const (
QueryValidators = querier.QueryValidators
QueryValidator = querier.QueryValidator
QueryDelegator = querier.QueryDelegator
QueryDelegation = querier.QueryDelegation
QueryUnbondingDelegation = querier.QueryUnbondingDelegation
QueryDelegatorValidators = querier.QueryDelegatorValidators
QueryDelegatorValidator = querier.QueryDelegatorValidator
QueryPool = querier.QueryPool
QueryParameters = querier.QueryParameters
QueryValidators = querier.QueryValidators
QueryValidator = querier.QueryValidator
QueryDelegatorDelegations = querier.QueryDelegatorDelegations
QueryDelegatorUnbondingDelegations = querier.QueryDelegatorUnbondingDelegations
QueryDelegatorRedelegations = querier.QueryDelegatorRedelegations
QueryDelegation = querier.QueryDelegation
QueryUnbondingDelegation = querier.QueryUnbondingDelegation
QueryDelegatorValidators = querier.QueryDelegatorValidators
QueryDelegatorValidator = querier.QueryDelegatorValidator
QueryPool = querier.QueryPool
QueryParameters = querier.QueryParameters
)

const (
Expand Down
7 changes: 0 additions & 7 deletions x/stake/types/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ type delegationValue struct {
Height int64
}

// aggregates of all delegations, unbondings and redelegations
type DelegationSummary struct {
Delegations []Delegation `json:"delegations"`
UnbondingDelegations []UnbondingDelegation `json:"unbonding_delegations"`
Redelegations []Redelegation `json:"redelegations"`
}

// return the delegation without fields contained within the key for the store
func MustMarshalDelegation(cdc *codec.Codec, delegation Delegation) []byte {
val := delegationValue{
Expand Down