Skip to content

Commit

Permalink
feat: collections
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Oct 18, 2023
1 parent 98bc8d6 commit a9650bf
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"fmt"

"cosmossdk.io/collections"
"cosmossdk.io/core/store"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import "cosmossdk.io/collections"

const (
// ModuleName defines the module name
ModuleName = "<%= moduleName %>"
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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") { %>
Expand All @@ -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() %>,<% } %>
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion ignite/templates/module/create/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-")

Check warning on line 172 in ignite/templates/module/create/ibc.go

View check run for this annotation

Codecov / codecov/patch

ignite/templates/module/create/ibc.go#L172

Added line #L172 was not covered by tests
)`
replacementPort := fmt.Sprintf(templatePort, opts.ModuleName)
content = replacer.Replace(content, module.PlaceholderIBCKeysPort, replacementPort)
Expand Down

0 comments on commit a9650bf

Please sign in to comment.