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 10 commits into
base: main
Choose a base branch
from

Conversation

belloibrahv
Copy link
Contributor

@belloibrahv belloibrahv commented Dec 21, 2024

Description:
This PR addresses type inconsistencies in the test specifications related to the ConfigService.get(<envName>) method. The changes ensure type safety and consistency across all affected test cases by aligning with the expected types defined in the ConfigService.get() method.

Key Changes:

  • Created a new file named envName.ts to define and manage environment variable types.
  • Refactored test cases to use the envName file, ensuring type consistency with ConfigService.get().
  • Removed unnecessary type casting where applicable and added necessary casting to eliminate TypeScript warnings and errors.
  • Improved overall type safety in the codebase.

Related issue(s):
Fixes #3142


Notes for Reviewer:

  • Type mismatches were prevalent in various test cases, leading to TypeScript warnings/errors. This PR systematically addresses those issues by aligning the test code with the ConfigService.get() method's expected types.
  • Refactoring also included ensuring that variable types are explicitly defined and consistent across the codebase.
  • Suggestions from the issue discussion (e.g., leveraging a ConfigName enum) were incorporated where feasible.

Checklist:

  • Documented: Added/updated relevant code comments and documentation.
  • Tested: Verified changes with updated and new unit tests to ensure correctness.

 - Ensured type consistency in all affected test cases.
 - Removed unnecessary type casting or added necessary casting.

Signed-off-by: belloibrahv <[email protected]>
 - Ensured type consistency in all affected test cases.
 - Removed unnecessary type casting or added necessary casting.

Signed-off-by: belloibrahv <[email protected]>
@belloibrahv belloibrahv changed the title fix: resolve type warnings for ConfigService.get() Fix: Resolve Type Warnings for ConfigService.get() Dec 21, 2024
@belloibrahv
Copy link
Contributor Author

@Nana-EC @ebadiere @quiet-node I've submitted a PR addressing issue #3142 to resolve type warnings for ConfigService.get(). It ensures type consistency across test cases by refining type usage and adding/removing the necessary casting. Let me know if further changes are needed. Thank you for reviewing!

@@ -26,7 +28,7 @@ export interface ConfigProperty {
}

