From a9650bf5709e6709d9e1d63fbe8531d03f43015d Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 19 Oct 2023 00:11:59 +0200 Subject: [PATCH] feat: collections --- .../testutil/keeper/{{moduleName}}.go.plush | 2 +- .../x/{{moduleName}}/keeper/keeper.go.plush | 22 ++++++++++-- .../keeper/msg_update_params.go.plush | 6 ++-- .../keeper/msg_update_params_test.go.plush | 2 +- .../x/{{moduleName}}/keeper/params.go.plush | 34 ------------------- .../keeper/params_test.go.plush | 18 ---------- .../x/{{moduleName}}/keeper/query.go.plush | 10 +++++- .../keeper/query_params.go.plush | 11 +++--- .../keeper/query_params_test.go.plush | 11 +++--- .../x/{{moduleName}}/module/genesis.go.plush | 9 +++-- .../x/{{moduleName}}/module/module.go.plush | 2 +- .../base/x/{{moduleName}}/types/keys.go.plush | 8 ++--- .../x/{{moduleName}}/types/params.go.plush | 16 --------- .../testutil/keeper/{{moduleName}}.go.plush | 2 +- ignite/templates/module/create/ibc.go | 2 +- 15 files changed, 59 insertions(+), 96 deletions(-) delete mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params.go.plush delete mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params_test.go.plush diff --git a/ignite/templates/module/create/files/base/testutil/keeper/{{moduleName}}.go.plush b/ignite/templates/module/create/files/base/testutil/keeper/{{moduleName}}.go.plush index bde969ac1b..2bf14af185 100644 --- a/ignite/templates/module/create/files/base/testutil/keeper/{{moduleName}}.go.plush +++ b/ignite/templates/module/create/files/base/testutil/keeper/{{moduleName}}.go.plush @@ -44,7 +44,7 @@ func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context) { ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger()) // Initialize params - k.SetParams(ctx, types.DefaultParams()) + _ = k.Params.Set(ctx, types.DefaultParams()) return k, ctx } diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush index d4ed36dd16..7705a6f714 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush @@ -3,6 +3,7 @@ package keeper import ( "fmt" + "cosmossdk.io/collections" "cosmossdk.io/core/store" "cosmossdk.io/log" "github.com/cosmos/cosmos-sdk/codec" @@ -26,9 +27,13 @@ type ( storeService store.KVStoreService logger log.Logger - // the address capable of executing a MsgUpdateParams message. Typically, this - // should be the x/gov module account. + // the address capable of executing a MsgUpdateParams message. + // Typically, this should be the x/gov module account. authority string + + Schema collections.Schema + Params collections.Item[types.Params] + <%= if (isIBC) { %> ibcKeeperFn func() *ibckeeper.Keeper capabilityScopedFn func(string) capabilitykeeper.ScopedKeeper @@ -52,7 +57,9 @@ func NewKeeper( panic(fmt.Sprintf("invalid authority address: %s", authority)) } - return Keeper{ + sb := collections.NewSchemaBuilder(storeService) + + k := Keeper{ cdc: cdc, storeService: storeService, authority: authority, @@ -61,7 +68,16 @@ func NewKeeper( capabilityScopedFn: capabilityScopedFn,<% } %> <%= for (dependency) in dependencies { %> <%= toVariableName(dependency.KeeperName()) %>: <%= toVariableName(dependency.KeeperName()) %>,<% } %> + Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)), } + + schema, err := sb.Build() + if err != nil { + panic(err) + } + k.Schema = schema + + return k } // GetAuthority returns the module's authority. diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params.go.plush index 93137c8ab9..2a29e4e669 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params.go.plush @@ -4,18 +4,16 @@ import ( "context" errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" "<%= modulePath %>/x/<%= moduleName %>/types" ) -func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) { +func (k msgServer) UpdateParams(ctx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) { if k.GetAuthority() != req.Authority { return nil, errorsmod.Wrapf(types.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority) } - ctx := sdk.UnwrapSDKContext(goCtx) - if err := k.SetParams(ctx, req.Params); err != nil { + if err := k.Params.Set(ctx, req.Params); err != nil { return nil, err } diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params_test.go.plush index 9742f5cdd8..a9f05d47ed 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params_test.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params_test.go.plush @@ -12,7 +12,7 @@ import ( func TestMsgUpdateParams(t *testing.T) { k, ms, ctx := setupMsgServer(t) params := types.DefaultParams() - require.NoError(t, k.SetParams(ctx, params)) + require.NoError(t, k.Params.Set(ctx, params)) wctx := sdk.UnwrapSDKContext(ctx) // default params diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params.go.plush deleted file mode 100644 index b64ed1ec37..0000000000 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params.go.plush +++ /dev/null @@ -1,34 +0,0 @@ -package keeper - -import ( - "context" - - "github.com/cosmos/cosmos-sdk/runtime" - - "<%= modulePath %>/x/<%= moduleName %>/types" -) - - -// GetParams get all parameters as types.Params -func (k Keeper) GetParams(ctx context.Context) (params types.Params) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - bz := store.Get(types.ParamsKey) - if bz == nil { - return params - } - - k.cdc.MustUnmarshal(bz, ¶ms) - return params -} - -// SetParams set the params -func (k Keeper) SetParams(ctx context.Context, params types.Params) error { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - bz, err := k.cdc.Marshal(¶ms) - if err != nil { - return err - } - store.Set(types.ParamsKey, bz) - - return nil -} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params_test.go.plush deleted file mode 100644 index 04f1e1685a..0000000000 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params_test.go.plush +++ /dev/null @@ -1,18 +0,0 @@ -package keeper_test - -import ( - "testing" - - "github.com/stretchr/testify/require" - - keepertest "<%= modulePath %>/testutil/keeper" - "<%= modulePath %>/x/<%= moduleName %>/types" -) - -func TestGetParams(t *testing.T) { - k, ctx := keepertest.<%= title(moduleName) %>Keeper(t) - params := types.DefaultParams() - - require.NoError(t, k.SetParams(ctx, params)) - require.EqualValues(t, params, k.GetParams(ctx)) -} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query.go.plush index dbdb2d2ea1..81c87a479e 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query.go.plush @@ -4,4 +4,12 @@ import ( "<%= modulePath %>/x/<%= moduleName %>/types" ) -var _ types.QueryServer = Keeper{} +var _ types.QueryServer = queryServer{} + +func NewQueryServerImpl(k Keeper) types.QueryServer { + return queryServer{k} +} + +type queryServer struct { + k Keeper +} \ No newline at end of file diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params.go.plush index 4d54fc5cae..0ac87d4654 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params.go.plush @@ -3,18 +3,21 @@ package keeper import ( "context" - sdk "github.com/cosmos/cosmos-sdk/types" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "<%= modulePath %>/x/<%= moduleName %>/types" ) -func (k Keeper) Params(goCtx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { +func (q queryServer) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "invalid request") } - ctx := sdk.UnwrapSDKContext(goCtx) - return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil + params, err := q.k.Params.Get(ctx) + if err != nil { + return nil, status.Error(codes.NotFound, "not found") + } + + return &types.QueryParamsResponse{Params: params}, nil } diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params_test.go.plush index c3ff71962e..285e888e5a 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params_test.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params_test.go.plush @@ -7,14 +7,17 @@ import ( keepertest "<%= modulePath %>/testutil/keeper" "<%= modulePath %>/x/<%= moduleName %>/types" + "<%= modulePath %>/x/<%= moduleName %>/keeper" ) func TestParamsQuery(t *testing.T) { - keeper, ctx := keepertest.<%= title(moduleName) %>Keeper(t) - params := types.DefaultParams() - require.NoError(t, keeper.SetParams(ctx, params)) + k, ctx := keepertest.<%= title(moduleName) %>Keeper(t) - response, err := keeper.Params(ctx, &types.QueryParamsRequest{}) + params := types.DefaultParams() + require.NoError(t, k.Params.Set(ctx, params)) + + queryServer := keeper.NewQueryServerImpl(k) + response, err := queryServer.Params(ctx, &types.QueryParamsRequest{}) require.NoError(t, err) require.Equal(t, &types.QueryParamsResponse{Params: params}, response) } diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush index ed35249792..a5fc5f7808 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush @@ -10,13 +10,18 @@ import ( // InitGenesis initializes the module's state from a provided genesis state. func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { // this line is used by starport scaffolding # genesis/module/init - k.SetParams(ctx, genState.Params) + k.Params.Set(ctx, genState.Params) } // ExportGenesis returns the module's exported genesis. func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { + var err error + genesis := types.DefaultGenesis() - genesis.Params = k.GetParams(ctx) + genesis.Params, err = k.Params.Get(ctx) + if err != nil { + panic(err) + } // this line is used by starport scaffolding # genesis/module/export diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush index 42a97558b8..2d53f686bf 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush @@ -135,7 +135,7 @@ func NewAppModule( // RegisterServices registers a gRPC query service to respond to the module-specific gRPC queries func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - types.RegisterQueryServer(cfg.QueryServer(), am.keeper) + types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper)) } // RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted) diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/keys.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/keys.go.plush index 2e03a1573e..137a383212 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/keys.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/keys.go.plush @@ -1,5 +1,7 @@ package types +import "cosmossdk.io/collections" + const ( // ModuleName defines the module name ModuleName = "<%= moduleName %>" @@ -14,11 +16,7 @@ const ( ) var ( - ParamsKey = []byte("p_<%= moduleName %>") + ParamsKey = collections.NewPrefix("p_<%= moduleName %>") ) <%= if (isIBC) { %>// this line is used by starport scaffolding # ibc/keys/port<% } %> - -func KeyPrefix(p string) []byte { - return []byte(p) -} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush index dbe9b6c49b..5e269790dd 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush @@ -2,12 +2,8 @@ package types import ( <%= if (len(params) > 0) { %>"fmt"<% } %> - - paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" ) -var _ paramtypes.ParamSet = (*Params)(nil) - <%= for (param) in params { %> var ( Key<%= param.Name.UpperCamel %> = []byte("<%= param.Name.UpperCamel %>")<%= if (param.DataType() == "string") { %> @@ -18,11 +14,6 @@ var ( ) <% } %> -// ParamKeyTable the param key table for launch module -func ParamKeyTable() paramtypes.KeyTable { - return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) -} - // NewParams creates a new Params instance func NewParams(<%= for (param) in params { %> <%= param.Name.LowerCamel %> <%= param.DataType() %>,<% } %> @@ -39,13 +30,6 @@ func DefaultParams() Params { ) } -// ParamSetPairs get the params.ParamSet -func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { - return paramtypes.ParamSetPairs{<%= for (param) in params { %> - paramtypes.NewParamSetPair(Key<%= param.Name.UpperCamel %>, &p.<%= param.Name.UpperCamel %>, validate<%= param.Name.UpperCamel %>),<% } %> - } -} - // Validate validates the set of params func (p Params) Validate() error {<%= for (param) in params { %> if err := validate<%= param.Name.UpperCamel %>(p.<%= param.Name.UpperCamel %>); err != nil { diff --git a/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush b/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush index 9dbc5263f6..19bc674afd 100644 --- a/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush +++ b/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush @@ -63,7 +63,7 @@ func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context) { ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger()) // Initialize params - k.SetParams(ctx, types.DefaultParams()) + k.Params.Set(ctx, types.DefaultParams()) return k, ctx } diff --git a/ignite/templates/module/create/ibc.go b/ignite/templates/module/create/ibc.go index 506d70e615..1b7feba645 100644 --- a/ignite/templates/module/create/ibc.go +++ b/ignite/templates/module/create/ibc.go @@ -169,7 +169,7 @@ PortID = "%[1]v"` // PlaceholderIBCKeysPort templatePort := `var ( // PortKey defines the key to store the port ID in store - PortKey = KeyPrefix("%[1]v-port-") + PortKey = collections.NewPrefix("%[1]v-port-") )` replacementPort := fmt.Sprintf(templatePort, opts.ModuleName) content = replacer.Replace(content, module.PlaceholderIBCKeysPort, replacementPort)