Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Resolve Type Warnings for ConfigService.get() #3350

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions packages/config-service/src/services/globalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,30 @@
*
*/

type TypeStrToType<Tstr extends string> = Tstr extends 'string'
? string
: Tstr extends 'boolean'
? boolean
: Tstr extends 'number'
? number
: Tstr extends 'array'
? unknown[]
: never;

type GetTypeStrOfKey<K extends string> = K extends keyof typeof _CONFIG
? typeof _CONFIG[K]['type']
: never;

export type TypeOfKey<K extends string> = TypeStrToType<GetTypeStrOfKey<K>>;

export interface ConfigProperty {
envName: string;
type: string;
required: boolean;
defaultValue: string | number | boolean | null;
}

export class GlobalConfig {
public static readonly ENTRIES: Record<string, ConfigProperty> = {
const _CONFIG = {
BATCH_REQUESTS_ENABLED: {
envName: 'BATCH_REQUESTS_ENABLED',
type: 'boolean',
Expand Down Expand Up @@ -568,6 +583,12 @@ export class GlobalConfig {
required: false,
defaultValue: null,
},
SERVER_HOST: {
envName: 'SERVER_HOST',
type: 'string',
required: false,
defaultValue: null,
},
SERVER_PORT: {
envName: 'SERVER_PORT',
type: 'number',
Expand Down Expand Up @@ -742,5 +763,10 @@ export class GlobalConfig {
required: false,
defaultValue: null,
},
};
} satisfies { [key: string]: ConfigProperty };

export type ConfigKey = keyof typeof _CONFIG;

export class GlobalConfig {
public static readonly ENTRIES: Record<ConfigKey, ConfigProperty> = _CONFIG;
}
6 changes: 4 additions & 2 deletions packages/config-service/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import dotenv from 'dotenv';
import findConfig from 'find-config';
import pino from 'pino';

import type { ConfigKey, TypeOfKey } from './globalConfig';
import { LoggerService } from './loggerService';
import { ValidationService } from './validationService';

Expand Down Expand Up @@ -97,7 +99,7 @@ export class ConfigService {
* @param name string
* @returns string | undefined
*/
public static get(name: string): string | number | boolean | null | undefined {
return this.getInstance().envs[name];
public static get<K extends ConfigKey>(name: K): TypeOfKey<K> | undefined {
return this.getInstance().envs[name] as TypeOfKey<K> | undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Hedera JSON RPC Relay
*
* Copyright (C) 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 @@ -21,6 +21,7 @@
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { ConfigService } from '../../../src/services';
import type { ConfigKey } from '../../../src/services/globalConfig';

chai.use(chaiAsPromised);

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

it('should return undefined for non-existing variable', async () => {
const res = ConfigService.get('NON_EXISTING_VAR');
const res = ConfigService.get('NON_EXISTING_VAR' as ConfigKey);
acuarica marked this conversation as resolved.
Show resolved Hide resolved

expect(res).to.equal(undefined);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,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 = 'CHAIN_ID';
const res = ConfigService.get(envName) as string | undefined;

expect(LoggerService.maskUpEnv(envName, res)).to.equal(`${envName} = ${res}`);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*-
*
* Hedera JSON RPC Relay
*
Expand Down
5 changes: 3 additions & 2 deletions packages/relay/tests/lib/eth/eth-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/

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

import { nanOrNumberTo0x,numberTo0x } from '../../../dist/formatters';
import constants from '../../../src/lib/constants';
import {
defaultDetailedContractResultByHash,
defaultEvmAddress,
Expand All @@ -29,8 +32,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
5 changes: 3 additions & 2 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,10 @@

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 { RelayImpl } from '../../../src';
import { RequestDetails } from '../../../src/lib/types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,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('USE_ASYNC_TX_PROCESSING');

beforeEach(() => {
clock = useFakeTimers();
Expand Down
36 changes: 27 additions & 9 deletions packages/relay/tests/lib/hapiService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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('HAPI_CLIENT_TRANSACTION_RESET')!),
);

const oldClientInstance = hapiService.getMainClientInstance();
let oldSDKInstance = hapiService.getSDKClient(); // decrease transaction limit by taking the instance
Expand All @@ -105,15 +107,19 @@ describe('HAPI Service', async function () {
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('HAPI_CLIENT_DURATION_RESET')!),
);

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('HAPI_CLIENT_DURATION_RESET')),
);
expect(oldSDKInstance).to.not.be.equal(newSDKInstance);
expect(oldClientInstance).to.not.be.equal(newClientInstance);
});
Expand All @@ -122,7 +128,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('HAPI_CLIENT_ERROR_RESET')!)[0],
);

const oldClientInstance = hapiService.getMainClientInstance();
const oldSDKInstance = hapiService.getSDKClient();
Expand All @@ -132,7 +140,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('HAPI_CLIENT_ERROR_RESET')!)[0],
);
});
});

Expand All @@ -146,15 +156,21 @@ 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('HAPI_CLIENT_ERROR_RESET')!)[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('HAPI_CLIENT_DURATION_RESET')!),
);
expect(hapiService.getErrorCodes()[0]).to.eq(
JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')!)[0],
);
expect(hapiService.getTransactionCount()).to.eq(
parseInt(ConfigService.get('HAPI_CLIENT_TRANSACTION_RESET')!) - 1,
); // one less because we took the instance once and decreased the counter
Expand Down Expand Up @@ -204,7 +220,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('HAPI_CLIENT_TRANSACTION_RESET')!),
);

