Skip to content

Commit

Permalink
feat(zombienet): Add javascript tests to zombienet testing (#3200)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardmack committed May 17, 2023
1 parent db0be3f commit aca9a5b
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
22 changes: 22 additions & 0 deletions zombienet_tests/functional/0002-basic-network.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[relaychain]
chain_spec_path = "chain/westend-local/westend-local-spec-raw.json"

chain = "westend-local"

[[relaychain.nodes]]
name = "alice"
command = "gossamer"
validator = true
args = ["--key alice"]

[[relaychain.nodes]]
name = "bob"
command = "gossamer"
validator = true
args = ["--key bob"]

[[relaychain.nodes]]
name = "charlie"
command = "gossamer"
validator = true
args = ["--key charlie"]
25 changes: 25 additions & 0 deletions zombienet_tests/functional/0002-basic-network.zndsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Description: Small Network test
Network: ./0002-basic-network.toml
Creds: config

# well know functions
alice: is up
bob: is up

{% set nodes = ["alice", "bob", "charlie"] %}

{% set nodeAddresses = ["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty", "5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y"] %}

# Check nodes are up, synced and have expected beginning balances
{% for node in nodes %}
{{node}}: is up
{{node}}: reports gossamer_network_syncer_is_synced is 1 within 30 seconds

{% for address in nodeAddresses %}
{{node}}: js-script ./scripts/free-balance.js with "{{address}}" return is equal to 1000000000000000000
{% endfor %}
{% endfor %}

# Test transfer from Alice to Bob, NOTE: this is currently failing because nodes are not finalizing blocks
alice: js-script ./scripts/transfer-tokens.js with "Alice,Bob" return is equal to 1 within 200 seconds
15 changes: 15 additions & 0 deletions zombienet_tests/functional/scripts/free-balance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright 2023 ChainSafe Systems (ON)
* SPDX-License-Identifier: LGPL-3.0-only
*/

async function run(nodeName, networkInfo, args) {
const {wsUri, userDefinedTypes} = networkInfo.nodesByName[nodeName];
const api = await zombie.connect(wsUri, userDefinedTypes);

const {nonce, data: balance} = await api.query.system.account(args[0]);

return balance.free;
}

module.exports = { run }
36 changes: 36 additions & 0 deletions zombienet_tests/functional/scripts/transfer-tokens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2023 ChainSafe Systems (ON)
* SPDX-License-Identifier: LGPL-3.0-only
*/

async function run(nodeName, networkInfo, args) {
const { sendTransaction } = await import("./tx-utils.mjs");
const {wsUri, userDefinedTypes} = networkInfo.nodesByName[nodeName];
const api = await zombie.connect(wsUri, userDefinedTypes);

await zombie.util.cryptoWaitReady();

// account to submit tx
const keyring = new zombie.Keyring({ type: "sr25519" });
const FROM = keyring.addFromUri("//" + args[0]);
const TO = keyring.addFromUri("//" + args[1]);
const AMOUNT = 1000000000000000;

const originalBalance = await api.query.system.account(TO.address);
console.log('originalBalance', originalBalance.toString());

await sendTransaction(
api.tx.balances.transfer({ Id: TO.address }, AMOUNT),
FROM
);

const newBalance = await api.query.system.account(TO.address);
console.log('newBalance', newBalance.toString());

const difference = newBalance.data.free - originalBalance.data.free
const result = difference === AMOUNT ? 1 : 0

return result;
}

module.exports = { run }
34 changes: 34 additions & 0 deletions zombienet_tests/functional/scripts/tx-utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2023 ChainSafe Systems (ON)
* SPDX-License-Identifier: LGPL-3.0-only
*/

export async function sendTransaction(transaction, sender) {
return new Promise((resolve, reject) => {
let unsubscribe;
let timeout;
const SPAWNING_TIME = 500000;

transaction.signAndSend(sender, async (result) => {
console.log(`Current status is ${result?.status}`);

if (result.isFinalized) {
if (unsubscribe) {
unsubscribe();
}

clearTimeout(timeout);
resolve(true);
}
}).then(unsub => {
unsubscribe = unsub;
}).catch(error => {
console.error(error);
reject(error);
});

timeout = setTimeout(() => {
reject(new Error('Transaction timeout'));
}, SPAWNING_TIME);
});
}

0 comments on commit aca9a5b

Please sign in to comment.