From 97a15cfbb19af9de85d71665bb77453a98efd887 Mon Sep 17 00:00:00 2001 From: Magic Cat <37407870+MonikaCat@users.noreply.github.com> Date: Thu, 6 Apr 2023 14:50:26 +0700 Subject: [PATCH] feat: decode IBC transfer data object to JSON for `MsgRecvPacket` (#93) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Fixes [BDU-894](https://forbole.atlassian.net/jira/software/c/projects/BDU/issues/BDU-894) and [BDU-446](https://forbole.atlassian.net/jira/software/c/projects/BDU/issues/BDU-446) Changes: - Decode transfer `data` object to JSON, append it to the message and store it inside `message` table for `/ibc.core.channel.v1.MsgRecvPacket` ## Checklist - [x] Targeted PR against correct branch. - [x] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [ ] Wrote unit tests. - [x] Re-reviewed `Files changed` in the Github PR explorer. [BDU-894]: https://forbole.atlassian.net/browse/BDU-894?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ [BDU-446]: https://forbole.atlassian.net/browse/BDU-446?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: Riccardo Montagnin Co-authored-by: Mai Quang Hiep --- CHANGELOG.md | 5 +++++ cmd/start/cmd.go | 2 +- modules/messages/message_handler.go | 17 +++++++++++++++++ modules/messages/utils.go | 11 +++++++++++ modules/module.go | 3 +-- parser/worker.go | 3 +-- types/cosmos.go | 3 +-- 7 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 modules/messages/utils.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 28c1cde9..d69e0262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## Unreleased +### Changes +- ([\#93](https://github.com/forbole/juno/pull/93)) Decode IBC transfer data to JSON for `/ibc.core.channel.v1.MsgRecvPacket` message + + ## v4.1.0 ### Changes - ([\#74](https://github.com/forbole/juno/pull/74)) Applied `GetMissingHeights()` in `enqueueMissingBlocks()` & in `parse blocks missing` cmd diff --git a/cmd/start/cmd.go b/cmd/start/cmd.go index 2c0674a4..55ceca91 100644 --- a/cmd/start/cmd.go +++ b/cmd/start/cmd.go @@ -8,6 +8,7 @@ import ( "time" parsecmdtypes "github.com/forbole/juno/v4/cmd/parse/types" + "github.com/forbole/juno/v4/modules" "github.com/forbole/juno/v4/types/utils" "github.com/forbole/juno/v4/logging" @@ -16,7 +17,6 @@ import ( "github.com/go-co-op/gocron" - "github.com/forbole/juno/v4/modules" "github.com/forbole/juno/v4/parser" "github.com/forbole/juno/v4/types" diff --git a/modules/messages/message_handler.go b/modules/messages/message_handler.go index 784f5b40..f0699409 100644 --- a/modules/messages/message_handler.go +++ b/modules/messages/message_handler.go @@ -1,8 +1,11 @@ package messages import ( + "fmt" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types" "github.com/gogo/protobuf/proto" "github.com/forbole/juno/v4/database" @@ -27,6 +30,20 @@ func HandleMsg( return err } + // Handle MsgRecvPacket data object + if msgIBC, ok := msg.(*channeltypes.MsgRecvPacket); ok { + trimMessageString := TrimLastChar(string(bz)) + trimDataString := string(msgIBC.Packet.Data)[1:] + return db.SaveMessage(types.NewMessage( + tx.TxHash, + index, + proto.MessageName(msg), + fmt.Sprintf("%s,%s", trimMessageString, trimDataString), + addresses, + tx.Height, + )) + } + return db.SaveMessage(types.NewMessage( tx.TxHash, index, diff --git a/modules/messages/utils.go b/modules/messages/utils.go new file mode 100644 index 00000000..99d27ef4 --- /dev/null +++ b/modules/messages/utils.go @@ -0,0 +1,11 @@ +package messages + +import "unicode/utf8" + +func TrimLastChar(s string) string { + r, size := utf8.DecodeLastRuneInString(s) + if r == utf8.RuneError && (size == 0 || size == 1) { + size = 0 + } + return s[:len(s)-size] +} diff --git a/modules/module.go b/modules/module.go index 39c70ec8..2a29545e 100644 --- a/modules/module.go +++ b/modules/module.go @@ -7,11 +7,10 @@ import ( "github.com/cosmos/cosmos-sdk/x/authz" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/forbole/juno/v4/types" "github.com/go-co-op/gocron" tmctypes "github.com/tendermint/tendermint/rpc/core/types" tmtypes "github.com/tendermint/tendermint/types" - - "github.com/forbole/juno/v4/types" ) // Module represents a generic module without any particular handling of data diff --git a/parser/worker.go b/parser/worker.go index de50f732..2637c424 100644 --- a/parser/worker.go +++ b/parser/worker.go @@ -14,9 +14,8 @@ import ( "github.com/forbole/juno/v4/database" "github.com/forbole/juno/v4/types/config" - "github.com/forbole/juno/v4/modules" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/forbole/juno/v4/modules" tmctypes "github.com/tendermint/tendermint/rpc/core/types" tmtypes "github.com/tendermint/tendermint/types" diff --git a/types/cosmos.go b/types/cosmos.go index 975128f8..6aebc25f 100644 --- a/types/cosmos.go +++ b/types/cosmos.go @@ -4,10 +4,9 @@ import ( "fmt" "time" - tmctypes "github.com/tendermint/tendermint/rpc/core/types" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/tx" + tmctypes "github.com/tendermint/tendermint/rpc/core/types" ) // Validator contains the data of a single validator