Skip to content

Commit

Permalink
add setup file to e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magecnion committed Oct 18, 2024
1 parent 9e086db commit 8a2c2b0
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 59 deletions.
8 changes: 4 additions & 4 deletions e2e-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"fmt-check": "prettier ./tests --check",
"fmt": "prettier ./tests --write",
"build": "./compile_contracts.sh",
"test": "mocha -r ts-node/register -t 600000 'tests/**/*.ts'",
"test": "mocha --file 'tests/setup.ts' -r ts-node/register -t 600000 'tests/**/*.ts'",
"test-sql": "FRONTIER_BACKEND_TYPE='sql' mocha -r ts-node/register 'tests/**/*.ts'"
},
"author": "",
"license": "ISC",
"dependencies": {
"@polkadot/api": "^12.2.1",
"@polkadot/api-augment": "^12.2.1",
"@polkadot/types": "^12.2.1",
"@polkadot/api": "latest",
"@polkadot/api-augment": "latest",
"@polkadot/types": "latest",
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"chai": "^4.3.7",
Expand Down
132 changes: 132 additions & 0 deletions e2e-tests/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { ApiPromise, HttpProvider, Keyring, WsProvider } from "@polkadot/api";
import { ASSET_HUB_NODE_URL, ASSET_HUB_PARA_ID, LAOS_NODE_URL, LAOS_PARA_ID, RELAYCHAIN_NODE_URL } from "./config";
import { u64 } from "@polkadot/types";
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

const awaitBlockProduction = async (nodeUrl: string) => {
const api = await ApiPromise.create({
provider: new HttpProvider(nodeUrl),
noInitWarn: true,
});

await api.isReady;

let counter = 3;
let blocksProducing = false;
while (!blocksProducing) {
const { number } = await api.rpc.chain.getHeader();

if (number.toNumber() > 0) {
blocksProducing = true;
}
await delay(1000);

counter += 1;
}

await api.disconnect().then(() => {});
};

const awaitEpochChange = async () => {
const apiRelaychain = await ApiPromise.create({
provider: new HttpProvider(RELAYCHAIN_NODE_URL),
noInitWarn: true,
});

await apiRelaychain.isReady;
// Fetch the current epoch index and cast it to u64
const currentEpochIndexCodec = await apiRelaychain.query.babe.epochIndex();
const currentEpochIndex = (currentEpochIndexCodec as u64).toNumber();

let counter = 1;
let changedEpoch = false;

while (!changedEpoch) {
// Fetch the latest epoch index and cast it to u64
const epochIndexCodec = await apiRelaychain.query.babe.epochIndex();
const epochIndex = (epochIndexCodec as u64).toNumber();

// Compare the numerical values
if (epochIndex > currentEpochIndex + 1) {
changedEpoch = true;
}

await delay(1000);
counter += 1;
}

console.log(`Epoch has changed after ${counter} seconds.`);
};

const sendOpenHrmpChannelTxs = async () => {
const apiRelaychain = await ApiPromise.create({
provider: new HttpProvider(RELAYCHAIN_NODE_URL),
noInitWarn: true,
});

await apiRelaychain.isReady;

const maxCapacity = 8;
const maxMessageSize = 1048576;
const keyring = new Keyring({ type: "sr25519" });
const sudo = keyring.addFromUri("//Alice");

const hrmpChannelCalls = [];

hrmpChannelCalls.push(
apiRelaychain.tx.hrmp.forceOpenHrmpChannel(LAOS_PARA_ID, ASSET_HUB_PARA_ID, maxCapacity, maxMessageSize)
);
hrmpChannelCalls.push(
apiRelaychain.tx.hrmp.forceOpenHrmpChannel(ASSET_HUB_PARA_ID, LAOS_PARA_ID, maxCapacity, maxMessageSize)
);
await apiRelaychain.tx.sudo.sudo(apiRelaychain.tx.utility.batchAll(hrmpChannelCalls)).signAndSend(sudo);
};

const openChannel = async (api: ApiPromise, sender: number, recipient: number) => {
console.log("[HRMP] Opening channel between ", sender, " and ", recipient);
const maxCapacity = 8;
const maxMessageSize = 1048576;
const keyring = new Keyring({ type: "sr25519" });
const sudo = keyring.addFromUri("//Alice");

const tx = api.tx.hrmp.forceOpenHrmpChannel(sender, recipient, maxCapacity, maxMessageSize);
api.tx.sudo
.sudo(tx)
.signAndSend(sudo, () => {})
.catch((error: any) => {
console.log("transaction failed", error);
});

while ((await isChannelOpen(api, sender, recipient)) == false) {
console.log("Waiting till channel is open..");
await delay(1000);
}
};

const isChannelOpen = async (api: ApiPromise, sender: number, recipient: number) => {
const channel = await api.query.hrmp.hrmpChannels({
sender,
recipient,
});
return !channel.isEmpty;
};

before(async function () {
console.log("[RELAY_CHAIN] Waiting for block production...");
await awaitBlockProduction(RELAYCHAIN_NODE_URL);
console.log("[ASSET_HUB] Waiting for block production...");
await awaitBlockProduction(ASSET_HUB_NODE_URL);
console.log("[LAOS] Waiting for block production...");
await awaitBlockProduction(LAOS_NODE_URL);
console.log("[HRMP] Check channels..."); // See: https://github.com/paritytech/polkadot-sdk/pull/1616
const apiRelaychain = await ApiPromise.create({
provider: new HttpProvider(RELAYCHAIN_NODE_URL),
noInitWarn: true,
});
if ((await isChannelOpen(apiRelaychain, LAOS_PARA_ID, ASSET_HUB_PARA_ID)) == false) {
await openChannel(apiRelaychain, LAOS_PARA_ID, ASSET_HUB_PARA_ID);
}
if ((await isChannelOpen(apiRelaychain, ASSET_HUB_PARA_ID, LAOS_PARA_ID)) == false) {
await openChannel(apiRelaychain, ASSET_HUB_PARA_ID, LAOS_PARA_ID);
}
});
52 changes: 1 addition & 51 deletions e2e-tests/tests/test-teleport-laos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,7 @@ import {
import { customRequest, describeWithExistingNode } from "./util";
import { Keyring } from "@polkadot/api";


describeWithExistingNode("Asset Hub (Create Foreign Asset)", (context) => {
// See: https://github.com/paritytech/polkadot-sdk/pull/1616
before("Open HRMP channels between LAOS and AssetHub", async function () {
// TODO wait till channel is not openned, ex: https://github.com/paritytech/asset-transfer-api/blob/main/scripts/testNetworkForeignAssets.ts#L213
const apiRelaychain = await context.networks.relaychain;
const maxCapacity = 8;
const maxMessageSize = 1048576;
const keyring = new Keyring({ type: "sr25519" });
const sudo = keyring.addFromUri("//Alice");

const laosToAssetHubChannel = await context.networks.relaychain.query.hrmp.hrmpChannels({
sender: LAOS_PARA_ID,
recipient: ASSET_HUB_PARA_ID,
});
if (laosToAssetHubChannel.isEmpty) {
const tx = apiRelaychain.tx.hrmp.forceOpenHrmpChannel(
LAOS_PARA_ID,
ASSET_HUB_PARA_ID,
maxCapacity,
maxMessageSize
);
apiRelaychain.tx.sudo
.sudo(tx)
.signAndSend(sudo, () => {})
.catch((error: any) => {
console.log("transaction failed", error);
});
}

const assetHubToLaosChannel = await context.networks.relaychain.query.hrmp.hrmpChannels({
sender: ASSET_HUB_PARA_ID,
recipient: LAOS_PARA_ID,
});
if (assetHubToLaosChannel.isEmpty) {
const tx = apiRelaychain.tx.hrmp.forceOpenHrmpChannel(
ASSET_HUB_PARA_ID,
LAOS_PARA_ID,
maxCapacity,
maxMessageSize
);
apiRelaychain.tx.sudo
.sudo(tx)
.signAndSend(sudo, () => {})
.catch((error: any) => {
console.log("transaction failed", error);
});
}
});

describeWithExistingNode("Teleport Asset Hub <-> LAOS", (context) => {
step("HRMP channels between Asset Hub and LAOS are open", async function () {
const laosToAssetHubChannel = await context.networks.relaychain.query.hrmp.hrmpChannels({
sender: LAOS_PARA_ID,
Expand Down Expand Up @@ -151,6 +102,5 @@ describeWithExistingNode("Asset Hub (Create Foreign Asset)", (context) => {
}).catch((error: any) => {
console.log("transaction failed", error);
});

});
});
2 changes: 2 additions & 0 deletions zombienet/native.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ force_decorator = "generic-evm"
name = "laos1"
command = "{{ZOMBIENET_LAOS_COMMAND}}"
validator = true
args = ["--log=xcm=trace"]

[[parachains.collators]]
name = "laos2"
command = "{{ZOMBIENET_LAOS_COMMAND}}"
validator = true
args = ["--log=xcm=trace"]

[[parachains]]
id = 1000
Expand Down

0 comments on commit 8a2c2b0

Please sign in to comment.