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

# isBlockhashValid

> 返回 blockhash 是否仍然有效

## 参数

<ResponseField name="blockhash" type="string" required>
  要验证的 blockhash（base-58 编码）
</ResponseField>

<ResponseField name="config" type="object" optional>
  包含以下内容的配置对象：

  <ResponseField name="commitment" type="string" optional>
    Commitment 级别（processed、confirmed、finalized）
  </ResponseField>

  <ResponseField name="minContextSlot" type="number" optional>
    请求可被评估的最小 slot
  </ResponseField>
</ResponseField>

## 响应

<ResponseField name="result" type="boolean">
  blockhash 是否仍然有效（true）或无效（false）
</ResponseField>

## 代码示例

### 基本请求

```bash theme={null}
curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "isBlockhashValid",
  "params": [
    "J7rBdM6AecPDEZp8aPq5tPmsPzPhQG4HD6YtAcQBDfJj"
  ]
}'
```

### 使用 web3.js

```typescript theme={null}
import { Connection } from '@solana/web3.js';

const connection = new Connection('https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY');

// Check if blockhash is valid
const isValid = await connection.isBlockhashValid('J7rBdM6AecPDEZp8aPq5tPmsPzPhQG4HD6YtAcQBDfJj');
console.log('Is blockhash valid:', isValid);

// Check blockhash validity with analysis
async function checkBlockhashValidityWithAnalysis(
  blockhash: string,
  config: { 
    commitment?: string;
    minContextSlot?: number;
  }
) {
  const isValid = await connection.isBlockhashValid(blockhash, config);
  
  return {
    blockhash,
    validity: {
      isValid,
      checkedAt: Date.now()
    },
    config: {
      commitment: config.commitment,
      minContextSlot: config.minContextSlot
    }
  };
}
```

## 注意事项

1. 返回 blockhash 是否仍然有效
2. blockhash 在经过一定数量的 slot 后会过期
3. 响应是即时的，因为它从当前状态读取
4. 无效的 blockhash 不能在交易中使用
5. blockhash 必须是 base-58 编码的

## 最佳实践

1. 根据需求使用适当的 commitment 级别
2. 在适当时缓存结果以减少 RPC 负载
3. 在发送交易之前检查 blockhash 有效性
4. 考虑使用 WebSocket 订阅获取实时更新
5. 适当处理网络错误并重试

## 常见错误

| 错误码    | 消息                  | 解决方案                  |
| ------ | ------------------- | --------------------- |
| -32601 | Method not found    | 验证是否连接到 Solana RPC 节点 |
| -32602 | Invalid params      | 检查 blockhash 格式       |
| -32007 | Blockhash not found | blockhash 已过期或无效      |

## 用例

1. **Blockhash 验证**
   ```typescript theme={null}
   interface BlockhashValidation {
     blockhash: string;
     validity: { isValid: boolean; checkedAt: number; };
     config: { commitment?: string; minContextSlot?: number; };
   }

   class BlockhashValidator {
     async validateBlockhash(
       blockhash: string,
       config: { commitment?: string; minContextSlot?: number; }
     ): Promise<BlockhashValidation> {
       const isValid = await connection.isBlockhashValid(blockhash, config);
       return {
         blockhash,
         validity: { isValid, checkedAt: Date.now() },
         config: { commitment: config.commitment, minContextSlot: config.minContextSlot }
       };
     }
   }
   ```

2. **Blockhash 监控**
   ```typescript theme={null}
   interface BlockhashStatus {
     blockhash: string;
     status: { isValid: boolean; lastChecked: number; checkCount: number; };
     history: Array<{ timestamp: number; isValid: boolean; }>;
   }

   class BlockhashMonitor {
     private statuses: Map<string, BlockhashStatus> = new Map();
     
     async monitorBlockhash(
       blockhash: string,
       config: { commitment?: string; minContextSlot?: number; }
     ): Promise<BlockhashStatus> {
       const isValid = await connection.isBlockhashValid(blockhash, config);
       const now = Date.now();
       
       let status = this.statuses.get(blockhash);
       if (!status) {
         status = {
           blockhash,
           status: { isValid, lastChecked: now, checkCount: 1 },
           history: [{ timestamp: now, isValid }]
         };
       } else {
         status.status.isValid = isValid;
         status.status.lastChecked = now;
         status.status.checkCount++;
         status.history.push({ timestamp: now, isValid });
       }
       
       this.statuses.set(blockhash, status);
       return status;
     }
   }
   ```

3. **交易规划**
   ```typescript theme={null}
   interface TransactionPlan {
     blockhash: string;
     validity: { isValid: boolean; checkedAt: number; };
     recommendations: Array<{ type: 'proceed' | 'retry' | 'abort'; reason: string; }>;
     metadata: { timestamp: number; };
   }

   class TransactionPlanner {
     private readonly maxRetries = 3;
     private readonly validityCheckInterval = 1000;
     
     async planTransaction(
       blockhash: string,
       config: { commitment?: string; minContextSlot?: number; }
     ): Promise<TransactionPlan> {
       const isValid = await connection.isBlockhashValid(blockhash, config);
       const now = Date.now();
       const recommendations: Array<{ type: 'proceed' | 'retry' | 'abort'; reason: string; }> = [];
       
       if (isValid) {
         recommendations.push({ type: 'proceed', reason: 'Blockhash is valid' });
       } else {
         recommendations.push({ type: 'abort', reason: 'Blockhash is invalid' });
         recommendations.push({ type: 'retry', reason: 'Get a new blockhash and retry' });
       }
       
       return {
         blockhash,
         validity: { isValid, checkedAt: now },
         recommendations,
         metadata: { timestamp: now }
       };
     }
     
     async waitForValidBlockhash(
       blockhash: string,
       config: { commitment?: string; minContextSlot?: number; }
     ): Promise<boolean> {
       let retries = 0;
       while (retries < this.maxRetries) {
         const isValid = await connection.isBlockhashValid(blockhash, config);
         if (isValid) { return true; }
         retries++;
         await new Promise(resolve => setTimeout(resolve, this.validityCheckInterval));
       }
       return false;
     }
   }
   ```
