cursor.go

Overview

The [cursor.go](/projects/291/69107) file defines a data structure and related functionality to manage cursor-based pagination state within the Cosmos blockchain context. It provides mechanisms to serialize and deserialize a cursor object that tracks pagination progress across multiple paginated queries, especially when querying blockchain transactions and blocks.

This cursor enables clients to resume paginated queries efficiently without losing context, by storing information such as block height, transaction index, and transaction-specific pagination state. The cursor is encoded as a base64 string for safe transmission and storage, and decoded back into the structured format when needed.


Detailed Documentation

Types

CursorState

type CursorState struct {
    TxID string `json:"txid"`
    Page int    `json:"page"`
}

Cursor

type Cursor struct {
    BlockHeight int64                    `json:"blockHeight"`
    TxIndex     *int                    `json:"txIndex"`
    State       map[string]*CursorState `json:"state"`
}

Methods

encode() (string, error)

func (c *Cursor) encode() (string, error)
cursor := &Cursor{
    BlockHeight: 123456,
    TxIndex:     ptrInt(2),
    State: map[string]*CursorState{
        "txhash1": {TxID: "txhash1", Page: 1},
    },
}

encoded, err := cursor.encode()
if err != nil {
    log.Fatal(err)
}
fmt.Println("Encoded Cursor:", encoded)

decode(b64 string) error

func (c *Cursor) decode(b64 string) error
var cursor Cursor
err := cursor.decode(encodedString)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Decoded Cursor: %+v\n", cursor)

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class Cursor {
        +int64 BlockHeight
        +*int TxIndex
        +map[string]*CursorState State
        +encode() string, error
        +decode(b64 string) error
    }
    class CursorState {
        +string TxID
        +int Page
    }
    Cursor --> "1..*" CursorState : contains

Summary

The [cursor.go](/projects/291/69107) file provides a robust, easy-to-use cursor abstraction to support pagination over Cosmos blockchain data. It encapsulates the pagination state in a structured format and provides encoding/decoding utilities to facilitate persistence and transmission. This enables efficient, reliable paginated queries that can be resumed seamlessly, improving performance and user experience in blockchain data retrieval workflows.