Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add evm tests for ctx state after chain upgrade #277

Merged
merged 3 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ts-tests/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EVM_CTX_DEPLOYER_KEY = xxx
1 change: 1 addition & 0 deletions ts-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/.env
5 changes: 0 additions & 5 deletions ts-tests/.mocharc.json

This file was deleted.

24 changes: 23 additions & 1 deletion ts-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cargo build --release --no-default-features --features manual-seal,rpc_binary_se
npm install
```

## Run the tests
## Run automated tests

```bash
npm run build && npm run test
Expand All @@ -34,3 +34,25 @@ ICE_LOG="warn,rpc=trace" npm run test
```

(The Ice node be listening for RPC on port 19933, mostly to avoid conflict with already running substrate node)

## Run tests for contracts state
Ensure that the deployed contract state is intact.
```bash
npm run test-ctx-state <chain>
```
*where `chain` could be one of the following:*
- *snow*
- *arctic*
- *snow_staging*
- *local*

`The contract should be readily available on SNOW/Arctic/Staging Network.`
*<b>Optionally:</b>* If you save your `private key` in `.env` file, you can deploy the contract to SNOW/Arctic:
```bash
npm run deploy-upgrade-ctx <chain>
```
*where `chain` could be one of the following:*
- *snow*
- *arctic*
- *snow_staging*
- *local*
76 changes: 76 additions & 0 deletions ts-tests/contracts/NetworkUpgrade.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract NetworkUpgrade {

uint256 number;
string message;
bytes message_bytes;
address addr;
mapping(string => uint16) chain_id;
uint256[] num_array;
string[4] fixed_str_array = ["ICE", "SNOW", "Arctic", "Frost"];

enum Days
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Days d;

sharma66mahesh marked this conversation as resolved.
Show resolved Hide resolved
struct TS {
int8 num1;
bytes1 bt;
bool b1;
address addr;
string name;
}
TS simple_struct;

struct BS {
bytes1 one_char;
bytes3 three_char;
bytes4 four_char;
bytes16 sixteen_char;
bytes32 thirtytwo_char;
}
BS bytes_struct;

struct Contract_state {
uint256 number;
string message;
uint16[2] chain_id;

uint256[] num_array;
string[4] fixed_str_array;
address addr;
Days d;
TS simple_struct;
BS bytes_struct;
bytes message_bytes;
}

constructor(uint256 _num, string memory _msg) {
number = _num;
message = _msg;
num_array.push(_num);
num_array.push(_num * 2);
addr = 0x8eFcaF2C4eBbf88Bf07f3BB44a2869C4C675AD7A;
d = Days.Sunday;
bytes_struct = BS("a", "123", "a1b2", "!@#$%^&*()123456", "abcdefghijklmnopqrstuvwxyz123456");
simple_struct = TS(127, "1", true, 0x8eFcaF2C4eBbf88Bf07f3BB44a2869C4C675AD7A, "SNOW");
message_bytes = bytes(_msg);
chain_id["snow"] = 552;
chain_id["arctic"] = 553;
}

function get() public view returns(Contract_state memory) {
return Contract_state(number, message, [chain_id["snow"], chain_id["arctic"]], num_array, fixed_str_array, addr, d, simple_struct, bytes_struct, message_bytes);
}
}
10 changes: 8 additions & 2 deletions ts-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"fmt-check": "prettier ./tests --check",
"fmt": "prettier ./tests --write",
"build": "truffle compile",
"test": "mocha -r ts-node/register 'tests/**/test-*.{js,ts}'"
"test": "mocha -r ts-node/register 'tests/test-*.{js,ts}'",
"deploy-upgrade-ctx": "node -r ts-node/register -r dotenv/config tests/networkUpgradeTests/deploy-upgrade-ctx.ts --chain $1",
"test-ctx-state": "mocha -r ts-node/register tests/networkUpgradeTests/test-ctx-state.ts --chain $1"
},
"author": "",
"license": "ISC",
Expand All @@ -16,6 +18,7 @@
"@types/mocha": "^8.0.0",
"chai": "^4.3.7",
"chai-as-promised": "^7.1.1",
"dotenv": "^16.0.3",
"ethereum-waffle": "^3.4.4",
"ethers": "^5.4.6",
"mocha": "^8.0.1",
Expand All @@ -24,10 +27,13 @@
"truffle": "^5.1.62",
"ts-node": "^8.10.2",
"typescript": "^3.9.6",
"web3": "^1.8.0-rc.0"
"web3": "^1.8.0-rc.0",
"yargs": "^17.6.2"
},
"devDependencies": {
"@types/chai-as-promised": "^7.1.5",
"@types/node": "^18.13.0",
"@types/yargs": "^17.0.22",
"prettier": "^2.6.2"
}
}
23 changes: 23 additions & 0 deletions ts-tests/tests/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,26 @@ export const BLOCK_TIMESTAMP = 6; // 6 seconds per block
export const BLOCK_HASH_COUNT = 256;
export const EXISTENTIAL_DEPOSIT = 10_000_000_000_000_000; // The minimum amount required to keep an account open
export const BLOCK_GAS_LIMIT = 60000000;

export const CHAINS = {
snow: {
RPC_ENDPOINT: "https://snow-rpc.icenetwork.io:9933",
CHAIN_ID: 552,
UPGRADE_CTX_ADDRESS: "0x3f71f31caf936b5b95fe1f207498fe798bd7723b",
},
arctic: {
RPC_ENDPOINT: "https://arctic-rpc.icenetwork.io:9933",
CHAIN_ID: 553,
UPGRADE_CTX_ADDRESS: "0x41897dad572342942d8e86572dd579cf3da16526",
},
snow_staging: {
RPC_ENDPOINT: "https://snow-staging-rpc.web3labs.com:9933",
CHAIN_ID: 552,
UPGRADE_CTX_ADDRESS: "0x5c1c61cb2d66bb77ae2df436cb5e3f92db065a3c",
},
local: {
RPC_ENDPOINT: "https://localhost:9933",
CHAIN_ID: 554,
UPGRADE_CTX_ADDRESS: "", // Update according to local env
},
}
25 changes: 25 additions & 0 deletions ts-tests/tests/networkUpgradeTests/deploy-upgrade-ctx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Contract, ContractFactory, Signer, Wallet } from "ethers";
import dotenv from "dotenv";

import UpgradeCtx from "../../build/contracts/NetworkUpgrade.json";
import { parseChainFromArgs, getEthersProvider } from "./helpers";

dotenv.config();
const EVM_CTX_DEPLOYER_KEY = process.env["EVM_CTX_DEPLOYER_KEY"];
const chain = parseChainFromArgs(process.argv);

async function deployContract() {
let deployer: Signer = new Wallet(EVM_CTX_DEPLOYER_KEY, getEthersProvider(chain));
let factory = new ContractFactory(UpgradeCtx.abi, UpgradeCtx.bytecode, deployer);

console.log(`Deploying contract to: ${chain}`);
console.log("Deployer wallet: ", await deployer.getAddress());

const contract: Contract = await factory.deploy(20, "SNOW Network");
await contract.deployed(); //wait until deployed

console.log("Contract address: ", contract.address);
console.log("\nSuccessfully deployed contract");
}

deployContract();
21 changes: 21 additions & 0 deletions ts-tests/tests/networkUpgradeTests/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import yargs from "yargs/yargs";
import { ethers } from "ethers";
import { CHAINS } from "../config";

export function parseChainFromArgs(args: typeof process.argv) {
const chainName = yargs(args.slice(2)).parseSync().chain;
// @ts-ignore
if(!Object.keys(CHAINS).includes(chainName)) {
throw new Error("Error: Supported chains are 'snow', 'arctic', 'snow_staging' and 'local'.")
}
// @ts-ignore
const CHAIN: keyof typeof CHAINS = chainName;
return CHAIN;
}

export function getEthersProvider(chain: keyof typeof CHAINS) {
return new ethers.providers.StaticJsonRpcProvider(CHAINS[chain].RPC_ENDPOINT, {
chainId: CHAINS[chain].CHAIN_ID,
name: chain,
});
}
30 changes: 30 additions & 0 deletions ts-tests/tests/networkUpgradeTests/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BigNumber } from "ethers";

export interface ContractResponse {
number: BigNumber;
message: String;
testStruct: {
num: BigNumber;
message: String;
};
num_array: BigNumber[];
fixed_str_array: String[];
chain_id: BigNumber[];
addr: string;
d: number;
simple_struct: {
num1: number;
bt: string;
b1: boolean;
addr: string;
name: string;
}
message_bytes: string;
bytes_struct: {
one_char: string;
three_char: string;
four_char: string;
sixteen_char: string;
thirtytwo_char: string;
}
}
67 changes: 67 additions & 0 deletions ts-tests/tests/networkUpgradeTests/test-ctx-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Contract, BigNumber } from "ethers";
import { expect } from "chai";
import { step } from "mocha-steps";

import { ContractResponse } from "./interfaces";
import { getEthersProvider, parseChainFromArgs } from "./helpers";
import UpgradeCtx from "../../build/contracts/NetworkUpgrade.json";
import { CHAINS } from "../config";

describe("Tests for checking existing contracts storage", () => {
step("Ensure the contract state is intact", async function (done) {
try {
this.timeout(10_000);
const chainName = parseChainFromArgs(process.argv);

const contract: Contract = new Contract(
CHAINS[chainName].UPGRADE_CTX_ADDRESS,
UpgradeCtx.abi,
getEthersProvider(chainName)
);

const {
number,
message,
num_array,
fixed_str_array,
chain_id,
addr,
d,
simple_struct,
bytes_struct,
message_bytes,
}: ContractResponse = await contract.get();

expect(number.toNumber(), "Returned an invalid number").to.equal(20);
expect(message, "Returned an invalid string").to.equal("SNOW Network");
expect(num_array, "Returned an invalid uint256 array").to.eql([BigNumber.from(20), BigNumber.from(40)]);
expect(chain_id, "Returned an invalid mapping array").to.eql([552, 553]);
expect(fixed_str_array, "Returned an invalid string array").to.eql(["ICE", "SNOW", "Arctic", "Frost"]);
expect(addr, "Returned an invalid H160 address").to.equal("0x8eFcaF2C4eBbf88Bf07f3BB44a2869C4C675AD7A");
expect(d, "Returned an invalid enum").to.equal(6);
expect(message_bytes, "Returned an invalid bytes").to.equal("0x534e4f57204e6574776f726b");

expect(bytes_struct.one_char, "Returned an invalid bytes struct").to.equal("0x61");
expect(bytes_struct.three_char, "Returned an invalid bytes struct").to.equal("0x313233");
expect(bytes_struct.four_char, "Returned an invalid bytes struct").to.equal("0x61316232");
expect(bytes_struct.sixteen_char, "Returned an invalid bytes struct").to.equal(
"0x21402324255e262a2829313233343536"
);
expect(bytes_struct.thirtytwo_char, "Returned an invalid bytes struct").to.equal(
"0x6162636465666768696a6b6c6d6e6f707172737475767778797a313233343536"
);

expect(simple_struct.num1, "Returned an invalid simple struct").to.equal(127);
expect(simple_struct.bt, "Returned an invalid simple struct").to.equal("0x31");
expect(simple_struct.b1, "Returned an invalid simple struct").to.equal(true);
expect(simple_struct.addr, "Returned an invalid simple struct").to.equal(
"0x8eFcaF2C4eBbf88Bf07f3BB44a2869C4C675AD7A"
);
expect(simple_struct.name, "Returned an invalid simple struct").to.equal("SNOW");

done();
} catch (e) {
done(e);
}
});
});
10 changes: 5 additions & 5 deletions ts-tests/tests/test-assets-evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describeWithIce("Ice RPC (AssetsERC20)", (context) => {
const tx = await erc20.transfer(receiver, "10");
await tx.wait();
const balanceOf = await erc20.balanceOf(receiver);
expect(balanceOf.toString()).to.equal((Number.parseInt(prevBalance)+10).toString());
expect(balanceOf.toString()).to.equal((Number.parseInt(prevBalance) + 10).toString());
});
// erc20Plus

Expand All @@ -65,18 +65,18 @@ describeWithIce("Ice RPC (AssetsERC20)", (context) => {
it("should burn token", async function () {
this.timeout(15000);
const prevSupply = await erc20Plus.totalSupply();
const tx= await erc20Plus.burn(genesisAccount.getAddress(),"1", {gasLimit: 5000000});
const tx = await erc20Plus.burn(genesisAccount.getAddress(), "1", { gasLimit: 5000000 });
await tx.wait();
const totalSupply = await erc20Plus.totalSupply();
expect(totalSupply.toString()).to.equal((Number.parseInt(prevSupply)-1).toString());
expect(totalSupply.toString()).to.equal((Number.parseInt(prevSupply) - 1).toString());
});

it("should mint token", async function () {
this.timeout(15000);
const prevSupply = await erc20Plus.totalSupply();
const tx= await erc20Plus.mint(genesisAccount.getAddress(),"1", {gasLimit: 5000000});
const tx = await erc20Plus.mint(genesisAccount.getAddress(), "1", { gasLimit: 5000000 });
await tx.wait();
const totalSupply = await erc20Plus.totalSupply();
expect(totalSupply.toString()).to.equal((Number.parseInt(prevSupply)+1).toString());
expect(totalSupply.toString()).to.equal((Number.parseInt(prevSupply) + 1).toString());
});
});
4 changes: 3 additions & 1 deletion ts-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"compilerOptions": {
"esModuleInterop": true,
"resolveJsonModule": true
"moduleResolution": "node",
"resolveJsonModule": true,
"target": "ES2020"
}
}
Loading