events.go


Overview

The `events.go` file is part of the Thorchain-specific functionality within a Cosmos SDK-based blockchain data processing system. Its primary purpose is to define Thorchain-specific blockchain event types and provide mechanisms to parse raw block events into strongly typed event structures. It also converts these typed events into generalized message formats used elsewhere in the system.

Specifically, this file:

This file acts as a domain-specific event parsing layer that abstracts raw blockchain event data into structured, typed objects, enabling downstream components like transaction parsers and API handlers to work with clear and consistent event data.


Detailed Explanation

Types

TypedEvent (interface)

type TypedEvent interface{}

EventFee

type EventFee struct {
    TxID       string `json:"tx_id"`
    Coins      string `json:"coins"`
    PoolDeduct string `json:"pool_deduct"`
    SynthUnits string `json:"synth_units"`
}

**Usage example:**

When parsing a block event with type `"fee"`, an `EventFee` instance is created and populated with attribute data extracted from the event.


EventOutbound

type EventOutbound struct {
    InTxID string `json:"in_tx_id"`
    ID     string `json:"id"`
    Chain  string `json:"chain"`
    From   string `json:"from"`
    To     string `json:"to"`
    Coin   string `json:"coin"`
    Memo   string `json:"memo"`
}

**Usage example:**

When parsing a block event with type `"outbound"`, an `EventOutbound` instance is created and populated accordingly.


Functions

ParseBlockEvents

func ParseBlockEvents(blockEvents []cosmos.ABCIEvent) (cosmos.EventsByMsgIndex, []TypedEvent, error)
blockEvents := []cosmos.ABCIEvent{...} // obtained from block results
eventsByMsgIndex, typedEvents, err := ParseBlockEvents(blockEvents)
if err != nil {
    // handle error
}
// typedEvents now contains parsed EventFee and EventOutbound structs
// eventsByMsgIndex organizes attributes by message index and event type

typedEventsToMessages

func typedEventsToMessages(events []TypedEvent) []cosmos.Message
typedEvents := []TypedEvent{...} // parsed events
messages := typedEventsToMessages(typedEvents)
// messages now contains Cosmos SDK compatible messages derived from outbound events

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Example

import (
    "github.com/shapeshift/unchained/pkg/cosmos"
    "gitlab.com/thorchain/thornode/v3/common"
    "github.com/thorchain/thornode/v3/pkg/thorchain"
)

// Assume blockEvents is obtained from block results
blockEvents := []cosmos.ABCIEvent{...}

eventsByMsgIndex, typedEvents, err := thorchain.ParseBlockEvents(blockEvents)
if err != nil {
    // handle error
}

// Convert typed events to generic messages
messages := thorchain.typedEventsToMessages(typedEvents)

for _, msg := range messages {
    fmt.Printf("Message Type: %s, From: %s, To: %s, Value: %+v\n",
        msg.Type, msg.From, msg.To, msg.Value)
}

Diagram: Structure and Workflow of events.go

flowchart TD
    subgraph Input
        A[Raw ABCI Events (blockEvents)]
    end

    subgraph ParseBlockEvents
        B[Identify Event Type]
        C[Create Typed Event Struct]
        D[Build JSON from Attributes]
        E[Unmarshal JSON into Typed Struct]
        F[Map Attributes by MsgIndex & EventType]
    end

    subgraph Output
        G[EventsByMsgIndex (map[string]map[string]Attributes)]
        H[TypedEvents ([]TypedEvent)]
    end

    subgraph typedEventsToMessages
        I[Iterate TypedEvents]
        J[Handle EventOutbound]
        K[Parse Coin]
        L[Construct cosmos.Message]
        M[Return []cosmos.Message]
    end

    A --> B --> C --> D --> E --> F --> G
    F --> H
    H --> I --> J --> K --> L --> M

Summary

The `events.go` file is a critical utility component in the Thorchain coinstack, responsible for:

By encapsulating the complexity of raw blockchain event parsing and providing a structured event representation, this file enables accurate and efficient transaction synthesis, fee indexing, and API serving tailored to Thorchain's unique blockchain event semantics.