diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b015cff833..59b56b3a1c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -2416,7 +2416,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-balance-endpoint.test.ts,./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-balance-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,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-sign-transaction-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-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,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-sign-transaction-endpoint.test.ts} TAPE_TEST_RUNNER_DISABLED: false runs-on: ubuntu-22.04 steps: diff --git a/.taprc b/.taprc index b868c9c34c..ba16cc18b6 100644 --- a/.taprc +++ b/.taprc @@ -24,9 +24,7 @@ files: - ./packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/openapi/openapi-validation.test.ts - ./packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/openapi/openapi-validation-no-keychain.test.ts - ./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/get-balance-endpoint.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-balance-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 diff --git a/jest.config.js b/jest.config.js index 98135c6ef1..e72ea6af09 100644 --- a/jest.config.js +++ b/jest.config.js @@ -36,9 +36,7 @@ module.exports = { `./packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/openapi/openapi-validation.test.ts`, `./packages/cactus-plugin-ledger-connector-xdai/src/test/typescript/integration/openapi/openapi-validation-no-keychain.test.ts`, `./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/get-balance-endpoint.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-balance-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`, diff --git a/packages/cactus-test-plugin-ledger-connector-besu/package.json b/packages/cactus-test-plugin-ledger-connector-besu/package.json index f32c9b063d..44bff0eeb4 100644 --- a/packages/cactus-test-plugin-ledger-connector-besu/package.json +++ b/packages/cactus-test-plugin-ledger-connector-besu/package.json @@ -69,6 +69,7 @@ "@types/uuid": "10.0.0", "axios": "1.7.7", "express": "4.19.2", + "http-status-codes": "2.3.0", "uuid": "10.0.0", "web3-core": "1.6.1" }, diff --git a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-balance-endpoint.test.ts b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-balance-endpoint.test.ts index e92e60a8f8..ea704c4328 100644 --- a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-balance-endpoint.test.ts +++ b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-balance-endpoint.test.ts @@ -1,11 +1,14 @@ -import test, { Test } from "tape-promise/tape"; +import { createServer } from "node:http"; +import { AddressInfo } from "node:net"; +Error.stackTraceLimit = 100; + +import "jest-extended"; + +import { StatusCodes } from "http-status-codes"; 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 { Account } from "web3-core"; import { ApiServer, @@ -16,13 +19,13 @@ import { Secp256k1Keys, KeyFormat, LogLevelDesc, + LoggerProvider, } from "@hyperledger/cactus-common"; - import { BesuTestLedger, + buildImageBesuAllInOneLatest, pruneDockerAllIfGithubAction, } from "@hyperledger/cactus-test-tooling"; - import { BesuApiClientOptions, BesuApiClient, @@ -30,149 +33,135 @@ import { PluginLedgerConnectorBesu, GetBalanceV1Request, } 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 getBalance 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(); -}); +describe("PluginLedgerConnectorBesu", () => { + const logLevel: LogLevelDesc = "INFO"; + const log = LoggerProvider.getOrCreate({ + label: "get-balance-endpoint.test.ts", + level: logLevel, + }); -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(), }); - + const pluginRegistry = new PluginRegistry({ plugins: [keychain] }); 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; - 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(); + const expectedBalance = Math.ceil(Math.random() * Math.pow(10, 6)); - const tearDown = async () => { - await besuTestLedger.stop(); - await besuTestLedger.destroy(); - }; + let ledger: BesuTestLedger; + let apiServer: ApiServer; + let testAccount: Account; + let node1Host: string; - test.onFinish(tearDown); - const testAccount = await besuTestLedger.createEthTestAccount(); - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); + beforeAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).toResolve(); + }); - // 2. Instantiate plugin registry which will provide the web service plugin with the key value storage plugin - const pluginRegistry = new PluginRegistry({ plugins: [keychain] }); + beforeAll(async () => { + const ledgerImg = await buildImageBesuAllInOneLatest({ logLevel }); + const { imageName, imageVersion } = ledgerImg; + ledger = new BesuTestLedger({ + containerImageName: imageName, + containerImageVersion: imageVersion, + logLevel, + }); + // we've built the image locally so trying to pull it would fail + const omitImagePullFromNetwork = true; + await ledger.start(omitImagePullFromNetwork); + }); - // 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, + beforeAll(async () => { + 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}`); + + testAccount = await ledger.createEthTestAccount(expectedBalance); + const rpcApiHttpHost = await ledger.getRpcApiHttpHost(); + const rpcApiWsHost = await ledger.getRpcApiWsHost(); + + // 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 cfgSvc = new ConfigService(); + const apiSrvOpts = await cfgSvc.newExampleConfig(); + apiSrvOpts.authorizationProtocol = AuthorizationProtocol.NONE; + apiSrvOpts.configFile = ""; + apiSrvOpts.apiCorsDomainCsv = "*"; + apiSrvOpts.apiPort = addressInfo1.port; + apiSrvOpts.cockpitPort = 0; + apiSrvOpts.grpcPort = 0; + apiSrvOpts.crpcPort = 0; + apiSrvOpts.apiTlsEnabled = false; + const config = await cfgSvc.newExampleConfigConvict(apiSrvOpts); + + pluginRegistry.add(pluginValidatorBesu); + + apiServer = new ApiServer({ + httpServerApi: httpServer1, + config: config.getProperties(), + pluginRegistry, + }); + + await apiServer.start(); + log.debug(`AddressInfo: ${JSON.stringify(addressInfo1)}`); }); - // 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); - await web3.eth.getTransaction(transactionHash); - - /* - const transaction = await web3.eth.getTransaction(transactionHash); - const singData = jsObjectSigner.sign(transaction.input); - const signDataHex = Buffer.from(singData).toString("hex"); - */ - - const request: GetBalanceV1Request = { - address: testAccount.address, - }; - - const configuration = new BesuApiClientOptions({ basePath: node1Host }); - const api = new BesuApiClient(configuration); - - // Test for 200 valid response test case - const res = await api.getBalanceV1(request); - t.ok(res, "API response object is truthy"); - t.true(typeof res.data.balance === "string", "Response is String 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(); + afterAll(async () => { + await ledger.stop(); + await ledger.destroy(); + }); + + afterAll(async () => await apiServer.shutdown()); + + it("getBalanceV1() - retrieves ETH account balance", async () => { + const request: GetBalanceV1Request = { + address: testAccount.address, + }; + + const configuration = new BesuApiClientOptions({ basePath: node1Host }); + const api = new BesuApiClient(configuration); + + // Test for 200 valid response test case + const getBalanceExchange = api.getBalanceV1(request); + await expect(getBalanceExchange).resolves.toMatchObject({ + status: StatusCodes.OK, + data: expect.objectContaining({ + balance: expect.stringMatching(expectedBalance.toFixed(0)), + }), + }); + }); }); diff --git a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-balance-endpoint.test.ts b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-balance-endpoint.test.ts index 0365519e64..ef87fb08e3 100644 --- a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-balance-endpoint.test.ts +++ b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-balance-endpoint.test.ts @@ -1,11 +1,14 @@ -import test, { Test } from "tape-promise/tape"; +import { createServer } from "node:http"; +import { AddressInfo } from "node:net"; +Error.stackTraceLimit = 100; + +import "jest-extended"; + +import { StatusCodes } from "http-status-codes"; 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 { Account } from "web3-core"; import { ApiServer, @@ -16,13 +19,12 @@ import { Secp256k1Keys, KeyFormat, LogLevelDesc, + LoggerProvider, } from "@hyperledger/cactus-common"; - import { BesuTestLedger, pruneDockerAllIfGithubAction, } from "@hyperledger/cactus-test-tooling"; - import { BesuApiClientOptions, BesuApiClient, @@ -30,152 +32,136 @@ import { PluginLedgerConnectorBesu, GetBalanceV1Request, } 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 getBalance 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(); -}); +describe("PluginLedgerConnectorBesu", () => { + const logLevel: LogLevelDesc = "INFO"; + const log = LoggerProvider.getOrCreate({ + label: "get-balance-endpoint.test.ts", + level: logLevel, + }); + const containerImageVersion = "2021-08-24--feat-1244"; + const containerImageName = + "ghcr.io/hyperledger/cactus-besu-21-1-6-all-in-one"; -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(), }); - + const pluginRegistry = new PluginRegistry({ plugins: [keychain] }); 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; - t.comment(`HttpServer1 AddressInfo: ${JSON.stringify(addressInfo1)}`); - const node1Host = `http://${addressInfo1.address}:${addressInfo1.port}`; - t.comment(`Cactus Node 1 Host: ${node1Host}`); - 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); - await besuTestLedger.start(); + const expectedBalance = Math.ceil(Math.random() * Math.pow(10, 6)); - const tearDown = async () => { - await besuTestLedger.stop(); - await besuTestLedger.destroy(); - }; + let ledger: BesuTestLedger; + let apiServer: ApiServer; + let testAccount: Account; + let node1Host: string; - test.onFinish(tearDown); - const testAccount = await besuTestLedger.createEthTestAccount(); - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); + beforeAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).toResolve(); + }); - // 2. Instantiate plugin registry which will provide the web service plugin with the key value storage plugin - const pluginRegistry = new PluginRegistry({ plugins: [keychain] }); + beforeAll(async () => { + ledger = new BesuTestLedger({ + containerImageName, + containerImageVersion, + logLevel, + }); + // we've are using a pre-built image so pulling is necessary + const omitImagePullFromNetwork = false; + await ledger.start(omitImagePullFromNetwork); + }); - // 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.apiTlsEnabled = false; - const config = await configService.newExampleConfigConvict(apiServerOptions); - - pluginRegistry.add(pluginValidatorBesu); - - const apiServer = new ApiServer({ - httpServerApi: httpServer1, - config: config.getProperties(), - pluginRegistry, + beforeAll(async () => { + 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}`); + + testAccount = await ledger.createEthTestAccount(expectedBalance); + const rpcApiHttpHost = await ledger.getRpcApiHttpHost(); + const rpcApiWsHost = await ledger.getRpcApiWsHost(); + + // 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 cfgSvc = new ConfigService(); + const apiSrvOpts = await cfgSvc.newExampleConfig(); + apiSrvOpts.authorizationProtocol = AuthorizationProtocol.NONE; + apiSrvOpts.configFile = ""; + apiSrvOpts.apiCorsDomainCsv = "*"; + apiSrvOpts.apiPort = addressInfo1.port; + apiSrvOpts.cockpitPort = 0; + apiSrvOpts.grpcPort = 0; + apiSrvOpts.crpcPort = 0; + apiSrvOpts.apiTlsEnabled = false; + const config = await cfgSvc.newExampleConfigConvict(apiSrvOpts); + + pluginRegistry.add(pluginValidatorBesu); + + apiServer = new ApiServer({ + httpServerApi: httpServer1, + config: config.getProperties(), + pluginRegistry, + }); + + await apiServer.start(); + log.debug(`AddressInfo: ${JSON.stringify(addressInfo1)}`); }); - // 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 web3JsQuorum: 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 web3JsQuorum.priv.generateAndSendRawTransaction(contractOptions); - await web3.eth.getTransaction(transactionHash); - - /* - const transaction = await web3.eth.getTransaction(transactionHash); - const singData = jsObjectSigner.sign(transaction.input); - const signDataHex = Buffer.from(singData).toString("hex"); - */ - - const request: GetBalanceV1Request = { - //is it suppose to be GetBalanceV1Request? - address: testAccount.address, - }; - - const configuration = new BesuApiClientOptions({ basePath: node1Host }); - const api = new BesuApiClient(configuration); - - // Test for 200 valid response test case - const res = await api.getBalanceV1(request); - t.ok(res, "API response object is truthy"); - t.true(typeof res.data.balance === "string", "Response is String ok"); -}); + afterAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).toResolve(); + }); + + afterAll(async () => { + await ledger.stop(); + await ledger.destroy(); + }); -test("AFTER " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning didn't throw OK"); - t.end(); + afterAll(async () => await apiServer.shutdown()); + + it("getBalanceV1() - retrieves ETH account balance", async () => { + const request: GetBalanceV1Request = { + address: testAccount.address, + }; + + const configuration = new BesuApiClientOptions({ basePath: node1Host }); + const api = new BesuApiClient(configuration); + + // Test for 200 valid response test case + const getBalanceExchange = api.getBalanceV1(request); + await expect(getBalanceExchange).resolves.toMatchObject({ + status: StatusCodes.OK, + data: expect.objectContaining({ + balance: expect.stringMatching(expectedBalance.toFixed(0)), + }), + }); + }); }); diff --git a/yarn.lock b/yarn.lock index 9c0173fd02..e1648af2ff 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10857,6 +10857,7 @@ __metadata: "@types/uuid": "npm:10.0.0" axios: "npm:1.7.7" express: "npm:4.19.2" + http-status-codes: "npm:2.3.0" key-encoder: "npm:2.0.3" socket.io: "npm:4.6.2" uuid: "npm:10.0.0"