Skip to content

Commit af6b1d5

Browse files
svyatonikbkontur
authored andcommitted
Add basic zombienet test to be used in the future (#2649)
* add basic zombienet test to be used in the future * removed unneeded variables * add README.md for zombienet folder
1 parent 528dc11 commit af6b1d5

10 files changed

+281
-0
lines changed

zombienet/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### Bridges Tests for Local Rococo <> Wococo Bridge
2+
3+
This folder contains [zombienet](https://github.com/paritytech/zombienet/) based integration tests for both
4+
onchain and offchain bridges code. Due to some
5+
[technical diffuculties](https://github.com/paritytech/parity-bridges-common/pull/2649#issue-1965339051), we
6+
are using native zombienet provider, which means that you need to build some binaries locally.
7+
8+
To start those tests, you need to:
9+
10+
- download latest [zombienet release](https://github.com/paritytech/zombienet/releases);
11+
12+
- build polkadot binary by running `cargo build -p polkadot --release` command in the
13+
[`polkadot-sdk`](https://github.com/paritytech/polkadot-sdk) repository clone;
14+
15+
- build polkadot binary by running `cargo build -p polkadot-parachain-bin --release` command in the
16+
[`polkadot-sdk`](https://github.com/paritytech/polkadot-sdk) repository clone;
17+
18+
- ensure that you have [`node`](https://nodejs.org/en) installed. Additionally, we'll need globally installed
19+
`polkadot/api-cli` package (use `npm install -g @polkadot/api-cli@beta` to install it);
20+
21+
- build substrate relay by running `cargo build -p substrate-relay --release` command in the
22+
[`parity-bridges-common`](https://github.com/paritytech/parity-bridges-common) repository clone.
23+
24+
- copy fresh `substrate-relay` binary, built in previous point, to the `~/local_bridge_testing/bin/substrate-relay`;
25+
26+
- change the `POLKADOT_SDK_FOLDER` and `ZOMBIENET_BINARY_PATH` (and ensure that the nearby variables
27+
have correct values) in the `./run-tests.sh`.
28+
29+
After that, you could run tests with the `./run-tests.sh` command. Hopefully, it'll show the
30+
"All tests have completed successfully" message in the end. Otherwise, it'll print paths to zombienet
31+
process logs, which, in turn, may be used to track locations of all spinned relay and parachain nodes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
async function run(nodeName, networkInfo, args) {
2+
const {wsUri, userDefinedTypes} = networkInfo.nodesByName[nodeName];
3+
const api = await zombie.connect(wsUri, userDefinedTypes);
4+
5+
// TODO: could be replaced with https://github.com/polkadot-js/api/issues/4930 (depends on metadata v15) later
6+
const bridgedChainName = args[0];
7+
const expectedBridgedChainHeaderNumber = Number(args[1]);
8+
const runtimeApiMethod = bridgedChainName + "FinalityApi_best_finalized";
9+
10+
while (true) {
11+
const encodedBestFinalizedHeaderId = await api.rpc.state.call(runtimeApiMethod, []);
12+
const bestFinalizedHeaderId = api.createType("Option<BpRuntimeHeaderId>", encodedBestFinalizedHeaderId);
13+
if (bestFinalizedHeaderId.isSome) {
14+
const bestFinalizedHeaderNumber = Number(bestFinalizedHeaderId.unwrap().toHuman()[0]);
15+
if (bestFinalizedHeaderNumber > expectedBridgedChainHeaderNumber) {
16+
return bestFinalizedHeaderNumber;
17+
}
18+
}
19+
20+
// else sleep and retry
21+
await new Promise((resolve) => setTimeout(resolve, 12000));
22+
}
23+
}
24+
25+
module.exports = { run }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
async function run(nodeName, networkInfo, args) {
2+
const {wsUri, userDefinedTypes} = networkInfo.nodesByName[nodeName];
3+
const api = await zombie.connect(wsUri, userDefinedTypes);
4+
5+
// TODO: could be replaced with https://github.com/polkadot-js/api/issues/4930 (depends on metadata v15) later
6+
const relayerAccountAddress = args[0];
7+
const laneId = args[1];
8+
const bridgedChainId = args[2];
9+
const relayerFundOwner = args[3];
10+
const expectedRelayerReward = BigInt(args[4]);
11+
while (true) {
12+
const relayerReward = await api.query.bridgeRelayers.relayerRewards(
13+
relayerAccountAddress,
14+
{ laneId: laneId, bridgedChainId: bridgedChainId, owner: relayerFundOwner }
15+
);
16+
if (relayerReward.isSome) {
17+
const relayerRewardBalance = relayerReward.unwrap().toBigInt();
18+
if (relayerRewardBalance > expectedRelayerReward) {
19+
return relayerRewardBalance;
20+
}
21+
}
22+
23+
// else sleep and retry
24+
await new Promise((resolve) => setTimeout(resolve, 12000));
25+
}
26+
}
27+
28+
module.exports = { run }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
async function run(nodeName, networkInfo, args) {
2+
const {wsUri, userDefinedTypes} = networkInfo.nodesByName[nodeName];
3+
const api = await zombie.connect(wsUri, userDefinedTypes);
4+
5+
// TODO: could be replaced with https://github.com/polkadot-js/api/issues/4930 (depends on metadata v15) later
6+
const accountAddress = args[0];
7+
const expectedForeignAssetBalance = BigInt(args[1]);
8+
const bridgedNetworkName = args[2];
9+
while (true) {
10+
const foreignAssetAccount = await api.query.foreignAssets.account(
11+
{ parents: 2, interior: { X1: { GlobalConsensus: bridgedNetworkName } } },
12+
accountAddress
13+
);
14+
if (foreignAssetAccount.isSome) {
15+
const foreignAssetAccountBalance = foreignAssetAccount.unwrap().balance.toBigInt();
16+
if (foreignAssetAccountBalance > expectedForeignAssetBalance) {
17+
return foreignAssetAccountBalance;
18+
}
19+
}
20+
21+
// else sleep and retry
22+
await new Promise((resolve) => setTimeout(resolve, 12000));
23+
}
24+
}
25+
26+
module.exports = { run }

zombienet/run-tests.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash
2+
#set -eu
3+
shopt -s nullglob
4+
5+
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
6+
7+
# assuming that we'll be using native provide && all processes will be executing locally
8+
# (we need absolute paths here, because they're used when scripts are called by zombienet from tmp folders)
9+
export POLKADOT_SDK_FOLDER=`realpath $(dirname "$0")/../..`
10+
export BRIDGE_TESTS_FOLDER=$POLKADOT_SDK_FOLDER/bridges/zombienet/tests
11+
export POLKADOT_BINARY_PATH=$POLKADOT_SDK_FOLDER/target/release/polkadot
12+
export POLKADOT_PARACHAIN_BINARY_PATH=$POLKADOT_SDK_FOLDER/target/release/polkadot-parachain
13+
export POLKADOT_PARACHAIN_BINARY_PATH_FOR_ASSET_HUB_ROCOCO=$POLKADOT_PARACHAIN_BINARY_PATH
14+
export POLKADOT_PARACHAIN_BINARY_PATH_FOR_ASSET_HUB_WOCOCO=$POLKADOT_PARACHAIN_BINARY_PATH
15+
export ZOMBIENET_BINARY_PATH=~/local_bridge_testing/bin/zombienet-linux
16+
17+
# bridge configuration
18+
export LANE_ID="00000001"
19+
20+
# tests configuration
21+
ALL_TESTS_FOLDER=`mktemp -d`
22+
23+
function start_coproc() {
24+
local command=$1
25+
local name=$2
26+
local coproc_log=`mktemp -p $TEST_FOLDER`
27+
coproc COPROC {
28+
$command >$coproc_log 2>&1
29+
}
30+
TEST_COPROCS[$COPROC_PID, 0]=$name
31+
TEST_COPROCS[$COPROC_PID, 1]=$coproc_log
32+
echo "Spawned $name coprocess. StdOut + StdErr: $coproc_log"
33+
34+
return $COPROC_PID
35+
}
36+
37+
# execute every test from tests folder
38+
TEST_INDEX=1
39+
while true
40+
do
41+
declare -A TEST_COPROCS
42+
TEST_COPROCS_COUNT=0
43+
TEST_PREFIX=$(printf "%04d" $TEST_INDEX)
44+
45+
# it'll be used by the `sync-exit.sh` script
46+
export TEST_FOLDER=`mktemp -d -p $ALL_TESTS_FOLDER`
47+
48+
# check if there are no more tests
49+
zndsl_files=($BRIDGE_TESTS_FOLDER/$TEST_PREFIX-*.zndsl)
50+
if [ ${#zndsl_files[@]} -eq 0 ]; then
51+
break
52+
fi
53+
54+
# start relay
55+
if [ -f $BRIDGE_TESTS_FOLDER/$TEST_PREFIX-start-relay.sh ]; then
56+
start_coproc "${BRIDGE_TESTS_FOLDER}/${TEST_PREFIX}-start-relay.sh" "relay"
57+
RELAY_COPROC=$COPROC_PID
58+
((TEST_COPROCS_COUNT++))
59+
fi
60+
# start tests
61+
for zndsl_file in "${zndsl_files[@]}"; do
62+
start_coproc "$ZOMBIENET_BINARY_PATH --provider native test $zndsl_file" "$zndsl_file"
63+
echo -n "1">>$TEST_FOLDER/exit-sync
64+
((TEST_COPROCS_COUNT++))
65+
done
66+
# wait until all tests are completed
67+
relay_exited=0
68+
for n in `seq 1 $TEST_COPROCS_COUNT`; do
69+
wait -n -p COPROC_PID
70+
exit_code=$?
71+
coproc_name=${TEST_COPROCS[$COPROC_PID, 0]}
72+
coproc_log=${TEST_COPROCS[$COPROC_PID, 1]}
73+
coproc_stdout=$(cat $coproc_log)
74+
relay_exited=$(expr "${coproc_name}" == "relay")
75+
echo "Process $coproc_name has finished with exit code: $exit_code"
76+
77+
# if exit code is not zero, exit
78+
if [ $exit_code -ne 0 ]; then
79+
echo "====================================================================="
80+
echo "=== Shutting down. Log of failed process below ==="
81+
echo "====================================================================="
82+
echo $coproc_stdout
83+
exit 1
84+
fi
85+
86+
# if last test has exited, exit relay too
87+
if [ $n -eq $(($TEST_COPROCS_COUNT - 1)) ] && [ $relay_exited -eq 0 ]; then
88+
kill $RELAY_COPROC
89+
break
90+
fi
91+
done
92+
((TEST_INDEX++))
93+
done
94+
95+
echo "====================================================================="
96+
echo "=== All tests have completed successfully ==="
97+
echo "====================================================================="

zombienet/scripts/invoke-script.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
pushd $POLKADOT_SDK_FOLDER/cumulus/scripts
4+
./bridges_rococo_wococo.sh $1
5+
popd

zombienet/scripts/sync-exit.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# every network adds a char to the file, let's remove ours
5+
truncate -s -1 $TEST_FOLDER/exit-sync
6+
7+
# when all chars are removed, then our test is done
8+
while true
9+
do
10+
if [ `stat --printf="%s" $TEST_FOLDER/exit-sync` -eq 0 ]; then
11+
exit
12+
fi
13+
sleep 100
14+
done
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Description: User is able to transfer ROC from Rococo Asset Hub to Wococo Asset Hub
2+
Network: ../../../cumulus/zombienet/bridge-hubs/bridge_hub_wococo_local_network.toml
3+
Creds: config
4+
5+
# step 1: initialize Wococo asset hub
6+
asset-hub-wococo-collator1: run ../scripts/invoke-script.sh with "init-asset-hub-wococo-local" within 120 seconds
7+
8+
# step 2: initialize Wococo bridge hub
9+
bridge-hub-wococo-collator1: run ../scripts/invoke-script.sh with "init-bridge-hub-wococo-local" within 120 seconds
10+
11+
# step 3: relay is started elsewhere - let's wait until with-Rococo GRANPDA pallet is initialized at Wococo
12+
bridge-hub-wococo-collator1: js-script ../helpers/best-finalized-header-at-bridged-chain.js with "Rococo,0" within 400 seconds
13+
14+
# step 2: send WOC to Rococo
15+
asset-hub-wococo-collator1: run ../scripts/invoke-script.sh with "reserve-transfer-assets-from-asset-hub-wococo-local" within 60 seconds
16+
17+
# step 3: elsewhere Rococo has sent ROC to //Alice - let's wait for it
18+
asset-hub-wococo-collator1: js-script ../helpers/wrapped-assets-balance.js with "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY,0,Rococo" within 600 seconds
19+
20+
# step 4: check that the relayer //Charlie is rewarded by both our AH and target AH
21+
bridge-hub-wococo-collator1: js-script ../helpers/relayer-rewards.js with "5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y,0x00000001,0x6268726F,BridgedChain,0" within 300 seconds
22+
bridge-hub-wococo-collator1: js-script ../helpers/relayer-rewards.js with "5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y,0x00000001,0x6268726F,ThisChain,0" within 300 seconds
23+
24+
# wait until other network test has completed OR exit with an error too
25+
asset-hub-wococo-collator1: run ../scripts/sync-exit.sh within 600 seconds
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Description: User is able to transfer WOC from Wococo Asset Hub to Rococo Asset Hub
2+
Network: ../../../cumulus/zombienet/bridge-hubs/bridge_hub_rococo_local_network.toml
3+
Creds: config
4+
5+
# step 1: initialize Rococo asset hub
6+
asset-hub-rococo-collator1: run ../scripts/invoke-script.sh with "init-asset-hub-rococo-local" within 120 seconds
7+
8+
# step 2: initialize Rococo bridge hub
9+
bridge-hub-rococo-collator1: run ../scripts/invoke-script.sh with "init-bridge-hub-rococo-local" within 120 seconds
10+
11+
# step 3: relay is started elsewhere - let's wait until with-Wococo GRANPDA pallet is initialized at Rococo
12+
bridge-hub-rococo-collator1: js-script ../helpers/best-finalized-header-at-bridged-chain.js with "Wococo,0" within 400 seconds
13+
14+
# step 4: send ROC to Wococo
15+
asset-hub-rococo-collator1: run ../scripts/invoke-script.sh with "reserve-transfer-assets-from-asset-hub-rococo-local" within 60 seconds
16+
17+
# step 5: elsewhere Wococo has sent WOC to //Alice - let's wait for it
18+
asset-hub-rococo-collator1: js-script ../helpers/wrapped-assets-balance.js with "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY,0,Wococo" within 600 seconds
19+
20+
# step 6: check that the relayer //Charlie is rewarded by both our AH and target AH
21+
bridge-hub-rococo-collator1: js-script ../helpers/relayer-rewards.js with "5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y,0x00000001,0x6268776F,BridgedChain,0" within 300 seconds
22+
bridge-hub-rococo-collator1: js-script ../helpers/relayer-rewards.js with "5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y,0x00000001,0x6268776F,ThisChain,0" within 300 seconds
23+
24+
# wait until other network test has completed OR exit with an error too
25+
asset-hub-rococo-collator1: run ../scripts/sync-exit.sh within 600 seconds
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
pushd $POLKADOT_SDK_FOLDER/cumulus/scripts
4+
./bridges_rococo_wococo.sh run-relay
5+
popd

0 commit comments

Comments
 (0)