Skip to content

Commit

Permalink
add test for incentive
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Dec 19, 2024
1 parent 6d264e2 commit a641a63
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions x/incentive/keeper/btc_staking_gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,45 @@ func FuzzRewardBTCStaking(f *testing.F) {
dc, err := datagen.GenRandomVotingPowerDistCache(r, 100)
require.NoError(t, err)

// create voter map from a random subset of finality providers
voterMap := make(map[string]struct{})
for i, fp := range dc.FinalityProviders {
// randomly decide if this FP voted (skip some FPs)
if i < int(dc.NumActiveFps) && datagen.OneInN(r, 2) {
voterMap[fp.BtcPk.MarshalHex()] = struct{}{}
}
}

// expected values
distributedCoins := sdk.NewCoins()
fpRewardMap := map[string]sdk.Coins{} // key: address, value: reward
btcDelRewardMap := map[string]sdk.Coins{} // key: address, value: reward

// calculate expected rewards only for FPs who voted
for _, fp := range dc.FinalityProviders {
// skip if not active or didn't vote
if _, ok := voterMap[fp.BtcPk.MarshalHex()]; !ok {
continue
}

fpPortion := dc.GetFinalityProviderPortion(fp)
coinsForFpsAndDels := gauge.GetCoinsPortion(fpPortion)
coinsForCommission := types.GetCoinsPortion(coinsForFpsAndDels, *fp.Commission)
if coinsForCommission.IsAllPositive() {
fpRewardMap[fp.GetAddress().String()] = coinsForCommission
distributedCoins.Add(coinsForCommission...)
distributedCoins = distributedCoins.Add(coinsForCommission...)
}
coinsForBTCDels := coinsForFpsAndDels.Sub(coinsForCommission...)
for _, btcDel := range fp.BtcDels {
btcDelPortion := fp.GetBTCDelPortion(btcDel)
coinsForDel := types.GetCoinsPortion(coinsForBTCDels, btcDelPortion)
if coinsForDel.IsAllPositive() {
btcDelRewardMap[btcDel.GetAddress().String()] = coinsForDel
distributedCoins.Add(coinsForDel...)
distributedCoins = distributedCoins.Add(coinsForDel...)
}
}
}

// create voter map from the voting power cache
voterMap := make(map[string]struct{})
for _, fp := range dc.FinalityProviders {
voterMap[fp.BtcPk.MarshalHex()] = struct{}{}
}

// distribute rewards in the gauge to finality providers/delegations
keeper.RewardBTCStaking(ctx, height, dc, voterMap)

Expand All @@ -88,5 +97,25 @@ func FuzzRewardBTCStaking(f *testing.F) {

// assert distributedCoins is a subset of coins in gauge
require.True(t, gauge.Coins.IsAllGTE(distributedCoins))

// verify that FPs who didn't vote got no rewards
for _, fp := range dc.FinalityProviders {
if _, voted := voterMap[fp.BtcPk.MarshalHex()]; !voted {
rg := keeper.GetRewardGauge(ctx, types.FinalityProviderType, fp.GetAddress())
if rg != nil {
require.True(t, rg.Coins.IsZero(),
"non-voting FP %s should not receive rewards", fp.GetAddress())
}

// their delegators should also not receive rewards
for _, btcDel := range fp.BtcDels {
rg := keeper.GetRewardGauge(ctx, types.BTCDelegationType, btcDel.GetAddress())
if rg != nil {
require.True(t, rg.Coins.IsZero(),
"delegator %s of non-voting FP should not receive rewards", btcDel.GetAddress())
}
}
}
}
})
}

0 comments on commit a641a63

Please sign in to comment.