Skip to content

Commit ea51719

Browse files
ebadierenatanasow
andauthored
Add cache on MirrorNodeClient.getBlock() (#1192) (#1230)
* Add caching * Fix code smell * Add test --------- Signed-off-by: nikolay <[email protected]> Signed-off-by: ebadiere <[email protected]> Co-authored-by: Nikolay Atanasow <[email protected]>
1 parent 008e7f4 commit ea51719

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

packages/relay/src/lib/clients/mirrorNodeClient.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,18 @@ export class MirrorNodeClient {
414414
}
415415

416416
public async getBlock(hashOrBlockNumber: string | number, requestId?: string) {
417-
return this.get(`${MirrorNodeClient.GET_BLOCK_ENDPOINT}${hashOrBlockNumber}`,
418-
MirrorNodeClient.GET_BLOCK_ENDPOINT,
419-
requestId);
417+
const cachedLabel = `${constants.CACHE_KEY.GET_BLOCK}.${hashOrBlockNumber}`;
418+
const cachedResponse: any = this.cache.get(cachedLabel);
419+
if (cachedResponse != undefined) {
420+
return cachedResponse;
421+
}
422+
423+
const block = await this.get(`${MirrorNodeClient.GET_BLOCK_ENDPOINT}${hashOrBlockNumber}`,
424+
MirrorNodeClient.GET_BLOCK_ENDPOINT,
425+
requestId);
426+
427+
this.cache.set(cachedLabel, block);
428+
return block;
420429
}
421430

422431
public async getBlocks(blockNumber?: number | string[], timestamp?: string, limitOrderParams?: ILimitOrderParams, requestId?: string) {

packages/relay/src/lib/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ enum CACHE_KEY {
2323
FEE_HISTORY = 'fee_history',
2424
GET_CONTRACT_RESULT = 'getContractResult',
2525
ETH_BLOCK_NUMBER = 'eth_block_number',
26-
ETH_GET_BALANCE = 'eth_get_balance'
26+
ETH_GET_BALANCE = 'eth_get_balance',
27+
GET_TINYBAR_GAS_FEE = 'getTinyBarGasFee',
28+
GET_BLOCK = 'getBlock'
2729
}
2830

2931
enum CACHE_TTL {

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ let restMock: MockAdapter, web3Mock: MockAdapter;
8080
let mirrorNodeInstance: MirrorNodeClient;
8181
let sdkClientStub;
8282
let cache;
83+
let mirrorNodeCache;
8384

8485
describe('Eth calls using MirrorNode', async function () {
8586
this.timeout(10000);
@@ -91,6 +92,9 @@ describe('Eth calls using MirrorNode', async function () {
9192
// @ts-ignore
9293
mirrorNodeInstance = new MirrorNodeClient(process.env.MIRROR_NODE_URL, logger.child({ name: `mirror-node` }), registry);
9394

95+
// @ts-ignore
96+
mirrorNodeCache = mirrorNodeInstance.cache;
97+
9498
// @ts-ignore
9599
restMock = new MockAdapter(mirrorNodeInstance.getMirrorNodeRestInstance(), { onNoMatch: "throwException" });
96100

@@ -792,6 +796,7 @@ describe('Eth calls using MirrorNode', async function () {
792796
});
793797

794798
it('eth_getBlockByNumber with zero transactions', async function () {
799+
mirrorNodeCache.clear();
795800
// mirror node request mocks
796801
restMock.onGet(`blocks/${blockNumber}`).reply(200, {...defaultBlock, gas_used: 0});
797802
restMock.onGet(`contracts/results?timestamp=gte:${defaultBlock.timestamp.from}&timestamp=lte:${defaultBlock.timestamp.to}&limit=100&order=asc`).reply(200, { 'results': [] });
@@ -814,6 +819,7 @@ describe('Eth calls using MirrorNode', async function () {
814819
});
815820

816821
it('eth_getBlockByNumber with match and details', async function () {
822+
mirrorNodeCache.clear();
817823
const resultWithNullGasUsed = {
818824
...defaultDetailedContractResults,
819825
gas_used: null
@@ -844,6 +850,7 @@ describe('Eth calls using MirrorNode', async function () {
844850
});
845851

846852
it('eth_getBlockByNumber with block match and contract revert', async function () {
853+
mirrorNodeCache.clear();
847854
// mirror node request mocks
848855
restMock.onGet(`blocks/${blockNumber}`).reply(200, {...defaultBlock, gas_used: gasUsed1});
849856
restMock.onGet(`contracts/results?timestamp=gte:${defaultBlock.timestamp.from}&timestamp=lte:${defaultBlock.timestamp.to}&limit=100&order=asc`).reply(200, defaultContractResultsRevert);
@@ -866,6 +873,7 @@ describe('Eth calls using MirrorNode', async function () {
866873
});
867874

868875
it('eth_getBlockByNumber with no match', async function () {
876+
mirrorNodeCache.clear();
869877
restMock.onGet(`blocks/${blockNumber}`).reply(400, {
870878
'_status': {
871879
'messages': [
@@ -993,6 +1001,7 @@ describe('Eth calls using MirrorNode', async function () {
9931001
});
9941002

9951003
it('eth_getBlockByHash with block match and contract revert', async function () {
1004+
mirrorNodeCache.clear();
9961005
// mirror node request mocks
9971006
restMock.onGet(`blocks/${blockHash}`).reply(200, {...defaultBlock, gas_used: gasUsed1});
9981007
restMock.onGet(`contracts/results?timestamp=gte:${defaultBlock.timestamp.from}&timestamp=lte:${defaultBlock.timestamp.to}&limit=100&order=asc`).reply(200, defaultContractResultsRevert);
@@ -1015,6 +1024,7 @@ describe('Eth calls using MirrorNode', async function () {
10151024
});
10161025

10171026
it('eth_getBlockByHash with no match', async function () {
1027+
mirrorNodeCache.clear();
10181028
// mirror node request mocks
10191029
restMock.onGet(`blocks/${blockHash}`).reply(400, {
10201030
'_status': {
@@ -1056,6 +1066,7 @@ describe('Eth calls using MirrorNode', async function () {
10561066
});
10571067

10581068
it('eth_getBlockTransactionCountByNumber with no match', async function () {
1069+
mirrorNodeCache.clear();
10591070
restMock.onGet(`blocks/${blockNumber}`).reply(400, {
10601071
'_status': {
10611072
'messages': [
@@ -1113,6 +1124,7 @@ describe('Eth calls using MirrorNode', async function () {
11131124
});
11141125

11151126
it('eth_getBlockTransactionCountByHash with no match', async function () {
1127+
mirrorNodeCache.clear();
11161128
// mirror node request mocks
11171129
restMock.onGet(`blocks/${blockHash}`).reply(400, {
11181130
'_status': {
@@ -1553,6 +1565,7 @@ describe('Eth calls using MirrorNode', async function () {
15531565
});
15541566

15551567
beforeEach(async () => {
1568+
mirrorNodeCache.clear();
15561569
restMock.onGet(`blocks?limit=1&order=desc`).reply(200, { blocks: [latestBlock] });
15571570
restMock.onGet(`blocks/3`).reply(200, defaultBlock);
15581571
restMock.onGet(`blocks/0`).reply(200, blockZero);
@@ -2355,6 +2368,7 @@ describe('Eth calls using MirrorNode', async function () {
23552368
});
23562369

23572370
it('with non-existing toBlock filter', async function () {
2371+
mirrorNodeCache.clear();
23582372
const filteredLogs = {
23592373
logs: [defaultLogs.logs[0]]
23602374
};
@@ -2493,6 +2507,7 @@ describe('Eth calls using MirrorNode', async function () {
24932507
});
24942508

24952509
it('with topics and blocks filter', async function () {
2510+
mirrorNodeCache.clear();
24962511
const filteredLogs = {
24972512
logs: [defaultLogs.logs[0], defaultLogs.logs[1]]
24982513
};
@@ -3537,7 +3552,7 @@ describe('Eth calls using MirrorNode', async function () {
35373552
});
35383553

35393554
it('eth_getStorageAt should throw a predefined RESOURCE_NOT_FOUND when block not found', async function () {
3540-
3555+
mirrorNodeCache.clear();
35413556
let hasError = false;
35423557
try {
35433558
restMock.onGet(`blocks/${blockNumber}`).reply(200, null);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,21 @@ describe('MirrorNodeClient', async function () {
649649
expect(result.number).equal(block.number);
650650
});
651651

652+
it('`getBlocks` should hit the cache', async () => {
653+
const hash = '0x3c08bbbee74d287b1dcd3f0ca6d1d2cb92c90883c4acf9747de9f3f3162ad25b999fc7e86699f60f2a3fb3ed9a646c6b';
654+
mock.onGet(`blocks/${hash}`).replyOnce(200, {
655+
'hash': '0x3c08bbbee74d287b1dcd3f0ca6d1d2cb92c90883c4acf9747de9f3f3162ad25b999fc7e86699f60f2a3fb3ed9a646c6b',
656+
'number': 77
657+
});
658+
659+
for (let i = 0; i < 3; i++) {
660+
const result = await mirrorNodeInstance.getBlock(hash);
661+
expect(result).to.exist;
662+
expect(result.hash).equal(hash);
663+
expect(result.number).equal(77);
664+
}
665+
});
666+
652667
it('`getNetworkExchangeRate`', async () => {
653668
const exchangerate = {
654669
'current_rate': {

0 commit comments

Comments
 (0)