Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
GAtom22 committed Feb 28, 2025
1 parent f84e6db commit 6ebe6d2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion x/incentive/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (k Keeper) BTCStakingGauge(goCtx context.Context, req *types.QueryBTCStakin
return &types.QueryBTCStakingGaugeResponse{Gauge: convertGaugeToBTCStakingResponse(*gauge)}, nil
}

// DelegationRewards implements types.QueryServer.
// DelegationRewards returns the current rewards for the specified finality provider and delegator
func (k Keeper) DelegationRewards(ctx context.Context, req *types.QueryDelegationRewardsRequest) (*types.QueryDelegationRewardsResponse, error) {
// try to cast address
fpAddr, err := sdk.AccAddressFromBech32(req.FinalityProviderAddress)
Expand Down
65 changes: 65 additions & 0 deletions x/incentive/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"math/rand"
"testing"

"cosmossdk.io/collections"
"cosmossdk.io/math"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
"github.com/babylonlabs-io/babylon/testutil/datagen"
testkeeper "github.com/babylonlabs-io/babylon/testutil/keeper"
"github.com/babylonlabs-io/babylon/x/incentive/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -86,3 +91,63 @@ func FuzzBTCStakingGaugeQuery(f *testing.F) {
}
})
}

func FuzzDelegationRewardsQuery(f *testing.F) {
datagen.AddRandomSeedsToFuzzer(f, 10)
f.Fuzz(func(t *testing.T, seed int64) {
t.Parallel()
var (
r = rand.New(rand.NewSource(seed))
storeKey = storetypes.NewKVStoreKey(types.StoreKey)
k, ctx = testkeeper.IncentiveKeeperWithStoreKey(t, storeKey, nil, nil, nil)
fp, del = datagen.GenRandomAddress(), datagen.GenRandomAddress()
storeService = runtime.NewKVStoreService(storeKey)
store = storeService.OpenKVStore(ctx)
storeAdaptor = runtime.KVStoreAdapter(store)
btcRwd = datagen.GenRandomBTCDelegationRewardsTracker(r)
)

// Setup a BTCDelegationRewardsTracker for the delegator
require.NoError(t, k.BTCDelegationRewardsTracker.Set(ctx, collections.Join(fp.Bytes(), del.Bytes()), btcRwd))
st := prefix.NewStore(storeAdaptor, types.BTCDelegatorToFPKey)
delStore := prefix.NewStore(st, del.Bytes())
delStore.Set(fp.Bytes(), []byte{0x00})

// Setup FP current rewards with the corresponding period
// and same TotalActiveSat than the BTCDelegationRewardsTracker
fpCurrentRwd := datagen.GenRandomFinalityProviderCurrentRewards(r)
fpCurrentRwd.Period = btcRwd.StartPeriodCumulativeReward + 2
fpCurrentRwd.TotalActiveSat = btcRwd.TotalActiveSat
err := k.FinalityProviderCurrentRewards.Set(ctx, fp.Bytes(), fpCurrentRwd)
require.NoError(t, err)

// set start historical rewards corresponding to btcRwd.StartPeriodCumulativeReward
amtRwdInHistStart := fpCurrentRwd.CurrentRewards.MulInt(types.DecimalAccumulatedRewards).QuoInt(math.NewInt(2))
startHist := types.NewFinalityProviderHistoricalRewards(amtRwdInHistStart)
err = k.FinalityProviderHistoricalRewards.Set(ctx, collections.Join(fp.Bytes(), btcRwd.StartPeriodCumulativeReward), startHist)
require.NoError(t, err)

// set end period historical rewards
// end period for calculation is fpCurrentRwd.Period-1
amtRwdInHistEnd := amtRwdInHistStart.Add(fpCurrentRwd.CurrentRewards.MulInt(types.DecimalAccumulatedRewards).QuoInt(fpCurrentRwd.TotalActiveSat)...)
endHist := types.NewFinalityProviderHistoricalRewards(amtRwdInHistEnd)
err = k.FinalityProviderHistoricalRewards.Set(ctx, collections.Join(fp.Bytes(), fpCurrentRwd.Period-1), endHist)
require.NoError(t, err)

// Calculate expected rewards
expectedRwd := endHist.CumulativeRewardsPerSat.Sub(startHist.CumulativeRewardsPerSat...)
expectedRwd = expectedRwd.MulInt(btcRwd.TotalActiveSat.Add(fpCurrentRwd.TotalActiveSat))
expectedRwd = expectedRwd.QuoInt(types.DecimalAccumulatedRewards)

// Call the DelegationRewards query
res, err := k.DelegationRewards(
ctx,
&types.QueryDelegationRewardsRequest{
FinalityProviderAddress: fp.String(),
DelegatorAddress: del.String(),
},
)
require.NoError(t, err)
require.Equal(t, expectedRwd, res.Rewards)
})
}

0 comments on commit 6ebe6d2

Please sign in to comment.