重复事务挂起 - web3js,本地 geth

Repeating transactions hangs - web3js, local geth

我的本地以太坊网络上的交易有问题 - 在某些时候,交易挂起并从我的帐户中花费大量 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

如果您从同一个账户发送交易back-to-back,您需要手动设置随机数,因为节点不会正确跟踪它。

示例代码

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