@@ -107,21 +107,37 @@ func (s *CCVTestSuite) TestRewardsDistribution() {
107
107
rewardCoins = providerBankKeeper .GetAllBalances (s .providerCtx (), rewardPool )
108
108
s .Require ().Equal (rewardCoins .AmountOf (rewardsIBCdenom ), providerExpRewardsAmount )
109
109
110
- // Set the consumer reward denom. This would be done by a governance proposal in prod.
110
+ // increase the block height so validators are eligible for consumer rewards (see `IsEligibleForConsumerRewards`)
111
+ currentProviderBlockHeight := s .providerCtx ().BlockHeight ()
112
+ numberOfBlocksToStartReceivingRewards := providerKeeper .GetNumberOfEpochsToStartReceivingRewards (s .providerCtx ()) * providerKeeper .GetBlocksPerEpoch (s .providerCtx ())
113
+ for s .providerCtx ().BlockHeight () <= currentProviderBlockHeight + numberOfBlocksToStartReceivingRewards {
114
+ s .providerChain .NextBlock ()
115
+ }
116
+
117
+ // Allowlist the consumer reward denom. This would be done by a governance proposal in prod.
111
118
providerKeeper .SetConsumerRewardDenom (s .providerCtx (), rewardsIBCdenom )
112
119
113
- // Refill the consumer fee pool
114
- err = consumerBankKeeper .SendCoinsFromAccountToModule (
115
- s .consumerCtx (),
116
- s .consumerChain .SenderAccount .GetAddress (),
117
- authtypes .FeeCollectorName ,
118
- fees ,
119
- )
120
+ // Even though the reward denom is allowlisted, the rewards are not yet distributed because `BeginBlockRD` has not executed.
121
+ // and hence the distribution has not taken place. Because of this, all the rewards are still stored in consumer rewards allocation by denom.
122
+ rewardsAlloc , err := providerKeeper .GetConsumerRewardsAllocationByDenom (s .providerCtx (), s .getFirstBundle ().ConsumerId , rewardsIBCdenom )
120
123
s .Require ().NoError (err )
124
+ remainingAlloc := rewardsAlloc .Rewards .AmountOf (rewardsIBCdenom )
125
+ s .Require ().Equal (
126
+ math .LegacyNewDecFromInt (providerExpRewardsAmount ),
127
+ rewardsAlloc .Rewards .AmountOf (rewardsIBCdenom ),
128
+ )
129
+ s .Require ().False (remainingAlloc .LTE (math .LegacyOneDec ()))
121
130
122
- // Pass two blocks
123
- s .consumerChain .NextBlock ()
124
- s .consumerChain .NextBlock ()
131
+ // Move by one block to execute `BeginBlockRD`.
132
+ s .providerChain .NextBlock ()
133
+
134
+ // Consumer allocations are now distributed between the validators and the community pool.
135
+ // The decimals resulting from the distribution are expected to remain in the consumer allocations
136
+ // and hence the check that remainingAlloc is less than one.
137
+ rewardsAlloc , err = providerKeeper .GetConsumerRewardsAllocationByDenom (s .providerCtx (), s .getFirstBundle ().ConsumerId , rewardsIBCdenom )
138
+ s .Require ().NoError (err )
139
+ remainingAlloc = rewardsAlloc .Rewards .AmountOf (rewardsIBCdenom )
140
+ s .Require ().True (remainingAlloc .LTE (math .LegacyOneDec ()))
125
141
126
142
// Save the consumer validators total outstanding rewards on the provider
127
143
consumerValsOutstandingRewardsFunc := func (ctx sdk.Context ) sdk.DecCoins {
@@ -137,45 +153,17 @@ func (s *CCVTestSuite) TestRewardsDistribution() {
137
153
valReward , _ := providerDistributionKeeper .GetValidatorOutstandingRewards (ctx , valAddr )
138
154
totalRewards = totalRewards .Add (valReward .Rewards ... )
139
155
}
140
- return totalRewards
141
- }
142
- consuValsRewards := consumerValsOutstandingRewardsFunc (s .providerCtx ())
143
156
144
- // increase the block height so validators are eligible for consumer rewards (see `IsEligibleForConsumerRewards`)
145
- numberOfBlocksToStartReceivingRewards := providerKeeper .GetNumberOfEpochsToStartReceivingRewards (s .providerCtx ()) * providerKeeper .GetBlocksPerEpoch (s .providerCtx ())
146
-
147
- for s .providerCtx ().BlockHeight () <= numberOfBlocksToStartReceivingRewards {
148
- s .providerChain .NextBlock ()
157
+ return totalRewards
149
158
}
150
159
151
- // Transfer rewards from consumer to provider and distribute rewards to
152
- // validators and community pool by calling BeginBlockRD
153
- relayAllCommittedPackets (
154
- s ,
155
- s .consumerChain ,
156
- s .transferPath ,
157
- transfertypes .PortID ,
158
- s .transferPath .EndpointA .ChannelID ,
159
- 1 ,
160
- )
161
-
162
- // Consumer allocations are distributed between the validators and the community pool.
163
- // The decimals resulting from the distribution are expected to remain in the consumer allocations.
164
- rewardsAlloc := providerKeeper .GetConsumerRewardsAllocation (s .providerCtx (), s .getFirstBundle ().ConsumerId )
165
- remainingAlloc := rewardsAlloc .Rewards .AmountOf (rewardsIBCdenom )
166
- s .Require ().True (remainingAlloc .LTE (math .LegacyOneDec ()))
167
-
168
- // Check that the reward pool still holds the coins from the first transfer
169
- // which were never allocated since they were not whitelisted
170
- // plus the remaining decimals from the second transfer.
160
+ // Check that the reward pool does not hold the coins from the transfer because
161
+ // after the coin got allowlisted, all coins of this denom were distributed.
171
162
rewardCoins = providerBankKeeper .GetAllBalances (s .providerCtx (), rewardPool )
172
- s .Require ().Equal (
173
- math .LegacyNewDecFromInt (rewardCoins .AmountOf (rewardsIBCdenom )),
174
- math .LegacyNewDecFromInt (providerExpRewardsAmount ).Add (remainingAlloc ),
175
- )
163
+ s .Require ().True (rewardCoins .AmountOf (rewardsIBCdenom ).LTE (math .NewInt (1 )))
176
164
177
165
// Check that the distribution module account balance is equal to the consumer rewards
178
- consuValsRewardsReceived := consumerValsOutstandingRewardsFunc (s .providerCtx ()). Sub ( consuValsRewards )
166
+ consuValsRewardsReceived := consumerValsOutstandingRewardsFunc (s .providerCtx ())
179
167
distrAcct := providerDistributionKeeper .GetDistributionAccount (s .providerCtx ())
180
168
distrAcctBalance := providerBankKeeper .GetAllBalances (s .providerCtx (), distrAcct .GetAddress ())
181
169
@@ -259,7 +247,7 @@ func (s *CCVTestSuite) TestSendRewardsRetries() {
259
247
}
260
248
261
249
// TestEndBlockRD tests that the last transmission block height (LTBH) is correctly updated after the expected
262
- // number of block have passed. It also checks that the IBC transfer transfer states are discarded if
250
+ // number of block have passed. It also checks that the IBC transfer states are discarded if
263
251
// the reward distribution to the provider has failed.
264
252
//
265
253
// Note: this method is effectively a unit test for EndBLockRD(), but is written as an integration test to avoid excessive mocking.
@@ -570,7 +558,7 @@ func (s *CCVTestSuite) TestIBCTransferMiddleware() {
570
558
{
571
559
"IBC Transfer coin denom isn't registered" ,
572
560
func (ctx sdk.Context , keeper * providerkeeper.Keeper , bankKeeper icstestingutils.TestBankKeeper ) {},
573
- false ,
561
+ true , // even if the denom is not registered/allowlisted, the rewards are still allocated by denom
574
562
false ,
575
563
},
576
564
{
@@ -600,9 +588,10 @@ func (s *CCVTestSuite) TestIBCTransferMiddleware() {
600
588
sdk .NewCoins (sdk .NewCoin (sdk .DefaultBondDenom , math .NewInt (100_000 ))),
601
589
)
602
590
// update consumer allocation
603
- keeper .SetConsumerRewardsAllocation (
591
+ keeper .SetConsumerRewardsAllocationByDenom (
604
592
ctx ,
605
593
s .getFirstBundle ().ConsumerId ,
594
+ getIBCDenom (packet .DestinationPort , packet .DestinationChannel ),
606
595
providertypes.ConsumerRewardsAllocation {
607
596
Rewards : sdk .NewDecCoins (sdk .NewDecCoin (sdk .DefaultBondDenom , math .NewInt (100_000 ))),
608
597
},
@@ -669,22 +658,25 @@ func (s *CCVTestSuite) TestIBCTransferMiddleware() {
669
658
rewardsPoolBalance := bankKeeper .GetAllBalances (s .providerCtx (), sdk .MustAccAddressFromBech32 (data .Receiver ))
670
659
671
660
// save the consumer's rewards allocated
672
- consumerRewardsAllocations := providerKeeper .GetConsumerRewardsAllocation (s .providerCtx (), s .getFirstBundle ().ConsumerId )
661
+ ibcDenom := getIBCDenom (packet .DestinationPort , packet .DestinationChannel )
662
+ consumerRewardsAllocations , err := providerKeeper .GetConsumerRewardsAllocationByDenom (s .providerCtx (), s .getFirstBundle ().ConsumerId , ibcDenom )
663
+ s .Require ().NoError (err )
673
664
674
665
// execute middleware OnRecvPacket logic
675
666
ack := cbs .OnRecvPacket (s .providerCtx (), packet , sdk.AccAddress {})
676
667
677
668
// compute expected rewards with provider denom
678
669
expRewards := sdk.Coin {
679
670
Amount : amount ,
680
- Denom : getIBCDenom ( packet . DestinationPort , packet . DestinationChannel ) ,
671
+ Denom : ibcDenom ,
681
672
}
682
673
683
674
// compute the balance and allocation difference
684
675
rewardsTransferred := bankKeeper .GetAllBalances (s .providerCtx (), sdk .MustAccAddressFromBech32 (data .Receiver )).
685
676
Sub (rewardsPoolBalance ... )
686
- rewardsAllocated := providerKeeper .GetConsumerRewardsAllocation (s .providerCtx (), s .getFirstBundle ().ConsumerId ).
687
- Rewards .Sub (consumerRewardsAllocations .Rewards )
677
+ rewardsAllocatedByDenom , err := providerKeeper .GetConsumerRewardsAllocationByDenom (s .providerCtx (), s .getFirstBundle ().ConsumerId , ibcDenom )
678
+ s .Require ().NoError (err )
679
+ rewardsAllocated := rewardsAllocatedByDenom .Rewards .Sub (consumerRewardsAllocations .Rewards )
688
680
689
681
if ! tc .expErr {
690
682
s .Require ().True (ack .Success ())
@@ -738,13 +730,18 @@ func (s *CCVTestSuite) TestAllocateTokens() {
738
730
totalRewards ,
739
731
)
740
732
733
+ // Allowlist a reward denom that the allocated rewards have
734
+ ibcDenom := "ibc/somedenom"
735
+ providerKeeper .SetConsumerRewardDenom (providerCtx , ibcDenom )
736
+
741
737
// Allocate rewards evenly between consumers
742
738
rewardsPerChain := totalRewards .QuoInt (math .NewInt (int64 (len (s .consumerBundles ))))
743
739
for consumerId := range s .consumerBundles {
744
740
// update consumer allocation
745
- providerKeeper .SetConsumerRewardsAllocation (
741
+ providerKeeper .SetConsumerRewardsAllocationByDenom (
746
742
providerCtx ,
747
743
consumerId ,
744
+ ibcDenom ,
748
745
providertypes.ConsumerRewardsAllocation {
749
746
Rewards : sdk .NewDecCoinsFromCoins (rewardsPerChain ... ),
750
747
},
@@ -798,7 +795,9 @@ func (s *CCVTestSuite) TestAllocateTokens() {
798
795
// check that the total expected rewards are transferred to the distribution module account
799
796
800
797
// store the decimal remainders in the consumer reward allocations
801
- allocRemainderPerChain := providerKeeper .GetConsumerRewardsAllocation (providerCtx , s .getFirstBundle ().ConsumerId ).Rewards
798
+ allocRemainderPerChainRes , err := providerKeeper .GetConsumerRewardsAllocationByDenom (providerCtx , s .getFirstBundle ().ConsumerId , ibcDenom )
799
+ s .Require ().NoError (err )
800
+ allocRemainderPerChain := allocRemainderPerChainRes .Rewards
802
801
803
802
// compute the total rewards distributed to the distribution module balance (validator outstanding rewards + community pool tax),
804
803
totalRewardsDistributed := sdk .NewDecCoinsFromCoins (totalRewards ... ).Sub (allocRemainderPerChain .MulDec (math .LegacyNewDec (int64 (consNum ))))
0 commit comments