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

feat: capture env variables into sigleton and reusable constants #3047

Open
wants to merge 81 commits into
base: main
Choose a base branch
from

Conversation

natanasow
Copy link
Collaborator

@natanasow natanasow commented Oct 1, 2024

Description:

Currently, we are dynamically getting the environment variables and parsing/casting them every time. This is inefficient and also, more importantly, allows env configurations to be dynamically changed, which can modify the behavior of the relay based on the current values of the env vars, which can be a potential exploit.

Also, when updating CI we noticed with the configuration updates, that if an environment variable such as OPERATOR_ID was defined with not value, it would override the values in the .env file. This caused tests to fail when the relay tried to submit a transaction. Ideally, the relay to fail fast, during bootstrap if required environment variables are not set.

Solution:

Consolidate environment variable loading to the start of the relay. Fail to start if any required environment variables are not set. Better to fail fast.

All environment variables should be handled in one class that can also validate them, list their usage in a --help option, and exit appropriately if a validation fails.

We should also replace environment variables with a yml settings file.

Notes for reviews:

  • created a new package that holds the config-service singleton
  • the config-service package is added as a dependency to relay, server, and ws-server
  • in the perfect world, the config-service must be immutable and should not provide an interface for overriding or extending once-loaded environment variables but there are many tests where we dynamically override envs in order to test specific cases so we should ConfigServiceTestHelper as well
  • the main review should be applied to the config-service package
  • almost all of the changes are just replaced values from process.env.<VAR_NAME> to ConfigService.get(<VAR_NAME>)

Needed validations:

  • Make sure that CHAIN_ID, HEDERA_NETWORK, MIRROR_NODE_URL, OPERATOR_ID_MAIN, OPERATOR_KEY_MAIN, SERVER_PORT are non-empty and in the expected format
  • Make sure that if OPERATOR_KEY_FORMAT is not specified, the provided OPERATOR_KEY_MAIN is in the DER format
  • Make sure that HBAR_RATE_LIMIT_TINYBAR is more than HBAR_RATE_LIMIT_BASIC, HBAR_RATE_LIMIT_EXTENDED, HBAR_RATE_LIMIT_PRIVILEGED

Implementation Steps

  • Consolidate all environment variables into one service class
  • Add validations and exit on fail of validation, to the environment service class
  • Add help/usage message and option
  • There are several solid proposals by @AlfredoG87 that we should take into account as well, described here.
  • Enhance to use yml instead of dotenv.

cc: @ebadiere

Related issue(s):

Fixes #3023 #2686

Notes for reviewer:

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
# Conflicts:
#	packages/relay/src/lib/services/hbarLimitService/index.ts
#	packages/relay/tests/lib/services/hbarLimitService/hbarLimitService.spec.ts
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
# Conflicts:
#	packages/relay/src/lib/constants.ts
#	packages/relay/src/lib/eth.ts
#	packages/relay/tests/lib/eth/eth_call.spec.ts
#	packages/relay/tests/lib/ethGetBlockBy.spec.ts
#	packages/relay/tests/lib/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository.spec.ts
#	packages/relay/tests/lib/repositories/hbarLimiter/hbarSpendingPlanRepository.spec.ts
#	packages/relay/tests/lib/services/cacheService/cacheService.spec.ts
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
# Conflicts:
#	packages/relay/src/lib/services/hbarLimitService/index.ts
# Conflicts:
#	packages/relay/src/lib/clients/sdkClient.ts
#	packages/relay/tests/lib/hbarLimiter.spec.ts
#	packages/relay/tests/lib/sdkClient.spec.ts
#	packages/server/tests/acceptance/hbarLimiter.spec.ts
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
Signed-off-by: nikolay <n.atanasow94@gmail.com>
# Conflicts:
#	packages/relay/src/lib/services/hapiService/hapiService.ts
#	packages/server/tests/acceptance/hbarLimiter.spec.ts
#	packages/server/tests/acceptance/index.spec.ts
#	packages/server/tests/acceptance/rpc_batch3.spec.ts
#	packages/ws-server/tests/acceptance/index.spec.ts
Signed-off-by: nikolay <n.atanasow94@gmail.com>
@hashgraph hashgraph deleted a comment from github-actions bot Oct 8, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 8, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 8, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 8, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 8, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 8, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 8, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 8, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 8, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 8, 2024
AlfredoG87
AlfredoG87 previously approved these changes Oct 8, 2024
Copy link
Collaborator

@AlfredoG87 AlfredoG87 left a comment

Choose a reason for hiding this comment

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

LGTM

Signed-off-by: nikolay <n.atanasow94@gmail.com>
Copy link

sonarcloud bot commented Oct 10, 2024

Copy link

🚨 Memory Leak Detected 🚨

A potential memory leak has been detected in the test titled should fail with empty TracerConfig containing invalid properties. This may impact the application's performance and stability.

Details

📊 Memory Leak Detection Report 📊

GC Type: MarkSweepCompact
Cost: 28,445.1 ms

