Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Nov 19, 2024
1 parent 45639bb commit 481e2b2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
30 changes: 27 additions & 3 deletions x/btccheckpoint/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"testing"
"time"

"github.com/btcsuite/btcd/chaincfg"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/stretchr/testify/require"

dg "github.com/babylonlabs-io/babylon/testutil/datagen"
keepertest "github.com/babylonlabs-io/babylon/testutil/keeper"
bbn "github.com/babylonlabs-io/babylon/types"
bkeeper "github.com/babylonlabs-io/babylon/x/btccheckpoint/keeper"
btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"
"github.com/btcsuite/btcd/chaincfg"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

type TestKeepers struct {
Expand Down Expand Up @@ -78,6 +81,27 @@ func (k *TestKeepers) onTipChange() {
k.BTCCheckpoint.OnTipChange(k.SdkCtx)
}

func TestUpdateParams(t *testing.T) {
tk := InitTestKeepers(t)

// Try to update params with a different minUnbondingTime
msg := &btcctypes.MsgUpdateParams{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Params: btcctypes.Params{
CheckpointFinalizationTimeout: btcctypes.DefaultParams().CheckpointFinalizationTimeout + 1,
},
}

_, err := tk.MsgSrv.UpdateParams(tk.Ctx, msg)
require.ErrorIs(t, err, govtypes.ErrInvalidProposalMsg,
"should not be able to change CheckpointFinalizationTimeout parameter")

// Verify params were not changed
params := tk.BTCCheckpoint.GetParams(tk.SdkCtx)
require.Equal(t, btcctypes.DefaultParams().CheckpointFinalizationTimeout, params.CheckpointFinalizationTimeout,
"minUnbondingTime should remain unchanged")
}

func TestRejectDuplicatedSubmission(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().Unix()))
epoch := uint64(1)
Expand Down
39 changes: 39 additions & 0 deletions x/btcstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
Expand All @@ -27,6 +29,43 @@ import (
"github.com/babylonlabs-io/babylon/x/btcstaking/types"
)

func FuzzMsgServer_UpdateParams(f *testing.F) {
datagen.AddRandomSeedsToFuzzer(f, 500)

f.Fuzz(func(t *testing.T, seed int64) {
r := rand.New(rand.NewSource(seed))
ctrl := gomock.NewController(t)
defer ctrl.Finish()

// mock BTC light client and BTC checkpoint modules
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
h := testutil.NewHelper(t, btclcKeeper, btccKeeper)

// set all parameters
h.GenAndApplyParams(r)

params := h.BTCStakingKeeper.GetParams(h.Ctx)
ckptFinalizationTimeout := btccKeeper.GetParams(h.Ctx).CheckpointFinalizationTimeout
params.MinUnbondingTimeBlocks = uint32(r.Intn(int(ckptFinalizationTimeout))) + 1

// Try to update params with minUnbondingTime less than or equal to checkpointFinalizationTimeout
msg := &types.MsgUpdateParams{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Params: params,
}

_, err := h.MsgServer.UpdateParams(h.Ctx, msg)
require.ErrorIs(t, err, govtypes.ErrInvalidProposalMsg,
"should not set minUnbondingTime to be less than checkpointFinalizationTimeout")

// Try to update params with minUnbondingTime larger than checkpointFinalizationTimeout
msg.Params.MinUnbondingTimeBlocks = uint32(r.Intn(1000)) + ckptFinalizationTimeout + 1
_, err = h.MsgServer.UpdateParams(h.Ctx, msg)
require.NoError(t, err)
})
}

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

Expand Down

0 comments on commit 481e2b2

Please sign in to comment.