重复事务挂起-Web3js,本地geth [英] Repeating transactions hangs - web3js, local geth

查看:158
本文介绍了重复事务挂起-Web3js,本地geth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的以太坊网络上的交易存在问题-有时,交易挂起&从我的帐户中花费了很多ETH.

I have an issue with transactions on my local ethereum network - at some point, transaction hangs & spends a lot of ETH from my account.

这是示例代码:

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调用处(承诺从未解决).

It hangs at sendTransaction call (promise is never resolved) on some random iteration.

脚本重新启动后,情况仍然相同-事务经过几次,然后挂起.

The situation remains the same after script restart - transaction passes a few times and then hangs.

geth版本:1.7.3

推荐答案

如果您要从同一帐户连续发送交易,则需要手动设置随机数,因为该节点将无法正确跟踪它.

If you are sending transactions back-to-back from the same account, you need to manually set the nonce, because the node will not keep track of it correctly.

示例代码

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);

这篇关于重复事务挂起-Web3js,本地geth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