Skip to content

Commit

Permalink
Merge pull request #37 from initia-labs/fix/submodules
Browse files Browse the repository at this point in the history
fix: submodules
  • Loading branch information
SeUkKim authored Apr 25, 2024
2 parents 64a5325 + 1aadbcf commit e48d0bf
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 79 deletions.
4 changes: 1 addition & 3 deletions submodules/block/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions submodules/block/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewBlockSubmodule(

return &BlockSubmodule{
cdc: cdc,
opChildKeeper: opChildKeeper,
blockByHeight: blockByHeight,
}, nil
}
Expand Down
13 changes: 8 additions & 5 deletions submodules/move-nft/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
)

Expand Down
137 changes: 78 additions & 59 deletions submodules/pair/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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
}

Expand Down
19 changes: 12 additions & 7 deletions submodules/pair/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,23 @@ 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 {
ClassData string `json:"classData"`
ClassId string `json:"classId"`
ClassUri string `json:"classUri"`
type PacketData struct {
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"`
//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"`
}
13 changes: 8 additions & 5 deletions submodules/wasm-nft/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
)

Expand Down
3 changes: 3 additions & 0 deletions x/kvindexer/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"fmt"
"runtime/debug"
"strings"

storetypes "cosmossdk.io/store/types"
Expand Down Expand Up @@ -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()
}
}()

Expand All @@ -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()
}
}()

Expand Down

0 comments on commit e48d0bf

Please sign in to comment.