Skip to content

Commit

Permalink
refactor: clean up configurations (#3331)
Browse files Browse the repository at this point in the history
* feat: Add `OPERATOR` tier

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: build error

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: remove duplicated fetch of remaining budget

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* chore: improve readability

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: make sure that operator address is always up-to-date with the client

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* chore: formatting

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* chore: formatting

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* chore: fix tests

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: do not ignore error when EVM address is not provided to `addExpense`

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* chore: revert unnecessary change

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* chore: revert unnecessary change

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* chore: reduce code duplication in metricService.spec.ts

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* chore: reduce code duplication in metricService.spec.ts

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: error in charts:install workflow

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: hbarLimiter.spec.ts

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: remove unnecessary rest in hbarLimiter.spec.ts

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* refactor: clean up configurations

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* refactor: clean up configurations

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* refactor: fix acceptance-workflow.yml

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* chore: prepend 0x to operator address

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* test: extend assertions in hbarLimitService.spec.ts

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: acceptance tests

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: acceptance tests

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: build error after rebase from `main`

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: acceptance-workflow.yml

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* test: add tests for `Utils.getOperator`

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* test: extend tests in hbarLimitService.spec.ts with cases where `OPERATOR_ID_MAIN` is not set

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: utils.spec.ts

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: unit tests

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* fix: acceptance-public.yml

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>

* chore: updated logging for HbarLimitService

Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>

* chore: reverted mock for openrpc eth_sendRawTransaction test

Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>

* fix: fixed flaky test in batch1

Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>

* fix: prevent echoing OPERATOR_KEY_MAIN to mitigate security risks

Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>

---------

Signed-off-by: Victor Yanev <victor.yanev@limechain.tech>
Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
Co-authored-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
  • Loading branch information
victor-yanev and quiet-node authored Jan 24, 2025
1 parent 0c6d059 commit a2bfebd
Show file tree
Hide file tree
Showing 42 changed files with 972 additions and 853 deletions.
26 changes: 15 additions & 11 deletions .github/workflows/acceptance-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,19 @@ on:
description: 'Codecov upload token'
required: true


env:
OPERATOR_ID_MAIN: ${{ inputs.operator_id }}

jobs:
acceptance-workflow:
runs-on: smart-contracts-linux-large
timeout-minutes: 50
permissions:
contents: read
checks: write
env:
OPERATOR_ID_MAIN: ${{ inputs.operator_id }}
steps:
- name: Set env variables
run: |
if [ -n "${{ inputs.operator_id }}" ]; then
echo "OPERATOR_ID_MAIN=${{ inputs.operator_id }}" >> $GITHUB_ENV
fi
- name: Harden Runner
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
Expand Down Expand Up @@ -97,26 +96,31 @@ jobs:
with:
max_attempts: ${{ inputs.maxAttempts }}
timeout_minutes: 30
command: npm run acceptancetest:${{ inputs.testfilter }}
command: |
if [[ -n "$OPERATOR_KEY" ]] && [[ "$OPERATOR_KEY" != "" ]]; then
echo "Overriding OPERATOR_KEY_MAIN with the secret value"
export OPERATOR_KEY_MAIN="$OPERATOR_KEY"
fi
npm run acceptancetest:${{ inputs.testfilter }}
env:
TEST_WS_SERVER: ${{ inputs.test_ws_server }}
SUBSCRIPTIONS_ENABLED: ${{ inputs.test_ws_server }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_PR_NUMBER: ${{ github.event.number }}
GITHUB_REPOSITORY: ${{ github.repository }}
OPERATOR_KEY_MAIN: ${{ secrets.operator_key }}
OPERATOR_KEY: ${{ secrets.operator_key }}

- name: Upload Heap Snapshots
if: ${{ !cancelled() }}
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
with:
name: Heap Snapshots
path: "**/*.heapsnapshot"
path: '**/*.heapsnapshot'
if-no-files-found: ignore

- name: Upload Test Results
if: always()
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
with:
name: Test Results (${{ inputs.testfilter }})
path: test-*.xml
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ jobs:

- name: Upload Heap Snapshots
if: ${{ !cancelled() }}
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
with:
name: Heap Snapshots
path: "**/*.heapsnapshot"
path: '**/*.heapsnapshot'
if-no-files-found: ignore

- name: Upload coverage report
Expand Down
1 change: 0 additions & 1 deletion charts/hedera-json-rpc-relay/environments/minikube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ config:
rolling_restart:
enabled: true
schedule: "0 0 * * *"

2 changes: 1 addition & 1 deletion charts/hedera-json-rpc-relay/templates/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ stringData:
OPERATOR_ID_MAIN: {{ .Values.config.OPERATOR_ID_MAIN | quote }}
OPERATOR_KEY_MAIN: {{ .Values.config.OPERATOR_KEY_MAIN | quote }}
OPERATOR_ID_ETH_SENDRAWTRANSACTION: {{ .Values.config.OPERATOR_ID_ETH_SENDRAWTRANSACTION | default (printf "%q" "") }}
OPERATOR_KEY_ETH_SENDRAWTRANSACTION: {{ .Values.config.OPERATOR_KEY_ETH_SENDRAWTRANSACTION | default (printf "%q" "") }}
OPERATOR_KEY_ETH_SENDRAWTRANSACTION: {{ .Values.config.OPERATOR_KEY_ETH_SENDRAWTRANSACTION | default (printf "%q" "") }}
8 changes: 4 additions & 4 deletions packages/config-service/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import dotenv from 'dotenv';
import findConfig from 'find-config';
import pino from 'pino';

import { LoggerService } from './loggerService';
import { ValidationService } from './validationService';

Expand Down Expand Up @@ -61,13 +62,12 @@ export class ConfigService {
private constructor() {
const configPath = findConfig(ConfigService.envFileName);

if (!configPath) {
if (configPath) {
dotenv.config({ path: configPath });
} else {
logger.warn('No .env file is found. The relay cannot operate without valid .env.');
}

// @ts-ignore
dotenv.config({ path: configPath });

// validate mandatory fields
ValidationService.startUp(process.env);

Expand Down
10 changes: 5 additions & 5 deletions packages/relay/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,17 @@ export default {
},

// @ts-ignore
HBAR_RATE_LIMIT_DURATION: parseInt(ConfigService.get('HBAR_RATE_LIMIT_DURATION') || '86400000'), // 1 day
HBAR_RATE_LIMIT_DURATION: parseInt(ConfigService.get('HBAR_RATE_LIMIT_DURATION')),
// @ts-ignore
// The logical OR operator || returns the first truthy value and 0 is falsy.
// The nullish coalescing operator ?? falls back to the default value when the left-hand operand is null or undefined, not when it's 0 or any other falsy value.
HBAR_RATE_LIMIT_TOTAL: BigNumber(ConfigService.get('HBAR_RATE_LIMIT_TINYBAR') ?? '800000000000'), // 8000 HBARs
HBAR_RATE_LIMIT_TOTAL: BigNumber(ConfigService.get('HBAR_RATE_LIMIT_TINYBAR')),
// @ts-ignore
HBAR_RATE_LIMIT_BASIC: BigNumber(ConfigService.get('HBAR_RATE_LIMIT_BASIC') || '1120000000'), // 11.2 HBARs
HBAR_RATE_LIMIT_BASIC: BigNumber(ConfigService.get('HBAR_RATE_LIMIT_BASIC')),
// @ts-ignore
HBAR_RATE_LIMIT_EXTENDED: BigNumber(ConfigService.get('HBAR_RATE_LIMIT_EXTENDED') || '3200000000'), // 32 HBARs
HBAR_RATE_LIMIT_EXTENDED: BigNumber(ConfigService.get('HBAR_RATE_LIMIT_EXTENDED')),
// @ts-ignore
HBAR_RATE_LIMIT_PRIVILEGED: BigNumber(ConfigService.get('HBAR_RATE_LIMIT_PRIVILEGED') || '8000000000'), // 80 HBARs
HBAR_RATE_LIMIT_PRIVILEGED: BigNumber(ConfigService.get('HBAR_RATE_LIMIT_PRIVILEGED')),
// @ts-ignore
GAS_PRICE_TINY_BAR_BUFFER: parseInt(ConfigService.get('GAS_PRICE_TINY_BAR_BUFFER') || '10000000000'),
WEB_SOCKET_PORT: ConfigService.get('WEB_SOCKET_PORT') || 8546,
Expand Down
5 changes: 0 additions & 5 deletions packages/relay/src/lib/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ export class RelayImpl implements Relay {
const hederaNetwork: string = (ConfigService.get('HEDERA_NETWORK') || '{}').toLowerCase();
const configuredChainId = ConfigService.get('CHAIN_ID') || constants.CHAIN_IDS[hederaNetwork] || '298';
const chainId = prepend0x(Number(configuredChainId).toString(16));

const duration = constants.HBAR_RATE_LIMIT_DURATION;
const total = constants.HBAR_RATE_LIMIT_TOTAL;

this.eventEmitter = new EventEmitter();
const reservedKeys = HbarSpendingPlanConfigService.getPreconfiguredSpendingPlanKeys(logger);
Expand Down Expand Up @@ -166,9 +164,6 @@ export class RelayImpl implements Relay {
const hapiService = new HAPIService(logger, register, this.cacheService, this.eventEmitter, hbarLimitService);

this.clientMain = hapiService.getMainClientInstance();
if (this.clientMain.operatorAccountId) {
hbarLimitService.setOperatorAddress(this.clientMain.operatorAccountId.toSolidityAddress());
}

this.web3Impl = new Web3Impl(this.clientMain);
this.netImpl = new NetImpl(this.clientMain);
Expand Down
48 changes: 6 additions & 42 deletions packages/relay/src/lib/services/hapiService/hapiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
*/

import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { AccountId, Client, PrivateKey } from '@hashgraph/sdk';
import dotenv from 'dotenv';
import { Client } from '@hashgraph/sdk';
import EventEmitter from 'events';
import findConfig from 'find-config';
import fs from 'fs';
import { Logger } from 'pino';
import { Counter, Registry } from 'prom-client';

Expand Down Expand Up @@ -194,18 +191,10 @@ export default class HAPIService {
eventEmitter: EventEmitter,
hbarLimitService: HbarLimitService,
) {
dotenv.config({ path: findConfig('.env') || '' });
if (fs.existsSync(findConfig('.env') || '')) {
this.config = dotenv.parse(fs.readFileSync(findConfig('.env') || ''));
} else {
this.config = {};
}

this.logger = logger;

this.hbarLimitService = hbarLimitService;
this.eventEmitter = eventEmitter;
this.hederaNetwork = (ConfigService.get('HEDERA_NETWORK') || this.config.HEDERA_NETWORK || '{}').toLowerCase();
this.hederaNetwork = ((ConfigService.get('HEDERA_NETWORK') as string) || '{}').toLowerCase();
this.clientMain = this.initClient(logger, this.hederaNetwork);

this.cacheService = cacheService;
Expand Down Expand Up @@ -290,9 +279,6 @@ export default class HAPIService {
this.clientMain = this.initClient(this.logger, this.hederaNetwork);
this.client = this.initSDKClient(this.logger);
this.resetCounters();
if (this.clientMain.operatorAccountId) {
this.hbarLimitService.setOperatorAddress(this.clientMain.operatorAccountId.toSolidityAddress());
}
}

/**
Expand Down Expand Up @@ -328,38 +314,16 @@ export default class HAPIService {
* @returns Client
*/
private initClient(logger: Logger, hederaNetwork: string, type: string | null = null): Client {
let client: Client, privateKey: PrivateKey;
let client: Client;
if (hederaNetwork in constants.CHAIN_IDS) {
client = Client.forName(hederaNetwork);
} else {
client = Client.forNetwork(JSON.parse(hederaNetwork));
}

if (type === 'eth_sendRawTransaction') {
if (
ConfigService.get('OPERATOR_ID_ETH_SENDRAWTRANSACTION') &&
ConfigService.get('OPERATOR_KEY_ETH_SENDRAWTRANSACTION')
) {
// @ts-ignore
privateKey = Utils.createPrivateKeyBasedOnFormat(ConfigService.get('OPERATOR_KEY_ETH_SENDRAWTRANSACTION'));
client = client.setOperator(
// @ts-ignore
AccountId.fromString(ConfigService.get('OPERATOR_ID_ETH_SENDRAWTRANSACTION')),
privateKey,
);
} else {
logger.warn(`Invalid 'ETH_SENDRAWTRANSACTION' env variables provided`);
}
} else {
const operatorId: string = ConfigService.get('OPERATOR_ID_MAIN') || this.config.OPERATOR_ID_MAIN || '';
const operatorKey: string = ConfigService.get('OPERATOR_KEY_MAIN') || this.config.OPERATOR_KEY_MAIN || '';

if (operatorId && operatorKey) {
privateKey = Utils.createPrivateKeyBasedOnFormat(operatorKey);
client = client.setOperator(AccountId.fromString(operatorId.trim()), privateKey);
} else {
logger.warn(`Invalid 'OPERATOR' env variables provided`);
}
const operator = Utils.getOperator(logger, type);
if (operator) {
client.setOperator(operator.accountId, operator.privateKey);
}

// @ts-ignore
Expand Down
Loading

0 comments on commit a2bfebd

Please sign in to comment.