block.go

Overview

The [block.go](/projects/291/69210) file is part of the Cosmos SDK ecosystem, specifically interacting with the CometBFT (previously Tendermint) blockchain node RPC interface. It provides a service and HTTP client methods to fetch and cache blockchain block data and related information. This file is designed to efficiently retrieve block details, cache them for quick access, and expose additional RPC calls for block search and block results.

Key functionalities include:

This file acts as a bridge between the application layer and the CometBFT RPC endpoints related to blockchain blocks.


Types and Structures

BlockFetcher Interface

type BlockFetcher interface {
	GetBlock(height *int) (*coretypes.ResultBlock, error)
}

BlockService Struct

type BlockService struct {
	Latest     *BlockResponse
	Blocks     map[int]*BlockResponse
	m          sync.RWMutex
	httpClient BlockFetcher
}

BlockResponse (Referenced but not defined in this file)

Note: This struct is used within this file but its definition is assumed elsewhere in the project. From usage, it contains at least:

This struct holds simplified block data used by the service.


Functions and Methods

NewBlockService

func NewBlockService(httpClient BlockFetcher) (*BlockService, error)

WriteBlock

func (s *BlockService) WriteBlock(block *BlockResponse, latest bool)

GetBlock (on BlockService)

func (s *BlockService) GetBlock(height int) (*BlockResponse, error)

GetBlock (on HTTPClient)

func (c *HTTPClient) GetBlock(height *int) (*coretypes.ResultBlock, error)

BlockSearch (on HTTPClient)

func (c *HTTPClient) BlockSearch(query string, page int, pageSize int) (*coretypes.ResultBlockSearch, error)

BlockResults (on HTTPClient)

func (c *HTTPClient) BlockResults(height int) (BlockResults, error)

Important Implementation Details and Algorithms


Interactions with Other System Components


Visual Diagram of BlockService Structure

classDiagram
    class BlockService {
        +Latest: *BlockResponse
        +Blocks: map[int]*BlockResponse
        -m: sync.RWMutex
        -httpClient: BlockFetcher
        +WriteBlock(block *BlockResponse, latest bool)
        +GetBlock(height int) (*BlockResponse, error)
    }

    class BlockResponse {
        +Height: int
        +Hash: string
        +Timestamp: int
    }

    class HTTPClient {
        +GetBlock(height *int) (*coretypes.ResultBlock, error)
        +BlockSearch(query string, page int, pageSize int) (*coretypes.ResultBlockSearch, error)
        +BlockResults(height int) (BlockResults, error)
    }

    BlockService --> HTTPClient : uses
    BlockService --> BlockResponse : caches

Summary

The [block.go](/projects/291/69210) file provides an essential service layer and HTTP client implementation for fetching, caching, and querying blockchain block data from a CometBFT node. It abstracts RPC calls, handles JSON unmarshalling, and caches blocks for efficient repeated access. It is a key component connecting blockchain data with higher-level application modules that require block information and results.