Skip to content

Commit

Permalink
pr comment
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Nov 22, 2024
1 parent eff728a commit 85689cb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 3 additions & 1 deletion x/btcstaking/keeper/inclusion_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ type DelegationTimeRangeInfo struct {

// VerifyInclusionProofAndGetHeight verifies the inclusion proof of the given staking tx
// and returns the start height and end height
// Note: the `minUnbondingTime` passed here should be from the corresponding params
// of the staking tx
func (k Keeper) VerifyInclusionProofAndGetHeight(
ctx sdk.Context,
stakingTx *btcutil.Tx,
confirmationDepth uint32,
stakingTime uint32,
minUnbondingTime uint32,
inclusionProof *types.ParsedProofOfInclusion,
) (*DelegationTimeRangeInfo, error) {
// Check:
Expand Down Expand Up @@ -59,7 +62,6 @@ func (k Keeper) VerifyInclusionProofAndGetHeight(
}

// ensure staking tx's timelock has more than unbonding BTC blocks left
minUnbondingTime := k.GetParamsWithVersion(ctx).Params.MinUnbondingTimeBlocks
if btcTip.Height+minUnbondingTime >= endHeight {
return nil, types.ErrInvalidStakingTx.
Wrapf("staking tx's timelock has no more than unbonding(=%d) blocks left", minUnbondingTime)
Expand Down
30 changes: 29 additions & 1 deletion x/btcstaking/keeper/inclusion_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func FuzzVerifyInclusionProofAndGetHeight(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 Down Expand Up @@ -77,6 +77,7 @@ func FuzzVerifyInclusionProofAndGetHeight(f *testing.F) {
stakingTx,
confirmationDepth,
stakingTime,
params.MinUnbondingTimeBlocks,
proof,
)

Expand All @@ -95,6 +96,7 @@ func FuzzVerifyInclusionProofAndGetHeight(f *testing.F) {
stakingTx,
confirmationDepth,
stakingTime,
params.MinUnbondingTimeBlocks,
proof,
)

Expand All @@ -112,6 +114,7 @@ func FuzzVerifyInclusionProofAndGetHeight(f *testing.F) {
stakingTx,
confirmationDepth,
stakingTime,
params.MinUnbondingTimeBlocks,
&copyProof,
)

Expand All @@ -131,6 +134,7 @@ func FuzzVerifyInclusionProofAndGetHeight(f *testing.F) {
stakingTx,
confirmationDepth,
stakingTime,
params.MinUnbondingTimeBlocks,
proof,
)

Expand All @@ -150,6 +154,30 @@ func FuzzVerifyInclusionProofAndGetHeight(f *testing.F) {
stakingTx,
confirmationDepth,
stakingTime,
params.MinUnbondingTimeBlocks,
proof,
)

require.ErrorContains(t, err, "staking tx's timelock has no more than unbonding")
})

t.Run("invalid min unbonding time", func(t *testing.T) {
// Set the tip height to be in the range of valid min and max tip height
tipHeight := datagen.RandomInt(r, int(maxValidTipHeight)-int(minValidTipHeight)+1) + uint64(minValidTipHeight)
mockTipHeaderInfo := &btclctypes.BTCHeaderInfo{Height: uint32(tipHeight)}

btclcKeeper.EXPECT().GetHeaderByHash(gomock.Any(), headerHash).Return(inclusionHeader).Times(1)
btclcKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(mockTipHeaderInfo).Times(1)

// an invalid min_unbonding_time should be >= end_height - tip_height
invalidMinUnbondingTime := uint32(datagen.RandomInt(r, 1000)) + inclusionHeight + stakingTime - uint32(tipHeight)

_, err = h.BTCStakingKeeper.VerifyInclusionProofAndGetHeight(
h.Ctx,
stakingTx,
confirmationDepth,
stakingTime,
invalidMinUnbondingTime,
proof,
)

Expand Down
2 changes: 2 additions & 0 deletions x/btcstaking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func (ms msgServer) CreateBTCDelegation(goCtx context.Context, req *types.MsgCre
btcutil.NewTx(parsedMsg.StakingTx.Transaction),
btccParams.BtcConfirmationDepth,
uint32(parsedMsg.StakingTime),
vp.Params.MinUnbondingTimeBlocks,
parsedMsg.StakingTxProofOfInclusion)
if err != nil {
return nil, fmt.Errorf("invalid inclusion proof: %w", err)
Expand Down Expand Up @@ -314,6 +315,7 @@ func (ms msgServer) AddBTCDelegationInclusionProof(
btcutil.NewTx(stakingTx),
btccParams.BtcConfirmationDepth,
btcDel.StakingTime,
minUnbondingTime,
parsedInclusionProof,
)

Expand Down

0 comments on commit 85689cb

Please sign in to comment.