From b155b6884f3f063e44634304e66388809376ee49 Mon Sep 17 00:00:00 2001 From: Marko Atanasievski Date: Tue, 19 Dec 2023 16:11:01 +0100 Subject: [PATCH] feat: add test for transaction inclusion in certificate (#48) --- .env | 5 +- contracts.yml | 2 + scripts/send-token.ts | 12 +++- tests/test_cert_delivery.sh | 1 - tests/test_cert_inclusion.sh | 39 +++---------- tests/test_transaction_in_certificate.sh | 73 ++++++++++++++++++++++++ tests/utils.sh | 56 ++++++++++++++++++ 7 files changed, 149 insertions(+), 39 deletions(-) create mode 100755 tests/test_transaction_in_certificate.sh diff --git a/.env b/.env index 3d3406a..bf09a4d 100644 --- a/.env +++ b/.env @@ -2,11 +2,11 @@ COMPOSE_FILE=network.yml:contracts.yml:subnet-topos.yml:subnet-incal.yml:executo COMPOSE_PROJECT_NAME=infra # Topos Subnet -TOPOS_HOST_PORT=10002 +export TOPOS_HOST_PORT=10002 TOPOS_CHAIN_ID=2357 # Incal -INCAL_HOST_PORT=20002 +export INCAL_HOST_PORT=20002 INCAL_CHAIN_ID=2358 INCAL_LOGO_URL=https://toposware.com/logo-incal.svg @@ -28,4 +28,3 @@ EXECUTOR_SERVICE_VERSION=1.0.0 # TESTS export LOCAL_ERC20_HOME=./ -export INCAL_HOST_PORT=20002 diff --git a/contracts.yml b/contracts.yml index 0c6e576..c1699cb 100644 --- a/contracts.yml +++ b/contracts.yml @@ -28,7 +28,9 @@ services: npx ts-node scripts/deploy-topos-msg-protocol http://topos-node-1:8545 $(cat /data/topos/node/node-1/consensus/validator.key) > /contracts/.env && echo export SUBNET_REGISTRATOR_CONTRACT_ADDRESS=$(npx ts-node scripts/deploy-subnet-registrator http://topos-node-1:8545 $(cat /data/topos/node/node-1/consensus/validator.key) $SUBNET_REGISTRATOR_SALT 4000000) >> /contracts/.env && source /contracts/.env && + cat /contracts/.env && npm run register-subnet http://topos-node-1:8545 $(printenv SUBNET_REGISTRATOR_CONTRACT_ADDRESS) Incal $INCAL_CHAIN_ID http://localhost:$INCAL_HOST_PORT ws://localhost:$INCAL_HOST_PORT/ws INCA $INCAL_LOGO_URL $(cat /data/topos/node/node-1/consensus/validator.key) $(cat /data/incal/data-1/consensus/validator.key)" + volumes: - contracts:/contracts - topos-data:/data/topos diff --git a/scripts/send-token.ts b/scripts/send-token.ts index 8e846ae..0c3516c 100644 --- a/scripts/send-token.ts +++ b/scripts/send-token.ts @@ -10,9 +10,10 @@ const DAILY_MINT_LIMIT = 100 const INITIAL_SUPPLY = 10_000_000 /// Usage: -/// ts-node ./scripts/send-token.ts +/// ts-node ./scripts/send-token.ts +/// If parameter return value is 'txhash', the script will return the transaction hash; by default it returns the block's receipt's root const main = async function (...args: string[]) { - const [providerEndpoint, senderPrivateKey, targetSubnetId, receiverAddress, amount] = args + const [providerEndpoint, senderPrivateKey, targetSubnetId, receiverAddress, amount, return_value] = args const provider = providers.getDefaultProvider(providerEndpoint) const erc20MessagingAddress = sanitizeHexString( process.env.ERC20_MESSAGING_CONTRACT_ADDRESS || '' @@ -79,7 +80,12 @@ const main = async function (...args: string[]) { receipt.blockHash, true, ]) - console.log(rawBlock.receiptsRoot) + if (return_value == 'txhash') { + console.log(sendTokenTx.hash) + return + } else { + console.log(rawBlock.receiptsRoot) + } } const sanitizeHexString = function (hexString: string) { diff --git a/tests/test_cert_delivery.sh b/tests/test_cert_delivery.sh index 235ce12..f0510ba 100755 --- a/tests/test_cert_delivery.sh +++ b/tests/test_cert_delivery.sh @@ -76,7 +76,6 @@ do fi exit 1 fi - done diff --git a/tests/test_cert_inclusion.sh b/tests/test_cert_inclusion.sh index 176561e..a0a4527 100755 --- a/tests/test_cert_inclusion.sh +++ b/tests/test_cert_inclusion.sh @@ -3,36 +3,8 @@ set -e source $LOCAL_ERC20_HOME/tests/utils.sh -function check_artifacts() -{ - if [ ! -d $LOCAL_ERC20_HOME/artifacts ] || [ -z "$(ls -A $LOCAL_ERC20_HOME/artifacts)" ]; then - $LOCAL_ERC20_HOME/scripts/copy_contract_artifacts.sh - fi -} - -function get_incal_subnet_id() -{ - echo $(docker compose logs incal-sequencer | grep "for subnet id 0x" | awk -F 'for subnet id ' '{print $2}') -} - -function send_token_with_retry() -{ - # $1 - number of retries - # $2 - target subnet id - arbitrary_receiver_address="0xF472626faeb65277342e07962a8333A735934554" - amount="1" - for i in $(seq 1 $1); - do - receipts_root=$(npx ts-node $LOCAL_ERC20_HOME/scripts/send-token http://localhost:$INCAL_HOST_PORT $PRIVATE_KEY $2 $arbitrary_receiver_address $amount) - if [ -z "$receipts_root" ]; then - "$(($i+1))" - sleep 5 - continue - fi - echo $receipts_root - break - done -} +export ERC20_MESSAGING_CONTRACT_ADDRESS=$(get_erc20_contract_address) +export TOPOS_CORE_PROXY_CONTRACT_ADDRESS=$(get_topos_core_proxy_contract_address) function check_certificate_inclusion_with_retry() { @@ -66,14 +38,17 @@ fi # Perform test echo "Executing certificate inclusion test..." incal_subnet_id=$(get_incal_subnet_id) -receipts_root=$(send_token_with_retry 3 $incal_subnet_id) +receipts_root=$(send_token_with_retry 3 $incal_subnet_id $TOPOS_HOST_PORT) echo "Receipts root hash: $receipts_root" is_certificate_present=$(check_certificate_inclusion_with_retry 3 $receipts_root) if [ $is_certificate_present -eq 0 ]; then echo "Certificate is present" - exit 0 else echo "Certificate is NOT present" + if [ $network_started -eq 1 ]; then + echo "Shutting down network started for this test" + stop_network + fi exit 1 fi diff --git a/tests/test_transaction_in_certificate.sh b/tests/test_transaction_in_certificate.sh new file mode 100755 index 0000000..99ea2f0 --- /dev/null +++ b/tests/test_transaction_in_certificate.sh @@ -0,0 +1,73 @@ +#!/bin/bash +set -e + +source $LOCAL_ERC20_HOME/tests/utils.sh + + +function get_transaction_in_certificate() +{ + echo $(docker compose logs incal-sequencer | grep "CrossSubnetMessageSentFilter" | \ + awk -F 'transaction_hash: ' '{print $2}' | awk -F ',' '{print $1'} | tail -1) +} + +export ERC20_MESSAGING_CONTRACT_ADDRESS=$(get_erc20_contract_address) + +network_started=0 + + +# Start network if it is not running +is_running=$(is_network_running) +if [ $is_running -eq 1 ]; then + echo "Test network is not running, starting it now..." + start_network + network_started=1 + sleep 5 # Warm up network +fi + + +# Make token transaction +echo "Executing test of transaction inclusion in certificate..." +topos_subnet_id=$(get_topos_subnet_id) +echo "Topos subnet id is $topos_subnet_id" +tx_hash=$(send_token_with_retry 3 $topos_subnet_id $INCAL_HOST_PORT "txhash") +transaction_valid=$? +echo "Transaction send token result: $transaction_valid, tx hash: $tx_hash" +if [ $transaction_valid -eq 1 ]; then + echo "Transaction send token failed, shutting down network started for this test" + if [ $network_started -eq 1 ]; then + echo "Shutting down network started for this test" + stop_network + fi + exit 1 +fi + +# Check if transaction is registered by the sequencer +# Wait multiple times to give time to sequencer to catch up/create the certificates +for i in {1..10}; +do + transaction_in_certificate=$(get_transaction_in_certificate) + echo "Last transaction registered by sequencer: $transaction_in_certificate" + if [ "$transaction_in_certificate" == "$tx_hash" ]; then + echo "Transaction is registered by the sequencer" + break + else + echo "Transaction is not registered by the sequncer" + if [[ "$i" != '10' ]]; then + echo "Trying again in 5 seconds for $(($i+1)) time" + sleep 5 + continue + fi + if [ $network_started -eq 1 ]; then + echo "Test failed, shutting down network started for this test" + stop_network + fi + exit 1 + fi +done + + +# Stop network if started for this test +if [ $network_started -eq 1 ]; then + echo "Shutting down network started for this test" + stop_network +fi diff --git a/tests/utils.sh b/tests/utils.sh index 1ffaa0a..1a003f6 100644 --- a/tests/utils.sh +++ b/tests/utils.sh @@ -49,3 +49,59 @@ function graphql_get_tce_certificate () { TCE_CERT=$(curl -X POST -H "Content-Type: application/json" -d "$query" $2 | jq -r '.data.certificate.id') echo $TCE_CERT } + + +function get_incal_subnet_id() +{ + echo $(docker compose logs incal-sequencer | grep "for subnet id 0x" | awk -F 'for subnet id ' '{print $2}') +} + +function get_topos_subnet_id() +{ + echo $(docker compose logs topos-sequencer | grep "for subnet id 0x" | awk -F 'for subnet id ' '{print $2}') +} + + +function send_token_with_retry() +{ + # $1 - number of retries + # $2 - target subnet id + # $3 - host port of the edge node to send transaction from + # $4 - return value type + arbitrary_receiver_address="0xF472626faeb65277342e07962a8333A735934554" + amount="1" + for i in $(seq 1 $1); + do + result=$(npx ts-node $LOCAL_ERC20_HOME/scripts/send-token http://localhost:$3 $PRIVATE_KEY $2 $arbitrary_receiver_address $amount $4) + if [ -z "$result" ]; then + echo "Transaction failed, trying again in 5 seconds for $(($i+1)) time" + if [ $i -eq $1 ]; then + return 1 + fi + sleep 5 + continue + fi + echo "$result" + break + done +} + +function check_artifacts() +{ + if [ ! -d $LOCAL_ERC20_HOME/artifacts ] || [ -z "$(ls -A $LOCAL_ERC20_HOME/artifacts)" ]; then + $LOCAL_ERC20_HOME/scripts/copy_contract_artifacts.sh + fi +} + +function get_erc20_contract_address() +{ + echo $(docker compose logs contracts-topos | grep "ERC20_MESSAGING_CONTRACT_ADDRESS" | \ + awk -F 'ERC20_MESSAGING_CONTRACT_ADDRESS=' '{print $2}') +} + +function get_topos_core_proxy_contract_address() +{ + echo $(docker compose logs contracts-topos | grep "TOPOS_CORE_PROXY_CONTRACT_ADDRESS" | \ + awk -F 'TOPOS_CORE_PROXY_CONTRACT_ADDRESS=' '{print $2}') +} +