From 9c3a40945f5b1e56fe56b0ba6a6e15c49a861f1b Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Wed, 20 Nov 2024 16:02:07 +0900 Subject: [PATCH 1/8] minor refactor --- Makefile | 1 - collection/option.go | 23 +++++++++++++++++++++++ submodules/evm-nft/grpc_query.go | 21 +++------------------ submodules/move-nft/grpc_query.go | 19 +++---------------- submodules/wasm-nft/grpc_query.go | 21 +++------------------ types.go | 8 -------- 6 files changed, 32 insertions(+), 61 deletions(-) create mode 100644 collection/option.go diff --git a/Makefile b/Makefile index 31a2783..4469272 100644 --- a/Makefile +++ b/Makefile @@ -65,7 +65,6 @@ comma := , build_tags_comma_sep := $(subst $(whitespace),$(comma),$(build_tags)) # process linker flags -# TODO: ldflags = # DB backend selection diff --git a/collection/option.go b/collection/option.go new file mode 100644 index 0000000..d4068c5 --- /dev/null +++ b/collection/option.go @@ -0,0 +1,23 @@ +package collection + +import ( + "cosmossdk.io/collections" + "github.com/cosmos/cosmos-sdk/types/query" +) + +// WithCollectionPaginationTriplePrefix applies a prefix to a collection, whose key is a collection.Triple, +// being paginated that needs prefixing. +func WithCollectionPaginationTriplePrefix[K1, K2, K3 any](prefix K1) func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { + return func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { + prefix := collections.TriplePrefix[K1, K2, K3](prefix) + o.Prefix = &prefix + } +} + +// WithCollectionPaginationTriplePrefix2 is similar to WithCollectionPaginationTriplePrefix, but it takes two prefixes. +func WithCollectionPaginationTriplePrefix2[K1, K2, K3 any](prefix K1, prefix2 K2) func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { + return func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { + prefix := collections.TripleSuperPrefix[K1, K2, K3](prefix, prefix2) + o.Prefix = &prefix + } +} diff --git a/submodules/evm-nft/grpc_query.go b/submodules/evm-nft/grpc_query.go index 940e7b7..4953649 100644 --- a/submodules/evm-nft/grpc_query.go +++ b/submodules/evm-nft/grpc_query.go @@ -12,6 +12,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + kvcollection "github.com/initia-labs/kvindexer/collection" nfttypes "github.com/initia-labs/kvindexer/nft/types" ) @@ -178,7 +179,7 @@ func (sm EvmNFTSubmodule) getTokensByAccount(ctx context.Context, req *nfttypes. identifiers = append(identifiers, collections.Join(k.K2(), k.K3())) return v, nil }, - WithCollectionPaginationTriplePrefix[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr), + kvcollection.WithCollectionPaginationTriplePrefix[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr), ) if err != nil { return nil, handleCollectionErr(err) @@ -221,7 +222,7 @@ func (sm EvmNFTSubmodule) getTokensByAccountAndCollection(ctx context.Context, r identifiers = append(identifiers, collections.Join(k.K2(), k.K3())) return v, nil }, - WithCollectionPaginationTriplePrefix2[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr, colSdkAddr), + kvcollection.WithCollectionPaginationTriplePrefix2[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr, colSdkAddr), ) if err != nil { return nil, handleCollectionErr(err) @@ -273,19 +274,3 @@ func (sm EvmNFTSubmodule) getTokensByAccountCollectionAndTokenId(ctx context.Con Tokens: []*nfttypes.IndexedToken{&token}, }, nil } - -// WithCollectionPaginationTriplePrefix applies a prefix to a collection, whose key is a collection.Triple, -// being paginated that needs prefixing. -func WithCollectionPaginationTriplePrefix[K1, K2, K3 any](prefix K1) func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - return func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - prefix := collections.TriplePrefix[K1, K2, K3](prefix) - o.Prefix = &prefix - } -} - -func WithCollectionPaginationTriplePrefix2[K1, K2, K3 any](prefix K1, prefix2 K2) func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - return func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - prefix := collections.TripleSuperPrefix[K1, K2, K3](prefix, prefix2) - o.Prefix = &prefix - } -} diff --git a/submodules/move-nft/grpc_query.go b/submodules/move-nft/grpc_query.go index ab35e38..af7cb48 100644 --- a/submodules/move-nft/grpc_query.go +++ b/submodules/move-nft/grpc_query.go @@ -12,6 +12,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + kvcollection "github.com/initia-labs/kvindexer/collection" nfttypes "github.com/initia-labs/kvindexer/nft/types" ) @@ -183,7 +184,7 @@ func (sm MoveNftSubmodule) getTokensByAccount(ctx context.Context, req *nfttypes identifiers = append(identifiers, collections.Join(k.K2(), k.K3())) return v, nil }, - WithCollectionPaginationTriplePrefix[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr), + kvcollection.WithCollectionPaginationTriplePrefix[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr), ) if err != nil { return nil, handleCollectionErr(err) @@ -228,7 +229,7 @@ func (sm MoveNftSubmodule) getTokensByAccountAndCollection(ctx context.Context, identifiers = append(identifiers, collections.Join(k.K2(), k.K3())) return v, nil }, - WithCollectionPaginationTriplePrefix2[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr, colSdkAddr), + kvcollection.WithCollectionPaginationTriplePrefix2[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr, colSdkAddr), ) if err != nil { return nil, handleCollectionErr(err) @@ -281,17 +282,3 @@ func (sm MoveNftSubmodule) getTokensByAccountCollectionAndTokenId(ctx context.Co Tokens: []*nfttypes.IndexedToken{&token}, }, nil } - -func WithCollectionPaginationTriplePrefix[K1, K2, K3 any](prefix K1) func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - return func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - prefix := collections.TriplePrefix[K1, K2, K3](prefix) - o.Prefix = &prefix - } -} - -func WithCollectionPaginationTriplePrefix2[K1, K2, K3 any](prefix K1, prefix2 K2) func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - return func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - prefix := collections.TripleSuperPrefix[K1, K2, K3](prefix, prefix2) - o.Prefix = &prefix - } -} diff --git a/submodules/wasm-nft/grpc_query.go b/submodules/wasm-nft/grpc_query.go index 84271df..131c4af 100644 --- a/submodules/wasm-nft/grpc_query.go +++ b/submodules/wasm-nft/grpc_query.go @@ -12,6 +12,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + kvcollection "github.com/initia-labs/kvindexer/collection" nfttypes "github.com/initia-labs/kvindexer/nft/types" ) @@ -182,7 +183,7 @@ func (sm WasmNFTSubmodule) getTokensByAccount(ctx context.Context, req *nfttypes identifiers = append(identifiers, collections.Join(k.K2(), k.K3())) return v, nil }, - WithCollectionPaginationTriplePrefix[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr), + kvcollection.WithCollectionPaginationTriplePrefix[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr), ) if err != nil { return nil, handleCollectionErr(err) @@ -227,7 +228,7 @@ func (sm WasmNFTSubmodule) getTokensByAccountAndCollection(ctx context.Context, identifiers = append(identifiers, collections.Join(k.K2(), k.K3())) return v, nil }, - WithCollectionPaginationTriplePrefix2[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr, colSdkAddr), + kvcollection.WithCollectionPaginationTriplePrefix2[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr, colSdkAddr), ) if err != nil { return nil, handleCollectionErr(err) @@ -280,19 +281,3 @@ func (sm WasmNFTSubmodule) getTokensByAccountCollectionAndTokenId(ctx context.Co Tokens: []*nfttypes.IndexedToken{&token}, }, nil } - -// WithCollectionPaginationTriplePrefix applies a prefix to a collection, whose key is a collection.Triple, -// being paginated that needs prefixing. -func WithCollectionPaginationTriplePrefix[K1, K2, K3 any](prefix K1) func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - return func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - prefix := collections.TriplePrefix[K1, K2, K3](prefix) - o.Prefix = &prefix - } -} - -func WithCollectionPaginationTriplePrefix2[K1, K2, K3 any](prefix K1, prefix2 K2) func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - return func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) { - prefix := collections.TripleSuperPrefix[K1, K2, K3](prefix, prefix2) - o.Prefix = &prefix - } -} diff --git a/types.go b/types.go index f23f6c2..26d4815 100644 --- a/types.go +++ b/types.go @@ -2,8 +2,6 @@ package indexer import ( "cosmossdk.io/log" - storetypes "cosmossdk.io/store/types" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/initia-labs/kvindexer/config" "github.com/initia-labs/kvindexer/x/kvindexer/keeper" ) @@ -13,9 +11,3 @@ type Indexer struct { keeper *keeper.Keeper logger log.Logger } - -type IndexableApplication interface { - GetIndexerKeeper() *keeper.Keeper - GetBaseApp() *baseapp.BaseApp - GetKeys() map[string]*storetypes.KVStoreKey -} From 31c8437e191d5b68b46ef18916ba4e5e4f86d23c Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Wed, 20 Nov 2024 16:20:17 +0900 Subject: [PATCH 2/8] cleanse comments --- Makefile | 4 ---- config/types.go | 11 +++++++++-- indexer.go | 1 - submodules/move-nft/collect.go | 1 - submodules/move-nft/store.go | 1 - submodules/pair/collect.go | 17 ++++------------- submodules/wasm-nft/types/types.go | 4 +--- x/kvindexer/module.go | 1 - 8 files changed, 14 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 4469272..a0c8f83 100644 --- a/Makefile +++ b/Makefile @@ -100,10 +100,6 @@ ifeq (,$(findstring nostrip,$(COSMOS_BUILD_OPTIONS))) BUILD_FLAGS += -trimpath endif -# The below include contains the tools and runsim targets. -# FIXME: uncomment the line below -#include contrib/devtools/Makefile - all: lint test diff --git a/config/types.go b/config/types.go index 70f56ab..8d9ddb1 100644 --- a/config/types.go +++ b/config/types.go @@ -4,9 +4,16 @@ import ( "github.com/spf13/viper" ) +// IndexerConfig defines the configuration options for the kvindexer. type IndexerConfig struct { - Enable bool `mapstructure:"indexer.enable"` - CacheCapacity int `mapstructure:"indexer.cache-capacity"` + // Enable defines whether the kvindexer is enabled. + Enable bool `mapstructure:"indexer.enable"` + // CacheCapacity defines the size of the cache used by the kvindexer. (unit: MiB) + CacheCapacity int `mapstructure:"indexer.cache-capacity"` + // Backend defines the type of the backend store and its options. + // It should have a key-value pair named 'type', and the value should exist in store supported by cosmos-db. + // Recommend to use default value unless you know about backend db storage. + // NOTE: "goleveldb" is the only supported type in the current version. BackendConfig *viper.Viper `mapstructure:"indexer.backend"` } diff --git a/indexer.go b/indexer.go index 4e603a7..10cea6a 100644 --- a/indexer.go +++ b/indexer.go @@ -90,7 +90,6 @@ func (i Indexer) ListenCommit(ctx context.Context, res abci.ResponseCommit, chan if err != nil { i.logger.Error("failed to handle commit", "err", err) } - //err = i.keeper.WriteStore() return err } diff --git a/submodules/move-nft/collect.go b/submodules/move-nft/collect.go index d3773a3..f28df51 100644 --- a/submodules/move-nft/collect.go +++ b/submodules/move-nft/collect.go @@ -221,7 +221,6 @@ func (sm MoveNftSubmodule) handleMutateEvent(ctx context.Context, event types.Ev // remove the nft from the sender's collection tpk, err := sm.tokenMap.Indexes.TokenAddress.MatchExact(ctx, objectSdkAddr) - //objectKey, err := nftByOwner.TokenAddress.MatchExact(ctx, objectSdkAddr) if err != nil { return errors.New("object key not found") } diff --git a/submodules/move-nft/store.go b/submodules/move-nft/store.go index 80b993e..8bd5a04 100644 --- a/submodules/move-nft/store.go +++ b/submodules/move-nft/store.go @@ -28,7 +28,6 @@ type TokenIndex struct { func (i TokenIndex) IndexesList() []collections.Index[collections.Pair[sdk.AccAddress, string], nfttypes.IndexedToken] { return []collections.Index[collections.Pair[sdk.AccAddress, string], nfttypes.IndexedToken]{ i.TokenAddress, - //i.OwnerAddress, } } diff --git a/submodules/pair/collect.go b/submodules/pair/collect.go index 6b95899..8e6ce39 100644 --- a/submodules/pair/collect.go +++ b/submodules/pair/collect.go @@ -104,10 +104,6 @@ func (sm PairSubmodule) pickAttribute(attrs []abci.EventAttribute, key string) s return "" } -func (sm PairSubmodule) generateCw721FromIcs721PortInfo(port, channel string) string { - return port + "/" + channel -} - func (sm PairSubmodule) pricessIbcNftPairEvent(ctx context.Context, packetDataStr, classId string) (err error) { packetData := types.PacketData{} @@ -129,15 +125,6 @@ func (sm PairSubmodule) pricessIbcNftPairEvent(ctx context.Context, packetDataSt return cosmoserr.Wrap(err, "failed to unmarshal class data") } - // block this part overwrite to recent - // _, err = sm.GetPair(ctx, false, packetData.ClassId) - // if err == nil { - // return nil // already exists - // } - // if !cosmoserr.IsOf(err, collections.ErrNotFound) { - // return cosmoserr.Wrap(err, "failed to check class existence") - // } - err = sm.SetPair(ctx, false, false, classId, classData.Name) if err != nil { return cosmoserr.Wrap(err, "failed to set class") @@ -178,3 +165,7 @@ func getChainIdFromClientState(csi exportedibc.ClientState) string { } return cs.ChainId } + +func (sm PairSubmodule) generateCw721FromIcs721PortInfo(port, channel string) string { + return port + "/" + channel +} diff --git a/submodules/wasm-nft/types/types.go b/submodules/wasm-nft/types/types.go index 51981fc..4c9e318 100644 --- a/submodules/wasm-nft/types/types.go +++ b/submodules/wasm-nft/types/types.go @@ -14,8 +14,6 @@ type EventWithAttributeMap struct { type CollectionResource struct { Type string `json:"type,omitempty"` Collection nfttypes.Collection `json:"data"` - // from here is additional fields, not original collection data - //ObjectAddr string `json:"object_addr,omitempty"` } // internal use only: struct from move resource @@ -51,5 +49,5 @@ type OwnerOf struct { type Approval struct { Spender string `json:"spender"` - Expiration uint64 `json:"expiration"` // FIXME: height? timestamp? + Expiration uint64 `json:"expiration"` } diff --git a/x/kvindexer/module.go b/x/kvindexer/module.go index ce58433..940ff83 100644 --- a/x/kvindexer/module.go +++ b/x/kvindexer/module.go @@ -19,7 +19,6 @@ var ( // AppModuleBasic defines the basic application module used by the move module. type AppModuleBasic struct { - //cdc codec.Codec keeper *keeper.Keeper } From 9bc816ff45100fdb6dc09a73a1cb8fd5e3e9593d Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Thu, 21 Nov 2024 10:54:10 +0900 Subject: [PATCH 3/8] fix error --- submodules/move-nft/collect.go | 2 +- x/kvindexer/keeper/handler.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/submodules/move-nft/collect.go b/submodules/move-nft/collect.go index f28df51..5ac5b70 100644 --- a/submodules/move-nft/collect.go +++ b/submodules/move-nft/collect.go @@ -95,7 +95,7 @@ func (sm MoveNftSubmodule) handleMintEvent(ctx context.Context, event types.Even _, err = sm.collectionMap.Get(ctx, collectionSdkAddr) if err != nil { if !cosmoserr.IsOf(err, collections.ErrNotFound) { - return errors.New("") + return err } err = sm.collectionMap.Set(ctx, collectionSdkAddr, *collection) if err != nil { diff --git a/x/kvindexer/keeper/handler.go b/x/kvindexer/keeper/handler.go index d863aab..2ad7e1c 100644 --- a/x/kvindexer/keeper/handler.go +++ b/x/kvindexer/keeper/handler.go @@ -65,6 +65,7 @@ func (k *Keeper) RegisterSubmodules(submodules ...types.Submodule) error { return nil } +// HandleFinalizeBlock processes the FinalizeBlock event for all submodules. func (k *Keeper) HandleFinalizeBlock(ctx context.Context, req abci.RequestFinalizeBlock, res abci.ResponseFinalizeBlock) (err error) { defer func() { if err := recover(); err != nil { From a4870a2ab8ac611cc3945fdbf2219017da4c6d29 Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Tue, 3 Dec 2024 19:13:26 +0900 Subject: [PATCH 4/8] update go version --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index fc71c06..d4ac72d 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/initia-labs/kvindexer -go 1.22.7 +go 1.23.3 toolchain go1.23.0 From 33d4327bb32c5f33fc0758d547182eb69de10344 Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Tue, 3 Dec 2024 19:15:13 +0900 Subject: [PATCH 5/8] go work use --- go.work | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.work b/go.work index e0b90a5..f0294c9 100644 --- a/go.work +++ b/go.work @@ -1,4 +1,4 @@ -go 1.23 +go 1.23.3 use ( . @@ -6,7 +6,7 @@ use ( ./submodules/evm-nft ./submodules/move-nft ./submodules/pair + ./submodules/tx ./submodules/wasm-nft ./submodules/wasm-pair - ./submodules/tx ) From 147bab9833e5b702703aef1ae6470ee078b2eadc Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Tue, 3 Dec 2024 19:16:38 +0900 Subject: [PATCH 6/8] tidy --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index d4ac72d..3e24fe3 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/initia-labs/kvindexer go 1.23.3 -toolchain go1.23.0 - require ( cosmossdk.io/collections v0.4.0 cosmossdk.io/core v0.11.0 From a48617f2fcec34dcef8a6ce0c957e0793016e887 Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Tue, 3 Dec 2024 19:23:24 +0900 Subject: [PATCH 7/8] bump golangci-lint --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 606d44e..6004572 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -26,7 +26,7 @@ jobs: # for private repo access GOPRIVATE: github.com/initia-labs GITHUB_ACCESS_TOKEN: ${{ secrets.GH_READ_TOKEN }} - GOLANGCI_LINT_VERSION: v1.59.1 + GOLANGCI_LINT_VERSION: v1.62.2 name: golangci-lint runs-on: ubuntu-latest steps: From 396f9c761723746c7610a05dc24c6521f037e418 Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Tue, 3 Dec 2024 19:26:30 +0900 Subject: [PATCH 8/8] chore: replace linter The linter 'exportloopref' is deprecated (since v1.60.2) due to: Since Go1.22 (loopvar) this linter is no longer relevant. Replaced by copyloopvar. --- .golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 537af74..3877fbd 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,7 +5,7 @@ linters: - dogsled - dupl - errcheck - - exportloopref + - copyloopvar - goconst - gofmt - goimports