Skip to content

Commit

Permalink
[FeeHistory] Allow test chains with few blocks (#699)
Browse files Browse the repository at this point in the history
The (gas)controller requests 20.000 long range results when opening the FeeMarket.
New chains / test chains normally don't have that amount of blocks, and in this case the controller requests feeHistory with a wrong hex value (0x-4756) which pollutes the node logs.

Beside this such a chain can return fewer results than requested, the current code doesn't handle this case correctly when analyzing feeHistory results. This leads to fallback to eth_gas mode.

FIXED: (Gas)Controller does not detect FeeMarket mode for test chains, even the chain supports EIP1559.
  • Loading branch information
peak3d authored and MajorLift committed Oct 11, 2023
1 parent f7cb964 commit 620f2b8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/gas/fetchBlockFeeHistory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,37 @@ describe('fetchBlockFeeHistory', () => {
]);
});
});

describe('given a range which exceeds existing blocks', () => {
beforeEach(() => {
jest.resetAllMocks();
});

it('should adjust fetched numberOfBlocks', async () => {
const latestBlockNumber = 1024;
const numberOfRequestedBlocks = 2048;
const endBlock = new BN(latestBlockNumber);

when(mockedQuery)
.calledWith(ethQuery, 'eth_feeHistory', [
toHex(latestBlockNumber),
toHex(latestBlockNumber),
[],
])
.mockResolvedValue({
oldestBlock: toHex(0),
baseFeePerGas: [],
gasUsedRatio: [],
reward: [],
});

await fetchBlockFeeHistory({
ethQuery,
numberOfBlocks: numberOfRequestedBlocks,
endBlock,
});

expect(mockedQuery).toHaveBeenCalledTimes(1);
});
});
});
11 changes: 10 additions & 1 deletion src/gas/fetchBlockFeeHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,14 @@ async function makeRequestForChunk<Percentile extends number>({
: response.baseFeePerGas.slice(0, numberOfBlocks);
const gasUsedRatios = response.gasUsedRatio;
const priorityFeePercentileGroups = response.reward ?? [];
// Chain is allowed to return fewer number of block results
const numberOfExistingResults = gasUsedRatios.length;

return baseFeesPerGasAsHex.map((baseFeePerGasAsHex, blockIndex) => {
const baseFeePerGas = fromHex(baseFeePerGasAsHex);
const number = startBlockNumber.addn(blockIndex);

return blockIndex > numberOfBlocks - 1
return blockIndex >= numberOfExistingResults
? buildNextFeeHistoryBlock({ baseFeePerGas, number })
: buildExistingFeeHistoryBlock({
baseFeePerGas,
Expand All @@ -329,6 +331,9 @@ async function makeRequestForChunk<Percentile extends number>({
* Divides a block range (specified by a range size and the end of the range) into chunks based on
* the maximum number of blocks that `eth_feeHistory` can return in a single call.
*
* If the requested totalNumberOfBlocks exceed endBlockNumber, totalNumberOfBlocks is
* truncated to avoid requesting chunks with negative endBlockNumber.
*
* @param endBlockNumber - The final block in the complete desired block range after all
* `eth_feeHistory` requests have been made.
* @param totalNumberOfBlocks - The total number of desired blocks after all `eth_feeHistory`
Expand All @@ -340,6 +345,10 @@ function determineRequestChunkSpecifiers(
endBlockNumber: BN,
totalNumberOfBlocks: number,
): RequestChunkSpecifier[] {
if (endBlockNumber.lt(new BN(totalNumberOfBlocks))) {
totalNumberOfBlocks = endBlockNumber.toNumber();
}

const specifiers = [];
for (
let chunkStartBlockNumber = endBlockNumber.subn(totalNumberOfBlocks);
Expand Down

0 comments on commit 620f2b8

Please sign in to comment.