cosmosSdkTypes.go
Overview
The `cosmosSdkTypes.go` file defines a set of Go data structures (structs) that model the responses and core entities related to validators within a Cosmos SDK-based blockchain environment. These types represent the structure of JSON responses from blockchain queries that retrieve information about validators, their descriptions, commission rates, and pagination details.
This file is primarily used to deserialize and work with validator-related data fetched from the blockchain REST API or internal Cosmos SDK modules. It facilitates easier access and manipulation of validator data within the application or service that interacts with the Cosmos blockchain.
Detailed Explanation of Types
1. QueryValidatorsResponse
type QueryValidatorsResponse struct {
Validators []ValidatorResponse `json:"validators"`
Pagination PageResponse `json:"pagination,omitempty"`
}
Purpose: Represents the response from a query returning multiple validators.
Fields:
Validators: A slice ofValidatorResponseobjects, representing individual validators.Pagination: Optional pagination info to handle paginated responses.
Usage Example:
var response QueryValidatorsResponse // Unmarshal JSON response from blockchain API into `response`. for _, validator := range response.Validators { fmt.Println(validator.OperatorAddress) }
2. QueryValidatorResponse
type QueryValidatorResponse struct {
Validator ValidatorResponse `json:"validator"`
}
Purpose: Represents the response from a query returning a single validator.
Fields:
Validator: A singleValidatorResponseentity.
Usage Example:
var singleValidator QueryValidatorResponse // Fetch a single validator info and unmarshal JSON response into `singleValidator`. fmt.Println(singleValidator.Validator.Description.Moniker)
3. PageResponse
type PageResponse struct {
NextKey string `json:"next_key,omitempty"`
Total int `json:"total,omitempty,string"`
}
Purpose: Provides pagination details for queries that support paginated responses.
Fields:
NextKey: A string key used for fetching the next page of results.Total: The total number of items matching the query (represented as a string in JSON, converted to int).
Usage: Used internally by queries to manage result sets over multiple pages.
4. DescriptionResponse
type DescriptionResponse struct {
Moniker string `json:"moniker,omitempty"`
Identity string `json:"identity,omitempty"`
Website string `json:"website,omitempty"`
SecurityContact string `json:"security_contact,omitempty"`
Details string `json:"details,omitempty"`
}
Purpose: Contains descriptive metadata about a validator.
Fields:
Moniker: Validator's display name.Identity: Validator's identity key (e.g., keybase.io identity).Website: Validator's website URL.SecurityContact: Contact information for security concerns.Details: Additional details about the validator.
Usage: Used to present human-readable information about validators in UI or logs.
5. CommissionRates
type CommissionRates struct {
Rate string `json:"rate"`
MaxRate string `json:"max_rate"`
MaxChangeRate string `json:"max_change_rate"`
}
Purpose: Represents commission parameters for validator rewards.
Fields:
Rate: Current commission rate charged by the validator (string representation of decimal).MaxRate: Maximum commission rate the validator can charge.MaxChangeRate: Maximum daily increase allowed for the commission rate.
Usage: Used to calculate or display the commission fees validators charge delegators.
6. Commission
type Commission struct {
CommissionRates `json:"commission_rates"`
UpdateTime time.Time `json:"update_time"`
}
Purpose: Wraps commission rates with their last update timestamp.
Fields:
Embedded
CommissionRatesstruct.UpdateTime: Timestamp of when the commission rates were last updated.
Usage: Tracks commission rates over time.
7. ValidatorResponse
type ValidatorResponse struct {
OperatorAddress string `json:"operator_address,omitempty"`
Jailed bool `json:"jailed,omitempty"`
Status string `json:"status,omitempty"`
Tokens string `json:"tokens"`
DelegatorShares string `json:"delegator_shares"`
Description DescriptionResponse `json:"description"`
UnbondingHeight int `json:"unbonding_height,omitempty,string"`
UnbondingTime time.Time `json:"unbonding_time"`
Commission Commission `json:"commission"`
MinSelfDelegation string `json:"min_self_delegation"`
}
Purpose: Represents detailed information about a validator.
Fields:
OperatorAddress: The unique address of the validator operator.Jailed: Boolean flag indicating if the validator is jailed (penalized).Status: The current status of the validator (e.g., bonded, unbonded).Tokens: Tokens staked by the validator.DelegatorShares: Total shares delegated to the validator.Description: Validator metadata (DescriptionResponse).UnbondingHeight: The blockchain height at which unbonding started.UnbondingTime: The time when unbonding completes.Commission: Validator commission info.MinSelfDelegation: Minimum self-delegation required.
Usage Example:
var validator ValidatorResponse fmt.Printf("Validator %s (%s) has %s tokens\n", validator.Description.Moniker, validator.OperatorAddress, validator.Tokens)
Important Implementation Details
JSON Serialization:
All structs include JSON tags to ensure correct serialization and deserialization when interacting with REST APIs or JSON-based data sources.Time Handling:
Uses Go'stime.Timefor timestamps likeUpdateTimeandUnbondingTime, ensuring proper parsing and manipulation of time data.Embedded Structs:
TheCommissionstruct embedsCommissionRatesto provide a clean, hierarchical representation of commission data.String Representations:
Many numeric fields like tokens, rates, and shares are represented as strings to preserve precision and avoid floating-point inaccuracies when dealing with large or highly precise decimal values common in blockchain tokenomics.
Interaction with Other Parts of the System
Blockchain Queries:
These types are typically used to decode responses from queries to the Cosmos SDK blockchain modules, especially staking and slashing modules.API Layer:
When implementing REST or gRPC endpoints, these structs can be used as response payloads or intermediate data structures.Business Logic:
Validator-related business rules, such as commission calculations or validator status checks, use these types to access required validator data.UI/Frontend:
Frontend components displaying validator lists, details, or statistics consume data structured as these types.
Visual Diagram: Class Structure
This diagram illustrates the data structure relationships and composition in `cosmosSdkTypes.go`.
classDiagram
class QueryValidatorsResponse {
+[]ValidatorResponse Validators
+PageResponse Pagination
}
class QueryValidatorResponse {
+ValidatorResponse Validator
}
class PageResponse {
+string NextKey
+int Total
}
class ValidatorResponse {
+string OperatorAddress
+bool Jailed
+string Status
+string Tokens
+string DelegatorShares
+DescriptionResponse Description
+int UnbondingHeight
+time.Time UnbondingTime
+Commission Commission
+string MinSelfDelegation
}
class DescriptionResponse {
+string Moniker
+string Identity
+string Website
+string SecurityContact
+string Details
}
class CommissionRates {
+string Rate
+string MaxRate
+string MaxChangeRate
}
class Commission {
+time.Time UpdateTime
}
QueryValidatorsResponse "1" --> "*" ValidatorResponse
QueryValidatorsResponse "1" --> "1" PageResponse
QueryValidatorResponse "1" --> "1" ValidatorResponse
ValidatorResponse "1" --> "1" DescriptionResponse
ValidatorResponse "1" --> "1" Commission
Commission "1" --|> CommissionRates
Summary
The `cosmosSdkTypes.go` file provides essential Go types for interacting with the Cosmos SDK's staking module responses, focusing on validators' data. These types enable systematic handling, parsing, and usage of validator and pagination information, supporting blockchain client applications, APIs, or tooling that require insight into validator status and metadata.