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 committed Jul 28, 2023
1 parent ab50253 commit b58032d
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { EventEmitter } from "events";
import Docker, { Container } from "dockerode"; // TODO - import dockerode from test-tooling?
import { v4 as internalIpV4 } from "internal-ip";
import Web3, { ContractAbi, TransactionReceipt } from "web3";
import type { Web3Account } from "web3-eth-accounts";
import { RuntimeError } from "run-time-error";

import {
Expand Down Expand Up @@ -36,9 +35,9 @@ export const GETH_TEST_LEDGER_DEFAULT_OPTIONS = Object.freeze({
});

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 @@ -264,7 +263,7 @@ export class GethTestLedger {
*
* @param [seedMoney=10e8] The amount of money to seed the new test account with.
*/
public async createEthTestAccount(seedMoney = 10e8): Promise<Web3Account> {
public async createEthTestAccount(seedMoney = "0x10e8"): Promise<any> {
const ethTestAccount = this.web3.eth.accounts.create();

const receipt = await this.transferAssetFromCoinbase(
Expand All @@ -289,11 +288,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 All @@ -312,7 +310,7 @@ export class GethTestLedger {
*/
public async transferAssetFromCoinbase(
targetAccount: string,
value: number,
value: string,
): Promise<TransactionReceipt> {
const fnTag = `${this.className}#transferAssetFromCoinbase()`;

Expand All @@ -321,6 +319,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 +357,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/simple-token-contract/contractData.json";

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

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

//////////////////////////////////
// Environment Setup
Expand Down Expand Up @@ -112,4 +105,66 @@ 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();
log.warn("createEthTestAccount", testEthAcc);

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

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

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

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

expect(txReceipt).toBeTruthy();
expect(Web3.utils.toChecksumAddress(txReceipt.to)).toEqual(
testEthAcc.address,
);
});

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

expect(deployedData).toBeTruthy();
expect(deployedData.contractAddress).toStartWith("0x");
});

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}`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pragma solidity ^0.5.1;

contract SimpleStorage {

event ValueChanged(string oldValue, string newValue);
string public value;

constructor(string memory _value) public {
value = _value;
}

function setValue(string memory _value) public {
emit ValueChanged(value, _value);
value = _value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"bytecode": "0x608060405234801561001057600080fd5b506040516105f13803806105f18339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b8382019150602082018581111561006957600080fd5b825186600182028301116401000000008211171561008657600080fd5b8083526020830192505050908051906020019080838360005b838110156100ba57808201518184015260208101905061009f565b50505050905090810190601f1680156100e75780820380516001836020036101000a031916815260200191505b50604052505050806000908051906020019061010492919061010b565b50506101b0565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061014c57805160ff191683800117855561017a565b8280016001018555821561017a579182015b8281111561017957825182559160200191906001019061015e565b5b509050610187919061018b565b5090565b6101ad91905b808211156101a9576000816000905550600101610191565b5090565b90565b610432806101bf6000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80633fa4f2451461003b57806393a09352146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610217565b005b60008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561020f5780601f106101e45761010080835404028352916020019161020f565b820191906000526020600020905b8154815290600101906020018083116101f257829003601f168201915b505050505081565b7f239747dab7046e5d3c295d335f2759559f55344dadbfa668569bb8b0f11b5c106000826040518080602001806020018381038352858181546001816001161561010002031660029004815260200191508054600181600116156101000203166002900480156102c85780601f1061029d576101008083540402835291602001916102c8565b820191906000526020600020905b8154815290600101906020018083116102ab57829003601f168201915b5050838103825284818151815260200191508051906020019080838360005b838110156103025780820151818401526020810190506102e7565b50505050905090810190601f16801561032f5780820380516001836020036101000a031916815260200191505b5094505050505060405180910390a18060009080519060200190610354929190610358565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061039957805160ff19168380011785556103c7565b828001600101855582156103c7579182015b828111156103c65782518255916020019190600101906103ab565b5b5090506103d491906103d8565b5090565b6103fa91905b808211156103f65760008160009055506001016103de565b5090565b9056fea265627a7a7231582008f91cf7a703f8030d4ebe4ae80d4aef0a75e295a64703c38f59fa04e9f0280d64736f6c63430005100032",
"abi": [
{
"inputs": [
{ "internalType": "string", "name": "_value", "type": "string" }
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "string",
"name": "oldValue",
"type": "string"
},
{
"indexed": false,
"internalType": "string",
"name": "newValue",
"type": "string"
}
],
"name": "ValueChanged",
"type": "event"
},
{
"constant": false,
"inputs": [
{ "internalType": "string", "name": "_value", "type": "string" }
],
"name": "setValue",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "value",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
}
5 changes: 3 additions & 2 deletions packages/cactus-test-geth-ledger/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"tsBuildInfoFile": "../../.build-cache/cactus-test-geth-ledger.tsbuildinfo"
},
"include": [
"./src"
"./src",
"./src/test/typescript/solidity/simple-token-contract/contractData.json"
],
"references": [
{
Expand All @@ -18,4 +19,4 @@
"path": "../cactus-test-tooling/tsconfig.json"
}
]
}
}

0 comments on commit b58032d

Please sign in to comment.