Skip to content

Commit

Permalink
move raw data field parsing to existing MsgEthereumTx parser
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentysc committed Dec 4, 2024
1 parent d329110 commit 20df578
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 303 deletions.
57 changes: 49 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,52 @@ 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)
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 +2552,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 +2567,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

0 comments on commit 20df578

Please sign in to comment.