Skip to content

Commit

Permalink
require exact amount of blocks for unbonding timelock
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec committed Dec 3, 2024
1 parent 8b35d80 commit 692c112
Show file tree
Hide file tree
Showing 25 changed files with 170 additions and 183 deletions.
2 changes: 1 addition & 1 deletion app/upgrades/v1/mainnet/btcstaking_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const BtcStakingParamsStr = `[
"slashing_pk_script": "dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==",
"min_slashing_tx_fee_sat": 1000,
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_time_blocks": 101,
"unbonding_fee_sat": 1000,
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
Expand Down
6 changes: 3 additions & 3 deletions app/upgrades/v1/testnet/btcstaking_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const BtcStakingParamsStr = `[
"slashing_pk_script": "dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==",
"min_slashing_tx_fee_sat": 1000,
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_time_blocks": 101,
"unbonding_fee_sat": 1000,
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
Expand All @@ -40,7 +40,7 @@ const BtcStakingParamsStr = `[
"slashing_pk_script": "dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==",
"min_slashing_tx_fee_sat": 1000,
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_time_blocks": 101,
"unbonding_fee_sat": 1000,
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
Expand All @@ -63,7 +63,7 @@ const BtcStakingParamsStr = `[
"slashing_pk_script": "dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==",
"min_slashing_tx_fee_sat": 1000,
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_time_blocks": 101,
"unbonding_fee_sat": 1000,
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
Expand Down
10 changes: 5 additions & 5 deletions cmd/babylond/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
flagMinStakingTimeBlocks = "min-staking-time-blocks"
flagMaxStakingTimeBlocks = "max-staking-time-blocks"
flagMaxActiveFinalityProviders = "max-active-finality-providers"
flagMinUnbondingTime = "min-unbonding-time"
flagUnbondingTime = "unbonding-time"
flagUnbondingFeeSat = "unbonding-fee-sat"
flagSlashingPkScript = "slashing-pk-script"
flagMinSlashingFee = "min-slashing-fee-sat"
Expand Down Expand Up @@ -83,7 +83,7 @@ type GenesisCLIArgs struct {
MinSlashingTransactionFeeSat int64
SlashingRate math.LegacyDec
MaxActiveFinalityProviders uint32
MinUnbondingTime uint16
UnbondingTime uint16
UnbondingFeeSat int64
MinCommissionRate math.LegacyDec
SignedBlocksWindow int64
Expand Down Expand Up @@ -120,7 +120,7 @@ func addGenesisFlags(cmd *cobra.Command) {
cmd.Flags().String(flagMinCommissionRate, "0", "Bitcoin staking validator minimum commission rate")
cmd.Flags().String(flagSlashingRate, "0.1", "Bitcoin staking slashing rate")
cmd.Flags().Uint32(flagMaxActiveFinalityProviders, 100, "Bitcoin staking maximum active finality providers")
cmd.Flags().Uint16(flagMinUnbondingTime, 0, "Min timelock on unbonding transaction in btc blocks")
cmd.Flags().Uint16(flagUnbondingTime, 21, "Required timelock on unbonding transaction in btc blocks. Must be larger than btc-finalization-timeout")
cmd.Flags().Int64(flagUnbondingFeeSat, 1000, "Required fee for unbonding transaction in satoshis")
// inflation args
cmd.Flags().Float64(flagInflationRateChange, 0.13, "Inflation rate change")
Expand Down Expand Up @@ -162,7 +162,7 @@ func parseGenesisFlags(cmd *cobra.Command) (*GenesisCLIArgs, error) {
minCommissionRate, _ := cmd.Flags().GetString(flagMinCommissionRate)
slashingRate, _ := cmd.Flags().GetString(flagSlashingRate)
maxActiveFinalityProviders, _ := cmd.Flags().GetUint32(flagMaxActiveFinalityProviders)
minUnbondingTime, _ := cmd.Flags().GetUint16(flagMinUnbondingTime)
unbondingTime, _ := cmd.Flags().GetUint16(flagUnbondingTime)
unbondingFeeSat, _ := cmd.Flags().GetInt64(flagUnbondingFeeSat)
genesisTimeUnix, _ := cmd.Flags().GetInt64(flagGenesisTime)
inflationRateChange, _ := cmd.Flags().GetFloat64(flagInflationRateChange)
Expand Down Expand Up @@ -220,7 +220,7 @@ func parseGenesisFlags(cmd *cobra.Command) (*GenesisCLIArgs, error) {
MinCommissionRate: math.LegacyMustNewDecFromStr(minCommissionRate),
SlashingRate: math.LegacyMustNewDecFromStr(slashingRate),
MaxActiveFinalityProviders: maxActiveFinalityProviders,
MinUnbondingTime: minUnbondingTime,
UnbondingTime: unbondingTime,
UnbondingFeeSat: unbondingFeeSat,
GenesisTime: genesisTime,
InflationRateChange: inflationRateChange,
Expand Down
6 changes: 3 additions & 3 deletions cmd/babylond/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Example:
genesisCliArgs.MinCommissionRate,
genesisCliArgs.SlashingRate,
genesisCliArgs.MaxActiveFinalityProviders,
genesisCliArgs.MinUnbondingTime,
genesisCliArgs.UnbondingTime,
genesisCliArgs.UnbondingFeeSat,
genesisCliArgs.InflationRateChange,
genesisCliArgs.InflationMin,
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestnetGenesisParams(
minCommissionRate sdkmath.LegacyDec,
slashingRate sdkmath.LegacyDec,
maxActiveFinalityProviders uint32,
minUnbondingTime uint16,
unbondingTime uint16,
unbondingFeeSat int64,
inflationRateChange float64,
inflationMin float64,
Expand Down Expand Up @@ -403,7 +403,7 @@ func TestnetGenesisParams(
genParams.BtcstakingParams.MinSlashingTxFeeSat = minSlashingFee
genParams.BtcstakingParams.MinCommissionRate = minCommissionRate
genParams.BtcstakingParams.SlashingRate = slashingRate
genParams.BtcstakingParams.MinUnbondingTimeBlocks = uint32(minUnbondingTime)
genParams.BtcstakingParams.UnbondingTimeBlocks = uint32(unbondingTime)
genParams.BtcstakingParams.UnbondingFeeSat = unbondingFeeSat
if err := genParams.BtcstakingParams.Validate(); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/babylond/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Example:
genesisCliArgs.MinCommissionRate,
genesisCliArgs.SlashingRate,
genesisCliArgs.MaxActiveFinalityProviders,
genesisCliArgs.MinUnbondingTime,
genesisCliArgs.UnbondingTime,
genesisCliArgs.UnbondingFeeSat,
genesisCliArgs.InflationRateChange,
genesisCliArgs.InflationMin,
Expand Down
5 changes: 3 additions & 2 deletions proto/babylon/btcstaking/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ message Params {
(gogoproto.nullable) = false
];
// PARAMETERS COVERING UNBONDING
// min_unbonding_time is the minimum time for unbonding transaction timelock in BTC blocks
uint32 min_unbonding_time_blocks = 10;
// unbonding_time is the exact unbonding time required from unbonding transaction
// it must be larger than `checkpoint_finalization_timeout` from `btccheckpoint` module
uint32 unbonding_time_blocks = 10;
// unbonding_fee exact fee required for unbonding transaction
int64 unbonding_fee_sat = 11;
// PARAMETERS COVERING FINALITY PROVIDERS
Expand Down
20 changes: 10 additions & 10 deletions test/e2e/btc_staking_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func (s *BTCStakingTestSuite) Test1CreateFinalityProviderAndDelegation() {
// BTC staking params, BTC delegation key pairs and PoP
params := nonValidatorNode.QueryBTCStakingParams()

// minimal required unbonding time
unbondingTime := params.MinUnbondingTimeBlocks
// required unbonding time
unbondingTime := params.UnbondingTimeBlocks

// NOTE: we use the node's address for the BTC delegation
stakerAddr := sdk.MustAccAddressFromBech32(nonValidatorNode.PublicAddress)
Expand Down Expand Up @@ -508,8 +508,8 @@ func (s *BTCStakingTestSuite) Test6MultisigBTCDelegation() {
// BTC staking params, BTC delegation key pairs and PoP
params := nonValidatorNode.QueryBTCStakingParams()

// minimal required unbonding time
unbondingTime := params.MinUnbondingTimeBlocks
// required unbonding time
unbondingTime := params.UnbondingTimeBlocks

// NOTE: we use the multisig address for the BTC delegation
multisigStakerAddr := sdk.MustAccAddressFromBech32(multisigAddr)
Expand Down Expand Up @@ -579,8 +579,8 @@ func (s *BTCStakingTestSuite) Test7BTCDelegationFeeGrant() {
// BTC staking btcStkParams, BTC delegation key pairs and PoP
btcStkParams := nonValidatorNode.QueryBTCStakingParams()

// minimal required unbonding time
unbondingTime := btcStkParams.MinUnbondingTimeBlocks
// required unbonding time
unbondingTime := btcStkParams.UnbondingTimeBlocks

// NOTE: we use the grantee staker address for the BTC delegation PoP
pop, err := bstypes.NewPoPBTC(granteeStakerAddr, s.delBTCSK)
Expand Down Expand Up @@ -670,8 +670,8 @@ func (s *BTCStakingTestSuite) Test8BTCDelegationFeeGrantTyped() {
// BTC staking btcStkParams, BTC delegation key pairs and PoP
btcStkParams := node.QueryBTCStakingParams()

// minimal required unbonding time
unbondingTime := btcStkParams.MinUnbondingTimeBlocks
// required unbonding time
unbondingTime := btcStkParams.UnbondingTimeBlocks

// NOTE: we use the grantee staker address for the BTC delegation PoP
pop, err := bstypes.NewPoPBTC(granteeStakerAddr, s.delBTCSK)
Expand Down Expand Up @@ -936,8 +936,8 @@ func (s *BTCStakingTestSuite) BTCStakingUnbondSlashInfo(
delegatorSig *bbn.BIP340Signature,
) {
covenantBTCPKs := CovenantBTCPKs(params)
// minimal required unbonding time
unbondingTime := params.MinUnbondingTimeBlocks
// required unbonding time
unbondingTime := params.UnbondingTimeBlocks

testStakingInfo = datagen.GenBTCStakingSlashingInfo(
s.r,
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/btc_staking_pre_approval_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func (s *BTCStakingPreApprovalTestSuite) Test1CreateFinalityProviderAndDelegatio
// BTC staking params, BTC delegation key pairs and PoP
params := nonValidatorNode.QueryBTCStakingParams()

// minimal required unbonding time
unbondingTime := params.MinUnbondingTimeBlocks
// required unbonding time
unbondingTime := params.UnbondingTimeBlocks

// NOTE: we use the node's address for the BTC delegation
stakerAddr := sdk.MustAccAddressFromBech32(nonValidatorNode.PublicAddress)
Expand Down Expand Up @@ -534,8 +534,8 @@ func (s *BTCStakingPreApprovalTestSuite) BTCStakingUnbondSlashInfo(
delegatorSig *bbn.BIP340Signature,
) {
covenantBTCPKs := CovenantBTCPKs(params)
// minimal required unbonding time
unbondingTime := params.MinUnbondingTimeBlocks
// required unbonding time
unbondingTime := params.UnbondingTimeBlocks

testStakingInfo = datagen.GenBTCStakingSlashingInfo(
s.r,
Expand Down
8 changes: 4 additions & 4 deletions testutil/btcstaking-helper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (h *Helper) BeginBlocker() {
}

func (h *Helper) GenAndApplyParams(r *rand.Rand) ([]*btcec.PrivateKey, []*btcec.PublicKey) {
// ensure that minUnbondingTime is larger than finalizationTimeout
// ensure that unbonding_time is larger than finalizationTimeout
return h.GenAndApplyCustomParams(r, 100, 200, 0)
}

Expand All @@ -135,7 +135,7 @@ func (h *Helper) SetCtxHeight(height uint64) {
func (h *Helper) GenAndApplyCustomParams(
r *rand.Rand,
finalizationTimeout uint32,
minUnbondingTime uint32,
unbondingTime uint32,
allowListExpirationHeight uint64,
) ([]*btcec.PrivateKey, []*btcec.PublicKey) {
// mock base header
Expand Down Expand Up @@ -165,7 +165,7 @@ func (h *Helper) GenAndApplyCustomParams(
MinSlashingTxFeeSat: 10,
MinCommissionRate: sdkmath.LegacyMustNewDecFromStr("0.01"),
SlashingRate: sdkmath.LegacyNewDecWithPrec(int64(datagen.RandomInt(r, 41)+10), 2),
MinUnbondingTimeBlocks: minUnbondingTime,
UnbondingTimeBlocks: unbondingTime,
UnbondingFeeSat: 1000,
AllowListExpirationHeight: allowListExpirationHeight,
BtcActivationHeight: 1,
Expand Down Expand Up @@ -250,7 +250,7 @@ func (h *Helper) CreateDelegationWithBtcBlockHeight(
if unbondingValue == 0 {
unbondingValue = defaultUnbondingValue
}
defaultUnbondingTime := bsParams.MinUnbondingTimeBlocks
defaultUnbondingTime := bsParams.UnbondingTimeBlocks
if unbondingTime == 0 {
unbondingTime = uint16(defaultUnbondingTime)
}
Expand Down
4 changes: 2 additions & 2 deletions testutil/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ func (h *Helper) CheckDelegator(delegator sdk.AccAddress, val sdk.ValAddress, fo
require.Equal(h.t, ok, found)
}

func (h *Helper) AddDelegation(del *btcstakingtypes.BTCDelegation, minUnbondingTime uint32) {
err := h.App.BTCStakingKeeper.AddBTCDelegation(h.Ctx, del, minUnbondingTime)
func (h *Helper) AddDelegation(del *btcstakingtypes.BTCDelegation) {
err := h.App.BTCStakingKeeper.AddBTCDelegation(h.Ctx, del)
h.NoError(err)
}

Expand Down
6 changes: 3 additions & 3 deletions x/btcstaking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ message Params {
(gogoproto.nullable) = false
];
// PARAMETERS COVERING UNBONDING
// min_unbonding_time is the minimum time for unbonding transaction timelock in BTC blocks
uint32 min_unbonding_time_blocks = 10;
// unbonding_fee exact fee required for unbonding transaction
// unbonding_time_blocks is the required timelock on unbonding transaction in BTC blocks
uint32 unbonding_time_blocks = 10;
// unbonding_fee_sat is the exact fee required for unbonding transaction
int64 unbonding_fee_sat = 11;
// PARAMETERS COVERING FINALITY PROVIDERS
// min_commission_rate is the chain-wide minimum commission rate that a finality provider
Expand Down
5 changes: 2 additions & 3 deletions x/btcstaking/keeper/btc_delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
func (k Keeper) AddBTCDelegation(
ctx sdk.Context,
btcDel *types.BTCDelegation,
minUnbondingTime uint32,
) error {
if err := btcDel.ValidateBasic(); err != nil {
return err
Expand Down Expand Up @@ -79,8 +78,8 @@ func (k Keeper) AddBTCDelegation(
NewState: types.BTCDelegationStatus_UNBONDED,
})

// NOTE: we should have verified that EndHeight > btcTip.Height + min_unbonding_time
k.addPowerDistUpdateEvent(ctx, btcDel.EndHeight-minUnbondingTime, unbondedEvent)
// NOTE: we should have verified that EndHeight > btcTip.Height + unbonding_time
k.addPowerDistUpdateEvent(ctx, btcDel.EndHeight-btcDel.UnbondingTime, unbondedEvent)
}

return nil
Expand Down
6 changes: 2 additions & 4 deletions x/btcstaking/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ func TestExportGenesis(t *testing.T) {
fps := datagen.CreateNFinalityProviders(r, t, numFps)
params := k.GetParams(ctx)

minUnbondingTime := params.MinUnbondingTimeBlocks

chainsHeight := make([]*types.BlockHeightBbnToBtc, 0)
// creates the first as it starts already with an chain height from the helper.
chainsHeight = append(chainsHeight, &types.BlockHeightBbnToBtc{
Expand Down Expand Up @@ -86,7 +84,7 @@ func TestExportGenesis(t *testing.T) {
totalDelegations++

// sets delegations
h.AddDelegation(del, minUnbondingTime)
h.AddDelegation(del)
btcDelegations = append(btcDelegations, del)

// BTC delegators idx
Expand Down Expand Up @@ -115,7 +113,7 @@ func TestExportGenesis(t *testing.T) {
idxEvent := uint64(totalDelegations - 1)
eventsIdx[idxEvent] = &types.EventIndex{
Idx: idxEvent,
BlockHeightBtc: del.EndHeight - minUnbondingTime,
BlockHeightBtc: del.EndHeight - del.UnbondingTime,
Event: unbondedEvent,
}
}
Expand Down
4 changes: 2 additions & 2 deletions x/btcstaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func FuzzFinalityProviderDelegations(f *testing.F) {
)
require.NoError(t, err)
expectedBtcDelsMap[btcDel.BtcPk.MarshalHex()] = btcDel
err = keeper.AddBTCDelegation(ctx, btcDel, btcDel.UnbondingTime-1)
err = keeper.AddBTCDelegation(ctx, btcDel)
require.NoError(t, err)
}

Expand Down Expand Up @@ -336,7 +336,7 @@ func FuzzPendingBTCDelegations(f *testing.F) {
btcDel.CovenantSigs = nil
pendingBtcDelsMap[btcDel.BtcPk.MarshalHex()] = btcDel
}
err = keeper.AddBTCDelegation(ctx, btcDel, btcDel.UnbondingTime-1)
err = keeper.AddBTCDelegation(ctx, btcDel)
require.NoError(t, err)

txHash := btcDel.MustGetStakingTxHash().String()
Expand Down
6 changes: 3 additions & 3 deletions x/btcstaking/keeper/inclusion_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (k Keeper) VerifyInclusionProofAndGetHeight(
stakingTx *btcutil.Tx,
confirmationDepth uint32,
stakingTime uint32,
minUnbondingTime uint32,
unbondingTime uint32,
inclusionProof *types.ParsedProofOfInclusion,
) (*DelegationTimeRangeInfo, error) {
// Check:
Expand Down Expand Up @@ -61,9 +61,9 @@ func (k Keeper) VerifyInclusionProofAndGetHeight(
}

// ensure staking tx's timelock has more than unbonding BTC blocks left
if btcTip.Height+minUnbondingTime >= endHeight {
if btcTip.Height+unbondingTime >= endHeight {
return nil, types.ErrInvalidStakingTx.
Wrapf("staking tx's timelock has no more than unbonding(=%d) blocks left", minUnbondingTime)
Wrapf("staking tx's timelock has no more than unbonding(=%d) blocks left", unbondingTime)
}

return &DelegationTimeRangeInfo{
Expand Down
Loading

0 comments on commit 692c112

Please sign in to comment.