Skip to content

Commit

Permalink
Merge pull request #741 from MrXJC/jiacheng/release0.8
Browse files Browse the repository at this point in the history
R4R: Add the distribution reward into Tags
  • Loading branch information
HaoyangLiu authored Dec 3, 2018
2 parents c0fe7b8 + 4e6f38f commit 555bb3d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 31 deletions.
21 changes: 13 additions & 8 deletions modules/distribution/handler.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package distribution

import (
sdk "github.com/irisnet/irishub/types"
"github.com/irisnet/irishub/modules/distribution/keeper"
"github.com/irisnet/irishub/modules/distribution/tags"
"github.com/irisnet/irishub/modules/distribution/types"
sdk "github.com/irisnet/irishub/types"
)

func NewHandler(k keeper.Keeper) sdk.Handler {
Expand Down Expand Up @@ -45,20 +45,22 @@ func handleMsgModifyWithdrawAddress(ctx sdk.Context, msg types.MsgSetWithdrawAdd

func handleMsgWithdrawDelegatorRewardsAll(ctx sdk.Context, msg types.MsgWithdrawDelegatorRewardsAll, k keeper.Keeper) sdk.Result {

k.WithdrawDelegationRewardsAll(ctx, msg.DelegatorAddr)
reward, withdrawTags := k.WithdrawDelegationRewardsAll(ctx, msg.DelegatorAddr)

tags := sdk.NewTags(
resultTags := sdk.NewTags(
tags.Action, tags.ActionWithdrawDelegatorRewardsAll,
tags.Delegator, []byte(msg.DelegatorAddr.String()),
tags.Reward, []byte(reward.ToString()),
)
resultTags = resultTags.AppendTags(withdrawTags)
return sdk.Result{
Tags: tags,
Tags: resultTags,
}
}

func handleMsgWithdrawDelegatorReward(ctx sdk.Context, msg types.MsgWithdrawDelegatorReward, k keeper.Keeper) sdk.Result {

err := k.WithdrawDelegationReward(ctx, msg.DelegatorAddr, msg.ValidatorAddr)
reward, err := k.WithdrawDelegationReward(ctx, msg.DelegatorAddr, msg.ValidatorAddr)
if err != nil {
return err.Result()
}
Expand All @@ -67,6 +69,7 @@ func handleMsgWithdrawDelegatorReward(ctx sdk.Context, msg types.MsgWithdrawDele
tags.Action, tags.ActionWithdrawDelegatorReward,
tags.Delegator, []byte(msg.DelegatorAddr.String()),
tags.Validator, []byte(msg.ValidatorAddr.String()),
tags.Reward, []byte(reward.ToString()),
)
return sdk.Result{
Tags: tags,
Expand All @@ -75,16 +78,18 @@ func handleMsgWithdrawDelegatorReward(ctx sdk.Context, msg types.MsgWithdrawDele

func handleMsgWithdrawValidatorRewardsAll(ctx sdk.Context, msg types.MsgWithdrawValidatorRewardsAll, k keeper.Keeper) sdk.Result {

err := k.WithdrawValidatorRewardsAll(ctx, msg.ValidatorAddr)
reward, withdrawTags, err := k.WithdrawValidatorRewardsAll(ctx, msg.ValidatorAddr)
if err != nil {
return err.Result()
}

tags := sdk.NewTags(
resultTags := sdk.NewTags(
tags.Action, tags.ActionWithdrawValidatorRewardsAll,
tags.Validator, []byte(msg.ValidatorAddr.String()),
tags.Reward, []byte(reward.ToString()),
)
resultTags = resultTags.AppendTags(withdrawTags)
return sdk.Result{
Tags: tags,
Tags: resultTags,
}
}
20 changes: 12 additions & 8 deletions modules/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package keeper

import (
sdk "github.com/irisnet/irishub/types"
"fmt"
"github.com/irisnet/irishub/modules/distribution/types"
sdk "github.com/irisnet/irishub/types"
)

// check whether a delegator distribution info exists
Expand Down Expand Up @@ -127,10 +128,10 @@ func (k Keeper) WithdrawToDelegator(ctx sdk.Context, feePool types.FeePool,
// NOTE: This gets called "onDelegationSharesModified",
// meaning any changes to bonded coins
func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress,
valAddr sdk.ValAddress) sdk.Error {
valAddr sdk.ValAddress) (types.DecCoins, sdk.Error) {

if !k.HasDelegationDistInfo(ctx, delAddr, valAddr) {
return types.ErrNoDelegationDistInfo(k.codespace)
return nil, types.ErrNoDelegationDistInfo(k.codespace)
}

feePool, valInfo, delInfo, withdraw :=
Expand All @@ -139,7 +140,7 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress
k.SetValidatorDistInfo(ctx, valInfo)
k.SetDelegationDistInfo(ctx, delInfo)
k.WithdrawToDelegator(ctx, feePool, delAddr, withdraw)
return nil
return withdraw, nil
}

// current rewards for a single delegation
Expand All @@ -157,17 +158,19 @@ func (k Keeper) CurrentDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress,
//___________________________________________________________________________________________

// return all rewards for all delegations of a delegator
func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress) {
withdraw := k.withdrawDelegationRewardsAll(ctx, delAddr)
func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress) (types.DecCoins, sdk.Tags) {
withdraw, tags := k.withdrawDelegationRewardsAll(ctx, delAddr)
feePool := k.GetFeePool(ctx)
k.WithdrawToDelegator(ctx, feePool, delAddr, withdraw)
return withdraw, tags
}

func (k Keeper) withdrawDelegationRewardsAll(ctx sdk.Context,
delAddr sdk.AccAddress) types.DecCoins {
delAddr sdk.AccAddress) (types.DecCoins, sdk.Tags) {

// iterate over all the delegations
withdraw := types.DecCoins{}
var tags sdk.Tags
operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) {

valAddr := del.GetValidatorAddr()
Expand All @@ -177,10 +180,11 @@ func (k Keeper) withdrawDelegationRewardsAll(ctx sdk.Context,
k.SetFeePool(ctx, feePool)
k.SetValidatorDistInfo(ctx, valInfo)
k.SetDelegationDistInfo(ctx, delInfo)
tags = tags.AppendTag(fmt.Sprintf(sdk.TagRewardFromValidator, valAddr.String()), []byte(diWithdraw.ToString()))
return false
}
k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation)
return withdraw
return withdraw, tags
}

// get all rewards for all delegations of a delegator
Expand Down
6 changes: 3 additions & 3 deletions modules/distribution/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package keeper
import (
"fmt"

sdk "github.com/irisnet/irishub/types"
"github.com/irisnet/irishub/modules/distribution/types"
sdk "github.com/irisnet/irishub/types"
)

// Create a new validator distribution record
Expand All @@ -25,7 +25,7 @@ func (k Keeper) onValidatorCreated(ctx sdk.Context, valAddr sdk.ValAddress) {
func (k Keeper) onValidatorModified(ctx sdk.Context, valAddr sdk.ValAddress) {
// This doesn't need to be run at genesis
if ctx.BlockHeight() > 0 {
if err := k.WithdrawValidatorRewardsAll(ctx, valAddr); err != nil {
if _, _, err := k.WithdrawValidatorRewardsAll(ctx, valAddr); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -72,7 +72,7 @@ func (k Keeper) onDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress,
func (k Keeper) onDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress,
valAddr sdk.ValAddress) {

if err := k.WithdrawDelegationReward(ctx, delAddr, valAddr); err != nil {
if _, err := k.WithdrawDelegationReward(ctx, delAddr, valAddr); err != nil {
panic(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/distribution/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package keeper

import (
"github.com/irisnet/irishub/codec"
sdk "github.com/irisnet/irishub/types"
"github.com/irisnet/irishub/modules/distribution/types"
"github.com/irisnet/irishub/modules/params"
sdk "github.com/irisnet/irishub/types"
)

// keeper of the stake store
Expand Down
12 changes: 6 additions & 6 deletions modules/distribution/keeper/validator.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package keeper

import (
sdk "github.com/irisnet/irishub/types"
"github.com/irisnet/irishub/modules/distribution/types"
sdk "github.com/irisnet/irishub/types"
)

// check whether a validator has distribution info
Expand Down Expand Up @@ -57,25 +57,25 @@ func (k Keeper) GetValidatorAccum(ctx sdk.Context, operatorAddr sdk.ValAddress)
}

// withdrawal all the validator rewards including the commission
func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.ValAddress) sdk.Error {
func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.ValAddress) (types.DecCoins, sdk.Tags, sdk.Error) {

if !k.HasValidatorDistInfo(ctx, operatorAddr) {
return types.ErrNoValidatorDistInfo(k.codespace)
return nil, nil, types.ErrNoValidatorDistInfo(k.codespace)
}

// withdraw self-delegation
accAddr := sdk.AccAddress(operatorAddr.Bytes())
withdraw := k.withdrawDelegationRewardsAll(ctx, accAddr)
withdraw, resultTags := k.withdrawDelegationRewardsAll(ctx, accAddr)

// withdrawal validator commission rewards
valInfo := k.GetValidatorDistInfo(ctx, operatorAddr)
wc := k.GetWithdrawContext(ctx, operatorAddr)
valInfo, feePool, commission := valInfo.WithdrawCommission(wc)
withdraw = withdraw.Plus(commission)
k.SetValidatorDistInfo(ctx, valInfo)

resultTags = resultTags.AppendTag(sdk.TagRewardCommission, []byte(commission.ToString()))
k.WithdrawToDelegator(ctx, feePool, accAddr, withdraw)
return nil
return withdraw, resultTags, nil
}

// get all the validator rewards including the commission
Expand Down
1 change: 1 addition & 0 deletions modules/distribution/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ var (
Action = sdk.TagAction
Validator = sdk.TagSrcValidator
Delegator = sdk.TagDelegator
Reward = sdk.TagReward
)
14 changes: 14 additions & 0 deletions modules/distribution/types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"

"bytes"

sdk "github.com/irisnet/irishub/types"
)

Expand Down Expand Up @@ -176,3 +178,15 @@ func (coins DecCoins) AmountOf(denom string) sdk.Dec {
}
}
}

func (coins DecCoins) ToString() string {
var rewardBytes bytes.Buffer
for i, coin := range coins {
if i != 0 {
rewardBytes.WriteString(",")
}
rewardBytes.WriteString(coin.Amount.String())
rewardBytes.WriteString(coin.Denom)
}
return rewardBytes.String()
}
12 changes: 7 additions & 5 deletions types/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ func MakeTag(k string, v []byte) Tag {

// common tags
var (
TagAction = "action"
TagSrcValidator = "source-validator"
TagDstValidator = "destination-validator"
TagDelegator = "delegator"
TagReward = "withdraw-reward"
TagAction = "action"
TagSrcValidator = "source-validator"
TagDstValidator = "destination-validator"
TagDelegator = "delegator"
TagReward = "withdraw-reward-total"
TagRewardFromValidator = "withdraw-reward-from-validator-%s"
TagRewardCommission = "withdraw-reward-commission"
)

0 comments on commit 555bb3d

Please sign in to comment.