【问题标题】:Repeating transactions hangs - web3js, local geth重复事务挂起 - web3js,本地 geth
【发布时间】:2018-09-04 20:36:31
【问题描述】:

我的本​​地以太坊网络上的交易出现问题 - 有时,交易挂起并从我的帐户中花费大量 ETH。

这是一个示例代码:

async function send(toAccount, weiVal) {
  let account = await w3.getDefAccount();

  for (let i = 0; i < 100; i++) {
    let res = await web3.eth.sendTransaction({
      from: account,
      to: toAccount,
      value: weiVal
    });
    await helper.timeout(2000);
  }
}

send('0x5648...', 100000000000000);

它在某个随机迭代中挂在sendTransaction 调用(承诺永远不会解决)。

脚本重新启动后情况保持不变 - 事务通过几次然后挂起。

geth 版本:1.7.3

【问题讨论】:

  • 如果在 for 循环中放置 try/catch 会出现错误吗?突出的一件事是您没有指定 gas 限制或 gas 价格,因此您的交易可能由于资金不足或发生某些revert 操作而在某些迭代中失败,然后消耗了您所有剩余的 gas。没有更多的调试信息很难判断。

标签: javascript ethereum web3js web3 geth


【解决方案1】:

如果您从同一帐户背靠背发送交易,则需要手动设置随机数,因为节点不会正确跟踪它。

示例代码

async function send(toAccount, weiVal) {
  const account = await web3.getDefAccount();
  // the transaction count does not include *pending* transactions
  // but is a good starting point for the nonce
  const nonce = await web3.eth.getTransactionCount(account);

  let promises =  [];
  for (let i = 0; i < 100; i++) {
    promises.push(web3.eth.sendTransaction({
      from: account,
      to: toAccount,
      nonce: nonce++, // increment the nonce for every transaction
      value: weiVal
    }));
  }

  return Promise.all(promises);
}

await send('0x5648...', 100000000000000);

【讨论】:

  • 这并没有完全解决我的问题。仍然让所有 tx 失败,尽管他们至少在链上注册以挂起。自此发布以来有什么进展吗?
  • 为什么循环 1.. 100?如果只有 1 个调用,它是如何工作的?
猜你喜欢
  • 2021-09-20
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 2023-04-08
  • 2011-11-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多