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: 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 diff --git a/Makefile b/Makefile index 31a2783..a0c8f83 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 @@ -101,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/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/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/go.mod b/go.mod index fc71c06..3e24fe3 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/initia-labs/kvindexer -go 1.22.7 - -toolchain go1.23.0 +go 1.23.3 require ( cosmossdk.io/collections v0.4.0 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 ) 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/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/collect.go b/submodules/move-nft/collect.go index d3773a3..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 { @@ -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/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/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/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/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/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 -} 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 { 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 }