Skip to content

Commit

Permalink
Implement AC1 and update relevant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio committed Apr 12, 2019
1 parent 3c88ddc commit 2319880
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
4 changes: 3 additions & 1 deletion client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank"
dclcommon "github.com/cosmos/cosmos-sdk/x/distribution/client/common"
distrrest "github.com/cosmos/cosmos-sdk/x/distribution/client/rest"
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
Expand Down Expand Up @@ -991,9 +992,10 @@ func TestDistributionFlow(t *testing.T) {
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &rewards))

// Query delegator's rewards total
var delRewards disttypes.QueryDelegatorTotalRewardsResponse
res, body = Request(t, port, "GET", fmt.Sprintf("/distribution/delegators/%s/rewards", operAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &rewards))
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &delRewards), body)

// Query delegator's withdrawal address
var withdrawAddr string
Expand Down
20 changes: 17 additions & 3 deletions client/lcd/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1470,9 +1470,7 @@ paths:
200:
description: OK
schema:
type: array
items:
$ref: "#/definitions/Coin"
$ref: "#/definitions/DelegatorTotalRewards"
400:
description: Invalid delegator address
500:
Expand Down Expand Up @@ -2096,6 +2094,22 @@ definitions:
$ref: "#/definitions/BlockID"
block:
$ref: "#/definitions/Block"
DelegationDelegatorReward:
type: object
properties:
validator_address:
$ref: "#/definitions/ValidatorAddress"
reward:
$ref: "#/definitions/Coin"
DelegatorTotalRewards:
type: object
properties:
rewards:
type: array
items:
$ref: "#/definitions/DelegationDelegatorReward"
total:
$ref: "#/definitions/Coin"
BaseReq:
type: object
properties:
Expand Down
4 changes: 4 additions & 0 deletions x/distribution/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type (
QueryValidatorSlashesParams = keeper.QueryValidatorSlashesParams
QueryDelegationRewardsParams = keeper.QueryDelegationRewardsParams
QueryDelegatorWithdrawAddrParams = keeper.QueryDelegatorWithdrawAddrParams

// querier response types
QueryDelegatorTotalRewardsResponse = types.QueryDelegatorTotalRewardsResponse
DelegationDelegatorReward = types.DelegationDelegatorReward
)

const (
Expand Down
14 changes: 9 additions & 5 deletions x/distribution/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,21 +241,25 @@ func queryDelegatorTotalRewards(ctx sdk.Context, _ []string, req abci.RequestQue
// cache-wrap context as to not persist state changes during querying
ctx, _ = ctx.CacheContext()

totalRewards := sdk.DecCoins{}
total := sdk.DecCoins{}
var delRewards []types.DelegationDelegatorReward

k.stakingKeeper.IterateDelegations(
ctx, params.DelegatorAddress,
func(_ int64, del sdk.Delegation) (stop bool) {
val := k.stakingKeeper.Validator(ctx, del.GetValidatorAddr())
valAddr := del.GetValidatorAddr()
val := k.stakingKeeper.Validator(ctx, valAddr)
endingPeriod := k.incrementValidatorPeriod(ctx, val)
rewards := k.calculateDelegationRewards(ctx, val, del, endingPeriod)
delReward := k.calculateDelegationRewards(ctx, val, del, endingPeriod)

totalRewards = totalRewards.Add(rewards)
delRewards = append(delRewards, types.NewDelegationDelegatorReward(valAddr, delReward))
total = total.Add(delReward)
return false
},
)

bz, err := codec.MarshalJSONIndent(k.cdc, totalRewards)
bz, err := codec.MarshalJSONIndent(k.cdc,
types.NewQueryDelegatorTotalRewardsResponse(delRewards, total))
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}
Expand Down
2 changes: 2 additions & 0 deletions x/distribution/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(MsgWithdrawDelegatorReward{}, "cosmos-sdk/MsgWithdrawDelegationReward", nil)
cdc.RegisterConcrete(MsgWithdrawValidatorCommission{}, "cosmos-sdk/MsgWithdrawValidatorCommission", nil)
cdc.RegisterConcrete(MsgSetWithdrawAddress{}, "cosmos-sdk/MsgModifyWithdrawAddress", nil)
cdc.RegisterConcrete(QueryDelegatorTotalRewardsResponse{}, "cosmos-sdk/QueryDelegatorTotalRewardsResponse", nil)
cdc.RegisterConcrete(DelegationDelegatorReward{}, "cosmos-sdk/DelegationDelegatorReward", nil)
}

// generic sealed codec to be used throughout module
Expand Down
29 changes: 29 additions & 0 deletions x/distribution/types/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package types

import sdk "github.com/cosmos/cosmos-sdk/types"

// QueryDelegatorTotalRewardsResponse defines the properties of
// QueryDelegatorTotalRewards query's response.
type QueryDelegatorTotalRewardsResponse struct {
Rewards []DelegationDelegatorReward `json:"rewards"`
Total sdk.DecCoins `json:"total"`
}

// NewQueryDelegatorTotalRewardsResponse constructs a QueryDelegatorTotalRewardsResponse
func NewQueryDelegatorTotalRewardsResponse(rewards []DelegationDelegatorReward,
total sdk.DecCoins) QueryDelegatorTotalRewardsResponse {
return QueryDelegatorTotalRewardsResponse{Rewards: rewards, Total: total}
}

// DelegationDelegatorReward defines the properties
// of a delegator's delegation reward.
type DelegationDelegatorReward struct {
ValidatorAddress sdk.ValAddress `json:"validator_address"`
Reward sdk.DecCoins `json:"reward"`
}

// NewDelegationDelegatorReward constructs a DelegationDelegatorReward.
func NewDelegationDelegatorReward(valAddr sdk.ValAddress,
reward sdk.DecCoins) DelegationDelegatorReward {
return DelegationDelegatorReward{ValidatorAddress: valAddr, Reward: reward}
}

0 comments on commit 2319880

Please sign in to comment.