types.go
Overview
The [types.go](/projects/291/69257) file defines core data structures, interfaces, and implementations used within the Cosmos blockchain ecosystem integration layer. It primarily provides typed responses for blockchain queries (e.g., account info, balances, blocks, validators), transaction history handling, and event abstraction. The file bridges types from the CometBFT RPC layer and Cosmos SDK types, enabling uniform handling of blockchain data and events.
This file plays a foundational role in representing blockchain-related data structures and their behaviors, facilitating communication between blockchain nodes and higher-level application logic or client interfaces.
Detailed Documentation
Structs
AccountResponse
Represents the essential account information retrieved from the blockchain.
Field | Type | Description |
|---|---|---|
Address | string | The Bech32 encoded account address. |
AccountNumber | int | The unique account number (nonce). |
Sequence | int | The transaction sequence number for replay protection. |
**Usage Example:**
acc := AccountResponse{
Address: "cosmos1abcd...",
AccountNumber: 12345,
Sequence: 10,
}
BalanceResponse
Represents the balance details of an account or address.
Field | Type | Description |
|---|---|---|
Amount | string | Total amount in string format (e.g., "1000uatom"). |
Assets | []Value | Slice of `Value` representing detailed asset balances (likely defined elsewhere). |
*Note:* The `json` tags indicate serialization keys when marshaling/unmarshaling to JSON.
BlockResponse
Represents basic block metadata.
Field | Type | Description |
|---|---|---|
Height | int | Block height (block number). |
Hash | string | Block hash string. |
Timestamp | int | Unix timestamp of block creation. |
ErrorResponse
Standardized error response structure.
Field | Type | Description |
|---|---|---|
Code | int | Numeric error code. |
Msg | string | Human-readable error message. |
Detail | []interface{} | Additional error details (can vary). |
Pagination
Represents pagination information for paginated queries.
Field | Type | Description |
|---|---|---|
NextKey | *[]byte | Pointer to next page key (optional). |
Total | uint64 | Total number of items as string in JSON. |
ABCIEventAttribute
Represents a key-value attribute in an ABCI event.
Field | Type | Description |
|---|---|---|
Key | string | Attribute key. |
Value | string | Attribute value. |
ABCIEvent
Represents an ABCI event emitted during block or transaction processing.
Field | Type | Description |
|---|---|---|
Type | string | Event type identifier. |
Attributes | []ABCIEventAttribute | List of key-value attributes for the event. |
ResultBlockResults
Wraps CometBFT's `ResultBlockResults` to implement the `BlockResults` interface.
type ResultBlockResults struct {
*coretypes.ResultBlockResults
}
Method:
GetBlockEvents() []ABCIEventReturns the list of ABCI events from the block’s finalization phase by converting CometBFT's event format to the local
ABCIEventformat.
**Example:**
var results ResultBlockResults
events := results.GetBlockEvents()
ResultTx
Wraps CometBFT's `ResultTx` to implement the `HistoryTx` interface and provides transaction formatting.
type ResultTx struct {
*coretypes.ResultTx
formatTx func(tx *coretypes.ResultTx) (*Tx, error)
}
Methods:
GetHeight() int64
Returns the block height where the transaction was included.GetIndex() int
Returns the transaction index within the block.GetTxID() string
Returns the transaction hash as a hex string.FormatTx() (*Tx, error)
Formats the raw transaction into a richerTxtype using the providedformatTxfunction.
**Usage:**
tx := ResultTx{ResultTx: someResultTx, formatTx: formatFunc}
height := tx.GetHeight()
txID := tx.GetTxID()
formattedTx, err := tx.FormatTx()
TxHistoryResponse
Represents paginated transaction history response.
Field | Type | Description |
|---|---|---|
Cursor | string | Cursor string for fetching next page. |
Txs | []Tx | List of transactions in the page. |
ValidatorsResponse
Represents the response for validators query.
Field | Type | Description |
|---|---|---|
Validators | []Validator | List of validator info structs. |
Pagination | PageResponse | Pagination metadata. |
*Note:* `Validator` and `PageResponse` types are assumed to be defined elsewhere.
signingTx (implements SigningTx)
Represents a simple transaction signing structure with memo and fee.
Field | Type | Description |
|---|---|---|
memo | string | Memo string attached to the transaction. |
fee | sdk.Coins | Fee amount in Cosmos SDK coin format. |
Methods:
GetMemo() string
Returns the memo string.GetFee() sdk.Coins
Returns the fee (coins).
Interfaces
BlockResults
Defines behavior for retrieving block events.
type BlockResults interface {
GetBlockEvents() []ABCIEvent
}
Implemented by
ResultBlockResults.
HistoryTx
Defines an interface for historical transaction data access.
type HistoryTx interface {
GetHeight() int64
GetIndex() int
GetTxID() string
FormatTx() (*Tx, error)
}
Implemented by
ResultTx.
SigningTx
Defines accessors for transaction signing metadata.
type SigningTx interface {
GetMemo() string
GetFee() sdk.Coins
}
Implemented by
signingTx.
Important Implementation Details
The file wraps and extends types from external dependencies (
cometbftandcosmos-sdk) to provide domain-specific interfaces and response types.Conversion of ABCI events from CometBFT's native format to local
ABCIEventstructs facilitates easier processing and abstraction.The use of interfaces (
BlockResults,HistoryTx,SigningTx) enables flexibility and abstraction, allowing different underlying implementations while exposing consistent method sets.Pagination fields use pointers and JSON annotations to support optional fields and string-based serialization for large integers.
The presence of
formatTxas a function field inResultTxallows injection of formatting logic, supporting extensibility for different transaction types or encoding schemes.
Interactions with Other Parts of the System
CometBFT RPC Layer:
Relies oncoretypesfrom CometBFT's RPC package for raw blockchain data (ResultBlockResults,ResultTx).Cosmos SDK Types:
Usessdk.Coinsfor fee representation and other Cosmos-specific types.Transaction Processing:
TheResultTxstruct andHistoryTxinterface are used in transaction history queries and formatting workflows, likely interacting with transaction decoders and presentation layers.Pagination and Querying:
Pagination structs and responses (Pagination,TxHistoryResponse,ValidatorsResponse) are used in APIs for list queries (e.g., fetching validators, transactions).Error Handling:
ErrorResponsestandardizes error messages and codes returned by API endpoints or internal components.
Visual Diagram
classDiagram
class AccountResponse {
+string Address
+int AccountNumber
+int Sequence
}
class BalanceResponse {
+string Amount
+[]Value Assets
}
class BlockResponse {
+int Height
+string Hash
+int Timestamp
}
class ErrorResponse {
+int Code
+string Msg
+[]interface{} Detail
}
class Pagination {
+*[]byte NextKey
+uint64 Total
}
class ABCIEventAttribute {
+string Key
+string Value
}
class ABCIEvent {
+string Type
+[]ABCIEventAttribute Attributes
}
class ResultBlockResults {
+GetBlockEvents() []ABCIEvent
}
class ResultTx {
+GetHeight() int64
+GetIndex() int
+GetTxID() string
+FormatTx() (*Tx, error)
}
class TxHistoryResponse {
+string Cursor
+[]Tx Txs
}
class ValidatorsResponse {
+[]Validator Validators
+PageResponse Pagination
}
class signingTx {
-string memo
-sdk.Coins fee
+GetMemo() string
+GetFee() sdk.Coins
}
ResultBlockResults --|> coretypes.ResultBlockResults
ResultTx --|> coretypes.ResultTx
signingTx ..|> SigningTx
ResultBlockResults ..|> BlockResults
ResultTx ..|> HistoryTx
Summary
The [types.go](/projects/291/69257) file is a core utility file that defines foundational data types, interfaces, and wrapper implementations necessary for interacting with the Cosmos blockchain via CometBFT RPC and Cosmos SDK. It abstracts raw blockchain data into usable Go structures, supports transaction history and event processing, and defines consistent interfaces for blockchain-related operations. This facilitates clean integration with other system components such as API layers, transaction processors, and user interfaces.