Skip to content

Commit

Permalink
[GasController] Check for undefined basefeePerGas (#703)
Browse files Browse the repository at this point in the history
go-ethereum based chains never return empty baseFeePerGas because this is omitted in the serializer:

```
type feeHistoryResult struct {
	OldestBlock  *hexutil.Big     `json:"oldestBlock"`
	Reward       [][]*hexutil.Big `json:"reward,omitempty"`
	BaseFee      []*hexutil.Big   `json:"baseFeePerGas,omitempty"` <---
	GasUsedRatio []float64        `json:"gasUsedRatio"`
}
```

Current code checks for baseFeePerGas.length > 0 without checking for the occurence of this element.
This leads to an exception and in later flow to fallback to eth_gasPrice mode

FIXED: no feeMarket mode available in case one of past requests return no results
  • Loading branch information
peak3d authored and MajorLift committed Oct 11, 2023
1 parent 5029ad8 commit 88dc961
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/gas/fetchBlockFeeHistory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ describe('fetchBlockFeeHistory', () => {

expect(feeHistory).toStrictEqual([]);
});

it('should be able to handle an response with undefined baseFeePerGas from eth_feeHistory', async () => {
when(mockedQuery)
.calledWith(ethQuery, 'eth_feeHistory', [
toHex(numberOfRequestedBlocks),
toHex(latestBlockNumber),
[],
])
.mockResolvedValue({
oldestBlock: toHex(0),
gasUsedRatio: null,
});

const feeHistory = await fetchBlockFeeHistory({
ethQuery,
numberOfBlocks: numberOfRequestedBlocks,
});

expect(feeHistory).toStrictEqual([]);
});
});

describe('given a numberOfBlocks that exceeds the max limit that the EVM returns', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/gas/fetchBlockFeeHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type RequestChunkSpecifier = {
* @property oldestBlock - The id of the oldest block (in hex format) in the range of blocks
* requested.
* @property baseFeePerGas - Base fee per gas for each block in the range of blocks requested.
* For go-ethereum based chains baseFeePerGas will not returned in case of empty results
* <github.com/ethereum/go-ethereum/blob/v1.10.16/internal/ethapi/api.go#L87>
* @property gasUsedRatio - A number between 0 and 1 that represents the gas used vs. gas limit for
* each block in the range of blocks requested.
* @property reward - The priority fee at the percentiles requested for each block in the range of
Expand All @@ -30,7 +32,7 @@ type RequestChunkSpecifier = {

export type EthFeeHistoryResponse = {
oldestBlock: string;
baseFeePerGas: string[];
baseFeePerGas?: string[];
gasUsedRatio: number[];
reward?: string[][];
};
Expand Down Expand Up @@ -288,6 +290,7 @@ async function makeRequestForChunk<Percentile extends number>({
const startBlockNumber = fromHex(response.oldestBlock);

if (
response.baseFeePerGas !== undefined &&
response.baseFeePerGas.length > 0 &&
response.gasUsedRatio.length > 0 &&
(response.reward === undefined || response.reward.length > 0)
Expand Down

0 comments on commit 88dc961

Please sign in to comment.