发送交易时未找到 Blockhash

Blockhash not found when sending transaction

使用 Solana web3 发送交易时,有时会显示此错误:
Error: failed to send transaction: Transaction simulation failed: Blockhash not found

除了重试 x 次之外,处理此错误的正确方法是什么?
有没有办法保证发送交易时不会出现这个问题?

这是我如何发送交易的示例:

const web3 = require("@solana/web3.js")
const bs58 = require('bs58')

const publicKey = new web3.PublicKey(new Uint8Array(bs58.decode("BASE_58_PUBLIC_KEY").toJSON().data))
const secretKey = new Uint8Array(bs58.decode("BASE_58_SECRET_KEY").toJSON().data)

const connection = new web3.Connection(
  "https://api.mainnet-beta.solana.com", "finalized",
  {
    commitment: "finalized",
    confirmTransactionInitialTimeout: 30000
  }
)
const transaction = new web3.Transaction().add(
  web3.SystemProgram.transfer({
    fromPubkey: publicKey,
    toPubkey: publicKey,
    lamports: 1
  })
)
web3.sendAndConfirmTransaction(
  connection,
  transaction,
  [{publicKey: publicKey, secretKey: secretKey}],
  {commitment: "finalized"}
)


我该如何改进以避免 Blockhash not found 错误?

重试不是坏事!在某些情况下,它实际上是处理丢失事务的首选方式。

您可能想通读这篇关于重试交易的食谱条目:https://solanacookbook.com/guides/retrying-transactions.html

具体解释了如何实现一些重试逻辑:https://solanacookbook.com/guides/retrying-transactions.html#customizing-rebroadcast-logic

以及重试的具体含义:https://solanacookbook.com/guides/retrying-transactions.html#when-to-re-sign-transactions