Skip to content

Commit

Permalink
test(geth-test-ledger): add basic tests
Browse files Browse the repository at this point in the history
- Add test suite for geth-test-ledger package.
- Add new test suit to CI
- It was initially proposed in #2588, I've added some cleanups and improvements.

Closes: #2579

Co-authored-by: Tomasz Awramski <[email protected]>

Signed-off-by: Michal Bajer <[email protected]>
  • Loading branch information
outSH committed Dec 12, 2023
1 parent f557241 commit ab080ef
Show file tree
Hide file tree
Showing 7 changed files with 542 additions and 13 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,31 @@ jobs:
node-version: ${{ env.NODEJS_VERSION }}
- uses: actions/[email protected]

- id: yarn-cache
name: Restore Yarn Cache
uses: actions/[email protected]
with:
key: ${{ runner.os }}-yarn-${{ hashFiles('./yarn.lock') }}
path: ./.yarn/
restore-keys: |
${{ runner.os }}-yarn-${{ hashFiles('./yarn.lock') }}
- run: ./tools/ci.sh
cactus-test-geth-ledger:
continue-on-error: false
env:
FULL_BUILD_DISABLED: true
JEST_TEST_PATTERN: packages/cactus-test-geth-ledger/src/test/typescript/(unit|integration|benchmark)/.*/*.test.ts
JEST_TEST_RUNNER_DISABLED: false
TAPE_TEST_RUNNER_DISABLED: true
needs: build-dev
runs-on: ubuntu-20.04
steps:
- name: Use Node.js ${{ env.NODEJS_VERSION }}
uses: actions/[email protected]
with:
node-version: ${{ env.NODEJS_VERSION }}
- uses: actions/[email protected]

- id: yarn-cache
name: Restore Yarn Cache
uses: actions/[email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SHA256 } from "crypto-js";
import {
PluginOdapGateway,
TransferInitializationV1Request,
ClientGatewayHelper
ClientGatewayHelper,
} from "@hyperledger/cactus-plugin-odap-hermes";
import { OdapMessageType } from "@hyperledger/cactus-plugin-odap-hermes";
import { FabricOdapGateway } from "./fabric-odap-gateway";
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// *****************************************************************************
// IMPORTANT: If you update this code then make sure to recompile
// it and update the .json file as well so that they
// remain in sync for consistent test executions.
// With that said, there shouldn't be any reason to recompile this, like ever...
// *****************************************************************************

pragma solidity >=0.7.0;

contract HelloWorld {
string private name = "CaptainCactus";

function sayHello() public pure returns (string memory) {
return "Hello World!";
}

function getName() public view returns (string memory) {
return name;
}

function setName(string memory newName) public {
name = newName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
// Constants
//////////////////////////////////

// Ledger settings
// const containerImageName = "ghcr.io/hyperledger/cactus-geth-all-in-one";
// const containerImageVersion = "2022-10-18-06770b6c";
// const useRunningLedger = false;

// Log settings
const testLogLevel: LogLevelDesc = "info";

import { GethTestLedger } from "../../../main/typescript/index";
import contractData from "../../solidity/hello-world-contract/HelloWorld.json";

import {
LogLevelDesc,
Expand All @@ -37,6 +33,7 @@ const log: Logger = LoggerProvider.getOrCreate({
*/
describe("Geth Test Ledger checks", () => {
let ledger: GethTestLedger;
let web3Instance: Web3;

//////////////////////////////////
// Environment Setup
Expand All @@ -51,10 +48,12 @@ describe("Geth Test Ledger checks", () => {
emitContainerLogs: true,
logLevel: testLogLevel,
});
log.debug("Geth image:", ledger.fullContainerImageName);
expect(ledger).toBeTruthy();
log.debug("Geth image:", ledger.fullContainerImageName);

await ledger.start();
await ledger.start(false);
web3Instance = new Web3(await ledger.getRpcApiHttpHost());
expect(web3Instance).toBeTruthy;
});

afterAll(async () => {
Expand Down Expand Up @@ -99,4 +98,78 @@ describe("Geth Test Ledger checks", () => {
wsWeb3.provider?.disconnect();
}
});

test("Class name is correct", async () => {
const className = ledger.className;
expect(className).toEqual("GethTestLedger");
});

test("Method createEthTestAccount works", async () => {
const testEthAcc = await ledger.createEthTestAccount();

expect(testEthAcc).toBeTruthy();
expect(testEthAcc.address).toHaveLength(42);
expect(testEthAcc.address).toStartWith("0x");
});

test("Method newEthPersonalAccount works", async () => {
const testEthAccount = await ledger.newEthPersonalAccount();

expect(testEthAccount).toBeTruthy();
expect(testEthAccount).toHaveLength(42);
expect(testEthAccount).toStartWith("0x");
});

test("Method transferAssetFromCoinbase works", async () => {
const testEthAcc = await ledger.createEthTestAccount();

const txReceipt = await ledger.transferAssetFromCoinbase(
testEthAcc.address,
1000,
);

expect(txReceipt).toBeTruthy();
expect(web3Instance.utils.toChecksumAddress(txReceipt.to)).toEqual(
testEthAcc.address,
);
expect(await web3Instance.eth.getBalance(testEthAcc.address)).toEqual(
BigInt("10000000000000001000"),
);
});

test("Method deployContract works and returns contract address", async () => {
const deployedData = await ledger.deployContract(
contractData.abi,
contractData.bytecode,
[],
);
expect(deployedData).toBeTruthy();
expect(deployedData.contractAddress).toStartWith("0x");
expect(deployedData.contractAddress).toHaveLength(42);

const contract = new web3Instance.eth.Contract(
contractData.abi,
deployedData.contractAddress,
);
expect(contract).toBeTruthy();

const contractCallResult = await contract.methods.sayHello().call();
expect(contractCallResult).toEqual("Hello World!");
});

test("Method getRpcApiHttpHost returns valid URL", async () => {
const httpHostAddress = await ledger.getRpcApiHttpHost();
const httpPort = await ledger.getHostPortHttp();
expect(httpHostAddress).toBeTruthy();
expect(httpHostAddress).toStartWith("http://");
expect(httpHostAddress).toContain(`${httpPort}`);
});

test("Method getRpcApiWebSocketHost returns valid URL", async () => {
const wsHostAddress = await ledger.getRpcApiWebSocketHost();
const wsPort = await ledger.getHostPortWs();
expect(wsHostAddress).toBeTruthy();
expect(wsHostAddress).toStartWith("ws://");
expect(wsHostAddress).toContain(`${wsPort}`);
});
});
6 changes: 2 additions & 4 deletions packages/cactus-test-geth-ledger/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
"rootDir": "./src",
"tsBuildInfoFile": "../../.build-cache/cactus-test-geth-ledger.tsbuildinfo"
},
"include": [
"./src"
],
"include": ["./src", "src/**/*.json"],
"references": [
{
"path": "../cactus-common/tsconfig.json"
Expand All @@ -18,4 +16,4 @@
"path": "../cactus-test-tooling/tsconfig.json"
}
]
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18485,7 +18485,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

0 comments on commit ab080ef

Please sign in to comment.