Repeating transactions hangs - web3js, local geth

為{幸葍}努か 提交于 2019-12-07 08:25:44

问题


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

Here's a sample code:

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

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 version: 1.7.3


回答1:


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.

Example code

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


来源:https://stackoverflow.com/questions/49495879/repeating-transactions-hangs-web3js-local-geth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!