Skip to content

Commit 9f59d17

Browse files
committed
Fix OpenZeppelin external tests to actually use Hardhat and the binary built in CI
1 parent 674b1ec commit 9f59d17

File tree

3 files changed

+103
-8
lines changed

3 files changed

+103
-8
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,6 @@ workflows:
12231223
name: t_ems_compile_ext_zeppelin
12241224
project: zeppelin
12251225
compile_only: 1
1226-
nodejs_version: '14'
12271226
- t_ems_ext:
12281227
<<: *workflow_emscripten
12291228
name: t_ems_compile_ext_ens
@@ -1250,7 +1249,8 @@ workflows:
12501249
<<: *workflow_emscripten
12511250
name: t_ems_test_ext_zeppelin
12521251
project: zeppelin
1253-
nodejs_version: '14'
1252+
# NOTE: Tests crash on nodejs 17: "Error: error:0308010C:digital envelope routines::unsupported"
1253+
nodejs_version: '16'
12541254
- t_ems_ext:
12551255
<<: *workflow_emscripten
12561256
name: t_ems_test_ext_ens

test/externalTests/common.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function setup_solcjs
7474
npm install
7575
cp "$soljson" soljson.js
7676
SOLCVERSION=$(./solcjs --version)
77+
SOLCVERSION_SHORT=$(echo "$SOLCVERSION" | sed -En 's/^([0-9.]+).*\+commit\.[0-9a-f]+.*$/\1/p')
7778
printLog "Using solcjs version $SOLCVERSION"
7879
cd ..
7980
}
@@ -161,6 +162,40 @@ function force_truffle_compiler_settings
161162
echo "module.exports['compilers'] = $(truffle_compiler_settings "$solc_path" "$level" "$evm_version");" >> "$config_file"
162163
}
163164

165+
function force_hardhat_compiler_binary
166+
{
167+
local config_file="$1"
168+
local solc_path="$2"
169+
170+
printLog "Configuring Hardhat..."
171+
echo "-------------------------------------"
172+
echo "Config file: ${config_file}"
173+
echo "Compiler path: ${solc_path}"
174+
hardhat_solc_build_subtask "$SOLCVERSION_SHORT" "$SOLCVERSION" "$solc_path" >> "$config_file"
175+
}
176+
177+
function force_hardhat_compiler_settings
178+
{
179+
local config_file="$1"
180+
local level="$2"
181+
local evm_version="${3:-"$CURRENT_EVM_VERSION"}"
182+
183+
printLog "Configuring Hardhat..."
184+
echo "-------------------------------------"
185+
echo "Config file: ${config_file}"
186+
echo "Optimization level: ${level}"
187+
echo "Optimizer settings: $(optimizer_settings_for_level "$level")"
188+
echo "EVM version: ${evm_version}"
189+
echo "Compiler version: ${SOLCVERSION_SHORT}"
190+
echo "Compiler version (full): ${SOLCVERSION}"
191+
echo "-------------------------------------"
192+
193+
{
194+
echo -n 'module.exports["solidity"] = '
195+
hardhat_compiler_settings "$SOLCVERSION_SHORT" "$level" "$evm_version"
196+
} >> "$config_file"
197+
}
198+
164199
function truffle_verify_compiler_version
165200
{
166201
local solc_version="$1"
@@ -170,11 +205,26 @@ function truffle_verify_compiler_version
170205
grep "$full_solc_version" --with-filename --recursive build/contracts || fail "Wrong compiler version detected."
171206
}
172207

