From 47b28f3d2cb2036224002d5ec3c85fe363f715d2 Mon Sep 17 00:00:00 2001 From: RafilxTenfen Date: Mon, 2 Dec 2024 18:33:46 -0300 Subject: [PATCH] chore: add one test for specific btc activation height from params --- testutil/btcstaking-helper/keeper.go | 22 +++++++- testutil/datagen/datagen.go | 4 ++ x/btcstaking/keeper/msg_server_test.go | 69 ++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/testutil/btcstaking-helper/keeper.go b/testutil/btcstaking-helper/keeper.go index ec4aa699b..6c48596d5 100644 --- a/testutil/btcstaking-helper/keeper.go +++ b/testutil/btcstaking-helper/keeper.go @@ -218,6 +218,26 @@ func (h *Helper) CreateDelegation( unbondingTime uint16, usePreApproval bool, addToAllowList bool, +) (string, *types.MsgCreateBTCDelegation, *types.BTCDelegation, *btclctypes.BTCHeaderInfo, *types.InclusionProof, *UnbondingTxInfo, error) { + return h.CreateDelegationWithBtcBlockHeight( + r, delSK, fpPK, changeAddress, stakingValue, + stakingTime, unbondingValue, unbondingTime, + usePreApproval, addToAllowList, 10, + ) +} + +func (h *Helper) CreateDelegationWithBtcBlockHeight( + r *rand.Rand, + delSK *btcec.PrivateKey, + fpPK *btcec.PublicKey, + changeAddress string, + stakingValue int64, + stakingTime uint16, + unbondingValue int64, + unbondingTime uint16, + usePreApproval bool, + addToAllowList bool, + btcBlockHeight uint32, ) (string, *types.MsgCreateBTCDelegation, *types.BTCDelegation, *btclctypes.BTCHeaderInfo, *types.InclusionProof, *UnbondingTxInfo, error) { stakingTimeBlocks := stakingTime bsParams := h.BTCStakingKeeper.GetParams(h.Ctx) @@ -262,7 +282,7 @@ func (h *Helper) CreateDelegation( prevBlock, _ := datagen.GenRandomBtcdBlock(r, 0, nil) btcHeaderWithProof := datagen.CreateBlockWithTransaction(r, &prevBlock.Header, testStakingInfo.StakingTx) btcHeader := btcHeaderWithProof.HeaderBytes - btcHeaderInfo := &btclctypes.BTCHeaderInfo{Header: &btcHeader, Height: 10} + btcHeaderInfo := &btclctypes.BTCHeaderInfo{Header: &btcHeader, Height: btcBlockHeight} serializedStakingTx, err := bbn.SerializeBTCTx(testStakingInfo.StakingTx) h.NoError(err) diff --git a/testutil/datagen/datagen.go b/testutil/datagen/datagen.go index d38fdf18d..fcc096212 100644 --- a/testutil/datagen/datagen.go +++ b/testutil/datagen/datagen.go @@ -24,6 +24,10 @@ func RandomInt(r *rand.Rand, rng int) uint64 { return uint64(r.Intn(rng)) } +func RandomUInt32(r *rand.Rand, rng uint32) uint32 { + return uint32(r.Intn(int(rng))) +} + func RandomIntOtherThan(r *rand.Rand, x int, rng int) uint64 { if rng == 1 && x == 0 { panic("There is no other int") diff --git a/x/btcstaking/keeper/msg_server_test.go b/x/btcstaking/keeper/msg_server_test.go index 3b168fb17..cb8b79bd8 100644 --- a/x/btcstaking/keeper/msg_server_test.go +++ b/x/btcstaking/keeper/msg_server_test.go @@ -242,6 +242,75 @@ func FuzzCreateBTCDelegation(f *testing.F) { }) } +func FuzzCreateBTCDelegationWithParamsFromBtcHeight(f *testing.F) { + datagen.AddRandomSeedsToFuzzer(f, 10) + + f.Fuzz(func(t *testing.T, seed int64) { + r := rand.New(rand.NewSource(time.Now().Unix())) + 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) + ctx, k := h.Ctx, h.BTCStakingKeeper + + versionedParams := k.GetParamsWithVersion(ctx) + currentParams := versionedParams.Params + + maxGapBlocksBetweenParams := datagen.RandomUInt32(r, 100) + 100 + expectedParamsBlockHeight := datagen.RandomUInt32(r, maxGapBlocksBetweenParams) + currentParams.BtcActivationHeight + 1 + expectedParamsVersion := versionedParams.Version + 1 + + currentParams.BtcActivationHeight = expectedParamsBlockHeight + err := k.SetParams(ctx, currentParams) + require.NoError(t, err) + + nextBtcActivationHeight := datagen.RandomUInt32(r, maxGapBlocksBetweenParams) + currentParams.BtcActivationHeight + 1 + currentParams.BtcActivationHeight = nextBtcActivationHeight + err = k.SetParams(ctx, currentParams) + require.NoError(t, err) + + // makes sure that at the BTC block height 300 will use the expected param + p, version, err := k.GetParamsForBtcHeight(ctx, uint64(nextBtcActivationHeight-1)) + h.NoError(err) + require.Equal(t, p.BtcActivationHeight, expectedParamsBlockHeight) + require.Equal(t, version, expectedParamsVersion) + + // creates one BTC delegation with BTC block height between expectedParamsBlockHeight and 500 + changeAddress, err := datagen.GenRandomBTCAddress(r, h.Net) + h.NoError(err) + + // generate and insert new finality provider + _, fpPK, _ := h.CreateFinalityProvider(r) + + btcBlockHeight := datagen.RandomUInt32(r, nextBtcActivationHeight-expectedParamsBlockHeight) + expectedParamsBlockHeight + // generate and insert new BTC delegation + stakingValue := int64(2 * 10e8) + delSK, _, err := datagen.GenRandomBTCKeyPair(r) + h.NoError(err) + _, _, btcDel, _, _, _, err := h.CreateDelegationWithBtcBlockHeight( + r, + delSK, + fpPK, + changeAddress.EncodeAddress(), + stakingValue, + 1000, + 0, + 0, + false, + false, + btcBlockHeight, + ) + h.NoError(err) + require.NotNil(t, btcDel.ParamsVersion, expectedParamsVersion) + }) +} + func TestProperVersionInDelegation(t *testing.T) { r := rand.New(rand.NewSource(time.Now().Unix())) ctrl := gomock.NewController(t)