Skip to content

Commit 12972d6

Browse files
committed
chore: resolve comments
Signed-off-by: nikolay <n.atanasow94@gmail.com>
1 parent 805bd7e commit 12972d6

File tree

6 files changed

+26
-52
lines changed

6 files changed

+26
-52
lines changed

packages/relay/src/lib/decorators/cache.decorator.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ const shouldSkipCachingForSingleParams = (args: IArguments, params: CacheSingleP
4545
if (values.indexOf(args[item.index]) > -1) {
4646
return true;
4747
}
48+
49+
// do not cache optional parameters like 'blockNumber' on 'eth_getStorageAt'
50+
if (!args.hasOwnProperty(item.index)) {
51+
return true;
52+
}
4853
}
4954

5055
return false;
@@ -71,16 +76,23 @@ const shouldSkipCachingForSingleParams = (args: IArguments, params: CacheSingleP
7176
* }]
7277
*/
7378
const shouldSkipCachingForNamedParams = (args: IArguments, params: CacheNamedParams[] = []): boolean => {
74-
for (const item of params) {
75-
const input = args[item.index];
76-
// @ts-ignore
77-
const skipList = Object.assign({}, ...params.filter(el => el.index == item.index)[0].fields.map(el => {
78-
return { [el.name]: el.value };
79-
}));
79+
for (const { index, fields } of params) {
80+
const input = args[index];
8081

82+
// build a map from field names to their match values
83+
const skipList: Record<string, string> = Object.fromEntries(
84+
fields.map(({ name, value }) => [name, value])
85+
);
86+
87+
// check each field in the skip list
8188
for (const [key, value] of Object.entries(skipList)) {
82-
const values = (value as string).split('|');
83-
if (values.indexOf(input[key]) > -1) {
89+
// convert "latest|safe" to ["latest", "safe"]
90+
const allowedValues = value.split('|');
91+
// get the actual value from the input object
92+
const actualValue = (input as Record<string, any>)[key];
93+
94+
// if the actual value is one of the values that should skip caching, return true
95+
if (allowedValues.includes(actualValue)) {
8496
return true;
8597
}
8698
}
@@ -132,9 +144,8 @@ const generateCacheKey = (methodName: string, args: IArguments) => {
132144
* @returns The first found `RequestDetails` instance, or a new one with default values if none is found.
133145
*/
134146
const extractRequestDetails = (args: IArguments): RequestDetails => {
135-
for (const [, value] of Object.entries(args)) {
136-
if (value?.constructor?.name == 'RequestDetails') {
137-
// @ts-ignore
147+
for (const value of Array.from(args)) {
148+
if (value instanceof RequestDetails) {
138149
return value;
139150
}
140151
}

packages/relay/src/lib/eth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ export class EthImpl implements Eth {
122122
this.logger = logger;
123123
this.common = new CommonService(mirrorNodeClient, logger, cacheService);
124124
this.filterService = new FilterService(mirrorNodeClient, logger, cacheService, this.common);
125-
this.feeService = new FeeService(mirrorNodeClient, this.common, logger, cacheService);
125+
this.feeService = new FeeService(mirrorNodeClient, this.common, logger);
126126
this.contractService = new ContractService(cacheService, this.common, hapiService, logger, mirrorNodeClient);
127127
this.accountService = new AccountService(cacheService, this.common, logger, mirrorNodeClient);
128-
this.blockService = new BlockService(cacheService, chain, this.common, mirrorNodeClient, logger);
128+
this.blockService = new BlockService(chain, this.common, mirrorNodeClient, logger);
129129
this.eventEmitter = eventEmitter;
130130
this.transactionService = new TransactionService(
131131
cacheService,

packages/relay/src/lib/services/ethService/blockService/BlockService.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ import { IBlockService, ICommonService } from '../../index';
1818
import { CommonService } from '../ethCommonService/CommonService';
1919

2020
export class BlockService implements IBlockService {
21-
/**
22-
* The cache service used for caching all responses.
23-
* @private
24-
*/
25-
private readonly cacheService: CacheService;
2621

2722
/**
2823
* The chain id.
@@ -53,25 +48,13 @@ export class BlockService implements IBlockService {
5348
*/
5449
private readonly mirrorNodeClient: MirrorNodeClient;
5550

56-
/**
57-
* The static method for the eth_getBlockByHash RPC call.
58-
*/
59-
private static ethGetBlockByHash = 'eth_GetBlockByHash';
60-
61-
/**
62-
* The static method for the eth_getBlockByNumber RPC call.
63-
*/
64-
private static ethGetBlockByNumber = 'eth_GetBlockByNumber';
65-
6651
/** Constructor */
6752
constructor(
68-
cacheService: CacheService,
6953
chain: string,
7054
common: ICommonService,
7155
mirrorNodeClient: MirrorNodeClient,
7256
logger: Logger,
7357
) {
74-
this.cacheService = cacheService;
7558
this.chain = chain;
7659
this.common = common;
7760
this.mirrorNodeClient = mirrorNodeClient;

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,6 @@ export class CommonService implements ICommonService {
6262
public static readonly isDevMode = ConfigService.get('DEV_MODE');
6363
public static readonly latestBlockNumber = 'getLatestBlockNumber';
6464

65-
/**
66-
* private constants
67-
* @private
68-
*/
69-
private readonly ethBlockNumberCacheTtlMs = parseNumericEnvVar(
70-
'ETH_BLOCK_NUMBER_CACHE_TTL_MS',
71-
'ETH_BLOCK_NUMBER_CACHE_TTL_MS_DEFAULT',
72-
);
73-
private readonly ethGasPriceCacheTtlMs = parseNumericEnvVar(
74-
'ETH_GET_GAS_PRICE_CACHE_TTL_MS',
75-
'ETH_GET_GAS_PRICE_CACHE_TTL_MS_DEFAULT',
76-
);
7765
private readonly maxBlockRange = parseNumericEnvVar('MAX_BLOCK_RANGE', 'MAX_BLOCK_RANGE');
7866
private readonly maxTimestampParamRange = 604800; // 7 days
7967

packages/relay/src/lib/services/ethService/feeService/FeeService.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ export class FeeService implements IFeeService {
3535
*/
3636
private readonly logger: Logger;
3737

38-
/**
39-
* The service used for caching items from requests.
40-
*
41-
* @private
42-
*/
43-
private readonly cacheService: CacheService;
44-
4538
/**
4639
* Constructor
4740
*
@@ -50,11 +43,10 @@ export class FeeService implements IFeeService {
5043
* @param logger
5144
* @param cacheService
5245
*/
53-
constructor(mirrorNodeClient: MirrorNodeClient, common: ICommonService, logger: Logger, cacheService: CacheService) {
46+
constructor(mirrorNodeClient: MirrorNodeClient, common: ICommonService, logger: Logger) {
5447
this.mirrorNodeClient = mirrorNodeClient;
5548
this.common = common;
5649
this.logger = logger;
57-
this.cacheService = cacheService;
5850
}
5951

6052
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('eth_getBlockBy', async function () {
141141
// @ts-ignore
142142
ethImpl = new EthImpl(hapiServiceInstance, mirrorNodeInstance, logger, '0x12a', cacheService, eventEmitter);
143143
const common = new CommonService(mirrorNodeInstance, logger, cacheService);
144-
blockService = new BlockService(cacheService, '0x12a', common, mirrorNodeInstance, logger);
144+
blockService = new BlockService('0x12a', common, mirrorNodeInstance, logger);
145145
});
146146

147147
this.beforeEach(async () => {

0 commit comments

Comments
 (0)