> ## Documentation Index
> Fetch the complete documentation index at: https://www.helius.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 如何使用 getBlocks

> 了解 getBlocks 的使用案例、代码示例、请求参数、响应结构和提示。

[`getBlocks`](https://www.helius.dev/docs/api-reference/rpc/http/getblocks) RPC方法允许您检索指定起始槽和可选结束[槽](https://www.helius.dev/blog/solana-slots-blocks-and-epochs)之间已确认的区块槽号列表。这在您需要知道特定范围内已确认的区块时非常有用，而无需获取每个区块的完整内容。

<Warning>
  **避免批处理以提高性能**

  批处理归档方法会显著增加延迟。不允许超过10个请求的批处理。
</Warning>

## 常见用例

* **识别范围内的已确认区块：** 快速获取分类账中两个点之间成功确认的所有区块槽的列表。
* **遍历区块：** 使用返回的槽列表来获取每个区块的详细信息，如果需要，可使用`getBlock`。
* **基本区块审核：** 在特定范围内验证区块的存在。

## 请求参数

`getBlocks`方法接受以下参数：

1. **`start_slot`** (u64, 必填)：要考虑的第一个槽（包含在内）。
2. **`end_slot`** (u64, 可选)：要考虑的最后一个槽（包含在内）。
   * 如果未提供，该查询将返回从`start_slot`到最新确认槽的区块。
   * `start_slot`和`end_slot`之间的范围（如果省略`end_slot`则为最新槽）**不得超过500,000个槽**。
3. **`commitment`** (string, 可选)：指定查询的提交级别。如果省略，则使用节点的默认提交级别。这作为配置对象中的唯一字段传递，作为最后一个参数。

## 响应结构

JSON-RPC响应的`result`字段将是一个u64整数数组。数组中的每个整数表示指定范围内的已确认区块槽号。

* 例如: `[5, 6, 7, 8, 9, 10]`

## 示例

### 1. 获取特定槽范围内的区块

此示例获取从槽位 `250000000` 到 `250000010` 的已确认区块槽列表。

<CodeGroup>
  ```bash cURL theme={"system"}
  curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getBlocks",
      "params": [
        250000000,
        250000010
      ]
    }'
  ```

  ```javascript JavaScript (using @solana/web3.js) theme={"system"}
  const { Connection } = require('@solana/web3.js');

  async function getBlocksInRange(startSlot, endSlot) {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const blocks = await connection.getBlocks(startSlot, endSlot);
      console.log(`Confirmed blocks between slot ${startSlot} and ${endSlot}:`, blocks);
    } catch (error) {
      console.error('Error fetching blocks:', error);
    }
  }

  // Example usage:
  const startSlot = 250000000;
  const endSlot = 250000010;
  getBlocksInRange(startSlot, endSlot);
  ```
</CodeGroup>

### 2. 从起始槽到最新确认槽获取区块

此示例获取从 `260000000` 开始的已确认区块槽，直到节点确认的最新区块（遵循从起始槽位最多 500,000 槽的范围限制）。

<CodeGroup>
  ```bash cURL theme={"system"}
  curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getBlocks",
      "params": [
        260000000 
      ]
    }'
  ```

  ```javascript JavaScript (using @solana/web3.js) theme={"system"}
  const { Connection } = require('@solana/web3.js');

  async function getBlocksFromStart(startSlot) {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      // The endSlot parameter is omitted to fetch up to the latest confirmed block
      const blocks = await connection.getBlocks(startSlot);
      console.log(`Confirmed blocks from slot ${startSlot} to latest:`, blocks);
      if (blocks.length > 0) {
        console.log(`Latest block in range: ${blocks[blocks.length - 1]}`);
      }
    } catch (error) {
      console.error('Error fetching blocks:', error);
    }
  }

  // Example usage (ensure this doesn't exceed the 500,000 slot limit from latest block):
  const recentStartSlot = 260000000; 
  getBlocksFromStart(recentStartSlot);
  ```
</CodeGroup>

### 3. 获取具有特定承诺级别的区块

此示例使用 `confirmed` 承诺级别获取区块。

<CodeGroup>
  ```bash cURL theme={"system"}
  curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getBlocks",
      "params": [
        270000000,
        270000005,
        { "commitment": "confirmed" }
      ]
    }'
  ```

  ```javascript JavaScript (using @solana/web3.js) theme={"system"}
  const { Connection } = require('@solana/web3.js'); 

  async function getBlocksWithCommitment(startSlot, endSlot) {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const blocks = await connection.getBlocks(startSlot, endSlot, { commitment: 'confirmed' });
      console.log(`Confirmed blocks (with 'confirmed' commitment) between slot ${startSlot} and ${endSlot}:`, blocks);
    } catch (error) {
      console.error('Error fetching blocks with commitment:', error);
    }
  }

  // Example usage:
  const commitStartSlot = 270000000;
  const commitEndSlot = 270000005;
  getBlocksWithCommitment(commitStartSlot, commitEndSlot);
  ```
</CodeGroup>

## 开发者提示

* **范围限制：** 请记住 500,000 槽的范围限制。请求更大的范围将导致错误。
* **节点数据可用性：** 节点可能不会保留所有历史槽位的信息。非常旧的 `start_slot` 值根据节点的配置和分类帐保留情况可能返回空数组或错误。
* **区块确认：** 此方法返回*已确认*的区块。确切的一组区块可能会根据所选的 `commitment` 级别和查询的节点有所不同，特别是对非常近期的槽位。
* **与 `getBlock` 的补充：** `getBlocks` 通常用作识别相关区块槽的第一步，然后使用 `getBlock` 来检索该列表中各个区块的完整详细信息。
* **分页替代方法：** 由于 `getBlocks` 具有范围限制，如果需要扫描区块链的很大一部分，您需要多次调用 `getBlocks`，将所需的总范围分割为不超过 500,000 槽的段。

本指南概述了如何使用 `getBlocks` RPC 方法列出 Solana 网络上的已确认区块槽。

## 相关方法

<CardGroup cols={2}>
  <Card title="getBlock" href="/docs/zh/api-reference/rpc/http/getblock">
    获取特定区块的详细信息
  </Card>

  <Card title="getBlocksWithLimit" href="/docs/zh/api-reference/rpc/http/getblockswithlimit">
    从特定槽位开始获取固定数量的区块
  </Card>
</CardGroup>
