Skip to content

Commit

Permalink
fix: resolve type warnings for ConfigService.get()
Browse files Browse the repository at this point in the history
 - Ensured type consistency in all affected test cases.
 - Removed unnecessary type casting or added necessary casting.

Signed-off-by: belloibrahv <belloibrahv@gmail.com>
  • Loading branch information
belloibrahv committed Dec 21, 2024
1 parent df2f57f commit a6285b0
Show file tree
Hide file tree
Showing 40 changed files with 334 additions and 262 deletions.
4 changes: 3 additions & 1 deletion packages/config-service/src/services/globalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*
*/

import { ConfigName } from './configName';

export interface ConfigProperty {
envName: string;
type: string;
Expand All @@ -26,7 +28,7 @@ export interface ConfigProperty {
}

export class GlobalConfig {
public static readonly ENTRIES: Record<string, ConfigProperty> = {
public static readonly ENTRIES: Record<ConfigName, ConfigProperty> = {
BATCH_REQUESTS_ENABLED: {
envName: 'BATCH_REQUESTS_ENABLED',
type: 'boolean',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { ConfigService } from '../../../src/services';
import { ConfigName } from '../../../src/services/configName';

chai.use(chaiAsPromised);

Expand Down Expand Up @@ -49,7 +50,7 @@ describe('ConfigService tests', async function () {
});

it('should be able to get existing env var', async () => {
const res = ConfigService.get('CHAIN_ID');
const res = ConfigService.get(ConfigName.CHAIN_ID);

expect(res).to.equal('0x12a');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import crypto from 'crypto';
import { ConfigService } from '../../../src/services';
import { LoggerService } from '../../../src/services/loggerService';
import { GlobalConfig } from '../../../dist/services/globalConfig';
import { ConfigName } from '../../../src/services/configName';

chai.use(chaiAsPromised);

Expand All @@ -47,8 +48,8 @@ describe('LoggerService tests', async function () {
});

it('should be able to return plain information', async () => {
const envName = GlobalConfig.ENTRIES.CHAIN_ID.envName;
const res = ConfigService.get(envName);
const envName = ConfigName.CHAIN_ID;
const res = ConfigService.get(envName) as string;

expect(LoggerService.maskUpEnv(envName, res)).to.equal(`${envName} = ${res}`);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
/*-
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -27,6 +27,7 @@ import pino, { Logger } from 'pino';
import { Registry } from 'prom-client';
import sinon from 'sinon';

import { ConfigName } from '../../../../config-service/src/services/configName';
import { HbarSpendingPlanConfigService } from '../../../src/lib/config/hbarSpendingPlanConfigService';
import { EvmAddressHbarSpendingPlanRepository } from '../../../src/lib/db/repositories/hbarLimiter/evmAddressHbarSpendingPlanRepository';
import { HbarSpendingPlanRepository } from '../../../src/lib/db/repositories/hbarLimiter/hbarSpendingPlanRepository';
Expand Down Expand Up @@ -112,7 +113,7 @@ describe('HbarSpendingPlanConfigService', function () {
});

after(async function () {
if (ConfigService.get('REDIS_ENABLED')) {
if (ConfigService.get(ConfigName.REDIS_ENABLED)) {
await cacheService.disconnectRedisClient();
}
});
Expand Down Expand Up @@ -392,7 +393,7 @@ describe('HbarSpendingPlanConfigService', function () {
it('should not delete pre-configured spending plans after default cache TTL expires', async function () {
await hbarSpendingPlanConfigService.populatePreconfiguredSpendingPlans();

await new Promise((resolve) => setTimeout(resolve, Number(ConfigService.get('CACHE_TTL'))));
await new Promise((resolve) => setTimeout(resolve, Number(ConfigService.get(ConfigName.CACHE_TTL))));

await verifySpendingPlans(spendingPlansConfig);
});
Expand Down
8 changes: 5 additions & 3 deletions packages/relay/tests/lib/eth/eth-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
*/

import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';

import { ConfigName } from '../../../../config-service/src/services/configName';
import { nanOrNumberTo0x,numberTo0x } from '../../../dist/formatters';
import constants from '../../../src/lib/constants';
import {
defaultDetailedContractResultByHash,
defaultEvmAddress,
Expand All @@ -29,8 +33,6 @@ import {
mockData,
toHex,
} from '../../helpers';
import { numberTo0x, nanOrNumberTo0x } from '../../../dist/formatters';
import constants from '../../../src/lib/constants';

export const BLOCK_TRANSACTION_COUNT = 77;
export const GAS_USED_1 = 200000;
Expand Down Expand Up @@ -119,7 +121,7 @@ export const CONTRACT_RESULT_MOCK = {
};

export const CONTRACT_CALL_DATA = '0xef641f44';
export const ETH_FEE_HISTORY_VALUE = ConfigService.get('ETH_FEE_HISTORY_FIXED') ?? true;
export const ETH_FEE_HISTORY_VALUE = ConfigService.get(ConfigName.ETH_FEE_HISTORY_FIXED) ?? true;
export const BLOCK_HASH_PREV_TRIMMED = '0xf7d6481f659c866c35391ee230c374f163642ebf13a5e604e04a95a9ca48a298';
export const BLOCK_NUMBER_HEX = `0x${BLOCK_NUMBER.toString(16)}`;
export const MAX_GAS_LIMIT = 250000;
Expand Down
3 changes: 2 additions & 1 deletion packages/relay/tests/lib/eth/eth-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import EventEmitter from 'events';
import pino from 'pino';
import { register, Registry } from 'prom-client';

import { ConfigName } from '../../../../config-service/src/services/configName';
import { ConfigServiceTestHelper } from '../../../../config-service/tests/configServiceTestHelper';
import { MirrorNodeClient } from '../../../src/lib/clients/mirrorNodeClient';
import constants from '../../../src/lib/constants';
Expand Down Expand Up @@ -55,7 +56,7 @@ export function generateEthTestEnv(fixedFeeHistory = false) {
const cacheService = new CacheService(logger.child({ name: `cache` }), registry);
// @ts-ignore
const mirrorNodeInstance = new MirrorNodeClient(
ConfigService.get('MIRROR_NODE_URL') || '',
ConfigService.get(ConfigName.MIRROR_NODE_URL) as string || '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
Expand Down
8 changes: 5 additions & 3 deletions packages/relay/tests/lib/eth/eth_common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { expect, use } from 'chai';
import { Registry } from 'prom-client';
import pino from 'pino';
import chaiAsPromised from 'chai-as-promised';
import pino from 'pino';
import { Registry } from 'prom-client';

import { ConfigName } from '../../../../config-service/src/services/configName';
import { RelayImpl } from '../../../src';
import { RequestDetails } from '../../../src/lib/types';

Expand All @@ -42,7 +44,7 @@ describe('@ethCommon', async function () {
it('should execute "eth_chainId"', async function () {
const chainId = Relay.eth().chainId(requestDetails);

expect(chainId).to.be.equal('0x' + Number(ConfigService.get('CHAIN_ID')).toString(16));
expect(chainId).to.be.equal('0x' + Number(ConfigService.get(ConfigName.CHAIN_ID)).toString(16));
});

it('should execute "eth_accounts"', async function () {
Expand Down
5 changes: 3 additions & 2 deletions packages/relay/tests/lib/eth/eth_sendRawTransaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import pino from 'pino';
import { Counter } from 'prom-client';
import sinon, { useFakeTimers } from 'sinon';

import { ConfigName } from '../../../../config-service/src/services/configName';
import { Eth, JsonRpcError, predefined } from '../../../src';
import { formatTransactionIdWithoutQueryParams } from '../../../src/formatters';
import { SDKClient } from '../../../src/lib/clients';
Expand Down Expand Up @@ -115,7 +116,7 @@ describe('@ethSendRawTransaction eth_sendRawTransaction spec', async function ()
},
};
const transaction = {
chainId: Number(ConfigService.get('CHAIN_ID') || 0x12a),
chainId: Number(ConfigService.get(ConfigName.CHAIN_ID) || 0x12a),
to: ACCOUNT_ADDRESS_1,
from: accountAddress,
value,
Expand All @@ -135,7 +136,7 @@ describe('@ethSendRawTransaction eth_sendRawTransaction spec', async function ()
},
receiver_sig_required: false,
};
const useAsyncTxProcessing = ConfigService.get('USE_ASYNC_TX_PROCESSING') as boolean;
const useAsyncTxProcessing = ConfigService.get(ConfigName.USE_ASYNC_TX_PROCESSING) as boolean;

beforeEach(() => {
clock = useFakeTimers();
Expand Down
3 changes: 2 additions & 1 deletion packages/relay/tests/lib/ethGetBlockBy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import HAPIService from '../../src/lib/services/hapiService/hapiService';
import { HbarLimitService } from '../../src/lib/services/hbarLimitService';
import { RequestDetails } from '../../src/lib/types';
import { defaultDetailedContractResults, overrideEnvsInMochaDescribe, useInMemoryRedisServer } from '../helpers';
import { ConfigName } from '../../../config-service/src/services/configName';

use(chaiAsPromised);

Expand Down Expand Up @@ -128,7 +129,7 @@ describe('eth_getBlockBy', async function () {

// @ts-ignore
mirrorNodeInstance = new MirrorNodeClient(
ConfigService.get('MIRROR_NODE_URL') ?? '',
(ConfigService.get(ConfigName.MIRROR_NODE_URL) as string) ?? '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
Expand Down
41 changes: 30 additions & 11 deletions packages/relay/tests/lib/hapiService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import EventEmitter from 'events';
import pino from 'pino';
import { register, Registry } from 'prom-client';

import { ConfigName } from '../../../config-service/src/services/configName';
import { SDKClient } from '../../src/lib/clients';
import constants from '../../src/lib/constants';
import { EvmAddressHbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/evmAddressHbarSpendingPlanRepository';
Expand Down Expand Up @@ -85,7 +86,9 @@ describe('HAPI Service', async function () {
withOverriddenEnvsInMochaTest({ HAPI_CLIENT_TRANSACTION_RESET: 2 }, () => {
it('should be able to reinitialise SDK instance upon reaching transaction limit', async function () {
hapiService = new HAPIService(logger, registry, cacheService, eventEmitter, hbarLimitService);
expect(hapiService.getTransactionCount()).to.eq(parseInt(ConfigService.get('HAPI_CLIENT_TRANSACTION_RESET')!));
expect(hapiService.getTransactionCount()).to.eq(
parseInt(ConfigService.get(ConfigName.HAPI_CLIENT_TRANSACTION_RESET)! as string),
);

const oldClientInstance = hapiService.getMainClientInstance();
let oldSDKInstance = hapiService.getSDKClient(); // decrease transaction limit by taking the instance
Expand All @@ -97,23 +100,27 @@ describe('HAPI Service', async function () {
expect(oldSDKInstance).to.not.be.equal(newSDKInstance);
expect(oldClientInstance).to.not.be.equal(newClientInstance);
expect(hapiService.getTransactionCount()).to.eq(
parseInt(ConfigService.get('HAPI_CLIENT_TRANSACTION_RESET')!) - 1,
parseInt(ConfigService.get(ConfigName.HAPI_CLIENT_TRANSACTION_RESET)! as string) - 1,
); // one less because we took the instance once and decreased the counter
});
});

withOverriddenEnvsInMochaTest({ HAPI_CLIENT_DURATION_RESET: 100 }, () => {
it('should be able to reinitialise SDK instance upon reaching time limit', async function () {
hapiService = new HAPIService(logger, registry, cacheService, eventEmitter, hbarLimitService);
expect(hapiService.getTimeUntilReset()).to.eq(parseInt(ConfigService.get('HAPI_CLIENT_DURATION_RESET')!));
expect(hapiService.getTimeUntilReset()).to.eq(
parseInt(ConfigService.get(ConfigName.HAPI_CLIENT_DURATION_RESET)! as string),
);

const oldClientInstance = hapiService.getMainClientInstance();
await new Promise((r) => setTimeout(r, 200)); // await to reach time limit
const oldSDKInstance = hapiService.getSDKClient();
const newSDKInstance = hapiService.getSDKClient();
const newClientInstance = hapiService.getMainClientInstance();

expect(hapiService.getTimeUntilReset()).to.eq(parseInt(ConfigService.get('HAPI_CLIENT_DURATION_RESET')!));
expect(hapiService.getTimeUntilReset()).to.eq(
parseInt(ConfigService.get(ConfigName.HAPI_CLIENT_DURATION_RESET)! as string),
);
expect(oldSDKInstance).to.not.be.equal(newSDKInstance);
expect(oldClientInstance).to.not.be.equal(newClientInstance);
});
Expand All @@ -122,7 +129,9 @@ describe('HAPI Service', async function () {
withOverriddenEnvsInMochaTest({ HAPI_CLIENT_ERROR_RESET: '[50]' }, () => {
it('should be able to reinitialise SDK instance upon error status code encounter', async function () {
hapiService = new HAPIService(logger, registry, cacheService, eventEmitter, hbarLimitService);
expect(hapiService.getErrorCodes()[0]).to.eq(JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')!)[0]);
expect(hapiService.getErrorCodes()[0]).to.eq(
JSON.parse(ConfigService.get(ConfigName.HAPI_CLIENT_ERROR_RESET)! as string)[0],
);

const oldClientInstance = hapiService.getMainClientInstance();
const oldSDKInstance = hapiService.getSDKClient();
Expand All @@ -132,7 +141,9 @@ describe('HAPI Service', async function () {

expect(oldSDKInstance).to.not.be.equal(newSDKInstance);
expect(oldClientInstance).to.not.be.equal(newClientInstance);
expect(hapiService.getErrorCodes()[0]).to.eq(JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')!)[0]);
expect(hapiService.getErrorCodes()[0]).to.eq(
JSON.parse(ConfigService.get(ConfigName.HAPI_CLIENT_ERROR_RESET)! as string)[0],
);
});
});

Expand All @@ -146,17 +157,23 @@ describe('HAPI Service', async function () {
it('should be able to reset all counter upon reinitialization of the SDK Client', async function () {
hapiService = new HAPIService(logger, registry, cacheService, eventEmitter, hbarLimitService);

expect(hapiService.getErrorCodes()[0]).to.eq(JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')!)[0]);
expect(hapiService.getErrorCodes()[0]).to.eq(
JSON.parse(ConfigService.get(ConfigName.HAPI_CLIENT_ERROR_RESET)! as string)[0],
);
const oldClientInstance = hapiService.getMainClientInstance();
const oldSDKInstance = hapiService.getSDKClient();
hapiService.decrementErrorCounter(errorStatus);
const newSDKInstance = hapiService.getSDKClient();
const newClientInstance = hapiService.getMainClientInstance();

expect(hapiService.getTimeUntilReset()).to.eq(parseInt(ConfigService.get('HAPI_CLIENT_DURATION_RESET')!));
expect(hapiService.getErrorCodes()[0]).to.eq(JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')!)[0]);
expect(hapiService.getTimeUntilReset()).to.eq(
parseInt(ConfigService.get(ConfigName.HAPI_CLIENT_DURATION_RESET)! as string),
);
expect(hapiService.getErrorCodes()[0]).to.eq(
JSON.parse(ConfigService.get(ConfigName.HAPI_CLIENT_ERROR_RESET)! as string)[0],
);
expect(hapiService.getTransactionCount()).to.eq(
parseInt(ConfigService.get('HAPI_CLIENT_TRANSACTION_RESET')!) - 1,
parseInt(ConfigService.get(ConfigName.HAPI_CLIENT_TRANSACTION_RESET)! as string) - 1,
); // one less because we took the instance once and decreased the counter
expect(oldSDKInstance).to.not.be.equal(newSDKInstance);
expect(oldClientInstance).to.not.be.equal(newClientInstance);
Expand Down Expand Up @@ -204,7 +221,9 @@ describe('HAPI Service', async function () {
() => {
it('should not be able to reinitialise and decrement counters, if it is disabled', async function () {
hapiService = new HAPIService(logger, registry, cacheService, eventEmitter, hbarLimitService);
expect(hapiService.getTransactionCount()).to.eq(parseInt(ConfigService.get('HAPI_CLIENT_TRANSACTION_RESET')!));
expect(hapiService.getTransactionCount()).to.eq(
parseInt(ConfigService.get(ConfigName.HAPI_CLIENT_TRANSACTION_RESET)! as string),
);

const oldClientInstance = hapiService.getMainClientInstance();
const oldSDKInstance = hapiService.getSDKClient();
Expand Down
9 changes: 5 additions & 4 deletions packages/relay/tests/lib/mirrorNodeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const registry = new Registry();
import { MirrorNodeTransactionRecord, RequestDetails } from '../../src/lib/types';
import { SDKClientError } from '../../src/lib/errors/SDKClientError';
import { BigNumber } from 'bignumber.js';
import { ConfigName } from '../../../config-service/src/services/configName';

const logger = pino();
const noTransactions = '?transactions=false';
Expand Down Expand Up @@ -63,7 +64,7 @@ describe('MirrorNodeClient', async function () {
cacheService = new CacheService(logger.child({ name: `cache` }), registry);
mirrorNodeInstance = new MirrorNodeClient(
// @ts-ignore
ConfigService.get('MIRROR_NODE_URL') || '',
ConfigService.get(ConfigName.MIRROR_NODE_URL) || '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
Expand Down Expand Up @@ -140,7 +141,7 @@ describe('MirrorNodeClient', async function () {

it('`restUrl` is exposed and correct', async () => {
// @ts-ignore
const domain = (ConfigService.get('MIRROR_NODE_URL') || '').replace(/^https?:\/\//, '');
const domain = (ConfigService.get(ConfigName.MIRROR_NODE_URL) || '').replace(/^https?:\/\//, '');
const prodMirrorNodeInstance = new MirrorNodeClient(
domain,
logger.child({ name: `mirror-node` }),
Expand All @@ -167,14 +168,14 @@ describe('MirrorNodeClient', async function () {
withOverriddenEnvsInMochaTest({ MIRROR_NODE_URL_HEADER_X_API_KEY: 'abc123iAManAPIkey' }, () => {
it('Can provide custom x-api-key header', async () => {
const mirrorNodeInstanceOverridden = new MirrorNodeClient(
ConfigService.get('MIRROR_NODE_URL') || '',
(ConfigService.get(ConfigName.MIRROR_NODE_URL) as string) || '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
);
const axiosHeaders = mirrorNodeInstanceOverridden.getMirrorNodeRestInstance().defaults.headers.common;
expect(axiosHeaders).has.property('x-api-key');
expect(axiosHeaders['x-api-key']).to.eq(ConfigService.get('MIRROR_NODE_URL_HEADER_X_API_KEY'));
expect(axiosHeaders['x-api-key']).to.eq(ConfigService.get(ConfigName.MIRROR_NODE_URL_HEADER_X_API_KEY));
});
});

Expand Down
5 changes: 3 additions & 2 deletions packages/relay/tests/lib/net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Registry } from 'prom-client';
import { RelayImpl } from '../../src/lib/relay';
import constants from '../../src/lib/constants';
import { withOverriddenEnvsInMochaTest } from '../helpers';
import { ConfigName } from '../../../config-service/src/services/configName';

const logger = pino();
let Relay;
Expand All @@ -40,8 +41,8 @@ describe('Net', async function () {
});

it('should execute "net_version"', function () {
const hederaNetwork: string = (ConfigService.get('HEDERA_NETWORK') || '{}').toLowerCase();
let expectedNetVersion = ConfigService.get('CHAIN_ID') || constants.CHAIN_IDS[hederaNetwork] || '298';
const hederaNetwork: string = ((ConfigService.get(ConfigName.HEDERA_NETWORK) as string) || '{}').toLowerCase();
let expectedNetVersion = ConfigService.get(ConfigName.CHAIN_ID) || constants.CHAIN_IDS[hederaNetwork] || '298';
if (expectedNetVersion.startsWith('0x')) expectedNetVersion = parseInt(expectedNetVersion, 16).toString();

const actualNetVersion = Relay.net().version();
Expand Down
Loading

0 comments on commit a6285b0

Please sign in to comment.