Heap Statistics (before vs after executing the test):

  • Total Heap Size: increased with 2.10 MB
  • Total Heap Size Executable: no changes
  • Total Physical Size: increased with 32.77 KB
  • Total Available Size: increased with 371.38 KB
  • Total Global Handles Size: no changes
  • Used Global Handles Size: decreased with 64.00 bytes
  • Used Heap Size: decreased with 822.68 KB
  • Heap Size Limit: no changes
  • Malloced Memory: increased with 65.78 KB
  • External Memory: no changes
  • Peak Malloced Memory: no changes

Heap Space Statistics (before vs after executing the test):

  • New Space:
    • Space Size: increased with 2.10 MB
    • Space Used Size: decreased with 675.31 KB
    • Space Available Size: increased with 1.71 MB
    • Physical Space Size: increased with 32.77 KB

Recommendations

Please investigate the memory allocations in this test, focusing on objects that are not being properly deallocated.

Copy link

🚨 Memory Leak Detected 🚨

A potential memory leak has been detected in the test titled throws an error if block hash is larger than 32bytes. This may impact the application's performance and stability.

Details

📊 Memory Leak Detection Report 📊

GC Type: MarkSweepCompact
Cost: 27,020.3 ms

Heap Statistics (before vs after executing the test):

  • Total Heap Size: increased with 1.05 MB
  • Total Heap Size Executable: no changes
  • Total Physical Size: increased with 274.43 KB
  • Total Available Size: decreased with 1.03 MB
  • Total Global Handles Size: no changes
  • Used Global Handles Size: decreased with 32.00 bytes
  • Used Heap Size: decreased with 246.06 KB
  • Heap Size Limit: no changes
  • Malloced Memory: no changes
  • External Memory: no changes
  • Peak Malloced Memory: no changes

Heap Space Statistics (before vs after executing the test):

  • New Space:
    • Space Size: increased with 1.05 MB
    • Space Used Size: decreased with 63.90 KB
    • Space Available Size: increased with 63.90 KB
    • Physical Space Size: increased with 274.43 KB

Recommendations

Please investigate the memory allocations in this test, focusing on objects that are not being properly deallocated.

Copy link

🚨 Memory Leak Detected 🚨

A potential memory leak has been detected in the test titled throws an error for input: {"onlyTopCall":"invalid"}. This may impact the application's performance and stability.

Details

📊 Memory Leak Detection Report 📊

GC Type: MarkSweepCompact
Cost: 26,883.1 ms

Heap Statistics (before vs after executing the test):

  • Total Heap Size: increased with 2.10 MB
  • Total Heap Size Executable: no changes
  • Total Physical Size: increased with 32.77 KB
  • Total Available Size: decreased with 729.38 KB
  • Total Global Handles Size: no changes
  • Used Global Handles Size: decreased with 32.00 bytes
  • Used Heap Size: decreased with 95.80 KB
  • Heap Size Limit: no changes
  • Malloced Memory: no changes
  • External Memory: no changes
  • Peak Malloced Memory: no changes

Heap Space Statistics (before vs after executing the test):

  • New Space:
    • Space Size: increased with 2.10 MB
    • Space Used Size: decreased with 74.46 KB
    • Space Available Size: increased with 1.11 MB
    • Physical Space Size: increased with 32.77 KB

Recommendations

Please investigate the memory allocations in this test, focusing on objects that are not being properly deallocated.

Copy link

🚨 Memory Leak Detected 🚨

A potential memory leak has been detected in the test titled does not throw an error for input: {}. This may impact the application's performance and stability.

Details

📊 Memory Leak Detection Report 📊

GC Type: MarkSweepCompact
Cost: 27,113.8 ms

Heap Statistics (before vs after executing the test):

  • Total Heap Size: increased with 1.05 MB
  • Total Heap Size Executable: no changes
  • Total Physical Size: increased with 274.43 KB
  • Total Available Size: decreased with 862.94 KB
  • Total Global Handles Size: no changes
  • Used Global Handles Size: decreased with 32.00 bytes
  • Used Heap Size: decreased with 186.54 KB
  • Heap Size Limit: no changes
  • Malloced Memory: no changes
  • External Memory: no changes
  • Peak Malloced Memory: no changes

Heap Space Statistics (before vs after executing the test):

  • New Space:
    • Space Size: increased with 1.05 MB
    • Space Used Size: decreased with 54.84 KB
    • Space Available Size: increased with 54.84 KB
    • Physical Space Size: increased with 274.43 KB

Recommendations

Please investigate the memory allocations in this test, focusing on objects that are not being properly deallocated.

Copy link

🚨 Memory Leak Detected 🚨

A potential memory leak has been detected in the test titled should create a ParseError with correct message and code. This may impact the application's performance and stability.

Details

📊 Memory Leak Detection Report 📊

GC Type: MarkSweepCompact
Cost: 25,663.8 ms

Heap Statistics (before vs after executing the test):

  • Total Heap Size: increased with 2.10 MB
  • Total Heap Size Executable: no changes
  • Total Physical Size: increased with 32.77 KB
  • Total Available Size: increased with 228.49 KB
  • Total Global Handles Size: no changes
  • Used Global Handles Size: decreased with 32.00 bytes
  • Used Heap Size: decreased with 247.48 KB
  • Heap Size Limit: no changes
  • Malloced Memory: no changes
  • External Memory: no changes
  • Peak Malloced Memory: no changes

