From acb64a95390c7276f07c2c39938eea4d1ee66a6b Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Tue, 3 Jan 2023 14:26:44 +0100 Subject: [PATCH] feat(logic): register params for genesis --- x/logic/keeper/params.go | 6 +-- x/logic/types/params.go | 92 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 10 deletions(-) diff --git a/x/logic/keeper/params.go b/x/logic/keeper/params.go index 95505435..1641c2ef 100644 --- a/x/logic/keeper/params.go +++ b/x/logic/keeper/params.go @@ -6,9 +6,9 @@ import ( ) // GetParams get all parameters as types.Params. -func (k Keeper) GetParams(ctx sdk.Context) types.Params { - // TODO implement me - return types.Params{} +func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { + k.paramstore.GetParamSet(ctx, ¶ms) + return } // SetParams set the params. diff --git a/x/logic/types/params.go b/x/logic/types/params.go index e0ab3dca..523d32b4 100644 --- a/x/logic/types/params.go +++ b/x/logic/types/params.go @@ -1,36 +1,114 @@ package types import ( + "fmt" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" ) var _ paramtypes.ParamSet = (*Params)(nil) +// Parameter store keys. +var ( + KeyInterpreter = []byte("Interpreter") + KeyLimits = []byte("Limits") +) + +var ( + DefaultRegisteredPredicates = make([]string, 0) + DefaultBootstrap = "" + DefaultMaxGas = uint64(200000) + DefaultMaxSize = uint32(0) + DefaultMaxResultCount = uint32(50) +) + // ParamKeyTable the param key table for launch module. func ParamKeyTable() paramtypes.KeyTable { return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) } +// NewParams creates a new Params object. +func NewParams(interpreter Interpreter, limits Limits) Params { + return Params{ + Interpreter: interpreter, + Limits: limits, + } +} + // DefaultParams returns a default set of parameters. func DefaultParams() Params { - // TODO implement me - return Params{} + return NewParams(DefaultInterpreter(), DefaultLimits()) } // ParamSetPairs get the params.ParamSet. func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { - // TODO implement me - return []paramtypes.ParamSetPair{} + return []paramtypes.ParamSetPair{ + paramtypes.NewParamSetPair(KeyInterpreter, &p.Interpreter, validateInterpreter), + paramtypes.NewParamSetPair(KeyLimits, &p.Limits, validateLimits), + } } // Validate validates the set of params. func (p Params) Validate() error { - // TODO implement me + if err := validateInterpreter(p.Interpreter); err != nil { + return err + } + if err := validateLimits(p.Limits); err != nil { + return err + } + return nil } // String implements the Stringer interface. func (p Params) String() string { - // TODO implement me - return "" + return p.Interpreter.String() + "\n" + + p.Limits.String() +} + +// NewInterpreter creates a new Interpreter object. +func NewInterpreter(registeredPredicates []string, bootstrap string) Interpreter { + return Interpreter{ + RegisteredPredicates: registeredPredicates, + Bootstrap: bootstrap, + } +} + +// DefaultInterpreter return an Interpreter object with default params. +func DefaultInterpreter() Interpreter { + return NewInterpreter(DefaultRegisteredPredicates, DefaultBootstrap) +} + +func validateInterpreter(i interface{}) error { + _, ok := i.(Interpreter) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + // TODO: Validate interpreter params. + return nil +} + +// NewLimits creates a new Limits object. +func NewLimits(maxGas uint64, maxSize, maxResultCount uint32) Limits { + return Limits{ + MaxGas: maxGas, + MaxSize: maxSize, + MaxResultCount: maxResultCount, + } +} + +// DefaultLimits return a Limits object with default params. +func DefaultLimits() Limits { + return NewLimits(DefaultMaxGas, DefaultMaxSize, DefaultMaxResultCount) +} + +func validateLimits(i interface{}) error { + _, ok := i.(Limits) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + // TODO: Validate limits params. + return nil }