代码之家  ›  专栏  ›  技术社区  ›  Dmitry T.

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

  •  4
  • Dmitry T.  · 技术社区  · 7 年前

    我对本地以太坊网络上的事务有问题-在某个时候,事务挂起&从我的账户上花了很多钱。

    以下是示例代码:

    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

    1 回复  |  直到 7 年前
        1
  •  3
  •   gaiazov    7 年前

    如果您从同一个帐户背靠背发送交易,您需要 手动操作 设置nonce,因为节点无法正确跟踪它。

    示例代码

    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);
    
    推荐文章