Skip to content

Commit

Permalink
Merge branch 'feat/pss-upgrade-v50' into sainoe/pss-reward-distr
Browse files Browse the repository at this point in the history
  • Loading branch information
sainoe committed Jun 25, 2024
2 parents cdb14ec + 3e63d54 commit f7f38b3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
42 changes: 23 additions & 19 deletions tests/integration/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,8 @@ func (s *CCVTestSuite) TestAllocateTokens() {
totalRewards := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(100))}

// increase the block height so validators are eligible for consumer rewards (see `IsEligibleForConsumerRewards`)
numberOfBlocksToStartReceivingRewards :=
providerKeeper.GetNumberOfEpochsToStartReceivingRewards(s.providerCtx()) * providerKeeper.GetBlocksPerEpoch(s.providerCtx())
numberOfBlocksToStartReceivingRewards := providerKeeper.GetNumberOfEpochsToStartReceivingRewards(
s.providerCtx()) * providerKeeper.GetBlocksPerEpoch(s.providerCtx())
providerCtx := s.providerCtx().WithBlockHeight(numberOfBlocksToStartReceivingRewards + s.providerCtx().BlockHeight())

// fund consumer rewards pool
Expand All @@ -733,32 +733,31 @@ func (s *CCVTestSuite) TestAllocateTokens() {
)

// Allocate rewards evenly between consumers
rewardsPerConsumer := totalRewards.QuoInt(math.NewInt(int64(len(s.consumerBundles))))
rewardsPerChain := totalRewards.QuoInt(math.NewInt(int64(len(s.consumerBundles))))
for chainID := range s.consumerBundles {
// update consumer allocation
providerKeeper.SetConsumerRewardsAllocation(
providerCtx,
chainID,
providertypes.ConsumerRewardsAllocation{
Rewards: sdk.NewDecCoinsFromCoins(rewardsPerConsumer...),
Rewards: sdk.NewDecCoinsFromCoins(rewardsPerChain...),
},
)
}

// Iterate over the validators and
// store their current voting power and outstanding rewards
lastValOutRewards := map[string]sdk.DecCoins{}
// iterate over the validators and verify that no validator has outstanding rewards
totalValsRewards := sdk.DecCoins{}
for _, val := range s.providerChain.Vals.Validators {
valRewards, err := distributionKeeper.GetValidatorOutstandingRewards(s.providerCtx(), sdk.ValAddress(val.Address))
valRewards, err := distributionKeeper.GetValidatorOutstandingRewards(providerCtx, sdk.ValAddress(val.Address))
s.Require().NoError(err)
lastValOutRewards[sdk.ValAddress(val.Address).String()] = valRewards.Rewards
totalValsRewards = totalValsRewards.Add(valRewards.Rewards...)
}

s.Require().True(totalValsRewards.IsZero())

// store community pool balance
// At this point the distribution module account
// only holds the community pool's tokens
// since there are no validators with outstanding rewards
lastCommPool := getDistrAcctBalFn(providerCtx)

// execute BeginBlock to trigger the token allocation
Expand All @@ -768,15 +767,15 @@ func (s *CCVTestSuite) TestAllocateTokens() {
consNum := len(s.consumerBundles)

// compute the expected validators token allocation by subtracting the community tax
rewardsPerConsumerDec := sdk.NewDecCoinsFromCoins(rewardsPerConsumer...)
rewardsPerChainDec := sdk.NewDecCoinsFromCoins(rewardsPerChain...)
communityTax, err := distributionKeeper.GetCommunityTax(providerCtx)
s.Require().NoError(err)

rewardsPerChain, _ := rewardsPerConsumerDec.
rewardsPerChainTrunc, _ := rewardsPerChainDec.
MulDecTruncate(math.LegacyOneDec().Sub(communityTax)).TruncateDecimal()
validatorsExpRewardsPerchain := sdk.NewDecCoinsFromCoins(rewardsPerChain...).QuoDec(math.LegacyNewDec(int64(valNum)))
validatorsExpRewardsPerChain := sdk.NewDecCoinsFromCoins(rewardsPerChainTrunc...).QuoDec(math.LegacyNewDec(int64(valNum)))
// multiply by the number of consumers
validatorsExpRewards := validatorsExpRewardsPerchain.MulDec(math.LegacyNewDec(int64(consNum)))
validatorsExpRewards := validatorsExpRewardsPerChain.MulDec(math.LegacyNewDec(int64(consNum)))

// verify the validator tokens allocation
// note that the validators have the same voting power to keep things simple
Expand All @@ -786,15 +785,20 @@ func (s *CCVTestSuite) TestAllocateTokens() {

s.Require().Equal(
valRewards.Rewards,
lastValOutRewards[sdk.ValAddress(val.Address).String()].Add(validatorsExpRewards...),
validatorsExpRewards,
)
}

// check that the total expected rewards is transferred to the distribution module account
allocRemPerConsu := providerKeeper.GetConsumerRewardsAllocation(s.providerCtx(), s.consumerChain.ChainID).Rewards
totalRewardsDistributed := sdk.NewDecCoinsFromCoins(totalRewards...).Sub(allocRemPerConsu.MulDec(math.LegacyNewDec(int64(consNum))))
// check that the total expected rewards are transferred to the distribution module account

// store the decimal remainders in the consumer reward allocations
allocRemainderPerChain := providerKeeper.GetConsumerRewardsAllocation(providerCtx, s.consumerChain.ChainID).Rewards

// compute the total rewards distributed to the distribution module balance (validator outstanding rewards + community pool tax),
totalRewardsDistributed := sdk.NewDecCoinsFromCoins(totalRewards...).Sub(allocRemainderPerChain.MulDec(math.LegacyNewDec(int64(consNum))))

s.Require().Equal(lastCommPool.Add(totalRewardsDistributed...), getDistrAcctBalFn(s.providerCtx()))
// compare the expected total rewards against the distribution module balance
s.Require().Equal(lastCommPool.Add(totalRewardsDistributed...), getDistrAcctBalFn(providerCtx))
}

