Skip to content

Commit

Permalink
Update params validation and genesis command
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec committed Aug 30, 2024
1 parent 1b71870 commit 54715fb
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 57 deletions.
20 changes: 20 additions & 0 deletions cmd/babylond/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const (
flagVoteExtensionEnableHeight = "vote-extension-enable-height"
flagCovenantPks = "covenant-pks"
flagCovenantQuorum = "covenant-quorum"
flagMinStakingAmtSat = "min-staking-amount-sat"
flagMaxStakingAmtSat = "max-staking-amount-sat"
flagMinStakingTimeBlocks = "min-staking-time-blocks"
flagMaxStakingTimeBlocks = "max-staking-time-blocks"
flagMaxActiveFinalityProviders = "max-active-finality-providers"
flagMinUnbondingTime = "min-unbonding-time"
flagUnbondingFeeSat = "unbonding-fee-sat"
Expand Down Expand Up @@ -65,6 +69,10 @@ type GenesisCLIArgs struct {
VoteExtensionEnableHeight int64
CovenantPKs []string
CovenantQuorum uint32
MinStakingAmtSat int64
MaxStakingAmtSat int64
MinStakingTimeBlocks uint16
MaxStakingTimeBlocks uint16
SlashingPkScript string
MinSlashingTransactionFeeSat int64
SlashingRate math.LegacyDec
Expand Down Expand Up @@ -92,6 +100,10 @@ func addGenesisFlags(cmd *cobra.Command) {
// btcstaking args
cmd.Flags().String(flagCovenantPks, strings.Join(btcstypes.DefaultParams().CovenantPksHex(), ","), "Bitcoin staking covenant public keys, comma separated")
cmd.Flags().Uint32(flagCovenantQuorum, btcstypes.DefaultParams().CovenantQuorum, "Bitcoin staking covenant quorum")
cmd.Flags().Int64(flagMinStakingAmtSat, 500000, "Minimum staking amount in satoshis")
cmd.Flags().Int64(flagMaxStakingAmtSat, 100000000000, "Maximum staking amount in satoshis")
cmd.Flags().Uint16(flagMinStakingTimeBlocks, 100, "Minimum staking time in blocks")
cmd.Flags().Uint16(flagMaxStakingTimeBlocks, 10000, "Maximum staking time in blocks")
cmd.Flags().String(flagSlashingPkScript, hex.EncodeToString(btcstypes.DefaultParams().SlashingPkScript), "Bitcoin staking slashing pk script. Hex encoded.")
cmd.Flags().Int64(flagMinSlashingFee, 1000, "Bitcoin staking minimum slashing fee")
cmd.Flags().String(flagMinCommissionRate, "0", "Bitcoin staking validator minimum commission rate")
Expand Down Expand Up @@ -124,6 +136,10 @@ func parseGenesisFlags(cmd *cobra.Command) *GenesisCLIArgs {
reporterAddresses, _ := cmd.Flags().GetString(flagAllowedReporterAddresses)
covenantPks, _ := cmd.Flags().GetString(flagCovenantPks)
covenantQuorum, _ := cmd.Flags().GetUint32(flagCovenantQuorum)
minStakingAmtSat, _ := cmd.Flags().GetInt64(flagMinStakingAmtSat)
maxStakingAmtSat, _ := cmd.Flags().GetInt64(flagMaxStakingAmtSat)
minStakingTimeBlocks, _ := cmd.Flags().GetUint16(flagMinStakingTimeBlocks)
maxStakingTimeBlocks, _ := cmd.Flags().GetUint16(flagMaxStakingTimeBlocks)
slashingPkScript, _ := cmd.Flags().GetString(flagSlashingPkScript)
minSlashingFee, _ := cmd.Flags().GetInt64(flagMinSlashingFee)
minCommissionRate, _ := cmd.Flags().GetString(flagMinCommissionRate)
Expand Down Expand Up @@ -163,6 +179,10 @@ func parseGenesisFlags(cmd *cobra.Command) *GenesisCLIArgs {
AllowedReporterAddresses: allowedReporterAddresses,
CovenantPKs: strings.Split(covenantPks, ","),
CovenantQuorum: covenantQuorum,
MinStakingAmtSat: minStakingAmtSat,
MaxStakingAmtSat: maxStakingAmtSat,
MinStakingTimeBlocks: minStakingTimeBlocks,
MaxStakingTimeBlocks: maxStakingTimeBlocks,
SlashingPkScript: slashingPkScript,
MinSlashingTransactionFeeSat: minSlashingFee,
MinCommissionRate: math.LegacyMustNewDecFromStr(minCommissionRate),
Expand Down
82 changes: 66 additions & 16 deletions cmd/babylond/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,37 @@ Example:

var genesisParams GenesisParams
if network == "testnet" {
genesisParams = TestnetGenesisParams(genesisCliArgs.MaxActiveValidators,
genesisCliArgs.BtcConfirmationDepth, genesisCliArgs.BtcFinalizationTimeout, genesisCliArgs.CheckpointTag,
genesisCliArgs.EpochInterval, genesisCliArgs.BaseBtcHeaderHex,
genesisCliArgs.BaseBtcHeaderHeight, genesisCliArgs.AllowedReporterAddresses,
genesisCliArgs.CovenantPKs, genesisCliArgs.CovenantQuorum,
genesisCliArgs.SlashingPkScript, genesisCliArgs.MinSlashingTransactionFeeSat,
genesisCliArgs.MinCommissionRate, genesisCliArgs.SlashingRate, genesisCliArgs.MaxActiveFinalityProviders,
genesisCliArgs.MinUnbondingTime, genesisCliArgs.UnbondingFeeSat, genesisCliArgs.InflationRateChange,
genesisCliArgs.InflationMin, genesisCliArgs.InflationMax, genesisCliArgs.GoalBonded,
genesisCliArgs.BlocksPerYear, genesisCliArgs.GenesisTime, genesisCliArgs.BlockGasLimit, genesisCliArgs.VoteExtensionEnableHeight)
genesisParams = TestnetGenesisParams(
genesisCliArgs.MaxActiveValidators,
genesisCliArgs.BtcConfirmationDepth,
genesisCliArgs.BtcFinalizationTimeout,
genesisCliArgs.CheckpointTag,
genesisCliArgs.EpochInterval,
genesisCliArgs.BaseBtcHeaderHex,
genesisCliArgs.BaseBtcHeaderHeight,
genesisCliArgs.AllowedReporterAddresses,
genesisCliArgs.CovenantPKs,
genesisCliArgs.CovenantQuorum,
genesisCliArgs.MinStakingAmtSat,
genesisCliArgs.MaxStakingAmtSat,
genesisCliArgs.MinStakingTimeBlocks,
genesisCliArgs.MaxStakingTimeBlocks,
genesisCliArgs.SlashingPkScript,
genesisCliArgs.MinSlashingTransactionFeeSat,
genesisCliArgs.MinCommissionRate,
genesisCliArgs.SlashingRate,
genesisCliArgs.MaxActiveFinalityProviders,
genesisCliArgs.MinUnbondingTime,
genesisCliArgs.UnbondingFeeSat,
genesisCliArgs.InflationRateChange,
genesisCliArgs.InflationMin,
genesisCliArgs.InflationMax,
genesisCliArgs.GoalBonded,
genesisCliArgs.BlocksPerYear,
genesisCliArgs.GenesisTime,
genesisCliArgs.BlockGasLimit,
genesisCliArgs.VoteExtensionEnableHeight,
)
} else if network == "mainnet" {
// TODO: mainnet genesis params
panic("Mainnet params not implemented.")
Expand Down Expand Up @@ -239,12 +260,37 @@ type GenesisParams struct {
VoteExtensionsEnableHeight int64
}

func TestnetGenesisParams(maxActiveValidators uint32, btcConfirmationDepth uint64,
btcFinalizationTimeout uint64, checkpointTag string, epochInterval uint64, baseBtcHeaderHex string,
baseBtcHeaderHeight uint64, allowedReporters []string, covenantPKs []string, covenantQuorum uint32, slashingPkScriptHex string, minSlashingFee int64,
minCommissionRate sdkmath.LegacyDec, slashingRate sdkmath.LegacyDec, maxActiveFinalityProviders uint32, minUnbondingTime uint16, unbondingFeeSat int64, inflationRateChange float64,
inflationMin float64, inflationMax float64, goalBonded float64,
blocksPerYear uint64, genesisTime time.Time, blockGasLimit int64, voteExtensionEnableHeight int64) GenesisParams {
func TestnetGenesisParams(
maxActiveValidators uint32,
btcConfirmationDepth uint64,
btcFinalizationTimeout uint64,
checkpointTag string,
epochInterval uint64,
baseBtcHeaderHex string,
baseBtcHeaderHeight uint64,
allowedReporters []string,
covenantPKs []string,
covenantQuorum uint32,
minStakingAmtSat int64,
maxStakingAmtSat int64,
minStakingTimeBlocks uint16,
maxStakingTimeBlocks uint16,
slashingPkScriptHex string,
minSlashingFee int64,
minCommissionRate sdkmath.LegacyDec,
slashingRate sdkmath.LegacyDec,
maxActiveFinalityProviders uint32,
minUnbondingTime uint16,
unbondingFeeSat int64,
inflationRateChange float64,
inflationMin float64,
inflationMax float64,
goalBonded float64,
blocksPerYear uint64,
genesisTime time.Time,
blockGasLimit int64,
voteExtensionEnableHeight int64,
) GenesisParams {

genParams := GenesisParams{}

Expand Down Expand Up @@ -345,6 +391,10 @@ func TestnetGenesisParams(maxActiveValidators uint32, btcConfirmationDepth uint6

genParams.BtcstakingParams.CovenantPks = covenantPKsBIP340
genParams.BtcstakingParams.CovenantQuorum = covenantQuorum
genParams.BtcstakingParams.MinStakingValueSat = minStakingAmtSat
genParams.BtcstakingParams.MaxStakingValueSat = maxStakingAmtSat
genParams.BtcstakingParams.MinStakingTimeBlocks = uint32(minStakingTimeBlocks)
genParams.BtcstakingParams.MaxStakingTimeBlocks = uint32(maxStakingTimeBlocks)
genParams.BtcstakingParams.SlashingPkScript = slashingPkScript
genParams.BtcstakingParams.MinSlashingTxFeeSat = minSlashingFee
genParams.BtcstakingParams.MinCommissionRate = minCommissionRate
Expand Down
39 changes: 31 additions & 8 deletions cmd/babylond/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,37 @@ Example:
return errors.New("base Bitcoin header height should be a uint64")
}

genesisParams := TestnetGenesisParams(genesisCliArgs.MaxActiveValidators,
genesisCliArgs.BtcConfirmationDepth, genesisCliArgs.BtcFinalizationTimeout, genesisCliArgs.CheckpointTag,
genesisCliArgs.EpochInterval, genesisCliArgs.BaseBtcHeaderHex, genesisCliArgs.BaseBtcHeaderHeight,
genesisCliArgs.AllowedReporterAddresses, genesisCliArgs.CovenantPKs, genesisCliArgs.CovenantQuorum,
genesisCliArgs.SlashingPkScript, genesisCliArgs.MinSlashingTransactionFeeSat, genesisCliArgs.MinCommissionRate,
genesisCliArgs.SlashingRate, genesisCliArgs.MaxActiveFinalityProviders, genesisCliArgs.MinUnbondingTime, genesisCliArgs.UnbondingFeeSat, genesisCliArgs.InflationRateChange, genesisCliArgs.InflationMin,
genesisCliArgs.InflationMax, genesisCliArgs.GoalBonded, genesisCliArgs.BlocksPerYear,
genesisCliArgs.GenesisTime, genesisCliArgs.BlockGasLimit, genesisCliArgs.VoteExtensionEnableHeight)
genesisParams := TestnetGenesisParams(
genesisCliArgs.MaxActiveValidators,
genesisCliArgs.BtcConfirmationDepth,
genesisCliArgs.BtcFinalizationTimeout,
genesisCliArgs.CheckpointTag,
genesisCliArgs.EpochInterval,
genesisCliArgs.BaseBtcHeaderHex,
genesisCliArgs.BaseBtcHeaderHeight,
genesisCliArgs.AllowedReporterAddresses,
genesisCliArgs.CovenantPKs,
genesisCliArgs.CovenantQuorum,
genesisCliArgs.MinStakingAmtSat,
genesisCliArgs.MaxStakingAmtSat,
genesisCliArgs.MinStakingTimeBlocks,
genesisCliArgs.MaxStakingTimeBlocks,
genesisCliArgs.SlashingPkScript,
genesisCliArgs.MinSlashingTransactionFeeSat,
genesisCliArgs.MinCommissionRate,
genesisCliArgs.SlashingRate,
genesisCliArgs.MaxActiveFinalityProviders,
genesisCliArgs.MinUnbondingTime,
genesisCliArgs.UnbondingFeeSat,
genesisCliArgs.InflationRateChange,
genesisCliArgs.InflationMin,
genesisCliArgs.InflationMax,
genesisCliArgs.GoalBonded,
genesisCliArgs.BlocksPerYear,
genesisCliArgs.GenesisTime,
genesisCliArgs.BlockGasLimit,
genesisCliArgs.VoteExtensionEnableHeight,
)

return InitTestnet(
clientCtx, cmd, config, mbm, genBalIterator, outputDir, genesisCliArgs.ChainID, minGasPrices,
Expand Down
4 changes: 4 additions & 0 deletions x/btcstaking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (h *Helper) GenAndApplyCustomParams(
err = h.BTCStakingKeeper.SetParams(h.Ctx, types.Params{
CovenantPks: bbn.NewBIP340PKsFromBTCPKs(covenantPKs),
CovenantQuorum: 3,
MinStakingValueSat: 1000,
MaxStakingValueSat: int64(4 * 10e8),
MinStakingTimeBlocks: 10,
MaxStakingTimeBlocks: 10000,
SlashingPkScript: slashingPkScript,
MinSlashingTxFeeSat: 10,
MinCommissionRate: sdkmath.LegacyMustNewDecFromStr("0.01"),
Expand Down
91 changes: 63 additions & 28 deletions x/btcstaking/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,85 @@ import (
func TestGenesisState_Validate(t *testing.T) {
tests := []struct {
desc string
genState *types.GenesisState
genState func() *types.GenesisState
valid bool
}{
{
desc: "default is valid",
genState: types.DefaultGenesis(),
valid: true,
desc: "default is valid",
genState: func() *types.GenesisState {
return types.DefaultGenesis()
},
valid: true,
},
{
desc: "valid genesis state",
genState: &types.GenesisState{
Params: []*types.Params{&types.Params{
CovenantPks: types.DefaultParams().CovenantPks,
CovenantQuorum: types.DefaultParams().CovenantQuorum,
SlashingPkScript: types.DefaultParams().SlashingPkScript,
MinSlashingTxFeeSat: 500,
MinCommissionRate: sdkmath.LegacyMustNewDecFromStr("0.5"),
SlashingRate: sdkmath.LegacyMustNewDecFromStr("0.1"),
MaxActiveFinalityProviders: 100,
UnbondingFeeSat: types.DefaultParams().UnbondingFeeSat,
}},
genState: func() *types.GenesisState {
return &types.GenesisState{
Params: []*types.Params{
&types.Params{
CovenantPks: types.DefaultParams().CovenantPks,
CovenantQuorum: types.DefaultParams().CovenantQuorum,
MinStakingValueSat: 1000,
MaxStakingValueSat: 100000000,
MinStakingTimeBlocks: 100,
MaxStakingTimeBlocks: 1000,
SlashingPkScript: types.DefaultParams().SlashingPkScript,
MinSlashingTxFeeSat: 500,
MinCommissionRate: sdkmath.LegacyMustNewDecFromStr("0.5"),
SlashingRate: sdkmath.LegacyMustNewDecFromStr("0.1"),
MaxActiveFinalityProviders: 100,
UnbondingFeeSat: types.DefaultParams().UnbondingFeeSat,
},
},
}
},
valid: true,
},
{
desc: "invalid slashing rate in genesis",
genState: &types.GenesisState{
Params: []*types.Params{&types.Params{
CovenantPks: types.DefaultParams().CovenantPks,
CovenantQuorum: types.DefaultParams().CovenantQuorum,
SlashingPkScript: types.DefaultParams().SlashingPkScript,
MinSlashingTxFeeSat: 500,
MinCommissionRate: sdkmath.LegacyMustNewDecFromStr("0.5"),
SlashingRate: sdkmath.LegacyZeroDec(), // invalid slashing rate
MaxActiveFinalityProviders: 100,
UnbondingFeeSat: types.DefaultParams().UnbondingFeeSat,
},
}},
genState: func() *types.GenesisState {
return &types.GenesisState{
Params: []*types.Params{
&types.Params{
CovenantPks: types.DefaultParams().CovenantPks,
CovenantQuorum: types.DefaultParams().CovenantQuorum,
SlashingPkScript: types.DefaultParams().SlashingPkScript,
MinSlashingTxFeeSat: 500,
MinCommissionRate: sdkmath.LegacyMustNewDecFromStr("0.5"),
SlashingRate: sdkmath.LegacyZeroDec(), // invalid slashing rate
MaxActiveFinalityProviders: 100,
UnbondingFeeSat: types.DefaultParams().UnbondingFeeSat,
},
},
}
},
valid: false,
},
{
desc: "min staking time larger than max staking time",
genState: func() *types.GenesisState {
d := types.DefaultGenesis()
d.Params[0].MinStakingTimeBlocks = 1000
d.Params[0].MaxStakingTimeBlocks = 100
return d
},
valid: false,
},
{
desc: "min staking value larger than max staking value",
genState: func() *types.GenesisState {
d := types.DefaultGenesis()
d.Params[0].MinStakingValueSat = 1000
d.Params[0].MaxStakingValueSat = 100
return d
},
valid: false,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
err := tc.genState.Validate()
state := tc.genState()
err := state.Validate()
if tc.valid {
require.NoError(t, err)
} else {
Expand Down
Loading

0 comments on commit 54715fb

Please sign in to comment.