How to Use getBlocks
Learn getBlocks use cases, code examples, request parameters, response structure, and tips.
The getBlocks
RPC method allows you to retrieve a list of confirmed block slot numbers between a specified start slot and an optional end slot. This is useful when you need to know which blocks have been confirmed in a particular range without fetching the full content of each block.
Common Use Cases
- Identifying Confirmed Blocks in a Range: Quickly get a list of all block slots that were successfully confirmed between two points in the ledger.
- Iterating Through Blocks: Use the returned list of slots to subsequently fetch detailed information for each block using
getBlock
if needed. - Basic Block Auditing: Verify block presence within a certain range.
Request Parameters
The getBlocks
method takes the following parameters:
start_slot
(u64, required): The first slot to consider for the range (inclusive).end_slot
(u64, optional): The last slot to consider for the range (inclusive).- If not provided, the query will return blocks up to the latest confirmed slot from
start_slot
. - The range between
start_slot
andend_slot
(or latest slot ifend_slot
is omitted) must not exceed 500,000 slots.
- If not provided, the query will return blocks up to the latest confirmed slot from
commitment
(string, optional): Specifies the commitment level for the query. If omitted, the default commitment of the node is used. This is passed as the sole field in a configuration object as the last parameter.
Response Structure
The result
field of the JSON-RPC response will be an array of u64 integers. Each integer in the array represents a confirmed block slot number within the specified range.
- Example:
[5, 6, 7, 8, 9, 10]
Examples
1. Get Blocks within a Specific Slot Range
This example fetches the list of confirmed block slots between slot 250000000
and 250000010
.
2. Get Blocks from a Start Slot to the Latest Confirmed Slot
This example fetches confirmed block slots starting from 260000000
up to the latest block confirmed by the node (respecting the 500,000 slot range limit from the start slot).
3. Get Blocks with a Specific Commitment Level
This example fetches blocks using the confirmed
commitment level.
Developer Tips
- Range Limit: Remember the 500,000 slot range limit. Requesting a larger range will result in an error.
- Node Data Availability: Nodes may not retain information for all historical slots. Very old
start_slot
values might return empty arrays or errors depending on the node’s configuration and ledger retention. - Block Confirmation: This method returns confirmed blocks. The exact set of blocks can vary slightly depending on the chosen
commitment
level and which node you query, especially for very recent slots. - Complement to
getBlock
:getBlocks
is often used as a first step to identify relevant block slots before usinggetBlock
to retrieve the full details of individual blocks within that list. - Pagination Alternative: Since
getBlocks
has a range limit, if you need to scan a very large portion of the chain, you’ll need to make multiple calls togetBlocks
, chunking your desired total range into segments of 500,000 slots or less.
This guide provides a clear overview of how to use the getBlocks
RPC method to list confirmed block slots on the Solana network.