-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce load on Infura in gas estimate fallback (#705)
Usually we use the MetaSwap API for gas fee estimates, but if that is down for some reason, we fall back to hitting Infura directly. A little while ago we were working on some UI improvements to the extension around gas fees, and to support this some changes to the MetaSwap API were made so that it requests the last 20,000 blocks from the network and uses their data to obtain a baseline for base fees so that we can display a network stability/congestion meter. We made the same changes to the gas fee estimation fallback logic. **This ended up being a very bad decision.** Because none of the the requests to Infura that the fallback logic makes are cached, when the MetaSwap API goes down, the number of requests made to Infura skyrockets and Infura suffers performance-wise. This problem already existed, but has been severely exacerbated by the new changes to the fallback logic. In fact, recently the Infura team had to disable the `eth_feeHistory` endpoint because some of the Ethereum nodes crashed under all of the load. To address this, this commit updates the part of the fallback logic which is responsible for calculating network congestion to always return 0.5 instead of making the aforementioned 20,000-block request. This means that the network stability meter in the extension will stay static, but that is absolutely preferable to blasting Infura.
- Loading branch information
Showing
6 changed files
with
133 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { BN } from 'ethereumjs-util'; | ||
import { mocked } from 'ts-jest/utils'; | ||
import { when } from 'jest-when'; | ||
import fetchBlockFeeHistory, { FeeHistoryBlock } from '../fetchBlockFeeHistory'; | ||
import { EthQuery } from './types'; | ||
import BlockFeeHistoryDatasetFetcher from './BlockFeeHistoryDatasetFetcher'; | ||
|
||
jest.mock('../fetchBlockFeeHistory'); | ||
|
||
const mockedFetchBlockFeeHistory = mocked(fetchBlockFeeHistory, true); | ||
|
||
describe('BlockFeeHistoryDatasetFetcher', () => { | ||
let fakeEthQuery: EthQuery; | ||
let fakeEndBlockNumber: BN; | ||
let fakeBlocks: FeeHistoryBlock<never>[]; | ||
|
||
beforeEach(() => { | ||
fakeEthQuery = { | ||
async getBlockByNumber() { | ||
return { number: new BN(1), baseFeePerGas: new BN(1) }; | ||
}, | ||
}; | ||
fakeEndBlockNumber = new BN(1); | ||
fakeBlocks = [ | ||
{ | ||
number: new BN(1), | ||
baseFeePerGas: new BN(1), | ||
gasUsedRatio: 1, | ||
}, | ||
]; | ||
}); | ||
|
||
describe('forMediumRange', () => { | ||
it('returns 200 blocks along with the 10th and 95th reward percentiles (excluding the next block)', async () => { | ||
when(mockedFetchBlockFeeHistory) | ||
.calledWith({ | ||
ethQuery: fakeEthQuery, | ||
endBlock: fakeEndBlockNumber, | ||
numberOfBlocks: 200, | ||
percentiles: [10, 95], | ||
}) | ||
.mockResolvedValue(fakeBlocks); | ||
|
||
const fetcher = new BlockFeeHistoryDatasetFetcher({ | ||
ethQuery: fakeEthQuery, | ||
endBlockNumber: fakeEndBlockNumber, | ||
}); | ||
|
||
expect(await fetcher.forMediumRange()).toStrictEqual(fakeBlocks); | ||
}); | ||
}); | ||
|
||
describe('forSmallRange', () => { | ||
it('returns 5 blocks along with the 10th, 20th, and 30th reward percentiles (excluding the next block)', async () => { | ||
when(mockedFetchBlockFeeHistory) | ||
.calledWith({ | ||
ethQuery: fakeEthQuery, | ||
endBlock: fakeEndBlockNumber, | ||
numberOfBlocks: 5, | ||
percentiles: [10, 20, 30], | ||
}) | ||
.mockResolvedValue(fakeBlocks); | ||
|
||
const fetcher = new BlockFeeHistoryDatasetFetcher({ | ||
ethQuery: fakeEthQuery, | ||
endBlockNumber: fakeEndBlockNumber, | ||
}); | ||
|
||
expect(await fetcher.forSmallRange()).toStrictEqual(fakeBlocks); | ||
}); | ||
}); | ||
|
||
describe('forTinyRange', () => { | ||
it('returns 2 blocks along with the 50th reward percentiles (excluding the next block)', async () => { | ||
when(mockedFetchBlockFeeHistory) | ||
.calledWith({ | ||
ethQuery: fakeEthQuery, | ||
endBlock: fakeEndBlockNumber, | ||
numberOfBlocks: 2, | ||
percentiles: [50], | ||
}) | ||
.mockResolvedValue(fakeBlocks); | ||
|
||
const fetcher = new BlockFeeHistoryDatasetFetcher({ | ||
ethQuery: fakeEthQuery, | ||
endBlockNumber: fakeEndBlockNumber, | ||
}); | ||
|
||
expect(await fetcher.forTinyRange()).toStrictEqual(fakeBlocks); | ||
}); | ||
}); | ||
|
||
describe('forLatestWithNextBlock', () => { | ||
it('returns 1 block along with the 10th and 90th reward percentiles (including the next block)', async () => { | ||
when(mockedFetchBlockFeeHistory) | ||
.calledWith({ | ||
ethQuery: fakeEthQuery, | ||
endBlock: fakeEndBlockNumber, | ||
numberOfBlocks: 1, | ||
includeNextBlock: true, | ||
percentiles: [10, 95], | ||
}) | ||
.mockResolvedValue(fakeBlocks); | ||
|
||
const fetcher = new BlockFeeHistoryDatasetFetcher({ | ||
ethQuery: fakeEthQuery, | ||
endBlockNumber: fakeEndBlockNumber, | ||
}); | ||
|
||
expect(await fetcher.forLatestWithNextBlock()).toStrictEqual(fakeBlocks); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters