Skip to content

Commit

Permalink
test: withdrawal from L2 to L1 (#126)
Browse files Browse the repository at this point in the history
- L2 to L1 withdrawal test
- Refactor tests to reduce complexity
- Bump kurtosis-cdk version to v0.2.15
- Improve CI by dumping enclave logs + files in kurtosis
  • Loading branch information
vcastellm authored Oct 24, 2024
1 parent 9e5bea7 commit 3a35c8e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 54 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/test-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
with:
repository: 0xPolygon/kurtosis-cdk
path: "kurtosis-cdk"
ref: "v0.2.14"
ref: "v0.2.15"

- name: Setup Bats and bats libs
uses: bats-core/[email protected]
Expand All @@ -81,3 +81,22 @@ jobs:
env:
KURTOSIS_FOLDER: ${{ github.workspace }}/kurtosis-cdk
BATS_LIB_PATH: /usr/lib/

- name: Dump enclave logs
if: failure()
run: kurtosis dump ./dump

- name: Generate archive name
if: failure()
run: |
archive_name="dump_run_with_args_${{matrix.e2e-group}}_${{ github.run_id }}"
echo "ARCHIVE_NAME=${archive_name}" >> "$GITHUB_ENV"
echo "Generated archive name: ${archive_name}"
kurtosis service exec cdk cdk-node-001 'cat /etc/cdk/cdk-node-config.toml' > ./dump/cdk-node-config.toml
- name: Upload logs
if: failure()
uses: actions/upload-artifact@v4
with:
name: ${{ env.ARCHIVE_NAME }}
path: ./dump
85 changes: 57 additions & 28 deletions test/bridge-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@ setup() {
bridge_default_address=$(echo "$combined_json_output" | jq -r .polygonZkEVMBridgeAddress)
BRIDGE_ADDRESS=$bridge_default_address
fi

echo "Bridge address=$BRIDGE_ADDRESS" >&3

readonly sender_private_key=${SENDER_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
readonly sender_addr="$(cast wallet address --private-key $sender_private_key)"
destination_net=${DESTINATION_NET:-"1"}
destination_addr=${DESTINATION_ADDRESS:-"0x0bb7AA0b4FdC2D2862c088424260e99ed6299148"}
ether_value=${ETHER_VALUE:-"0.0200000054"}
amount=$(cast to-wei $ether_value ether)
token_addr=${TOKEN_ADDRESS:-"0x0000000000000000000000000000000000000000"}
readonly native_token_addr=${NATIVE_TOKEN_ADDRESS:-"0x0000000000000000000000000000000000000000"}
if [[ -n "$GAS_TOKEN_ADDR" ]]; then
echo "Using provided GAS_TOKEN_ADDR: $GAS_TOKEN_ADDR" >&3
gas_token_addr="$GAS_TOKEN_ADDR"
else
echo "GAS_TOKEN_ADDR not provided, retrieving from rollup parameters file." >&3
readonly rollup_params_file=/opt/zkevm/create_rollup_parameters.json
run bash -c "$contracts_service_wrapper 'cat $rollup_params_file' | tail -n +2 | jq -r '.gasTokenAddress'"
assert_success
assert_output --regexp "0x[a-fA-F0-9]{40}"
gas_token_addr=$output
fi
readonly is_forced=${IS_FORCED:-"true"}
readonly bridge_addr=$BRIDGE_ADDRESS
readonly meta_bytes=${META_BYTES:-"0x"}
Expand All @@ -30,47 +41,39 @@ setup() {
readonly bridge_api_url=${BRIDGE_API_URL:-"$(kurtosis port print $enclave zkevm-bridge-service-001 rpc)"}

readonly dry_run=${DRY_RUN:-"false"}
readonly sender_addr="$(cast wallet address --private-key $sender_private_key)"
readonly l1_rpc_network_id=$(cast call --rpc-url $l1_rpc_url $bridge_addr 'networkID() (uint32)')
readonly l2_rpc_network_id=$(cast call --rpc-url $l2_rpc_url $bridge_addr 'networkID() (uint32)')
gas_price=$(cast gas-price --rpc-url "$l2_rpc_url")
readonly weth_token_addr=$(cast call --rpc-url $l2_rpc_url $bridge_addr 'WETHToken()' | cast parse-bytes32-address)
}

@test "Run deposit" {
@test "Native gas token deposit to WETH" {
local initial_receiver_balance=$(cast call --rpc-url "$l2_rpc_url" "$weth_token_addr" "$balance_of_fn_sig" "$destination_addr" | awk '{print $1}')
echo "Initial receiver balance of native token on L2 $initial_receiver_balance" >&3

echo "Running LxLy deposit" >&3
run deposit
run bridgeAsset "$native_token_addr" "$l1_rpc_url"
assert_success
assert_output --partial 'transactionHash'
}

@test "Run claim" {
echo "Running LxLy claim" >&3

timeout="120"
claim_frequency="10"
run wait_for_claim "$timeout" "$claim_frequency"
run wait_for_claim "$timeout" "$claim_frequency" "$l2_rpc_url"
assert_success
}

@test "Custom native token transfer" {
# Use GAS_TOKEN_ADDR if provided, otherwise retrieve from file
if [[ -n "$GAS_TOKEN_ADDR" ]]; then
echo "Using provided GAS_TOKEN_ADDR: $GAS_TOKEN_ADDR" >&3
local gas_token_addr="$GAS_TOKEN_ADDR"
else
echo "GAS_TOKEN_ADDR not provided, retrieving from rollup parameters file." >&3
readonly rollup_params_file=/opt/zkevm/create_rollup_parameters.json
run bash -c "$contracts_service_wrapper 'cat $rollup_params_file' | tail -n +2 | jq -r '.gasTokenAddress'"
assert_success
assert_output --regexp "0x[a-fA-F0-9]{40}"
local gas_token_addr=$output
run verify_balance "$l2_rpc_url" "$weth_token_addr" "$destination_addr" "$initial_receiver_balance" "$ether_value"
if [ $status -eq 0 ]; then
break
fi
assert_success
}

@test "Custom gas token deposit" {
echo "Gas token addr $gas_token_addr, L1 RPC: $l1_rpc_url" >&3

# Set receiver address and query for its initial native token balance on the L2
receiver=${RECEIVER:-"0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"}
local initial_receiver_balance=$(cast balance --ether "$receiver" --rpc-url "$l2_rpc_url")
local initial_receiver_balance=$(cast balance "$receiver" --rpc-url "$l2_rpc_url")
echo "Initial receiver balance of native token on L2 $initial_receiver_balance" >&3

# Query for initial sender balance
Expand Down Expand Up @@ -106,20 +109,46 @@ setup() {
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"

# Deposit
token_addr=$gas_token_addr
destination_addr=$receiver
destination_net=$l2_rpc_network_id
amount=$wei_amount
run deposit
run bridgeAsset "$gas_token_addr" "$l1_rpc_url"
assert_success

# Claim deposits (settle them on the L2)
timeout="120"
claim_frequency="10"
run wait_for_claim "$timeout" "$claim_frequency"
run wait_for_claim "$timeout" "$claim_frequency" "$l2_rpc_url"
assert_success

# Validate that the native token of receiver on L2 has increased by the bridge tokens amount
run verify_native_token_balance "$l2_rpc_url" "$receiver" "$initial_receiver_balance" "$tokens_amount"
run verify_balance "$l2_rpc_url" "$native_token_addr" "$receiver" "$initial_receiver_balance" "$tokens_amount"
assert_success
}

@test "Custom gas token withdrawal" {
echo "Running LxLy withdrawal" >&3
echo "Gas token addr $gas_token_addr, L1 RPC: $l1_rpc_url" >&3

local initial_receiver_balance=$(cast call --rpc-url "$l1_rpc_url" "$gas_token_addr" "$balance_of_fn_sig" "$destination_addr" | awk '{print $1}')
assert_success
echo "Receiver balance of gas token on L1 $initial_receiver_balance" >&3

destination_net=$l1_rpc_network_id
run bridgeAsset "$native_token_addr" "$l2_rpc_url"
assert_success

# Claim withdrawals (settle them on the L1)
timeout="360"
claim_frequency="10"
destination_net=$l1_rpc_network_id
run wait_for_claim "$timeout" "$claim_frequency" "$l1_rpc_url"
assert_success

# Validate that the token of receiver on L1 has increased by the bridge tokens amount
run verify_balance "$l1_rpc_url" "$gas_token_addr" "$destination_addr" "$initial_receiver_balance" "$ether_value"
if [ $status -eq 0 ]; then
break
fi
assert_success
}
2 changes: 0 additions & 2 deletions test/combinations/fork11-rollup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ args:
zkevm_node_image: hermeznetwork/zkevm-node:v0.7.0-fork11-RC1
cdk_node_image: cdk
zkevm_use_gas_token_contract: true
additional_services:
- pless_zkevm_node
data_availability_mode: rollup
sequencer_type: erigon
2 changes: 1 addition & 1 deletion test/combinations/fork9-cdk-validium.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
args:
zkevm_contracts_image: leovct/zkevm-contracts:v6.0.0-rc.1-fork.9
zkevm_prover_image: hermeznetwork/zkevm-prover:v6.0.4
zkevm_prover_image: hermeznetwork/zkevm-prover:v6.0.6
cdk_erigon_node_image: hermeznetwork/cdk-erigon:v2.1.0
zkevm_node_image: hermeznetwork/zkevm-node:v0.7.3-RC1
cdk_validium_node_image: 0xpolygon/cdk-validium-node:0.7.0-cdk
Expand Down
24 changes: 14 additions & 10 deletions test/helpers/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,25 @@ function check_balances() {
fi
}

function verify_native_token_balance() {
local rpc_url="$1" # RPC URL
local account="$2" # account address
local initial_balance="$3" # initial balance in Ether (decimal)
local ether_amount="$4" # amount to be added (in Ether, decimal)

# Convert initial balance and amount to wei (no decimals)
local initial_balance_wei=$(cast --to-wei "$initial_balance")
function verify_balance() {
local rpc_url="$1" # RPC URL
local token_addr="$2" # gas token contract address
local account="$3" # account address
local initial_balance_wei="$4" # initial balance in Wei (decimal)
local ether_amount="$5" # amount to be added (in Ether, decimal)

# Trim 'ether' from ether_amount if it exists
ether_amount=$(echo "$ether_amount" | sed 's/ether//')
local amount_wei=$(cast --to-wei "$ether_amount")

# Get final balance in wei (after the operation)
local final_balance_wei=$(cast balance "$account" --rpc-url "$rpc_url" | awk '{print $1}')
local final_balance_wei
if [[ $token_addr == "0x0000000000000000000000000000000000000000" ]]; then
final_balance_wei=$(cast balance "$account" --rpc-url "$rpc_url" | awk '{print $1}')
else
final_balance_wei=$(cast call --rpc-url "$rpc_url" "$token_addr" "$balance_of_fn_sig" "$destination_addr" | awk '{print $1}')
fi
echo "Final balance of $account in $rpc_url: $final_balance_wei wei" >&3

# Calculate expected final balance (initial_balance + amount)
local expected_final_balance_wei=$(echo "$initial_balance_wei + $amount_wei" | bc)
Expand All @@ -317,7 +321,7 @@ function verify_native_token_balance() {
if [ "$(echo "$final_balance_wei == $expected_final_balance_wei" | bc)" -eq 1 ]; then
echo "✅ Balance verification successful: final balance is correct."
else
echo "❌ Balance verification failed: expected $expected_final_balance_wei but got $final_balance_wei."
echo "❌ Balance verification failed: expected $expected_final_balance_wei but got $final_balance_wei." >&3
exit 1
fi
}
Expand Down
31 changes: 19 additions & 12 deletions test/helpers/lxly-bridge-test.bash
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
#!/usr/bin/env bash
# Error code reference https://hackmd.io/WwahVBZERJKdfK3BbKxzQQ
function deposit() {
readonly deposit_sig='bridgeAsset(uint32,address,uint256,address,bool,bytes)'
function bridgeAsset() {
local token_addr="$1"
local rpc_url="$2"
readonly bridge_sig='bridgeAsset(uint32,address,uint256,address,bool,bytes)'

if [[ $token_addr == "0x0000000000000000000000000000000000000000" ]]; then
echo "The ETH balance for sender "$sender_addr":" >&3
cast balance -e --rpc-url $l1_rpc_url $sender_addr >&3
cast balance -e --rpc-url $rpc_url $sender_addr >&3
else
echo "The "$token_addr" token balance for sender "$sender_addr":" >&3
balance_wei=$(cast call --rpc-url "$l1_rpc_url" "$token_addr" "$balance_of_fn_sig" "$sender_addr")
echo "cast call --rpc-url $rpc_url $token_addr \"$balance_of_fn_sig\" $sender_addr" >&3
balance_wei=$(cast call --rpc-url "$rpc_url" "$token_addr" "$balance_of_fn_sig" "$sender_addr" | awk '{print $1}')
echo "$(cast --from-wei "$balance_wei")" >&3
fi

echo "Attempting to deposit $amount [wei] to $destination_addr, token $token_addr (sender=$sender_addr, network id=$destination_net, rpc url=$l1_rpc_url)" >&3
echo "Attempting to deposit $amount [wei] to $destination_addr, token $token_addr (sender=$sender_addr, network id=$destination_net, rpc url=$rpc_url)" >&3

if [[ $dry_run == "true" ]]; then
cast calldata $deposit_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes
cast calldata $bridge_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes
else
if [[ $token_addr == "0x0000000000000000000000000000000000000000" ]]; then
cast send --legacy --private-key $sender_private_key --value $amount --rpc-url $l1_rpc_url $bridge_addr $deposit_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes
echo "cast send --legacy --private-key $sender_private_key --value $amount --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes" >&3
cast send --legacy --private-key $sender_private_key --value $amount --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes
else
cast send --legacy --private-key $sender_private_key --rpc-url $l1_rpc_url $bridge_addr $deposit_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes
echo "cast send --legacy --private-key $sender_private_key --rpc-url $rpc_url $bridge_addr \"$bridge_sig\" $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes"
cast send --legacy --private-key $sender_private_key --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes
fi
fi
}

function claim() {
local destination_rpc_url="$1"
readonly claim_sig="claimAsset(bytes32[32],bytes32[32],uint256,bytes32,bytes32,uint32,address,uint32,address,uint256,bytes)"
readonly bridge_deposit_file=$(mktemp)
readonly claimable_deposit_file=$(mktemp)
echo "Getting full list of deposits" >&3
curl -s "$bridge_api_url/bridges/$destination_addr?limit=100&offset=0" | jq '.' | tee $bridge_deposit_file


echo "Looking for claimable deposits" >&3
jq '[.deposits[] | select(.ready_for_claim == true and .claim_tx_hash == "" and .dest_net == '$destination_net')]' $bridge_deposit_file | tee $claimable_deposit_file
readonly claimable_count=$(jq '. | length' $claimable_deposit_file)
Expand Down Expand Up @@ -69,16 +76,15 @@ function claim() {

if [[ $dry_run == "true" ]]; then
cast calldata $claim_sig "$in_merkle_proof" "$in_rollup_merkle_proof" $in_global_index $in_main_exit_root $in_rollup_exit_root $in_orig_net $in_orig_addr $in_dest_net $in_dest_addr $in_amount $in_metadata
cast call --rpc-url $l2_rpc_url $bridge_addr $claim_sig "$in_merkle_proof" "$in_rollup_merkle_proof" $in_global_index $in_main_exit_root $in_rollup_exit_root $in_orig_net $in_orig_addr $in_dest_net $in_dest_addr $in_amount $in_metadata
else
local comp_gas_price=$(bc -l <<< "$gas_price * 1.5" | sed 's/\..*//')
if [[ $? -ne 0 ]]; then
echo "Failed to calculate gas price" >&3
exit 1
fi

echo "cast send --legacy --gas-price $comp_gas_price --rpc-url $l2_rpc_url --private-key $sender_private_key $bridge_addr \"$claim_sig\" \"$in_merkle_proof\" \"$in_rollup_merkle_proof\" $in_global_index $in_main_exit_root $in_rollup_exit_root $in_orig_net $in_orig_addr $in_dest_net $in_dest_addr $in_amount $in_metadata" >&3
cast send --legacy --gas-price $comp_gas_price --rpc-url $l2_rpc_url --private-key $sender_private_key $bridge_addr "$claim_sig" "$in_merkle_proof" "$in_rollup_merkle_proof" $in_global_index $in_main_exit_root $in_rollup_exit_root $in_orig_net $in_orig_addr $in_dest_net $in_dest_addr $in_amount $in_metadata
echo "cast send --legacy --gas-price $comp_gas_price --rpc-url $destination_rpc_url --private-key $sender_private_key $bridge_addr \"$claim_sig\" \"$in_merkle_proof\" \"$in_rollup_merkle_proof\" $in_global_index $in_main_exit_root $in_rollup_exit_root $in_orig_net $in_orig_addr $in_dest_net $in_dest_addr $in_amount $in_metadata" >&3
cast send --legacy --gas-price $comp_gas_price --rpc-url $destination_rpc_url --private-key $sender_private_key $bridge_addr "$claim_sig" "$in_merkle_proof" "$in_rollup_merkle_proof" $in_global_index $in_main_exit_root $in_rollup_exit_root $in_orig_net $in_orig_addr $in_dest_net $in_dest_addr $in_amount $in_metadata
fi

done < <(seq 0 $((claimable_count - 1)))
Expand All @@ -87,6 +93,7 @@ function claim() {
function wait_for_claim() {
local timeout="$1" # timeout (in seconds)
local claim_frequency="$2" # claim frequency (in seconds)
local destination_rpc_url="$3" # destination rpc url
local start_time=$(date +%s)
local end_time=$((start_time + timeout))

Expand All @@ -97,7 +104,7 @@ function wait_for_claim() {
exit 1
fi

run claim
run claim $destination_rpc_url
if [ $status -eq 0 ]; then
break
fi
Expand Down

0 comments on commit 3a35c8e

Please sign in to comment.