export class GlobalConfig {
public static readonly ENTRIES: Record<string, ConfigProperty> = {
public static readonly ENTRIES: Record<ConfigName, ConfigProperty> = {
Copy link
Contributor

@acuarica acuarica Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of enumerating the keys manually, can we do something like

const _CONFIG = {
  BATCH_REQUESTS_ENABLED: {
    envName: 'BATCH_REQUESTS_ENABLED',
    type: 'boolean',
    required: false,
    defaultValue: null,
  },
  //...
  WS_SUBSCRIPTION_LIMIT: {
    envName: 'WS_SUBSCRIPTION_LIMIT',
    type: 'number',
    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;
}

?

So we retain type safety for the keys (even if they're strings).

Note that we can use satisfies because we're using TypeScript 4.9.5 as defined in package-lock.json.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh I like the suggestion! Looks cleaner for sure 👏🏼

Copy link
Collaborator

@Nana-EC Nana-EC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution.

*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
* Copyright (C) 2024 Hedera Hashgraph, LLC
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: revert

*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
* Copyright (C) 2024 Hedera Hashgraph, LLC
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this and any other license header changes where you're changing an old header date into this year.
Might be a bug in an automated script


describe('@server-config Server Configuration Options Coverage', function () {
describe('Koa Server Timeout', () => {
it('should timeout a request after the specified time', async () => {
const requestTimeoutMs: number = parseInt(ConfigService.get('SERVER_REQUEST_TIMEOUT_MS') || '3000');
const requestTimeoutMs: number = parseInt(ConfigService.get(ConfigName.SERVER_REQUEST_TIMEOUT_MS) as string || '3000');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should add a getString and do the casting in the ConfigService to make this easier on devs.
Thinking out loud and getting thoughts.
No need to change in this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion about ConfigService.getString. I agree that it could streamline type handling. I suggest we open a separate issue to implement and test this change comprehensively.

@quiet-node quiet-node added this to the 0.64.0 milestone Dec 23, 2024
@quiet-node quiet-node added the internal For changes that affect the project's internal workings but not its outward-facing functionality. label Dec 23, 2024
Copy link

codecov bot commented Dec 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 49.70%. Comparing base (13d09e9) to head (a6285b0).

❗ There is a different number of reports uploaded between BASE (13d09e9) and HEAD (a6285b0). Click for more details.

HEAD has 18 uploads less than BASE
Flag BASE (13d09e9) HEAD (a6285b0)
config-service 1 0
relay 1 0
17 1
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #3350       +/-   ##
===========================================
- Coverage   85.04%   49.70%   -35.35%     
===========================================
  Files          65       66        +1     
  Lines        4508     4601       +93     
  Branches     1023     1023               
===========================================
- Hits         3834     2287     -1547     
- Misses        330     1926     +1596     
- Partials      344      388       +44     
Flag Coverage Δ
config-service ?
relay ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/config-service/src/services/configName.ts 100.00% <100.00%> (ø)
...ckages/config-service/src/services/globalConfig.ts 100.00% <100.00%> (ø)

... and 45 files with indirect coverage changes

@belloibrahv belloibrahv requested review from a team as code owners December 25, 2024 07:55
@belloibrahv belloibrahv force-pushed the 3142-fix-configservice-get-type-warnings branch from 27253f5 to 7f440e4 Compare December 25, 2024 08:54
@belloibrahv
Copy link
Contributor Author

@Nana-EC @acuarica @quiet-node, Could you please review my updates and let me know if further adjustments are needed?

Copy link
Contributor

@acuarica acuarica left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @belloibrahv, thanks for taking the time to send this, it's looking good. Left a couple of suggestions we can continue iterating on to improve the config.

@@ -49,13 +50,13 @@ 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('CHAIN_ID' as ConfigKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change the definition of ConfigService.get to get(name: ConfigKey)? So we can then avoid all these type assertions, i.e., as ConfigKey.

Not related with your PR, but I tried this change locally and I saw there was a configuration entry missing, SERVER_HOST. If we change the definition of get as mentioned above, we might want add a SERVER_HOST entry.

@acuarica
Copy link
Contributor

Also @belloibrahv please take a look at #3350 (comment).

@quiet-node
Copy link
Member

@quiet-node
Copy link
Member

Hey @belloibrahv just reacing out to see how things are going. Let us know if you face any challenges or blockers! The team is here to help.

@quiet-node quiet-node modified the milestones: 0.64.0, 0.64.1, 0.65.0 Jan 15, 2025
@belloibrahv
Copy link
Contributor Author

@acuarica, @quiet-node I have refactored the code and reverted the requested changes. Kindly review the updates at your convenience. Thank you!

@belloibrahv belloibrahv force-pushed the 3142-fix-configservice-get-type-warnings branch from 704aba5 to 65432e8 Compare January 16, 2025 04:13
@quiet-node
Copy link
Member

@belloibrahv nice thanks for the effort! However, all the CIs are broken now. It seems like the build of the Relay is failing. Didn't have a chance to go through your new updates yet but seems like they cause the build failure. Please take a look.

Signed-off-by: belloibrahv <[email protected]>
@belloibrahv belloibrahv force-pushed the 3142-fix-configservice-get-type-warnings branch from 65432e8 to ea9d40f Compare January 18, 2025 00:27
@belloibrahv belloibrahv requested a review from Nana-EC January 18, 2025 00:31
@belloibrahv
Copy link
Contributor Author

quiet-node Resolved CI issues. Kindly help review the changes. Thank you

@belloibrahv
Copy link
Contributor Author

@acuarica @quiet-node @Nana-EC I've addressed the failing CI checks in this PR. Please take another look at the updated code. I'm eager to get feedback and address any remaining concerns. Thank You.

Copy link
Contributor

@acuarica acuarica left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's looking good, left a couple of questions. We are getting close!

packages/relay/tests/lib/sdkClient.spec.ts Outdated Show resolved Hide resolved
packages/relay/tests/lib/sdkClient.spec.ts Outdated Show resolved Hide resolved
packages/relay/tests/lib/sdkClient.spec.ts Outdated Show resolved Hide resolved
Signed-off-by: belloibrahv <[email protected]>
@belloibrahv
Copy link
Contributor Author

@acuarica I have added the required change, Kindly check when you free.

Signed-off-by: belloibrahv <[email protected]>
acuarica
acuarica previously approved these changes Jan 22, 2025
Copy link
Contributor

@acuarica acuarica left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks for your contribution.

@quiet-node
Copy link
Member

@belloibrahv The build process is failing: https://github.com/hashgraph/hedera-json-rpc-relay/actions/runs/12903875068/job/36018850517?pr=3350#step:12:49.

It looks like the createPrivateKeyBasedOnFormat() function is failing because it couldn’t parse the correct type from ConfigService.get('OPERATOR_KEY_FORMAT'). It seems that ConfigService.get('OPERATOR_KEY_FORMAT') is returning undefined, as shown in the screenshot below: image

This is resulting in the following errors:
image

@acuarica Could there be anything missing from the approach you proposed? Just want to make sure we’re covering all the bases!

@acuarica
Copy link
Contributor

@quiet-node see https://github.com/hashgraph/hedera-json-rpc-relay/pull/3350/files#r1925959856.

@belloibrahv
Copy link
Contributor Author

@acuarica @quiet-node I have added the required changes as suggested, kindly help check it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal For changes that affect the project's internal workings but not its outward-facing functionality.
Projects
None yet
4 participants