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

feat(zombienet): Add javascript tests to zombienet testing #3200

Merged
merged 7 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ func (s *Service) Start() error {
logger.Info("started network service with supported protocols " + strings.Join(s.host.protocols(), ", "))

if s.Metrics.Publish {
// TODO(ed) remove before commit
edwardmack marked this conversation as resolved.
Show resolved Hide resolved
processStartTimeGauge.Set(float64(time.Now().Unix()))
go s.updateMetrics()
}
Expand Down
1 change: 1 addition & 0 deletions dot/rpc/modules/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ func (sm *StateModule) QueryStorage(

// QueryStorageAt queries historical storage entries (by key) at the block hash given or
// the best block if the given block hash is nil
// TODO(ed): remove before merge into development after PR #3191 has been merged
edwardmack marked this conversation as resolved.
Show resolved Hide resolved
func (sm *StateModule) QueryStorageAt(
_ *http.Request, request *StateStorageQueryAtRequest, response *[]StorageChangeSetResponse) error {
atBlockHash := request.At
Expand Down
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);
});
}