Truffle migrate deploy contract ESOCKETTIMEDOUT - How to fix

使用Truffle 来部署NTF项目的时候,很多时候,合约比较大,部署计较耗时,很容易出现超时。

人们常见的提问:

Am getting ESOCKETTIMEDOUT Error when I try to connect to Rinkeby or ropsten from my local truffle project. How do I resolve this?

ESOCKETTIMEDOUT ERROR while trying to deploy to Rinkeby

truffle migrate --network rinkeby Time Out # Error: PollingBlockTracker - encountered an error while attempting to update latest block:

truffle migrate timeout error

常见的报错:

Error: Unhandled error. ({
  code: -32603,
  message: 'ESOCKETTIMEDOUT',
  data: { originalError: { code: 'ESOCKETTIMEDOUT', connect: false } }
})
    at new NodeError (node:internal/errors:371:5)
    at Web3ProviderEngine.emit (node:events:509:17)
    at ..../node_modules/web3-provider-engine/index.js:54:14
    at afterRequest (..../node_modules/web3-provider-engine/index.js:148:21)
    at ..../node_modules/web3-provider-engine/index.js:174:21
    at ..../node_modules/web3-provider-engine/index.js:232:9
    at ..../node_modules/async/internal/once.js:12:16
    at replenish (..../node_modules/async/internal/eachOfLimit.js:61:25)
    at ..../node_modules/async/internal/eachOfLimit.js:71:9
    at eachLimit (..../node_modules/async/eachLimit.js:43:36)
    at ..../node_modules/async/internal/doLimit.js:9:16
    at end (..../node_modules/web3-provider-engine/index.js:211:5)
    at Request._callback (..../node_modules/web3-provider-engine/subproviders/rpc.js:36:21)  // check your callstack
    at self.callback (..../node_modules/request/request.js:185:22)
    at Request.emit (node:events:520:28)
    at ClientRequest.<anonymous> (..../node_modules/request/request.js:820:16)

另外一种报错:

/....../node_modules/eth-block-tracker/src/polling.js:53
        const newErr = new Error(`PollingBlockTracker - encountered an error while attempting to update latest block:\n${err.stack}`)
                       ^
Error: PollingBlockTracker - encountered an error while attempting to update latest block:
undefined
    at PollingBlockTracker._performSync (/......./node_modules/eth-block-tracker/src/polling.js:53:24)
    at runMicrotasks (<anonymous>)

还有一种报错:

Error: Unhandled error. ({
  code: -32603,
  message: 'ESOCKETTIMEDOUT',
  data: { originalError: { code: 'ESOCKETTIMEDOUT', connect: false } }
})
    at new NodeError (node:internal/errors:371:5)
    at Web3ProviderEngine.emit (node:events:509:17)
    at /..../node_modules/@truffle/hdwallet-provider/node_modules/web3-provider-engine/index.js:54:14
    at afterRequest (/..../node_modules/@truffle/hdwallet-provider/node_modules/web3-provider-engine/index.js:148:21)
    at /..../node_modules/@truffle/hdwallet-provider/node_modules/web3-provider-engine/index.js:174:21
    at /..../node_modules/@truffle/hdwallet-provider/node_modules/web3-provider-engine/index.js:232:9
    at /..../node_modules/async/internal/once.js:12:16
    at replenish (/..../node_modules/async/internal/eachOfLimit.js:61:25)
    at /..../node_modules/async/internal/eachOfLimit.js:71:9
    at eachLimit (/..../node_modules/async/eachLimit.js:43:36)
    at /..../node_modules/async/internal/doLimit.js:9:16
    at end (/..../node_modules/@truffle/hdwallet-provider/node_modules/web3-provider-engine/index.js:211:5)
    at Request._callback (/..../node_modules/@truffle/hdwallet-provider/node_modules/web3-provider-engine/subproviders/rpc.js:36:21)   // check your callstack
    at self.callback (/..../node_modules/request/request.js:185:22)
    at Request.emit (node:events:520:28)
    at ClientRequest.<anonymous> (/..../node_modules/request/request.js:819:16)

常见的解决方案:
在 truffle-config.js中加入pollingIntervalnetworkCheckTimeouttimeoutBlocks

ccmtest: {
      provider: () => new HDWalletProvider(privateKey, `http://127.0.0.1:9934`),
      networkCheckTimeout: 10000000,
      pollingInterval:30000,      
      network_id: 1,       
      // gas: 5500000,        
      confirmations: 0,    
      timeoutBlocks: 2000,  
      skipDryRun: true     
    },

但 这里几个参数解决的报错是:

Error: There was a timeout while attempting to connect to the network.     
       Check to see that your provider is valid.
       If you have a slow internet connection, try configuring a longer timeout in your Truffle config. Use the networks[networkName].networkCheckTimeout property to do this.

有时候,并不能解决最上面遇到到的错误。

实际需要修改代码超时的地方是在web3-provider-engine 这个模块。在代码 web3-provider-engine/subproviders/rpc.jsxhr默认的超时时间是 timeout: 20000,把它改大。

xhr({
    uri: targetUrl,
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(newPayload),
    rejectUnauthorized: false,
    timeout: 20000,     //change this  config
  }, function(err, res, body) {

查看报错的堆栈
常见有几处地方:

  1. Truffle 使用的hdwallet-provider模块下有web3-provider-engine
    .../node_modules/@truffle/hdwallet-provider/node_modules/web3-provider-engine/subproviders/rpc.js
  2. 直接引用的web3-provider-engine
    .../node_modules/web3-provider-engine/subproviders/rpc.js:36:21

找到他们都把他们改掉。ESOCKETTIMEDOUT 问题就能解决。

参考:https://juejin.cn/post/6865562790778634253

posted @ 2022-05-01 11:53  softfair  阅读(930)  评论(0编辑  收藏  举报