From b4502eb390ecc1cdea251ea84267b21e2a55b108 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Mon, 16 Dec 2024 13:07:04 +0400 Subject: [PATCH] working test with symbiotic --- test/scripts/bridge-build-artifacts.sh | 5 ++ .../bridge/assets/execution-relay.json | 2 +- test/scripts/bridge/assets/genesis.json | 2 +- test/scripts/bridge/build-relayer.sh | 2 +- ...racts.sh => build-snowbridge-contracts.sh} | 2 +- .../bridge/build-symbiotic-contracts.sh | 26 +++++++ .../bridge/deploy-ethereum-contracts.sh | 15 ++-- test/scripts/bridge/generate-contract-info.sh | 15 ---- test/scripts/bridge/generate-eth-info.sh | 17 +++++ test/scripts/bridge/set-env.sh | 10 ++- test/scripts/bridge/setup-relayer.sh | 4 +- test/scripts/bridge/ts-scripts/package.json | 13 ++++ .../ts-scripts/src/generateContractInfo.ts | 42 +++++++++++ .../test_zombie_tanssi_relay_eth_bridge.ts | 70 +++++++++++-------- 14 files changed, 169 insertions(+), 56 deletions(-) rename test/scripts/bridge/{build-eth-contracts.sh => build-snowbridge-contracts.sh} (80%) create mode 100755 test/scripts/bridge/build-symbiotic-contracts.sh delete mode 100755 test/scripts/bridge/generate-contract-info.sh create mode 100755 test/scripts/bridge/generate-eth-info.sh create mode 100644 test/scripts/bridge/ts-scripts/package.json create mode 100644 test/scripts/bridge/ts-scripts/src/generateContractInfo.ts diff --git a/test/scripts/bridge-build-artifacts.sh b/test/scripts/bridge-build-artifacts.sh index 8d45ac490..2dabcf0bf 100755 --- a/test/scripts/bridge-build-artifacts.sh +++ b/test/scripts/bridge-build-artifacts.sh @@ -13,3 +13,8 @@ GOBIN=$output_bin_dir go install github.com/ferranbt/fastssz/sszgen@v0.1.4 $bridge_scripts/build-ethereum-node.sh $bridge_scripts/build-relayer.sh +$bridge_scripts/build-symbiotic-contracts.sh + +pushd $ts_scripts_dir +pnpm install +popd diff --git a/test/scripts/bridge/assets/execution-relay.json b/test/scripts/bridge/assets/execution-relay.json index 67f268b74..39afaad4e 100644 --- a/test/scripts/bridge/assets/execution-relay.json +++ b/test/scripts/bridge/assets/execution-relay.json @@ -31,7 +31,7 @@ }, "instantVerification": false, "schedule": { - "id": null, + "id": 1, "totalRelayerCount": 1, "sleepInterval": 1 } diff --git a/test/scripts/bridge/assets/genesis.json b/test/scripts/bridge/assets/genesis.json index 4af3fe436..49a0747ed 100644 --- a/test/scripts/bridge/assets/genesis.json +++ b/test/scripts/bridge/assets/genesis.json @@ -22,7 +22,7 @@ "gasLimit": "80000000", "alloc": { "90A987B944Cb1dCcE5564e5FDeCD7a54D3de27Fe": { - "balance": "10000000000000000000000" + "balance": "1000000000000000000000000" }, "Be68fC2d8249eb60bfCf0e71D5A0d2F2e292c4eD": { "balance": "100000000000000000000" diff --git a/test/scripts/bridge/build-relayer.sh b/test/scripts/bridge/build-relayer.sh index 8d1ad1d86..bf0c54944 100755 --- a/test/scripts/bridge/build-relayer.sh +++ b/test/scripts/bridge/build-relayer.sh @@ -21,7 +21,7 @@ else popd fi -$scripts_path/build-eth-contracts.sh +$scripts_path/build-snowbridge-contracts.sh echo "Building Relayer" pushd $relayer_root_dir diff --git a/test/scripts/bridge/build-eth-contracts.sh b/test/scripts/bridge/build-snowbridge-contracts.sh similarity index 80% rename from test/scripts/bridge/build-eth-contracts.sh rename to test/scripts/bridge/build-snowbridge-contracts.sh index b64ad4662..331a7d7fc 100755 --- a/test/scripts/bridge/build-eth-contracts.sh +++ b/test/scripts/bridge/build-snowbridge-contracts.sh @@ -7,7 +7,7 @@ scripts_path="$(realpath ./scripts/bridge)" source $scripts_path/set-env.sh -echo "Building contracts" +echo "Building snowbridge contracts" pushd $contract_dir forge build diff --git a/test/scripts/bridge/build-symbiotic-contracts.sh b/test/scripts/bridge/build-symbiotic-contracts.sh new file mode 100755 index 000000000..662343401 --- /dev/null +++ b/test/scripts/bridge/build-symbiotic-contracts.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Exit on any error +set -e + +scripts_path="$(realpath ./scripts/bridge)" + +source $scripts_path/set-env.sh + +echo "Building symbiotic contracts" + +echo "Checkout Symbiotic contract repository" + +if [ -d "$symbiotic_contracts_dir" ]; +then + echo "Symbiotic contract repository seems to be already setup. Skipping git fetch" +else + git clone https://github.com/moondance-labs/tanssi-symbiotic $symbiotic_contracts_dir + pushd $symbiotic_contracts_dir + git fetch && git checkout 518f7e9d0059d24d899d2fc8340da5387a127b3b + popd +fi + +pushd $symbiotic_contracts_dir +forge build +popd diff --git a/test/scripts/bridge/deploy-ethereum-contracts.sh b/test/scripts/bridge/deploy-ethereum-contracts.sh index 544c1824d..87c6ed396 100755 --- a/test/scripts/bridge/deploy-ethereum-contracts.sh +++ b/test/scripts/bridge/deploy-ethereum-contracts.sh @@ -8,15 +8,22 @@ source $scripts_path/set-env.sh deploy_command() { local deploy_script=$1 - pushd "$contract_dir" - forge script \ + OWNER_PRIVATE_KEY=$ethereum_key forge script \ --rpc-url $eth_endpoint_http \ --legacy \ --broadcast \ -vvv \ + --slow \ + --skip-simulation \ $deploy_script - popd } -echo "Deploying contracts" +echo "Deploying snowbridge contracts" +pushd "$contract_dir" deploy_command scripts/DeployLocal.sol:DeployLocal +popd + +echo "Deploying symbiotic contracts" +pushd "$symbiotic_contracts_dir" +deploy_command script/test/DeployTanssiEcosystemDemo.s.sol:DeployTanssiEcosystem +popd diff --git a/test/scripts/bridge/generate-contract-info.sh b/test/scripts/bridge/generate-contract-info.sh deleted file mode 100755 index 04b65d3d4..000000000 --- a/test/scripts/bridge/generate-contract-info.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash - -set -eu - -scripts_path="$(realpath ./scripts/bridge)" -source $scripts_path/set-env.sh - -pushd "$test_helpers_dir" > /dev/null -pnpm generateContracts "$output_dir/contracts.json" > /dev/null -popd > /dev/null - -# Output the file so that invoker can read it -contract_output=$(cat "$output_dir/contracts.json") - -echo "{ \"data\": $contract_output, \"ethereum_key\": \"$ethereum_key\" }" diff --git a/test/scripts/bridge/generate-eth-info.sh b/test/scripts/bridge/generate-eth-info.sh new file mode 100755 index 000000000..44ada149b --- /dev/null +++ b/test/scripts/bridge/generate-eth-info.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -eu + +scripts_path="$(realpath ./scripts/bridge)" +source $scripts_path/set-env.sh + +pushd "$ts_scripts_dir" > /dev/null +contract_dir="$artifacts_dir/relayer/contracts" deploy_script="DeployLocal.sol" pnpm generateContracts "$output_dir/snowbridge_contracts.json" > /dev/null +contract_dir="$artifacts_dir/tanssi-symbiotic" deploy_script="DeployTanssiEcosystemDemo.s.sol" pnpm generateContracts "$output_dir/symbiotic_contracts.json" > /dev/null +popd > /dev/null + +# Output the file so that invoker can read it +snowbridge_info=$(cat "$output_dir/snowbridge_contracts.json") +symbiotic_info=$(cat "$output_dir/symbiotic_contracts.json") + +echo "{ \"snowbridge_info\": $snowbridge_info, \"symbiotic_info\": $symbiotic_info, \"ethereum_key\": \"$ethereum_key\" }" diff --git a/test/scripts/bridge/set-env.sh b/test/scripts/bridge/set-env.sh index 9e0dca75d..7f286a090 100755 --- a/test/scripts/bridge/set-env.sh +++ b/test/scripts/bridge/set-env.sh @@ -1,6 +1,7 @@ root_dir="$(realpath .)" scripts_root_dir="$root_dir/scripts/bridge" +ts_scripts_dir="$scripts_root_dir/ts-scripts" assets_dir="$scripts_root_dir/assets" artifacts_dir="$root_dir/tmp/bridge" @@ -15,6 +16,7 @@ mkdir -p $output_bin_dir export PATH="$output_bin_dir:$PATH" relayer_root_dir="$artifacts_dir/relayer" +symbiotic_contracts_dir="$artifacts_dir/tanssi-symbiotic" web_dir="$relayer_root_dir/web" export contract_dir="$relayer_root_dir/contracts" test_helpers_dir="$web_dir/packages/test-helpers" @@ -109,8 +111,12 @@ ethereum_key="0x5e002a1af63fd31f1c25258f3082dc889762664cb8f218d86da85dff8b07b342 ethereum_address="90A987B944Cb1dCcE5564e5FDeCD7a54D3de27Fe" -address_for() { - jq -r ".contracts.${1}.address" "$output_dir/contracts.json" +snowbridge_address_for() { + jq -r ".contracts.${1}.address" "$output_dir/snowbridge_contracts.json" +} + +symbiotic_address_for() { + jq -r ".contracts.${1}.address" "$output_dir/symbiotic_contracts.json" } kill_all() { diff --git a/test/scripts/bridge/setup-relayer.sh b/test/scripts/bridge/setup-relayer.sh index c40e6b9ff..47850dfc0 100755 --- a/test/scripts/bridge/setup-relayer.sh +++ b/test/scripts/bridge/setup-relayer.sh @@ -13,7 +13,7 @@ mkdir -p $data_store_dir config_relayer() { # Configure beefy relay jq \ - --arg k1 "$(address_for BeefyClient)" \ + --arg k1 "$(snowbridge_address_for BeefyClient)" \ --arg eth_endpoint_ws $eth_endpoint_ws \ --arg eth_gas_limit $eth_gas_limit \ --arg relay_chain_endpoint $RELAYCHAIN_ENDPOINT \ @@ -45,7 +45,7 @@ config_relayer() { # Configure execution relay for starlight jq \ --arg eth_endpoint_ws $eth_endpoint_ws \ - --arg k1 "$(address_for GatewayProxy)" \ + --arg k1 "$(snowbridge_address_for GatewayProxy)" \ --arg relay_chain_endpoint $RELAYCHAIN_ENDPOINT \ --arg channelID $PRIMARY_GOVERNANCE_CHANNEL_ID \ --arg data_store_dir $data_store_dir \ diff --git a/test/scripts/bridge/ts-scripts/package.json b/test/scripts/bridge/ts-scripts/package.json new file mode 100644 index 000000000..ad9cfc01c --- /dev/null +++ b/test/scripts/bridge/ts-scripts/package.json @@ -0,0 +1,13 @@ +{ + "name": "@tanssi-symbiotic/test-helpers", + "version": "1.0.0", + "description": "Tanssi symbiotic test helpers", + "license": "Apache-2.0", + "scripts": { + "generateContracts": "npx tsx src/generateContractInfo.ts" + }, + "dependencies": { + "@types/node": "^18.16.8", + "tsx": "^4.19.2" + } +} diff --git a/test/scripts/bridge/ts-scripts/src/generateContractInfo.ts b/test/scripts/bridge/ts-scripts/src/generateContractInfo.ts new file mode 100644 index 000000000..a746f4419 --- /dev/null +++ b/test/scripts/bridge/ts-scripts/src/generateContractInfo.ts @@ -0,0 +1,42 @@ +import fs from "node:fs"; +import path from "node:path"; + +const run = async () => { + const NetworkId = process.env.ETH_NETWORK_ID || 11155111; + const basedir = process.env.contract_dir || "../contracts"; + const DeployInfoFile = path.join(basedir, "broadcast", process.env.deploy_script, `${NetworkId}/run-latest.json`); + const BuildInfoDir = path.join(basedir, "./out"); + const DestFile = process.argv.length >= 3 ? process.argv[2] : process.env["output_dir"] + "/contracts.json"; + type Contract = { + [key: string]: ContractInfo; + }; + const contracts: Contract = {}; + const deploymentInfo = JSON.parse(fs.readFileSync(DeployInfoFile, "utf8")); + type ContractInfo = { + abi?: object; + address?: string; + }; + for (const transaction of deploymentInfo.transactions) { + if (transaction.transactionType === "CREATE") { + const contractName: string = transaction.contractName; + if (contractName) { + const contractInfo: ContractInfo = { address: transaction.contractAddress }; + const contractBuildingInfo = JSON.parse( + fs.readFileSync(path.join(BuildInfoDir, contractName + ".sol", contractName + ".json"), "utf8") + ); + contractInfo.abi = contractBuildingInfo.abi; + contracts[contractName] = contractInfo; + } + } + } + fs.writeFileSync(DestFile, JSON.stringify({ contracts }, null, 2), "utf8"); +}; + +run() + .then(() => { + console.log("Contract File generated successfully"); + process.exit(0); + }) + .catch((err) => { + console.error(err); + }); diff --git a/test/suites/zombie_tanssi_relay_eth_bridge/test_zombie_tanssi_relay_eth_bridge.ts b/test/suites/zombie_tanssi_relay_eth_bridge/test_zombie_tanssi_relay_eth_bridge.ts index 708218b10..28ce773bf 100644 --- a/test/suites/zombie_tanssi_relay_eth_bridge/test_zombie_tanssi_relay_eth_bridge.ts +++ b/test/suites/zombie_tanssi_relay_eth_bridge/test_zombie_tanssi_relay_eth_bridge.ts @@ -32,10 +32,13 @@ describeSuite({ let relayerChildProcess; let alice; let beefyClientDetails; - let gatewayProxyAddress; - let gatewayDetails; + const ethUrl = "ws://127.0.0.1:8546"; + let customHttpProvider; let ethereumWallet; + let middlewareContract; + let gatewayProxyAddress; + let middlewareDetails; let operatorAccount; let operatorNimbusKey; @@ -57,7 +60,6 @@ describeSuite({ operatorAccount = keyring.addFromUri("//Charlie", { name: "Charlie default" }); // We rotate the keys for charlie so that we have access to them from this test as well as the node operatorNimbusKey = await relayCharlieApi.rpc.author.rotateKeys(); - console.log(`operatorNimbusKey: ${operatorNimbusKey}`); await relayApi.tx.session.setKeys(operatorNimbusKey, []).signAndSend(operatorAccount); const fundingTxHash = await relayApi.tx.utility @@ -86,20 +88,25 @@ describeSuite({ console.log("Waiting some time for ethereum node to produce block, before we deploy contract"); await sleep(20000); - await execCommand("./scripts/bridge/deploy-ethereum-contracts.sh"); + await execCommand("./scripts/bridge/deploy-ethereum-contracts.sh", { + env: { + OPERATOR1_KEY: u8aToHex(operatorAccount.addressRaw), + ...process.env, + }, + }); console.log("Contracts deployed"); - const contractInfoData = JSON.parse( - (await execCommand("./scripts/bridge/generate-contract-info.sh")).stdout - ); + const ethInfo = JSON.parse((await execCommand("./scripts/bridge/generate-eth-info.sh")).stdout); + + console.log("BeefyClient contract address is:", ethInfo.snowbridge_info.contracts.BeefyClient.address); + beefyClientDetails = ethInfo.snowbridge_info.contracts.BeefyClient; - console.log("BeefyClient contract address is:", contractInfoData.data.contracts.BeefyClient.address); - beefyClientDetails = contractInfoData.data.contracts.BeefyClient; + console.log("Gateway contract proxy address is:", ethInfo.snowbridge_info.contracts.GatewayProxy.address); + gatewayProxyAddress = ethInfo.snowbridge_info.contracts.GatewayProxy.address; - console.log("Gateway contract proxy address is:", contractInfoData.data.contracts.GatewayProxy.address); - gatewayProxyAddress = contractInfoData.data.contracts.GatewayProxy.address; - gatewayDetails = contractInfoData.data.contracts.Gateway; + console.log("Symbiotic middleware address is: ", ethInfo.symbiotic_info.contracts.Middleware.address); + middlewareDetails = ethInfo.symbiotic_info.contracts.Middleware; console.log("Setting gateway address to proxy contract:", gatewayProxyAddress); const setGatewayAddressTxHash = await relayApi.tx.sudo @@ -107,8 +114,13 @@ describeSuite({ .signAndSend(alice); console.log("Set gateway address transaction hash:", setGatewayAddressTxHash.toHex()); - const customHttpProvider = new ethers.WebSocketProvider("ws://127.0.0.1:8546"); - ethereumWallet = new ethers.Wallet(contractInfoData.ethereum_key, customHttpProvider); + customHttpProvider = new ethers.WebSocketProvider(ethUrl); + ethereumWallet = new ethers.Wallet(ethInfo.ethereum_key, customHttpProvider); + + // Setting up Middleware + middlewareContract = new ethers.Contract(middlewareDetails.address, middlewareDetails.abi, ethereumWallet); + const tx = await middlewareContract.setGateway(gatewayProxyAddress); + await tx.wait(); const initialBeaconUpdate = JSON.parse( ( @@ -163,8 +175,6 @@ describeSuite({ id: "T02", title: "Dancelight Blocks are being recognized on ethereum", test: async function () { - const url = "ws://127.0.0.1:8546"; - const customHttpProvider = new ethers.WebSocketProvider(url); const beefyContract = new ethers.Contract( beefyClientDetails.address, beefyClientDetails.abi, @@ -182,21 +192,21 @@ describeSuite({ id: "T03", title: "Message can be passed from ethereum to Starlight", test: async function () { - const gatewayContract = new ethers.Contract(gatewayProxyAddress, gatewayDetails.abi, ethereumWallet); - const externalValidatorsBefore = await relayApi.query.externalValidators.externalValidators(); - const sessionValidatorsBefore = await relayApi.query.session.validators(); - expect(!sessionValidatorsBefore.includes(operatorNimbusKey)); + const epoch = await middlewareContract.getCurrentEpoch(); + const currentOperators = await middlewareContract.getOperatorsByEpoch(epoch); + const currentOperatorsKeys = []; + for (let i = 0; i < currentOperators.length; i++) { + currentOperatorsKeys.push(await middlewareContract.getCurrentOperatorKey(currentOperators[i])); + } - const rawValidators = [ - u8aToHex(operatorAccount.addressRaw), - "0x7894567890123456789012345678901234567890123456789012345678901234", - "0x4564567890123456789012345678901234567890123456789012345678901234", - ]; + console.log("Middleware: Epoch is:", epoch); + console.log("Middleware: Operator keys are:", currentOperatorsKeys); + console.log("Starlight: External validators are:", externalValidatorsBefore.toJSON()); try { - const tx = await gatewayContract.sendOperatorsData(rawValidators, 1); + const tx = await middlewareContract.sendCurrentOperatorsKeys(); await tx.wait(); } catch (error) { throw new Error(`Failed to send operator data: ${error.message}`, error.code); @@ -226,10 +236,12 @@ describeSuite({ return u8aToHex(decodeAddress(x)); }); - expect(externalValidatorsHex).to.deep.eq(rawValidators); + console.log("After message transfer:"); + + console.log("Middleware: Operator keys are:", currentOperatorsKeys); + console.log("Starlight: External validators are:", externalValidatorsHex); - const sessionValidators = await relayApi.query.session.validators(); - expect(sessionValidators.includes(operatorNimbusKey)); + expect(externalValidatorsHex).to.deep.eq(currentOperatorsKeys); }, });