Heap Space Statistics (before vs after executing the test):

  • New Space:
    • Space Size: increased with 2.10 MB
    • Space Used Size: decreased with 78.63 KB
    • Space Available Size: increased with 1.11 MB
    • Physical Space Size: increased with 32.77 KB

Recommendations

Please investigate the memory allocations in this test, focusing on objects that are not being properly deallocated.

Copy link

🚨 Memory Leak Detected 🚨

A potential memory leak has been detected in the test titled should return a valid JSON-RPC response with error. This may impact the application's performance and stability.

Details

📊 Memory Leak Detection Report 📊

GC Type: MarkSweepCompact
Cost: 34,038.2 ms

Heap Statistics (before vs after executing the test):

  • Total Heap Size: increased with 1.05 MB
  • Total Heap Size Executable: no changes
  • Total Physical Size: increased with 274.43 KB
  • Total Available Size: decreased with 1.05 MB
  • Total Global Handles Size: no changes
  • Used Global Handles Size: decreased with 32.00 bytes
  • Used Heap Size: decreased with 167.85 KB
  • Heap Size Limit: no changes
  • Malloced Memory: no changes
  • External Memory: no changes
  • Peak Malloced Memory: no changes

Heap Space Statistics (before vs after executing the test):

  • New Space:
    • Space Size: increased with 1.05 MB
    • Space Used Size: decreased with 65.73 KB
    • Space Available Size: increased with 65.73 KB
    • Physical Space Size: increased with 274.43 KB

Recommendations

Please investigate the memory allocations in this test, focusing on objects that are not being properly deallocated.

Copy link

codecov bot commented Oct 10, 2024

Codecov Report

Attention: Patch coverage is 81.72589% with 36 lines in your changes missing coverage. Please review.

Project coverage is 85.13%. Comparing base (4a69be9) to head (99be3d0).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
.../relay/src/lib/services/hapiService/hapiService.ts 23.07% 4 Missing and 6 partials ⚠️
packages/relay/src/lib/constants.ts 54.54% 0 Missing and 5 partials ⚠️
packages/relay/src/lib/clients/mirrorNodeClient.ts 81.81% 0 Missing and 4 partials ⚠️
packages/relay/src/lib/eth.ts 42.85% 0 Missing and 4 partials ⚠️
...s/server/src/koaJsonRpc/lib/methodConfiguration.ts 62.50% 0 Missing and 3 partials ⚠️
packages/relay/src/lib/clients/sdkClient.ts 71.42% 1 Missing and 1 partial ⚠️
packages/relay/src/lib/poller.ts 33.33% 0 Missing and 2 partials ⚠️
packages/relay/src/lib/relay.ts 66.66% 0 Missing and 2 partials ⚠️
...kages/config-service/src/services/loggerService.ts 83.33% 0 Missing and 1 partial ⚠️
...kages/relay/src/lib/clients/cache/localLRUCache.ts 66.66% 0 Missing and 1 partial ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3047      +/-   ##
==========================================
+ Coverage   84.88%   85.13%   +0.24%     
==========================================
  Files          43       63      +20     
  Lines        3216     4015     +799     
  Branches      649      803     +154     
==========================================
+ Hits         2730     3418     +688     
- Misses        283      358      +75     
- Partials      203      239      +36     
Flag Coverage Δ
config-service 98.27% <98.27%> (?)
relay 85.05% <71.55%> (+0.16%) ⬆️
server 83.48% <84.00%> (?)
ws-server 97.91% <100.00%> (?)

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

Files with missing lines Coverage Δ
...ckages/config-service/src/services/globalConfig.ts 100.00% <100.00%> (ø)
packages/config-service/src/services/index.ts 100.00% <100.00%> (ø)
...s/config-service/src/services/validationService.ts 100.00% <100.00%> (ø)
packages/relay/src/lib/hbarlimiter/index.ts 100.00% <100.00%> (ø)
packages/relay/src/lib/net.ts 100.00% <100.00%> (+12.50%) ⬆️
...elay/src/lib/services/cacheService/cacheService.ts 92.53% <100.00%> (+0.05%) ⬆️
...kages/relay/src/lib/services/debugService/index.ts 76.81% <100.00%> (+0.34%) ⬆️
.../lib/services/ethService/ethCommonService/index.ts 89.23% <100.00%> (+0.08%) ⬆️
.../lib/services/ethService/ethFilterService/index.ts 79.76% <100.00%> (+0.24%) ⬆️
...ay/src/lib/services/metricService/metricService.ts 92.68% <100.00%> (+0.18%) ⬆️
... and 22 more

... and 7 files with indirect coverage changes

@hashgraph hashgraph deleted a comment from github-actions bot Oct 10, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 10, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 10, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 10, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 10, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 10, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 10, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 10, 2024
@hashgraph hashgraph deleted a comment from github-actions bot Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

All environment variables required by the relay should be initially loaded and validated.
3 participants