Skip to content

Commit

Permalink
feat: support collections (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Nov 25, 2023
1 parent b9d9dda commit 0675047
Show file tree
Hide file tree
Showing 42 changed files with 288 additions and 638 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [#3536](https://github.com/ignite/cli/pull/3536) Change app.go to v2 and add AppWiring feature
- [#3659](https://github.com/ignite/cli/pull/3659) cosmos-sdk `v0.50.x`
- [#3670](https://github.com/ignite/cli/pull/3670) Remove nodetime binaries
- [#3707](https://github.com/ignite/cli/pull/3707) Add collections support
- [#3724](https://github.com/ignite/cli/pull/3724) Add or vendor proto packages from Go dependencies
- [#3715](https://github.com/ignite/cli/pull/3715) Add test suite for the cli tests
- [#3756](https://github.com/ignite/cli/pull/3756) Add faucet compatibility for latest sdk chains
Expand Down
7 changes: 7 additions & 0 deletions ignite/templates/app/files/app/ibc.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ func (app *App) registerIBCModules() {
app.GetMemKey(capabilitytypes.MemStoreKey),
)

// add capability keeper and ScopeToModule for ibc module
app.CapabilityKeeper = capabilitykeeper.NewKeeper(
app.AppCodec(),
app.GetKey(capabilitytypes.StoreKey),
app.GetMemKey(capabilitytypes.MemStoreKey),
)

// add capability keeper and ScopeToModule for ibc module
scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule(ibcexported.ModuleName)
scopedIBCTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// <%= queryName.UpperCamel %>Result returns the <%= queryName.UpperCamel %> result by RequestId
func (k Keeper) <%= queryName.UpperCamel %>Result(c context.Context, req *types.Query<%= queryName.UpperCamel %>Request) (*types.Query<%= queryName.UpperCamel %>Response, error) {
func (q queryServer) <%= queryName.UpperCamel %>Result(c context.Context, req *types.Query<%= queryName.UpperCamel %>Request) (*types.Query<%= queryName.UpperCamel %>Response, error) {
ctx := sdk.UnwrapSDKContext(c)
result, err := k.Get<%= queryName.UpperCamel %>Result(ctx, types.OracleRequestID(req.RequestId))
if err != nil {
Expand All @@ -18,7 +18,7 @@ func (k Keeper) <%= queryName.UpperCamel %>Result(c context.Context, req *types.
}

// Last<%= queryName.UpperCamel %>Id returns the last <%= queryName.UpperCamel %> request Id
func (k Keeper) Last<%= queryName.UpperCamel %>Id(c context.Context, req *types.QueryLast<%= queryName.UpperCamel %>IdRequest) (*types.QueryLast<%= queryName.UpperCamel %>IdResponse, error) {
func (q queryServer) Last<%= queryName.UpperCamel %>Id(c context.Context, req *types.QueryLast<%= queryName.UpperCamel %>IdRequest) (*types.QueryLast<%= queryName.UpperCamel %>IdResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
id := k.GetLast<%= queryName.UpperCamel %>ID(ctx)
return &types.QueryLast<%= queryName.UpperCamel %>IdResponse{RequestId: id}, nil
Expand Down

This file was deleted.

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,14 @@ type (
storeService store.KVStoreService
logger log.Logger

// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string
// 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]
// this line is used by starport scaffolding # 1

<%= if (isIBC) { %>
ibcKeeperFn func() *ibckeeper.Keeper
capabilityScopedFn func(string) capabilitykeeper.ScopedKeeper
Expand All @@ -52,7 +58,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 +69,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,14 @@ import (
"<%= modulePath %>/x/<%= moduleName %>/types"
)

var _ types.QueryServer = Keeper{}
var _ types.QueryServer = queryServer{}

// NewQueryServerImpl returns an implementation of the QueryServer interface
// for the provided Keeper.
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 @@ -2,19 +2,29 @@ package keeper

import (
"context"
"errors"

"cosmossdk.io/collections"

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 {
if errors.Is(err, collections.ErrNotFound) {
return nil, status.Error(codes.NotFound, "not found")
}

return nil, status.Error(codes.Internal, "internal error")
}

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 @@ -128,7 +128,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
Loading

0 comments on commit 0675047

Please sign in to comment.