208+
function hardhat_verify_compiler_version
209+
{
210+
local solc_version="$1"
211+
local full_solc_version="$2"
212+
213+
printLog "Verify that the correct version (${solc_version}/${full_solc_version}) of the compiler was used to compile the contracts..."
214+
grep '"solcVersion": "'"${solc_version}"'"' --with-filename artifacts/build-info/*.json || fail "Wrong compiler version detected."
215+
grep '"solcLongVersion": "'"${full_solc_version}"'"' --with-filename artifacts/build-info/*.json || fail "Wrong compiler version detected."
216+
}
217+
173218
function truffle_clean
174219
{
175220
rm -rf build/
176221
}
177222

223+
function hardhat_clean
224+
{
225+
rm -rf artifacts/ cache/
226+
}
227+
178228
function run_test
179229
{
180230
local compile_fn="$1"
@@ -221,6 +271,39 @@ function truffle_compiler_settings
221271
echo "}"
222272
}
223273

274+
function hardhat_solc_build_subtask {
275+
local solc_version="$1"
276+
local full_solc_version="$2"
277+
local solc_path="$3"
278+
279+
echo "const {TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD} = require('hardhat/builtin-tasks/task-names');"
280+
echo "const assert = require('assert');"
281+
echo
282+
echo "subtask(TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD, async (args, hre, runSuper) => {"
283+
echo " assert(args.solcVersion == '${solc_version}', 'Unexpected solc version: ' + args.solcVersion)"
284+
echo " return {"
285+
echo " compilerPath: '$(realpath "$solc_path")',"
286+
echo " isSolcJs: true,"
287+
echo " version: args.solcVersion,"
288+
echo " longVersion: '${full_solc_version}'"
289+
echo " }"
290+
echo "})"
291+
}
292+
293+
function hardhat_compiler_settings {
294+
local solc_version="$1"
295+
local level="$2"
296+
local evm_version="$3"
297+
298+
echo "{"
299+
echo " version: '${solc_version}',"
300+
echo " settings: {"
301+
echo " optimizer: $(optimizer_settings_for_level "$level"),"
302+
echo " evmVersion: '${evm_version}'"
303+
echo " }"
304+
echo "}"
305+
}
306+
224307
function compile_and_run_test
225308
{
226309
local compile_fn="$1"
@@ -252,6 +335,18 @@ function truffle_run_test
252335
compile_and_run_test compile_fn test_fn truffle_verify_compiler_version
253336
}
254337

338+
function hardhat_run_test
339+
{
340+
local config_file="$1"
341+
local optimizer_level="$2"
342+
local compile_fn="$3"
343+
local test_fn="$4"
344+
345+
hardhat_clean
346+
force_hardhat_compiler_settings "$config_file" "$optimizer_level"
347+
compile_and_run_test compile_fn test_fn hardhat_verify_compiler_version
348+
}
349+
255350
function external_test
256351
{
257352
local name="$1"

test/externalTests/zeppelin.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ source test/externalTests/common.sh
2727
verify_input "$1"
2828
SOLJSON="$1"
2929

30-
function compile_fn { npx truffle compile; }
31-
function test_fn { npm run test; }
30+
function compile_fn { npm run compile; }
31+
function test_fn { npm test; }
3232

3333
function zeppelin_test
3434
{
3535
local repo="https://github.com/OpenZeppelin/openzeppelin-contracts.git"
3636
local branch=master
37-
local config_file="truffle-config.js"
37+
local config_file="hardhat.config.js"
3838
local min_optimizer_level=1
3939
local max_optimizer_level=3
4040

@@ -46,14 +46,14 @@ function zeppelin_test
4646
download_project "$repo" "$branch" "$DIR"
4747

4848
neutralize_package_json_hooks
49-
force_truffle_compiler_settings "$config_file" "${DIR}/solc" "$min_optimizer_level"
49+
force_hardhat_compiler_binary "$config_file" "$SOLJSON"
50+
force_hardhat_compiler_settings "$config_file" "$min_optimizer_level"
5051
npm install
5152

5253
replace_version_pragmas
53-
force_solc_modules "${DIR}/solc"
5454

5555
for level in $selected_optimizer_levels; do
56-
truffle_run_test "$config_file" "${DIR}/solc" "$level" compile_fn test_fn
56+
hardhat_run_test "$config_file" "$level" compile_fn test_fn
5757
done
5858
}
5959

0 commit comments

Comments
 (0)