Skip to content

Commit

Permalink
chore: add test for FinalityProviderCurrentRewards
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Dec 9, 2024
1 parent a2eeede commit 6fb3549
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions testutil/datagen/incentive.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"
ftypes "github.com/babylonlabs-io/babylon/x/finality/types"
"github.com/babylonlabs-io/babylon/x/incentive/types"
itypes "github.com/babylonlabs-io/babylon/x/incentive/types"
)

Expand Down Expand Up @@ -142,3 +143,10 @@ func GenRandomBTCTimestampingRewardDistInfo(r *rand.Rand) *btcctypes.RewardDistI
}
return btcctypes.NewRewardDistInfo(best, others...)
}

func GenRandomFinalityProviderCurrentRewards(r *rand.Rand) types.FinalityProviderCurrentRewards {
rwd := GenRandomCoins(r)
period := RandomInt(r, 100)
activeSatoshi := RandomMathInt(r, 10000)
return types.NewFinalityProviderCurrentRewards(rwd, period, activeSatoshi)
}
7 changes: 7 additions & 0 deletions x/incentive/keeper/reward_tracker_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func (k Keeper) deleteKeysFromBTCDelegationRewardsTracker(ctx context.Context, f
}
}

// GetBTCDelegationRewardsTracker returns the BTCDelegationRewardsTracker based on the delegation key (fp, del)
// It returns an error in case the key is not found.
func (k Keeper) GetBTCDelegationRewardsTracker(ctx context.Context, fp, del sdk.AccAddress) (types.BTCDelegationRewardsTracker, error) {
key := del.Bytes()
bz := k.storeBTCDelegationRewardsTracker(ctx, fp).Get(key)
Expand All @@ -139,6 +141,7 @@ func (k Keeper) GetBTCDelegationRewardsTracker(ctx context.Context, fp, del sdk.
return value, nil
}

// setBTCDelegationRewardsTracker sets a new structure in the store, it fails and returns an error if the rwd fails to marshal.
func (k Keeper) setBTCDelegationRewardsTracker(ctx context.Context, fp, del sdk.AccAddress, rwd types.BTCDelegationRewardsTracker) error {
key := del.Bytes()
bz, err := rwd.Marshal()
Expand All @@ -151,6 +154,7 @@ func (k Keeper) setBTCDelegationRewardsTracker(ctx context.Context, fp, del sdk.
return nil
}

// setFinalityProviderCurrentRewards sets a new structure in the store, it fails and returns an error if the rwd fails to marshal.
func (k Keeper) setFinalityProviderCurrentRewards(ctx context.Context, fp sdk.AccAddress, rwd types.FinalityProviderCurrentRewards) error {
key := fp.Bytes()
bz, err := rwd.Marshal()
Expand All @@ -162,6 +166,8 @@ func (k Keeper) setFinalityProviderCurrentRewards(ctx context.Context, fp sdk.Ac
return nil
}

// deleteAllFromFinalityProviderRwd deletes all the data related to Finality Provider Rewards
// Historical and current from a fp address key.
func (k Keeper) deleteAllFromFinalityProviderRwd(ctx context.Context, fp sdk.AccAddress) {
st := k.storeFpHistoricalRewards(ctx, fp)

Expand All @@ -180,6 +186,7 @@ func (k Keeper) deleteAllFromFinalityProviderRwd(ctx context.Context, fp sdk.Acc
k.deleteFinalityProviderCurrentRewards(ctx, fp)
}

// deleteFinalityProviderCurrentRewards deletes the current FP reward based on the key received
func (k Keeper) deleteFinalityProviderCurrentRewards(ctx context.Context, fp sdk.AccAddress) {
key := fp.Bytes()
k.storeFpCurrentRewards(ctx).Delete(key)
Expand Down
40 changes: 40 additions & 0 deletions x/incentive/keeper/reward_tracker_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,46 @@ func FuzzCheckBTCDelegationRewardsTracker(f *testing.F) {
})
}

func FuzzCheckFinalityProviderCurrentRewards(f *testing.F) {
datagen.AddRandomSeedsToFuzzer(f, 10)

f.Fuzz(func(t *testing.T, seed int64) {
t.Parallel()
r := rand.New(rand.NewSource(seed))

k, ctx := NewKeeperWithCtx(t)
fp1, fp2 := datagen.GenRandomAddress(), datagen.GenRandomAddress()

_, err := k.GetFinalityProviderCurrentRewards(ctx, fp1)
require.EqualError(t, err, types.ErrFPCurrentRewardsNotFound.Error())

expectedCurrentRwdFp1 := datagen.GenRandomFinalityProviderCurrentRewards(r)
err = k.setFinalityProviderCurrentRewards(ctx, fp1, expectedCurrentRwdFp1)
require.NoError(t, err)

currentRwdFp1, err := k.GetFinalityProviderCurrentRewards(ctx, fp1)
require.NoError(t, err)
require.Equal(t, expectedCurrentRwdFp1.CurrentRewards.String(), currentRwdFp1.CurrentRewards.String())
require.Equal(t, expectedCurrentRwdFp1.TotalActiveSat.String(), currentRwdFp1.TotalActiveSat.String())
require.Equal(t, expectedCurrentRwdFp1.Period, currentRwdFp1.Period)

k.deleteAllFromFinalityProviderRwd(ctx, fp1)
_, err = k.GetFinalityProviderCurrentRewards(ctx, fp1)
require.EqualError(t, err, types.ErrFPCurrentRewardsNotFound.Error())

// sets a new fp
err = k.setFinalityProviderCurrentRewards(ctx, fp2, datagen.GenRandomFinalityProviderCurrentRewards(r))
require.NoError(t, err)

_, err = k.GetFinalityProviderCurrentRewards(ctx, fp2)
require.NoError(t, err)

k.deleteFinalityProviderCurrentRewards(ctx, fp2)
_, err = k.GetFinalityProviderCurrentRewards(ctx, fp2)
require.EqualError(t, err, types.ErrFPCurrentRewardsNotFound.Error())
})
}

func NewKeeperWithCtx(t *testing.T) (Keeper, sdk.Context) {
encConf := appparams.DefaultEncodingConfig()
ctx, kvStore := store.NewStoreWithCtx(t, types.ModuleName)
Expand Down

0 comments on commit 6fb3549

Please sign in to comment.