github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/params/subspace/common_test.go (about)

     1  package subspace_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/params/subspace"
    10  )
    11  
    12  var (
    13  	keyUnbondingTime = []byte("UnbondingTime")
    14  	keyMaxValidators = []byte("MaxValidators")
    15  	keyBondDenom     = []byte("BondDenom")
    16  
    17  	key  = sdk.NewKVStoreKey("storekey")
    18  	tkey = sdk.NewTransientStoreKey("transientstorekey")
    19  )
    20  
    21  type params struct {
    22  	UnbondingTime time.Duration `json:"unbonding_time" yaml:"unbonding_time"`
    23  	MaxValidators uint16        `json:"max_validators" yaml:"max_validators"`
    24  	BondDenom     string        `json:"bond_denom" yaml:"bond_denom"`
    25  }
    26  
    27  func validateUnbondingTime(i interface{}) error {
    28  	v, ok := i.(time.Duration)
    29  	if !ok {
    30  		return fmt.Errorf("invalid parameter type: %T", i)
    31  	}
    32  
    33  	if v < (24 * time.Hour) {
    34  		return fmt.Errorf("unbonding time must be at least one day")
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  func validateMaxValidators(i interface{}) error {
    41  	_, ok := i.(uint16)
    42  	if !ok {
    43  		return fmt.Errorf("invalid parameter type: %T", i)
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func validateBondDenom(i interface{}) error {
    50  	v, ok := i.(string)
    51  	if !ok {
    52  		return fmt.Errorf("invalid parameter type: %T", i)
    53  	}
    54  
    55  	if len(v) == 0 {
    56  		return errors.New("denom cannot be empty")
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  func (p *params) ParamSetPairs() subspace.ParamSetPairs {
    63  	return subspace.ParamSetPairs{
    64  		{keyUnbondingTime, &p.UnbondingTime, validateUnbondingTime},
    65  		{keyMaxValidators, &p.MaxValidators, validateMaxValidators},
    66  		{keyBondDenom, &p.BondDenom, validateBondDenom},
    67  	}
    68  }
    69  
    70  func paramKeyTable() subspace.KeyTable {
    71  	return subspace.NewKeyTable().RegisterParamSet(&params{})
    72  }