Skip to content
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
2 changes: 1 addition & 1 deletion parachain/ts-tests/common/setup/register-parathread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '@polkadot/api-augment';
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import { TypeRegistry } from '@polkadot/types/create';
import { Bytes } from '@polkadot/types';
import { loadConfig, signAndSend } from '../utils';
import { loadConfig, signAndSend } from '../utils/index.js';

async function registerParathread(api: ApiPromise, config: any) {
// Get keyring of Alice, who is also the sudo in dev chain spec
Expand Down
2 changes: 1 addition & 1 deletion parachain/ts-tests/common/setup/setup-enclave.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '@polkadot/api-augment';
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import { loadConfig, signAndSend, sudoWrapperGc } from '../utils';
import { loadConfig, signAndSend, sudoWrapperGc } from '../utils/index.js';
import { hexToU8a } from '@polkadot/util';

const mrenclave = process.argv[2];
Expand Down
2 changes: 1 addition & 1 deletion parachain/ts-tests/common/setup/teebag-set-dev-mode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '@polkadot/api-augment';
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import { loadConfig, signAndSend, sudoWrapperGc } from '../utils';
import { loadConfig, signAndSend, sudoWrapperGc } from '../utils/index.js';

async function setAliceAsAdmin(api: ApiPromise, config: any) {
// Get keyring of Alice, who is also the sudo in dev chain spec
Expand Down
2 changes: 1 addition & 1 deletion parachain/ts-tests/common/setup/upgrade-parathread.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@polkadot/api-augment';
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import type { ISubmittableResult } from '@polkadot/types/types';
import { loadConfig } from '../utils';
import { loadConfig } from '../utils/index.js';

// upgrade the parathread to parachain by forcibly leasing a certain period
// can be used to extend the leasing period if it's already in onboarding process
Expand Down
2 changes: 1 addition & 1 deletion parachain/ts-tests/common/setup/wait-finalized-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '@polkadot/api-augment';
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import type { VoidFn } from '@polkadot/api/types';
import type { ISubmittableResult } from '@polkadot/types/types';
import { loadConfig } from '../utils';
import { loadConfig } from '../utils/index.js';

const FINALIZED_BLOCKS_COUNT = 1;
const TIMEOUT_MIN = 5;
Expand Down
23 changes: 19 additions & 4 deletions parachain/ts-tests/common/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { config as dotenvConfig } from 'dotenv';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export function loadConfig() {
require('dotenv').config();
dotenvConfig();

let configPath: string;
switch (process.env.NODE_ENV) {
case 'local':
return require('../../config.local.json');
configPath = join(__dirname, '../../config.local.json');
break;
case 'test':
case 'ci':
return require('../../config.ci.json');
configPath = join(__dirname, '../../config.ci.json');
break;
case 'prod':
return require('../../config.prod.json');
configPath = join(__dirname, '../../config.prod.json');
break;
default:
throw new Error(`Invalid NODE_ENV: ${process.env.NODE_ENV}`);
}

return JSON.parse(readFileSync(configPath, 'utf-8'));
}
6 changes: 3 additions & 3 deletions parachain/ts-tests/common/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './config';
export * from './integration-setup';
export * from './function';
export * from './config.js';
export * from './integration-setup.js';
export * from './function.js';
23 changes: 13 additions & 10 deletions parachain/ts-tests/common/utils/integration-setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'mocha';
import { describe, beforeAll, afterAll } from 'vitest';
import '@polkadot/api-augment';
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import { KeyringPair } from '@polkadot/keyring/types';
import { loadConfig } from './config';
import { loadConfig } from './config.js';

export class ParachainConfig {
api!: ApiPromise;
Expand Down Expand Up @@ -54,10 +54,7 @@ export async function initApiPromise(config: any): Promise<ParachainConfig> {
}

export function describeLitentry(title: string, specFilename: string, cb: (context: ParachainConfig) => void) {
describe(title, function () {
// Set timeout to 6000 seconds (Because of 50-blocks delay of rococo, so called "training wheels")
this.timeout(6000000);

describe(title, () => {
let context: ParachainConfig = {
api: {} as ApiPromise,
parachain: {} as string,
Expand All @@ -66,18 +63,24 @@ export function describeLitentry(title: string, specFilename: string, cb: (conte
eve: {} as KeyringPair,
ferdie: {} as KeyringPair,
};
// Making sure the Litentry node has started
before('Starting Litentry Test Node', async function () {

beforeAll(async () => {
console.log('Starting Litentry Test Node');
const config = loadConfig();
const initApi = await initApiPromise(config);
context.parachain = initApi.parachain;
context.api = initApi.api;
context.alice = initApi.alice;
context.bob = initApi.bob;
context.eve = initApi.eve;
});
context.ferdie = initApi.ferdie;
}, 60000);

after(async function () {});
afterAll(async () => {
if (context.api) {
await context.api.disconnect();
}
});

cb(context);
});
Expand Down
17 changes: 8 additions & 9 deletions parachain/ts-tests/integration-tests/base-filter.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { expect } from 'chai';
import { step } from 'mocha-steps';
import { signAndSend, describeLitentry } from '../common/utils';
import { expect, test } from 'vitest';
import { signAndSend, describeLitentry } from '../common/utils/index.js';

describeLitentry('Test Base Filter', ``, (context) => {
console.log(`Test Base Filter`);

step('Transfer 1000 unit from Eve to Bob', async function () {
test('Transfer 1000 unit from Eve to Bob', async () => {
// Get the initial balance of Eve and Bob
const { nonce: eveInitNonce, data: eveInitBalance } = await context.api.query.system.account(
context.eve.address
Expand All @@ -24,12 +23,12 @@ describeLitentry('Test Base Filter', ``, (context) => {
context.bob.address
);

expect(eveCurrentNonce.toNumber()).to.equal(eveInitNonce.toNumber() + 1);
expect(eveCurrentNonce.toNumber()).toBe(eveInitNonce.toNumber() + 1);
// the balance transfer should work for rococo and litentry
expect(bobCurrentBalance.free.toBigInt()).to.equal(bobInitBalance.free.toBigInt() + BigInt(1000));
expect(bobCurrentBalance.free.toBigInt()).toBe(bobInitBalance.free.toBigInt() + BigInt(1000));
});

step('Transfer 1000 unit from Eve to Bob with Sudo', async function () {
test('Transfer 1000 unit from Eve to Bob with Sudo', async () => {
// only work for rococo
const parachain = (await context.api.rpc.system.chain()).toString().toLowerCase();
if (parachain !== 'rococo-dev') {
Expand Down Expand Up @@ -58,7 +57,7 @@ describeLitentry('Test Base Filter', ``, (context) => {
);

// The transfer should always succeed
expect(aliceCurrentNonce.toNumber()).to.equal(aliceInitNonce.toNumber() + 1);
expect(bobCurrentBalance.free.toBigInt()).to.equal(bobInitBalance.free.toBigInt() + BigInt(1000));
expect(aliceCurrentNonce.toNumber()).toBe(aliceInitNonce.toNumber() + 1);
expect(bobCurrentBalance.free.toBigInt()).toBe(bobInitBalance.free.toBigInt() + BigInt(1000));
});
});
37 changes: 16 additions & 21 deletions parachain/ts-tests/integration-tests/evm-contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assert, expect } from 'chai';
import { step } from 'mocha-steps';
import { signAndSend, describeLitentry, loadConfig, sudoWrapperTc } from '../common/utils';
import { expect, test } from 'vitest';
import { signAndSend, describeLitentry, loadConfig, sudoWrapperTc } from '../common/utils/index.js';
import { compiled } from '../common/utils/compile';
import { evmToAddress } from '@polkadot/util-crypto';
import { hexToU8a, u8aToHex } from '@polkadot/util';
Expand All @@ -16,12 +15,12 @@ describeLitentry('Test EVM Module Contract', ``, (context) => {
mappedAddress: evmToAddress('0xaaafB3972B05630fCceE866eC69CdADd9baC2771', 31),
};

step('Set ExtrinsicFilter mode to Test', async function () {
test('Set ExtrinsicFilter mode to Test', async () => {
let extrinsic = await sudoWrapperTc(context.api, context.api.tx.extrinsicFilter.setMode('Test'));
await signAndSend(extrinsic, context.alice);
});

step('Transfer Value from Eve to EVM external account', async function () {
test('Transfer Value from Eve to EVM external account', async () => {
let eveMappedEVMAccount = context.eve.publicKey.slice(0, 20);
let eveMappedSustrateAccount = evmToAddress(eveMappedEVMAccount, 31);

Expand Down Expand Up @@ -58,13 +57,11 @@ describeLitentry('Test EVM Module Contract', ``, (context) => {
// If a substrate account using pallet_evm to trigger evm transaction,
// it will bump 2 for nonce (one for substrate extrinsic, one for evm).
// +1 nonce for original substrate account, plus another 1 nonce for original substrate account's truncated evm address's mapped susbtrate account.
expect(eveCurrentNonce.toNumber()).to.equal(eveInitNonce.toNumber() + 1);
expect(evmAccountCurrentBalance.free.toBigInt()).to.equal(
evmAccountInitBalance.free.toBigInt() + BigInt(value)
);
expect(eveCurrentNonce.toNumber()).toBe(eveInitNonce.toNumber() + 1);
expect(evmAccountCurrentBalance.free.toBigInt()).toBe(evmAccountInitBalance.free.toBigInt() + BigInt(value));
});

step('Transfer some value back to Eve Mapped account from EVM external account', async function () {
test('Transfer some value back to Eve Mapped account from EVM external account', async () => {
// Get the initial balance of Eve and EVM external account
let eveMappedEVMAccount = context.eve.publicKey.slice(0, 20);
let eveMappedSustrateAccount = evmToAddress(eveMappedEVMAccount, 31);
Expand Down Expand Up @@ -99,11 +96,11 @@ describeLitentry('Test EVM Module Contract', ``, (context) => {

console.log(`evmAccount Balance: ${evmAccountCurrentBalance}`);

expect(evmAccountCurrentNonce.toNumber()).to.equal(evmAccountInitNonce.toNumber() + 1);
expect(eveCurrentBalance.free.toBigInt()).to.equal(eveInitBalance.free.toBigInt() + BigInt(value));
expect(evmAccountCurrentNonce.toNumber()).toBe(evmAccountInitNonce.toNumber() + 1);
expect(eveCurrentBalance.free.toBigInt()).toBe(eveInitBalance.free.toBigInt() + BigInt(value));
});

step('Test substrate signature can not access ultra vires evm/substrate account', async function () {
test('Test substrate signature can not access ultra vires evm/substrate account', async () => {
// Get the initial balance of Eve and EVM external account
const eveInitNonce = (await context.api.query.system.account(context.eve.address)).nonce;
const evmAccountInitBalance = (await context.api.query.system.account(evmAccountRaw.mappedAddress)).data;
Expand All @@ -130,12 +127,12 @@ describeLitentry('Test EVM Module Contract', ``, (context) => {

// Extrinsic succeed with failed origin
// So the evm transaction nonce bump will not be triggered
expect(eveCurrentNonce.toNumber()).to.equal(eveInitNonce.toNumber() + 1);
expect(eveCurrentNonce.toNumber()).toBe(eveInitNonce.toNumber() + 1);
// Which means balance unchanged
expect(evmAccountCurrentBalance.free.toBigInt()).to.equal(evmAccountInitBalance.free.toBigInt());
expect(evmAccountCurrentBalance.free.toBigInt()).toBe(evmAccountInitBalance.free.toBigInt());
});

step('Deploy and test contract by EVM external account', async function () {
test('Deploy and test contract by EVM external account', async () => {
// Get the bytecode and API
const bytecode = compiled.evm.bytecode.object;
const abi = compiled.abi;
Expand Down Expand Up @@ -183,8 +180,7 @@ describeLitentry('Test EVM Module Contract', ``, (context) => {
};

const message = await sayMessage(deployed.contractAddress!);
const initialResult = message === 'Hello World' ? 1 : 0;
assert.equal(1, initialResult, 'Contract initial storage query mismatch');
expect(message).toBe('Hello World');

// Test set message contract method
const setMessage = async (contractAddress: string, accountFrom: any, message: string) => {
Expand All @@ -209,11 +205,10 @@ describeLitentry('Test EVM Module Contract', ``, (context) => {
};
const setMsg = await setMessage(deployed.contractAddress!, evmAccountRaw, 'Goodbye World');
const sayMsg = await sayMessage(deployed.contractAddress!);
const setResult = sayMsg === 'Goodbye World' ? 1 : 0;
assert.equal(1, setResult, 'Contract modified storage query mismatch');
expect(sayMsg).toBe('Goodbye World');
});

step('Set ExtrinsicFilter mode to Normal', async function () {
test('Set ExtrinsicFilter mode to Normal', async () => {
let extrinsic = await sudoWrapperTc(context.api, context.api.tx.extrinsicFilter.setMode('Normal'));
await signAndSend(extrinsic, context.alice);
});
Expand Down
Loading