history.go

Overview

The `history.go` file is part of the `cosmos` package and provides functionality to query, manage, and paginate transaction histories from a Cosmos blockchain node via RPC calls. It abstracts the complexity of fetching transactions matching certain queries, handling pagination, filtering based on cursors, and formatting transactions for client consumption.

This file is primarily responsible for:

This functionality is critical for applications (such as block explorers or wallets) that need to display a user's transaction history reliably and efficiently.


Types and Functions

Type Aliases

type RequestFn = func(string, int, int) ([]HistoryTx, error)

Struct: TxState

type TxState struct {
	hasMore  bool        // Indicates if more tx history is available from this source
	lastTxID string      // Tracks the last transaction ID returned from this source
	page     int         // Current page number for pagination
	query    string      // Query string for the RPC search
	request  RequestFn   // Function to perform the transaction search request
	txs      []HistoryTx // Cached transactions returned for the current page
}

**Constructor:**

func NewTxState(hasMore bool, query string, request RequestFn) *TxState

Functions: TxHistorySources and ValidatorTxHistorySources

func TxHistorySources(client APIClient, pubkey string, formatTx func(*coretypes.ResultTx) (*Tx, error)) map[string]*TxState
func ValidatorTxHistorySources(client APIClient, pubkey string, formatTx func(*coretypes.ResultTx) (*Tx, error)) map[string]*TxState

**Parameters:**

**Returns:**

**Usage Example:**

sources := TxHistorySources(client, "cosmos1abc...", formatTxFunc)
sendTxState := sources["send"]

Struct: History

type History struct {
	cursor   *Cursor
	pageSize int
	state    map[string]*TxState
}

Method: doRequest

func (h *History) doRequest(txState *TxState) ([]HistoryTx, error)

Method: filterByCursor

func (h *History) filterByCursor(txs []HistoryTx) ([]HistoryTx, error)

Method: get

func (h *History) get() (*TxHistoryResponse, error)

Method: fetch

func (h *History) fetch(more bool) error

Method: hasTxHistory

func (h *History) hasTxHistory() bool

Method: getNextTx

func (h *History) getNextTx() (*Tx, error)

Helper Function: getMostRecentHeight

func getMostRecentHeight(txs []HistoryTx) int

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Flowchart of Main Functions and Their Relationships

flowchart TD
    A[TxHistorySources / ValidatorTxHistorySources] --> B[Create TxState map]
    B --> C[NewTxState instances with queries & request function]
    D[History struct] --> E[fetch(more)]
    E --> F[doRequest(txState)]
    F --> G[TxSearch RPC call]
    E -->|parallel| F
    D --> H[get()]
    H --> E
    H --> I[filterByCursor(txs)]
    H --> J[getNextTx()]
    I --> J
    J --> K[FormatTx]
    H --> L[Update Cursor]
    L --> M[Return TxHistoryResponse with Txs and Cursor]

Summary

The `history.go` file is a core utility within the Cosmos SDK ecosystem for retrieving and managing blockchain transaction histories efficiently. It abstracts the complexities of querying multiple transaction types, handling pagination, filtering duplicates with cursors, and merging multi-source results into a seamless paginated API response. This makes it an essential component for user-facing blockchain clients requiring robust transaction history features.