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

test(connector-besu): migrated lock-contract.test.ts to Jest #2934

Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ jobs:
JEST_TEST_PATTERN: packages/cactus-plugin-ledger-connector-besu/src/test/typescript/(unit|integration|benchmark)/.*/*.test.ts
JEST_TEST_RUNNER_DISABLED: false
TAPE_TEST_PATTERN: >-
--files={./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-block.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-past-logs.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-record-locator.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/private-deploy-contract-from-json-cactus.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/private-deploy-contract-from-json-web3-eea.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-deploy-contract-from-json.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-balance.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-block.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-past-logs.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-record-locator.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-invoke-contract.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/lock-contract.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/openapi/openapi-validation.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/v21-besu-get-transaction.test.ts}
--files={./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-block.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-past-logs.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-record-locator.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/private-deploy-contract-from-json-cactus.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/private-deploy-contract-from-json-web3-eea.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-deploy-contract-from-json.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-balance.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-block.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-past-logs.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-record-locator.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-invoke-contract.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/openapi/openapi-validation.test.ts,./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/v21-besu-get-transaction.test.ts}
TAPE_TEST_RUNNER_DISABLED: false
needs: build-dev
runs-on: ubuntu-20.04
Expand Down
1 change: 0 additions & 1 deletion .taprc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ ts: true
files:
- ./packages/cactus-plugin-keychain-aws-sm/src/test/typescript/integration/openapi/openapi-validation.test.ts
- ./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/deploy-lock-asset.test.ts
- ./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/lock-contract.test.ts
- ./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/run-transaction-with-ws-ids.test.ts
- ./packages/cactus-plugin-ledger-connector-corda/src/test/typescript/integration/deploy-cordapp-jars-to-nodes-v4.8-express.test.ts
- ./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-invoke-contract-json-object-endpoints.test.ts
Expand Down
1 change: 0 additions & 1 deletion jest.config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,100 +1,115 @@
import test, { Test } from "tape";
import "jest-extended";
import { v4 as uuidv4 } from "uuid";
import Web3 from "web3";
import { Account } from "web3-core";

import { PluginRegistry } from "@hyperledger/cactus-core";
import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";
import { BesuTestLedger } from "@hyperledger/cactus-test-tooling";
import { LogLevelDesc } from "@hyperledger/cactus-common";
import { PluginImportType } from "@hyperledger/cactus-core-api";

import LockAssetContractJson from "../../../solidity/hello-world-contract/LockAsset.json";

import {
EthContractInvocationType,
Web3SigningCredentialType,
PluginLedgerConnectorBesu,
PluginFactoryLedgerConnector,
//Web3SigningCredentialCactusKeychainRef,
ReceiptType,
} from "../../../../main/typescript/public-api";
import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";
import { BesuTestLedger } from "@hyperledger/cactus-test-tooling";
import { LoggerProvider, LogLevelDesc } from "@hyperledger/cactus-common";
import LockAssetContractJson from "../../../solidity/hello-world-contract/LockAsset.json";
import Web3 from "web3";
import { PluginImportType } from "@hyperledger/cactus-core-api";
const level = "INFO";
const label = "lock-contact test";
const log = LoggerProvider.getOrCreate({ level, label });
test("deploys contract via .json file", async (t: Test) => {
const logLevel: LogLevelDesc = "TRACE";

const logLevel: LogLevelDesc = "INFO";

describe("PluginLedgerConnectorBesu", () => {
const besuTestLedger = new BesuTestLedger();
await besuTestLedger.start();
let besuKeyPair: { privateKey: string };
let firstHighNetWorthAccount: string;
let keychainPlugin: PluginKeychainMemory;
let testEthAccount: Account;
let connector: PluginLedgerConnectorBesu;

beforeAll(async () => {
await besuTestLedger.start();

/**
* Constant defining the standard 'dev' Besu genesis.json contents.
*
* @see https://github.com/hyperledger/besu/blob/1.5.1/config/src/main/resources/dev.json
*/
firstHighNetWorthAccount = besuTestLedger.getGenesisAccountPubKey();
besuKeyPair = {
privateKey: besuTestLedger.getGenesisAccountPrivKey(),
};
});

