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(templates): use collections for map and remove multi index support #4094

Merged
merged 22 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
updates
  • Loading branch information
julienrbrt committed Apr 23, 2024
commit 457c9f5aa3c564b5625d9fccf91c02c7a951d57c
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Features

- [#3707](https://github.com/ignite/cli/pull/3707) Add collections support.
- [#3707](https://github.com/ignite/cli/pull/3707) and [#4094](https://github.com/ignite/cli/pull/4094) Add collections support.
- [#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
- [#4001](https://github.com/ignite/cli/pull/4001) Improve `xgenny` dry run
Expand All @@ -15,6 +15,7 @@

### Changes

- [#4094](https://github.com/ignite/cli/pull/4094) Scaffolding a multi-index map using `ignite s map foo bar baz --index foobar,foobaz` is no longer supported. Use one index instead of use `collections.IndexedMap`.
- [#4058](https://github.com/ignite/cli/pull/4058) Simplify scaffolded modules by including `ValidateBasic()` logic in message handler.
- [#4058](https://github.com/ignite/cli/pull/4058) Use `address.Codec` instead of `AccAddressFromBech32`.
- [#3993](https://github.com/ignite/cli/pull/3993) Oracle scaffolding was deprecated and has been removed
Expand Down
9 changes: 7 additions & 2 deletions ignite/templates/field/plushhelpers/plushhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ func ExtendPlushContext(ctx *plush.Context) {
ctx.Set("mergeGoImports", mergeGoImports)
ctx.Set("mergeProtoImports", mergeProtoImports)
ctx.Set("mergeCustomImports", mergeCustomImports)
ctx.Set("appendFieldsAndMergeCustomImports", appendFieldsAndMergeCustomImports)
ctx.Set("title", xstrings.Title)
ctx.Set("toLower", strings.ToLower)
}

func appendFieldsAndMergeCustomImports(f field.Field, fields ...field.Fields) []string {
return mergeCustomImports(append(fields, field.Fields{f})...)
}

func mergeCustomImports(fields ...field.Fields) []string {
allImports := make([]string, 0)
exist := make(map[string]struct{})
for _, fields := range fields {
for _, customImport := range fields.Custom() {
for _, field := range fields {
for _, customImport := range field.Custom() {
if _, ok := exist[customImport]; ok {
continue
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
syntax = "proto3";
package <%= protoPkgName %>;

option go_package = "<%= ModulePath %>/x/<%= ModuleName %>/types";<%= for (importName) in mergeCustomImports(Fields, Indexes) { %>
option go_package = "<%= ModulePath %>/x/<%= ModuleName %>/types";<%= for (importName) in appendFieldsAndMergeCustomImports(Index, Fields) { %>
import "<%= AppName %>/<%= ModuleName %>/<%= importName %>.proto"; <% } %><%= for (importName) in mergeProtoImports(Fields) { %>
import "<%= importName %>"; <% } %>

message <%= TypeName.UpperCamel %> {<%= for (i, index) in Indexes { %>
<%= index.ProtoType(i+1) %>; <% } %><%= for (i, field) in Fields { %>
<%= field.ProtoType(i+1+len(Indexes)) %>; <% } %>
<%= if (!NoMessage) { %>string <%= MsgSigner.LowerCamel %> = <%= len(Fields)+len(Indexes)+1 %>;<% } %>
message <%= TypeName.UpperCamel %> {
<%= Index.ProtoType(1) %>; <%= for (i, field) in Fields { %>
<%= field.ProtoType(i+2) %>; <% } %>
<%= if (!NoMessage) { %>string <%= MsgSigner.LowerCamel %> = <%= len(Fields)+2 %>;<% } %>
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,8 @@ func (s queryServer) List<%= TypeName.UpperCamel %>(ctx context.Context, req *ty
var <%= TypeName.LowerCamel %>s []types.<%= TypeName.UpperCamel %>

// TODO use collections (query.CollectionPaginate)

{{/* store := runtime.KVStoreAdapter(s.k.storeService.OpenKVStore(ctx))
<%= TypeName.LowerCamel %>Store := prefix.NewStore(store, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix))

pageRes, err := query.Paginate(<%= TypeName.LowerCamel %>Store, req.Pagination, func(key []byte, value []byte) error {
var <%= TypeName.LowerCamel %> types.<%= TypeName.UpperCamel %>
if err := s.k.cdc.Unmarshal(value, &<%= TypeName.LowerCamel %>); err != nil {
return err
}

<%= TypeName.LowerCamel %>s = append(<%= TypeName.LowerCamel %>s, <%= TypeName.LowerCamel %>)
return nil
}) */}}
var pageRes *query.PageResponse
var err error

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand All @@ -44,7 +33,7 @@ func (s queryServer) Get<%= TypeName.UpperCamel %>(ctx context.Context, req *typ
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

val, err := s.k.<%= TypeName.UpperCamel %>.Get(ctx, <%= Index.Name.UpperCamel %>)
val, err := s.k.<%= TypeName.UpperCamel %>.Get(ctx, req.<%= Index.Name.UpperCamel %>)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, status.Error(codes.NotFound, "not found")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package types

import "cosmossdk.io/collections"

// <%= TypeName.UpperCamel %>KeyPrefix is the prefix to retrieve all <%= TypeName.UpperCamel %>
var <%= TypeName.UpperCamel %>KeyPrefix = collections.NewPrefix("<%= TypeName.UpperCamel %>/value/")
// <%= TypeName.UpperCamel %>Key is the prefix to retrieve all <%= TypeName.UpperCamel %>
var <%= TypeName.UpperCamel %>Key = collections.NewPrefix("<%= TypeName.UpperCamel %>/value/")
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"<%= ModulePath %>/x/<%= ModuleName %>/types"
"cosmossdk.io/collections"
errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)


Expand All @@ -31,11 +31,11 @@ func (k msgServer) Create<%= TypeName.UpperCamel %>(ctx context.Context, msg *t
<% } %>
}

if err := k.<%= TypeName.UpperCamel %>.Set(ctx, <%= TypeName.LowerCamel %>); err != nil {
if err := k.<%= TypeName.UpperCamel %>.Set(ctx, <%= TypeName.LowerCamel %>.<%= Index.Name.UpperCamel %>, <%= TypeName.LowerCamel %>); err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, err.Error())
}

return &types.MsgCreate<%= TypeName.UpperCamel %>Response{}, nil
return &types.MsgCreate<%= TypeName.UpperCamel %>Response{}, nil
}

func (k msgServer) Update<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgUpdate<%= TypeName.UpperCamel %>) (*types.MsgUpdate<%= TypeName.UpperCamel %>Response, error) {
Expand All @@ -44,7 +44,7 @@ func (k msgServer) Update<%= TypeName.UpperCamel %>(ctx context.Context, msg *t
}

// Check if the value exists
val, err := k.<%= TypeName.UpperCamel %>.Get(ctx, <%= msg.<%= Index.Name.UpperCamel %>)
val, err := k.<%= TypeName.UpperCamel %>.Get(ctx, msg.<%= Index.Name.UpperCamel %>)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, "index not set")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewMsgCreate<%= TypeName.UpperCamel %>(

func NewMsgUpdate<%= TypeName.UpperCamel %>(
<%= MsgSigner.LowerCamel %> string,
<%= Index.Name.LowerCamel %> <%= index.DataType() %>,
<%= Index.Name.LowerCamel %> <%= Index.DataType() %>,
<%= for (field) in Fields { %><%= field.Name.LowerCamel %> <%= field.DataType() %>,
<% } %>
) *MsgUpdate<%= TypeName.UpperCamel %> {
Expand All @@ -30,7 +30,7 @@ func NewMsgUpdate<%= TypeName.UpperCamel %>(

func NewMsgDelete<%= TypeName.UpperCamel %>(
<%= MsgSigner.LowerCamel %> string,
<%= Index.Name.LowerCamel %> <%= index.DataType() %>,
<%= Index.Name.LowerCamel %> <%= Index.DataType() %>,
<% } %>
) *MsgDelete<%= TypeName.UpperCamel %> {
return &MsgDelete<%= TypeName.UpperCamel %>{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) {

srv := keeper.NewMsgServerImpl(k)
expected := &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,
<%= Index.Name.UpperCamel %>: <%= index.ValueIndex() %>,
<%= Index.Name.UpperCamel %>: <%= Index.ValueIndex() %>,
}
_, err = srv.Create<%= TypeName.UpperCamel %>(ctx, expected)
require.NoError(t, err)
Expand All @@ -56,28 +56,28 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) {
{
desc: "invalid address",
request: &types.MsgUpdate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: "invalid",
<%= Index.Name.UpperCamel %>: <%= index.ValueIndex() %>,
<%= Index.Name.UpperCamel %>: <%= Index.ValueIndex() %>,
},
err: sdkerrors.ErrInvalidAddress,
},
{
desc: "unauthorized",
request: &types.MsgUpdate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: unauthorizedAddr,
<%= Index.Name.UpperCamel %>: <%= index.ValueIndex() %>,
<%= Index.Name.UpperCamel %>: <%= Index.ValueIndex() %>,
},
err: sdkerrors.ErrUnauthorized,
},
{
desc: "key not found",
request: &types.MsgUpdate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,
<%= Index.Name.UpperCamel %>: <%= index.ValueInvalidIndex() %>,
<%= Index.Name.UpperCamel %>: <%= Index.ValueInvalidIndex() %>,
},
err: sdkerrors.ErrKeyNotFound,
},
{
desc: "completed",
request: &types.MsgUpdate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,
<%= Index.Name.UpperCamel %>: <%= index.ValueIndex() %>,
<%= Index.Name.UpperCamel %>: <%= Index.ValueIndex() %>,
},
},
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) {

srv := keeper.NewMsgServerImpl(k)
_, err = srv.Create<%= TypeName.UpperCamel %>(ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,
<%= Index.Name.UpperCamel %>: <%= index.ValueIndex() %>,
<%= Index.Name.UpperCamel %>: <%= Index.ValueIndex() %>,
})
require.NoError(t, err)

Expand All @@ -118,28 +118,28 @@ func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) {
{
desc: "invalid address",
request: &types.MsgDelete<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: "invalid",
<%= Index.Name.UpperCamel %>: <%= index.ValueIndex() %>,
<%= Index.Name.UpperCamel %>: <%= Index.ValueIndex() %>,
},
err: sdkerrors.ErrInvalidAddress,
},
{
desc: "unauthorized",
request: &types.MsgDelete<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: unauthorizedAddr,
<%= Index.Name.UpperCamel %>: <%= index.ValueIndex() %>,
<%= Index.Name.UpperCamel %>: <%= Index.ValueIndex() %>,
},
err: sdkerrors.ErrUnauthorized,
},
{
desc: "key not found",
request: &types.MsgDelete<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,
<%= Index.Name.UpperCamel %>: <%= index.ValueInvalidIndex() %>,
<%= Index.Name.UpperCamel %>: <%= Index.ValueInvalidIndex() %>,
},
err: sdkerrors.ErrKeyNotFound,
},
{
desc: "completed",
request: &types.MsgDelete<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,
<%= Index.Name.UpperCamel %>: <%= index.ValueIndex() %>,
<%= Index.Name.UpperCamel %>: <%= Index.ValueIndex() %>,
},
},
}
Expand All @@ -150,7 +150,7 @@ func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) {
require.ErrorIs(t, err, tc.err)
} else {
require.NoError(t, err)
_, err := k.<%= TypeName.UpperCamel %>.Has(ctx, tc.request.<%= Index.Name.UpperCamel %>)
foud, err := k.<%= TypeName.UpperCamel %>.Has(ctx, tc.request.<%= Index.Name.UpperCamel %>)
require.NoError(t, err)
require.False(t, found)
}
Expand Down
4 changes: 2 additions & 2 deletions ignite/templates/typed/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genn
content = replacer.Replace(content, typed.PlaceholderGenesisTypesDefault, replacementTypesDefault)

// lines of code to call the key function with the indexes of the element
keyCall := fmt.Sprintf("%sKey(elem.%s)", opts.TypeName.UpperCamel, opts.Index.Name.UpperCamel)
keyCall := fmt.Sprintf("string(elem.%s)", opts.Index.Name.UpperCamel)

templateTypesValidate := `// Check for duplicated index in %[2]v
%[2]vIndexMap := make(map[string]struct{})
Expand All @@ -381,7 +381,7 @@ for _, elem := range gs.%[3]vList {
typed.PlaceholderGenesisTypesValidate,
opts.TypeName.LowerCamel,
opts.TypeName.UpperCamel,
fmt.Sprintf("string(%s)", keyCall),
keyCall,
)
content = replacer.Replace(content, typed.PlaceholderGenesisTypesValidate, replacementTypesValidate)

Expand Down
4 changes: 2 additions & 2 deletions ignite/templates/typed/map/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func moduleSimulationModify(replacer placeholder.Replacer, opts *typed.Options)
return err
}

// Create a list of two different indexes and fields to use as sample
// Create a list of two different index/fields to use as sample
sampleIndexes := make([]string, 2)
for i := 0; i < 2; i++ {
sampleIndexes[i] = fmt.Sprintf("%s: sample.AccAddress(),\n", opts.MsgSigner.UpperCamel)
sampleIndexes[i] += opts.Index.GenesisArgs(i)
sampleIndexes[i] = opts.Index.GenesisArgs(i)
}

// simulation genesis state
Expand Down
10 changes: 2 additions & 8 deletions integration/map/cmd_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
envtest "github.com/ignite/cli/v29/integration"
)

// TODO verify multi-index isn't supported

func TestCreateMap(t *testing.T) {
var (
env = envtest.New(t)
Expand Down Expand Up @@ -155,14 +157,6 @@ func TestCreateMap(t *testing.T) {
)),
))

env.Must(env.Exec("should prevent creating a map with duplicated indexes",
step.NewSteps(step.New(
step.Exec(envtest.IgniteApp, "s", "map", "--yes", "map_with_duplicated_index", "email", "--index", "foo,foo"),
step.Workdir(app.SourcePath()),
)),
envtest.ExecShouldError(),
))

env.Must(env.Exec("should prevent creating a map with an index present in fields",
step.NewSteps(step.New(
step.Exec(envtest.IgniteApp, "s", "map", "--yes", "map_with_invalid_index", "email", "--index", "email"),
Expand Down
Loading