diff --git a/e2e-tests/package-lock.json b/e2e-tests/package-lock.json index 42c8e380..44de938d 100644 --- a/e2e-tests/package-lock.json +++ b/e2e-tests/package-lock.json @@ -9,9 +9,9 @@ "version": "1.0.0", "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", @@ -21,7 +21,7 @@ "mocha-steps": "^1.3.0", "rimraf": "^5.0.0", "solc": "^0.8.3", - "truffle": "^5.10.2", + "truffle": "^5.11.5", "ts-node": "^10.9.1", "typescript": "^4.9.5", "wait-on": "^7.0.1", diff --git a/e2e-tests/package.json b/e2e-tests/package.json index 9273fdb0..d552e23d 100644 --- a/e2e-tests/package.json +++ b/e2e-tests/package.json @@ -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", diff --git a/e2e-tests/tests/setup.ts b/e2e-tests/tests/setup.ts new file mode 100644 index 00000000..14595f19 --- /dev/null +++ b/e2e-tests/tests/setup.ts @@ -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); + } +}); diff --git a/e2e-tests/tests/test-teleport-laos.ts b/e2e-tests/tests/test-teleport-laos.ts index 8bb7dec8..62cc4f5c 100644 --- a/e2e-tests/tests/test-teleport-laos.ts +++ b/e2e-tests/tests/test-teleport-laos.ts @@ -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, @@ -151,6 +102,5 @@ describeWithExistingNode("Asset Hub (Create Foreign Asset)", (context) => { }).catch((error: any) => { console.log("transaction failed", error); }); - }); }); diff --git a/zombienet/native.toml b/zombienet/native.toml index 6f3a4402..57e19c10 100644 --- a/zombienet/native.toml +++ b/zombienet/native.toml @@ -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