Skip to content

Commit

Permalink
fix: BTCDelgationResponse missing staking_time (#197)
Browse files Browse the repository at this point in the history
In pre-approval flow, the delegation has zero value for both
`startHeight` and `endHeight`. Therefore, the staking time should not be
inferred from `endHeight - startHeight`. Instead, it should be stored
seprately. This PR adds `stakingTime` to `BTCDelegationResponse`
  • Loading branch information
gitferry authored Oct 15, 2024
1 parent c4f1ff0 commit 6f1808b
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 191 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Bug fixes

* [#197](https://github.com/babylonlabs-io/babylon/pull/197) Fix `BTCDelgationResponse` missing `staking_time`
* [#193](https://github.com/babylonlabs-io/babylon/pull/193) Fix witness construction of slashing tx
* [#154](https://github.com/babylonlabs-io/babylon/pull/154) Fix "edit-finality-provider" cmd argument index
* [#186](https://github.com/babylonlabs-io/babylon/pull/186) Do not panic on `nil`
Expand Down
18 changes: 10 additions & 8 deletions cmd/babylond/cmd/genhelpers/set_btc_delegations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
"testing"

sdkmath "cosmossdk.io/math"
"github.com/babylonlabs-io/babylon/cmd/babylond/cmd/genhelpers"
"github.com/babylonlabs-io/babylon/testutil/datagen"
"github.com/babylonlabs-io/babylon/testutil/helper"
bbn "github.com/babylonlabs-io/babylon/types"
btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"
btcstktypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/cosmos/cosmos-sdk/client"
genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/stretchr/testify/require"

"github.com/babylonlabs-io/babylon/cmd/babylond/cmd/genhelpers"
"github.com/babylonlabs-io/babylon/testutil/datagen"
"github.com/babylonlabs-io/babylon/testutil/helper"
bbn "github.com/babylonlabs-io/babylon/types"
btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"
btcstktypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
)

func FuzzCmdSetBtcDels(f *testing.F) {
Expand Down Expand Up @@ -69,6 +70,7 @@ func FuzzCmdSetBtcDels(f *testing.F) {

startHeight := uint32(datagen.RandomInt(r, 100)) + 1
endHeight := uint32(datagen.RandomInt(r, 1000)) + startHeight + btcctypes.DefaultParams().CheckpointFinalizationTimeout + 1
stakingTime := endHeight - startHeight
slashingRate := sdkmath.LegacyNewDecWithPrec(int64(datagen.RandomInt(r, 41)+10), 2)
slashingChangeLockTime := uint16(101)

Expand All @@ -86,7 +88,7 @@ func FuzzCmdSetBtcDels(f *testing.F) {
covenantPKs,
covenantQuorum,
slashingPkScript,
startHeight, endHeight, 10000,
stakingTime, startHeight, endHeight, 10000,
slashingRate,
slashingChangeLockTime,
)
Expand Down Expand Up @@ -155,7 +157,7 @@ func FuzzCmdSetBtcDels(f *testing.F) {
covenantPKs,
covenantQuorum,
slashingPkScript,
startHeight, endHeight, 10000,
stakingTime, startHeight, endHeight, 10000,
slashingRate,
slashingChangeLockTime,
)
Expand Down
28 changes: 15 additions & 13 deletions proto/babylon/btcstaking/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -251,40 +251,42 @@ message BTCDelegationResponse {
// fp_btc_pk_list is the list of BIP-340 PKs of the finality providers that
// this BTC delegation delegates to
repeated bytes fp_btc_pk_list = 3 [ (gogoproto.customtype) = "github.com/babylonlabs-io/babylon/types.BIP340PubKey" ];
// staking_time is the number of blocks for which the delegation is locked on BTC chain
uint32 staking_time = 4;
// start_height is the start BTC height of the BTC delegation
// it is the start BTC height of the timelock
uint32 start_height = 4;
uint32 start_height = 5;
// end_height is the end height of the BTC delegation
// it is the end BTC height of the timelock - w
uint32 end_height = 5;
uint32 end_height = 6;
// total_sat is the total amount of BTC stakes in this delegation
// quantified in satoshi
uint64 total_sat = 6;
uint64 total_sat = 7;
// staking_tx_hex is the hex string of staking tx
string staking_tx_hex = 7;
string staking_tx_hex = 8;
// slashing_tx_hex is the hex string of slashing tx
string slashing_tx_hex = 8;
string slashing_tx_hex = 9;
// delegator_slash_sig_hex is the signature on the slashing tx
// by the delegator (i.e., SK corresponding to btc_pk) as string hex.
// It will be a part of the witness for the staking tx output.
string delegator_slash_sig_hex = 9;
string delegator_slash_sig_hex = 10;
// covenant_sigs is a list of adaptor signatures on the slashing tx
// by each covenant member
// It will be a part of the witness for the staking tx output.
repeated CovenantAdaptorSignatures covenant_sigs = 10;
repeated CovenantAdaptorSignatures covenant_sigs = 11;
// staking_output_idx is the index of the staking output in the staking tx
uint32 staking_output_idx = 11;
uint32 staking_output_idx = 12;
// whether this delegation is active
bool active = 12;
bool active = 13;
// descriptive status of current delegation.
string status_desc = 13;
string status_desc = 14;
// unbonding_time used in unbonding output timelock path and in slashing transactions
// change outputs
uint32 unbonding_time = 14;
uint32 unbonding_time = 15;
// undelegation_response is the undelegation info of this delegation.
BTCUndelegationResponse undelegation_response = 15;
BTCUndelegationResponse undelegation_response = 16;
// params version used to validate delegation
uint32 params_version = 16;
uint32 params_version = 17;
}

// BTCUndelegationResponse provides all necessary info about the undeleagation
Expand Down
6 changes: 2 additions & 4 deletions testutil/datagen/btcstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func GenRandomBTCDelegation(
covenantPks []*btcec.PublicKey,
covenantQuorum uint32,
slashingPkScript []byte,
startHeight, endHeight uint32,
stakingTime, startHeight, endHeight uint32,
totalSat uint64,
slashingRate sdkmath.LegacyDec,
slashingChangeLockTime uint16,
Expand All @@ -104,8 +104,6 @@ func GenRandomBTCDelegation(
}
staker := GenRandomAccount()

stakingTime := uint16(endHeight - startHeight)

// staking/slashing tx
stakingSlashingInfo := GenBTCStakingSlashingInfo(
r,
Expand All @@ -115,7 +113,7 @@ func GenRandomBTCDelegation(
fpPKs,
covenantPks,
covenantQuorum,
stakingTime,
uint16(stakingTime),
int64(totalSat),
slashingPkScript,
slashingRate,
Expand Down
15 changes: 12 additions & 3 deletions x/btcstaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,17 @@ func FuzzPendingBTCDelegations(f *testing.F) {
btclcKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(&btclctypes.BTCHeaderInfo{Height: startHeight}).AnyTimes()

endHeight := uint32(datagen.RandomInt(r, 1000)) + startHeight + btcctypes.DefaultParams().CheckpointFinalizationTimeout + 1
stakingTime := endHeight - startHeight
numBTCDels := datagen.RandomInt(r, 10) + 1
pendingBtcDelsMap := make(map[string]*types.BTCDelegation)
for _, fp := range fps {
for j := uint64(0); j < numBTCDels; j++ {
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
require.NoError(t, err)
// 0.5 chance that the delegation is created via pre-approval flow
if r.Intn(2) == 0 {
startHeight, endHeight = 0, 0
}
btcDel, err := datagen.GenRandomBTCDelegation(
r,
t,
Expand All @@ -223,7 +228,7 @@ func FuzzPendingBTCDelegations(f *testing.F) {
covenantPKs,
covenantQuorum,
slashingPkScript,
startHeight, endHeight, 10000,
stakingTime, startHeight, endHeight, 10000,
slashingRate,
slashingChangeLockTime,
)
Expand Down Expand Up @@ -266,6 +271,7 @@ func FuzzPendingBTCDelegations(f *testing.F) {
for _, btcDel := range resp.BtcDelegations {
_, ok := pendingBtcDelsMap[btcDel.BtcPk.MarshalHex()]
require.True(t, ok)
require.Equal(t, stakingTime, btcDel.StakingTime)
}
// Construct the next page request
pagination.Key = resp.Pagination.NextKey
Expand Down Expand Up @@ -420,6 +426,8 @@ func FuzzActiveFinalityProvidersAtHeight(f *testing.F) {
for j := uint64(0); j < numBTCDels; j++ {
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
require.NoError(t, err)
startHeight, endHeight := uint32(1), uint32(1000)
stakingTime := endHeight - startHeight
btcDel, err := datagen.GenRandomBTCDelegation(
r,
t,
Expand All @@ -430,7 +438,7 @@ func FuzzActiveFinalityProvidersAtHeight(f *testing.F) {
covenantPKs,
covenantQuorum,
slashingPkScript,
1, 1000, 10000,
stakingTime, 1, 1000, 10000,
slashingRate,
slashingChangeLockTime,
)
Expand Down Expand Up @@ -524,6 +532,7 @@ func FuzzFinalityProviderDelegations(f *testing.F) {

startHeight := uint32(datagen.RandomInt(r, 100)) + 1
endHeight := uint32(datagen.RandomInt(r, 1000)) + startHeight + btcctypes.DefaultParams().CheckpointFinalizationTimeout + 1
stakingTime := endHeight - startHeight
btclcKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(&btclctypes.BTCHeaderInfo{Height: startHeight}).AnyTimes()
// Generate a random number of BTC delegations under this finality provider
numBTCDels := datagen.RandomInt(r, 10) + 1
Expand All @@ -541,7 +550,7 @@ func FuzzFinalityProviderDelegations(f *testing.F) {
covenantPKs,
covenantQuorum,
slashingPkScript,
startHeight, endHeight, 10000,
stakingTime, startHeight, endHeight, 10000,
slashingRate,
slashingChangeLockTime,
)
Expand Down
3 changes: 3 additions & 0 deletions x/btcstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,8 @@ func createNDelegationsForFinalityProvider(

slashingRate := sdkmath.LegacyNewDecWithPrec(int64(datagen.RandomInt(r, 41)+10), 2)

startHeight, endHeight := 1, math.MaxUint16
stakingTime := uint32(endHeight) - uint32(startHeight)
del, err := datagen.GenRandomBTCDelegation(
r,
t,
Expand All @@ -926,6 +928,7 @@ func createNDelegationsForFinalityProvider(
covenantPks,
quorum,
slashingPkScript,
stakingTime,
1,
1+(math.MaxUint16-1),
uint64(stakingValue),
Expand Down
8 changes: 5 additions & 3 deletions x/btcstaking/types/btc_delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"testing"

sdkmath "cosmossdk.io/math"
bbn "github.com/babylonlabs-io/babylon/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/stretchr/testify/require"

bbn "github.com/babylonlabs-io/babylon/types"

asig "github.com/babylonlabs-io/babylon/crypto/schnorr-adaptor-signature"
btctest "github.com/babylonlabs-io/babylon/testutil/bitcoin"
"github.com/babylonlabs-io/babylon/testutil/datagen"
Expand Down Expand Up @@ -97,7 +98,7 @@ func FuzzBTCDelegation_SlashingTx(f *testing.F) {
CovenantQuorum: covenantQuorum,
}

stakingTimeBlocks := uint16(5)
stakingTimeBlocks := uint32(5)
stakingValue := int64(2 * 10e8)
slashingAddress, err := datagen.GenRandomBTCAddress(r, &chaincfg.SimNetParams)
require.NoError(t, err)
Expand All @@ -121,8 +122,9 @@ func FuzzBTCDelegation_SlashingTx(f *testing.F) {
covenantPKs,
covenantQuorum,
slashingPkScript,
stakingTimeBlocks,
1000,
uint32(1000+stakingTimeBlocks),
1000+stakingTimeBlocks,
uint64(stakingValue),
slashingRate,
slashingChangeLockTime,
Expand Down
10 changes: 6 additions & 4 deletions x/btcstaking/types/btc_undelegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"testing"

sdkmath "cosmossdk.io/math"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/stretchr/testify/require"

asig "github.com/babylonlabs-io/babylon/crypto/schnorr-adaptor-signature"
btctest "github.com/babylonlabs-io/babylon/testutil/bitcoin"
"github.com/babylonlabs-io/babylon/testutil/datagen"
bbn "github.com/babylonlabs-io/babylon/types"
"github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/stretchr/testify/require"
)

func FuzzBTCUndelegation_SlashingTx(f *testing.F) {
Expand Down Expand Up @@ -49,7 +50,7 @@ func FuzzBTCUndelegation_SlashingTx(f *testing.F) {
}
covenantSigners := covenantSKs

stakingTimeBlocks := uint16(5)
stakingTimeBlocks := uint32(5)
stakingValue := int64(2 * 10e8)
slashingAddress, err := datagen.GenRandomBTCAddress(r, &chaincfg.SimNetParams)
require.NoError(t, err)
Expand All @@ -72,6 +73,7 @@ func FuzzBTCUndelegation_SlashingTx(f *testing.F) {
covenantPKs,
covenantQuorum,
slashingPkScript,
stakingTimeBlocks,
1000,
uint32(stakingTimeBlocks)+1000,
uint64(stakingValue),
Expand Down
1 change: 1 addition & 0 deletions x/btcstaking/types/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func NewBTCDelegationResponse(btcDel *BTCDelegation, status BTCDelegationStatus)
StakerAddr: btcDel.StakerAddr,
BtcPk: btcDel.BtcPk,
FpBtcPkList: btcDel.FpBtcPkList,
StakingTime: btcDel.StakingTime,
StartHeight: btcDel.StartHeight,
EndHeight: btcDel.EndHeight,
TotalSat: btcDel.TotalSat,
Expand Down
Loading

0 comments on commit 6f1808b

Please sign in to comment.