Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test runner hardening for local (mac) #8

Merged
merged 7 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/helpers/common-setup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ _common_setup() {
readonly contracts_container=${KURTOSIS_CONTRACTS:-contracts-001}
readonly contracts_service_wrapper=${KURTOSIS_CONTRACTS_WRAPPER:-"kurtosis service exec $enclave $contracts_container"}
readonly erigon_rpc_node=${KURTOSIS_ERIGON_RPC:-cdk-erigon-rpc-001}
readonly l2_rpc_url=${L2_ETH_RPC_URL:-"$(kurtosis port print $enclave $erigon_rpc_node rpc)"}
readonly l2_rpc_url=${L2_RPC_URL:-"$(kurtosis port print $enclave $erigon_rpc_node rpc)"}
}
65 changes: 50 additions & 15 deletions core/helpers/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function deploy_contract() {
local rpc_url="$1"
local private_key="$2"
local contract_artifact="$3"
contract_artifact="$3"

# Check if rpc_url is available
if [[ -z "$rpc_url" ]]; then
Expand Down Expand Up @@ -56,8 +56,9 @@ function deploy_contract() {
echo "Deploy contract output:" >&3
echo "$cast_output" >&3

# Extract the contract address from the output
local deployed_contract_address=$(echo "$cast_output" | grep 'contractAddress' | sed 's/contractAddress\s\+//')
# Extract the contract address from the output using updated regex
local deployed_contract_address=$(echo "$cast_output" | grep -o 'contractAddress\s\+\(0x[a-fA-F0-9]\{40\}\)' | sed 's/contractAddress\s\+//')
local deployed_contract_address=$(echo "$deployed_contract_address" | sed -E 's/^contractAddress[[:space:]]+//')
echo "Deployed contract address: $deployed_contract_address" >&3

if [[ -z "$deployed_contract_address" ]]; then
Expand Down Expand Up @@ -259,35 +260,69 @@ function check_balances() {

# Transaction hash regex: 0x followed by 64 hexadecimal characters
if [[ ! "$tx_hash" =~ ^0x[a-fA-F0-9]{64}$ ]]; then
echo "Error: Invalid transaction hash: $tx_hash".
echo "Error: Invalid transaction hash: $tx_hash"
return 1
fi

local sender_final_balance=$(cast balance "$sender" --ether --rpc-url "$rpc_url") || return 1
local tx_output=$(cast tx "$tx_hash" --rpc-url "$rpc_url")
local gas_used=$(tx_output | grep '^gas ' | awk '{print $2}')
local gas_price=$(tx_output | grep '^gasPrice' | awk '{print $2}')
local gas_fee=$(echo "$gas_used * $gas_price" | bc)
local gas_fee_in_ether=$(cast to-unit "$gas_fee" ether)
local sender_final_balance=$(cast balance "$sender" --ether --rpc-url "$l2_rpc_url") || return 1
echo "Sender final balance: '$sender_final_balance' wei"
echo "RPC url: '$l2_rpc_url'"

local sender_balance_change=$(echo "$sender_initial_balance - $sender_final_balance" | bc)
# Capture transaction output
local tx_output
tx_output=$(cast tx "$tx_hash" --rpc-url "$l2_rpc_url")
if [[ $? -ne 0 ]]; then
echo "Error: Failed to fetch transaction details"
echo "$tx_output"
return 1
fi

# Debugging tx_output
echo "Transaction output: $tx_output"

# Parse gas used and gas price from tx_output using updated regex
local gas_used
gas_used=$(echo "$tx_output" | grep -Eo "gas\s+[0-9]+" | awk '{print $2}')
local gas_price
gas_price=$(echo "$tx_output" | grep -Eo "gasPrice\s+[0-9]+" | awk '{print $2}')

# Check if gas_used and gas_price are found
if [[ -z "$gas_used" || -z "$gas_price" ]]; then
echo "Error: Gas used or gas price not found in transaction output."
return 1
fi

echo "Gas used: $gas_used"
echo "Gas price: $gas_price"

local gas_fee
gas_fee=$(echo "$gas_used * $gas_price" | bc)
local gas_fee_in_ether
gas_fee_in_ether=$(cast to-unit "$gas_fee" ether)

local sender_balance_change
sender_balance_change=$(echo "$sender_initial_balance - $sender_final_balance" | bc)
echo "Sender balance changed by: '$sender_balance_change' wei"
echo "Gas fee paid: '$gas_fee_in_ether' ether"

local receiver_final_balance=$(cast balance "$receiver" --ether --rpc-url "$rpc_url") || return 1
local receiver_balance_change=$(echo "$receiver_final_balance - $receiver_initial_balance" | bc)
local receiver_final_balance
receiver_final_balance=$(cast balance "$receiver" --ether --rpc-url "$l2_rpc_url") || return 1
local receiver_balance_change
receiver_balance_change=$(echo "$receiver_final_balance - $receiver_initial_balance" | bc)
echo "Receiver balance changed by: '$receiver_balance_change' wei"

# Trim 'ether' suffix from amount to get the numeric part
local value_in_ether=$(echo "$amount" | sed 's/ether$//')
local value_in_ether
value_in_ether=$(echo "$amount" | sed 's/ether$//')

if ! echo "$receiver_balance_change == $value_in_ether" | bc -l; then
echo "Error: receiver balance updated incorrectly. Expected: $value_in_ether, Actual: $receiver_balance_change"
return 1
fi

# Calculate expected sender balance change
local expected_sender_change=$(echo "$value_in_ether + $gas_fee_in_ether" | bc)
local expected_sender_change
expected_sender_change=$(echo "$value_in_ether + $gas_fee_in_ether" | bc)
if ! echo "$sender_balance_change == $expected_sender_change" | bc -l; then
echo "Error: sender balance updated incorrectly. Expected: $expected_sender_change, Actual: $sender_balance_change"
return 1
Expand Down
9 changes: 6 additions & 3 deletions core/helpers/run-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ fi
echo "Running tests for network: $NETWORK"
echo "Using GAS_TOKEN_ADDR: $GAS_TOKEN_ADDR"

l2_rpc_url="http://127.0.0.1:59761"
if [[ "$DEPLOY_INFRA" == "true" ]]; then
echo "Deploying infrastructure using Kurtosis..."

Expand All @@ -45,12 +44,16 @@ if [[ "$DEPLOY_INFRA" == "true" ]]; then
cp "$BASE_FOLDER/config/kurtosis-cdk-node-config.toml.template" "$KURTOSIS_FOLDER/templates/trusted-node/cdk-node-config.toml"

kurtosis run --enclave cdk --args-file "combinations/${NETWORK}.yml" --image-download always "$KURTOSIS_FOLDER"
l2_rpc_url="$(kurtosis port print cdk cdk-erigon-rpc-001 rpc)"
L2_RPC_URL="$(kurtosis port print cdk cdk-erigon-rpc-001 rpc)"
else
echo "Skipping infrastructure deployment. Ensure the required services are already running!"
fi

export L2_RPC_URL="${L2_RPC_URL:-$l2_rpc_url}"
# Check if L2_RPC_URL is empty or not set
if [[ -z "$L2_RPC_URL" ]]; then
echo "Error: L2_RPC_URL is a required environment variable. Please update the .env file."
exit 1 # Exit the script with an error code
fi

# Run selected tests with exported environment variables
if [[ "$BATS_TESTS" == "all" ]]; then
Expand Down
10 changes: 7 additions & 3 deletions test-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ while [[ "$#" -gt 0 ]]; do
done

# 🔍 Set infra
l2_rpc_url="http://127.0.0.1:59761"
if [[ "$DEPLOY_INFRA" == "true" ]]; then
echo "⏳ Deploying infrastructure using Kurtosis..."

Expand All @@ -78,11 +77,16 @@ if [[ "$DEPLOY_INFRA" == "true" ]]; then
cp "$PROJECT_ROOT/core/helpers/config/kurtosis-cdk-node-config.toml.template" "$KURTOSIS_FOLDER/templates/trusted-node/cdk-node-config.toml"

kurtosis run --enclave cdk --args-file "$PROJECT_ROOT/core/helpers/combinations/${NETWORK}.yml" --image-download always "$KURTOSIS_FOLDER"
l2_rpc_url="$(kurtosis port print cdk cdk-erigon-rpc-001 rpc)"
L2_RPC_URL="$(kurtosis port print cdk cdk-erigon-rpc-001 rpc)"
else
echo "⏩ Skipping infrastructure deployment. Ensure the required services are already running!"
fi
export L2_RPC_URL="${L2_RPC_URL:-$l2_rpc_url}"

# Check if L2_RPC_URL is empty or not set
if [[ -z "$L2_RPC_URL" ]]; then
echo "Error: L2_RPC_URL is a required environment variable. Please update the .env file."
exit 1 # Exit the script with an error code
fi

# 🔍 Set BATS test files
echo "🚀 Running tests with tags: $FILTER_TAGS"
Expand Down
1 change: 1 addition & 0 deletions tests/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
KURTOSIS_ENCLAVE=cdk
TMP_CDK_FOLDER=tmp/cdk
USE_L1_GAS_TOKEN_CONTRACT=true
L2_RPC_URL="http://127.0.0.1:53015"
L2_SENDER_PRIVATE_KEY=0x12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625
3 changes: 2 additions & 1 deletion tests/light/eoa-transaction.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ setup() {
_common_setup

readonly sender_private_key=${SENDER_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
readonly receiver=${RECEIVER:-"0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"}

}

# bats test_tags=light,eoa
@test "Send EOA transaction" {
local receiver=${RECEIVER:-"0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"}
local sender_addr=$(cast wallet address --private-key "$sender_private_key")
local initial_nonce=$(cast nonce "$sender_addr" --rpc-url "$l2_rpc_url") || {
echo "Failed to retrieve nonce for sender: $sender_addr using RPC URL: $l2_rpc_url"
Expand Down
64 changes: 31 additions & 33 deletions tests/light/erc20-tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,38 @@ setup() {
_common_setup

readonly sender_private_key=${SENDER_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
readonly contract_artifact="./core/contracts/erc20mock/ERC20Mock.json"
contract_artifact="./core/contracts/erc20mock/ERC20Mock.json"
}

# bats test_tags=light,erc20
@test "Test ERC20Mock contract" {
# wallet_A_output=$(cast wallet new)
# address_A=$(echo "$wallet_A_output" | grep "Address" | awk '{print $2}')
# address_A_private_key=$(echo "$wallet_A_output" | grep "Private key" | awk '{print $3}')
# address_B=$(cast wallet new | grep "Address" | awk '{print $2}')

# # Deploy ERC20Mock
# run deploy_contract "$l2_rpc_url" "$sender_private_key" "$contract_artifact"
echo $(pwd)
cat "$contract_artifact"
# assert_success
# contract_addr=$(echo "$output" | tail -n 1)

# # Mint ERC20 tokens
# local amount="5"
# run send_tx "$l2_rpc_url" "$sender_private_key" "$contract_addr" "$mint_fn_sig" "$address_A" "$amount"
# assert_success
# assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"

# # Insufficient gas scenario (should fail)
# local bytecode=$(jq -r .bytecode "$contract_artifact")
# [[ -z "$bytecode" || "$bytecode" == "null" ]] && { echo "Error: Failed to read bytecode"; return 1; }

# local gas_units=$(cast estimate --rpc-url "$l2_rpc_url" --create "$bytecode")
# gas_units=$(echo "scale=0; $gas_units / 2" | bc)
# local gas_price=$(cast gas-price --rpc-url "$l2_rpc_url")
# local value=$(echo "$gas_units * $gas_price" | bc)
# local value_ether=$(cast to-unit "$value" ether)"ether"

# cast send --rpc-url "$l2_rpc_url" --private-key "$sender_private_key" "$address_A" --value "$value_ether" --legacy
# run deploy_contract "$l2_rpc_url" "$address_A_private_key" "$contract_artifact"
# assert_failure
}
wallet_A_output=$(cast wallet new)
address_A=$(echo "$wallet_A_output" | grep "Address" | awk '{print $2}')
address_A_private_key=$(echo "$wallet_A_output" | grep "Private key" | awk '{print $3}')
address_B=$(cast wallet new | grep "Address" | awk '{print $2}')

# Deploy ERC20Mock
run deploy_contract "$l2_rpc_url" "$sender_private_key" "$contract_artifact"
assert_success
contract_addr=$(echo "$output" | tail -n 1)

# Mint ERC20 tokens
local amount="5"
run send_tx "$l2_rpc_url" "$sender_private_key" "$contract_addr" "$mint_fn_sig" "$address_A" "$amount"
assert_success
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"

# Insufficient gas scenario (should fail)
local bytecode=$(jq -r .bytecode "$contract_artifact")
[[ -z "$bytecode" || "$bytecode" == "null" ]] && { echo "Error: Failed to read bytecode"; return 1; }

local gas_units=$(cast estimate --rpc-url "$l2_rpc_url" --create "$bytecode")
gas_units=$(echo "scale=0; $gas_units / 2" | bc)
local gas_price=$(cast gas-price --rpc-url "$l2_rpc_url")
local value=$(echo "$gas_units * $gas_price" | bc)
local value_ether=$(cast to-unit "$value" ether)"ether"

cast send --rpc-url "$l2_rpc_url" --private-key "$sender_private_key" "$address_A" --value "$value_ether" --legacy
run deploy_contract "$l2_rpc_url" "$address_A_private_key" "$contract_artifact"
assert_failure
}
Loading