From 481e2b2f8eb1925cdfd2a97868afc9d131995155 Mon Sep 17 00:00:00 2001 From: Fangyu Gai Date: Tue, 19 Nov 2024 20:17:42 +0800 Subject: [PATCH] add tests --- x/btccheckpoint/keeper/msg_server_test.go | 30 +++++++++++++++-- x/btcstaking/keeper/msg_server_test.go | 39 +++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/x/btccheckpoint/keeper/msg_server_test.go b/x/btccheckpoint/keeper/msg_server_test.go index 8b234d59d..de6973c56 100644 --- a/x/btccheckpoint/keeper/msg_server_test.go +++ b/x/btccheckpoint/keeper/msg_server_test.go @@ -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 { @@ -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) diff --git a/x/btcstaking/keeper/msg_server_test.go b/x/btcstaking/keeper/msg_server_test.go index 3c0da809d..91a03db2f 100644 --- a/x/btcstaking/keeper/msg_server_test.go +++ b/x/btcstaking/keeper/msg_server_test.go @@ -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" @@ -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)