Skip to content

Commit db24ab3

Browse files
Removing cache for getBalance in dev mode and 'blockNumberOrTagOrHash' is explicit (#1924)
* Removing cache for dev mode if blockNumberOrTagOrHash is explicitly 'latest' or 'pending' --------- Signed-off-by: Stefan Stefanov <[email protected]>
1 parent 897a658 commit db24ab3

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

packages/relay/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"compile": "tsc -b tsconfig.json",
2525
"lint": "npx eslint --ext .js,.ts .",
2626
"format": "npx prettier --ignore-path ../../.gitignore --write \"**/*.+(js|ts|json)\"",
27-
"test": "nyc ts-mocha --recursive './tests/**/*.spec.ts' './tests/**/**/*.spec.ts' --exit"
27+
"test": "nyc ts-mocha --recursive './tests/**/*.spec.ts' './tests/**/**/*.spec.ts' --exit",
28+
"test:balance": "nyc ts-mocha --recursive './tests/**/*.spec.ts' './tests/**/**/*.spec.ts' -g '@balance' --exit"
2829
},
2930
"dependencies": {
3031
"@ethersproject/asm": "^5.7.0",

packages/relay/src/lib/eth.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ export class EthImpl implements Eth {
260260
this.debugServiceImpl = new DebugService(mirrorNodeClient, logger, this.common);
261261
}
262262

263+
private shouldUseCacheForBalance(tag: string | null): boolean {
264+
// should only cache balance when is Not latest or pending and is not in dev mode
265+
return !CommonService.blockTagIsLatestOrPendingStrict(tag) && !CommonService.isDevMode;
266+
}
267+
263268
private initEthExecutionCounter(register: Registry) {
264269
const metricCounterName = 'rpc_relay_eth_executions';
265270
register.removeSingleMetric(metricCounterName);
@@ -842,6 +847,7 @@ export class EthImpl implements Eth {
842847
// this check is required, because some tools like Metamask pass for parameter latest block, with a number (ex 0x30ea)
843848
// tolerance is needed, because there is a small delay between requesting latest block from blockNumber and passing it here
844849
if (!this.common.blockTagIsLatestOrPending(blockNumberOrTagOrHash)) {
850+
let blockHashNumber, isHash;
845851
const cacheKey = `${constants.CACHE_KEY.ETH_BLOCK_NUMBER}`;
846852
const blockNumberCached = this.cacheService.get(cacheKey, EthImpl.ethGetBalance, requestIdPrefix);
847853

@@ -852,9 +858,6 @@ export class EthImpl implements Eth {
852858
latestBlock = await this.blockNumberTimestamp(EthImpl.ethGetBalance, requestIdPrefix);
853859
}
854860

855-
let blockHashNumber;
856-
let isHash;
857-
858861
if (blockNumberOrTagOrHash != null && blockNumberOrTagOrHash.length > 32) {
859862
isHash = true;
860863
blockHashNumber = await this.mirrorNodeClient.getBlock(blockNumberOrTagOrHash);
@@ -878,7 +881,7 @@ export class EthImpl implements Eth {
878881
// create a key for the cache
879882
const cacheKey = `${constants.CACHE_KEY.ETH_GET_BALANCE}-${account}-${blockNumberOrTagOrHash}`;
880883
let cachedBalance = this.cacheService.get(cacheKey, EthImpl.ethGetBalance, requestIdPrefix);
881-
if (cachedBalance) {
884+
if (cachedBalance && this.shouldUseCacheForBalance(blockNumberOrTagOrHash)) {
882885
this.logger.trace(`${requestIdPrefix} returning cached value ${cacheKey}:${JSON.stringify(cachedBalance)}`);
883886
return cachedBalance;
884887
}

packages/relay/src/lib/services/ethService/ethCommonService/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class CommonService implements ICommonService {
6161
static blockLatest = 'latest';
6262
static blockEarliest = 'earliest';
6363
static blockPending = 'pending';
64+
static isDevMode = process.env.DEV_MODE === 'true';
6465

6566
// function callerNames
6667
static latestBlockNumber = 'getLatestBlockNumber';
@@ -77,6 +78,10 @@ export class CommonService implements ICommonService {
7778
this.cacheService = cacheService;
7879
}
7980

81+
public static blockTagIsLatestOrPendingStrict(tag: string | null): boolean {
82+
return tag === CommonService.blockLatest || tag === CommonService.blockPending;
83+
}
84+
8085
public blockTagIsLatestOrPending = (tag) => {
8186
return tag == null || tag === CommonService.blockLatest || tag === CommonService.blockPending;
8287
};

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1790,9 +1790,12 @@ describe('Eth calls using MirrorNode', async function () {
17901790
});
17911791
});
17921792

1793-
describe('eth_getBalance', async function () {
1793+
describe('@balance eth_getBalance', async function () {
17941794
const defBalance = 99960581137;
1795+
const defBalanceSec = 99960581137 + 1;
17951796
const defHexBalance = numberTo0x(BigInt(defBalance) * TINYBAR_TO_WEIBAR_COEF_BIGINT);
1797+
const defHexBalanceFunc = (defBalance) => numberTo0x(BigInt(defBalance) * TINYBAR_TO_WEIBAR_COEF_BIGINT);
1798+
17961799
it('should return balance from mirror node', async () => {
17971800
restMock.onGet(`blocks?limit=1&order=desc`).reply(200, {
17981801
blocks: [
@@ -1852,6 +1855,37 @@ describe('Eth calls using MirrorNode', async function () {
18521855
expect(newBalanceHex).to.equal(resBalanceNew);
18531856
});
18541857

1858+
it('should not return balance for explicitly latest block from cache', async () => {
1859+
restMock.onGet(`blocks?limit=1&order=desc`).reply(200, {
1860+
blocks: [
1861+
{
1862+
number: 10000,
1863+
},
1864+
],
1865+
});
1866+
restMock.onGet(`accounts/${contractAddress1}?limit=100`).reply(200, {
1867+
account: contractAddress1,
1868+
balance: {
1869+
balance: defBalance,
1870+
},
1871+
});
1872+
1873+
const resBalance = await ethImpl.getBalance(contractAddress1, 'latest', getRequestId());
1874+
expect(resBalance).to.equal(defHexBalance);
1875+
restMock.onGet(`accounts/${contractAddress1}?limit=100`).reply(200, {
1876+
account: contractAddress1,
1877+
balance: {
1878+
balance: defBalance + 1,
1879+
},
1880+
});
1881+
1882+
const resBalanceCached = await ethImpl.getBalance(contractAddress1, 'latest');
1883+
expect(
1884+
resBalanceCached,
1885+
`To equal the result from the request ${defHexBalanceFunc(defBalanceSec)} not from cache ${defHexBalance}`,
1886+
).to.equal(defHexBalanceFunc(defBalanceSec));
1887+
});
1888+
18551889
it('should return balance from mirror node with block number passed as param the same as latest', async () => {
18561890
const blockNumber = '0x2710';
18571891
restMock.onGet(`blocks?limit=1&order=desc`).reply(200, {

0 commit comments

Comments
 (0)