-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrunsol.sh
More file actions
executable file
·343 lines (309 loc) · 9.96 KB
/
runsol.sh
File metadata and controls
executable file
·343 lines (309 loc) · 9.96 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env bash
set -euo pipefail
# Check for input file
if [[ $# -lt 1 ]]; then
echo "Usage: $0 file.solc [options]"
echo "Options:"
echo " --runtime-calldata sig [args...] Generate calldata using cast calldata"
echo " --runtime-raw-calldata hex Pass raw calldata directly to geth"
echo " --runtime-callvalue value Pass callvalue to geth (in wei)"
echo " --debug-runtime Explore the evm execution in the interactive debugger"
echo " --create true|false Run the initcode to deploy the contract (default: true)"
echo " --create-arguments sig [args...] Generate calldata using cast calldata"
echo " --create-raw-arguments hex Pass raw calldata directly to geth"
echo " --create-callvalue value Pass callvalue to geth (in wei)"
echo " --debug-create Explore the evm execution in the interactive debugger"
exit 1
fi
# Setup file paths
file=$1
shift
if [[ ! -f "$file" ]]; then
echo "Error: File '$file' not found"
exit 1
fi
echo "Processing: $file"
root_dir="$(cd "$(dirname "$(readlink --canonicalize "${BASH_SOURCE[0]}")")" && pwd)"
build_dir="$root_dir/build"
base=$(basename "$file" .solc)
hull="$build_dir/output1.hull"
hexfile="$build_dir/$base.hex"
yulfile="$build_dir/$base.yul"
runtime_tracefile="$build_dir/trace.runtime.jsonl"
create_tracefile="$build_dir/trace.create.jsonl"
create_poststate="$build_dir/create.poststate.json"
# Parse arguments
runtime_calldata_sig=""
runtime_calldata_args=()
runtime_raw_calldata=""
runtime_callvalue=""
runtime_debug=false
create=true
create_arguments_sig=""
create_args=()
create_raw_args=""
create_callvalue=""
create_debug=false
while [[ $# -gt 0 ]]; do
case $1 in
--runtime-calldata)
shift
if [[ $# -eq 0 ]]; then
echo "Error: --runtime-calldata requires a signature"
exit 1
fi
runtime_calldata_sig=$1
shift
# Collect arguments until next flag or end
while [[ $# -gt 0 ]] && [[ ! "$1" =~ ^-- ]]; do
runtime_calldata_args+=("$1")
shift
done
;;
--runtime-raw-calldata)
shift
if [[ $# -eq 0 ]]; then
echo "Error: --runtime-raw-calldata requires a hex value"
exit 1
fi
runtime_raw_calldata=$1
shift
;;
--runtime-callvalue)
shift
if [[ $# -eq 0 ]]; then
echo "Error: --runtime-callvalue requires a value"
exit 1
fi
runtime_callvalue=$1
shift
;;
--debug-runtime)
shift
runtime_debug=true
;;
--create)
shift
if [[ $# -eq 0 ]]; then
echo "Error: --create requires true or false"
exit 1
fi
if [[ "$1" == "true" ]]; then
create=true
elif [[ "$1" == "false" ]]; then
create=false
else
echo "Error: --create requires true or false, got '$1'"
exit 1
fi
shift
;;
--create-arguments)
shift
if [[ $# -eq 0 ]]; then
echo "Error: --create-arguments requires a signature"
exit 1
fi
create_arguments_sig=$1
shift
# Collect arguments until next flag or end
while [[ $# -gt 0 ]] && [[ ! "$1" =~ ^-- ]]; do
create_args+=("$1")
shift
done
;;
--create-raw-arguments)
shift
if [[ $# -eq 0 ]]; then
echo "Error: --create-raw-arguments requires a hex value"
exit 1
fi
create_raw_args=$1
shift
;;
--create-callvalue)
shift
if [[ $# -eq 0 ]]; then
echo "Error: --create-callvalue requires a value"
exit 1
fi
create_callvalue=$1
shift
;;
--debug-create)
shift
create_debug=true
;;
*)
echo "Error: Unknown option: $1"
exit 1
;;
esac
done
# Check for conflicting flags
if [[ -n "$runtime_calldata_sig" ]] && [[ -n "$runtime_raw_calldata" ]]; then
echo "Error: Cannot use both --runtime-calldata and --runtime-raw-calldata"
exit 1
fi
if [[ -n "$create_arguments_sig" ]] && [[ -n "$create_raw_args" ]]; then
echo "Error: Cannot use both --create-arguments and --create-raw-arguments"
exit 1
fi
# Execute compilation pipeline
echo "Compiling to hull..."
if ! cabal run sol-core -- -f "$file"; then
echo "Error: sol-core compilation failed"
exit 1
fi
mkdir -p build
if ls ./output*.hull 1> /dev/null 2>&1; then
mv ./output*.hull build/
fi
echo "Generating Yul..."
yule_args=("$hull" -o "$yulfile")
if [[ "$create" == "false" ]]; then
yule_args+=(--nodeploy)
fi
if ! cabal run yule -- "${yule_args[@]}"; then
echo "Error: yule generation failed"
exit 1
fi
echo "Compiling to bytecode..."
if ! solc --strict-assembly --bin --optimize "$yulfile" | tail -1 | tr -d '\n' > "$hexfile"; then
echo "Error: solc compilation failed"
exit 1
fi
echo "Hex output: $hexfile"
if [[ "$create" == "true" ]]; then
evm_cmd="evm run --trace --trace.nomemory=false --trace.noreturndata=false"
# Build and execute evm command for create
evm_cmd="$evm_cmd --create --dump --codefile $hexfile"
if [[ -n "$create_arguments_sig" ]]; then
if ! arguments=$(cast abi-encode "$create_arguments_sig" "${create_args[@]}"); then
echo "Error: Failed to generate calldata"
exit 1
fi
echo -n "${arguments#0x}" >> $hexfile
elif [[ -n "$create_raw_args" ]]; then
echo -n "${create_raw_args#0x}" >> $hexfile
fi
if [[ -n "$create_callvalue" ]]; then
evm_cmd="$evm_cmd --value $create_callvalue"
fi
echo "Executing..."
output=$(eval "$evm_cmd" 2>&1) || true
# Extract post-state from output and construct proper genesis JSON
start_line=$(echo "$output" | grep -n '^{' | tail -1 | cut -d: -f1)
post_state=$(echo "$output" | sed -n "${start_line},\$p" | jq -r '.')
# Construct proper genesis JSON for evm run --prestate with latest mainnet EVM version
genesis_json=$(echo "$post_state" | jq '{
config: {
chainId: 1,
homesteadBlock: 0,
eip150Block: 0,
eip155Block: 0,
eip158Block: 0,
byzantiumBlock: 0,
constantinopleBlock: 0,
petersburgBlock: 0,
istanbulBlock: 0,
muirGlacierBlock: 0,
berlinBlock: 0,
londonBlock: 0,
arrowGlacierBlock: 0,
grayGlacierBlock: 0,
mergeNetsplitBlock: 0,
shanghaiTime: 0,
cancunTime: 0,
pragueTime: 0,
blobSchedule: {
cancun: {
target: 3,
max: 6,
baseFeeUpdateFraction: 3338477
},
prague: {
target: 6,
max: 12,
baseFeeUpdateFraction: 6676954
},
osaka: {
target: 9,
max: 18,
baseFeeUpdateFraction: 10015431
}
}
},
nonce: 0,
timestamp: 0,
extraData: "0x",
gasLimit: "0x47e7c4",
difficulty: "0x1",
mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
coinbase: "0x0000000000000000000000000000000000000000",
alloc: .accounts
}')
echo "$genesis_json" > "$create_poststate"
echo "$output" | jq -R -c 'fromjson? | select(type == "object")' > "$create_tracefile"
# Extract the result and error from the create tracefile
result=$(jq -sr 'last | .output' "$create_tracefile")
error=$(jq -sr 'last | .error' "$create_tracefile")
if [[ "$create_debug" == "true" ]]; then
traceview "$create_tracefile"
fi
if [[ "$error" == "null" ]]; then
echo "Creation successful"
echo "returndata: 0x${result}"
else
echo "Creation failed: $error"
echo "returndata: 0x$result"
fi
fi
evm_cmd="evm run --trace --trace.nomemory=false --trace.noreturndata=false"
# Build and execute evm command for runtime
if [[ "$create" == "true" ]]; then
evm_cmd="$evm_cmd --receiver 0x1f2a98889594024BFfdA3311CbE69728d392C06D --prestate $create_poststate"
else
evm_cmd="$evm_cmd --codefile $hexfile"
fi
if [[ -n "$runtime_calldata_sig" ]]; then
if ! calldata=$(cast calldata "$runtime_calldata_sig" "${runtime_calldata_args[@]}"); then
echo "Error: Failed to generate calldata"
exit 1
fi
evm_cmd="$evm_cmd --input $calldata"
elif [[ -n "$runtime_raw_calldata" ]]; then
evm_cmd="$evm_cmd --input $runtime_raw_calldata"
fi
if [[ -n "$runtime_callvalue" ]]; then
evm_cmd="$evm_cmd --value $runtime_callvalue"
fi
echo "Executing..."
output=$(eval "$evm_cmd" 2>&1) || true
echo "$output" | jq -R -c 'fromjson? | select(type == "object")' > "$runtime_tracefile"
result=$(jq -sr 'last | .output' "$runtime_tracefile")
error=$(jq -sr 'last | .error' "$runtime_tracefile")
gasUsed=$(jq -sr 'last | .gasUsed' "$runtime_tracefile")
if [[ "$runtime_debug" == "true" ]]; then
traceview "$runtime_tracefile"
fi
if [[ "$error" == "null" ]]; then
if [[ -n "$runtime_calldata_sig" ]]; then
echo "Execution successful"
# Remove quotes from result and decode
if [[ -n "$result" ]]; then
decoded=$(cast abi-decode "$runtime_calldata_sig" "0x$result")
echo "Decoded output: $decoded"
gasDecoded=$(printf "%d" $gasUsed)
echo "Gas used: $gasDecoded"
else
echo "No return data to decode"
fi
else
echo "Execution successful"
echo "returndata: 0x${result}"
fi
else
echo "Execution failed: $error"
echo "returndata: 0x$result"
fi