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

refactor: clean up configurations #3331

Draft
wants to merge 3 commits into
base: 3099-Add-operator-tier
Choose a base branch
from
Draft
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
11 changes: 5 additions & 6 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
3 changes: 0 additions & 3 deletions charts/hedera-json-rpc-relay/environments/minikube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ config:
HEDERA_NETWORK: {"127.0.01:50211":"0.0.3"}
OPERATOR_ID_MAIN: ""
OPERATOR_KEY_MAIN: ""
OPERATOR_ID_ETH_SENDRAWTRANSACTION: ""
OPERATOR_KEY_ETH_SENDRAWTRANSACTION: ""

rolling_restart:
enabled: true
schedule: "0 0 * * *"

2 changes: 0 additions & 2 deletions charts/hedera-json-rpc-relay/templates/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ type: Opaque
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" "") }}
2 changes: 0 additions & 2 deletions charts/hedera-json-rpc-relay/value-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ config:
# HEDERA_NETWORK: ""
OPERATOR_ID_MAIN: ''
OPERATOR_KEY_MAIN: ''
OPERATOR_ID_ETH_SENDRAWTRANSACTION: ''
OPERATOR_KEY_ETH_SENDRAWTRANSACTION: ''
DEFAULT_RATE_LIMIT: 200
TIER_1_RATE_LIMIT: 100
TIER_2_RATE_LIMIT: 800
Expand Down
2 changes: 0 additions & 2 deletions charts/hedera-json-rpc-relay/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ config:
MIRROR_NODE_URL: ''
OPERATOR_ID_MAIN: ''
OPERATOR_KEY_MAIN: ''
OPERATOR_ID_ETH_SENDRAWTRANSACTION: ''
OPERATOR_KEY_ETH_SENDRAWTRANSACTION: ''
RATE_LIMIT_DISABLED: false
SDK_REQUEST_TIMEOUT: 10000
SERVER_PORT: 7546
Expand Down
12 changes: 0 additions & 12 deletions packages/config-service/src/services/globalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,24 +496,12 @@ export class GlobalConfig {
required: true,
defaultValue: null,
},
OPERATOR_ID_ETH_SENDRAWTRANSACTION: {
envName: 'OPERATOR_ID_ETH_SENDRAWTRANSACTION',
type: 'string',
required: false,
defaultValue: null,
},
OPERATOR_ID_MAIN: {
envName: 'OPERATOR_ID_MAIN',
type: 'string',
required: true,
defaultValue: null,
},
OPERATOR_KEY_ETH_SENDRAWTRANSACTION: {
envName: 'OPERATOR_KEY_ETH_SENDRAWTRANSACTION',
type: 'string',
required: false,
defaultValue: null,
},
OPERATOR_KEY_FORMAT: {
envName: 'OPERATOR_KEY_FORMAT',
type: 'string',
Expand Down
1 change: 0 additions & 1 deletion packages/config-service/src/services/loggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { GlobalConfig } from './globalConfig';
export class LoggerService {
public static readonly SENSITIVE_FIELDS = [
GlobalConfig.ENTRIES.OPERATOR_KEY_MAIN.envName,
GlobalConfig.ENTRIES.OPERATOR_KEY_ETH_SENDRAWTRANSACTION.envName,
GlobalConfig.ENTRIES.GITHUB_TOKEN.envName,
GlobalConfig.ENTRIES.GH_ACCESS_TOKEN.envName,
];
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 @@ -142,17 +142,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
3 changes: 0 additions & 3 deletions packages/relay/src/lib/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,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
46 changes: 8 additions & 38 deletions packages/relay/src/lib/services/hapiService/hapiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,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 +282,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 @@ -324,42 +313,23 @@ export default class HAPIService {
* Configure Client
* @param {Logger} logger
* @param {string} hederaNetwork
* @param {string | null} type
* @returns Client
*/
private initClient(logger: Logger, hederaNetwork: string, type: string | null = null): Client {
private initClient(logger: Logger, hederaNetwork: string): Client {
let client: Client, privateKey: PrivateKey;
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`);
}
const operatorId = ConfigService.get('OPERATOR_ID_MAIN') as string;
const operatorKey = ConfigService.get('OPERATOR_KEY_MAIN') as string;
if (operatorId && operatorKey) {
privateKey = Utils.createPrivateKeyBasedOnFormat(operatorKey);
client = client.setOperator(AccountId.fromString(operatorId.trim()), privateKey);
} 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`);
}
logger.warn(`Invalid 'OPERATOR' env variables provided`);
}

// @ts-ignore
Expand Down
19 changes: 3 additions & 16 deletions packages/relay/src/lib/services/hbarLimitService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class HbarLimitService implements IHbarLimitService {
* The operator address for the rate limiter.
* @private
*/
private operatorAddress?: string;
private readonly operatorAddress: string;

constructor(
private readonly hbarSpendingPlanRepository: HbarSpendingPlanRepository,
Expand All @@ -98,13 +98,8 @@ export class HbarLimitService implements IHbarLimitService {
) {
this.reset = this.getResetTimestamp();

const operatorId = ConfigService.get('OPERATOR_ID_MAIN');
const operatorKey = ConfigService.get('OPERATOR_KEY_MAIN');
if (operatorId) {
this.operatorAddress = AccountId.fromString(operatorId as string).toSolidityAddress();
} else if (operatorKey) {
this.operatorAddress = Utils.createPrivateKeyBasedOnFormat(operatorKey as string).publicKey.toEvmAddress();
}
const operatorId = ConfigService.get('OPERATOR_ID_MAIN') as string;
this.operatorAddress = AccountId.fromString(operatorId).toSolidityAddress();

const totalBudget = HbarLimitService.TIER_LIMITS[SubscriptionTier.OPERATOR];
if (totalBudget.toTinybars().lte(0)) {
Expand Down Expand Up @@ -171,14 +166,6 @@ export class HbarLimitService implements IHbarLimitService {
return this.isHBarRateLimiterEnabled;
}

/**
* Sets the operator address for the rate limiter. Used for tracking operator expenses.
* @param {string} operatorAddress - The EVM address of the operator.
*/
setOperatorAddress(operatorAddress: string) {
this.operatorAddress = operatorAddress;
}

/**
* Resets the {@link HbarSpendingPlan#amountSpent} field for all existing plans.
* @param {RequestDetails} requestDetails - The request details used for logging and tracking.
Expand Down
51 changes: 23 additions & 28 deletions packages/server/tests/acceptance/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,37 @@
*
*/
// External resources
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
// Constants
import constants from '@hashgraph/json-rpc-relay/dist/lib/constants';
import { app as wsApp } from '@hashgraph/json-rpc-ws-server/dist/webSocketServer';
// Hashgraph SDK
import { AccountId, Hbar } from '@hashgraph/sdk';
import chai from 'chai';
import dotenv from 'dotenv';
import path from 'path';
import pino from 'pino';
import chaiAsPromised from 'chai-as-promised';
import { GCProfiler } from 'v8';

// Other external resources
import fs from 'fs';
import { Server } from 'http';
import path from 'path';
import pino from 'pino';
import { GCProfiler } from 'v8';

// Clients
import ServicesClient from '../clients/servicesClient';
import MirrorClient from '../clients/mirrorClient';
import RelayClient from '../clients/relayClient';
import MetricsClient from '../clients/metricsClient';

import { ConfigServiceTestHelper } from '../../../config-service/tests/configServiceTestHelper';
// Server related
import app from '../../dist/server';
import { app as wsApp } from '@hashgraph/json-rpc-ws-server/dist/webSocketServer';

// Hashgraph SDK
import { AccountId, Hbar } from '@hashgraph/sdk';

// Constants
import constants from '@hashgraph/json-rpc-relay/dist/lib/constants';

import { setServerTimeout } from '../../src/koaJsonRpc/lib/utils';
import MetricsClient from '../clients/metricsClient';
import MirrorClient from '../clients/mirrorClient';
import RelayClient from '../clients/relayClient';
// Clients
import ServicesClient from '../clients/servicesClient';
// Utils and types
import { Utils } from '../helpers/utils';
import { AliasAccount } from '../types/AliasAccount';
import { setServerTimeout } from '../../src/koaJsonRpc/lib/utils';
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { Server } from 'http';

chai.use(chaiAsPromised);
dotenv.config({ path: path.resolve(__dirname, '../../../../.env') });
const DOT_ENV = dotenv.parse(fs.readFileSync(path.resolve(__dirname, '../../../../.env')));

ConfigServiceTestHelper.appendEnvsFromPath(path.resolve(__dirname, '../../../../.env'));

const testLogger = pino({
name: 'hedera-json-rpc-relay',
Expand All @@ -68,10 +63,10 @@ const testLogger = pino({
});
const logger = testLogger.child({ name: 'rpc-acceptance-test' });

const NETWORK = ConfigService.get('HEDERA_NETWORK') || DOT_ENV.HEDERA_NETWORK || '';
const OPERATOR_KEY = ConfigService.get('OPERATOR_KEY_MAIN') || DOT_ENV.OPERATOR_KEY_MAIN || '';
const OPERATOR_ID = ConfigService.get('OPERATOR_ID_MAIN') || DOT_ENV.OPERATOR_ID_MAIN || '';
const MIRROR_NODE_URL = ConfigService.get('MIRROR_NODE_URL') || DOT_ENV.MIRROR_NODE_URL || '';
const NETWORK = ConfigService.get('HEDERA_NETWORK') as string;
const OPERATOR_KEY = ConfigService.get('OPERATOR_KEY_MAIN') as string;
const OPERATOR_ID = ConfigService.get('OPERATOR_ID_MAIN') as string;
const MIRROR_NODE_URL = ConfigService.get('MIRROR_NODE_URL') as string;
const LOCAL_RELAY_URL = 'http://localhost:7546';
const RELAY_URL = ConfigService.get('E2E_RELAY_HOST') || LOCAL_RELAY_URL;
const CHAIN_ID = ConfigService.get('CHAIN_ID') || '0x12a';
Expand Down
41 changes: 21 additions & 20 deletions packages/ws-server/tests/acceptance/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,31 @@
* limitations under the License.
*
*/
import chai from 'chai';
import path from 'path';
import pino from 'pino';
import dotenv from 'dotenv';
import chaiAsPromised from 'chai-as-promised';
import fs from 'fs';
import { AccountId, Hbar } from '@hashgraph/sdk';
import app from '@hashgraph/json-rpc-server/dist/server';
import { Server } from 'node:http';

import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import constants from '@hashgraph/json-rpc-relay/dist/lib/constants';
import RelayClient from '@hashgraph/json-rpc-server/tests/clients/relayClient';
import { RequestDetails } from '@hashgraph/json-rpc-relay/dist/lib/types';
import { setServerTimeout } from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/utils';
import app from '@hashgraph/json-rpc-server/dist/server';
import MirrorClient from '@hashgraph/json-rpc-server/tests/clients/mirrorClient';
import { app as wsApp } from '@hashgraph/json-rpc-ws-server/dist/webSocketServer';
import RelayClient from '@hashgraph/json-rpc-server/tests/clients/relayClient';
import ServicesClient from '@hashgraph/json-rpc-server/tests/clients/servicesClient';
import { Utils } from '@hashgraph/json-rpc-server/tests/helpers/utils';
import { AliasAccount } from '@hashgraph/json-rpc-server/tests/types/AliasAccount';
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { RequestDetails } from '@hashgraph/json-rpc-relay/dist/lib/types';
import { Server } from 'node:http';
import { setServerTimeout } from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/utils';
import { app as wsApp } from '@hashgraph/json-rpc-ws-server/dist/webSocketServer';
import { AccountId, Hbar } from '@hashgraph/sdk';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import fs from 'fs';
import path from 'path';
import pino from 'pino';

import { ConfigServiceTestHelper } from '../../../config-service/tests/configServiceTestHelper';

chai.use(chaiAsPromised);

dotenv.config({ path: path.resolve(__dirname, '../../../../.env') });
const DOT_ENV = dotenv.parse(fs.readFileSync(path.resolve(__dirname, '../../../../.env')));
ConfigServiceTestHelper.appendEnvsFromPath(path.resolve(__dirname, '../../../../.env'));

const testLogger = pino({
name: 'hedera-json-rpc-relay',
Expand All @@ -55,10 +56,10 @@ const testLogger = pino({
});
const logger = testLogger.child({ name: 'rpc-acceptance-test' });

const NETWORK = ConfigService.get('HEDERA_NETWORK') || DOT_ENV.HEDERA_NETWORK || '';
const OPERATOR_KEY = ConfigService.get('OPERATOR_KEY_MAIN') || DOT_ENV.OPERATOR_KEY_MAIN || '';
const OPERATOR_ID = ConfigService.get('OPERATOR_ID_MAIN') || DOT_ENV.OPERATOR_ID_MAIN || '';
const MIRROR_NODE_URL = ConfigService.get('MIRROR_NODE_URL') || DOT_ENV.MIRROR_NODE_URL || '';
const NETWORK = ConfigService.get('HEDERA_NETWORK') as string;
const OPERATOR_KEY = ConfigService.get('OPERATOR_KEY_MAIN') as string;
const OPERATOR_ID = ConfigService.get('OPERATOR_ID_MAIN') as string;
const MIRROR_NODE_URL = ConfigService.get('MIRROR_NODE_URL') as string;
const LOCAL_RELAY_URL = 'http://localhost:7546';
const RELAY_URL = ConfigService.get('E2E_RELAY_HOST') || LOCAL_RELAY_URL;
const CHAIN_ID = ConfigService.get('CHAIN_ID') || '0x12a';
Expand Down
Loading