Skip to content

Commit

Permalink
chore: add test for add fp rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Dec 10, 2024
1 parent 2e8363f commit e33a176
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions x/incentive/keeper/reward_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ func (k Keeper) initializeBTCDelegation(ctx context.Context, fp, del sdk.AccAddr
return k.setBTCDelegationRewardsTracker(ctx, fp, del, rwd)
}

// AddFinalityProviderRewardsForBtcDelegations gets the current finality provider rewards
// and adds rewards to it, without increasing the finality provider period
// it also does not initiliaze the FP, so it must have been initialized prior
// to adding rewards. In the sense that a FP would first receive active delegations sats
// be properly initialized (creates current and historical reward structures in the store)
// than will start to receive rewards for contributing.
func (k Keeper) AddFinalityProviderRewardsForBtcDelegations(ctx context.Context, fp sdk.AccAddress, rwd sdk.Coins) error {
fpCurrentRwd, err := k.GetFinalityProviderCurrentRewards(ctx, fp)
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions x/incentive/keeper/reward_tracker_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
package keeper

import (
"math/rand"
"testing"

"cosmossdk.io/math"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/testutil/datagen"
"github.com/babylonlabs-io/babylon/x/incentive/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

func FuzzCheckAddFinalityProviderRewardsForBtcDelegations(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)
fp := datagen.GenRandomAddress()

coinsAdded := datagen.GenRandomCoins(r)
// add rewards without initiliaze should error out
err := k.AddFinalityProviderRewardsForBtcDelegations(ctx, fp, coinsAdded)
require.EqualError(t, err, types.ErrFPCurrentRewardsNotFound.Error())

_, err = k.initializeFinalityProvider(ctx, fp)
require.NoError(t, err)
err = k.AddFinalityProviderRewardsForBtcDelegations(ctx, fp, coinsAdded)
require.NoError(t, err)

currentRwd, err := k.GetFinalityProviderCurrentRewards(ctx, fp)
require.NoError(t, err)
require.Equal(t, coinsAdded.String(), currentRwd.CurrentRewards.String())

// adds again the same amounts
err = k.AddFinalityProviderRewardsForBtcDelegations(ctx, fp, coinsAdded)
require.NoError(t, err)

currentRwd, err = k.GetFinalityProviderCurrentRewards(ctx, fp)
require.NoError(t, err)
require.Equal(t, coinsAdded.MulInt(math.NewInt(2)).String(), currentRwd.CurrentRewards.String())
})
}

func TestIncrementFinalityProviderPeriod(t *testing.T) {
k, ctx := NewKeeperWithCtx(t)

Expand Down

0 comments on commit e33a176

Please sign in to comment.