Skip to content

Commit

Permalink
Refinment of ConfigService.get() (to conform to newly suggested changes)
Browse files Browse the repository at this point in the history
Signed-off-by: belloibrahv <belloibrahv@gmail.com>
  • Loading branch information
belloibrahv committed Dec 26, 2024
1 parent f62379c commit c7e6a08
Show file tree
Hide file tree
Showing 41 changed files with 146 additions and 139 deletions.
6 changes: 6 additions & 0 deletions packages/config-service/src/services/globalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ const _CONFIG = {
required: false,
defaultValue: null,
},
SERVER_HOST: {
envName: 'SERVER_HOST',
type: 'string',
required: false,
defaultValue: null,
},
SERVER_PORT: {
envName: 'SERVER_PORT',
type: 'number',
Expand Down
3 changes: 2 additions & 1 deletion packages/config-service/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import findConfig from 'find-config';
import pino from 'pino';
import { LoggerService } from './loggerService';
import { ValidationService } from './validationService';
import { ConfigKey } from './globalConfig';

const mainLogger = pino({
name: 'hedera-json-rpc-relay',
Expand Down Expand Up @@ -97,7 +98,7 @@ export class ConfigService {
* @param name string
* @returns string | undefined
*/
public static get(name: string): string | number | boolean | null | undefined {
public static get(name: ConfigKey): string | number | boolean | null | undefined {
return this.getInstance().envs[name];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { ConfigService } from '../../../src/services';
import { ConfigKey } from '../../../src/services/globalConfig';
import type { ConfigKey } from '../../../src/services/globalConfig';

chai.use(chaiAsPromised);

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

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

expect(res).to.equal('0x12a');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('LoggerService tests', async function () {
});

it('should be able to return plain information', async () => {
const envName = 'CHAIN_ID' as ConfigKey;
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
Expand Up @@ -113,7 +113,7 @@ describe('HbarSpendingPlanConfigService', function () {
});

after(async function () {
if (ConfigService.get('REDIS_ENABLED' as ConfigKey)) {
if (ConfigService.get('REDIS_ENABLED')) {
await cacheService.disconnectRedisClient();
}
});
Expand Down Expand Up @@ -393,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' as ConfigKey))));
await new Promise((resolve) => setTimeout(resolve, Number(ConfigService.get('CACHE_TTL'))));

await verifySpendingPlans(spendingPlansConfig);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/eth/eth-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,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' as ConfigKey) ?? true;
export const ETH_FEE_HISTORY_VALUE = ConfigService.get('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
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/eth/eth-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,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' as ConfigKey) || '',
ConfigService.get('MIRROR_NODE_URL') || '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/eth/eth_common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,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' as ConfigKey)).toString(16));
expect(chainId).to.be.equal('0x' + Number(ConfigService.get('CHAIN_ID')).toString(16));
});

it('should execute "eth_accounts"', async function () {
Expand Down
4 changes: 2 additions & 2 deletions packages/relay/tests/lib/eth/eth_sendRawTransaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('@ethSendRawTransaction eth_sendRawTransaction spec', async function ()
},
};
const transaction = {
chainId: Number(ConfigService.get('CHAIN_ID' as ConfigKey) || 0x12a),
chainId: Number(ConfigService.get('CHAIN_ID') || 0x12a),
to: ACCOUNT_ADDRESS_1,
from: accountAddress,
value,
Expand All @@ -136,7 +136,7 @@ describe('@ethSendRawTransaction eth_sendRawTransaction spec', async function ()
},
receiver_sig_required: false,
};
const useAsyncTxProcessing = ConfigService.get('USE_ASYNC_TX_PROCESSING' as ConfigKey);
const useAsyncTxProcessing = ConfigService.get('USE_ASYNC_TX_PROCESSING');

beforeEach(() => {
clock = useFakeTimers();
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/ethGetBlockBy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('eth_getBlockBy', async function () {

// @ts-ignore
mirrorNodeInstance = new MirrorNodeClient(
(ConfigService.get('MIRROR_NODE_URL') as ConfigKey) ?? '',
(ConfigService.get('MIRROR_NODE_URL')) ?? '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
Expand Down
22 changes: 11 additions & 11 deletions packages/relay/tests/lib/hapiService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('HAPI Service', async function () {
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')! as ConfigKey),
parseInt(ConfigService.get('HAPI_CLIENT_TRANSACTION_RESET')!),
);

const oldClientInstance = hapiService.getMainClientInstance();
Expand All @@ -100,7 +100,7 @@ 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')! as ConfigKey) - 1,
parseInt(ConfigService.get('HAPI_CLIENT_TRANSACTION_RESET')!) - 1,
); // one less because we took the instance once and decreased the counter
});
});
Expand All @@ -109,7 +109,7 @@ describe('HAPI Service', async function () {
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')! as ConfigKey),
parseInt(ConfigService.get('HAPI_CLIENT_DURATION_RESET')!),
);

const oldClientInstance = hapiService.getMainClientInstance();
Expand All @@ -119,7 +119,7 @@ describe('HAPI Service', async function () {
const newClientInstance = hapiService.getMainClientInstance();

expect(hapiService.getTimeUntilReset()).to.eq(
parseInt(ConfigService.get('HAPI_CLIENT_DURATION_RESET')! as ConfigKey),
parseInt(ConfigService.get('HAPI_CLIENT_DURATION_RESET')),
);
expect(oldSDKInstance).to.not.be.equal(newSDKInstance);
expect(oldClientInstance).to.not.be.equal(newClientInstance);
Expand All @@ -130,7 +130,7 @@ describe('HAPI Service', async function () {
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')! as ConfigKey)[0],
JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')!)[0],
);

const oldClientInstance = hapiService.getMainClientInstance();
Expand All @@ -142,7 +142,7 @@ 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')! as ConfigKey)[0],
JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')!)[0],
);
});
});
Expand All @@ -158,7 +158,7 @@ describe('HAPI Service', async function () {
hapiService = new HAPIService(logger, registry, cacheService, eventEmitter, hbarLimitService);

expect(hapiService.getErrorCodes()[0]).to.eq(
JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')! as ConfigKey)[0],
JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')!)[0],
);
const oldClientInstance = hapiService.getMainClientInstance();
const oldSDKInstance = hapiService.getSDKClient();
Expand All @@ -167,13 +167,13 @@ describe('HAPI Service', async function () {
const newClientInstance = hapiService.getMainClientInstance();

expect(hapiService.getTimeUntilReset()).to.eq(
parseInt(ConfigService.get('HAPI_CLIENT_DURATION_RESET')! as ConfigKey),
parseInt(ConfigService.get('HAPI_CLIENT_DURATION_RESET')!),
);
expect(hapiService.getErrorCodes()[0]).to.eq(
JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')! as ConfigKey)[0],
JSON.parse(ConfigService.get('HAPI_CLIENT_ERROR_RESET')!)[0],
);
expect(hapiService.getTransactionCount()).to.eq(
parseInt(ConfigService.get('HAPI_CLIENT_TRANSACTION_RESET')! as ConfigKey) - 1,
parseInt(ConfigService.get('HAPI_CLIENT_TRANSACTION_RESET')!) - 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 @@ -222,7 +222,7 @@ 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')! as ConfigKey),
parseInt(ConfigService.get('HAPI_CLIENT_TRANSACTION_RESET')!),
);

const oldClientInstance = hapiService.getMainClientInstance();
Expand Down
8 changes: 4 additions & 4 deletions packages/relay/tests/lib/mirrorNodeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,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' as ConfigKey) || '',
ConfigService.get('MIRROR_NODE_URL') || '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('MirrorNodeClient', async function () {

it('`restUrl` is exposed and correct', async () => {
// @ts-ignore
const domain = (ConfigService.get('MIRROR_NODE_URL' as ConfigKey) || '').replace(/^https?:\/\//, '');
const domain = (ConfigService.get('MIRROR_NODE_URL') || '').replace(/^https?:\/\//, '');
const prodMirrorNodeInstance = new MirrorNodeClient(
domain,
logger.child({ name: `mirror-node` }),
Expand All @@ -168,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' as ConfigKey)) || '',
(ConfigService.get('MIRROR_NODE_URL')) || '',
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' as ConfigKey));
expect(axiosHeaders['x-api-key']).to.eq(ConfigService.get('MIRROR_NODE_URL_HEADER_X_API_KEY'));
});
});

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 @@ -41,7 +41,7 @@ describe('Net', async function () {
});

it('should execute "net_version"', function () {
const hederaNetwork: string = ((ConfigService.get('HEDERA_NETWORK') as ConfigKey) || '{}').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' as ConfigKey)) || '',
(ConfigService.get('MIRROR_NODE_URL')) || '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/poller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('Polling', async function () {
).to.equal(true);
expect(
loggerSpy.calledWith(
`Poller: Starting polling with interval=${ConfigService.get('WS_POLLING_INTERVAL' as ConfigKey)}`,
`Poller: Starting polling with interval=${ConfigService.get('WS_POLLING_INTERVAL')}`,
),
).to.equal(true);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/precheck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('Precheck', async function () {

// @ts-ignore
const mirrorNodeInstance = new MirrorNodeClient(
ConfigService.get('MIRROR_NODE_URL' as ConfigKey)!,
ConfigService.get('MIRROR_NODE_URL')!,
logger.child({ name: `mirror-node` }),
registry,
new CacheService(logger.child({ name: `cache` }), registry),
Expand Down
28 changes: 14 additions & 14 deletions packages/relay/tests/lib/sdkClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ describe('SdkClient', async function () {
overrideEnvsInMochaDescribe({ GET_RECORD_DEFAULT_TO_CONSENSUS_NODE: true });

before(() => {
const hederaNetwork = ConfigService.get('HEDERA_NETWORK' as ConfigKey)! as string;
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' as ConfigKey)! as string),
Utils.createPrivateKeyBasedOnFormat(ConfigService.get('OPERATOR_KEY_MAIN' as ConfigKey)! as string),
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 @@ -149,7 +149,7 @@ describe('SdkClient', async function () {

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

this.beforeEach(() => {
if (ConfigService.get('OPERATOR_KEY_FORMAT' as ConfigKey) !== 'BAD_FORMAT' as ConfigKey) {
if (ConfigService.get('OPERATOR_KEY_FORMAT') !== 'BAD_FORMAT') {
hapiService = new HAPIService(logger, registry, cacheService, eventEmitter, hbarLimitService);
}
});
Expand Down Expand Up @@ -336,8 +336,8 @@ describe('SdkClient', async function () {
});

describe('HBAR Limiter', async () => {
const FILE_APPEND_CHUNK_SIZE = Number(ConfigService.get('FILE_APPEND_CHUNK_SIZE' as ConfigKey)) || 5120;
const MAX_CHUNKS = Number(ConfigService.get('FILE_APPEND_MAX_CHUNKS' as ConfigKey)) || 20;
const FILE_APPEND_CHUNK_SIZE = Number(ConfigService.get('FILE_APPEND_CHUNK_SIZE')) || 5120;
const MAX_CHUNKS = Number(ConfigService.get('FILE_APPEND_MAX_CHUNKS')) || 20;
const transactionBuffer = new Uint8Array([
2, 249, 250, 182, 130, 1, 42, 7, 1, 133, 209, 56, 92, 123, 240, 131, 228, 225, 192, 148, 61, 176, 51, 137, 34,
205, 229, 74, 102, 224, 197, 133, 1, 18, 73, 145, 93, 50, 210, 37, 134, 9, 24, 78, 114, 160, 0, 185, 250, 68, 130,
Expand Down Expand Up @@ -2145,7 +2145,7 @@ describe('SdkClient', async function () {
transactionFee = toHbar ? new Hbar(fileCreateFee / 10 ** 8) : fileCreateFee;
transfers = [
{
accountId: ConfigService.get('OPERATOR_ID_MAIN' as ConfigKey),
accountId: ConfigService.get('OPERATOR_ID_MAIN'),
amount: Hbar.fromTinybars(-1 * fileCreateFee),
is_approval: false,
},
Expand All @@ -2155,7 +2155,7 @@ describe('SdkClient', async function () {
transactionFee = toHbar ? new Hbar(fileAppendFee / 10 ** 8) : fileAppendFee;
transfers = [
{
accountId: ConfigService.get('OPERATOR_ID_MAIN' as ConfigKey),
accountId: ConfigService.get('OPERATOR_ID_MAIN'),
amount: Hbar.fromTinybars(-1 * fileAppendFee),
is_approval: false,
},
Expand All @@ -2165,7 +2165,7 @@ describe('SdkClient', async function () {
transactionFee = toHbar ? new Hbar(fileDeleteFee / 10 ** 8) : fileDeleteFee;
transfers = [
{
accountId: ConfigService.get('OPERATOR_ID_MAIN' as ConfigKey),
accountId: ConfigService.get('OPERATOR_ID_MAIN'),
amount: Hbar.fromTinybars(-1 * fileDeleteFee),
is_approval: false,
},
Expand All @@ -2180,12 +2180,12 @@ describe('SdkClient', async function () {
is_approval: false,
},
{
accountId: ConfigService.get('OPERATOR_ID_MAIN' as ConfigKey),
accountId: ConfigService.get('OPERATOR_ID_MAIN'),
amount: Hbar.fromTinybars(-1 * defaultTransactionFee),
is_approval: false,
},
{
accountId: ConfigService.get('OPERATOR_ID_MAIN' as ConfigKey),
accountId: ConfigService.get('OPERATOR_ID_MAIN'),
amount: Hbar.fromTinybars(defaultTransactionFee),
is_approval: false,
},
Expand Down Expand Up @@ -2651,7 +2651,7 @@ describe('SdkClient', async function () {
is_approval: false,
},
{
account: ConfigService.get('OPERATOR_ID_MAIN' as ConfigKey),
account: ConfigService.get('OPERATOR_ID_MAIN'),
amount: -1 * defaultTransactionFee,
is_approval: false,
},
Expand Down Expand Up @@ -2744,7 +2744,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' as ConfigKey) as string) || '';
const accountId = (ConfigService.get('OPERATOR_ID_MAIN')) || '';
const mockedTxRecord = getMockedTransactionRecord(EthereumTransaction.name, true);

const transactionFee = sdkClient.getTransferAmountSumForAccount(mockedTxRecord, accountId);
Expand Down
Loading

0 comments on commit c7e6a08

Please sign in to comment.