Skip to content

Commit 23f1d6e

Browse files
committed
fix status field in rpc api (#505)
fixes: #503 Every query was using latest params to compute wheter delegation is active instead of params related to delegation version. This could lead to not returnig certain results in case of updating covenant quorum.
1 parent eff7248 commit 23f1d6e

File tree

3 files changed

+132
-6
lines changed

3 files changed

+132
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
4545
### Bug fixes
4646

4747
- [#486](https://github.com/babylonlabs-io/babylon/pull/486) crypto: blinding base mult of nonce
48+
- [#443](https://github.com/babylonlabs-io/babylon/pull/443) Fix swagger generation for incentive missing `v1` in path
49+
- [#505](https://github.com/babylonlabs-io/babylon/pull/505) Fix `x/btcstaking`
50+
delegation queries
4851

4952
## v1.0.0-rc5
5053

x/btcstaking/keeper/grpc_query.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ func (k Keeper) BTCDelegations(ctx context.Context, req *types.QueryBTCDelegatio
8383
return nil, status.Error(codes.InvalidArgument, "empty request")
8484
}
8585

86-
covenantQuorum := k.GetParams(ctx).CovenantQuorum
87-
8886
// get current BTC height
8987
btcTipHeight := k.btclcKeeper.GetTipInfo(ctx).Height
9088

@@ -94,8 +92,10 @@ func (k Keeper) BTCDelegations(ctx context.Context, req *types.QueryBTCDelegatio
9492
var btcDel types.BTCDelegation
9593
k.cdc.MustUnmarshal(value, &btcDel)
9694

95+
params := k.GetParamsByVersion(ctx, btcDel.ParamsVersion)
96+
9797
// hit if the queried status is ANY or matches the BTC delegation status
98-
status := btcDel.GetStatus(btcTipHeight, covenantQuorum)
98+
status := btcDel.GetStatus(btcTipHeight, params.CovenantQuorum)
9999
if req.Status == types.BTCDelegationStatus_ANY || status == req.Status {
100100
if accumulate {
101101
resp := types.NewBTCDelegationResponse(&btcDel, status)
@@ -136,7 +136,6 @@ func (k Keeper) FinalityProviderDelegations(ctx context.Context, req *types.Quer
136136
btcDelStore := k.btcDelegatorFpStore(sdkCtx, fpPK)
137137

138138
btcHeight := k.btclcKeeper.GetTipInfo(ctx).Height
139-
covenantQuorum := k.GetParams(ctx).CovenantQuorum
140139

141140
btcDels := []*types.BTCDelegatorDelegationsResponse{}
142141
pageRes, err := query.Paginate(btcDelStore, req.Pagination, func(key, value []byte) error {
@@ -149,9 +148,11 @@ func (k Keeper) FinalityProviderDelegations(ctx context.Context, req *types.Quer
149148

150149
btcDelsResp := make([]*types.BTCDelegationResponse, len(curBTCDels.Dels))
151150
for i, btcDel := range curBTCDels.Dels {
151+
params := k.GetParamsByVersion(sdkCtx, btcDel.ParamsVersion)
152+
152153
status := btcDel.GetStatus(
153154
btcHeight,
154-
covenantQuorum,
155+
params.CovenantQuorum,
155156
)
156157
btcDelsResp[i] = types.NewBTCDelegationResponse(btcDel, status)
157158
}
@@ -186,9 +187,11 @@ func (k Keeper) BTCDelegation(ctx context.Context, req *types.QueryBTCDelegation
186187
return nil, types.ErrBTCDelegationNotFound
187188
}
188189

190+
params := k.GetParamsByVersion(ctx, btcDel.ParamsVersion)
191+
189192
status := btcDel.GetStatus(
190193
k.btclcKeeper.GetTipInfo(ctx).Height,
191-
k.GetParams(ctx).CovenantQuorum,
194+
params.CovenantQuorum,
192195
)
193196

194197
return &types.QueryBTCDelegationResponse{

x/btcstaking/keeper/grpc_query_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"math/rand"
77
"testing"
8+
"time"
89

910
sdkmath "cosmossdk.io/math"
1011

@@ -405,3 +406,122 @@ func AddFinalityProvider(t *testing.T, goCtx context.Context, k btcstakingkeeper
405406
})
406407
require.NoError(t, err)
407408
}
409+
410+
func TestCorrectParamsVersionIsUsed(t *testing.T) {
411+
r := rand.New(rand.NewSource(time.Now().UnixNano()))
412+
ctrl := gomock.NewController(t)
413+
defer ctrl.Finish()
414+
415+
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
416+
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
417+
btccKeeper.EXPECT().GetParams(gomock.Any()).Return(btcctypes.DefaultParams()).AnyTimes()
418+
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, nil)
419+
420+
// covenant and slashing addr
421+
covenantSKs, covenantPKs, covenantQuorum := datagen.GenCovenantCommittee(r)
422+
slashingAddress, err := datagen.GenRandomBTCAddress(r, net)
423+
require.NoError(t, err)
424+
slashingPkScript, err := txscript.PayToAddrScript(slashingAddress)
425+
require.NoError(t, err)
426+
slashingChangeLockTime := uint16(101)
427+
428+
// Generate a slashing rate in the range [0.1, 0.50] i.e., 10-50%.
429+
// NOTE - if the rate is higher or lower, it may produce slashing or change outputs
430+
// with value below the dust threshold, causing test failure.
431+
// Our goal is not to test failure due to such extreme cases here;
432+
// this is already covered in FuzzGeneratingValidStakingSlashingTx
433+
slashingRate := sdkmath.LegacyNewDecWithPrec(int64(datagen.RandomInt(r, 41)+10), 2)
434+
435+
fp, err := datagen.GenRandomFinalityProvider(r)
436+
require.NoError(t, err)
437+
AddFinalityProvider(t, ctx, *keeper, fp)
438+
439+
startHeight := uint32(datagen.RandomInt(r, 100)) + 1
440+
btclcKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(&btclctypes.BTCHeaderInfo{Height: startHeight}).AnyTimes()
441+
442+
endHeight := uint32(datagen.RandomInt(r, 1000)) + startHeight + btcctypes.DefaultParams().CheckpointFinalizationTimeout + 1
443+
stakingTime := endHeight - startHeight
444+
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
445+
require.NoError(t, err)
446+
btcDel, err := datagen.GenRandomBTCDelegation(
447+
r,
448+
t,
449+
net,
450+
[]bbn.BIP340PubKey{*fp.BtcPk},
451+
delSK,
452+
covenantSKs,
453+
covenantPKs,
454+
covenantQuorum,
455+
slashingPkScript,
456+
stakingTime, startHeight, endHeight, 10000,
457+
slashingRate,
458+
slashingChangeLockTime,
459+
)
460+
require.NoError(t, err)
461+
462+
err = keeper.AddBTCDelegation(ctx, btcDel)
463+
require.NoError(t, err)
464+
465+
// delegation is active as it have all covenant sigs
466+
txHash := btcDel.MustGetStakingTxHash().String()
467+
delView, err := keeper.BTCDelegation(ctx, &types.QueryBTCDelegationRequest{
468+
StakingTxHashHex: txHash,
469+
})
470+
require.NoError(t, err)
471+
require.NotNil(t, delView)
472+
473+
require.True(t, delView.BtcDelegation.Active)
474+
475+
dp := types.DefaultParams()
476+
477+
// Generate 3 new key pairs and increase keys and quorum size in params, this
478+
// will mean new delegations will require more signatures to be active
479+
_, pk1, err := datagen.GenRandomBTCKeyPair(r)
480+
require.NoError(t, err)
481+
_, pk2, err := datagen.GenRandomBTCKeyPair(r)
482+
require.NoError(t, err)
483+
_, pk3, err := datagen.GenRandomBTCKeyPair(r)
484+
require.NoError(t, err)
485+
486+
// Convert public keys to BIP340 format
487+
bip340pk1 := bbn.NewBIP340PubKeyFromBTCPK(pk1)
488+
bip340pk2 := bbn.NewBIP340PubKeyFromBTCPK(pk2)
489+
bip340pk3 := bbn.NewBIP340PubKeyFromBTCPK(pk3)
490+
491+
dp.BtcActivationHeight = 10
492+
dp.CovenantPks = append(dp.CovenantPks, *bip340pk1, *bip340pk2, *bip340pk3)
493+
dp.CovenantQuorum += 3
494+
495+
err = keeper.SetParams(ctx, dp)
496+
require.NoError(t, err)
497+
498+
// check delegation is still active in every endpoint
499+
delView, err = keeper.BTCDelegation(ctx, &types.QueryBTCDelegationRequest{
500+
StakingTxHashHex: txHash,
501+
})
502+
require.NoError(t, err)
503+
require.NotNil(t, delView)
504+
505+
require.True(t, delView.BtcDelegation.Active)
506+
507+
delegationsView, err := keeper.BTCDelegations(ctx, &types.QueryBTCDelegationsRequest{
508+
Status: types.BTCDelegationStatus_ACTIVE,
509+
})
510+
require.NoError(t, err)
511+
require.NotNil(t, delegationsView)
512+
require.Len(t, delegationsView.BtcDelegations, 1)
513+
514+
pagination := constructRequestWithLimit(r, 10)
515+
// Generate the initial query
516+
req := types.QueryFinalityProviderDelegationsRequest{
517+
FpBtcPkHex: fp.BtcPk.MarshalHex(),
518+
Pagination: pagination,
519+
}
520+
521+
fpView, err := keeper.FinalityProviderDelegations(ctx, &req)
522+
require.NoError(t, err)
523+
require.NotNil(t, fpView)
524+
require.Len(t, fpView.BtcDelegatorDelegations, 1)
525+
require.Len(t, fpView.BtcDelegatorDelegations[0].Dels, 1)
526+
require.True(t, fpView.BtcDelegatorDelegations[0].Dels[0].Active)
527+
}

0 commit comments

Comments
 (0)