Skip to content

Commit

Permalink
feat: add test for transaction inclusion in certificate (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
atanmarko committed Dec 19, 2023
1 parent 7b89864 commit b155b68
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 39 deletions.
5 changes: 2 additions & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -28,4 +28,3 @@ EXECUTOR_SERVICE_VERSION=1.0.0

# TESTS
export LOCAL_ERC20_HOME=./
export INCAL_HOST_PORT=20002
2 changes: 2 additions & 0 deletions contracts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions scripts/send-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ const DAILY_MINT_LIMIT = 100
const INITIAL_SUPPLY = 10_000_000

/// Usage:
/// ts-node ./scripts/send-token.ts <node endpoint> <sender private key> <receiver account> <amount>
/// ts-node ./scripts/send-token.ts <node endpoint> <sender private key> <receiver account> <amount> <return_value>
/// 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 || ''
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion tests/test_cert_delivery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ do
fi
exit 1
fi

done


Expand Down
39 changes: 7 additions & 32 deletions tests/test_cert_inclusion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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

Expand Down
73 changes: 73 additions & 0 deletions tests/test_transaction_in_certificate.sh
Original file line number Diff line number Diff line change
@@ -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
56 changes: 56 additions & 0 deletions tests/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
}

0 comments on commit b155b68

Please sign in to comment.