github.com/cosmos/cosmos-sdk@v0.50.10/x/slashing/types/params.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"cosmossdk.io/math"
     8  )
     9  
    10  // Default parameter namespace
    11  const (
    12  	DefaultSignedBlocksWindow   = int64(100)
    13  	DefaultDowntimeJailDuration = 60 * 10 * time.Second
    14  )
    15  
    16  var (
    17  	DefaultMinSignedPerWindow      = math.LegacyNewDecWithPrec(5, 1)
    18  	DefaultSlashFractionDoubleSign = math.LegacyNewDec(1).Quo(math.LegacyNewDec(20))
    19  	DefaultSlashFractionDowntime   = math.LegacyNewDec(1).Quo(math.LegacyNewDec(100))
    20  )
    21  
    22  // NewParams creates a new Params object
    23  func NewParams(
    24  	signedBlocksWindow int64, minSignedPerWindow math.LegacyDec, downtimeJailDuration time.Duration,
    25  	slashFractionDoubleSign, slashFractionDowntime math.LegacyDec,
    26  ) Params {
    27  	return Params{
    28  		SignedBlocksWindow:      signedBlocksWindow,
    29  		MinSignedPerWindow:      minSignedPerWindow,
    30  		DowntimeJailDuration:    downtimeJailDuration,
    31  		SlashFractionDoubleSign: slashFractionDoubleSign,
    32  		SlashFractionDowntime:   slashFractionDowntime,
    33  	}
    34  }
    35  
    36  // DefaultParams defines the parameters for this module
    37  func DefaultParams() Params {
    38  	return NewParams(
    39  		DefaultSignedBlocksWindow,
    40  		DefaultMinSignedPerWindow,
    41  		DefaultDowntimeJailDuration,
    42  		DefaultSlashFractionDoubleSign,
    43  		DefaultSlashFractionDowntime,
    44  	)
    45  }
    46  
    47  // Validate validates the params
    48  func (p Params) Validate() error {
    49  	if err := validateSignedBlocksWindow(p.SignedBlocksWindow); err != nil {
    50  		return err
    51  	}
    52  	if err := validateMinSignedPerWindow(p.MinSignedPerWindow); err != nil {
    53  		return err
    54  	}
    55  	if err := validateDowntimeJailDuration(p.DowntimeJailDuration); err != nil {
    56  		return err
    57  	}
    58  	if err := validateSlashFractionDoubleSign(p.SlashFractionDoubleSign); err != nil {
    59  		return err
    60  	}
    61  	if err := validateSlashFractionDowntime(p.SlashFractionDowntime); err != nil {
    62  		return err
    63  	}
    64  	return nil
    65  }
    66  
    67  func validateSignedBlocksWindow(i interface{}) error {
    68  	v, ok := i.(int64)
    69  	if !ok {
    70  		return fmt.Errorf("invalid parameter type: %T", i)
    71  	}
    72  
    73  	if v <= 0 {
    74  		return fmt.Errorf("signed blocks window must be positive: %d", v)
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  func validateMinSignedPerWindow(i interface{}) error {
    81  	v, ok := i.(math.LegacyDec)
    82  	if !ok {
    83  		return fmt.Errorf("invalid parameter type: %T", i)
    84  	}
    85  
    86  	if v.IsNil() {
    87  		return fmt.Errorf("min signed per window cannot be nil: %s", v)
    88  	}
    89  	if v.IsNegative() {
    90  		return fmt.Errorf("min signed per window cannot be negative: %s", v)
    91  	}
    92  	if v.GT(math.LegacyOneDec()) {
    93  		return fmt.Errorf("min signed per window too large: %s", v)
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  func validateDowntimeJailDuration(i interface{}) error {
   100  	v, ok := i.(time.Duration)
   101  	if !ok {
   102  		return fmt.Errorf("invalid parameter type: %T", i)
   103  	}
   104  
   105  	if v <= 0 {
   106  		return fmt.Errorf("downtime jail duration must be positive: %s", v)
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  func validateSlashFractionDoubleSign(i interface{}) error {
   113  	v, ok := i.(math.LegacyDec)
   114  	if !ok {
   115  		return fmt.Errorf("invalid parameter type: %T", i)
   116  	}
   117  
   118  	if v.IsNil() {
   119  		return fmt.Errorf("double sign slash fraction cannot be nil: %s", v)
   120  	}
   121  	if v.IsNegative() {
   122  		return fmt.Errorf("double sign slash fraction cannot be negative: %s", v)
   123  	}
   124  	if v.GT(math.LegacyOneDec()) {
   125  		return fmt.Errorf("double sign slash fraction too large: %s", v)
   126  	}
   127  
   128  	return nil
   129  }
   130  
   131  func validateSlashFractionDowntime(i interface{}) error {
   132  	v, ok := i.(math.LegacyDec)
   133  	if !ok {
   134  		return fmt.Errorf("invalid parameter type: %T", i)
   135  	}
   136  
   137  	if v.IsNil() {
   138  		return fmt.Errorf("downtime slash fraction cannot be nil: %s", v)
   139  	}
   140  	if v.IsNegative() {
   141  		return fmt.Errorf("downtime slash fraction cannot be negative: %s", v)
   142  	}
   143  	if v.GT(math.LegacyOneDec()) {
   144  		return fmt.Errorf("downtime slash fraction too large: %s", v)
   145  	}
   146  
   147  	return nil
   148  }