From b52878ae0c2a1f6a78ba705c29efc84571493e4d Mon Sep 17 00:00:00 2001 From: Rodrigo <10286502+rodr-r@users.noreply.github.com> Date: Sat, 4 Feb 2023 10:53:12 -0300 Subject: [PATCH] Feat/ Chain fee service HTTP retries (#342) * Feat/ Chain fee service HTTP retries Adding a retry policy in chain-fee-service http requests to catch random errors. This helps to reduce the possibility of using on-chain requests to get the block number and the gas prices. * fixing tests * test fixing * fix tests --- .../src/controllers/GasPricesController.ts | 20 +++++++++++++------ .../block-updates/BlockFetchController.ts | 19 +++++++++++++----- .../controllers/GasPricesController.test.ts | 4 ++-- .../BlockFetchController.test.ts | 2 +- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/packages/background/src/controllers/GasPricesController.ts b/packages/background/src/controllers/GasPricesController.ts index 83d5ecf3b..fd458b434 100644 --- a/packages/background/src/controllers/GasPricesController.ts +++ b/packages/background/src/controllers/GasPricesController.ts @@ -15,9 +15,13 @@ import BlockUpdatesController, { BlockUpdatesEvents, } from './block-updates/BlockUpdatesController'; import httpClient from '../utils/http'; +import { MILISECOND } from '../utils/constants/time'; +import { retryHandling } from '../utils/retryHandling'; const CHAIN_FEE_DATA_SERVICE_URL = 'https://chain-fee.blockwallet.io/v1'; const BLOCKS_TO_WAIT_BEFORE_CHECKING_FOR_CHAIN_SUPPORT = 100; +const API_CALLS_DELAY = 100 * MILISECOND; +const API_CALLS_RETRIES = 5; export enum GasPriceLevelsEnum { SLOW = 'slow', @@ -450,11 +454,16 @@ export class GasPricesController extends BaseController( - `${CHAIN_FEE_DATA_SERVICE_URL}/fee_data`, - { - c: chainId, - } + const feeDataResponse = await retryHandling( + () => + httpClient.get( + `${CHAIN_FEE_DATA_SERVICE_URL}/fee_data`, + { + c: chainId, + } + ), + API_CALLS_DELAY, + API_CALLS_RETRIES ); if (feeDataResponse) { @@ -560,7 +569,6 @@ export class GasPricesController extends BaseController { try { - const blockDataResponse = await httpClient.get<{ - bn: string; - }>(`${OFF_CHAIN_BLOCK_FETCH_SERVICE_URL}/bn`, { - c: chainId, - }); + const blockDataResponse = await retryHandling( + () => + httpClient.get<{ + bn: string; + }>(`${OFF_CHAIN_BLOCK_FETCH_SERVICE_URL}/bn`, { + c: chainId, + }), + API_CALLS_DELAY, + API_CALLS_RETRIES + ); if (!blockDataResponse) { throw new Error('empty response'); diff --git a/packages/background/test/controllers/GasPricesController.test.ts b/packages/background/test/controllers/GasPricesController.test.ts index fca3e524f..c0128b6b3 100644 --- a/packages/background/test/controllers/GasPricesController.test.ts +++ b/packages/background/test/controllers/GasPricesController.test.ts @@ -1263,7 +1263,7 @@ describe('GasPrices Controller', () => { networkController.getProvider(), 'send' ); - providerStub.onFirstCall().returns( + providerStub.returns( new Promise((resolve) => { resolve({ baseFeePerGas: [BigNumber.from('110000')], @@ -1559,7 +1559,7 @@ describe('GasPrices Controller', () => { networkController.getProvider(), 'send' ); - providerStub.onFirstCall().returns( + providerStub.returns( new Promise((resolve) => { resolve({ baseFeePerGas: [BigNumber.from('110000')], diff --git a/packages/background/test/controllers/block-updates/BlockFetchController.test.ts b/packages/background/test/controllers/block-updates/BlockFetchController.test.ts index b9cb50b04..e9b2c4850 100644 --- a/packages/background/test/controllers/block-updates/BlockFetchController.test.ts +++ b/packages/background/test/controllers/block-updates/BlockFetchController.test.ts @@ -231,7 +231,7 @@ describe('OffChainBlockFetchService', () => { expect(_recurrentFetch).not.equal(undefined); expect(_recurrentFetch).not.equal(null); - await wait(200); + await wait(2000); loopActivated = false; await errorsPromise;