Skip to content

Commit

Permalink
Feat/ Chain fee service HTTP retries (#342)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
chk0912 authored Feb 4, 2023
1 parent fe92fed commit b52878a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
20 changes: 14 additions & 6 deletions packages/background/src/controllers/GasPricesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -450,11 +454,16 @@ export class GasPricesController extends BaseController<GasPricesControllerState
// Fetch the service to detect if the chain has support.
try {
// If the chain has support request the service
const feeDataResponse = await httpClient.get<FeeDataResponse>(
`${CHAIN_FEE_DATA_SERVICE_URL}/fee_data`,
{
c: chainId,
}
const feeDataResponse = await retryHandling(
() =>
httpClient.get<FeeDataResponse>(
`${CHAIN_FEE_DATA_SERVICE_URL}/fee_data`,
{
c: chainId,
}
),
API_CALLS_DELAY,
API_CALLS_RETRIES
);

if (feeDataResponse) {
Expand Down Expand Up @@ -560,7 +569,6 @@ export class GasPricesController extends BaseController<GasPricesControllerState
}
} catch (error) {
log.error('error calling chain fees service', error);
return undefined;
}
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import log from 'loglevel';
import { BaseController } from '../../infrastructure/BaseController';
import NetworkController from '../NetworkController';
import httpClient, { RequestError } from '../../utils/http';
import { retryHandling } from '../../utils/retryHandling';
import { MILISECOND } from '../../utils/constants/time';

export const BLOCKS_TO_WAIT_BEFORE_CHECHKING_FOR_CHAIN_SUPPORT = 100;
const OFF_CHAIN_BLOCK_FETCH_SERVICE_URL = 'https://chain-fee.blockwallet.io/v1';
const OFF_CHAIN_BLOCK_FETCH_SERVICE_MAX_REPEATED_BLOCKS_TOLERANCE = 100;
const API_CALLS_DELAY = 100 * MILISECOND;
const API_CALLS_RETRIES = 5;

export interface BlockFetchData {
offChainSupport: boolean;
Expand Down Expand Up @@ -364,11 +368,16 @@ export class OffChainBlockFetchService {
*/
public async fetchBlockNumber(chainId: number): Promise<number> {
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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ describe('GasPrices Controller', () => {
networkController.getProvider(),
'send'
);
providerStub.onFirstCall().returns(
providerStub.returns(
new Promise<any>((resolve) => {
resolve({
baseFeePerGas: [BigNumber.from('110000')],
Expand Down Expand Up @@ -1559,7 +1559,7 @@ describe('GasPrices Controller', () => {
networkController.getProvider(),
'send'
);
providerStub.onFirstCall().returns(
providerStub.returns(
new Promise<any>((resolve) => {
resolve({
baseFeePerGas: [BigNumber.from('110000')],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b52878a

Please sign in to comment.