Skip to content

Commit

Permalink
feat: Add SlashingRate param in btcstaking module (#95)
Browse files Browse the repository at this point in the history
* proto def

* proto def

* genesis setup

* fix

* more comments

* comprehensive comments

* improve checks

* fix checks

* change slashing_percentage data type to cosmos.Dec

* fix slashing_percentage usages to cosmos.Dec

* fix validation check for default params

* fix checks and tests

* change default percentage to 10

* fix template

* change slashing percentage to slashing rate in proto

* replace usages with slashingRate

* fix comments and checks

* fix error message
  • Loading branch information
gusin13 authored Nov 3, 2023
1 parent 3869365 commit 06a9a8d
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 31 deletions.
8 changes: 7 additions & 1 deletion cmd/babylond/cmd/flags.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

babylonApp "github.com/babylonchain/babylon/app"
btcctypes "github.com/babylonchain/babylon/x/btccheckpoint/types"
btcstypes "github.com/babylonchain/babylon/x/btcstaking/types"
Expand All @@ -30,6 +31,7 @@ const (
flagJuryPk = "jury-pk"
flagSlashingAddress = "slashing-address"
flagMinSlashingFee = "min-slashing-fee-sat"
flagSlashingRate = "slashing-rate"
flagMinPubRand = "min-pub-rand"
flagMinCommissionRate = "min-commission-rate"
)
Expand All @@ -53,6 +55,7 @@ type GenesisCLIArgs struct {
JuryPK string
SlashingAddress string
MinSlashingTransactionFeeSat int64
SlashingRate sdk.Dec
MinPubRand uint64
MinCommissionRate sdk.Dec
}
Expand All @@ -76,6 +79,7 @@ func addGenesisFlags(cmd *cobra.Command) {
cmd.Flags().String(flagSlashingAddress, btcstypes.DefaultParams().SlashingAddress, "Bitcoin staking slashing address")
cmd.Flags().Int64(flagMinSlashingFee, 1000, "Bitcoin staking minimum slashing fee")
cmd.Flags().String(flagMinCommissionRate, "0", "Bitcoin staking validator minimum commission rate")
cmd.Flags().String(flagSlashingRate, "0.1", "Bitcoin staking slashing rate")
// finality args
cmd.Flags().Uint64(flagMinPubRand, 100, "Bitcoin staking minimum public randomness commit")
// inflation args
Expand Down Expand Up @@ -103,6 +107,7 @@ func parseGenesisFlags(cmd *cobra.Command) *GenesisCLIArgs {
slashingAddress, _ := cmd.Flags().GetString(flagSlashingAddress)
minSlashingFee, _ := cmd.Flags().GetInt64(flagMinSlashingFee)
minCommissionRate, _ := cmd.Flags().GetString(flagMinCommissionRate)
slashingRate, _ := cmd.Flags().GetString(flagSlashingRate)
minPubRand, _ := cmd.Flags().GetUint64(flagMinPubRand)
genesisTimeUnix, _ := cmd.Flags().GetInt64(flagGenesisTime)
inflationRateChange, _ := cmd.Flags().GetFloat64(flagInflationRateChange)
Expand Down Expand Up @@ -131,6 +136,7 @@ func parseGenesisFlags(cmd *cobra.Command) *GenesisCLIArgs {
SlashingAddress: slashingAddress,
MinSlashingTransactionFeeSat: minSlashingFee,
MinCommissionRate: sdk.MustNewDecFromStr(minCommissionRate),
SlashingRate: sdk.MustNewDecFromStr(slashingRate),
MinPubRand: minPubRand,
GenesisTime: genesisTime,
InflationRateChange: inflationRateChange,
Expand Down
9 changes: 6 additions & 3 deletions cmd/babylond/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cmd
import (
"encoding/json"
"fmt"
"time"

btcstakingtypes "github.com/babylonchain/babylon/x/btcstaking/types"
finalitytypes "github.com/babylonchain/babylon/x/finality/types"
"time"

appparams "github.com/babylonchain/babylon/app/params"
bbn "github.com/babylonchain/babylon/types"
Expand Down Expand Up @@ -69,7 +70,8 @@ Example:
genesisCliArgs.EpochInterval, genesisCliArgs.BaseBtcHeaderHex,
genesisCliArgs.BaseBtcHeaderHeight, genesisCliArgs.JuryPK,
genesisCliArgs.SlashingAddress, genesisCliArgs.MinSlashingTransactionFeeSat,
genesisCliArgs.MinCommissionRate, genesisCliArgs.MinPubRand, genesisCliArgs.InflationRateChange,
genesisCliArgs.MinCommissionRate, genesisCliArgs.SlashingRate,
genesisCliArgs.MinPubRand, genesisCliArgs.InflationRateChange,
genesisCliArgs.InflationMin, genesisCliArgs.InflationMax, genesisCliArgs.GoalBonded,
genesisCliArgs.BlocksPerYear, genesisCliArgs.GenesisTime, genesisCliArgs.BlockGasLimit)
} else if network == "mainnet" {
Expand Down Expand Up @@ -229,7 +231,7 @@ type GenesisParams struct {
func TestnetGenesisParams(maxActiveValidators uint32, btcConfirmationDepth uint64,
btcFinalizationTimeout uint64, checkpointTag string, epochInterval uint64, baseBtcHeaderHex string,
baseBtcHeaderHeight uint64, juryPk string, slashingAddress string, minSlashingFee int64,
minCommissionRate sdk.Dec, minPubRand uint64, inflationRateChange float64,
minCommissionRate sdk.Dec, slashingRate sdk.Dec, minPubRand uint64, inflationRateChange float64,
inflationMin float64, inflationMax float64, goalBonded float64,
blocksPerYear uint64, genesisTime time.Time, blockGasLimit int64) GenesisParams {

Expand Down Expand Up @@ -310,6 +312,7 @@ func TestnetGenesisParams(maxActiveValidators uint32, btcConfirmationDepth uint6
genParams.BtcstakingParams.SlashingAddress = slashingAddress
genParams.BtcstakingParams.MinSlashingTxFeeSat = minSlashingFee
genParams.BtcstakingParams.MinCommissionRate = minCommissionRate
genParams.BtcstakingParams.SlashingRate = slashingRate
if err := genParams.BtcstakingParams.Validate(); err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/babylond/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ Example:
genesisCliArgs.BtcConfirmationDepth, genesisCliArgs.BtcFinalizationTimeout, genesisCliArgs.CheckpointTag,
genesisCliArgs.EpochInterval, genesisCliArgs.BaseBtcHeaderHex, genesisCliArgs.BaseBtcHeaderHeight,
genesisCliArgs.JuryPK, genesisCliArgs.SlashingAddress, genesisCliArgs.MinSlashingTransactionFeeSat,
genesisCliArgs.MinCommissionRate, genesisCliArgs.MinPubRand, genesisCliArgs.InflationRateChange, genesisCliArgs.InflationMin,
genesisCliArgs.MinCommissionRate, genesisCliArgs.SlashingRate, genesisCliArgs.MinPubRand,
genesisCliArgs.InflationRateChange, genesisCliArgs.InflationMin,
genesisCliArgs.InflationMax, genesisCliArgs.GoalBonded, genesisCliArgs.BlocksPerYear,
genesisCliArgs.GenesisTime, genesisCliArgs.BlockGasLimit)

Expand Down
10 changes: 9 additions & 1 deletion proto/babylon/btcstaking/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package babylon.btcstaking.v1;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/babylonchain/babylon/x/btcstaking/types";

Expand All @@ -15,7 +16,7 @@ message Params {
// slashing address is the address that the slashed BTC goes to
// the address is in string on Bitcoin
string slashing_address = 2;
// min_slashing_tx_fee_sat is the minimum amount of tx fee (quantified
// min_slashing_tx_fee_sat is the minimum amount of tx fee (quantified
// in Satoshi) needed for the pre-signed slashing tx
// TODO: change to satoshi per byte?
int64 min_slashing_tx_fee_sat = 3;
Expand All @@ -24,4 +25,11 @@ message Params {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// slashing_rate determines the portion of the staked amount to be slashed,
// expressed as a decimal (e.g., 0.5 for 50%).
string slashing_rate = 5 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
2 changes: 2 additions & 0 deletions x/btcstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func getJuryInfo(t *testing.T,
SlashingAddress: slashingAddr.String(),
MinSlashingTxFeeSat: 10,
MinCommissionRate: sdk.MustNewDecFromStr("0.01"),
SlashingRate: sdk.MustNewDecFromStr("0.1"),
})
require.NoError(t, err)
return jurySK, juryPK, slashingAddr
Expand Down Expand Up @@ -406,6 +407,7 @@ func TestDoNotAllowDelegationWithoutValidator(t *testing.T) {
SlashingAddress: slashingAddr.String(),
MinSlashingTxFeeSat: 10,
MinCommissionRate: sdk.MustNewDecFromStr("0.01"),
SlashingRate: sdk.MustNewDecFromStr("0.1"),
})
require.NoError(t, err)

Expand Down
17 changes: 16 additions & 1 deletion x/btcstaking/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package types_test

import (
sdkmath "cosmossdk.io/math"
"testing"

sdkmath "cosmossdk.io/math"

"github.com/babylonchain/babylon/x/btcstaking/types"
"github.com/stretchr/testify/require"
)
Expand All @@ -27,10 +28,24 @@ func TestGenesisState_Validate(t *testing.T) {
SlashingAddress: types.DefaultParams().SlashingAddress,
MinSlashingTxFeeSat: 500,
MinCommissionRate: sdkmath.LegacyMustNewDecFromStr("0.5"),
SlashingRate: sdkmath.LegacyMustNewDecFromStr("0.1"),
},
},
valid: true,
},
{
desc: "invalid slashing rate in genesis",
genState: &types.GenesisState{
Params: types.Params{
JuryPk: types.DefaultParams().JuryPk,
SlashingAddress: types.DefaultParams().SlashingAddress,
MinSlashingTxFeeSat: 500,
MinCommissionRate: sdkmath.LegacyMustNewDecFromStr("0.5"),
SlashingRate: sdkmath.LegacyZeroDec(), // invalid slashing rate
},
},
valid: false,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
Expand Down
16 changes: 15 additions & 1 deletion x/btcstaking/types/params.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package types

import (
"cosmossdk.io/math"
"fmt"

"cosmossdk.io/math"
bbn "github.com/babylonchain/babylon/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
Expand Down Expand Up @@ -43,6 +44,8 @@ func DefaultParams() Params {
SlashingAddress: defaultSlashingAddress(),
MinSlashingTxFeeSat: 1000,
MinCommissionRate: math.LegacyZeroDec(),
// The Default slashing rate is 0.1 i.e., 10% of the total staked BTC will be burned.
SlashingRate: math.LegacyNewDecWithPrec(1, 1), // 1 * 10^{-1} = 0.1
}
}

Expand Down Expand Up @@ -73,6 +76,14 @@ func validateMinCommissionRate(rate sdk.Dec) error {
return nil
}

// validateSlashingRate checks if the slashing rate is within the valid range (0, 1].
func validateSlashingRate(slashingRate sdk.Dec) error {
if slashingRate.LTE(math.LegacyZeroDec()) || slashingRate.GT(math.LegacyOneDec()) {
return fmt.Errorf("slashing rate must be in the range (0, 1] i.e., 0 exclusive and 1 inclusive")
}
return nil
}

// Validate validates the set of params
func (p Params) Validate() error {
if err := validateMinSlashingTxFeeSat(p.MinSlashingTxFeeSat); err != nil {
Expand All @@ -81,6 +92,9 @@ func (p Params) Validate() error {
if err := validateMinCommissionRate(p.MinCommissionRate); err != nil {
return err
}
if err := validateSlashingRate(p.SlashingRate); err != nil {
return err
}
return nil
}

Expand Down
99 changes: 76 additions & 23 deletions x/btcstaking/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 06a9a8d

Please sign in to comment.