Split delegator summary endpoint

This commit is contained in:
Federico Kunze 2018-10-20 22:25:50 +02:00
parent 57277d1143
commit 152b3bd67b
5 changed files with 143 additions and 62 deletions

View File

@ -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)
@ -575,11 +573,12 @@ 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")
@ -587,9 +586,8 @@ func TestBonding(t *testing.T) {
// 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, "")
@ -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 summary stake.DelegationSummary
var dels []stake.Delegation
err := cdc.UnmarshalJSON([]byte(body), &summary)
err := cdc.UnmarshalJSON([]byte(body), &dels)
require.Nil(t, err)
return summary
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 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 {

View File

@ -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
@ -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

View File

@ -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
@ -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:
@ -102,23 +108,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)
unbondingDelegations := k.GetAllUnbondingDelegations(ctx, params.DelegatorAddr)
redelegations := k.GetAllRedelegations(ctx, params.DelegatorAddr)
summary := types.DelegationSummary{
Delegations: delegations,
UnbondingDelegations: unbondingDelegations,
Redelegations: redelegations,
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("")
}
res, errRes = codec.MarshalJSONIndent(cdc, summary)
unbondingDelegations := k.GetAllUnbondingDelegations(ctx, params.DelegatorAddr)
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, redelegations)
if errRes != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
}

View File

@ -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]
@ -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)
}

View File

@ -84,18 +84,6 @@ var (
NewQuerier = querier.NewQuerier
)
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
)
const (
DefaultCodespace = types.DefaultCodespace
CodeInvalidValidator = types.CodeInvalidValidator