-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hotfix: Invalid minUnbondingTime for verifying inclusion proof #289
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/btcsuite/btcd/btcutil" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
|
||
testutil "github.com/babylonlabs-io/babylon/testutil/btcstaking-helper" | ||
"github.com/babylonlabs-io/babylon/testutil/datagen" | ||
bbntypes "github.com/babylonlabs-io/babylon/types" | ||
btclctypes "github.com/babylonlabs-io/babylon/x/btclightclient/types" | ||
"github.com/babylonlabs-io/babylon/x/btcstaking/types" | ||
) | ||
|
||
func FuzzVerifyInclusionProofAndGetHeight(f *testing.F) { | ||
datagen.AddRandomSeedsToFuzzer(f, 100) | ||
|
||
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) | ||
|
||
// generate dummy staking tx data | ||
msgTx := datagen.CreateDummyTx() | ||
stakingTx := btcutil.NewTx(msgTx) | ||
confirmationDepth := uint32(6) | ||
stakingTime := uint32(1000) | ||
|
||
params := h.BTCStakingKeeper.GetParams(h.Ctx) | ||
|
||
// generate common merkle proof and inclusion header | ||
prevBlock, _ := datagen.GenRandomBtcdBlock(r, 0, nil) | ||
btcHeaderWithProof := datagen.CreateBlockWithTransaction(r, &prevBlock.Header, msgTx) | ||
headerHash := btcHeaderWithProof.HeaderBytes.Hash() | ||
headerBytes, err := bbntypes.NewBTCHeaderBytesFromBytes(btcHeaderWithProof.HeaderBytes) | ||
require.NoError(t, err) | ||
inclusionHeight := uint32(datagen.RandomInt(r, 1000) + 1) | ||
inclusionHeader := &btclctypes.BTCHeaderInfo{ | ||
Header: &headerBytes, | ||
Height: inclusionHeight, | ||
} | ||
|
||
// create inclusion proof | ||
proof := &types.ParsedProofOfInclusion{ | ||
HeaderHash: headerHash, | ||
Proof: btcHeaderWithProof.SpvProof.MerkleNodes, | ||
Index: btcHeaderWithProof.SpvProof.BtcTransactionIndex, | ||
} | ||
|
||
// indicates the staking tx has room for unbonding | ||
maxValidTipHeight := inclusionHeight + stakingTime - params.MinUnbondingTimeBlocks - 1 | ||
// indicates the staking tx is k-deep | ||
minValidTipHeight := inclusionHeight + confirmationDepth | ||
|
||
t.Run("successful verification", 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) | ||
|
||
// Verify inclusion proof | ||
timeRange, err := h.BTCStakingKeeper.VerifyInclusionProofAndGetHeight( | ||
h.Ctx, | ||
stakingTx, | ||
confirmationDepth, | ||
stakingTime, | ||
params.MinUnbondingTimeBlocks, | ||
proof, | ||
) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, inclusionHeader.Height, timeRange.StartHeight) | ||
require.Equal(t, inclusionHeader.Height+stakingTime, timeRange.EndHeight) | ||
}) | ||
|
||
t.Run("nil inclusion header", func(t *testing.T) { | ||
// set the returned inclusion header as nil | ||
btclcKeeper.EXPECT().GetHeaderByHash(gomock.Any(), headerHash).Return(nil).Times(1) | ||
|
||
// Verify inclusion proof | ||
_, err := h.BTCStakingKeeper.VerifyInclusionProofAndGetHeight( | ||
h.Ctx, | ||
stakingTx, | ||
confirmationDepth, | ||
stakingTime, | ||
params.MinUnbondingTimeBlocks, | ||
proof, | ||
) | ||
|
||
require.ErrorContains(t, err, "header that includes the staking tx is not found") | ||
}) | ||
|
||
t.Run("invalid proof", func(t *testing.T) { | ||
btclcKeeper.EXPECT().GetHeaderByHash(gomock.Any(), headerHash).Return(inclusionHeader).Times(1) | ||
|
||
copyProof := *proof | ||
// make the proof invalid by setting the index to a different value | ||
copyProof.Index = proof.Index + 1 | ||
_, err = h.BTCStakingKeeper.VerifyInclusionProofAndGetHeight( | ||
h.Ctx, | ||
stakingTx, | ||
confirmationDepth, | ||
stakingTime, | ||
params.MinUnbondingTimeBlocks, | ||
©Proof, | ||
) | ||
|
||
require.ErrorContains(t, err, "not included in the Bitcoin chain") | ||
}) | ||
|
||
t.Run("insufficient confirmation depth", func(t *testing.T) { | ||
tipHeight := inclusionHeight + uint32(datagen.RandomInt(r, int(confirmationDepth))) | ||
mockTipHeaderInfo := &btclctypes.BTCHeaderInfo{Height: tipHeight} | ||
|
||
btclcKeeper.EXPECT().GetHeaderByHash(gomock.Any(), headerHash).Return(inclusionHeader).Times(1) | ||
btclcKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(mockTipHeaderInfo).Times(1) | ||
|
||
// Verify inclusion proof | ||
_, err = h.BTCStakingKeeper.VerifyInclusionProofAndGetHeight( | ||
h.Ctx, | ||
stakingTx, | ||
confirmationDepth, | ||
stakingTime, | ||
params.MinUnbondingTimeBlocks, | ||
proof, | ||
) | ||
|
||
require.ErrorContains(t, err, "not k-deep") | ||
}) | ||
|
||
t.Run("insufficient unbonding time", func(t *testing.T) { | ||
tipHeight := datagen.RandomInt(r, 1000) + uint64(maxValidTipHeight) + 1 | ||
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) | ||
|
||
// Verify inclusion proof | ||
_, err = h.BTCStakingKeeper.VerifyInclusionProofAndGetHeight( | ||
h.Ctx, | ||
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, | ||
) | ||
|
||
require.ErrorContains(t, err, "staking tx's timelock has no more than unbonding") | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you double check the tests ?
I have changed this back locally to
vp.Params.MinStakingTimeBlocks
and run the tests, and they did not catch this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh that's because the tests are for
VerifyInclusionProofAndGetHeight
. Seems we need to have a special case for testingCreateBTCDelegation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In d729190 I added a happy case to
TestDoNotAllowDelegationWithoutFinalityProvider
so that if you changevp.Params.MinStakingTimeBlocks
, the test won't pass. But I don't think it's comprehensive enough. We can create an issue to add comprehensive test forCreateBTCDelegation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also noticed that we don't have a validation rule to ensure
minStakingTime > minUnbondingTime
in params. I think this rule is important. Otherwise, the verification will never pass if the user choose to stake withminStakingTime
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, lets create issue for this 👍