> ## 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.

# 如何使用getBlockProduction

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

这个RPC方法提供了有关当前纪元或指定槽范围内区块生产的信息。可用于检索所有验证者或特定验证者身份的数据。这对于监控验证者性能和了解网络上区块生产模式非常有价值。

## 常见用例

* **监控验证者性能：** 跟踪特定验证者生产的区块数量和领导槽。
* **分析纪元性能：** 获取当前或过去纪元中所有验证者的区块生产概况。
* **识别错过的槽：** 查看验证者是否错过了分配的领导槽。
* **网络健康检查：** 收集整体区块生产效率的数据。

## 请求参数

该方法接受一个可选的配置对象，包含以下参数：

1. **（string，可选）**：指定查询使用的承诺级别。如果省略，将使用节点的默认承诺。
2. **（object，可选）**：定义要查询的槽范围。
   * （u64）：要获取区块生产信息的第一个槽（包含）。
   * （u64，可选）：要获取区块生产信息的最后一个槽（包含）。如果省略，将返回当前纪元的区块生产信息直到当前槽。
3. **（string，可选）**：验证者身份的base58编码公钥。如果提供，响应将仅包括该特定验证者的区块生产信息。如果省略，将返回指定范围（或当前纪元）内所有验证者的信息。

**注意：** 至少必须提供其中之一。如果未提供，参数是需要的。

## 响应结构

JSON-RPC响应的字段将是一个包含以下内容的对象：

* （object）：
  * （u64）：获取信息的槽。
* （object）：
  * （object）：一个对象，其中键是验证者身份字符串（base58编码的公钥），值是包含以下内容的对象：
    * （u64）：在查询的范围/纪元内分配给该验证者的领导槽数量。
    * （u64）：在查询的范围/纪元内该验证者生产的区块数量。
  * （object）：
    * （u64）：查询范围的第一个槽。
    * （u64）：查询范围的最后一个槽。

## 开发者提示

* **理解纪元边界**：当通过`identity`查询而没有`range`时，返回的数据是当前纪元直到节点处理的最新插槽。如果需要某个特定验证器的完整过去纪元的数据，你需要确定该纪元的`firstSlot`和`lastSlot`。
* **节点数据可用性**：历史块生产数据的范围可能因RPC节点而异。非常旧的插槽范围可能在所有节点上都不可用。
* **性能监控**：`getBlockProduction`是构建相对于验证器正常运行时间和区块生产成功率的仪表板或警报的关键。
* **与`getLeaderSchedule`结合使用**：为了进行更深入的分析，你可以将`getBlockProduction`数据与`getLeaderSchedule`的输出相关联，以查看哪些具体的领导插槽被制作或错过。
* **身份与所有验证器**：在没有`identity`的情况下查询会返回大量数据，尤其是在没有`range`或指定了广泛的`range`时。注意响应大小和处理要求。

## 示例

### 1. 获取当前纪元的区块生产（所有验证者）

此示例获取当前纪元中所有验证者的区块生产数据。

<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": "getBlockProduction"
    }'
  ```

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

  async function getEpochBlockProduction() {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const blockProduction = await connection.getBlockProduction();
      console.log('Block Production for Current Epoch:', JSON.stringify(blockProduction, null, 2));
      // Example: Accessing data for a specific validator (if present)
      // const specificValidatorIdentity = " deaktivCQ2sV2KF1y7n4qX1xUFk6VXL79CdfjrmLdshs"; // Replace with actual identity
      // if (blockProduction.value.byIdentity[specificValidatorIdentity]) {
      //   console.log(\`Data for \${specificValidatorIdentity}:\`, blockProduction.value.byIdentity[specificValidatorIdentity]);
      // }
    } catch (error) {
      console.error('Error fetching block production:', error);
    }
  }

  getEpochBlockProduction();
  ```
</CodeGroup>

### 2. 获取当前纪元中特定验证者的区块生产

此示例获取当前纪元中特定验证者身份的区块生产。

<CodeGroup>
  ```bash cURL theme={"system"}
  # Replace YOUR_VALIDATOR_IDENTITY_PUBKEY with the actual validator's base58 public key.
  curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \\
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getBlockProduction",
      "params": [
        {
          "identity": "YOUR_VALIDATOR_IDENTITY_PUBKEY"
        }
      ]
    }'
  ```

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

  async function getValidatorBlockProduction(validatorIdentity) {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const blockProduction = await connection.getBlockProduction({ identity: validatorIdentity });
      console.log(\`Block Production for \${validatorIdentity}:\`, JSON.stringify(blockProduction, null, 2));
    } catch (error) {
      console.error(\`Error fetching block production for \${validatorIdentity}:\`, error);
    }
  }

  // Replace with the validator identity you want to query
  const validatorIdentity = "So11111111111111111111111111111111111111112"; // Example: Solana Foundation
  getValidatorBlockProduction(validatorIdentity);
  ```
</CodeGroup>

### 3. 获取特定插槽范围和验证者的区块生产

此示例获取在定义的插槽范围内特定验证者的区块生产数据。

<CodeGroup>
  ```bash cURL theme={"system"}
  # Replace YOUR_VALIDATOR_IDENTITY_PUBKEY, START_SLOT, and END_SLOT.
  curl https://mainnet.helius-rpc.com/?api-key=<api-key> -X POST -H "Content-Type: application/json" -d \\
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getBlockProduction",
      "params": [
        {
          "identity": "YOUR_VALIDATOR_IDENTITY_PUBKEY",
          "range": {
            "firstSlot": START_SLOT, # e.g., 200000000
            "lastSlot": END_SLOT    # e.g., 200010000
          }
        }
      ]
    }'
  ```

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

  async function getValidatorBlockProductionForRange(validatorIdentity, firstSlot, lastSlot) {
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    try {
      const blockProduction = await connection.getBlockProduction({
        identity: validatorIdentity,
        range: {
          firstSlot: firstSlot,
          lastSlot: lastSlot,
        },
      });
      console.log(\`Block Production for \${validatorIdentity} in range \${firstSlot}-\${lastSlot}:\`, JSON.stringify(blockProduction, null, 2));
    } catch (error) {
      console.error(\`Error fetching block production for \${validatorIdentity} in range:\`, error);
    }
  }

  // Replace with your desired parameters
  const validatorIdentity = "Beefy1So11111111111111111111111111111111111"; // Example validator
  const firstSlot = 200000000; // Example start slot
  const lastSlot = 200010000;   // Example end slot
  getValidatorBlockProductionForRange(validatorIdentity, firstSlot, lastSlot);
  ```
</CodeGroup>

本指南应能让你充分理解如何使用`getBlockProduction` RPC方法来分析Solana上的区块生产。

## 相关方法

<CardGroup cols={2}>
  <Card title="getLeaderSchedule" href="/docs/zh/api-reference/rpc/http/getleaderschedule">
    获取完整的领导赛程以与生产数据相关联
  </Card>

  <Card title="getVoteAccounts" href="/docs/zh/api-reference/rpc/http/getvoteaccounts">
    获取所有投票验证器的信息
  </Card>
</CardGroup>
