Skip to content

Commit

Permalink
add fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Aug 6, 2024
1 parent e44fc9e commit d2fb59f
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions x/finality/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"time"

"cosmossdk.io/core/header"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/babylonlabs-io/babylon/testutil/datagen"
keepertest "github.com/babylonlabs-io/babylon/testutil/keeper"
bbn "github.com/babylonlabs-io/babylon/types"
bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/babylonlabs-io/babylon/x/finality/keeper"
"github.com/babylonlabs-io/babylon/x/finality/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)

func setupMsgServer(t testing.TB) (*keeper.Keeper, types.MsgServer, context.Context) {
Expand All @@ -29,7 +30,7 @@ func TestMsgServer(t *testing.T) {
}

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

f.Fuzz(func(t *testing.T, seed int64) {
r := rand.New(rand.NewSource(seed))
Expand All @@ -39,6 +40,8 @@ func FuzzCommitPubRandList(f *testing.F) {
bsKeeper := types.NewMockBTCStakingKeeper(ctrl)
fKeeper, ctx := keepertest.FinalityKeeper(t, bsKeeper, nil)
ms := keeper.NewMsgServerImpl(*fKeeper)
committedEpochNum := datagen.GenRandomEpochNum(r)
bsKeeper.EXPECT().GetEpoch(gomock.Any()).Return(committedEpochNum).AnyTimes()

// create a random finality provider
btcSK, btcPK, err := datagen.GenRandomBTCKeyPair(r)
Expand Down Expand Up @@ -75,6 +78,11 @@ func FuzzCommitPubRandList(f *testing.F) {
// query last public randomness and assert
lastPrCommit := fKeeper.GetLastPubRandCommit(ctx, fpBTCPK)
require.NotNil(t, lastPrCommit)
require.Equal(t, committedEpochNum, lastPrCommit.EpochNum)
committedHeight := datagen.RandomInt(r, int(numPubRand)) + startHeight
commitByHeight, err := fKeeper.GetPubRandCommitForHeight(ctx, fpBTCPK, committedHeight)
require.NoError(t, err)
require.Equal(t, committedEpochNum, commitByHeight.EpochNum)

// Case 4: commit a pubrand list with overlap of the existing pubrand in KVStore and it should fail
overlappedStartHeight := startHeight + numPubRand - 1 - datagen.RandomInt(r, 5)
Expand All @@ -93,7 +101,7 @@ func FuzzCommitPubRandList(f *testing.F) {
}

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

f.Fuzz(func(t *testing.T, seed int64) {
r := rand.New(rand.NewSource(seed))
Expand All @@ -113,6 +121,11 @@ func FuzzAddFinalitySig(f *testing.F) {
fpBTCPKBytes := fpBTCPK.MustMarshal()
require.NoError(t, err)
bsKeeper.EXPECT().HasFinalityProvider(gomock.Any(), gomock.Eq(fpBTCPKBytes)).Return(true).AnyTimes()

// set committed epoch num
committedEpochNum := datagen.GenRandomEpochNum(r) + 1
bsKeeper.EXPECT().GetEpoch(gomock.Any()).Return(committedEpochNum).AnyTimes()

// commit some public randomness
startHeight := uint64(0)
numPubRand := uint64(200)
Expand All @@ -128,6 +141,18 @@ func FuzzAddFinalitySig(f *testing.F) {
msg, err := datagen.NewMsgAddFinalitySig(signer, btcSK, startHeight, blockHeight, randListInfo, blockAppHash)
require.NoError(t, err)

// Case 0: fail if the committed epoch is not finalized
lastFinalizedEpoch := datagen.RandomIntOtherThan(r, 0, int(committedEpochNum))
o1 := bsKeeper.EXPECT().GetLastFinalizedEpoch(gomock.Any()).Return(lastFinalizedEpoch).Times(1)
bsKeeper.EXPECT().GetVotingPower(gomock.Any(), gomock.Eq(fpBTCPKBytes), gomock.Eq(blockHeight)).Return(uint64(1)).Times(1)
bsKeeper.EXPECT().GetFinalityProvider(gomock.Any(), gomock.Eq(fpBTCPKBytes)).Return(fp, nil).Times(1)
_, err = ms.AddFinalitySig(ctx, msg)
require.ErrorIs(t, err, types.ErrPubRandCommitNotBTCTimestamped)

// set the committed epoch finalized for the rest of the cases
lastFinalizedEpoch = datagen.GenRandomEpochNum(r) + committedEpochNum
bsKeeper.EXPECT().GetLastFinalizedEpoch(gomock.Any()).Return(lastFinalizedEpoch).After(o1).AnyTimes()

// Case 1: fail if the finality provider does not have voting power
bsKeeper.EXPECT().GetVotingPower(gomock.Any(), gomock.Eq(fpBTCPKBytes), gomock.Eq(blockHeight)).Return(uint64(0)).Times(1)
bsKeeper.EXPECT().GetFinalityProvider(gomock.Any(), gomock.Eq(fpBTCPKBytes)).Return(fp, nil).Times(1)
Expand Down

0 comments on commit d2fb59f

Please sign in to comment.