test.onFinish(async () => {
afterAll(async () => {
await besuTestLedger.stop();
await besuTestLedger.destroy();
});

const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost();
const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost();

/**
* Constant defining the standard 'dev' Besu genesis.json contents.
*
* @see https://github.com/hyperledger/besu/blob/1.5.1/config/src/main/resources/dev.json
*/
const firstHighNetWorthAccount = besuTestLedger.getGenesisAccountPubKey();
const besuKeyPair = {
privateKey: besuTestLedger.getGenesisAccountPrivKey(),
};
const contractName = "LockAsset";

const web3 = new Web3(rpcApiHttpHost);
const testEthAccount = web3.eth.accounts.create(uuidv4());

const keychainEntryKey = uuidv4();
const keychainEntryValue = testEthAccount.privateKey;
const keychainPlugin = new PluginKeychainMemory({
instanceId: uuidv4(),
keychainId: uuidv4(),
// pre-provision keychain with mock backend holding the private key of the
// test account that we'll reference while sending requests with the
// signing credential pointing to this keychain entry.
backend: new Map([[keychainEntryKey, keychainEntryValue]]),
logLevel,
});
keychainPlugin.set(
LockAssetContractJson.contractName,
JSON.stringify(LockAssetContractJson),
);
const factory = new PluginFactoryLedgerConnector({
pluginImportType: PluginImportType.Local,
});
const connector: PluginLedgerConnectorBesu = await factory.create({
rpcApiHttpHost,
rpcApiWsHost,
instanceId: uuidv4(),
pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }),
afterAll(async () => {
await connector.shutdown();
});

await connector.transact({
web3SigningCredential: {
ethAccount: firstHighNetWorthAccount,
secret: besuKeyPair.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
consistencyStrategy: {
blockConfirmations: 0,
receiptType: ReceiptType.NodeTxPoolAck,
},
transactionConfig: {
from: firstHighNetWorthAccount,
to: testEthAccount.address,
value: 10e9,
gas: 1000000,
},
});
it("creates test account from scratch with seed money", async () => {
const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost();
const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost();

const web3 = new Web3(rpcApiHttpHost);
testEthAccount = web3.eth.accounts.create(uuidv4());

const keychainEntryKey = uuidv4();
const keychainEntryValue = testEthAccount.privateKey;
keychainPlugin = new PluginKeychainMemory({
instanceId: uuidv4(),
keychainId: uuidv4(),
// pre-provision keychain with mock backend holding the private key of the
// test account that we'll reference while sending requests with the
// signing credential pointing to this keychain entry.
backend: new Map([[keychainEntryKey, keychainEntryValue]]),
logLevel,
});
keychainPlugin.set(
LockAssetContractJson.contractName,
JSON.stringify(LockAssetContractJson),
);
const factory = new PluginFactoryLedgerConnector({
pluginImportType: PluginImportType.Local,
});
connector = await factory.create({
rpcApiHttpHost,
rpcApiWsHost,
instanceId: uuidv4(),
logLevel,
pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }),
});

await connector.transact({
web3SigningCredential: {
ethAccount: firstHighNetWorthAccount,
secret: besuKeyPair.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
consistencyStrategy: {
blockConfirmations: 0,
receiptType: ReceiptType.NodeTxPoolAck,
},
transactionConfig: {
from: firstHighNetWorthAccount,
to: testEthAccount.address,
value: 10e9,
gas: 1000000,
},
});

const balance = await web3.eth.getBalance(testEthAccount.address);
t.ok(balance, "Retrieved balance of test account OK");
t.equals(parseInt(balance, 10), 10e9, "Balance of test account is OK");
const balance = await web3.eth.getBalance(testEthAccount.address);
expect(balance).toBeTruthy();

let contractAddress: string;
expect(parseInt(balance, 10)).toEqual(10e9);
});

test("deploys contract via .json file", async (t2: Test) => {
it("deploys contract via .json file, verifies lock/unlock ops", async () => {
const deployOut = await connector.deployContract({
keychainId: keychainPlugin.getKeychainId(),
contractName: LockAssetContractJson.contractName,
Expand All @@ -108,24 +123,16 @@ test("deploys contract via .json file", async (t: Test) => {
bytecode: LockAssetContractJson.bytecode,
gas: 1000000,
});
t2.ok(deployOut, "deployContract() output is truthy OK");
t2.ok(
deployOut.transactionReceipt,
"deployContract() output.transactionReceipt is truthy OK",
);
t2.ok(
deployOut.transactionReceipt.contractAddress,
"deployContract() output.transactionReceipt.contractAddress is truthy OK",
);
expect(deployOut).toBeTruthy();
expect(deployOut.transactionReceipt).toBeTruthy();
expect(deployOut.transactionReceipt.contractAddress).toBeTruthy();

contractAddress = deployOut.transactionReceipt.contractAddress as string;
t2.ok(
typeof contractAddress === "string",
"contractAddress typeof string OK",
);
const contractAddress = deployOut.transactionReceipt.contractAddress;
expect(contractAddress).toBeString();
expect(contractAddress).not.toBeEmpty();

const { success: createRes } = await connector.invokeContract({
contractName,
contractName: LockAssetContractJson.contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "createAsset",
Expand All @@ -137,11 +144,11 @@ test("deploys contract via .json file", async (t: Test) => {
},
gas: 1000000,
});
t2.ok(createRes, "create Asset() output is truthy");
t2.equals(createRes, true, "create Asset Ok");
log.warn("create ok");
expect(createRes).toBeTruthy();
expect(createRes).toBeTrue();

const { success: lockRes } = await connector.invokeContract({
contractName,
contractName: LockAssetContractJson.contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "lockAsset",
Expand All @@ -153,11 +160,12 @@ test("deploys contract via .json file", async (t: Test) => {
},
gas: 1000000,
});
log.warn("checking lock res");
t2.ok(lockRes, "lock Asset() output is truthy");
t2.equals(lockRes, true, "lock Asset Ok");

expect(lockRes).toBeTruthy();
expect(lockRes).toEqual(true);

const { success: unLockRes } = await connector.invokeContract({
contractName,
contractName: LockAssetContractJson.contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "unLockAsset",
Expand All @@ -169,10 +177,12 @@ test("deploys contract via .json file", async (t: Test) => {
},
gas: 1000000,
});
t2.ok(unLockRes, "unlock Asset() output is truthy");
t2.equals(unLockRes, true, "unlock Asset Ok");

expect(unLockRes).toBeTruthy();
expect(unLockRes).toBeTrue();

const { success: lockRes2 } = await connector.invokeContract({
contractName,
contractName: LockAssetContractJson.contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "lockAsset",
Expand All @@ -184,11 +194,12 @@ test("deploys contract via .json file", async (t: Test) => {
},
gas: 1000000,
});
t2.ok(lockRes2, "lock Asset() again output is truthy");
t2.equals(lockRes2, true, "lock Asset again Ok");
log.warn("asset is locked again");

expect(lockRes2).toBeTruthy();
expect(lockRes2).toBeTrue();

const { success: deleteRes } = await connector.invokeContract({
contractName,
contractName: LockAssetContractJson.contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "deleteAsset",
Expand All @@ -200,9 +211,8 @@ test("deploys contract via .json file", async (t: Test) => {
},
gas: 1000000,
});
console.log(deleteRes);
t2.ok(deleteRes, "delete Asset() output is truthy");
t2.equals(deleteRes, true, "delete Asset Ok");

expect(deleteRes).toBeTruthy();
expect(deleteRes).toBeTrue();
});
t.end();
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18454,7 +18454,7 @@ __metadata:
"@types/react": ^18.2.39
"@types/react-dom": ^18.2.17
"@types/uuid": ^9.0.7
axios: ^1.6.2
axios: 1.6.0
react: ^18.2.0
react-dom: ^18.2.0
react-scripts: 5.0.1
Expand Down