Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: move evm msg raw field parsing to existing MsgEthereumTx parser #897

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions usecase/parser/msg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
Expand All @@ -9,9 +10,7 @@ import (
"time"

"github.com/crypto-com/chain-indexing/entity/command"
jsoniter "github.com/json-iterator/go"
"github.com/mitchellh/mapstructure"

"github.com/crypto-com/chain-indexing/external/json"
"github.com/crypto-com/chain-indexing/external/primptr"
"github.com/crypto-com/chain-indexing/external/tmcosmosutils"
"github.com/crypto-com/chain-indexing/external/utctime"
Expand All @@ -25,6 +24,10 @@ import (
"github.com/crypto-com/chain-indexing/usecase/parser/icaauth"
"github.com/crypto-com/chain-indexing/usecase/parser/utils"
mapstructure_utils "github.com/crypto-com/chain-indexing/usecase/parser/utils/mapstructure"
eth_types "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
jsoniter "github.com/json-iterator/go"
"github.com/mitchellh/mapstructure"
)

const (
Expand Down Expand Up @@ -2359,14 +2362,53 @@ func ParseMsgEthereumTx(
var commands []command.Command
var possibleSignerAddresses []string

log := utils.NewParsedTxsResultLog(&parserParams.TxsResult.Log[parserParams.MsgIndex])
var input []byte
if rawMsg.Raw != "" {
events := utils.NewParsedTxsResultsEvents(parserParams.TxsResult.Events)
log := events.ParsedEventToTxsResultLog()
parserParams.TxsResult.Log = log

if rawMsg.Data.Data != "" {
inputData, err := base64.StdEncoding.DecodeString(rawMsg.Data.Data)
var rawTxData []byte
rawTxData, err = hex.DecodeString(rawMsg.Raw[2:]) // Remove hex prefix "0x..."
if err != nil {
panic("error decoding raw tx data")
}

// decode the raw tx data
var tx eth_types.Transaction
reader := bytes.NewReader(rawTxData)
err = tx.DecodeRLP(rlp.NewStream(reader, 0))
if err != nil {
err = tx.UnmarshalBinary(rawTxData)
if err != nil {
panic(fmt.Errorf("error unmarshalling tx: %v", err))
}
}

// unmarshal the raw tx data into `decodeRaw` field
var txBytes []byte
txBytes, err = tx.MarshalJSON()
if err != nil {
panic(fmt.Errorf("error marshalling tx: %v", err))
}

var decodedRaw model.DecodedRaw
err = json.Unmarshal(txBytes, &decodedRaw)
if err != nil {
panic(fmt.Errorf("error unmarshalling tx: %v", err))
}
msgEthereumTxParams.DecodedRaw = &decodedRaw
input = tx.Data()
} else if rawMsg.Data.Data != "" {
input, err = base64.StdEncoding.DecodeString(rawMsg.Data.Data)
if err != nil {
panic(fmt.Errorf("error decoding RawMsgEthereumTx.Data.Data: %v", err))
}
}

log := utils.NewParsedTxsResultLog(&parserParams.TxsResult.Log[parserParams.MsgIndex])

if string(input) != "" {
// parse IBC msgChannelOpenInit
if log.HasEvent("channel_open_init") {
events := log.GetEventsByType("channel_open_init")
Expand Down Expand Up @@ -2511,7 +2553,7 @@ func ParseMsgEthereumTx(
// parse msgCreateClient
sendEvents := log.GetEventsByType("create_client")
if len(sendEvents) > 0 {
msg, err := parserParams.EthereumTxInnerMsgDecoder.DecodeCosmosMsgFromTxInput(inputData, MSG_CREATE_CLIENT_TYPE_URL)
msg, err := parserParams.EthereumTxInnerMsgDecoder.DecodeCosmosMsgFromTxInput(input, MSG_CREATE_CLIENT_TYPE_URL)
if err != nil {
panic(fmt.Errorf("error deserializing MsgCreateClient: %v", err))
}
Expand All @@ -2526,7 +2568,7 @@ func ParseMsgEthereumTx(
// parse MsgUpdateClient
sendEvents := log.GetEventsByType("update_client")
if len(sendEvents) > 0 {
msg, err := parserParams.EthereumTxInnerMsgDecoder.DecodeCosmosMsgFromTxInput(inputData, MSG_UPDATE_CLIENT_TYPE_URL)
msg, err := parserParams.EthereumTxInnerMsgDecoder.DecodeCosmosMsgFromTxInput(input, MSG_UPDATE_CLIENT_TYPE_URL)
if err != nil {
panic(fmt.Errorf("error deserializing MsgUpdateClient: %v", err))
}
Expand Down
3 changes: 1 addition & 2 deletions usecase/parser/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ func RegisterBreakingVersionParsers(manager *utils.CosmosParserManager) {
/*
Cronos v1.4.0
*/

if manager.GetCronosV1_4_0BlockHeight() > 0 {
manager.RegisterParser("/ethermint.evm.v1.MsgEthereumTx", manager.GetCronosV1_4_0BlockHeight(), V1_4_0_cronos_msg.ParseMsgEthereumTx)
manager.RegisterParser("/ethermint.evm.v1.MsgEthereumTx", manager.GetCronosV1_4_0BlockHeight(), V1_4_0_cronos_msg.ParseMsgEventsToLog(ParseMsgEthereumTx))

// cosmos bank
manager.RegisterParser("/cosmos.bank.v1beta1.MsgSend", manager.GetCronosV1_4_0BlockHeight(), V1_4_0_cronos_msg.ParseMsgEventsToLog(ParseMsgSend))
Expand Down
Loading
Loading