首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在发送事务时找不到Block散列

在发送事务时找不到Block散列
EN

Stack Overflow用户
提问于 2022-01-15 00:40:16
回答 3查看 6.7K关注 0票数 2

使用Solana web3发送事务时,有时会显示以下错误:

Error: failed to send transaction: Transaction simulation failed: Blockhash not found

除了重试x次之外,处理此错误的正确方法是什么?

是否有办法保证在发送事务时不会发生此问题?

下面是我如何发送事务的一个例子:

代码语言:javascript
运行
复制
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错误?

EN

Stack Overflow用户

回答已采纳

发布于 2022-01-15 12:06:12

重试并不是件坏事!在某些情况下,它实际上是处理丢弃事务的首选方法。例如,这意味着:

代码语言:javascript
运行
复制
// assuming you have a transaction named `transaction` already
const blockhashResponse = await connection.getLatestBlockhashAndContext();
const lastValidBlockHeight = blockhashResponse.context.slot + 150;
const rawTransaction = transaction.serialize();
let blockheight = await connection.getBlockHeight();

while (blockheight < lastValidBlockHeight) {
  connection.sendRawTransaction(rawTransaction, {
    skipPreflight: true,
  });
  await sleep(500);
  blockheight = await connection.getBlockHeight();
}

您可能喜欢阅读有关重试事务: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

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70717996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档