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

feat/Update log parsing functions to normalize new standard for Osmosis v24 #546

Merged
merged 1 commit into from
Apr 19, 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
16 changes: 16 additions & 0 deletions core/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
"github.com/cosmos/cosmos-sdk/types"
cosmosTx "github.com/cosmos/cosmos-sdk/types/tx"
"gorm.io/gorm"

indexerEvents "github.com/DefiantLabs/cosmos-tax-cli/cosmos/events"
)

// Unmarshal JSON to a particular type. There can be more than one handler for each type.
Expand Down Expand Up @@ -272,6 +274,10 @@ func ProcessRPCBlockByHeightTXs(db *gorm.DB, cl *client.ChainClient, blockResult
// We can entirely ignore failed TXs in downstream parsers, because according to the Cosmos specification, a single failed message in a TX fails the whole TX
if txResult.Code == 0 {
logs, err = types.ParseABCILogs(txResult.Log)

if err != nil {
logs, err = indexerEvents.ParseTxEventsToMessageIndexEvents(len(txFull.Body.Messages), txResult.Events)
}
} else {
err = nil
}
Expand Down Expand Up @@ -344,6 +350,16 @@ func ProcessRPCTXs(db *gorm.DB, cl *client.ChainClient, txEventResp *cosmosTx.Ge
currTx := txEventResp.Txs[txIdx]
currTxResp := txEventResp.TxResponses[txIdx]

if len(currTxResp.Logs) == 0 && len(currTxResp.Events) != 0 {
parsedLogs, err := indexerEvents.ParseTxEventsToMessageIndexEvents(len(currTx.Body.Messages), currTxResp.Events)
if err != nil {
config.Log.Errorf("Error parsing events to message index events to normalize: %v", err)
return nil, blockTime, err
}

currTxResp.Logs = parsedLogs
}

// Get the Messages and Message Logs
for msgIdx := range currTx.Body.Messages {
currMsg := currTx.Body.Messages[msgIdx].GetCachedValue()
Expand Down
89 changes: 89 additions & 0 deletions cosmos/events/normalization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package events

import (
"fmt"
"strconv"

"github.com/DefiantLabs/cosmos-tax-cli/config"
txtypes "github.com/DefiantLabs/cosmos-tax-cli/cosmos/modules/tx"
cometAbciTypes "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/types"
)

func NormalizedAttributesToAttributes(attrs []txtypes.Attribute) []types.Attribute {
list := []types.Attribute{}
for _, attr := range attrs {
lma := types.Attribute{Key: attr.Key, Value: attr.Value}
list = append(list, lma)
}

return list
}

func AttributesToNormalizedAttributes(attrs []types.Attribute) []txtypes.Attribute {
list := []txtypes.Attribute{}
for _, attr := range attrs {
lma := txtypes.Attribute{Key: attr.Key, Value: attr.Value}
list = append(list, lma)
}

return list
}

func EventAttributesToNormalizedAttributes(attrs []cometAbciTypes.EventAttribute) []txtypes.Attribute {
list := []txtypes.Attribute{}
for _, attr := range attrs {
lma := txtypes.Attribute{Key: attr.Key, Value: attr.Value}
list = append(list, lma)
}

return list
}

func StringEventstoNormalizedEvents(msgEvents types.StringEvents) (list []txtypes.LogMessageEvent) {
for _, evt := range msgEvents {
lme := txtypes.LogMessageEvent{Type: evt.Type, Attributes: AttributesToNormalizedAttributes(evt.Attributes)}
list = append(list, lme)
}

return list
}

func toNormalizedEvents(msgEvents []cometAbciTypes.Event) (list []txtypes.LogMessageEvent) {
for _, evt := range msgEvents {
lme := txtypes.LogMessageEvent{Type: evt.Type, Attributes: EventAttributesToNormalizedAttributes(evt.Attributes)}
list = append(list, lme)
}

return list
}

func ParseTxEventsToMessageIndexEvents(numMessages int, events []cometAbciTypes.Event) (types.ABCIMessageLogs, error) {
parsedLogs := make(types.ABCIMessageLogs, numMessages)
for index := range parsedLogs {
parsedLogs[index] = types.ABCIMessageLog{
MsgIndex: uint32(index),
}
}

// TODO: Fix this to be more efficient, no need to translate multiple times to hack this together
logMessageEvents := toNormalizedEvents(events)
for _, event := range logMessageEvents {
loopEvent := event
val, err := txtypes.GetValueForAttribute("msg_index", &loopEvent)

if err == nil && val != "" {
msgIndex, err := strconv.Atoi(val)
if err != nil {
config.Log.Error(fmt.Sprintf("Error parsing msg_index from event: %v", err))
return nil, err
}

if msgIndex >= 0 && msgIndex < len(parsedLogs) {
parsedLogs[msgIndex].Events = append(parsedLogs[msgIndex].Events, types.StringEvent{Type: event.Type, Attributes: NormalizedAttributesToAttributes(event.Attributes)})
}
}
}

return parsedLogs, nil
}
Loading