Skip to content

Commit 541c06e

Browse files
authored
Add cache to eth_getBlockByNumber (#930)
* Add cache Signed-off-by: nikolay <[email protected]> * Add test Signed-off-by: nikolay <[email protected]> * Edit test Signed-off-by: nikolay <[email protected]> --------- Signed-off-by: nikolay <[email protected]>
1 parent e7a6eee commit 541c06e

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

packages/relay/src/lib/eth.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -770,15 +770,27 @@ export class EthImpl implements Eth {
770770

771771
/**
772772
* Gets the block by its block number.
773-
* @param blockNumOrTag
773+
* @param blockNumOrTag Possible values are earliest/pending/latest or hex, and can't be null (validator check).
774774
* @param showDetails
775775
*/
776776
async getBlockByNumber(blockNumOrTag: string, showDetails: boolean, requestId?: string): Promise<Block | null> {
777777
const requestIdPrefix = formatRequestIdMessage(requestId);
778778
this.logger.trace(`${requestIdPrefix} getBlockByNumber(blockNum=${blockNumOrTag}, showDetails=${showDetails})`);
779-
return this.getBlock(blockNumOrTag, showDetails, requestId).catch((e: any) => {
780-
throw this.genericErrorHandler(e, `${requestIdPrefix} Failed to retrieve block for blockNum ${blockNumOrTag}`);
781-
});
779+
780+
const cacheKey = `eth_getBlockByNumber_${blockNumOrTag}_${showDetails}`;
781+
let block = this.cache.get(cacheKey);
782+
if (!block) {
783+
block = await this.getBlock(blockNumOrTag, showDetails, requestId).catch((e: any) => {
784+
throw this.genericErrorHandler(e, `${requestIdPrefix} Failed to retrieve block for blockNum ${blockNumOrTag}`);
785+
});
786+
787+
if (blockNumOrTag != EthImpl.blockLatest && blockNumOrTag != EthImpl.blockPending) {
788+
this.logger.trace(`${requestIdPrefix} caching ${cacheKey} for ${constants.CACHE_TTL.ONE_HOUR} ms`);
789+
this.cache.set(cacheKey, block);
790+
}
791+
}
792+
793+
return block;
782794
}
783795

784796
/**

packages/relay/tests/lib/eth.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,21 @@ describe('Eth calls using MirrorNode', async function () {
677677
verifyBlockConstants(result);
678678
});
679679

680+
it('eth_getBlockByNumber should return cached result', async function() {
681+
// mirror node request mocks
682+
restMock.onGet(`blocks/${blockNumber}`).reply(200, defaultBlock);
683+
restMock.onGet(`contracts/results?timestamp=gte:${defaultBlock.timestamp.from}&timestamp=lte:${defaultBlock.timestamp.to}&limit=100&order=asc`).reply(200, defaultContractResults);
684+
restMock.onGet(`contracts/${contractAddress1}/results/${contractTimestamp1}`).reply(200, defaultDetailedContractResults);
685+
restMock.onGet(`contracts/${contractAddress2}/results/${contractTimestamp2}`).reply(200, defaultDetailedContractResults);
686+
restMock.onGet('network/fees').reply(200, defaultNetworkFees);
687+
const resBeforeCache = await ethImpl.getBlockByNumber(EthImpl.numberTo0x(blockNumber), false);
688+
689+
restMock.onGet(`blocks/${blockNumber}`).reply(404);
690+
const resAfterCache = await ethImpl.getBlockByNumber(EthImpl.numberTo0x(blockNumber), false);
691+
692+
expect(resBeforeCache).to.eq(resAfterCache);
693+
});
694+
680695
it('eth_getBlockByNumber with zero transactions', async function () {
681696
// mirror node request mocks
682697
restMock.onGet(`blocks/${blockNumber}`).reply(200, {...defaultBlock, gas_used: 0});

0 commit comments

Comments
 (0)