From d08ddd90b6b8dff45464b8ac4d20660f99c81073 Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Tue, 23 Apr 2024 17:34:45 +0900 Subject: [PATCH 1/3] fix block/nft + printStack when panic --- submodules/block/collect.go | 4 +--- submodules/block/submodule.go | 1 + submodules/move-nft/grpc_query.go | 13 ++++++++----- submodules/wasm-nft/grpc_query.go | 13 ++++++++----- x/kvindexer/keeper/handler.go | 3 +++ 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/submodules/block/collect.go b/submodules/block/collect.go index 4c75327..488ee06 100644 --- a/submodules/block/collect.go +++ b/submodules/block/collect.go @@ -48,10 +48,9 @@ func (bs BlockSubmodule) collectBlock(ctx context.Context, req abci.RequestFinal f := tx.GetFee() feeCoins = feeCoins.Add(f...) } - block.TotalFee = feeCoins + block.TotalFee = feeCoins block.TxCount = int64(len(req.Txs)) - block.GasUsed = 0 block.GasWanted = 0 for tx := range req.Txs { @@ -69,7 +68,6 @@ func (bs BlockSubmodule) collectBlock(ctx context.Context, req abci.RequestFinal block.BlockTime = req.Time.Sub(prevBlock.Timestamp).Milliseconds() } } - if err := bs.blockByHeight.Set(ctx, req.Height, block); err != nil { return errors.Wrap(err, "failed to set block by height") } diff --git a/submodules/block/submodule.go b/submodules/block/submodule.go index 36ba5e4..153976a 100644 --- a/submodules/block/submodule.go +++ b/submodules/block/submodule.go @@ -43,6 +43,7 @@ func NewBlockSubmodule( return &BlockSubmodule{ cdc: cdc, + opChildKeeper: opChildKeeper, blockByHeight: blockByHeight, }, nil } diff --git a/submodules/move-nft/grpc_query.go b/submodules/move-nft/grpc_query.go index 410b9ec..0581518 100644 --- a/submodules/move-nft/grpc_query.go +++ b/submodules/move-nft/grpc_query.go @@ -230,13 +230,16 @@ func (sm MoveNftSubmodule) getTokensByAccountAndCollection(ctx context.Context, ownerSdkAddr := getCosmosAddress(ownerAddr) ownerAddrStr := ownerSdkAddr.String() - res, pageRes, err := query.CollectionPaginate(ctx, sm.tokenMap, req.Pagination, - func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (*nfttypes.IndexedToken, error) { + res, pageRes, err := query.CollectionFilteredPaginate(ctx, sm.tokenMap, req.Pagination, + func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (bool, error) { if slices.Equal(k.K1(), colSdkAddr) && (v.OwnerAddr == ownerAddrStr) { - v.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, v.CollectionName) - return &v, nil + return true, nil } - return nil, nil + return false, nil + }, + func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (*nfttypes.IndexedToken, error) { + v.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, v.CollectionName) + return &v, nil }, ) diff --git a/submodules/wasm-nft/grpc_query.go b/submodules/wasm-nft/grpc_query.go index 5d277b2..6e52903 100644 --- a/submodules/wasm-nft/grpc_query.go +++ b/submodules/wasm-nft/grpc_query.go @@ -229,13 +229,16 @@ func (sm WasmNFTSubmodule) getTokensByAccountAndCollection(ctx context.Context, ownerSdkAddr := getCosmosAddress(ownerAddr) ownerAddrStr := ownerSdkAddr.String() - res, pageRes, err := query.CollectionPaginate(ctx, sm.tokenMap, req.Pagination, - func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (*nfttypes.IndexedToken, error) { + res, pageRes, err := query.CollectionFilteredPaginate(ctx, sm.tokenMap, req.Pagination, + func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (bool, error) { if slices.Equal(k.K1(), colSdkAddr) && (v.OwnerAddr == ownerAddrStr) { - v.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, v.CollectionName) - return &v, nil + return true, nil } - return nil, nil + return false, nil + }, + func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (*nfttypes.IndexedToken, error) { + v.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, v.CollectionName) + return &v, nil }, ) diff --git a/x/kvindexer/keeper/handler.go b/x/kvindexer/keeper/handler.go index 61dfbe5..d863aab 100644 --- a/x/kvindexer/keeper/handler.go +++ b/x/kvindexer/keeper/handler.go @@ -3,6 +3,7 @@ package keeper import ( "context" "fmt" + "runtime/debug" "strings" storetypes "cosmossdk.io/store/types" @@ -68,6 +69,7 @@ func (k *Keeper) HandleFinalizeBlock(ctx context.Context, req abci.RequestFinali defer func() { if err := recover(); err != nil { k.Logger(ctx).Error("panic in HandleFinalizeBlock", "err", err) + debug.PrintStack() } }() @@ -84,6 +86,7 @@ func (k *Keeper) HandleCommit(ctx context.Context, res abci.ResponseCommit, chan defer func() { if err := recover(); err != nil { k.Logger(ctx).Error("panic in HandleCommit", "err", err) + debug.PrintStack() } }() From a0bb57ec035edc9eca296ff746f6e7bf7ad60d35 Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Wed, 24 Apr 2024 18:19:58 +0900 Subject: [PATCH 2/3] fix tag for nftevent --- submodules/pair/types/types.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/submodules/pair/types/types.go b/submodules/pair/types/types.go index 014c4b9..84ee452 100644 --- a/submodules/pair/types/types.go +++ b/submodules/pair/types/types.go @@ -48,9 +48,9 @@ type NftClassData struct { } type WriteAckForNftEvent struct { - ClassData string `json:"classData"` - ClassId string `json:"classId"` - ClassUri string `json:"classUri"` + ClassData string `json:"class_data"` + ClassId string `json:"class_id"` + ClassUri string `json:"class_uri"` Receiver sdk.AccAddress `json:"receiver"` Sender sdk.AccAddress `json:"sender"` //TokenData []string `json:"tokenData"` From 1aadbcf2364a81a052b05a4c18a48e9f53976809 Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Thu, 25 Apr 2024 15:44:01 +0900 Subject: [PATCH 3/3] fix pair to collect name instead of description --- submodules/pair/collect.go | 137 +++++++++++++++++++-------------- submodules/pair/types/types.go | 13 +++- 2 files changed, 87 insertions(+), 63 deletions(-) diff --git a/submodules/pair/collect.go b/submodules/pair/collect.go index 322bb35..cbe04a0 100644 --- a/submodules/pair/collect.go +++ b/submodules/pair/collect.go @@ -13,6 +13,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/tx" + channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" exportedibc "github.com/cosmos/ibc-go/v8/modules/core/exported" ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint" @@ -27,17 +28,30 @@ const ( func (sm PairSubmodule) finalizeBlock(ctx context.Context, req abci.RequestFinalizeBlock, res abci.ResponseFinalizeBlock) error { sm.Logger(ctx).Debug("finalizeBlock", "submodule", types.SubmoduleName, "txs", len(req.Txs), "height", req.Height) - if err := sm.collectOPfungibleTokens(ctx, req); err != nil { - sm.Logger(ctx).Warn("collectOPfungibleTokens", "error", err, "submodule", types.SubmoduleName) - } - if err := sm.collectIBCFungibleTokens(ctx); err != nil { // don't return error sm.Logger(ctx).Warn("collectIBCFungibleTokens", "error", err, "submodule", types.SubmoduleName) } - if err := sm.collectIBCNonfungibleTokens(ctx, res); err != nil { - sm.Logger(ctx).Warn("collectIBCNonfungibleTokens", "error", err, "submodule", types.SubmoduleName) + for txIdx, txBytes := range req.Txs { + tx, err := sm.parseTx(txBytes) + if err != nil { + return err + } + for _, msg := range tx.GetMsgs() { + switch msg := msg.(type) { + case *opchildtypes.MsgFinalizeTokenDeposit: + err = sm.collectOPfungibleTokens(ctx, msg) + if err != nil { + sm.Logger(ctx).Warn("collectOPfungibleTokens", "error", err) + } + case *channeltypes.MsgRecvPacket: + err = sm.collectIBCNonfungibleTokens(ctx, res.TxResults[txIdx]) + if err != nil { + sm.Logger(ctx).Warn("collectIBCNonfungibleTokens", "error", err) + } + } + } } return nil @@ -53,78 +67,83 @@ func (sm PairSubmodule) parseTx(txBytes []byte) (*tx.Tx, error) { return &tx, nil } -func (sm PairSubmodule) collectOPfungibleTokens(ctx context.Context, req abci.RequestFinalizeBlock) (err error) { - for _, txBytes := range req.Txs { - tx, err := sm.parseTx(txBytes) - if err != nil { - return err - } - for _, msg := range tx.GetMsgs() { - targetMsg, ok := msg.(*opchildtypes.MsgFinalizeTokenDeposit) - if !ok { - continue - } - err := sm.SetPair(ctx, false, true, targetMsg.Amount.Denom, targetMsg.BaseDenom) - if err != nil { - sm.Logger(ctx).Warn("SetPair", "error", err, "denom", targetMsg.Amount.Denom, "baseDenom", targetMsg.BaseDenom) - } - } +func (sm PairSubmodule) collectOPfungibleTokens(ctx context.Context, msg *opchildtypes.MsgFinalizeTokenDeposit) (err error) { + err = sm.SetPair(ctx, false, true, msg.Amount.Denom, msg.BaseDenom) + if err != nil { + sm.Logger(ctx).Warn("SetPair", "error", err, "denom", msg.Amount.Denom, "baseDenom", msg.BaseDenom) } return nil } -func (sm PairSubmodule) collectIBCNonfungibleTokens(ctx context.Context, res abci.ResponseFinalizeBlock) (err error) { - for _, txResult := range res.TxResults { - for _, event := range txResult.Events { - if event.Type != "write_acknowledgement" { - continue - } - err := sm.handleWriteAcknowledgementEvent(ctx, event.Attributes) +func (sm PairSubmodule) collectIBCNonfungibleTokens(ctx context.Context, txResult *abci.ExecTxResult) (err error) { + var packetData, classId string + + for _, event := range txResult.Events { + switch event.Type { + case "recv_packet": + packetData = sm.pickAttribute(event.Attributes, "packet_data") + case "class_trace": + classId = sm.pickAttribute(event.Attributes, "class_id") if err != nil { - sm.Logger(ctx).Warn("failed to handle write_acknowledgement event", "error", err, "event", event) + sm.Logger(ctx).Warn("failed to handle class_trace event", "error", err, "event", event) } } + if packetData != "" && classId != "" { + break + } } + if packetData == "" || classId == "" { + return nil + } + err = sm.pricessPairEvent(ctx, packetData, classId) + if err != nil { + sm.Logger(ctx).Warn("failed to handle recv_packet event", "error", err, "recv_packet.packet_data", packetData, "class_trace.class_id", classId) + } + return nil } -func (sm PairSubmodule) handleWriteAcknowledgementEvent(ctx context.Context, attrs []abci.EventAttribute) (err error) { - sm.Logger(ctx).Debug("write-ack", "attrs", attrs) +func (sm PairSubmodule) pickAttribute(attrs []abci.EventAttribute, key string) string { for _, attr := range attrs { - if attr.Key != "packet_data" { - continue + if attr.Key == key { + return attr.Value } + } + return "" +} - data := types.WriteAckForNftEvent{} - if err = json.Unmarshal([]byte(attr.Value), &data); err != nil { - // may be not target - return nil - } +func (sm PairSubmodule) pricessPairEvent(ctx context.Context, packetDataStr, classId string) (err error) { + sm.Logger(ctx).Debug("processPairEvent", "packet_data", packetDataStr, "class_id", classId) - cdb, err := base64.StdEncoding.DecodeString(data.ClassData) - if err != nil { - return cosmoserr.Wrap(err, "failed to decode class data") - } - classData := types.NftClassData{} - if err = json.Unmarshal(cdb, &classData); err != nil { - return cosmoserr.Wrap(err, "failed to unmarshal class data") - } + packetData := types.PacketData{} + if err = json.Unmarshal([]byte(packetDataStr), &packetData); err != nil { + // may be not target + return nil + } - _, err = sm.GetPair(ctx, false, data.ClassId) - if err == nil { - return nil // already exists - } - if !cosmoserr.IsOf(err, collections.ErrNotFound) { - return cosmoserr.Wrap(err, "failed to check class existence") - } + cdb, err := base64.StdEncoding.DecodeString(packetData.ClassData) + if err != nil { + return cosmoserr.Wrap(err, "failed to decode class data") + } + classData := types.NftClassData{} + if err = json.Unmarshal(cdb, &classData); err != nil { + return cosmoserr.Wrap(err, "failed to unmarshal class data") + } - err = sm.SetPair(ctx, false, false, data.ClassId, classData.Description.Value) - if err != nil { - return cosmoserr.Wrap(err, "failed to set class") - } + _, err = sm.GetPair(ctx, false, classId) + if err == nil { + return nil // already exists + } + if !cosmoserr.IsOf(err, collections.ErrNotFound) { + return cosmoserr.Wrap(err, "failed to check class existence") + } - sm.Logger(ctx).Info("nft class added", "classId", data.ClassId, "description", classData.Description.Value) + err = sm.SetPair(ctx, false, false, classId, classData.Name) + if err != nil { + return cosmoserr.Wrap(err, "failed to set class") } + + sm.Logger(ctx).Info("nft class added", "classId", classId, "classData", classData) return nil } diff --git a/submodules/pair/types/types.go b/submodules/pair/types/types.go index 84ee452..212e506 100644 --- a/submodules/pair/types/types.go +++ b/submodules/pair/types/types.go @@ -42,12 +42,11 @@ type MoveResource struct { } type NftClassData struct { - Description struct { - Value string `json:"value"` - } `json:"initia:description"` + Description string `json:"description"` + Name string `json:"name"` } -type WriteAckForNftEvent struct { +type PacketData struct { ClassData string `json:"class_data"` ClassId string `json:"class_id"` ClassUri string `json:"class_uri"` @@ -57,3 +56,9 @@ type WriteAckForNftEvent struct { //TokenIds []string `json:"tokenIds"` //TokenUris []string `json:"tokenUris"` } + +type ClassTrace struct { + TraceHash string `json:"trace_hash"` + ClassId string `json:"class_id"` + //MsgIndex int `json:"msg_index"` +}