2019-04-01 12:32:53 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# This file is part of solidity.
|
|
|
|
#
|
|
|
|
# solidity is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# solidity is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with solidity. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
#
|
|
|
|
# (c) 2019 solidity contributors.
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
set -e
|
|
|
|
|
2019-04-26 09:57:49 +00:00
|
|
|
# Requires "${REPO_ROOT}/scripts/common.sh" to be included before.
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2021-10-25 12:29:43 +00:00
|
|
|
CURRENT_EVM_VERSION=london
|
|
|
|
|
2021-10-28 10:46:34 +00:00
|
|
|
function print_optimizer_levels_or_exit
|
|
|
|
{
|
|
|
|
local selected_levels="$1"
|
|
|
|
|
|
|
|
[[ $selected_levels != "" ]] || { printWarning "No steps to run. Exiting."; exit 0; }
|
|
|
|
|
|
|
|
printLog "Selected optimizer levels: ${selected_levels}"
|
|
|
|
}
|
|
|
|
|
2019-04-01 12:32:53 +00:00
|
|
|
function verify_input
|
|
|
|
{
|
2021-12-03 13:51:05 +00:00
|
|
|
local binary_type="$1"
|
|
|
|
local binary_path="$2"
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2021-12-03 13:51:05 +00:00
|
|
|
(( $# == 2 )) || fail "Usage: $0 native|solcjs <path to solc or soljson.js>"
|
|
|
|
[[ $binary_type == native || $binary_type == solcjs ]] || fail "Invalid binary type: '${binary_type}'. Must be either 'native' or 'solcjs'."
|
2021-12-03 11:57:39 +00:00
|
|
|
[[ -f "$binary_path" ]] || fail "The compiler binary does not exist at '${binary_path}'"
|
2019-09-25 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 13:51:05 +00:00
|
|
|
function setup_solc
|
2019-04-01 12:32:53 +00:00
|
|
|
{
|
2021-12-03 11:57:39 +00:00
|
|
|
local test_dir="$1"
|
2021-12-03 13:51:05 +00:00
|
|
|
local binary_type="$2"
|
|
|
|
local binary_path="$3"
|
|
|
|
local solcjs_branch="${4:-master}"
|
|
|
|
local install_dir="${5:-solc/}"
|
|
|
|
|
|
|
|
[[ $binary_type == native || $binary_type == solcjs ]] || assertFail
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2021-12-03 11:57:39 +00:00
|
|
|
cd "$test_dir"
|
2019-09-25 13:59:52 +00:00
|
|
|
|
2021-12-03 13:51:05 +00:00
|
|
|
if [[ $binary_type == solcjs ]]
|
|
|
|
then
|
|
|
|
printLog "Setting up solc-js..."
|
|
|
|
git clone --depth 1 -b "$solcjs_branch" https://github.com/ethereum/solc-js.git "$install_dir"
|
|
|
|
|
|
|
|
pushd "$install_dir"
|
|
|
|
npm install
|
|
|
|
cp "$binary_path" soljson.js
|
|
|
|
SOLCVERSION=$(./solcjs --version)
|
|
|
|
popd
|
|
|
|
else
|
|
|
|
printLog "Setting up solc..."
|
|
|
|
SOLCVERSION=$("$binary_path" --version | tail -n 1 | sed -n -E 's/^Version: (.*)$/\1/p')
|
|
|
|
fi
|
|
|
|
|
2021-10-26 13:49:00 +00:00
|
|
|
SOLCVERSION_SHORT=$(echo "$SOLCVERSION" | sed -En 's/^([0-9.]+).*\+commit\.[0-9a-f]+.*$/\1/p')
|
2021-12-03 11:57:39 +00:00
|
|
|
printLog "Using compiler version $SOLCVERSION"
|
2019-04-01 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function download_project
|
|
|
|
{
|
|
|
|
local repo="$1"
|
2021-12-03 11:57:39 +00:00
|
|
|
local solcjs_branch="$2"
|
|
|
|
local test_dir="$3"
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2021-12-03 11:57:39 +00:00
|
|
|
printLog "Cloning $solcjs_branch of $repo..."
|
|
|
|
git clone --depth 1 "$repo" -b "$solcjs_branch" "$test_dir/ext"
|
2019-04-01 12:32:53 +00:00
|
|
|
cd ext
|
2020-12-01 19:32:12 +00:00
|
|
|
echo "Current commit hash: $(git rev-parse HEAD)"
|
2019-04-01 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 09:19:17 +00:00
|
|
|
function force_truffle_version
|
|
|
|
{
|
2020-12-01 23:55:12 +00:00
|
|
|
local version="$1"
|
2019-11-29 09:19:17 +00:00
|
|
|
|
2020-12-01 23:55:12 +00:00
|
|
|
sed -i 's/"truffle":\s*".*"/"truffle": "'"$version"'"/g' package.json
|
2019-11-29 09:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 12:32:53 +00:00
|
|
|
function replace_version_pragmas
|
|
|
|
{
|
2019-10-25 15:38:29 +00:00
|
|
|
# Replace fixed-version pragmas (part of Consensys best practice).
|
|
|
|
# Include all directories to also cover node dependencies.
|
2019-04-01 12:32:53 +00:00
|
|
|
printLog "Replacing fixed-version pragmas..."
|
2020-11-30 13:01:16 +00:00
|
|
|
find . test -name '*.sol' -type f -print0 | xargs -0 sed -i -E -e 's/pragma solidity [^;]+;/pragma solidity >=0.0;/'
|
2019-04-01 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 13:00:34 +00:00
|
|
|
function neutralize_package_lock
|
|
|
|
{
|
|
|
|
# Remove lock files (if they exist) to prevent them from overriding our changes in package.json
|
|
|
|
printLog "Removing package lock files..."
|
|
|
|
rm --force --verbose yarn.lock
|
|
|
|
rm --force --verbose package-lock.json
|
|
|
|
}
|
|
|
|
|
2021-10-26 13:39:19 +00:00
|
|
|
function neutralize_package_json_hooks
|
|
|
|
{
|
|
|
|
printLog "Disabling package.json hooks..."
|
|
|
|
[[ -f package.json ]] || fail "package.json not found"
|
|
|
|
sed -i 's|"prepublish": *".*"|"prepublish": ""|g' package.json
|
|
|
|
sed -i 's|"prepare": *".*"|"prepare": ""|g' package.json
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:26:52 +00:00
|
|
|
function force_solc_modules
|
2019-04-01 12:32:53 +00:00
|
|
|
{
|
2021-10-27 14:26:52 +00:00
|
|
|
local custom_solcjs_path="${1:-solc/}"
|
|
|
|
|
|
|
|
[[ -d node_modules/ ]] || assertFail
|
2020-12-08 15:23:37 +00:00
|
|
|
|
2021-10-27 14:26:52 +00:00
|
|
|
printLog "Replacing all installed solc-js with a link to the latest version..."
|
|
|
|
soljson_binaries=$(find node_modules -type f -path "*/solc/soljson.js")
|
|
|
|
for soljson_binary in $soljson_binaries
|
2019-04-01 12:32:53 +00:00
|
|
|
do
|
2021-10-27 14:26:52 +00:00
|
|
|
local solc_module_path
|
|
|
|
solc_module_path=$(dirname "$soljson_binary")
|
|
|
|
|
|
|
|
printLog "Found and replaced solc-js in $solc_module_path"
|
|
|
|
rm -r "$solc_module_path"
|
|
|
|
ln -s "$custom_solcjs_path" "$solc_module_path"
|
2019-04-01 12:32:53 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-12-08 15:23:37 +00:00
|
|
|
function force_truffle_compiler_settings
|
2019-04-01 12:32:53 +00:00
|
|
|
{
|
|
|
|
local config_file="$1"
|
2021-12-03 13:51:05 +00:00
|
|
|
local binary_type="$2"
|
|
|
|
local solc_path="$3"
|
|
|
|
local level="$4"
|
|
|
|
local evm_version="${5:-"$CURRENT_EVM_VERSION"}"
|
|
|
|
|
|
|
|
[[ $binary_type == native || $binary_type == solcjs ]] || assertFail
|
|
|
|
|
|
|
|
[[ $binary_type == native ]] && local solc_path="native"
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2020-12-08 15:23:37 +00:00
|
|
|
printLog "Forcing Truffle compiler settings..."
|
2019-04-01 12:32:53 +00:00
|
|
|
echo "-------------------------------------"
|
|
|
|
echo "Config file: $config_file"
|
2021-12-03 13:51:05 +00:00
|
|
|
echo "Binary type: $binary_type"
|
2020-12-08 15:23:37 +00:00
|
|
|
echo "Compiler path: $solc_path"
|
2020-12-07 19:00:53 +00:00
|
|
|
echo "Optimization level: $level"
|
|
|
|
echo "Optimizer settings: $(optimizer_settings_for_level "$level")"
|
|
|
|
echo "EVM version: $evm_version"
|
2019-04-01 12:32:53 +00:00
|
|
|
echo "-------------------------------------"
|
|
|
|
|
|
|
|
# Forcing the settings should always work by just overwriting the solc object. Forcing them by using a
|
|
|
|
# dedicated settings objects should only be the fallback.
|
2020-12-08 15:23:37 +00:00
|
|
|
echo "module.exports['compilers'] = $(truffle_compiler_settings "$solc_path" "$level" "$evm_version");" >> "$config_file"
|
2019-04-01 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:00 +00:00
|
|
|
function force_hardhat_compiler_binary
|
|
|
|
{
|
|
|
|
local config_file="$1"
|
2021-12-03 13:51:05 +00:00
|
|
|
local binary_type="$2"
|
|
|
|
local solc_path="$3"
|
2021-10-26 13:49:00 +00:00
|
|
|
|
|
|
|
printLog "Configuring Hardhat..."
|
|
|
|
echo "-------------------------------------"
|
|
|
|
echo "Config file: ${config_file}"
|
2021-12-03 13:51:05 +00:00
|
|
|
echo "Binary type: ${binary_type}"
|
2021-10-26 13:49:00 +00:00
|
|
|
echo "Compiler path: ${solc_path}"
|
2021-12-03 13:51:05 +00:00
|
|
|
hardhat_solc_build_subtask "$SOLCVERSION_SHORT" "$SOLCVERSION" "$binary_type" "$solc_path" >> "$config_file"
|
2021-10-26 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function force_hardhat_compiler_settings
|
|
|
|
{
|
|
|
|
local config_file="$1"
|
|
|
|
local level="$2"
|
|
|
|
local evm_version="${3:-"$CURRENT_EVM_VERSION"}"
|
|
|
|
|
|
|
|
printLog "Configuring Hardhat..."
|
|
|
|
echo "-------------------------------------"
|
|
|
|
echo "Config file: ${config_file}"
|
|
|
|
echo "Optimization level: ${level}"
|
|
|
|
echo "Optimizer settings: $(optimizer_settings_for_level "$level")"
|
|
|
|
echo "EVM version: ${evm_version}"
|
|
|
|
echo "Compiler version: ${SOLCVERSION_SHORT}"
|
|
|
|
echo "Compiler version (full): ${SOLCVERSION}"
|
|
|
|
echo "-------------------------------------"
|
|
|
|
|
|
|
|
{
|
|
|
|
echo -n 'module.exports["solidity"] = '
|
|
|
|
hardhat_compiler_settings "$SOLCVERSION_SHORT" "$level" "$evm_version"
|
|
|
|
} >> "$config_file"
|
|
|
|
}
|
|
|
|
|
2021-10-25 12:29:43 +00:00
|
|
|
function truffle_verify_compiler_version
|
2019-04-01 12:32:53 +00:00
|
|
|
{
|
|
|
|
local solc_version="$1"
|
2021-10-25 12:29:43 +00:00
|
|
|
local full_solc_version="$2"
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2021-10-25 12:29:43 +00:00
|
|
|
printLog "Verify that the correct version (${solc_version}/${full_solc_version}) of the compiler was used to compile the contracts..."
|
|
|
|
grep "$full_solc_version" --with-filename --recursive build/contracts || fail "Wrong compiler version detected."
|
2019-04-01 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:00 +00:00
|
|
|
function hardhat_verify_compiler_version
|
|
|
|
{
|
|
|
|
local solc_version="$1"
|
|
|
|
local full_solc_version="$2"
|
|
|
|
|
|
|
|
printLog "Verify that the correct version (${solc_version}/${full_solc_version}) of the compiler was used to compile the contracts..."
|
|
|
|
grep '"solcVersion": "'"${solc_version}"'"' --with-filename artifacts/build-info/*.json || fail "Wrong compiler version detected."
|
|
|
|
grep '"solcLongVersion": "'"${full_solc_version}"'"' --with-filename artifacts/build-info/*.json || fail "Wrong compiler version detected."
|
|
|
|
}
|
|
|
|
|
2021-10-25 12:29:43 +00:00
|
|
|
function truffle_clean
|
2019-04-01 12:32:53 +00:00
|
|
|
{
|
2021-10-25 12:29:43 +00:00
|
|
|
rm -rf build/
|
2019-04-01 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:00 +00:00
|
|
|
function hardhat_clean
|
|
|
|
{
|
|
|
|
rm -rf artifacts/ cache/
|
|
|
|
}
|
|
|
|
|
2019-04-01 12:32:53 +00:00
|
|
|
function run_test
|
|
|
|
{
|
|
|
|
local compile_fn="$1"
|
|
|
|
local test_fn="$2"
|
|
|
|
|
2019-10-25 15:38:29 +00:00
|
|
|
replace_version_pragmas
|
2019-10-28 11:12:52 +00:00
|
|
|
|
2019-09-25 13:59:52 +00:00
|
|
|
printLog "Running compile function..."
|
|
|
|
$compile_fn
|
|
|
|
|
|
|
|
printLog "Running test function..."
|
|
|
|
$test_fn
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:58:47 +00:00
|
|
|
function optimizer_settings_for_level
|
|
|
|
{
|
2020-12-07 19:00:53 +00:00
|
|
|
local level="$1"
|
|
|
|
|
|
|
|
case "$level" in
|
|
|
|
1) echo "{enabled: false}" ;;
|
2021-12-10 14:34:07 +00:00
|
|
|
2) echo "{enabled: true, details: {yul: false}}" ;;
|
2020-12-07 19:00:53 +00:00
|
|
|
3) echo "{enabled: true, details: {yul: true}}" ;;
|
|
|
|
*)
|
2021-12-03 11:53:50 +00:00
|
|
|
fail "Optimizer level not found. Please define OPTIMIZER_LEVEL=[1, 2, 3]"
|
2020-12-07 19:00:53 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2021-12-03 13:51:05 +00:00
|
|
|
function replace_global_solc
|
|
|
|
{
|
|
|
|
local solc_path="$1"
|
|
|
|
|
|
|
|
[[ ! -e solc ]] || fail "A file named 'solc' already exists in '${PWD}'."
|
|
|
|
|
|
|
|
ln -s "$solc_path" solc
|
|
|
|
export PATH="$PWD:$PATH"
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:58:47 +00:00
|
|
|
function truffle_compiler_settings
|
|
|
|
{
|
2020-12-08 15:21:46 +00:00
|
|
|
local solc_path="$1"
|
|
|
|
local level="$2"
|
|
|
|
local evm_version="$3"
|
|
|
|
|
|
|
|
echo "{"
|
|
|
|
echo " solc: {"
|
|
|
|
echo " version: \"${solc_path}\","
|
|
|
|
echo " settings: {"
|
|
|
|
echo " optimizer: $(optimizer_settings_for_level "$level"),"
|
|
|
|
echo " evmVersion: \"${evm_version}\""
|
|
|
|
echo " }"
|
|
|
|
echo " }"
|
|
|
|
echo "}"
|
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:00 +00:00
|
|
|
function hardhat_solc_build_subtask {
|
|
|
|
local solc_version="$1"
|
|
|
|
local full_solc_version="$2"
|
2021-12-03 13:51:05 +00:00
|
|
|
local binary_type="$3"
|
|
|
|
local solc_path="$4"
|
|
|
|
|
|
|
|
[[ $binary_type == native || $binary_type == solcjs ]] || assertFail
|
|
|
|
|
|
|
|
[[ $binary_type == native ]] && local is_solcjs=false
|
|
|
|
[[ $binary_type == solcjs ]] && local is_solcjs=true
|
2021-10-26 13:49:00 +00:00
|
|
|
|
|
|
|
echo "const {TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD} = require('hardhat/builtin-tasks/task-names');"
|
|
|
|
echo "const assert = require('assert');"
|
|
|
|
echo
|
|
|
|
echo "subtask(TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD, async (args, hre, runSuper) => {"
|
|
|
|
echo " assert(args.solcVersion == '${solc_version}', 'Unexpected solc version: ' + args.solcVersion)"
|
|
|
|
echo " return {"
|
|
|
|
echo " compilerPath: '$(realpath "$solc_path")',"
|
2021-12-03 13:51:05 +00:00
|
|
|
echo " isSolcJs: ${is_solcjs},"
|
2021-10-26 13:49:00 +00:00
|
|
|
echo " version: args.solcVersion,"
|
|
|
|
echo " longVersion: '${full_solc_version}'"
|
|
|
|
echo " }"
|
|
|
|
echo "})"
|
|
|
|
}
|
|
|
|
|
|
|
|
function hardhat_compiler_settings {
|
|
|
|
local solc_version="$1"
|
|
|
|
local level="$2"
|
|
|
|
local evm_version="$3"
|
|
|
|
|
|
|
|
echo "{"
|
|
|
|
echo " version: '${solc_version}',"
|
|
|
|
echo " settings: {"
|
|
|
|
echo " optimizer: $(optimizer_settings_for_level "$level"),"
|
|
|
|
echo " evmVersion: '${evm_version}'"
|
|
|
|
echo " }"
|
|
|
|
echo "}"
|
|
|
|
}
|
|
|
|
|
2021-10-25 12:29:43 +00:00
|
|
|
function compile_and_run_test
|
2019-09-25 13:59:52 +00:00
|
|
|
{
|
2021-10-25 12:29:43 +00:00
|
|
|
local compile_fn="$1"
|
|
|
|
local test_fn="$2"
|
|
|
|
local verify_fn="$3"
|
2019-09-25 13:59:52 +00:00
|
|
|
|
2021-10-25 12:29:43 +00:00
|
|
|
printLog "Running compile function..."
|
|
|
|
$compile_fn
|
|
|
|
$verify_fn "$SOLCVERSION_SHORT" "$SOLCVERSION"
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2021-10-25 12:29:43 +00:00
|
|
|
if [[ "$COMPILE_ONLY" == 1 ]]; then
|
|
|
|
printLog "Skipping test function..."
|
|
|
|
else
|
|
|
|
printLog "Running test function..."
|
|
|
|
$test_fn
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function truffle_run_test
|
|
|
|
{
|
|
|
|
local config_file="$1"
|
2021-12-03 13:51:05 +00:00
|
|
|
local binary_type="$2"
|
|
|
|
local solc_path="$3"
|
|
|
|
local optimizer_level="$4"
|
|
|
|
local compile_fn="$5"
|
|
|
|
local test_fn="$6"
|
2021-10-25 12:29:43 +00:00
|
|
|
|
|
|
|
truffle_clean
|
2021-12-03 13:51:05 +00:00
|
|
|
force_truffle_compiler_settings "$config_file" "$binary_type" "$solc_path" "$optimizer_level"
|
2021-10-25 12:29:43 +00:00
|
|
|
compile_and_run_test compile_fn test_fn truffle_verify_compiler_version
|
2019-04-01 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:00 +00:00
|
|
|
function hardhat_run_test
|
|
|
|
{
|
|
|
|
local config_file="$1"
|
|
|
|
local optimizer_level="$2"
|
|
|
|
local compile_fn="$3"
|
|
|
|
local test_fn="$4"
|
|
|
|
|
|
|
|
hardhat_clean
|
|
|
|
force_hardhat_compiler_settings "$config_file" "$optimizer_level"
|
|
|
|
compile_and_run_test compile_fn test_fn hardhat_verify_compiler_version
|
|
|
|
}
|
|
|
|
|
2019-04-01 12:32:53 +00:00
|
|
|
function external_test
|
|
|
|
{
|
|
|
|
local name="$1"
|
|
|
|
local main_fn="$2"
|
|
|
|
|
|
|
|
printTask "Testing $name..."
|
|
|
|
echo "==========================="
|
2021-10-26 10:50:30 +00:00
|
|
|
DIR=$(mktemp -d -t "ext-test-${name}-XXXXXX")
|
2019-04-01 12:32:53 +00:00
|
|
|
(
|
2021-12-03 11:53:50 +00:00
|
|
|
[[ "$main_fn" != "" ]] || fail "Test main function not defined."
|
2019-04-01 12:32:53 +00:00
|
|
|
$main_fn
|
|
|
|
)
|
|
|
|
rm -rf "$DIR"
|
|
|
|
echo "Done."
|
|
|
|
}
|