Skip to content

Commit

Permalink
chore: finalized tests for simple store functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Dec 9, 2024
1 parent 6fb3549 commit 64edb30
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
5 changes: 5 additions & 0 deletions testutil/datagen/incentive.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,8 @@ func GenRandomFinalityProviderCurrentRewards(r *rand.Rand) types.FinalityProvide
activeSatoshi := RandomMathInt(r, 10000)
return types.NewFinalityProviderCurrentRewards(rwd, period, activeSatoshi)
}

func GenRandomFinalityProviderHistoricalRewards(r *rand.Rand) types.FinalityProviderHistoricalRewards {
rwd := GenRandomCoins(r)
return types.NewFinalityProviderHistoricalRewards(rwd)
}
6 changes: 3 additions & 3 deletions x/incentive/keeper/reward_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (k Keeper) FpSlashed(ctx context.Context, fp sdk.AccAddress) error {
keysBtcDelRwdTracker := make([][]byte, 0)
if err := k.IterateBTCDelegationRewardsTracker(ctx, fp, func(fp, del sdk.AccAddress) error {
keysBtcDelRwdTracker = append(keysBtcDelRwdTracker, del.Bytes())
return k.CalculateBTCDelegationRewardsAndSend(ctx, fp, del, endedPeriod)
return k.CalculateBTCDelegationRewardsAndSendToGauge(ctx, fp, del, endedPeriod)
}); err != nil {
return err
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func (k Keeper) btcDelegationModifiedWithPreInitDel(
return err
}

if err := k.CalculateBTCDelegationRewardsAndSend(ctx, fp, del, endedPeriod); err != nil {
if err := k.CalculateBTCDelegationRewardsAndSendToGauge(ctx, fp, del, endedPeriod); err != nil {
return err
}

Expand All @@ -101,7 +101,7 @@ func (k Keeper) btcDelegationModifiedWithPreInitDel(
return k.initializeBTCDelegation(ctx, fp, del)
}

func (k Keeper) CalculateBTCDelegationRewardsAndSend(ctx context.Context, fp, del sdk.AccAddress, endPeriod uint64) error {
func (k Keeper) CalculateBTCDelegationRewardsAndSendToGauge(ctx context.Context, fp, del sdk.AccAddress, endPeriod uint64) error {
rewards, err := k.CalculateBTCDelegationRewards(ctx, fp, del, endPeriod)
if err != nil {
if !errors.Is(err, types.ErrBTCDelegationRewardsTrackerNotFound) {
Expand Down
19 changes: 12 additions & 7 deletions x/incentive/keeper/reward_tracker_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// storeBTCDelegatorToFp returns the KVStore of the mapping del => fp
// note: it stores the finality provider as key and sets a one byte as value
// so each BTC delegator address can have multiple finality providers.
// Usefull to iterate over all the pairs (fp,del) by filtering the
// Useful to iterate over all the pairs (fp,del) by filtering the
// delegator address.
// prefix: BTCDelegatorToFPKey
// key: (DelAddr, FpAddr)
Expand Down Expand Up @@ -53,6 +53,7 @@ func (k Keeper) storeFpHistoricalRewards(ctx context.Context, fp sdk.AccAddress)
return prefix.NewStore(st, fp.Bytes())
}

// setBTCDelegatorToFP sets a new delegator to finality provider record.
func (k Keeper) setBTCDelegatorToFP(ctx context.Context, del, fp sdk.AccAddress) {
st := k.storeBTCDelegatorToFp(ctx, del)
st.Set(fp.Bytes(), []byte{0x00})
Expand Down Expand Up @@ -169,18 +170,18 @@ func (k Keeper) setFinalityProviderCurrentRewards(ctx context.Context, fp sdk.Ac
// 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)
stHistoricalRwd := k.storeFpHistoricalRewards(ctx, fp)

keys := make([][]byte, 0)

iter := st.Iterator(nil, nil)
iter := stHistoricalRwd.Iterator(nil, nil)
defer iter.Close()

keys := make([][]byte, 0)
for ; iter.Valid(); iter.Next() {
keys = append(keys, iter.Key())
}

for _, key := range keys {
st.Delete(key)
stHistoricalRwd.Delete(key)
}

k.deleteFinalityProviderCurrentRewards(ctx, fp)
Expand All @@ -192,13 +193,15 @@ func (k Keeper) deleteFinalityProviderCurrentRewards(ctx context.Context, fp sdk
k.storeFpCurrentRewards(ctx).Delete(key)
}

// GetFinalityProviderHistoricalRewards returns the FinalityProviderHistoricalRewards based on the key (fp, period)
// It returns an error if the key is not found inside the store.
func (k Keeper) GetFinalityProviderHistoricalRewards(ctx context.Context, fp sdk.AccAddress, period uint64) (types.FinalityProviderHistoricalRewards, error) {
key := make([]byte, 8)
binary.LittleEndian.PutUint64(key, period)

bz := k.storeFpHistoricalRewards(ctx, fp).Get(key)
if bz == nil {
return types.FinalityProviderHistoricalRewards{}, types.ErrFPCurrentRewardsNotFound
return types.FinalityProviderHistoricalRewards{}, types.ErrFPHistoricalRewardsNotFound
}

var value types.FinalityProviderHistoricalRewards
Expand All @@ -208,6 +211,8 @@ func (k Keeper) GetFinalityProviderHistoricalRewards(ctx context.Context, fp sdk
return value, nil
}

// setFinalityProviderHistoricalRewards sets a new value inside the store, it returns an error
// if the marshal of the `rwd` fails.
func (k Keeper) setFinalityProviderHistoricalRewards(ctx context.Context, fp sdk.AccAddress, period uint64, rwd types.FinalityProviderHistoricalRewards) error {
key := make([]byte, 8)
binary.LittleEndian.PutUint64(key, period)
Expand Down
51 changes: 51 additions & 0 deletions x/incentive/keeper/reward_tracker_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,57 @@ func FuzzCheckFinalityProviderCurrentRewards(f *testing.F) {
})
}

func FuzzCheckFinalityProviderHistoricalRewards(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()

fp1Period1 := datagen.RandomInt(r, 10)
_, err := k.GetFinalityProviderHistoricalRewards(ctx, fp1, fp1Period1)
require.EqualError(t, err, types.ErrFPHistoricalRewardsNotFound.Error())

expectedHistRwdFp1 := datagen.GenRandomFinalityProviderHistoricalRewards(r)
err = k.setFinalityProviderHistoricalRewards(ctx, fp1, fp1Period1, expectedHistRwdFp1)
require.NoError(t, err)

fp1Period1Historical, err := k.GetFinalityProviderHistoricalRewards(ctx, fp1, fp1Period1)
require.NoError(t, err)
require.Equal(t, expectedHistRwdFp1.CumulativeRewardsPerSat.String(), fp1Period1Historical.CumulativeRewardsPerSat.String())

// sets multiple historical for fp2
fp2Period1Historical := datagen.RandomInt(r, 10)
err = k.setFinalityProviderHistoricalRewards(ctx, fp2, fp2Period1Historical, datagen.GenRandomFinalityProviderHistoricalRewards(r))
require.NoError(t, err)
fp2Period2Historical := datagen.RandomInt(r, 10)
err = k.setFinalityProviderHistoricalRewards(ctx, fp2, fp2Period2Historical, datagen.GenRandomFinalityProviderHistoricalRewards(r))
require.NoError(t, err)

// sets a new current fp rwd to check the delete all
err = k.setFinalityProviderCurrentRewards(ctx, fp2, datagen.GenRandomFinalityProviderCurrentRewards(r))
require.NoError(t, err)

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

// deleted all from fp2
k.deleteAllFromFinalityProviderRwd(ctx, fp2)

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

_, err = k.GetFinalityProviderHistoricalRewards(ctx, fp2, fp2Period1Historical)
require.EqualError(t, err, types.ErrFPHistoricalRewardsNotFound.Error())

_, err = k.GetFinalityProviderHistoricalRewards(ctx, fp2, fp2Period2Historical)
require.EqualError(t, err, types.ErrFPHistoricalRewardsNotFound.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 64edb30

Please sign in to comment.