Skip to content

Commit

Permalink
Test/e2e speed up (paritytech#749)
Browse files Browse the repository at this point in the history
* test: speed up e2e

* fix for mac osx

* Bump to resolve conflicts & supress warning

* Add rpc def when initialize to suppress warning

* Removes unnecessary beacon relay check. Adds total time printout for start-services at the end of the script. (paritytech#750)

Co-authored-by: claravanstaden <Cats 4 life!>

* minor fix

Co-authored-by: Clara van Staden <[email protected]>
  • Loading branch information
yrong and claravanstaden authored Jan 10, 2023
1 parent 0330bdd commit fc11987
Show file tree
Hide file tree
Showing 20 changed files with 948 additions and 511 deletions.
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"scripts": {
"preinstall": "npx only-allow pnpm",
"postinstall": "cd .. && husky install",
"postinstall": "cd .. && husky install && cd core/packages/test && ./scripts/hack-ethereum.sh",
"build": "turbo run build",
"test": "turbo run test",
"lint": "turbo run lint",
Expand Down
4 changes: 2 additions & 2 deletions core/packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"typescript": "^4.8.4"
},
"dependencies": {
"@polkadot/api": "^9.6.2",
"@polkadot/types": "^9.6.2",
"@polkadot/api": "9.11.1",
"@polkadot/types": "9.11.1",
"@snowbridge/contracts": "workspace:*",
"ethers": "^5.7.2"
}
Expand Down
2 changes: 1 addition & 1 deletion core/packages/contracts/scripts/configure-beefy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
} from "@polkadot/types/interfaces/beefy/types"

let endpoint = process.env.RELAYCHAIN_ENDPOINT || "ws://localhost:9944"
const beefyStartBlock = process.env.BEEFY_START_BLOCK ? parseInt(process.env.BEEFY_START_BLOCK) : 29
const beefyStartBlock = process.env.BEEFY_START_BLOCK ? parseInt(process.env.BEEFY_START_BLOCK) : 15

async function configureBeefy() {
let [signer] = await hre.ethers.getSigners()
Expand Down
2 changes: 1 addition & 1 deletion core/packages/test/.envrc-example
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export POLKADOT_BIN=/path/to/polkadot/target/release/polkadot
# export PARACHAIN_RELAY_ETH_KEY= # Your Parachain relayer account private key
# export E2E_TEST_ETH_KEY= # Your E2E test account private key

# BEEFY_START_BLOCK= # Start block to configure beefy client, default value: 29
# BEEFY_START_BLOCK= # Start block to configure beefy client, default value: 15

# RELAYCHAIN_ENDPOINT= # Relaychain endpoint, default value: ws://localhost:9944

Expand Down
2 changes: 1 addition & 1 deletion core/packages/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ These tests are meant to closely replicate real-world behaviour. This means that
To just run a specific test, the bridge needs to be bootstrapped first:

```bash
pnpm test:integration test/bootstrap.js
pnpm test:bootstrap
```

Now individual tests can be run, like the following:
Expand Down
2 changes: 1 addition & 1 deletion core/packages/test/config/beacon-relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"endpoint": "http://localhost:9596",
"spec": {
"minimal": {
"slotsInEpoch": 8,
"slotsInEpoch": 4,
"epochsPerSyncCommitteePeriod": 8
},
"mainnet": {
Expand Down
11 changes: 6 additions & 5 deletions core/packages/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"directory": "core/packages/test"
},
"scripts": {
"test:integration": "mocha --timeout 600000 --exit"
"test:integration": "mocha --timeout 600000 --exit",
"test:bootstrap": "mocha --timeout 600000 --parallel --grep Bootstrap --exit"
},
"devDependencies": {
"@polkadot/api": "9.6.2",
"@polkadot/types": "9.6.2",
"@polkadot/util": "10.1.11",
"@polkadot/util-crypto": "10.1.11",
"@polkadot/api": "9.11.1",
"@polkadot/types": "9.11.1",
"@polkadot/util": "10.2.3",
"@polkadot/util-crypto": "10.2.3",
"@chainsafe/lodestar": "1.2.2",
"@types/keccak": "^3.0.1",
"@types/node": "^16.4.2",
Expand Down
8 changes: 8 additions & 0 deletions core/packages/test/scripts/build-binary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,17 @@ build_relayer()
cp $relay_bin "$output_bin_dir"
}

build_e2e_test() {
echo "Building tests"
pushd "$core_dir"
pnpm install
popd
}

install_binary() {
echo "Building and installing binaries."
build_relaychain
build_parachain
build_relayer
build_e2e_test
}
2 changes: 1 addition & 1 deletion core/packages/test/scripts/configure-contracts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source scripts/set-env.sh

configure_contracts()
{
pushd "$ethereum_dir"
pushd "$contract_dir"

npx hardhat run ./scripts/configure-beefy.ts --network $eth_network

Expand Down
2 changes: 1 addition & 1 deletion core/packages/test/scripts/deploy-ethereum.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ start_lodestar() {

deploy_contracts()
{
pushd "$ethereum_dir"
pushd "$contract_dir"
npx hardhat deploy --network $eth_network --reset --export "$output_dir/contracts.json"
popd
echo "Exported contract artifacts: $output_dir/contracts.json"
Expand Down
18 changes: 18 additions & 0 deletions core/packages/test/scripts/hack-ethereum.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -eu

source scripts/set-env.sh

hack_lodestar() {
preset_minimal_phase0_file="$core_dir/node_modules/.pnpm/@lodestar+params@$lodestar_version/node_modules/@lodestar/params/lib/presets/minimal/phase0.js"
if [[ "$OSTYPE" =~ ^darwin ]]
then
sed_cmd=gsed
else
sed_cmd=sed
fi
echo "change variable SLOTS_PER_EPOCH from 8 to 4"
$sed_cmd -i "s/SLOTS_PER_EPOCH: 8/SLOTS_PER_EPOCH: 4/g" $preset_minimal_phase0_file
}

hack_lodestar
12 changes: 11 additions & 1 deletion core/packages/test/scripts/set-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ parachain_dir="$root_dir/parachain"
parachain_runtime="${PARACHAIN_RUNTIME:-snowbase}"
parachain_bin="$parachain_dir/target/release/snowbridge"
test_collator_bin="$parachain_dir/utils/test-parachain/target/release/snowbridge-test-node"
ethereum_dir="$root_dir/core/packages/contracts"
core_dir="$root_dir/core"
lodestar_version="${LODESTAR_VER:-1.2.2}"
contract_dir="$core_dir/packages/contracts"
relay_dir="$root_dir/relayer"
relay_bin="$relay_dir/build/snowbridge-relay"
output_dir=/tmp/snowbridge
Expand Down Expand Up @@ -102,11 +104,19 @@ check_tool() {
echo 'Error: mage is not installed.'
exit
fi
if ! [ -x "$(command -v pnpm)" ]; then
echo 'Error: pnpm is not installed.'
exit
fi
if [[ "$OSTYPE" =~ ^darwin ]]; then
if ! [ -x "$(command -v gdate)" ]; then
echo 'Error: gdate is not installed.'
exit
fi
if ! [ -x "$(command -v gsed)" ]; then
echo 'Error: gsed is not installed.'
exit
fi
fi
}

7 changes: 0 additions & 7 deletions core/packages/test/scripts/start-relayer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ start_relayer()
sleep 20
done
) &

# waiting sync headers
until grep "starting to sync finalized headers" beacon-relay.log > /dev/null; do
echo "Waiting for beacon relay to sync headers..."
sleep 5
done
echo "Ready for beacon relayer to sync headers"
}

if [ -z "${from_start_services:-}" ]; then
Expand Down
10 changes: 9 additions & 1 deletion core/packages/test/scripts/start-services.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/usr/bin/env bash
set -eu

start=$(date +%s)

source scripts/set-env.sh
source scripts/build-binary.sh
from_start_services=true

trap kill_all SIGINT SIGTERM EXIT
kill_all && cleanup
# 0. check required tools
# 0. check required tools
echo "Check building tools"
check_tool

Expand All @@ -27,4 +29,10 @@ start_relayer

echo "Testnet has been initialized"

end=$(date +%s)
runtime=$((end-start))
minutes=$(( (runtime % 3600) / 60 ));
seconds=$(( (runtime % 3600) % 60 ));
echo "Took $minutes minutes $seconds seconds"

wait
17 changes: 17 additions & 0 deletions core/packages/test/src/subclient/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ class SubClient {
const provider = new WsProvider(this.endpoint);
this.api = await ApiPromise.create({
provider,
rpc: {
basicOutboundChannel: {
getMerkleProof: {
params: [
{
name: 'commitment_hash',
type: 'H256',
},
{
name: 'leaf_index',
type: 'u64',
}
],
type: 'Bytes'
}
}
}
})

this.keyring = new Keyring({ type: 'sr25519' });
Expand Down
45 changes: 45 additions & 0 deletions core/packages/test/test/bootstrap-e2s.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const Web3 = require('web3');

const BigNumber = require('bignumber.js');

const { expect } = require("chai")
.use(require("chai-as-promised"))
.use(require("chai-bignumber")(BigNumber));

const { treasuryAddressSS58, polkadotSenderSS58Alice,
polkadotRecipientSS58, polkadotRecipient, bootstrap } = require('../src/fixtures');

const { ChannelId } = require("../src/helpers");

describe('Bootstrap-Ethereum-to-Substrate', function () {
let ethClient, subClient;

before(async function () {
const clients = await bootstrap();
ethClient = clients.ethClient;
subClient = clients.subClient;
this.testParaEthAssetId = 0;
});

it('should transfer ETH from Ethereum to Substrate (basic channel)', async function () {
const amount = BigNumber(Web3.utils.toWei('0.001', "ether"));
const ethAccount = ethClient.accounts[1];

const subBalances = await subClient.subscribeAssetsAccountBalances(
this.testParaEthAssetId, polkadotRecipientSS58, 2
);

const beforeEthBalance = await ethClient.getEthBalance(ethAccount);
const beforeSubBalance = await subBalances[0];

const { gasCost } = await ethClient.lockETH(ethAccount, amount, polkadotRecipient, ChannelId.BASIC, 0, 0);

const afterEthBalance = await ethClient.getEthBalance(ethAccount);
const afterSubBalance = await subBalances[1];

expect(beforeEthBalance.minus(afterEthBalance)).to.be.bignumber.equal(amount.plus(gasCost));
expect(afterSubBalance.minus(beforeSubBalance)).to.be.bignumber.equal(amount);
// conservation of value
expect(beforeEthBalance.plus(beforeSubBalance)).to.be.bignumber.equal(afterEthBalance.plus(afterSubBalance).plus(gasCost));
});
});
42 changes: 42 additions & 0 deletions core/packages/test/test/bootstrap-s2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const Web3 = require('web3');

const BigNumber = require('bignumber.js');

const { expect } = require("chai")
.use(require("chai-as-promised"))
.use(require("chai-bignumber")(BigNumber));

const { treasuryAddressSS58, polkadotSenderSS58Alice,
polkadotRecipientSS58, polkadotRecipient, bootstrap } = require('../src/fixtures');

const { ChannelId } = require("../src/helpers");

describe('Bootstrap-Substrate-to-Ethereum', function () {
let ethClient, subClient;

before(async function () {
const clients = await bootstrap();
ethClient = clients.ethClient;
subClient = clients.subClient;
this.testParaEthAssetId = 0;
});

it('should transfer DOT from Substrate to Ethereum (basic channel)', async function () {
const amount = BigNumber('100000000000000'); // 100 DOT (12 decimal places in this environment)
const amountWrapped = BigNumber(Web3.utils.toWei('100', "ether")); // 100 SnowDOT (18 decimal places)
const ethAccount = ethClient.accounts[1];

const beforeEthBalance = await ethClient.getDotBalance(ethAccount);
const beforeSubBalanceAlice = await subClient.queryAccountBalance(polkadotSenderSS58Alice);

// lock DOT using basic channel
await subClient.lockDOT(subClient.alice, ethAccount, amount.toFixed(), ChannelId.BASIC)
await ethClient.waitForNextEventData({ appName: 'appDOT', eventName: 'Minted' });

const afterEthBalance = await ethClient.getDotBalance(ethAccount);
const afterSubBalanceAlice = await subClient.queryAccountBalance(polkadotSenderSS58Alice);

expect(afterEthBalance.minus(beforeEthBalance)).to.be.bignumber.equal(amountWrapped);
expect(beforeSubBalanceAlice.minus(afterSubBalanceAlice)).to.be.bignumber.greaterThan(amount);
});
});
66 changes: 0 additions & 66 deletions core/packages/test/test/bootstrap.js

This file was deleted.

Loading

0 comments on commit fc11987

Please sign in to comment.