github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/staking/types/params.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"time"
     7  
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	"github.com/fibonacci-chain/fbc/x/common"
    10  	"github.com/fibonacci-chain/fbc/x/params"
    11  )
    12  
    13  const (
    14  	// Update the validator set every 252 blocks by default
    15  	DefaultBlocksPerEpoch = 252
    16  
    17  	// Default maximum number of validators to vote
    18  	DefaultMaxValsToVote = 30
    19  
    20  	// Default validate rate update interval by hours
    21  	DefaultValidateRateUpdateInterval = 24
    22  )
    23  
    24  // Staking params default values
    25  const (
    26  
    27  	// Default unbonding duration, 14 days
    28  	DefaultUnbondingTime time.Duration = time.Hour * 24 * 7 * 2
    29  
    30  	// Default maximum number of bonded validators
    31  	DefaultMaxValidators uint16 = 21
    32  
    33  	DefaultEpoch              uint16 = DefaultBlocksPerEpoch
    34  	DefaultMaxValsToAddShares uint16 = DefaultMaxValsToVote
    35  )
    36  
    37  var (
    38  	// DefaultMinDelegation is the limit value of delegation or undelegation
    39  	DefaultMinDelegation = sdk.NewDecWithPrec(1, 4)
    40  	// DefaultMinSelfDelegation is the default value of each validator's msd (hard code)
    41  	DefaultMinSelfDelegation = sdk.NewDec(10000)
    42  )
    43  
    44  // nolint - Keys for parameter access
    45  var (
    46  	KeyUnbondingTime     = []byte("UnbondingTime")
    47  	KeyMaxValidators     = []byte("MaxValidators")
    48  	KeyEpoch             = []byte("BlocksPerEpoch")    // how many blocks each epoch has
    49  	KeyTheEndOfLastEpoch = []byte("TheEndOfLastEpoch") // a block height that is the end of last epoch
    50  
    51  	KeyMaxValsToAddShares = []byte("MaxValsToAddShares")
    52  	KeyMinDelegation      = []byte("MinDelegation")
    53  	KeyMinSelfDelegation  = []byte("MinSelfDelegation")
    54  
    55  	KeyHistoricalEntries = []byte("HistoricalEntries")
    56  )
    57  
    58  var _ params.ParamSet = (*Params)(nil)
    59  
    60  // Params defines the high level settings for staking
    61  type Params struct {
    62  	// time duration of unbonding
    63  	UnbondingTime time.Duration `json:"unbonding_time" yaml:"unbonding_time"`
    64  	// note: we need to be a bit careful about potential overflow here, since this is user-determined
    65  	// maximum number of validators (max uint16 = 65535)
    66  	MaxValidators uint16 `json:"max_bonded_validators" yaml:"max_bonded_validators"`
    67  	// epoch for validator update
    68  	Epoch              uint16 `json:"epoch" yaml:"epoch"`
    69  	MaxValsToAddShares uint16 `json:"max_validators_to_add_shares" yaml:"max_validators_to_add_shares"`
    70  	// limited amount of delegate
    71  	MinDelegation sdk.Dec `json:"min_delegation" yaml:"min_delegation"`
    72  	// validator's self declared minimum self delegation
    73  	MinSelfDelegation sdk.Dec `json:"min_self_delegation" yaml:"min_self_delegation"`
    74  
    75  	HistoricalEntries uint32 `protobuf:"varint,4,opt,name=historical_entries,json=historicalEntries,proto3" json:"historical_entries,omitempty" yaml:"historical_entries"`
    76  }
    77  
    78  // NewParams creates a new Params instance
    79  func NewParams(unbondingTime time.Duration, maxValidators uint16, epoch uint16, maxValsToAddShares uint16, minDelegation sdk.Dec,
    80  	minSelfDelegation sdk.Dec) Params {
    81  	return Params{
    82  		UnbondingTime:      unbondingTime,
    83  		MaxValidators:      maxValidators,
    84  		Epoch:              epoch,
    85  		MaxValsToAddShares: maxValsToAddShares,
    86  		MinDelegation:      minDelegation,
    87  		MinSelfDelegation:  minSelfDelegation,
    88  	}
    89  }
    90  
    91  // TODO: to supplement the validate function for every pair of param
    92  func validateParams(value interface{}) error {
    93  	return nil
    94  }
    95  
    96  // ParamSetPairs is the implements params.ParamSet
    97  func (p *Params) ParamSetPairs() params.ParamSetPairs {
    98  	return params.ParamSetPairs{
    99  		{Key: KeyUnbondingTime, Value: &p.UnbondingTime, ValidatorFn: common.ValidateDurationPositive("unbonding time")},
   100  		{Key: KeyMaxValidators, Value: &p.MaxValidators, ValidatorFn: common.ValidateUint16Positive("max validators")},
   101  		{Key: KeyEpoch, Value: &p.Epoch, ValidatorFn: common.ValidateUint16Positive("epoch")},
   102  		{Key: KeyMaxValsToAddShares, Value: &p.MaxValsToAddShares, ValidatorFn: common.ValidateUint16Positive("max vals to add shares")},
   103  		{Key: KeyMinDelegation, Value: &p.MinDelegation, ValidatorFn: common.ValidateDecPositive("min delegation")},
   104  		{Key: KeyMinSelfDelegation, Value: &p.MinSelfDelegation, ValidatorFn: common.ValidateDecPositive("min self delegation")},
   105  	}
   106  }
   107  func validateHistoricalEntries(i interface{}) error {
   108  	_, ok := i.(uint32)
   109  	if !ok {
   110  		return fmt.Errorf("invalid parameter type: %T", i)
   111  	}
   112  
   113  	return nil
   114  }
   115  
   116  // Equal returns a boolean determining if two Param types are identical
   117  // TODO: This is slower than comparing struct fields directly
   118  func (p Params) Equal(p2 Params) bool {
   119  	bz1 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&p)
   120  	bz2 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&p2)
   121  	return bytes.Equal(bz1, bz2)
   122  }
   123  
   124  // DefaultParams returns a default set of parameters
   125  func DefaultParams() Params {
   126  	return NewParams(
   127  		DefaultUnbondingTime,
   128  		DefaultMaxValidators,
   129  		DefaultEpoch,
   130  		DefaultMaxValsToAddShares,
   131  		DefaultMinDelegation,
   132  		DefaultMinSelfDelegation,
   133  	)
   134  }
   135  
   136  // String returns a human readable string representation of the Params
   137  func (p *Params) String() string {
   138  	return fmt.Sprintf(`Params:
   139    Unbonding Time:    		%s
   140    Max Validators:   	 	%d
   141    Epoch: 					%d
   142    MaxValsToAddShares:       %d
   143    MinDelegation				%d
   144    MinSelfDelegation         %d`,
   145  		p.UnbondingTime, p.MaxValidators, p.Epoch, p.MaxValsToAddShares, p.MinDelegation, p.MinSelfDelegation)
   146  }
   147  
   148  // Validate gives a quick validity check for a set of params
   149  func (p Params) Validate() error {
   150  	if p.MaxValidators == 0 {
   151  		return fmt.Errorf("staking parameter MaxValidators must be a positive integer")
   152  	}
   153  	if p.Epoch == 0 {
   154  		return fmt.Errorf("staking parameter Epoch must be a positive integer")
   155  	}
   156  	if p.MaxValsToAddShares == 0 {
   157  		return fmt.Errorf("staking parameter MaxValsToAddShares must be a positive integer")
   158  	}
   159  
   160  	return nil
   161  }