|
| 1 | +package events |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + abci "github.com/cometbft/cometbft/abci/types" |
| 8 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 9 | + juno "github.com/forbole/juno/v6/types" |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | + |
| 12 | + "github.com/forbole/callisto/v4/database" |
| 13 | +) |
| 14 | + |
| 15 | +// TransactionLogUpdater represents an interface for updating transaction logs |
| 16 | +type TransactionLogUpdater interface { |
| 17 | + UpdateTransactionLogs(hash string, logs []byte) error |
| 18 | +} |
| 19 | + |
| 20 | +// ConvertEventsToLogs converts a slice of ABCI events to a slice of SDK ABCIMessageLog |
| 21 | +// It groups events by message index when possible |
| 22 | +func ConvertEventsToLogs(events []abci.Event) []sdk.ABCIMessageLog { |
| 23 | + var logs []sdk.ABCIMessageLog |
| 24 | + |
| 25 | + log.Debug(). |
| 26 | + Int("events_count", len(events)). |
| 27 | + Msg("EVENTS: Starting conversion of events to logs") |
| 28 | + |
| 29 | + // Create a log entry for each message index by grouping events with the same msg_index |
| 30 | + msgIndexMap := make(map[uint32][]abci.Event) |
| 31 | + |
| 32 | + // Group events by message index, default to 0 if not specified |
| 33 | + for i, event := range events { |
| 34 | + msgIndex := uint32(0) // Default to first message |
| 35 | + |
| 36 | + // Try to find msg_index attribute |
| 37 | + for _, attr := range event.Attributes { |
| 38 | + if attr.Key == "msg_index" { |
| 39 | + // Try to parse as integer |
| 40 | + var idx int |
| 41 | + _, err := fmt.Sscanf(attr.Value, "%d", &idx) |
| 42 | + if err == nil && idx >= 0 { |
| 43 | + msgIndex = uint32(idx) |
| 44 | + log.Debug(). |
| 45 | + Int("event_index", i). |
| 46 | + Str("event_type", event.Type). |
| 47 | + Uint32("msg_index", msgIndex). |
| 48 | + Msg("EVENTS: Found explicit msg_index in event") |
| 49 | + break |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Add event to the appropriate message group |
| 55 | + msgIndexMap[msgIndex] = append(msgIndexMap[msgIndex], event) |
| 56 | + log.Debug(). |
| 57 | + Int("event_index", i). |
| 58 | + Str("event_type", event.Type). |
| 59 | + Uint32("assigned_msg_index", msgIndex). |
| 60 | + Msg("EVENTS: Assigned event to message group") |
| 61 | + } |
| 62 | + |
| 63 | + // If we couldn't find any message indices, use all events for the first message |
| 64 | + if len(msgIndexMap) == 0 && len(events) > 0 { |
| 65 | + log.Debug().Msg("EVENTS: No message indices found, assigning all events to message index 0") |
| 66 | + msgIndexMap[0] = events |
| 67 | + } |
| 68 | + |
| 69 | + // Log the grouping results |
| 70 | + for msgIndex, events := range msgIndexMap { |
| 71 | + log.Debug(). |
| 72 | + Uint32("msg_index", msgIndex). |
| 73 | + Int("events_count", len(events)). |
| 74 | + Msg("EVENTS: Message group events count") |
| 75 | + } |
| 76 | + |
| 77 | + // Create logs for each message index |
| 78 | + for msgIndex, msgEvents := range msgIndexMap { |
| 79 | + stringEvents := sdk.StringifyEvents(msgEvents) |
| 80 | + |
| 81 | + msgLog := sdk.ABCIMessageLog{ |
| 82 | + MsgIndex: msgIndex, |
| 83 | + Events: stringEvents, |
| 84 | + } |
| 85 | + logs = append(logs, msgLog) |
| 86 | + |
| 87 | + log.Debug(). |
| 88 | + Uint32("msg_index", msgIndex). |
| 89 | + Int("events_count", len(stringEvents)). |
| 90 | + Msg("EVENTS: Created log entry for message index") |
| 91 | + } |
| 92 | + |
| 93 | + log.Debug(). |
| 94 | + Int("created_logs_count", len(logs)). |
| 95 | + Msg("EVENTS: Completed conversion of events to logs") |
| 96 | + |
| 97 | + return logs |
| 98 | +} |
| 99 | + |
| 100 | +// UpdateTransactionLogs updates the transaction logs in the database if they're empty |
| 101 | +// and there are events available. Returns true if logs were updated, false otherwise. |
| 102 | +func UpdateTransactionLogs(tx *juno.Transaction, db *database.Db) bool { |
| 103 | + // Skip if logs are already present |
| 104 | + if len(tx.Logs) > 0 { |
| 105 | + log.Debug(). |
| 106 | + Str("txhash", tx.TxHash). |
| 107 | + Int("logs_count", len(tx.Logs)). |
| 108 | + Msg("EVENTS: Transaction already has logs, skipping") |
| 109 | + return false |
| 110 | + } |
| 111 | + |
| 112 | + // Skip if no events are present |
| 113 | + if len(tx.Events) == 0 { |
| 114 | + log.Debug(). |
| 115 | + Str("txhash", tx.TxHash). |
| 116 | + Msg("EVENTS: Transaction has no events, skipping") |
| 117 | + return false |
| 118 | + } |
| 119 | + |
| 120 | + log.Info(). |
| 121 | + Str("txhash", tx.TxHash). |
| 122 | + Int("events_count", len(tx.Events)). |
| 123 | + Msg("EVENTS: Enriching transaction logs from events") |
| 124 | + |
| 125 | + // Log some details about the events for debugging |
| 126 | + for i, event := range tx.Events { |
| 127 | + log.Debug(). |
| 128 | + Str("txhash", tx.TxHash). |
| 129 | + Int("event_index", i). |
| 130 | + Str("event_type", event.Type). |
| 131 | + Int("attributes_count", len(event.Attributes)). |
| 132 | + Msg("EVENTS: Processing event") |
| 133 | + } |
| 134 | + |
| 135 | + // Convert events to logs format |
| 136 | + logs := ConvertEventsToLogs(tx.Events) |
| 137 | + |
| 138 | + // Skip if no logs were created |
| 139 | + if len(logs) == 0 { |
| 140 | + log.Debug(). |
| 141 | + Str("txhash", tx.TxHash). |
| 142 | + Msg("EVENTS: No logs were created from events") |
| 143 | + return false |
| 144 | + } |
| 145 | + |
| 146 | + log.Debug(). |
| 147 | + Str("txhash", tx.TxHash). |
| 148 | + Int("created_logs_count", len(logs)). |
| 149 | + Msg("EVENTS: Successfully converted events to logs") |
| 150 | + |
| 151 | + // Store the logs in the transaction |
| 152 | + logsBz, err := json.Marshal(logs) |
| 153 | + if err != nil { |
| 154 | + log.Error(). |
| 155 | + Err(err). |
| 156 | + Str("txhash", tx.TxHash). |
| 157 | + Msg("EVENTS: Error while marshaling logs") |
| 158 | + return false |
| 159 | + } |
| 160 | + |
| 161 | + // Update the transaction logs |
| 162 | + err = db.UpdateTransactionLogs(tx.TxHash, logsBz) |
| 163 | + if err != nil { |
| 164 | + log.Error(). |
| 165 | + Err(err). |
| 166 | + Str("txhash", tx.TxHash). |
| 167 | + Msg("EVENTS: Error while updating transaction logs") |
| 168 | + return false |
| 169 | + } |
| 170 | + |
| 171 | + log.Info(). |
| 172 | + Str("txhash", tx.TxHash). |
| 173 | + Int("logs_count", len(logs)). |
| 174 | + Msg("EVENTS: Successfully updated transaction logs in database") |
| 175 | + |
| 176 | + // Update the transaction object with the new logs |
| 177 | + tx.Logs = logs |
| 178 | + |
| 179 | + return true |
| 180 | +} |
0 commit comments