-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconflicting-contract-calls.bats
More file actions
137 lines (125 loc) · 5.24 KB
/
conflicting-contract-calls.bats
File metadata and controls
137 lines (125 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bats
# bats file_tags=standard,execution
setup_file() {
# shellcheck source=core/helpers/common.bash
source "$BATS_TEST_DIRNAME/../../core/helpers/common.bash"
_setup_vars
}
setup() {
iteration_count=5
# source existing helper functions for ephemeral account setup
# shellcheck disable=SC1091
source "$BATS_TEST_DIRNAME/../lxly/bridge-tests-suite-assets/helpers/bridge-tests-helper.bash"
}
wait_block_increment() {
local wait_blocks="$1"
local timeout_seconds="$2"
start_block=$(cast block-number --rpc-url "$l2_rpc_url")
echo "[DEBUG]: starting block: $start_block" >&3
echo "[DEBUG]: waiting until: $((start_block + wait_blocks))" >&3
block_diff=0
start_time=$(date +%s)
while [[ $block_diff -lt $wait_blocks ]]; do
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [[ $elapsed_time -ge $timeout_seconds ]]; then
echo "ERROR: Timeout of ${timeout_seconds} seconds reached" >&3
return 1
fi
current_block=$(cast block-number --rpc-url "$l2_rpc_url")
echo "[DEBUG]: current block: $current_block" >&3
block_diff=$((current_block - start_block))
sleep 1
done
}
is_cdk_erigon() {
run cast rpc zkevm_getForkId --rpc-url "$l2_rpc_url"
if [[ "$status" -eq 0 ]]; then
return 0
else
return 1
fi
}
# bats test_tags=transaction-eoa,evm-pool
@test "Make conflicting contract calls" {
local ephemeral_data
local ephemeral_private_key
local ephemeral_address
ephemeral_data=$(_generate_ephemeral_account "conflicting-contract-calls")
ephemeral_private_key=$(echo "$ephemeral_data" | cut -d' ' -f1)
ephemeral_address=$(echo "$ephemeral_data" | cut -d' ' -f2)
echo "ephemeral_address: $ephemeral_address" >&3
# Fund the ephemeral account using imported function
_fund_ephemeral_account "$ephemeral_address" "$l2_rpc_url" "$l2_private_key" "10000000000000000"
index=0;
nonce=$(cast nonce --rpc-url "$l2_rpc_url" "$ephemeral_address")
gas_price=$(cast gas-price --rpc-url "$l2_rpc_url")
while true ; do
index=$((index+1));
if [[ $index -gt "$iteration_count" ]]; then
break;
fi
echo "[DEBUG]: cast send --nonce \"$nonce\" --rpc-url \"$l2_rpc_url\" --gas-limit 21000 --gas-price \"$gas_price\" --async --legacy --private-key \"$ephemeral_private_key\" --value $index 0x0000000000000000000000000000000000000000" >&2
# this should work
run cast send \
--nonce "$nonce" \
--rpc-url "$l2_rpc_url" \
--gas-limit 21000 \
--gas-price "$gas_price" \
--async \
--legacy \
--private-key "$ephemeral_private_key" \
--value $index \
0x0000000000000000000000000000000000000000
if [[ "$status" -ne 0 ]]; then
echo "Test $index expected success but failed: $output" >&2
return 1
fi
echo "Test $index transaction hash: $output" >&2
index=$((index+1));
echo "[DEBUG]: cast send --nonce \"$nonce\" --rpc-url \"$l2_rpc_url\" --gas-limit 21000 --gas-price \"$gas_price\" --async --legacy --private-key \"$ephemeral_private_key\" --value $index 0x0000000000000000000000000000000000000000" >&2
# this should fail
run cast send \
--nonce "$nonce" \
--rpc-url "$l2_rpc_url" \
--gas-limit 21000 \
--gas-price "$gas_price" \
--async \
--legacy \
--private-key "$ephemeral_private_key" \
--value $index \
0x0000000000000000000000000000000000000000
txn_hash=$output
txn_status=$status
nonce=$((nonce + 1));
# check if RPC client is using cdk-erigon
if is_cdk_erigon; then
# check if the command succeeded (exit code 0) but transaction failed (status 0 in output)
if [[ "$txn_status" -eq 0 ]]; then
# for cdk-erigon, even invalid transactions can exist in the pool for a short time before being rejected
# wait for 3 blocks and then recheck if the transaction hash exists
echo "[DEBUG]: cdk-erigon detected" >&2
# usage: wait_block_increment <number_of_blocks_to_wait> <timeout_in_seconds>
wait_block_increment 12 144
# command succeeded, now check if transaction failed
run cast tx "$txn_hash" --rpc-url "$l2_rpc_url"
if [[ "$status" -ne 0 ]]; then
echo "Transaction correctly failed as expected" >&3
else
echo "Test expected transaction to not exists but exists: $output" >&3
return 1
fi
else
# transaction fails immediately as expected
echo "Transaction correctly failed as expected" >&3
fi
else
# process normally for non-cdk-erigon clients
if [[ "$txn_status" -ne 1 ]]; then
echo "Test $index expected fail but succeeded: $txn_hash" >&2
return 1
fi
fi
done
for job in $(jobs -p); do wait "$job"; done
}