Skip to content

Commit

Permalink
test: Custom native gas token transfer E2E test (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal authored Sep 20, 2024
1 parent 681c987 commit a68d900
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 116 deletions.
33 changes: 15 additions & 18 deletions test/access-list-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ setup() {
load 'helpers/common'
_common_setup

readonly enclave=${ENCLAVE:-cdk-v1}
readonly sequencer=${KURTOSIS_NODE:-cdk-erigon-sequencer-001}
readonly node=${KURTOSIS_NODE:-cdk-erigon-node-001}
readonly rpc_url=${RPC_URL:-$(kurtosis port print "$enclave" "$node" http-rpc)}
readonly erigon_sequencer_node=${KURTOSIS_ERIGON_SEQUENCER:-cdk-erigon-sequencer-001}
readonly kurtosis_sequencer_wrapper=${KURTOSIS_SEQUENCER_WRAPPER:-"kurtosis service exec $enclave $erigon_sequencer_node"}
readonly key=${SENDER_key:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
readonly receiver=${RECEIVER:-"0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"}
readonly data_dir=${ACL_DATA_DIR:-"/home/erigon/data/dynamic-kurtosis-sequencer/txpool/acls"}
readonly kurtosis_sequencer_wrapper=${KURTOSIS_WRAPPER:-"kurtosis service exec $enclave $sequencer"}
}

teardown() {
Expand All @@ -36,7 +33,7 @@ set_acl_mode() {
@test "Test Block List - Sending regular transaction when address not in block list" {
local value="10ether"
run set_acl_mode "blocklist"
run sendTx $key $receiver $value
run sendTx $l2_rpc_url $key $receiver $value

assert_success
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"
Expand All @@ -45,7 +42,7 @@ set_acl_mode() {
@test "Test Block List - Sending contracts deploy transaction when address not in block list" {
local contract_artifact="./contracts/erc20mock/ERC20Mock.json"
run set_acl_mode "blocklist"
run deployContract $key $contract_artifact
run deployContract $l2_rpc_url $key $contract_artifact

assert_success

Expand All @@ -59,7 +56,7 @@ set_acl_mode() {
run set_acl_mode "blocklist"
run add_to_access_list "blocklist" "sendTx"

run sendTx $key $receiver $value
run sendTx $l2_rpc_url $key $receiver $value

assert_failure
assert_output --partial "sender disallowed to send tx by ACL policy"
Expand All @@ -70,7 +67,7 @@ set_acl_mode() {

run set_acl_mode "blocklist"
run add_to_access_list "blocklist" "deploy"
run deployContract $key $contract_artifact
run deployContract $l2_rpc_url $key $contract_artifact

assert_failure
assert_output --partial "sender disallowed to deploy contract by ACL policy"
Expand All @@ -80,7 +77,7 @@ set_acl_mode() {
local value="10ether"

run set_acl_mode "allowlist"
run sendTx $key $receiver $value
run sendTx $l2_rpc_url $key $receiver $value

assert_failure
assert_output --partial "sender disallowed to send tx by ACL policy"
Expand All @@ -90,7 +87,7 @@ set_acl_mode() {
local contract_artifact="./contracts/erc20mock/ERC20Mock.json"

run set_acl_mode "allowlist"
run deployContract $key $contract_artifact
run deployContract $l2_rpc_url $key $contract_artifact

assert_failure
assert_output --partial "sender disallowed to deploy contract by ACL policy"
Expand All @@ -99,20 +96,20 @@ set_acl_mode() {
@test "Test Allow List - Sending regular transaction when address is in allow list" {
local value="10ether"

run set_acl_mode "allowlist"
run add_to_access_list "allowlist" "sendTx"
run sendTx $key $receiver $value
run set_acl_mode "allowlist"
run add_to_access_list "allowlist" "sendTx"
run sendTx $l2_rpc_url $key $receiver $value

assert_success
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"
}

@test "Test Allow List - Sending contracts deploy transaction when address is in allow list" {
local contract_artifact="./contracts/erc20mock/ERC20Mock.json"

run set_acl_mode "allowlist"
run add_to_access_list "allowlist" "deploy"
run deployContract $key $contract_artifact
run set_acl_mode "allowlist"
run add_to_access_list "allowlist" "deploy"
run deployContract $l2_rpc_url $key $contract_artifact

assert_success

Expand Down
15 changes: 5 additions & 10 deletions test/basic-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ setup() {
load 'helpers/common'
_common_setup

readonly enclave=${ENCLAVE:-cdk-v1}
readonly node=${KURTOSIS_NODE:-cdk-erigon-node-001}
readonly rpc_url=${RPC_URL:-$(kurtosis port print "$enclave" "$node" http-rpc)}
readonly private_key=${SENDER_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
readonly sender_private_key=${SENDER_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
readonly receiver=${RECEIVER:-"0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"}
}

Expand All @@ -16,7 +13,7 @@ setup() {
local value="10ether"

# case 1: Transaction successful sender has sufficient balance
run sendTx "$private_key" "$receiver" "$value"
run sendTx "$l2_rpc_url" "$sender_private_key" "$receiver" "$value"
assert_success
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"

Expand All @@ -37,21 +34,19 @@ setup() {
local contract_artifact="./contracts/erc20mock/ERC20Mock.json"

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

# Mint ERC20 tokens
local mintFnSig="function mint(address receiver, uint256 amount)"
local amount="5"

run sendTx "$private_key" "$contract_addr" "$mintFnSig" "$receiver" "$amount"
run sendTx "$l2_rpc_url" "$sender_private_key" "$contract_addr" "$mint_fn_sig" "$receiver" "$amount"
assert_success
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"

# Assert that balance is correct
local balanceOfFnSig="function balanceOf(address) (uint256)"
run queryContract "$contract_addr" "$balanceOfFnSig" "$receiver"
run queryContract "$l2_rpc_url" "$contract_addr" "$balance_of_fn_sig" "$receiver"
assert_success
receiverBalance=$(echo "$output" | tail -n 1)

Expand Down
143 changes: 100 additions & 43 deletions test/bridge-e2e.bats
Original file line number Diff line number Diff line change
@@ -1,71 +1,128 @@
setup() {
load 'helpers/common-setup'
_common_setup
load 'helpers/common'
load 'helpers/lxly-bridge-test'

readonly data_availability_mode=${DATA_AVAILABILITY_MODE:-"cdk-validium"}
$PROJECT_ROOT/test/scripts/kurtosis_prepare_params_yml.sh ../kurtosis-cdk $data_availability_mode
[ $? -ne 0 ] && echo "Error preparing params.yml" && exit 1

# Check if the genesis file is already downloaded
if [ ! -f "./tmp/cdk/genesis/genesis.json" ]; then
mkdir -p ./tmp/cdk
kurtosis files download cdk-v1 genesis ./tmp/cdk/genesis
[ $? -ne 0 ] && echo "Error downloading genesis file" && exit 1
if [ -z "$BRIDGE_ADDRESS" ]; then
local combined_json_file="/opt/zkevm/combined.json"
echo "BRIDGE_ADDRESS env variable is not provided, resolving the bridge address from the Kurtosis CDK '$combined_json_file'" >&3

# Fetching the combined JSON output and filtering to get polygonZkEVMBridgeAddress
combined_json_output=$($contracts_service_wrapper "cat $combined_json_file" | tail -n +2)
bridge_default_address=$(echo "$combined_json_output" | jq -r .polygonZkEVMBridgeAddress)
BRIDGE_ADDRESS=$bridge_default_address
fi
# Download the genesis file
readonly bridge_default_address=$(jq -r ".genesis[] | select(.contractName == \"PolygonZkEVMBridge proxy\") | .address" ./tmp/cdk/genesis/genesis.json)

readonly skey=${SENDER_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
readonly destination_net=${DESTINATION_NET:-"1"}
readonly destination_addr=${DESTINATION_ADDRESS:-"0x0bb7AA0b4FdC2D2862c088424260e99ed6299148"}
readonly ether_value=${ETHER_VALUE:-"0.0200000054"}
readonly token_addr=${TOKEN_ADDRESS:-"0x0000000000000000000000000000000000000000"}

echo "Bridge address=$BRIDGE_ADDRESS" >&3

readonly sender_private_key=${SENDER_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
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 is_forced=${IS_FORCED:-"true"}
readonly bridge_addr=${BRIDGE_ADDRESS:-$bridge_default_address}
readonly bridge_addr=$BRIDGE_ADDRESS
readonly meta_bytes=${META_BYTES:-"0x"}

readonly l1_rpc_url=${L1_ETH_RPC_URL:-"$(kurtosis port print cdk-v1 el-1-geth-lighthouse rpc)"}
readonly l2_rpc_url=${L2_ETH_RPC_URL:-"$(kurtosis port print cdk-v1 cdk-erigon-node-001 http-rpc)"}
readonly bridge_api_url=${BRIDGE_API_URL:-"$(kurtosis port print cdk-v1 zkevm-bridge-service-001 rpc)"}
readonly l1_rpc_url=${L1_ETH_RPC_URL:-"$(kurtosis port print $enclave el-1-geth-lighthouse rpc)"}
readonly bridge_api_url=${BRIDGE_API_URL:-"$(kurtosis port print $enclave zkevm-bridge-service-001 rpc)"}

readonly dry_run=${DRY_RUN:-"false"}

readonly amount=$(cast to-wei $ether_value ether)
readonly current_addr="$(cast wallet address --private-key $skey)"
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)')
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)')
}

@test "Run deposit" {
load 'helpers/lxly-bridge-test'
echo "Running LxLy deposit" >&3
run deposit
assert_success
assert_output --partial 'transactionHash'
}

@test "Run claim" {
load 'helpers/lxly-bridge-test'
echo "Running LxLy claim"
echo "Running LxLy claim" >&3

# The script timeout (in seconds).
timeout="120"
start_time=$(date +%s)
end_time=$((start_time + timeout))

while true; do
current_time=$(date +%s)
if ((current_time > end_time)); then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ❌ Exiting... Timeout reached!"
exit 1
fi

run claim
if [ $status -eq 0 ]; then
break
fi
sleep 10
done

claim_frequency="10"
run wait_for_claim "$timeout" "$claim_frequency"
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
fi

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")
echo "Initial receiver balance of native token on L2 $initial_receiver_balance" >&3

# Query for initial sender balance
run queryContract "$l1_rpc_url" "$gas_token_addr" "$balance_of_fn_sig" "$sender_addr"
assert_success
local gas_token_init_sender_balance=$(echo "$output" | tail -n 1 | awk '{print $1}')
echo "Initial sender balance $gas_token_init_sender_balance" of gas token on L1 >&3

# Mint gas token on L1
local tokens_amount="0.1ether"
local wei_amount=$(cast --to-unit $tokens_amount wei)
local minter_key=${MINTER_KEY:-"42b6e34dc21598a807dc19d7784c71b2a7a01f6480dc6f58258f78e539f1a1fa"}
run mint_erc20_tokens "$l1_rpc_url" "$gas_token_addr" "$minter_key" "$sender_addr" "$tokens_amount"
assert_success

# Assert that balance of gas token (on the L1) is correct
run queryContract "$l1_rpc_url" "$gas_token_addr" "$balance_of_fn_sig" "$sender_addr"
assert_success
local gas_token_final_sender_balance=$(echo "$output" |
tail -n 1 |
awk '{print $1}')
local expected_balance=$(echo "$gas_token_init_sender_balance + $wei_amount" |
bc |
awk '{print $1}')

echo "Sender balance ($sender_addr) (gas token L1): $gas_token_final_sender_balance" >&3
assert_equal "$gas_token_final_sender_balance" "$expected_balance"

# Send approve transaction to the gas token on L1
deposit_ether_value="0.1ether"
run sendTx "$l1_rpc_url" "$sender_private_key" "$gas_token_addr" "$approve_fn_sig" "$bridge_addr" "$deposit_ether_value"
assert_success
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
assert_success

# Claim deposits (settle them on the L2)
timeout="120"
claim_frequency="10"
run wait_for_claim "$timeout" "$claim_frequency"
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"
assert_success
}
17 changes: 15 additions & 2 deletions test/helpers/common-setup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@
_common_setup() {
bats_load_library 'bats-support'
bats_load_library 'bats-assert'

# get the containing directory of this file
# use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0,
# as those will point to the bats executable's location or the preprocessed file respectively
PROJECT_ROOT="$( cd "$( dirname "$BATS_TEST_FILENAME" )/.." >/dev/null 2>&1 && pwd )"
PROJECT_ROOT="$(cd "$(dirname "$BATS_TEST_FILENAME")/.." >/dev/null 2>&1 && pwd)"
# make executables in src/ visible to PATH
PATH="$PROJECT_ROOT/src:$PATH"

# ERC20 contracts function signatures
readonly mint_fn_sig="function mint(address,uint256)"
readonly balance_of_fn_sig="function balanceOf(address) (uint256)"
readonly approve_fn_sig="function approve(address,uint256)"


# Kurtosis enclave and service identifiers
readonly enclave=${KURTOSIS_ENCLAVE:-cdk-v1}
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-node-001}
readonly l2_rpc_url=${L2_ETH_RPC_URL:-"$(kurtosis port print $enclave $erigon_rpc_node http-rpc)"}
}
Loading

0 comments on commit a68d900

Please sign in to comment.