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 <tomasz.awramski@fujitsu.com>
  • Loading branch information
rwat17 committed Aug 1, 2023
1 parent ab50253 commit 6ef8f7f
Show file tree
Hide file tree
Showing 6 changed files with 585 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,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 @@ -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
@@ -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"
}
]
}
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 6ef8f7f

Please sign in to comment.