From 418bfb6b730f4657d64a872c643e36ab49491a65 Mon Sep 17 00:00:00 2001 From: Aleksandr Kukharenko Date: Thu, 8 Feb 2024 15:23:55 +0200 Subject: [PATCH 1/2] fix: additional error handling, more logs, more retries --- package.json | 2 +- src/plugins/eth/web3Http.js | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index a12fd9c..9308f77 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lumerin/wallet-core", - "version": "1.0.88", + "version": "1.0.89", "author": { "name": "Lumerin", "email": "developer@lumerin.io", diff --git a/src/plugins/eth/web3Http.js b/src/plugins/eth/web3Http.js index 88c8841..8a2731d 100644 --- a/src/plugins/eth/web3Http.js +++ b/src/plugins/eth/web3Http.js @@ -2,13 +2,17 @@ const Web3 = require('web3') const https = require('https') const logger = require('../../logger') -const isRateLimitError = (response) => { +const isRateLimitError = (response, payload) => { const { result, ...data } = response const code = response.error?.code if (code === 429 || code === -32029 || code === -32097) { return true } + if (payload?.method === 'eth_call' && response.error?.message?.includes('execution reverted') || response.error?.code === -32000) { + return true + } + const message = response.error?.message?.toLowerCase() if (!message) { return false @@ -24,13 +28,19 @@ const isRateLimitError = (response) => { ); } +const MAX_RETRIES = 10; const timeouts = { - 0: 500, - 1: 750, - 2: 1000, - 3: 1500, - 4: 2000, + 0: 1000, + 1: 1200, + 2: 1400, + 3: 1600, + 4: 1800, 5: 2000, + 6: 2200, + 7: 2400, + 8: 2600, + 9: 2800, + 10: 3000, } class Web3Http extends Web3 { @@ -65,9 +75,9 @@ class Web3Http extends Web3 { const originalSend = this.currentProvider.send.bind(this.currentProvider) this.currentProvider.send = (payload, callback) => { originalSend(payload, async (error, response) => { - if (error || isRateLimitError(response)) { + if (error || isRateLimitError(response, payload)) { // Avoid infinite loop - if (this.retryCount >= this.providers.length * 2) { + if (this.retryCount >= MAX_RETRIES) { callback(error, response) this.retryCount = 0 return @@ -79,7 +89,7 @@ class Web3Http extends Web3 { this.retryCount += 1 const timeout = timeouts[this.retryCount] || 1000; logger.error( - `Switched to provider: ${this.providers[this.currentIndex].host}, timeout: ${timeout}` + `Switched to provider: ${this.providers[this.currentIndex].host}, timeout: ${timeout}, retry count: ${this.retryCount}, request payload: ${JSON.stringify(payload)}`, ) await new Promise((resolve) => setTimeout(resolve, timeout)) From ba596f87ca3e5c8cbaa07c48607281f2bf79c36c Mon Sep 17 00:00:00 2001 From: Aleksandr Kukharenko Date: Thu, 8 Feb 2024 15:28:02 +0200 Subject: [PATCH 2/2] group if condition --- src/plugins/eth/web3Http.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/eth/web3Http.js b/src/plugins/eth/web3Http.js index 8a2731d..820f3d4 100644 --- a/src/plugins/eth/web3Http.js +++ b/src/plugins/eth/web3Http.js @@ -9,7 +9,7 @@ const isRateLimitError = (response, payload) => { return true } - if (payload?.method === 'eth_call' && response.error?.message?.includes('execution reverted') || response.error?.code === -32000) { + if (payload?.method === 'eth_call' && (response.error?.message?.includes('execution reverted') || response.error?.code === -32000)) { return true }