Skip to content

Commit

Permalink
[master] ZIL-5488: acceptance test fixes (Zilliqa#3889)
Browse files Browse the repository at this point in the history
* (feat) Define network in environment variables
(fix)  Transfer enough from a zil account to account for gas fees on the called contract.
(fix)  Set the default wallet address in Transfer.ts so we don't end up accidentally using a different one.

* (fix) Write the filename we're expecting when CHAIN_NAME is set. Grotty, but what about this isn't?

* (fix) Disable tests that won't pass anyway.

* (fix) ZIL-5488: Fix whitespace. Swearing elided.
(fix) ZIL-5488: Remove spurious console.log

* (feat) ZIL-5488: Add docs on env var use to README
  • Loading branch information
rrw-zilliqa authored Dec 11, 2023
1 parent 54cdc84 commit 0e45d5d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
16 changes: 16 additions & 0 deletions tests/EvmAcceptanceTests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
MOCHA_REPORTER=./BriefMochaReporter.ts npx hardhat test # To use brief reporter.
```

# Running tests from env vars

You can run tests from a chain specified via environment variables (this is used by `z blockchain perf`) - eg:

```
export CHAIN_URL=http://localhost:12003
export CHAIN_WEBSOCKET_URL=http://localhost:12003
export CHAIN_NAME="zbconnect2"
export CHAIN_ID=700
npm i
npx hardhat init-signers --from [privkey] --from-address-type zil --count 30 --balance 1000 --network from_env
npx hardhat test --parallel --network from_env
```

# Start running tests

If it's the first time you want to run tests, or you just created [a new network](#how-to-define-a-new-network-for-hardhat) and you want to run tests against this newly added network, you're supposed to call `setup`:
Expand Down
12 changes: 12 additions & 0 deletions tests/EvmAcceptanceTests/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@ declare module "hardhat/types/config" {
}
}

// The ?? "" s below are simply because hardhat requires a string even if the
// variable is undefined - rrw 2023-12-11
const config: HardhatUserConfig = {
solidity: "0.8.9",
defaultNetwork: "isolated_server",

networks: {
from_env: {
url: process.env.CHAIN_URL ?? "",
websocketUrl: process.env.CHAIN_WEBSOCKET_URL ?? "",
accounts: [...loadFromSignersFile(process.env.CHAIN_NAME)],
chainId: process.env.CHAIN_ID | 0x8000,
zilliqaNetwork: true,
web3ClientVersion: "Zilliqa/v8.2",
protocolVersion: 0x41,
miningState: false
},
public_devnet: {
url: "https://api.devnet.zilliqa.com",
websocketUrl: "ws://api.devnet.zilliqa.com",
Expand Down
16 changes: 8 additions & 8 deletions tests/EvmAcceptanceTests/helpers/parallel-tests/SignerPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export default class SignerPool {
}

function shuffleArray(array: any[]) {
for (let i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
for (let i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}

return array;
}
return array;
}
15 changes: 10 additions & 5 deletions tests/EvmAcceptanceTests/tasks/InitSigners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ task("init-signers", "A task to init signers")
)
.addParam("count", "Number of signers to be generated")
.addParam("balance", "Balance of each newly generated signers")
.addOptionalParam("fileName", "Output file name")
.addFlag("append", "Append new signers to the end of the .signer-<network> file")
.setAction(async (taskArgs, hre) => {
const {from, fromAddressType, count, balance, append} = taskArgs;
const {from, fromAddressType, count, balance, append, fileName} = taskArgs;

const spinner = ora();
spinner.start(`Creating ${count} accounts...`);
Expand All @@ -36,17 +37,18 @@ task("init-signers", "A task to init signers")

spinner.succeed();

const file_name = `${hre.network.name}.json`;
const outputFileStem = fileName ?? process.env.CHAIN_NAME ?? `${hre.network.name}`;
const outputFileName = `${outputFileStem}.json`;

try {
await writeToFile(
accounts.map((account) => account.privateKey),
append,
file_name
outputFileName
);
console.log();
console.log(
clc.bold(`.signers/${file_name}`),
clc.bold(`.signers/${outputFileName}`),
clc.blackBright(`${append ? "updated" : "created"} successfully.`)
);
} catch (error) {
Expand Down Expand Up @@ -98,10 +100,13 @@ const createAccountsZil = async (
amount: BigNumber,
count: number
) => {
// Add +amount for the source account itself, add +100 for transaction costs.
let gasSupply = BigNumber.from(100).mul(ethers.constants.WeiPerEther);
let amountToMove = ethers.utils.formatEther(amount.mul(count * 2).add(gasSupply));
await hre.run("transfer", {
from: privateKey,
to: getEthAddress(privateKey),
amount: ethers.utils.formatEther(amount.mul(count * 2)), // Add +amount for the source account itself
amount: amountToMove,
fromAddressType: "zil"
});

Expand Down
4 changes: 3 additions & 1 deletion tests/EvmAcceptanceTests/tasks/Transfer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {BN, Zilliqa, bytes, toChecksumAddress, units} from "@zilliqa-js/zilliqa";
import * as zilliqaCrypto from "@zilliqa-js/crypto";
import clc from "cli-color";
import {ethers} from "ethers";
import {task} from "hardhat/config";
Expand Down Expand Up @@ -46,11 +47,12 @@ const fundZil = async (hre: HardhatRuntimeEnvironment, privateKey: string, to: s
let zilliqa = new Zilliqa(hre.getNetworkUrl());
const msgVersion = 1; // current msgVersion
const VERSION = bytes.pack(hre.getZilliqaChainId(), msgVersion);
let fromAddress = zilliqaCrypto.getAddressFromPrivateKey(privateKey);
zilliqa.wallet.addByPrivateKey(privateKey);
zilliqa.wallet.setDefault(fromAddress);
const ethAddrConverted = toChecksumAddress(to); // Zil checksum
const balance = await getZilBalance(hre, to);
console.log(`Current balance: ${clc.yellow.bold(balance)}`);

const tx = await zilliqa.blockchain.createTransactionWithoutConfirm(
zilliqa.transactions.new(
{
Expand Down
2 changes: 1 addition & 1 deletion tests/EvmAcceptanceTests/test/EventsLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("Events and logs #parallel", function () {
expect(receipt.logs.length).to.be.eq(1);
});

it("Should return 2 logs whenever a function with two events is called @block-1", async function () {
it("Should return 2 logs whenever a function with two events is called @block-2", async function () {
const tx = await contract.two_logs();
const receipt = await ethers.provider.getTransactionReceipt(tx.hash);
expect(receipt.logs.length).to.be.eq(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("Openzeppelin ownable contract functionality #parallel", function () {

it("should return the deployer as the owner @block-1", async function () {
const owner = contract.signer as SignerWithAddress;
console.log(`Owner is ${owner.address}`);
expect(await contract.owner()).to.be.equal(owner.address);
});

Expand All @@ -24,6 +25,8 @@ describe("Openzeppelin ownable contract functionality #parallel", function () {
it("should not be possible to call a restricted function using an arbitrary account @block-1", async function () {
const notOwner = hre.allocateEthSigner();

console.log(`NotOwner is ${notOwner.address}`);

await expect(contract.connect(notOwner).store(123)).to.be.revertedWith("Ownable: caller is not the owner");

hre.releaseEthSigner(notOwner);
Expand Down

0 comments on commit 0e45d5d

Please sign in to comment.