Skip to content

Commit

Permalink
eth_feeHistory invalid cached response (#882)
Browse files Browse the repository at this point in the history
* Fix cache

Signed-off-by: nikolay <[email protected]>

* Fix unit test

Signed-off-by: nikolay <[email protected]>

---------

Signed-off-by: nikolay <[email protected]>
  • Loading branch information
natanasow authored Feb 11, 2023
1 parent 1f9b860 commit 6875135
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 6 additions & 5 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,14 @@ export class EthImpl implements Eth {
return EthImpl.feeHistoryZeroBlockCountResponse;
}

let feeHistory: object | undefined = this.cache.get(constants.CACHE_KEY.FEE_HISTORY);
const cacheKey = `${constants.CACHE_KEY.FEE_HISTORY}_${blockCount}_${newestBlock}_${rewardPercentiles?.join('')}`;
let feeHistory: object | undefined = this.cache.get(cacheKey);
if (!feeHistory) {

feeHistory = await this.getFeeHistory(blockCount, newestBlockNumber, latestBlockNumber, rewardPercentiles, requestId);

this.logger.trace(`${requestIdPrefix} caching ${constants.CACHE_KEY.FEE_HISTORY} for ${constants.CACHE_TTL.ONE_HOUR} ms`);
this.cache.set(constants.CACHE_KEY.FEE_HISTORY, feeHistory);
if (newestBlock != EthImpl.blockLatest && newestBlock != EthImpl.blockPending) {
this.logger.trace(`${requestIdPrefix} caching ${cacheKey} for ${constants.CACHE_TTL.ONE_HOUR} ms`);
this.cache.set(cacheKey, feeHistory);
}
}

return feeHistory;
Expand Down
13 changes: 7 additions & 6 deletions packages/relay/tests/lib/eth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2154,21 +2154,22 @@ describe('Eth calls using MirrorNode', async function () {
expect(feeHistory['gasUsedRatio'].length).to.equal(maxResultsCap);
});

it('eth_feeHistory verify cached value', async function () {
const latestBlock = {...defaultBlock, number: blockNumber3};
it('eth_feeHistory verify cached value', async function() {
const latestBlock = { ...defaultBlock, number: blockNumber3 };
const latestFees = defaultNetworkFees;
const hexBlockNumber = `0x${latestBlock.number.toString(16)}`;

restMock.onGet('blocks?limit=1&order=desc').reply(200, {blocks: [latestBlock]});
restMock.onGet('blocks?limit=1&order=desc').reply(200, { blocks: [latestBlock] });
restMock.onGet(`blocks/${latestBlock.number}`).reply(200, latestBlock);
restMock.onGet(`network/fees?timestamp=lte:${latestBlock.timestamp.to}`).reply(200, latestFees);

const firstFeeHistory = await ethImpl.feeHistory(1, 'latest', null);
const secondFeeHistory = await ethImpl.feeHistory(3, 'latest', null);
const firstFeeHistory = await ethImpl.feeHistory(1, hexBlockNumber, null);
const secondFeeHistory = await ethImpl.feeHistory(1, hexBlockNumber, null);

expect(firstFeeHistory).to.exist;
expect(firstFeeHistory['baseFeePerGas'][0]).to.equal('0x84b6a5c400');
expect(firstFeeHistory['gasUsedRatio'][0]).to.equal(gasUsedRatio);
expect(firstFeeHistory['oldestBlock']).to.equal(`0x${latestBlock.number.toString(16)}`);
expect(firstFeeHistory['oldestBlock']).to.equal(hexBlockNumber);

expect(firstFeeHistory).to.equal(secondFeeHistory);
});
Expand Down

0 comments on commit 6875135

Please sign in to comment.