-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsimple-validations.bats
More file actions
200 lines (168 loc) · 7.23 KB
/
simple-validations.bats
File metadata and controls
200 lines (168 loc) · 7.23 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env bats
# bats file_tags=evm-rpc
setup() {
rpc_url=${L2_RPC_URL:-"$(kurtosis port print cdk cdk-erigon-rpc-001 rpc)"}
# bridge_service_url=${BRIDGE_SERVICE_URL:-"$(kurtosis port print cdk zkevm-bridge-service-001 rpc)"}
private_key=${L2_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
eth_address=$(cast wallet address --private-key "$private_key")
export ETH_RPC_URL="$rpc_url"
}
# bats test_tags=evm-gas,transaction-eoa
@test "send and sweep account with precise gas calculation" {
wallet_info=$(cast wallet new --json | jq '.[0]')
tmp_address=$(echo "$wallet_info" | jq -r '.address')
tmp_private_key=$(echo "$wallet_info" | jq -r '.private_key')
# Send 0.01 ETH to the new address
cast send \
--value "10000000000000000" \
--private-key "$private_key" "$tmp_address"
gas_price=$(cast gas-price)
gas_price=$(bc <<< "$gas_price * 2")
value_to_return=$(bc <<< "10000000000000000 - (21000 * $gas_price)")
echo "Attempting to return $value_to_return wei based on gas price $gas_price and gas limit of 21,000"
cast send \
--gas-price "$gas_price" \
--gas-limit 21000 \
--value "$value_to_return" \
--private-key "$tmp_private_key" "$eth_address"
}
# bats test_tags=transaction-eoa
@test "send zero priced transactions and confirm rejection" {
if cast send --legacy --gas-price 0 --value "1" --private-key "$private_key" 0x0000000000000000000000000000000000000000 ; then
echo "A zero priced legacy transaction was mined"
exit 1
fi
if cast send --gas-price 0 --value "1" --private-key "$private_key" 0x0000000000000000000000000000000000000000 ; then
echo "A zero priced eip-1559 transaction was mined"
exit 1
fi
hex_nonce=$(printf "0x%x" "$(cast nonce --rpc-url "$rpc_url" "$eth_address")")
tx_fields='["'"$hex_nonce"'","0x","0x5208","0x0000000000000000000000000000000000000000","0x","0x"]'
rlp_data=$(cast to-rlp "$tx_fields")
signing_data=$(cast keccak "$rlp_data")
signature=$(cast wallet sign --no-hash --private-key "$private_key" "$signing_data")
signature_r=$(echo "$signature" | cut -c 3-66)
signature_s=$(echo "$signature" | cut -c 67-130)
signature_v=$(echo "$signature" | cut -c 131-132)
signed_tx_fields='["'"$hex_nonce"'","0x","0x5208","0x0000000000000000000000000000000000000000","0x","0x","'"$signature_v"'","'"$signature_r"'","'"$signature_s"'"]'
signed_data=$(cast to-rlp "$signed_tx_fields")
if cast publish "$signed_data" ; then
echo "A zero priced pre eip-155 transaction was mined"
exit 1
fi
}
# bats test_tags=evm-nonce,transaction-eoa
@test "send ETH and verify pending nonce updates" {
# shellcheck disable=SC2034
for i in {1..20}; do
nonce=$(cast nonce --block pending "$eth_address")
tmp_file=$(mktemp)
cast send \
--gas-limit 21000 \
--nonce "$nonce" \
--async \
--value "1" \
--private-key "$private_key" 0x0000000000000000000000000000000000000000 > "$tmp_file"
pending_nonce=$(cast nonce --block pending "$eth_address")
tx_hash="$(cat "$tmp_file")"
# block until mined
cast receipt "$tx_hash" &> /dev/null
final_pending_nonce=$(cast nonce --block pending "$eth_address")
if [[ $nonce -eq $pending_nonce ]]; then
echo "the pending nonce returned by the rpc is not updated after accepting a transaction into the pool"
printf "tx hash: %s\n" "$tx_hash"
printf "initial nonce: %s\n" "$nonce"
printf "pending nonce: %s\n" "$pending_nonce"
printf "final nonce: %s\n" "$final_pending_nonce"
exit 1
fi
sleep 2
done
}
# bats test_tags=evm-block
@test "query finalized, safe, latest, and pending blocks return expected order" {
prev_finalized_block=0
prev_safe_block=0
prev_latest_block=0
prev_pending_block=0
# shellcheck disable=SC2034
for i in {1..20}; do
finalized_block=$(cast block-number finalized)
safe_block=$(cast block-number safe)
latest_block=$(cast block-number latest)
pending_block=$(cast block-number pending)
earliest_block=$(cast block-number earliest)
if [[ $finalized_block -eq 0 || $safe_block -eq 0 || $latest_block -eq 0 || $pending_block -eq 0 ]]; then
echo "Safe, finalized, latest, and pending blocks are not all non-zero"
exit 1
fi
if [[ $earliest_block -ne 0 ]]; then
echo "the earliest block is not equal to 0"
exit 1
fi
if [[ $pending_block -lt $latest_block ]]; then
echo "The pending block is less than the latest block. This should never happen"
exit 1
fi
if [[ $latest_block -lt $safe_block ]]; then
echo "The latest block is less than the safe block. This should never happen"
exit 1
fi
if [[ $safe_block -lt $finalized_block ]]; then
echo "The safe block is less than the finalized block. This should never happen"
exit 1
fi
if [[ $prev_pending_block -gt $pending_block ]]; then
echo "The pending block number seems to have gone backward"
printf "Prev %d, Current %d\n" "$prev_pending_block" "$pending_block"
exit 1
fi
if [[ $prev_latest_block -gt $latest_block ]]; then
echo "The latest block number seemed to have gone backward"
printf "Prev %d, Current %d\n" "$prev_latest_block" "$latest_block"
exit 1
fi
if [[ $prev_safe_block -gt $safe_block ]]; then
echo "The safe block number seemed to have gone backward"
printf "Prev %d, Current %d\n" "$prev_safe_block" "$safe_block"
exit 1
fi
if [[ $prev_finalized_block -gt $finalized_block ]]; then
echo "The safe block number seemed to have gone backward"
printf "Prev %d, Current %d\n" "$prev_finalized_block" "$finalized_block"
exit 1
fi
prev_finalized_block="$finalized_block"
prev_safe_block="$safe_block"
prev_latest_block="$latest_block"
prev_pending_block="$pending_block"
sleep 2
done
}
# bats test_tags=evm-nonce,transaction-eoa
@test "send multiple transactions with same nonce and verify rejection" {
nonce=$(cast nonce "$eth_address")
gas_price=$(cast gas-price)
# shellcheck disable=SC2034
for i in {1..5}; do
for j in {1..5}; do
cantor=$(bc <<< "((($i+$j)*($i+$j+1))/2)+$j")
set +e
cast send \
--gas-limit 21000 \
--nonce "$nonce" \
--async \
--gas-price "$gas_price" \
--value "$cantor" \
--private-key "$private_key" 0x0000000000000000000000000000000000000000
exit_code=$?
set -e
if [[ $j -ne 1 && $exit_code -eq 0 ]]; then
echo "it seems like the a later transaction was accepted into the pool. Race condition?"
exit 1
fi
done
nonce=$((nonce + 1))
sleep 2
done
}