@@ -39,7 +39,7 @@ source "${REPO_ROOT}/scripts/common.sh"
39
39
40
40
function print_usage
41
41
{
42
- fail " Usage: ${0} ast [--exit-on-error]."
42
+ fail " Usage: ${0} ast|evm-assembly [--exit-on-error]."
43
43
}
44
44
45
45
function print_used_commands
@@ -75,13 +75,15 @@ for PARAM in "$@"
75
75
do
76
76
case " $PARAM " in
77
77
ast) IMPORT_TEST_TYPE=" ast" ;;
78
+ evm-assembly) IMPORT_TEST_TYPE=" evm-assembly" ;;
78
79
--exit-on-error) EXIT_ON_ERROR=1 ;;
79
80
* ) print_usage ;;
80
81
esac
81
82
done
82
83
83
84
SYNTAXTESTS_DIR=" ${REPO_ROOT} /test/libsolidity/syntaxTests"
84
85
ASTJSONTESTS_DIR=" ${REPO_ROOT} /test/libsolidity/ASTJSON"
86
+ SEMANTICTESTS_DIR=" ${REPO_ROOT} /test/libsolidity/semanticTests"
85
87
86
88
FAILED=0
87
89
UNCOMPILABLE=0
@@ -127,6 +129,107 @@ function ast_import_export_equivalence
127
129
TESTED=$(( TESTED + 1 ))
128
130
}
129
131
132
+ function evmjson_import_export_equivalence
133
+ {
134
+ local sol_file=" $1 "
135
+ local input_files=( " ${@: 2} " )
136
+ local outputs=( " asm" " bin" " bin-runtime" " opcodes" " srcmap" " srcmap-runtime" )
137
+ local export_command=(" $SOLC " --combined-json " $( IFS=, ; echo " ${outputs[*]} " ) " --pretty-json --json-indent 4 " ${input_files[@]} " )
138
+ local success=1
139
+ if ! " ${export_command[@]} " > expected.json 2> expected.error
140
+ then
141
+ success=0
142
+ printError " ❌ ERROR: (export) EVM Assembly JSON reimport failed for ${sol_file} "
143
+ if [[ $EXIT_ON_ERROR == 1 ]]
144
+ then
145
+ print_used_commands " $( pwd) " " ${export_command[*]} " " "
146
+ return 1
147
+ fi
148
+ fi
149
+
150
+ # Note that we have some test files, that only consists of free functions.
151
+ # Those files doesn't define any contracts, so the resulting json does not define any
152
+ # keys. In this case `jq` returns an error like `jq: error: null (null) has no keys`
153
+ # to not get spammed by these errors, errors are redirected to /dev/null.
154
+ for contract in $( jq ' .contracts | keys | .[]' expected.json 2> /dev/null)
155
+ do
156
+ for output in " ${outputs[@]} "
157
+ do
158
+ jq --raw-output " .contracts.${contract} .\" ${output} \" " expected.json > " expected.${output} .json"
159
+ done
160
+
161
+ assembly=$( cat expected.asm.json)
162
+ [[ $assembly != " " && $assembly != " null" ]] || continue
163
+
164
+ local import_command=(" ${SOLC} " --combined-json " bin,bin-runtime,opcodes,asm,srcmap,srcmap-runtime" --pretty-json --json-indent 4 --import-asm-json expected.asm.json)
165
+ if ! " ${import_command[@]} " > obtained.json 2> obtained.error
166
+ then
167
+ success=0
168
+ printError " ❌ ERROR: (import) EVM Assembly JSON reimport failed for ${sol_file} "
169
+ if [[ $EXIT_ON_ERROR == 1 ]]
170
+ then
171
+ print_used_commands " $( pwd) " " ${export_command[*]} " " ${import_command[*]} "
172
+ return 1
173
+ fi
174
+ fi
175
+
176
+ for output in " ${outputs[@]} "
177
+ do
178
+ for obtained_contract in $( jq ' .contracts | keys | .[]' obtained.json 2> /dev/null)
179
+ do
180
+ jq --raw-output " .contracts.${obtained_contract} .\" ${output} \" " obtained.json > " obtained.${output} .json"
181
+ # compare expected and obtained evm assembly json
182
+ if ! diff_files " expected.${output} .json" " obtained.${output} .json"
183
+ then
184
+ success=0
185
+ printError " ❌ ERROR: (${output} ) EVM Assembly JSON reimport failed for ${sol_file} "
186
+ if [[ $EXIT_ON_ERROR == 1 ]]
187
+ then
188
+ print_used_commands " $( pwd) " " ${export_command[*]} " " ${import_command[*]} "
189
+ return 1
190
+ fi
191
+ fi
192
+ done
193
+ done
194
+
195
+ # direct export via --asm-json, if imported with --import-asm-json.
196
+ if ! " ${SOLC} " --asm-json --import-asm-json expected.asm.json --pretty-json --json-indent 4 | tail -n+4 > obtained_direct_import_export.json 2> obtained_direct_import_export.error
197
+ then
198
+ success=0
199
+ printError " ❌ ERROR: (direct) EVM Assembly JSON reimport failed for ${sol_file} "
200
+ if [[ $EXIT_ON_ERROR == 1 ]]
201
+ then
202
+ print_used_commands " $( pwd) " " ${SOLC} --asm-json --import-asm-json expected.asm.json --pretty-json --json-indent 4 | tail -n+4" " "
203
+ return 1
204
+ fi
205
+ fi
206
+
207
+ # reformat json files using jq.
208
+ jq . expected.asm.json > expected.asm.json.pretty
209
+ jq . obtained_direct_import_export.json > obtained_direct_import_export.json.pretty
210
+
211
+ # compare expected and obtained evm assembly.
212
+ if ! diff_files expected.asm.json.pretty obtained_direct_import_export.json.pretty
213
+ then
214
+ success=0
215
+ printError " ❌ ERROR: EVM Assembly JSON reimport failed for ${sol_file} "
216
+ if [[ $EXIT_ON_ERROR == 1 ]]
217
+ then
218
+ print_used_commands " $( pwd) " " ${export_command[*]} " " ${import_command[*]} "
219
+ return 1
220
+ fi
221
+ fi
222
+ done
223
+
224
+ if (( success == 1 ))
225
+ then
226
+ echo -n " ✅"
227
+ TESTED=$(( TESTED + 1 ))
228
+ else
229
+ FAILED=$(( FAILED + 1 ))
230
+ fi
231
+ }
232
+
130
233
# function tests whether exporting and importing again is equivalent.
131
234
# Results are recorded by adding to FAILED or UNCOMPILABLE.
132
235
# Also, in case of a mismatch a diff is printed
@@ -149,6 +252,7 @@ function test_import_export_equivalence {
149
252
then
150
253
case " $IMPORT_TEST_TYPE " in
151
254
ast) ast_import_export_equivalence " ${sol_file} " " ${input_files[@]} " ;;
255
+ evm-assembly) evmjson_import_export_equivalence " ${sol_file} " " ${input_files[@]} " ;;
152
256
* ) fail " Unknown import test type '${IMPORT_TEST_TYPE} '. Aborting." ;;
153
257
esac
154
258
else
@@ -173,6 +277,7 @@ command_available jq --version
173
277
174
278
case " $IMPORT_TEST_TYPE " in
175
279
ast) TEST_DIRS=(" ${SYNTAXTESTS_DIR} " " ${ASTJSONTESTS_DIR} " ) ;;
280
+ evm-assembly) TEST_DIRS=(" ${SYNTAXTESTS_DIR} " " ${SEMANTICTESTS_DIR} " ) ;;
176
281
* ) print_usage ;;
177
282
esac
178
283
0 commit comments