types.go

Overview

The `types.go` file defines core data structures and interfaces related to blockchain blocks and transactions within the Thorchain module. It primarily provides abstractions over block data and transaction results as used in the context of CometBFT (formerly Tendermint) blockchain consensus engine events and Cosmos SDK transaction handling.

This file encapsulates:

Overall, this file acts as a bridge between raw blockchain event data and the higher-level Cosmos transaction processing components.


Detailed Descriptions

Interface: Block

type Block interface {
	Hash() string
	Height() int64
	Timestamp() int64
}

Purpose

Defines a minimal interface for blockchain block metadata access.

Methods

Method

Returns

Description

`Hash()`

`string`

Returns the block hash as a string.

`Height()`

`int64`

Returns the block height (block number).

`Timestamp()`

`int64`

Returns the block timestamp in milliseconds since Unix epoch.

Usage Example

func PrintBlockInfo(b Block) {
    fmt.Printf("Block %d at %d ms with hash %s\n", b.Height(), b.Timestamp(), b.Hash())
}

Struct: ResultBlock

type ResultBlock struct {
	*cometbfttypes.Block
}

Purpose

Wraps the `cometbfttypes.Block` type to implement the `Block` interface.

Methods

Method

Returns

Description

`Hash()`

`string`

Returns the block hash using CometBFT's `Block.Hash()` method.

`Height()`

`int64`

Returns the block height stored in the embedded block.

`Timestamp()`

`int64`

Returns the block time as Unix milliseconds.

Implementation Details

Usage Example

var cb *cometbfttypes.Block = getCometBlock()
rb := ResultBlock{Block: cb}
fmt.Println(rb.Hash())

Struct: NewBlock

type NewBlock struct {
	types.EventDataNewBlock
}

Purpose

Wraps `types.EventDataNewBlock` (an event emitted on new blocks by CometBFT) to implement the `Block` interface.

Methods

Method

Returns

Description

`Hash()`

`string`

Returns the block hash from the embedded `EventDataNewBlock`.

`Height()`

`int64`

Returns the block height from the embedded block.

`Timestamp()`

`int64`

Returns the block timestamp as Unix milliseconds.

Implementation Details


Struct: ResultTx

type ResultTx struct {
	BlockHash    string
	BlockHeight  int64
	Timestamp    int
	Index        int
	TxID         string
	Memo         string
	Fee          cosmos.Value
	Events       cosmos.EventsByMsgIndex
	Messages     []cosmos.Message
	TypedEvent   TypedEvent
	latestHeight int
	formatTx     func(tx *ResultTx) (*cosmos.Tx, error)
}

Purpose

Represents a blockchain transaction derived from block result events, enriched with Cosmos SDK-specific transaction data and events.

Fields

Field

Type

Description

`BlockHash`

`string`

Hash of the block containing this transaction.

`BlockHeight`

`int64`

Height of the block containing this transaction.

`Timestamp`

`int`

Timestamp of the block in Unix seconds (note: differs from block's ms).

`Index`

`int`

Index of the transaction within the block.

`TxID`

`string`

Unique identifier (hash) of the transaction.

`Memo`

`string`

Memo string attached to the transaction.

`Fee`

`cosmos.Value`

Fee paid for this transaction.

`Events`

`cosmos.EventsByMsgIndex`

Events grouped by message index within the transaction.

`Messages`

`[]cosmos.Message`

Slice of Cosmos SDK messages included in the transaction.

`TypedEvent`

`TypedEvent`

Typed event representation associated with the transaction.

`latestHeight`

`int`

Internal field tracking latest block height for caching or validation.

`formatTx`

`func(*ResultTx) (*cosmos.Tx, error)`

Function pointer to a formatter that converts `ResultTx` to a Cosmos `Tx`.

Methods

Method

Returns

Description

`GetHeight()`

`int64`

Returns the block height of the transaction.

`GetIndex()`

`int`

Returns the transaction index in the block.

`GetTxID()`

`string`

Returns the transaction ID (hash).

`FormatTx()`

`(*cosmos.Tx, error)`

Applies the formatter function to produce a Cosmos SDK `Tx` object.

Usage Example

tx := ResultTx{
    BlockHash: "ABC123",
    BlockHeight: 100,
    TxID: "TX456",
    formatTx: func(r *ResultTx) (*cosmos.Tx, error) {
       // custom formatting logic here
       return cosmosTx, nil
    },
}

fmt.Println(tx.GetTxID())  // Output: TX456

cosmosTx, err := tx.FormatTx()
if err != nil {
    log.Fatal(err)
}

Implementation Details


Implementation Notes and Algorithms


Interaction with Other Components


Mermaid Diagram: Structure of types.go

classDiagram
    class Block {
        <<interface>>
        +Hash() string
        +Height() int64
        +Timestamp() int64
    }

    class ResultBlock {
        -Block *cometbfttypes.Block
        +Hash() string
        +Height() int64
        +Timestamp() int64
    }

    class NewBlock {
        -EventDataNewBlock types.EventDataNewBlock
        +Hash() string
        +Height() int64
        +Timestamp() int64
    }

    class ResultTx {
        +BlockHash string
        +BlockHeight int64
        +Timestamp int
        +Index int
        +TxID string
        +Memo string
        +Fee cosmos.Value
        +Events cosmos.EventsByMsgIndex
        +Messages []cosmos.Message
        +TypedEvent TypedEvent
        -latestHeight int
        -formatTx func(*ResultTx) (*cosmos.Tx, error)
        +GetHeight() int64
        +GetIndex() int
        +GetTxID() string
        +FormatTx() (*cosmos.Tx, error)
    }

    ResultBlock ..|> Block
    NewBlock ..|> Block

Summary

The `types.go` file in the Thorchain module defines key abstractions for blockchain blocks and transactions, bridging CometBFT raw data and Cosmos SDK transaction structures. It standardizes access to block metadata across different block representations and encapsulates complex transaction data along with flexible formatting capabilities. This modular design facilitates event-driven processing of blockchain data within the broader Thorchain and Cosmos ecosystem.