const oldClientInstance = hapiService.getMainClientInstance();
const oldSDKInstance = hapiService.getSDKClient();
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/mirrorNodeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ 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('MIRROR_NODE_URL')) || '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Net', async function () {
});

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

Expand Down
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/openrpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('Open RPC Specification', function () {
const cacheService = new CacheService(logger.child({ name: `cache` }), registry);
// @ts-ignore
mirrorNodeInstance = new MirrorNodeClient(
ConfigService.get('MIRROR_NODE_URL') || '',
(ConfigService.get('MIRROR_NODE_URL')) || '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
Expand Down
11 changes: 7 additions & 4 deletions packages/relay/tests/lib/poller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
*/

import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { EthImpl } from '../../src/lib/eth';
import { expect } from 'chai';
import pino from 'pino';
import { Poller } from '../../src/lib/poller';
import sinon from 'sinon';
import { Registry } from 'prom-client';
import sinon from 'sinon';

import { EthImpl } from '../../src/lib/eth';
import { Poller } from '../../src/lib/poller';

const logger = pino({ level: 'trace' });

Expand Down Expand Up @@ -187,7 +188,9 @@ describe('Polling', async function () {
),
).to.equal(true);
expect(
loggerSpy.calledWith(`Poller: Starting polling with interval=${ConfigService.get('WS_POLLING_INTERVAL')}`),
loggerSpy.calledWith(
`Poller: Starting polling with interval=${ConfigService.get('WS_POLLING_INTERVAL')}`,
),
).to.equal(true);
});

Expand Down
10 changes: 5 additions & 5 deletions packages/relay/tests/lib/sdkClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ describe('SdkClient', async function () {
overrideEnvsInMochaDescribe({ GET_RECORD_DEFAULT_TO_CONSENSUS_NODE: true });

before(() => {
const hederaNetwork = ConfigService.get('HEDERA_NETWORK')!;
const hederaNetwork = ConfigService.get('HEDERA_NETWORK')! as string;
if (hederaNetwork in constants.CHAIN_IDS) {
client = Client.forName(hederaNetwork);
} else {
client = Client.forNetwork(JSON.parse(hederaNetwork));
}

client = client.setOperator(
AccountId.fromString(ConfigService.get('OPERATOR_ID_MAIN')!),
Utils.createPrivateKeyBasedOnFormat(ConfigService.get('OPERATOR_KEY_MAIN')!),
AccountId.fromString(ConfigService.get('OPERATOR_ID_MAIN')! as string),
Utils.createPrivateKeyBasedOnFormat(ConfigService.get('OPERATOR_KEY_MAIN')! as string),
);
const duration = constants.HBAR_RATE_LIMIT_DURATION;
eventEmitter = new EventEmitter();
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('SdkClient', async function () {

// mirror node client
mirrorNodeClient = new MirrorNodeClient(
ConfigService.get('MIRROR_NODE_URL') || '',
(ConfigService.get('MIRROR_NODE_URL')) || '',
logger.child({ name: `mirror-node` }),
registry,
new CacheService(logger.child({ name: `cache` }), registry),
Expand Down Expand Up @@ -2743,7 +2743,7 @@ describe('SdkClient', async function () {
});

it('Should execute getTransferAmountSumForAccount() to calculate transactionFee by only transfers that are paid by the specify accountId', () => {
const accountId = ConfigService.get('OPERATOR_ID_MAIN') || '';
const accountId = (ConfigService.get('OPERATOR_ID_MAIN')) || '';
const mockedTxRecord = getMockedTransactionRecord(EthereumTransaction.name, true);

const transactionFee = sdkClient.getTransferAmountSumForAccount(mockedTxRecord, accountId);
Expand Down
17 changes: 9 additions & 8 deletions packages/relay/tests/lib/services/debugService/debug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@
*/

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

import { predefined } from '../../../../src';
import { strip0x } from '../../../../src/formatters';
import { MirrorNodeClient } from '../../../../src/lib/clients';
import pino from 'pino';
import { IOpcodesResponse } from '../../../../src/lib/clients/models/IOpcodesResponse';
import { TracerType } from '../../../../src/lib/constants';
import { DebugService } from '../../../../src/lib/services/debugService';
import { getQueryParams, withOverriddenEnvsInMochaTest } from '../../../helpers';
import RelayAssertions from '../../../assertions';
import { predefined } from '../../../../src';
import { CacheService } from '../../../../src/lib/services/cacheService/cacheService';
import { DebugService } from '../../../../src/lib/services/debugService';
import { CommonService } from '../../../../src/lib/services/ethService';
import { IOpcodesResponse } from '../../../../src/lib/clients/models/IOpcodesResponse';
import { strip0x } from '../../../../src/formatters';
import { RequestDetails } from '../../../../src/lib/types';
import RelayAssertions from '../../../assertions';
import { getQueryParams, withOverriddenEnvsInMochaTest } from '../../../helpers';

chai.use(chaiAsPromised);

Expand Down
Loading