Skip to content

Commit

Permalink
chore: add check for status and expired delegations
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Dec 19, 2024
1 parent 20c1c08 commit 5ed4b1c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions x/btcstaking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func (ms msgServer) AddCovenantSigs(goCtx context.Context, req *types.MsgAddCove
// ensure BTC delegation is still pending, i.e., not unbonded
btcTipHeight := ms.btclcKeeper.GetTipInfo(ctx).Height
status := btcDel.GetStatus(btcTipHeight, params.CovenantQuorum)
if status == types.BTCDelegationStatus_UNBONDED {
if status == types.BTCDelegationStatus_UNBONDED || status == types.BTCDelegationStatus_EXPIRED {
ms.Logger(ctx).Debug("Received covenant signature after the BTC delegation is already unbonded", "covenant pk", req.Pk.MarshalHex())
return nil, types.ErrInvalidCovenantSig.Wrap("the BTC delegation is already unbonded")
}
Expand Down Expand Up @@ -606,7 +606,7 @@ func (ms msgServer) BTCUndelegate(goCtx context.Context, req *types.MsgBTCUndele
bsParams.CovenantQuorum,
)

if btcDelStatus == types.BTCDelegationStatus_UNBONDED {
if btcDelStatus == types.BTCDelegationStatus_UNBONDED || btcDelStatus == types.BTCDelegationStatus_EXPIRED {
return nil, types.ErrInvalidBTCUndelegateReq.Wrap("cannot unbond an unbonded BTC delegation")
}

Expand Down
70 changes: 70 additions & 0 deletions x/btcstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,76 @@ func FuzzBTCUndelegate(f *testing.F) {
})
}

func FuzzBTCUndelegateExpired(f *testing.F) {
datagen.AddRandomSeedsToFuzzer(f, 10)

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
covenantSKs, _ := h.GenAndApplyParams(r)

bsParams := h.BTCStakingKeeper.GetParams(h.Ctx)

changeAddress, err := datagen.GenRandomBTCAddress(r, h.Net)
require.NoError(t, err)

// generate and insert new finality provider
_, fpPK, _ := h.CreateFinalityProvider(r)

// generate and insert new BTC delegation
stakingValue := int64(2 * 10e8)
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)
stakingTxHash, msgCreateBTCDel, actualDel, btcHeaderInfo, inclusionProof, unbondingInfo, err := h.CreateDelegationWithBtcBlockHeight(
r,
delSK,
fpPK,
changeAddress.EncodeAddress(),
stakingValue,
1000,
0,
0,
true,
false,
10,
10,
)
h.NoError(err)

// add covenant signatures to this BTC delegation
h.CreateCovenantSigs(r, covenantSKs, msgCreateBTCDel, actualDel, 10)
// activate the BTC delegation
btcTip := uint32(30)
h.AddInclusionProof(stakingTxHash, btcHeaderInfo, inclusionProof, btcTip)

// ensure the BTC delegation is bonded right now
actualDel, err = h.BTCStakingKeeper.GetBTCDelegation(h.Ctx, stakingTxHash)
h.NoError(err)
status := actualDel.GetStatus(btcTip, bsParams.CovenantQuorum)
require.Equal(t, types.BTCDelegationStatus_ACTIVE, status)

msg := &types.MsgBTCUndelegate{
Signer: datagen.GenRandomAccount().Address,
StakingTxHash: stakingTxHash,
StakeSpendingTx: actualDel.BtcUndelegation.UnbondingTx,
StakeSpendingTxInclusionProof: unbondingInfo.UnbondingTxInclusionProof,
}

// expires the delegation
h.BTCLightClientKeeper.EXPECT().GetTipInfo(gomock.Eq(h.Ctx)).Return(&btclctypes.BTCHeaderInfo{Height: 2000}).AnyTimes()
_, err = h.MsgServer.BTCUndelegate(h.Ctx, msg)
require.EqualError(t, err, types.ErrInvalidBTCUndelegateReq.Wrap("cannot unbond an unbonded BTC delegation").Error())
})
}

func FuzzSelectiveSlashing(f *testing.F) {
datagen.AddRandomSeedsToFuzzer(f, 10)

Expand Down

0 comments on commit 5ed4b1c

Please sign in to comment.