github.com/cosmos/cosmos-sdk@v0.50.10/baseapp/params_legacy.go (about)

     1  /*
     2  Deprecated.
     3  
     4  Legacy types are defined below to aid in the migration of CometBFT consensus
     5  parameters from use of the now deprecated x/params modules to a new dedicated
     6  x/consensus module.
     7  
     8  Application developers should ensure that they implement their upgrade handler
     9  correctly such that app.ConsensusParamsKeeper.Set() is called with the values
    10  returned by GetConsensusParams().
    11  
    12  Example:
    13  
    14  	baseAppLegacySS := app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())
    15  
    16  	app.UpgradeKeeper.SetUpgradeHandler(
    17  		UpgradeName,
    18  		func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
    19  			if cp := baseapp.GetConsensusParams(ctx, baseAppLegacySS); cp != nil {
    20  				app.ConsensusParamsKeeper.Set(ctx, cp)
    21  			} else {
    22  				ctx.Logger().Info("warning: consensus parameters are undefined; skipping migration", "upgrade", UpgradeName)
    23  			}
    24  
    25  			return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM)
    26  		},
    27  	)
    28  
    29  Developers can also bypass the use of the legacy Params subspace and set the
    30  values to app.ConsensusParamsKeeper.Set() explicitly.
    31  
    32  Note, for new chains this is not necessary as CometBFT's consensus parameters
    33  will automatically be set for you in InitChain.
    34  */
    35  package baseapp
    36  
    37  import (
    38  	"errors"
    39  	"fmt"
    40  
    41  	cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
    42  
    43  	sdk "github.com/cosmos/cosmos-sdk/types"
    44  )
    45  
    46  const Paramspace = "baseapp"
    47  
    48  var (
    49  	ParamStoreKeyBlockParams     = []byte("BlockParams")
    50  	ParamStoreKeyEvidenceParams  = []byte("EvidenceParams")
    51  	ParamStoreKeyValidatorParams = []byte("ValidatorParams")
    52  )
    53  
    54  type LegacyParamStore interface {
    55  	Get(ctx sdk.Context, key []byte, ptr interface{})
    56  	Has(ctx sdk.Context, key []byte) bool
    57  }
    58  
    59  func ValidateBlockParams(i interface{}) error {
    60  	v, ok := i.(cmtproto.BlockParams)
    61  	if !ok {
    62  		return fmt.Errorf("invalid parameter type: %T", i)
    63  	}
    64  
    65  	if v.MaxBytes <= 0 {
    66  		return fmt.Errorf("block maximum bytes must be positive: %d", v.MaxBytes)
    67  	}
    68  
    69  	if v.MaxGas < -1 {
    70  		return fmt.Errorf("block maximum gas must be greater than or equal to -1: %d", v.MaxGas)
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  func ValidateEvidenceParams(i interface{}) error {
    77  	v, ok := i.(cmtproto.EvidenceParams)
    78  	if !ok {
    79  		return fmt.Errorf("invalid parameter type: %T", i)
    80  	}
    81  
    82  	if v.MaxAgeNumBlocks <= 0 {
    83  		return fmt.Errorf("evidence maximum age in blocks must be positive: %d", v.MaxAgeNumBlocks)
    84  	}
    85  
    86  	if v.MaxAgeDuration <= 0 {
    87  		return fmt.Errorf("evidence maximum age time duration must be positive: %v", v.MaxAgeDuration)
    88  	}
    89  
    90  	if v.MaxBytes < 0 {
    91  		return fmt.Errorf("maximum evidence bytes must be non-negative: %v", v.MaxBytes)
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func ValidateValidatorParams(i interface{}) error {
    98  	v, ok := i.(cmtproto.ValidatorParams)
    99  	if !ok {
   100  		return fmt.Errorf("invalid parameter type: %T", i)
   101  	}
   102  
   103  	if len(v.PubKeyTypes) == 0 {
   104  		return errors.New("validator allowed pubkey types must not be empty")
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  func GetConsensusParams(ctx sdk.Context, paramStore LegacyParamStore) *cmtproto.ConsensusParams {
   111  	if paramStore == nil {
   112  		return nil
   113  	}
   114  
   115  	cp := new(cmtproto.ConsensusParams)
   116  
   117  	if paramStore.Has(ctx, ParamStoreKeyBlockParams) {
   118  		var bp cmtproto.BlockParams
   119  
   120  		paramStore.Get(ctx, ParamStoreKeyBlockParams, &bp)
   121  		cp.Block = &bp
   122  	}
   123  
   124  	if paramStore.Has(ctx, ParamStoreKeyEvidenceParams) {
   125  		var ep cmtproto.EvidenceParams
   126  
   127  		paramStore.Get(ctx, ParamStoreKeyEvidenceParams, &ep)
   128  		cp.Evidence = &ep
   129  	}
   130  
   131  	if paramStore.Has(ctx, ParamStoreKeyValidatorParams) {
   132  		var vp cmtproto.ValidatorParams
   133  
   134  		paramStore.Get(ctx, ParamStoreKeyValidatorParams, &vp)
   135  		cp.Validator = &vp
   136  	}
   137  
   138  	return cp
   139  }
   140  
   141  func MigrateParams(ctx sdk.Context, lps LegacyParamStore, ps ParamStore) error {
   142  	if cp := GetConsensusParams(ctx, lps); cp != nil {
   143  		if err := ps.Set(ctx, *cp); err != nil {
   144  			return err
   145  		}
   146  	} else {
   147  		ctx.Logger().Info("warning: consensus parameters are undefined; skipping migration")
   148  	}
   149  	return nil
   150  }