From cd3b61a03e7192033a1f2ea068a70b4e432a382a Mon Sep 17 00:00:00 2001 From: Akshitha1020 <159411458+Akshitha1020@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:46:49 +0000 Subject: [PATCH] test(connector-besu): migrate get-past-logs-endpoint to Jest Primary Changes ------------------ 1. Updated the migrate get-past-logs-endpoint test to use Jest 2. Updated the ci.yaml TAPE_TEST_PATTERN to incorporate the same Fixes #3505 Signed-off-by: Akshitha1020 <159411458+Akshitha1020@users.noreply.github.com> --- .github/workflows/ci.yaml | 2 +- .taprc | 1 - .../tsconfig.json | 2 +- examples/test-run-transaction/tsconfig.json | 6 +- jest.config.js | 1 - .../create-runtime-error-with-cause.ts | 39 +++ .../get-past-logs-endpoint.test.ts | 259 +++++++++--------- tsconfig.base.json | 7 +- tsconfig.json | 7 +- 9 files changed, 179 insertions(+), 145 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 248e9709e6..8bc7942e51 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -2381,7 +2381,7 @@ jobs: JEST_TEST_COVERAGE_PATH: ./code-coverage-ts/ctp-ledger-connector-besu JEST_TEST_CODE_COVERAGE_ENABLED: true TAPE_TEST_PATTERN: >- - --files={./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-transaction-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-block-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-past-logs-endpoint.test.ts} + --files={./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-transaction-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-block-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-past-logs-endpoint.test.ts} TAPE_TEST_RUNNER_DISABLED: false runs-on: ubuntu-22.04 steps: diff --git a/.taprc b/.taprc index f91051e159..aa464ea1f7 100644 --- a/.taprc +++ b/.taprc @@ -25,7 +25,6 @@ files: - ./packages/cactus-common/src/test/typescript/unit/logging/logger.test.ts - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-past-logs-endpoint.test.ts - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-block-endpoint.test.ts - - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-transaction-endpoint.test.ts - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts - ./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/identity-client.test.ts diff --git a/examples/cactus-example-supply-chain-frontend/tsconfig.json b/examples/cactus-example-supply-chain-frontend/tsconfig.json index cb97846075..5781182096 100644 --- a/examples/cactus-example-supply-chain-frontend/tsconfig.json +++ b/examples/cactus-example-supply-chain-frontend/tsconfig.json @@ -14,7 +14,7 @@ "importHelpers": true, "target": "ES2022", "lib": [ - "es2018", + "ES2022", "dom" ], "resolveJsonModule": true, diff --git a/examples/test-run-transaction/tsconfig.json b/examples/test-run-transaction/tsconfig.json index 98a8691880..d56cfc0018 100644 --- a/examples/test-run-transaction/tsconfig.json +++ b/examples/test-run-transaction/tsconfig.json @@ -2,12 +2,10 @@ "compilerOptions": { /* Basic Options */ "incremental": true, /* Enable incremental compilation */ - "target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ + "target": "ES2022", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ "module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "lib": [ - "es2015", - "es2016", - "es2017", + "ES2022", "dom" ], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ diff --git a/jest.config.js b/jest.config.js index 5526f5f6db..bdc65d407b 100644 --- a/jest.config.js +++ b/jest.config.js @@ -37,7 +37,6 @@ module.exports = { `./packages/cactus-common/src/test/typescript/unit/logging/logger.test.ts`, `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-past-logs-endpoint.test.ts`, `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-block-endpoint.test.ts`, - `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts`, `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-transaction-endpoint.test.ts`, `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts`, `./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/identity-client.test.ts`, diff --git a/packages/cactus-common/src/main/typescript/exception/create-runtime-error-with-cause.ts b/packages/cactus-common/src/main/typescript/exception/create-runtime-error-with-cause.ts index bef8326dff..f6bf6029d1 100644 --- a/packages/cactus-common/src/main/typescript/exception/create-runtime-error-with-cause.ts +++ b/packages/cactus-common/src/main/typescript/exception/create-runtime-error-with-cause.ts @@ -2,6 +2,25 @@ import { RuntimeError } from "run-time-error-cjs"; import { coerceUnknownToError } from "./coerce-unknown-to-error"; /** + * ## DEPRECATED + * + * Instead of relying on this function, in the future, use the new `cause` + * property of the built-in `Error` type in combination + * with the `asError(unknown)` utility function: + * ```typescript + * import { asError } from "@hyperledger/cactus-common"; + * + * try { + * await performSomeImportantOperation(); + * } catch (ex: unknown) { + * const cause = asError(ex); + * throw new Error("Something went wrong while doing something.", { cause }); + * } + * ``` + * More information about the EcmaScript proposal that made this possible: + * https://github.com/tc39/proposal-error-cause + * + * ## The Old Documentation Prior to the Deprecation: * ### STANDARD EXCEPTION HANDLING - EXAMPLE WITH RE-THROW: * * Use the this utility function and pass in any throwable of whatever type and format @@ -77,6 +96,7 @@ import { coerceUnknownToError } from "./coerce-unknown-to-error"; * return result; // 42 * } * ``` + * @deprecated * * @param message The contextual information that will be passed into the * constructor of the returned {@link RuntimeError} instance. @@ -93,9 +113,28 @@ export function createRuntimeErrorWithCause( } /** + * ## DEPRECATED + * + * Instead of relying on this function, in the future, use the new `cause` + * property of the built-in `Error` type in combination + * with the `asError(unknown)` utility function: + * ```typescript + * import { asError } from "@hyperledger/cactus-common"; + * + * try { + * await performSomeImportantOperation(); + * } catch (ex: unknown) { + * const cause = asError(ex); + * throw new Error("Something went wrong while doing something.", { cause }); + * } + * ``` + * More information about the EcmaScript proposal that made this possible: + * https://github.com/tc39/proposal-error-cause + * * An alias to the `createRuntimeErrorWithCause` function for those prefering * a shorter utility for their personal style. * + * @deprecated * @see {@link createRuntimeErrorWithCause} * @returns `RuntimeError` */ diff --git a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts index 169265fe5a..26341516b8 100644 --- a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts +++ b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts @@ -1,12 +1,13 @@ -import test, { Test } from "tape-promise/tape"; +import "jest-extended"; import { v4 as uuidv4 } from "uuid"; import { createServer } from "http"; + import KeyEncoder from "key-encoder"; import { AddressInfo } from "net"; + import Web3 from "web3"; import Web3JsQuorum, { IWeb3Quorum } from "web3js-quorum"; - import { ApiServer, AuthorizationProtocol, @@ -16,13 +17,12 @@ import { Secp256k1Keys, KeyFormat, LogLevelDesc, + LoggerProvider, } from "@hyperledger/cactus-common"; - import { BesuTestLedger, pruneDockerAllIfGithubAction, } from "@hyperledger/cactus-test-tooling"; - import { BesuApiClientOptions, BesuApiClient, @@ -30,145 +30,150 @@ import { PluginLedgerConnectorBesu, GetPastLogsV1Request, } from "@hyperledger/cactus-plugin-ledger-connector-besu"; - import { PluginRegistry } from "@hyperledger/cactus-core"; - import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory"; const testCase = "API client can call getPastLogs via network"; const logLevel: LogLevelDesc = "TRACE"; -test("BEFORE " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning didn't throw OK"); - t.end(); -}); - -test(testCase, async (t: Test) => { - const keyEncoder: KeyEncoder = new KeyEncoder("secp256k1"); - const keychainId = uuidv4(); - const keychainRef = uuidv4(); - - const { privateKey } = Secp256k1Keys.generateKeyPairsBuffer(); - const keyHex = privateKey.toString("hex"); - const pem = keyEncoder.encodePrivate(keyHex, KeyFormat.Raw, KeyFormat.PEM); - - const keychain = new PluginKeychainMemory({ - backend: new Map([[keychainRef, pem]]), - keychainId, - logLevel, - instanceId: uuidv4(), +describe("API client can call getPastLogs via network", () => { + const log = LoggerProvider.getOrCreate({ + label: "get-past-logs-endpoint.test.ts", + level: logLevel, }); - const httpServer1 = createServer(); - await new Promise((resolve, reject) => { - httpServer1.once("error", reject); - httpServer1.once("listening", resolve); - httpServer1.listen(0, "127.0.0.1"); + const containerImageVersion = "2021-08-24--feat-1244"; + const containerImageName = + "ghcr.io/hyperledger/cactus-besu-21-1-6-all-in-one"; + const besuOptions = { containerImageName, containerImageVersion }; + const besuTestLedger = new BesuTestLedger(besuOptions); + + let apiServer: ApiServer; + let node1Host: string; + let Web3Quorum: IWeb3Quorum; + let besuPrivateKey: string; + let web3: Web3; + + beforeAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).toResolve(); + + await besuTestLedger.start(); + + const keyEncoder: KeyEncoder = new KeyEncoder("secp256k1"); + const keychainId = uuidv4(); + const keychainRef = uuidv4(); + const { privateKey } = Secp256k1Keys.generateKeyPairsBuffer(); + const keyHex = privateKey.toString("hex"); + const pem = keyEncoder.encodePrivate(keyHex, KeyFormat.Raw, KeyFormat.PEM); + const keychain = new PluginKeychainMemory({ + backend: new Map([[keychainRef, pem]]), + keychainId, + logLevel, + instanceId: uuidv4(), + }); + + const httpServer1 = createServer(); + await new Promise((resolve, reject) => { + httpServer1.once("error", reject); + httpServer1.once("listening", resolve); + httpServer1.listen(0, "127.0.0.1"); + }); + + const addressInfo1 = httpServer1.address() as AddressInfo; + log.debug(`HttpServer1 AddressInfo: ${JSON.stringify(addressInfo1)}`); + node1Host = `http://${addressInfo1.address}:${addressInfo1.port}`; + log.debug(`Cactus Node 1 Host: ${node1Host}`); + + const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); + const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); + + const pluginRegistry = new PluginRegistry({ plugins: [keychain] }); + const options: IPluginLedgerConnectorBesuOptions = { + instanceId: uuidv4(), + rpcApiHttpHost, + rpcApiWsHost, + pluginRegistry, + logLevel, + }; + + const pluginValidatorBesu = new PluginLedgerConnectorBesu(options); + + const configService = new ConfigService(); + const apiServerOptions = await configService.newExampleConfig(); + apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE; + apiServerOptions.configFile = ""; + apiServerOptions.apiCorsDomainCsv = "*"; + apiServerOptions.apiPort = addressInfo1.port; + apiServerOptions.cockpitPort = 0; + apiServerOptions.grpcPort = 0; + apiServerOptions.crpcPort = 0; + apiServerOptions.apiTlsEnabled = false; + const config = + await configService.newExampleConfigConvict(apiServerOptions); + pluginRegistry.add(pluginValidatorBesu); + + apiServer = new ApiServer({ + httpServerApi: httpServer1, + config: config.getProperties(), + pluginRegistry, + }); + + await apiServer.start(); + + const web3Provider = new Web3.providers.HttpProvider(rpcApiHttpHost); + web3 = new Web3(web3Provider); + Web3Quorum = Web3JsQuorum(web3); + const besuKeyPair = await besuTestLedger.getBesuKeyPair(); + besuPrivateKey = besuKeyPair.privateKey.toLowerCase().startsWith("0x") + ? besuKeyPair.privateKey.substring(2) + : besuKeyPair.privateKey; }); - const addressInfo1 = httpServer1.address() as AddressInfo; - t.comment(`HttpServer1 AddressInfo: ${JSON.stringify(addressInfo1)}`); - const node1Host = `http://${addressInfo1.address}:${addressInfo1.port}`; - t.comment(`Cactus Node 1 Host: ${node1Host}`); - const besuTestLedger = new BesuTestLedger(); - await besuTestLedger.start(); + afterAll(async () => { + await apiServer.shutdown(); + }); const tearDown = async () => { await besuTestLedger.stop(); await besuTestLedger.destroy(); }; - test.onFinish(tearDown); - const testAccount = await besuTestLedger.createEthTestAccount(); - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); - - // 2. Instantiate plugin registry which will provide the web service plugin with the key value storage plugin - const pluginRegistry = new PluginRegistry({ plugins: [keychain] }); - - // 3. Instantiate the web service consortium plugin - const options: IPluginLedgerConnectorBesuOptions = { - instanceId: uuidv4(), - rpcApiHttpHost, - rpcApiWsHost, - pluginRegistry, - logLevel, - }; - const pluginValidatorBesu = new PluginLedgerConnectorBesu(options); - - // 4. Create the API Server object that we embed in this test - const configService = new ConfigService(); - const apiServerOptions = await configService.newExampleConfig(); - apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE; - apiServerOptions.configFile = ""; - apiServerOptions.apiCorsDomainCsv = "*"; - apiServerOptions.apiPort = addressInfo1.port; - apiServerOptions.cockpitPort = 0; - apiServerOptions.grpcPort = 0; - apiServerOptions.crpcPort = 0; - apiServerOptions.apiTlsEnabled = false; - const config = await configService.newExampleConfigConvict(apiServerOptions); - - pluginRegistry.add(pluginValidatorBesu); - - const apiServer = new ApiServer({ - httpServerApi: httpServer1, - config: config.getProperties(), - pluginRegistry, - }); - - // 5. make sure the API server is shut down when the testing if finished. - test.onFinish(() => apiServer.shutdown()); - - // 6. Start the API server which is now listening on port A and it's healthcheck works through the main SDK - await apiServer.start(); - - // 7. Instantiate the main SDK dynamically with whatever port the API server ended up bound to (port 0) - t.comment(`AddressInfo: ${JSON.stringify(addressInfo1)}`); - - const web3Provider = new Web3.providers.HttpProvider(rpcApiHttpHost); - const web3 = new Web3(web3Provider); - const Web3Quorum: IWeb3Quorum = Web3JsQuorum(web3); - - const orionKeyPair = await besuTestLedger.getOrionKeyPair(); - const besuKeyPair = await besuTestLedger.getBesuKeyPair(); - - const besuPrivateKey = besuKeyPair.privateKey.toLowerCase().startsWith("0x") - ? besuKeyPair.privateKey.substring(2) - : besuKeyPair.privateKey; // besu node's private key - - const contractOptions = { - data: `0x123`, - // privateFrom : Orion public key of the sender. - privateFrom: orionKeyPair.publicKey, - // privateFor : Orion public keys of recipients or privacyGroupId: Privacy group to receive the transaction - privateFor: [orionKeyPair.publicKey], - // privateKey: Ethereum private key with which to sign the transaction. - privateKey: besuPrivateKey, - }; - - const transactionHash = - await Web3Quorum.priv.generateAndSendRawTransaction(contractOptions); + afterAll(tearDown); - await web3.eth.getTransaction(transactionHash); - - const request: GetPastLogsV1Request = { - address: testAccount.address, - }; - - const configuration = new BesuApiClientOptions({ basePath: node1Host }); - const api = new BesuApiClient(configuration); - - const res = await api.getPastLogsV1(request); - // const { } = res; - t.ok(res, "API response object is truthy"); - t.ok(res.data.logs, "Response.logs is truthy ok"); - t.true(Array.isArray(res.data.logs), "Response.logs is Array ok"); -}); + afterAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).toResolve(); + }); -test("AFTER " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning didn't throw OK"); - t.end(); + test(testCase, async () => { + const orionKeyPair = await besuTestLedger.getOrionKeyPair(); + const contractOptions = { + data: `0x123`, + // privateFrom : Orion public key of the sender. + privateFrom: orionKeyPair.publicKey, + // privateFor : Orion public keys of recipients or privacyGroupId: Privacy group to receive the transaction + privateFor: [orionKeyPair.publicKey], + // privateKey: Ethereum private key with which to sign the transaction. + privateKey: besuPrivateKey, + }; + + const transactionHash = + await Web3Quorum.priv.generateAndSendRawTransaction(contractOptions); + await web3.eth.getTransaction(transactionHash); + + const testAccount = await besuTestLedger.createEthTestAccount(); + const request: GetPastLogsV1Request = { + address: testAccount.address, + }; + + const configuration = new BesuApiClientOptions({ basePath: node1Host }); + const api = new BesuApiClient(configuration); + + const res = await api.getPastLogsV1(request); + + expect(res).toBeTruthy(); + expect(res.data.logs).toBeTruthy(); + expect(res.data.logs).toBeArray(); + }); }); diff --git a/tsconfig.base.json b/tsconfig.base.json index 1290f6141f..3867cf1474 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -9,13 +9,10 @@ }, /* Basic Options */ "incremental": true, /* Enable incremental compilation */ - "target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ + "target": "ES2022", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ "module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "lib": [ - "es2015", - "es2016", - "es2017", - "es2019", + "ES2022", "dom" ], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ diff --git a/tsconfig.json b/tsconfig.json index 600c138e5e..790f7474e6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -172,13 +172,10 @@ ], "compilerOptions": { "incremental": true, - "target": "ES2017", + "target": "ES2022", "module": "Node16", "lib": [ - "es2015", - "es2016", - "es2017", - "es2019", + "ES2022", "dom" ] /* Specify library files to be included in the compilation. */, // "allowJs": true, /* Allow javascript files to be compiled. */