Skip to content

Commit

Permalink
test: add test for geth-test-ledger
Browse files Browse the repository at this point in the history
-add tests for geth-test-ledger
-fix small errors in GethTestLedger class

Closes: hyperledger-cacti#2579
Depends on: hyperledger-cacti#2577

Signed-off-by: Tomasz Awramski <[email protected]>
  • Loading branch information
rwat17 authored and petermetz committed Aug 15, 2023
1 parent 1b800fe commit 17eef7f
Show file tree
Hide file tree
Showing 6 changed files with 536 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/cactus-test-geth-ledger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"author": {
"name": "Hyperledger Cactus Contributors",
"email": "[email protected]",
"url": "https://www.hyperledger.org/use/cactus"
"url": "https://www.hyperledger.org/use/cacti"
},
"contributors": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ export interface IGethTestLedgerOptions {
* Default values used by GethTestLedger constructor.
*/
export const GETH_TEST_LEDGER_DEFAULT_OPTIONS = Object.freeze({
containerImageName: "cactus_geth_all_in_one",
containerImageVersion: "latest",
containerImageName: "cacti-geth-all-in-one",
containerImageVersion: "2023-07-27-2a8c48ed6",
logLevel: "info" as LogLevelDesc,
emitContainerLogs: false,
envVars: [],
useRunningLedger: false,
});

export const WHALE_ACCOUNT_PRIVATE_KEY =
"86bbf98cf5e5b1c43d2c8701764897357e0fa24982c0137efabf6dc3a6e7b69e";
"0x86bbf98cf5e5b1c43d2c8701764897357e0fa24982c0137efabf6dc3a6e7b69e";
export const WHALE_ACCOUNT_PUBLIC_KEY =
"6a2ec8c50ba1a9ce47c52d1cb5b7136ee9d0ccc0";
"0x6a2ec8c50ba1a9ce47c52d1cb5b7136ee9d0ccc0";

export class GethTestLedger {
public static readonly CLASS_NAME = "GethTestLedger";
Expand Down Expand Up @@ -289,11 +289,10 @@ export class GethTestLedger {
* @returns New account address
*/
public async newEthPersonalAccount(
seedMoney = 10e8,
seedMoney = 0x10e8,
password = "test",
): Promise<string> {
const account = await this.web3.eth.personal.newAccount(password);

const receipt = await this.transferAssetFromCoinbase(account, seedMoney);

if (receipt instanceof Error) {
Expand Down Expand Up @@ -321,6 +320,7 @@ export class GethTestLedger {
from: WHALE_ACCOUNT_PUBLIC_KEY,
to: targetAccount,
value: value,
gasPrice: await this.web3.eth.getGasPrice(),
gas: 1000000,
},
WHALE_ACCOUNT_PRIVATE_KEY,
Expand Down Expand Up @@ -358,7 +358,8 @@ export class GethTestLedger {
{
from: WHALE_ACCOUNT_PUBLIC_KEY,
data: contractTx.encodeABI(),
gas: 8000000, // Max possible gas
gasPrice: await this.web3.eth.getGasPrice(),
gas: 1000000,
nonce: await this.web3.eth.getTransactionCount(
WHALE_ACCOUNT_PUBLIC_KEY,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@
* Tests of Geth helper typescript setup class.
*
* TODO:
* - `fullContainerImageName` test
* - `className` test
* - `container`/`web3` throws when not started, works after
* - `getContainerStatus` (check if healthy after test start)
* - dont test `useRunningLedger` or `omitPull` options
* - `createEthTestAccount` test
* - `newEthPersonalAccount` test
* - Refactor to use `Clef` instead of `eth.personal`
* - `transferAssetFromCoinbase` test
* - `deployContract` test
* - `getRpcApiHttpHost` / `getRpcApiWebSocketHost` - valid URL returned
*/

//////////////////////////////////
Expand All @@ -28,6 +18,7 @@
const testLogLevel: LogLevelDesc = "info";

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

import {
LogLevelDesc,
Expand All @@ -48,9 +39,11 @@ const log: Logger = LoggerProvider.getOrCreate({
/**
* Main test suite
*/

describe("Geth Test Ledger checks", () => {
let ledger: GethTestLedger;

let testEthAcc: any;
let web3Instance: Web3;
//////////////////////////////////
// Environment Setup
//////////////////////////////////
Expand All @@ -64,10 +57,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(true);
web3Instance = new Web3(await ledger.getRpcApiHttpHost());
expect(web3Instance).toBeTruthy;
});

afterAll(async () => {
Expand Down Expand Up @@ -112,4 +107,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 () => {
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 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(1000001000),
);
});

test("Method deployContract works and returns contract address", async () => {
const deployedData = await ledger.deployContract(
contractData.abi,
contractData.bytecode,
[],
);

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

const contractCallResult = await contract.methods.sayHello().call();

expect(deployedData).toBeTruthy();
expect(deployedData.contractAddress).toStartWith("0x");
expect(deployedData.contractAddress).toHaveLength(42);
expect(contractCallResult).toEqual("Hello World!");
});

test("Method getRpcApiHttpHost works and overlaps with actual open ports", 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 works and overlaps with actual open ports", async () => {
const wsHostAddress = await ledger.getRpcApiWebSocketHost();
const wsPort = await ledger.getHostPortWs();

expect(wsHostAddress).toBeTruthy();
expect(wsHostAddress).toStartWith("ws://");
expect(wsHostAddress).toContain(`${wsPort}`);
});
});
Loading

0 comments on commit 17eef7f

Please sign in to comment.