Skip to content

Commit

Permalink
adds fuzz test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Nov 29, 2024
1 parent d53be52 commit 06308cb
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions x/incentive/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,51 @@ func FuzzWithdrawReward(f *testing.F) {
require.True(t, newRg.IsFullyWithdrawn())
})
}

func FuzzSetWithdrawAddr(f *testing.F) {
datagen.AddRandomSeedsToFuzzer(f, 10)
f.Fuzz(func(t *testing.T, seed int64) {
r := rand.New(rand.NewSource(seed))

ctrl := gomock.NewController(t)
defer ctrl.Finish()

// mock bank keeper
bk := types.NewMockBankKeeper(ctrl)

ik, ctx := testkeeper.IncentiveKeeper(t, bk, nil, nil)
ms := keeper.NewMsgServerImpl(*ik)

// generate and set a random reward gauge with a random set of withdrawable coins
rg := datagen.GenRandomRewardGauge(r)
rg.WithdrawnCoins = datagen.GenRandomWithdrawnCoins(r, rg.Coins)
sType := datagen.GenRandomStakeholderType(r)
sAddr := datagen.GenRandomAccount().GetAddress()
withdrawalAddr := datagen.GenRandomAccount().GetAddress()

ik.SetRewardGauge(ctx, sType, sAddr, rg)

// mock transfer of withdrawable coins
withdrawableCoins := rg.GetWithdrawableCoins()
bk.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), gomock.Eq(types.ModuleName), gomock.Eq(withdrawalAddr), gomock.Eq(withdrawableCoins)).Times(1)

_, err := ms.SetWithdrawAddress(ctx, &types.MsgSetWithdrawAddress{
DelegatorAddress: sAddr.String(),
WithdrawAddress: withdrawalAddr.String(),
})
require.NoError(t, err)

// invoke withdraw and assert consistency
resp, err := ms.WithdrawReward(ctx, &types.MsgWithdrawReward{
Type: sType.String(),
Address: sAddr.String(),
})
require.NoError(t, err)
require.Equal(t, withdrawableCoins, resp.Coins)

// ensure reward gauge is now empty
newRg := ik.GetRewardGauge(ctx, sType, sAddr)
require.NotNil(t, newRg)
require.True(t, newRg.IsFullyWithdrawn())
})
}

0 comments on commit 06308cb

Please sign in to comment.