// getEscrowBalance gets the current balances in the escrow account holding the transferred tokens to the provider
Expand Down
16 changes: 8 additions & 8 deletions x/ccv/provider/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ func (k Keeper) AllocateTokens(ctx sdk.Context) {
// compute rewards for validators
consumerRewards := alloc.Rewards
voteMultiplier := math.LegacyOneDec().Sub(communityTax)
feeMultiplier := consumerRewards.MulDecTruncate(voteMultiplier)
validatorsRewards := consumerRewards.MulDecTruncate(voteMultiplier)

// compute remaining rewards for the community pool
remaining := consumerRewards.Sub(feeMultiplier)
remaining := consumerRewards.Sub(validatorsRewards)

// transfer validators rewards to distribution module account
feeMultiplierTrunc, feeMultiplierChange := feeMultiplier.TruncateDecimal()
err = k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ConsumerRewardsPool, distrtypes.ModuleName, feeMultiplierTrunc)
validatorsRewardsTrunc, validatorsRewardsChange := validatorsRewards.TruncateDecimal()
err = k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ConsumerRewardsPool, distrtypes.ModuleName, validatorsRewardsTrunc)
if err != nil {
k.Logger(ctx).Error(
"cannot send rewards to distribution module account %s: %s",
Expand All @@ -146,12 +146,12 @@ func (k Keeper) AllocateTokens(ctx sdk.Context) {
k.AllocateTokensToConsumerValidators(
ctx,
consumerChainID,
sdk.NewDecCoinsFromCoins(feeMultiplierTrunc...),
sdk.NewDecCoinsFromCoins(validatorsRewardsTrunc...),
)

// allocate remaining rewards to the community pool
remainingCoins, remainingChanges := remaining.TruncateDecimal()
err = k.distributionKeeper.FundCommunityPool(context.Context(ctx), remainingCoins, k.accountKeeper.GetModuleAccount(ctx, types.ConsumerRewardsPool).GetAddress())
remainingRewards, remainingChanges := remaining.TruncateDecimal()
err = k.distributionKeeper.FundCommunityPool(context.Context(ctx), remainingRewards, k.accountKeeper.GetModuleAccount(ctx, types.ConsumerRewardsPool).GetAddress())
if err != nil {
k.Logger(ctx).Error(
"fail to allocate rewards from consumer chain %s to community pool: %s",
Expand All @@ -162,7 +162,7 @@ func (k Keeper) AllocateTokens(ctx sdk.Context) {
}

// set consumer allocations to the remaining rewards decimals
alloc.Rewards = feeMultiplierChange.Add(remainingChanges...)
alloc.Rewards = validatorsRewardsChange.Add(remainingChanges...)
k.SetConsumerRewardsAllocation(ctx, consumerChainID, alloc)
}
}
Expand Down
8 changes: 4 additions & 4 deletions x/ccv/provider/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ func (h Hooks) BeforeDelegationRemoved(_ context.Context, _ sdk.AccAddress, _ sd
return nil
}

func (h Hooks) BeforeTokenizeShareRecordRemoved(_ context.Context, _ uint64) error {
return nil
}

//
// gov hooks
//
Expand Down Expand Up @@ -271,7 +275,3 @@ func (h Hooks) GetConsumerAdditionLegacyPropFromProp(
}
return providertypes.ConsumerAdditionProposal{}, false
}

func (h Hooks) BeforeTokenizeShareRecordRemoved(_ sdk.Context, _ uint64) error {
return nil
}

0 comments on commit f7f38b3

Please sign in to comment.