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
|
|
|
|
|
|
|
function verify_input
|
|
|
|
{
|
|
|
|
if [ ! -f "$1" ]; then
|
|
|
|
printError "Usage: $0 <path to soljson.js>"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-09-25 13:59:52 +00:00
|
|
|
function verify_version_input
|
|
|
|
{
|
|
|
|
if [ -z "$1" ] || [ ! -f "$1" ] || [ -z "$2" ]; then
|
|
|
|
printError "Usage: $0 <path to soljson.js> <version>"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function setup
|
|
|
|
{
|
2020-12-01 19:44:21 +00:00
|
|
|
local soljson="$1"
|
|
|
|
local branch="$2"
|
2019-09-25 13:59:52 +00:00
|
|
|
|
2020-12-01 19:44:21 +00:00
|
|
|
setup_solcjs "$DIR" "$soljson" "$branch" "solc"
|
2019-09-25 13:59:52 +00:00
|
|
|
cd solc
|
|
|
|
}
|
|
|
|
|
2019-04-01 12:32:53 +00:00
|
|
|
function setup_solcjs
|
|
|
|
{
|
|
|
|
local dir="$1"
|
|
|
|
local soljson="$2"
|
2019-09-25 13:59:52 +00:00
|
|
|
local branch="$3"
|
|
|
|
local path="$4"
|
2019-04-01 12:32:53 +00:00
|
|
|
|
|
|
|
cd "$dir"
|
|
|
|
printLog "Setting up solc-js..."
|
2019-09-25 13:59:52 +00:00
|
|
|
git clone --depth 1 -b "$branch" https://github.com/ethereum/solc-js.git "$path"
|
|
|
|
|
|
|
|
cd "$path"
|
|
|
|
|
2019-04-01 12:32:53 +00:00
|
|
|
npm install
|
|
|
|
cp "$soljson" soljson.js
|
|
|
|
SOLCVERSION=$(./solcjs --version)
|
|
|
|
printLog "Using solcjs version $SOLCVERSION"
|
|
|
|
cd ..
|
|
|
|
}
|
|
|
|
|
|
|
|
function download_project
|
|
|
|
{
|
|
|
|
local repo="$1"
|
|
|
|
local branch="$2"
|
|
|
|
local dir="$3"
|
|
|
|
|
|
|
|
printLog "Cloning $branch of $repo..."
|
|
|
|
git clone --depth 1 "$repo" -b "$branch" "$dir/ext"
|
|
|
|
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-09-25 13:59:52 +00:00
|
|
|
function truffle_setup
|
2019-04-01 12:32:53 +00:00
|
|
|
{
|
2020-12-01 19:44:21 +00:00
|
|
|
local soljson="$1"
|
|
|
|
local repo="$2"
|
|
|
|
local branch="$3"
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2020-12-01 19:44:21 +00:00
|
|
|
setup_solcjs "$DIR" "$soljson" "master" "solc"
|
2019-04-01 12:32:53 +00:00
|
|
|
download_project "$repo" "$branch" "$DIR"
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-08 15:23:37 +00:00
|
|
|
function force_truffle_solc_modules
|
2019-04-01 12:32:53 +00:00
|
|
|
{
|
2020-12-08 15:23:37 +00:00
|
|
|
local soljson="$1"
|
|
|
|
|
2019-04-01 12:32:53 +00:00
|
|
|
# Replace solc package by v0.5.0 and then overwrite with current version.
|
|
|
|
printLog "Forcing solc version for all Truffle modules..."
|
|
|
|
for d in node_modules node_modules/truffle/node_modules
|
|
|
|
do
|
|
|
|
(
|
|
|
|
if [ -d "$d" ]; then
|
|
|
|
cd $d
|
|
|
|
rm -rf solc
|
2019-12-11 16:48:11 +00:00
|
|
|
git clone --depth 1 -b master https://github.com/ethereum/solc-js.git solc
|
2020-12-08 15:23:37 +00:00
|
|
|
cp "$soljson" solc/soljson.js
|
2020-12-01 20:20:22 +00:00
|
|
|
|
|
|
|
cd solc
|
|
|
|
npm install
|
2019-04-01 12:32:53 +00:00
|
|
|
fi
|
|
|
|
)
|
|
|
|
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"
|
2020-12-08 15:23:37 +00:00
|
|
|
local solc_path="$2"
|
|
|
|
local level="$3"
|
|
|
|
local evm_version="$4"
|
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"
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function verify_compiler_version
|
|
|
|
{
|
|
|
|
local solc_version="$1"
|
|
|
|
|
|
|
|
printLog "Verify that the correct version ($solc_version) of the compiler was used to compile the contracts..."
|
|
|
|
grep -e "$solc_version" -r build/contracts > /dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
function clean
|
|
|
|
{
|
|
|
|
rm -rf build || true
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_install
|
|
|
|
{
|
2020-12-01 19:44:21 +00:00
|
|
|
local soljson="$1"
|
|
|
|
local init_fn="$2"
|
2019-04-01 12:32:53 +00:00
|
|
|
printLog "Running install function..."
|
2019-11-28 21:12:42 +00:00
|
|
|
|
|
|
|
replace_version_pragmas
|
2020-12-08 15:23:37 +00:00
|
|
|
force_truffle_solc_modules "$soljson"
|
2021-08-09 08:06:53 +00:00
|
|
|
force_truffle_compiler_settings "$CONFIG" "${DIR}/solc" "$OPTIMIZER_LEVEL" london
|
2019-11-28 21:12:42 +00:00
|
|
|
|
2019-04-01 12:32:53 +00:00
|
|
|
$init_fn
|
|
|
|
}
|
|
|
|
|
|
|
|
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}" ;;
|
|
|
|
2) echo "{enabled: true}" ;;
|
|
|
|
3) echo "{enabled: true, details: {yul: true}}" ;;
|
|
|
|
*)
|
|
|
|
printError "Optimizer level not found. Please define OPTIMIZER_LEVEL=[1, 2, 3]"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
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 "}"
|
|
|
|
}
|
|
|
|
|
2019-09-25 13:59:52 +00:00
|
|
|
function truffle_run_test
|
|
|
|
{
|
2020-12-01 19:44:21 +00:00
|
|
|
local soljson="$1"
|
|
|
|
local compile_fn="$2"
|
|
|
|
local test_fn="$3"
|
2019-09-25 13:59:52 +00:00
|
|
|
|
2019-10-30 13:28:19 +00:00
|
|
|
replace_version_pragmas
|
2020-12-08 15:23:37 +00:00
|
|
|
force_truffle_solc_modules "$soljson"
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2020-12-07 19:00:53 +00:00
|
|
|
for level in $(seq "$OPTIMIZER_LEVEL" 3)
|
2019-04-01 12:32:53 +00:00
|
|
|
do
|
|
|
|
clean
|
2021-08-09 08:06:53 +00:00
|
|
|
force_truffle_compiler_settings "$CONFIG" "${DIR}/solc" "$level" london
|
2019-04-01 12:32:53 +00:00
|
|
|
|
|
|
|
printLog "Running compile function..."
|
|
|
|
$compile_fn
|
|
|
|
verify_compiler_version "$SOLCVERSION"
|
2019-11-20 23:00:27 +00:00
|
|
|
|
|
|
|
if [[ "$COMPILE_ONLY" == 1 ]]; then
|
|
|
|
printLog "Skipping test function..."
|
|
|
|
else
|
|
|
|
printLog "Running test function..."
|
|
|
|
$test_fn
|
|
|
|
fi
|
2019-04-01 12:32:53 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
(
|
|
|
|
if [ -z "$main_fn" ]; then
|
|
|
|
printError "Test main function not defined."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
$main_fn
|
|
|
|
)
|
|
|
|
rm -rf "$DIR"
|
|
|
|
echo "Done."
|
|
|
|
}
|