From a75f5b50d87017d5f3c8340f987b031efa4a7b4c Mon Sep 17 00:00:00 2001 From: Eric Badiere Date: Tue, 23 Jul 2024 06:15:29 -0600 Subject: [PATCH] =?UTF-8?q?fix:=20Updated=20the=20account=20from=20the=200?= =?UTF-8?q?.0.2=20account=20to=20an=20account=20from=20th=E2=80=A6=20(#269?= =?UTF-8?q?0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Updated the account from the 0.0.2 account to an account from the local node to track transaction costs. Signed-off-by: ebadiere * fix: Turning off test to potentially get through CI. Opened issue: https://github.com/hashgraph/hedera-json-rpc-relay/issues/2700 Signed-off-by: ebadiere * fix: WIP: Pushing up to collaborate Signed-off-by: ebadiere fix: Updated the private key to the hex ECDSA value. Signed-off-by: ebadiere fix: Bumped up the gasLimit to the required amount to activate a hollow account. Signed-off-by: ebadiere * fix: Added key format support to the serviceClient. Signed-off-by: ebadiere * fix: Extracted the createPrivateKeyBasedOnFormat(key) to the Utils class. Signed-off-by: ebadiere * fix: Updated import of createPrivateKeyBasedOnFormat from Utils. Signed-off-by: ebadiere * fix: Removed memory leak check. PR 26954 addresses memory leaks. Signed-off-by: ebadiere * fix: Removed null test because an env var which is a string cannot be set to null Signed-off-by: ebadiere * fix: Removed the comment in the GH workflow Signed-off-by: ebadiere --------- Signed-off-by: ebadiere Signed-off-by: Eric Badiere --- .github/workflows/test.yml | 9 +------ .../lib/services/hapiService/hapiService.ts | 20 +++------------ packages/relay/src/utils.ts | 20 +++++++++++++++ packages/relay/tests/lib/sdkClient.spec.ts | 25 ++++++------------- .../server/tests/clients/servicesClient.ts | 3 ++- packages/server/tests/localAcceptance.env | 8 +++--- .../acceptance/sendRawTransaction.spec.ts | 2 +- 7 files changed, 38 insertions(+), 49 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bdb6654ce7..54947f708a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -42,14 +42,7 @@ jobs: run: npm install -g pnpm - name: Build Typescript and Run tests - run: | - output=$(npm run build-and-test 2>&1) - echo "$output" - if echo "$output" | grep -q "JavaScript heap out of memory"; then - echo "Heap out of Memory error detected" - exit 1 - fi - shell: bash + run: npm run build-and-test - name: Upload coverage report if: ${{ always() && !cancelled() }} diff --git a/packages/relay/src/lib/services/hapiService/hapiService.ts b/packages/relay/src/lib/services/hapiService/hapiService.ts index 61c05c5b96..98e5de04aa 100644 --- a/packages/relay/src/lib/services/hapiService/hapiService.ts +++ b/packages/relay/src/lib/services/hapiService/hapiService.ts @@ -27,6 +27,7 @@ import { SDKClient } from '../../clients/sdkClient'; import constants from '../../constants'; import HbarLimit from '../../hbarlimiter'; import { CacheService } from '../cacheService/cacheService'; +import { Utils } from './../../../utils'; export default class HAPIService { private transactionCount: number; @@ -218,14 +219,14 @@ export default class HAPIService { if (type === 'eth_sendRawTransaction') { if (process.env.OPERATOR_ID_ETH_SENDRAWTRANSACTION && process.env.OPERATOR_KEY_ETH_SENDRAWTRANSACTION) { - privateKey = this.createPrivateKeyBasedOnFormat(process.env.OPERATOR_KEY_ETH_SENDRAWTRANSACTION); + privateKey = Utils.createPrivateKeyBasedOnFormat(process.env.OPERATOR_KEY_ETH_SENDRAWTRANSACTION); client = client.setOperator(AccountId.fromString(process.env.OPERATOR_ID_ETH_SENDRAWTRANSACTION), privateKey); } else { logger.warn(`Invalid 'ETH_SENDRAWTRANSACTION' env variables provided`); } } else { if (process.env.OPERATOR_ID_MAIN && process.env.OPERATOR_KEY_MAIN) { - privateKey = this.createPrivateKeyBasedOnFormat(process.env.OPERATOR_KEY_MAIN); + privateKey = Utils.createPrivateKeyBasedOnFormat(process.env.OPERATOR_KEY_MAIN); client = client.setOperator(AccountId.fromString(process.env.OPERATOR_ID_MAIN.trim()), privateKey); } else { logger.warn(`Invalid 'OPERATOR' env variables provided`); @@ -246,21 +247,6 @@ export default class HAPIService { return client; } - private createPrivateKeyBasedOnFormat(operatorMainKey: string): PrivateKey { - switch (process.env.OPERATOR_KEY_FORMAT) { - case 'DER': - case undefined: - case null: - return PrivateKey.fromStringDer(operatorMainKey); - case 'HEX_ED25519': - return PrivateKey.fromStringED25519(operatorMainKey); - case 'HEX_ECDSA': - return PrivateKey.fromStringECDSA(operatorMainKey); - default: - throw new Error(`Invalid OPERATOR_KEY_FORMAT provided: ${process.env.OPERATOR_KEY_FORMAT}`); - } - } - /** * Return current main client instance * @returns Main Client diff --git a/packages/relay/src/utils.ts b/packages/relay/src/utils.ts index 5d513f3128..463dd3793a 100644 --- a/packages/relay/src/utils.ts +++ b/packages/relay/src/utils.ts @@ -18,6 +18,7 @@ * */ +import { PrivateKey } from '@hashgraph/sdk'; import constants from './lib/constants'; export class Utils { @@ -35,4 +36,23 @@ export class Utils { return gasPrice; }; + + /** + * @param operatorMainKey + * @returns PrivateKey + */ + public static createPrivateKeyBasedOnFormat(operatorMainKey: string): PrivateKey { + switch (process.env.OPERATOR_KEY_FORMAT) { + case 'DER': + case undefined: + case null: + return PrivateKey.fromStringDer(operatorMainKey); + case 'HEX_ED25519': + return PrivateKey.fromStringED25519(operatorMainKey); + case 'HEX_ECDSA': + return PrivateKey.fromStringECDSA(operatorMainKey); + default: + throw new Error(`Invalid OPERATOR_KEY_FORMAT provided: ${process.env.OPERATOR_KEY_FORMAT}`); + } + } } diff --git a/packages/relay/tests/lib/sdkClient.spec.ts b/packages/relay/tests/lib/sdkClient.spec.ts index c142a7c262..520c4e05bd 100644 --- a/packages/relay/tests/lib/sdkClient.spec.ts +++ b/packages/relay/tests/lib/sdkClient.spec.ts @@ -53,6 +53,7 @@ import { MAX_GAS_LIMIT_HEX } from './eth/eth-config'; import { getRequestId, signTransaction } from '../helpers'; import { TransactionReceipt } from 'ethers'; import exp from 'constants'; +import { Utils } from '../../src/utils'; describe('SdkClient', async function () { this.timeout(20000); @@ -180,7 +181,7 @@ describe('SdkClient', async function () { }); describe('HAPIService', async () => { - let originalEnv: NodeJS.ProcessEnv; + let originalEnv: NodeJS.ProcessEnv, keyFormat; const OPERATOR_KEY_ED25519 = { DER: '302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137', @@ -218,47 +219,35 @@ describe('SdkClient', async function () { it('Initialize the privateKey for default which is DER', async () => { const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry)); - const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.DER); + const privateKey = Utils.createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.DER); expect(privateKey.toString()).to.eq(OPERATOR_KEY_ED25519.DER); }); it('Initialize the privateKey for default which is DER when OPERATOR_KEY_FORMAT is undefined', async () => { delete process.env.OPERATOR_KEY_FORMAT; const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry)); - const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.DER); - expect(privateKey.toString()).to.eq(OPERATOR_KEY_ED25519.DER); - }); - - it('Initialize the privateKey for default which is DER when OPERATOR_KEY_FORMAT is null', async () => { - const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry)); - const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.DER); + const privateKey = Utils.createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.DER); expect(privateKey.toString()).to.eq(OPERATOR_KEY_ED25519.DER); }); it('Initialize the privateKey for OPERATOR_KEY_FORMAT set to DER', async () => { process.env.OPERATOR_KEY_FORMAT = 'DER'; const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry)); - const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ECDSA.DER); + const privateKey = Utils.createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ECDSA.DER); expect(privateKey.toString()).to.eq(OPERATOR_KEY_ECDSA.DER); }); it('Initialize the privateKey for OPERATOR_KEY_FORMAT set to HEX_ED25519', async () => { process.env.OPERATOR_KEY_FORMAT = 'HEX_ED25519'; const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry)); - const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call( - hapiService, - OPERATOR_KEY_ED25519.HEX_ED25519, - ); + const privateKey = Utils.createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.HEX_ED25519); expect(privateKey.toString()).to.eq(OPERATOR_KEY_ED25519.DER); }); it('Initialize the privateKey for OPERATOR_KEY_FORMAT set to HEX_ECDSA', async () => { process.env.OPERATOR_KEY_FORMAT = 'HEX_ECDSA'; const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry)); - const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call( - hapiService, - OPERATOR_KEY_ECDSA.HEX_ECDSA, - ); + const privateKey = Utils.createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ECDSA.HEX_ECDSA); expect(privateKey.toString()).to.eq(OPERATOR_KEY_ECDSA.DER); }); diff --git a/packages/server/tests/clients/servicesClient.ts b/packages/server/tests/clients/servicesClient.ts index 2dbbb788ea..924e67a401 100644 --- a/packages/server/tests/clients/servicesClient.ts +++ b/packages/server/tests/clients/servicesClient.ts @@ -58,6 +58,7 @@ import { Logger } from 'pino'; import { ethers } from 'ethers'; import { Utils } from '../helpers/utils'; import { AliasAccount } from '../types/AliasAccount'; +import { Utils as relayUtils } from '@hashgraph/json-rpc-relay/dist/utils'; const supportedEnvs = ['previewnet', 'testnet', 'mainnet']; @@ -75,7 +76,7 @@ export default class ServicesClient { this.network = network; if (!network) network = '{}'; - const opPrivateKey = PrivateKey.fromString(key); + const opPrivateKey = relayUtils.createPrivateKeyBasedOnFormat(key); if (supportedEnvs.includes(network.toLowerCase())) { this.client = Client.forName(network); } else { diff --git a/packages/server/tests/localAcceptance.env b/packages/server/tests/localAcceptance.env index 153860b95e..b43309b95b 100644 --- a/packages/server/tests/localAcceptance.env +++ b/packages/server/tests/localAcceptance.env @@ -1,8 +1,8 @@ HEDERA_NETWORK={"127.0.0.1:50211":"0.0.3"} - -#This is a static acount created at startup in the local node -OPERATOR_ID_MAIN=0.0.1022 -OPERATOR_KEY_MAIN=302e020100300506032b657004220420a608e2130a0a3cb34f86e757303c862bee353d9ab77ba4387ec084f881d420d4 +# Account from local node. This is the account that will be used to pay for transactions. +OPERATOR_ID_MAIN=0.0.1002 +OPERATOR_KEY_MAIN=0x7f109a9e3b0d8ecfba9cc23a3614433ce0fa7ddcc80f2a8f10b222179a5a80d6 +OPERATOR_KEY_FORMAT=HEX_ECDSA CHAIN_ID=0x12a MIRROR_NODE_URL_WEB3=http://127.0.0.1:8545 REDIS_ENABLED=true diff --git a/packages/ws-server/tests/acceptance/sendRawTransaction.spec.ts b/packages/ws-server/tests/acceptance/sendRawTransaction.spec.ts index 49c45f8fbc..63fc3576bb 100644 --- a/packages/ws-server/tests/acceptance/sendRawTransaction.spec.ts +++ b/packages/ws-server/tests/acceptance/sendRawTransaction.spec.ts @@ -83,7 +83,7 @@ describe('@web-socket-batch-2 eth_sendRawTransaction', async function () { value: (10 * 10 ** 18).toString(), // 10hbar - the gasPrice to deploy deterministic proxy contract to: constants.DETERMINISTIC_DEPLOYMENT_SIGNER, gasPrice: await global.relay.gasPrice(), - gasLimit: numberTo0x(5000000), + gasLimit: constants.TX_HOLLOW_ACCOUNT_CREATION_GAS, }; });