Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support collections #3707

Merged
merged 29 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b33bb05
feat: support collections
julienrbrt Mar 4, 2024
cae207e
Merge branch 'main' into julien/collections
julienrbrt Mar 4, 2024
f4e3b9e
updates
julienrbrt Mar 4, 2024
3a1877c
updates
julienrbrt Mar 4, 2024
9e1d4b0
updates
julienrbrt Mar 5, 2024
5b4bbea
updates
julienrbrt Mar 5, 2024
72338e3
updates
julienrbrt Mar 5, 2024
8f06b67
updates
julienrbrt Mar 5, 2024
635c94a
updates
julienrbrt Mar 5, 2024
57dac64
updates
julienrbrt Mar 5, 2024
e24161b
Merge branch 'main' into julien/collections
Pantani Mar 7, 2024
5977313
Merge branch 'main' into julien/collections
julienrbrt Mar 11, 2024
54afc0c
Merge branch 'main' into julien/collections
julienrbrt Mar 13, 2024
dbc68f2
fix build
julienrbrt Mar 14, 2024
24d3b83
updates
julienrbrt Mar 15, 2024
3f1c59d
revert map due to high complexity to scaffold (map and indexmap and v…
julienrbrt Mar 15, 2024
3c04d99
`make lint-fix`
julienrbrt Mar 15, 2024
0fa63da
re-add missing key prefix helper
julienrbrt Mar 15, 2024
c751da7
updates
julienrbrt Mar 15, 2024
a7d8882
fix
julienrbrt Mar 15, 2024
9b447a3
fix sims
julienrbrt Mar 15, 2024
56cf7f3
updates
julienrbrt Mar 15, 2024
6d57e3a
Merge branch 'main' into julien/collections
julienrbrt Mar 17, 2024
e3a2eb3
Merge branch 'main' into julien/collections
julienrbrt Mar 18, 2024
2310bb6
Merge branch 'main' into julien/collections
Pantani Mar 19, 2024
3bb81ae
Merge branch 'main' into julien/collections
Pantani Mar 19, 2024
2046035
Merge branch 'main' into julien/collections
Pantani Mar 19, 2024
44e7b62
Merge branch 'main' into julien/collections
Pantani Mar 19, 2024
e84b503
Merge branch 'main' into julien/collections
julienrbrt Mar 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- [#3707](https://github.com/ignite/cli/pull/3707) Add collections support.
- [#4019](https://github.com/ignite/cli/pull/4019) Add `skip-proto` flag to `s chain` command
- [#3977](https://github.com/ignite/cli/pull/3977) Add `chain lint` command to lint the chain's codebase using `golangci-lint`
- [#3770](https://github.com/ignite/cli/pull/3770) Add `scaffold configs` and `scaffold params` commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import (
)


func (k msgServer) <%= MsgName.UpperCamel %>(goCtx context.Context, msg *types.Msg<%= MsgName.UpperCamel %>) (*types.Msg<%= MsgName.UpperCamel %>Response, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx
func (k msgServer) <%= MsgName.UpperCamel %>(ctx context.Context, msg *types.Msg<%= MsgName.UpperCamel %>) (*types.Msg<%= MsgName.UpperCamel %>Response, error) {
// TODO: Handle the message

return &types.Msg<%= MsgName.UpperCamel %>Response{}, nil
}
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
if err := k.SetParams(ctx, types.DefaultParams()); err != nil {
if err := k.Params.Set(ctx, types.DefaultParams()); err != nil {
panic(err)
}

Expand Down
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 # collection/type

<%= 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,17 @@ 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)),
// this line is used by starport scaffolding # collection/instantiate
}

schema, err := sb.Build()
if err != nil {
panic(err)
Pantani marked this conversation as resolved.
Show resolved Hide resolved
}
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,8 +12,7 @@ import (
func TestMsgUpdateParams(t *testing.T) {
k, ms, ctx := setupMsgServer(t)
params := types.DefaultParams()
require.NoError(t, k.SetParams(ctx, params))
wctx := sdk.UnwrapSDKContext(ctx)
require.NoError(t, k.Params.Set(ctx, params))

// default params
testCases := []struct {
Expand Down Expand Up @@ -51,7 +50,7 @@ func TestMsgUpdateParams(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := ms.UpdateParams(wctx, tc.input)
_, err := ms.UpdateParams(ctx, tc.input)

if tc.expErr {
require.Error(t, err)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (

var _ types.QueryServer = queryServer{}

type queryServer struct {
k Keeper
}

// NewQueryServerImpl returns an implementation of the QueryServer interface.
// 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 (s queryServer) 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: s.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 @@ -5,16 +5,18 @@ import (

"github.com/stretchr/testify/require"

keepertest "<%= modulePath %>/testutil/keeper"
keepertest "<%= modulePath %>/testutil/keeper"
"<%= modulePath %>/x/<%= moduleName %>/keeper"
"<%= modulePath %>/x/<%= moduleName %>/types"
"<%= modulePath %>/x/<%= moduleName %>/keeper"
"<%= modulePath %>/x/<%= moduleName %>/types"
)

func TestParamsQuery(t *testing.T) {
k, ctx := keepertest.<%= title(moduleName) %>Keeper(t)

qs := keeper.NewQueryServerImpl(k)
params := types.DefaultParams()
require.NoError(t, k.SetParams(ctx, params))
require.NoError(t, k.Params.Set(ctx, params))

response, err := qs.Params(ctx, &types.QueryParamsRequest{})
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ 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
if err := k.SetParams(ctx, genState.Params); err != nil {
if err := k.Params.Set(ctx, genState.Params); err != nil {
panic(err)
}
}

// 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
@@ -1,5 +1,7 @@
package types

import "cosmossdk.io/collections"

const (
// ModuleName defines the module name
ModuleName = "<%= moduleName %>"
Expand All @@ -14,11 +16,11 @@ 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 @@ -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
if err := k.SetParams(ctx, types.DefaultParams()); err != nil {
if err := k.Params.Set(ctx, types.DefaultParams()); err != nil {
panic(err)
}

Expand Down
2 changes: 1 addition & 1 deletion ignite/templates/module/create/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
// 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 177 in ignite/templates/module/create/ibc.go

View check run for this annotation

Codecov / codecov/patch

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

Added line #L177 was not covered by tests
)`
replacementPort := fmt.Sprintf(templatePort, opts.ModuleName)
content = replacer.Replace(content, module.PlaceholderIBCKeysPort, replacementPort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import (
"context"

"<%= ModulePath %>/x/<%= ModuleName %>/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (s queryServer) <%= QueryName.UpperCamel %>(goCtx context.Context, req *types.Query<%= QueryName.UpperCamel %>Request) (*types.Query<%= QueryName.UpperCamel %>Response, error) {
func (q queryServer) <%= QueryName.UpperCamel %>(ctx context.Context, req *types.Query<%= QueryName.UpperCamel %>Request) (*types.Query<%= QueryName.UpperCamel %>Response, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Process the query
_ = ctx

return &types.Query<%= QueryName.UpperCamel %>Response{}, nil
}
Loading
Loading