Skip to content

Commit

Permalink
chore: add one test for specific btc activation height from params
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Dec 2, 2024
1 parent da09b21 commit 47b28f3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
22 changes: 21 additions & 1 deletion testutil/btcstaking-helper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions testutil/datagen/datagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
69 changes: 69 additions & 0 deletions x/btcstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 47b28f3

Please sign in to comment.