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
|
|
|
|
|
2021-12-17 23:36:30 +00:00
|
|
|
# Requires $REPO_ROOT to be defined and "${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-12-17 15:01:19 +00:00
|
|
|
AVAILABLE_PRESETS=(
|
|
|
|
legacy-no-optimize
|
|
|
|
ir-no-optimize
|
|
|
|
legacy-optimize-evm-only
|
|
|
|
ir-optimize-evm-only
|
|
|
|
legacy-optimize-evm+yul
|
|
|
|
ir-optimize-evm+yul
|
|
|
|
)
|
|
|
|
|
|
|
|
function print_presets_or_exit
|
2021-10-28 10:46:34 +00:00
|
|
|
{
|
2021-12-09 13:37:21 +00:00
|
|
|
local selected_presets="$1"
|
2021-10-28 10:46:34 +00:00
|
|
|
|
2021-12-09 13:37:21 +00:00
|
|
|
[[ $selected_presets != "" ]] || { printWarning "No presets to run. Exiting."; exit 0; }
|
2021-10-28 10:46:34 +00:00
|
|
|
|
2021-12-09 13:37:21 +00:00
|
|
|
printLog "Selected settings presets: ${selected_presets}"
|
2021-10-28 10:46:34 +00:00
|
|
|
}
|
|
|
|
|
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"
|
2021-12-17 15:01:19 +00:00
|
|
|
local selected_presets="$3"
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2021-12-17 15:01:19 +00:00
|
|
|
(( $# >= 2 && $# <= 3 )) || fail "Usage: $0 native|solcjs <path to solc or soljson.js> [preset]"
|
2021-12-03 13:51:05 +00:00
|
|
|
[[ $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}'"
|
2021-12-17 15:01:19 +00:00
|
|
|
|
|
|
|
if [[ $selected_presets != "" ]]
|
|
|
|
then
|
|
|
|
for preset in $selected_presets
|
|
|
|
do
|
|
|
|
if [[ " ${AVAILABLE_PRESETS[*]} " != *" $preset "* ]]
|
|
|
|
then
|
|
|
|
fail "Preset '${preset}' does not exist. Available presets: ${AVAILABLE_PRESETS[*]}."
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
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/}"
|
2022-01-28 19:51:04 +00:00
|
|
|
local solcjs_dir="$6"
|
2021-12-03 13:51:05 +00:00
|
|
|
|
|
|
|
[[ $binary_type == native || $binary_type == solcjs ]] || assertFail
|
2022-01-28 19:51:04 +00:00
|
|
|
[[ $binary_type == solcjs || $solcjs_dir == "" ]] || 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..."
|
2022-01-28 19:51:04 +00:00
|
|
|
if [[ $solcjs_dir == "" ]]; then
|
|
|
|
printLog "Cloning branch ${solcjs_branch}..."
|
|
|
|
git clone --depth 1 -b "$solcjs_branch" https://github.com/ethereum/solc-js.git "$install_dir"
|
|
|
|
else
|
|
|
|
printLog "Using local solc-js from ${solcjs_dir}..."
|
|
|
|
cp -ra "$solcjs_dir" solc
|
|
|
|
fi
|
2021-12-03 13:51:05 +00:00
|
|
|
|
|
|
|
pushd "$install_dir"
|
|
|
|
npm install
|
|
|
|
cp "$binary_path" soljson.js
|
2022-01-25 15:42:37 +00:00
|
|
|
npm run build
|
|
|
|
SOLCVERSION=$(dist/solc.js --version)
|
2021-12-03 13:51:05 +00:00
|
|
|
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"
|
2022-01-10 16:25:20 +00:00
|
|
|
local ref_type="$2"
|
|
|
|
local solcjs_ref="$3"
|
|
|
|
local test_dir="$4"
|
|
|
|
|
|
|
|
[[ $ref_type == commit || $ref_type == branch || $ref_type == tag ]] || assertFail
|
|
|
|
|
|
|
|
printLog "Cloning ${ref_type} ${solcjs_ref} of ${repo}..."
|
|
|
|
if [[ $ref_type == commit ]]; then
|
|
|
|
mkdir ext
|
|
|
|
cd ext
|
|
|
|
git init
|
|
|
|
git remote add origin "$repo"
|
|
|
|
git fetch --depth 1 origin "$solcjs_ref"
|
|
|
|
git reset --hard FETCH_HEAD
|
|
|
|
else
|
|
|
|
git clone --depth 1 "$repo" -b "$solcjs_ref" "$test_dir/ext"
|
|
|
|
cd ext
|
|
|
|
fi
|
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 11:02:42 +00:00
|
|
|
function neutralize_packaged_contracts
|
|
|
|
{
|
|
|
|
# Frameworks will build contracts from any package that contains a configuration file.
|
|
|
|
# This is both unnecessary (any files imported from these packages will get compiled again as a
|
|
|
|
# part of the main project anyway) and trips up our version check because it won't use our
|
|
|
|
# custom compiler binary.
|
|
|
|
printLog "Removing framework config and artifacts from npm packages..."
|
|
|
|
find node_modules/ -type f '(' -name 'hardhat.config.*' -o -name 'truffle-config.*' ')' -delete
|
|
|
|
|
|
|
|
# Some npm packages also come packaged with pre-built artifacts.
|
|
|
|
find node_modules/ -path '*artifacts/build-info/*.json' -delete
|
|
|
|
}
|
|
|
|
|
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"
|
2021-12-09 13:37:21 +00:00
|
|
|
local preset="$4"
|
2021-12-03 13:51:05 +00:00
|
|
|
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"
|
2021-12-09 13:37:21 +00:00
|
|
|
echo "Settings preset: ${preset}"
|
|
|
|
echo "Settings: $(settings_from_preset "$preset" "$evm_version")"
|
2020-12-07 19:00:53 +00:00
|
|
|
echo "EVM version: $evm_version"
|
2021-12-09 13:37:21 +00:00
|
|
|
echo "Compiler version: ${SOLCVERSION_SHORT}"
|
|
|
|
echo "Compiler version (full): ${SOLCVERSION}"
|
2019-04-01 12:32:53 +00:00
|
|
|
echo "-------------------------------------"
|
|
|
|
|
2021-12-17 23:36:30 +00:00
|
|
|
local compiler_settings gas_reporter_settings
|
|
|
|
compiler_settings=$(truffle_compiler_settings "$solc_path" "$preset" "$evm_version")
|
|
|
|
gas_reporter_settings=$(eth_gas_reporter_settings "$preset")
|
|
|
|
|
|
|
|
{
|
|
|
|
echo "require('eth-gas-reporter');"
|
|
|
|
echo "module.exports['mocha'] = {"
|
|
|
|
echo " reporter: 'eth-gas-reporter',"
|
|
|
|
echo " reporterOptions: ${gas_reporter_settings}"
|
|
|
|
echo "};"
|
|
|
|
|
|
|
|
echo "module.exports['compilers'] = ${compiler_settings};"
|
|
|
|
} >> "$config_file"
|
2019-04-01 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 22:08:01 +00:00
|
|
|
function name_hardhat_default_export
|
|
|
|
{
|
|
|
|
local config_file="$1"
|
|
|
|
local config_var_name="$2"
|
|
|
|
|
|
|
|
local import="import {HardhatUserConfig} from 'hardhat/types';"
|
|
|
|
local config="const config: HardhatUserConfig = {"
|
|
|
|
sed -i "s|^\s*export\s*default\s*{|${import}\n${config}|g" "$config_file"
|
|
|
|
echo "export default config;" >> "$config_file"
|
|
|
|
}
|
|
|
|
|
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-10-26 09:12:45 +00:00
|
|
|
|
|
|
|
local language="${config_file##*.}"
|
|
|
|
hardhat_solc_build_subtask "$SOLCVERSION_SHORT" "$SOLCVERSION" "$binary_type" "$solc_path" "$language" >> "$config_file"
|
2021-10-26 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 16:46:40 +00:00
|
|
|
function force_hardhat_unlimited_contract_size
|
|
|
|
{
|
|
|
|
local config_file="$1"
|
|
|
|
local config_var_name="$2"
|
|
|
|
|
|
|
|
printLog "Configuring Hardhat..."
|
|
|
|
echo "-------------------------------------"
|
|
|
|
echo "Allow unlimited contract size: true"
|
|
|
|
echo "-------------------------------------"
|
|
|
|
|
|
|
|
if [[ $config_file == *\.js ]]; then
|
|
|
|
[[ $config_var_name == "" ]] || assertFail
|
2022-01-19 16:05:00 +00:00
|
|
|
echo "module.exports.networks.hardhat = module.exports.networks.hardhat || {allowUnlimitedContractSize: undefined}"
|
|
|
|
echo "module.exports.networks.hardhat.allowUnlimitedContractSize = true"
|
2022-01-13 16:46:40 +00:00
|
|
|
else
|
|
|
|
[[ $config_file == *\.ts ]] || assertFail
|
|
|
|
[[ $config_var_name != "" ]] || assertFail
|
2022-01-21 15:10:08 +00:00
|
|
|
echo "${config_var_name}.networks!.hardhat = ${config_var_name}.networks!.hardhat ?? {allowUnlimitedContractSize: undefined};"
|
2022-01-19 16:05:00 +00:00
|
|
|
echo "${config_var_name}.networks!.hardhat!.allowUnlimitedContractSize = true"
|
|
|
|
fi >> "$config_file"
|
2022-01-13 16:46:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:00 +00:00
|
|
|
function force_hardhat_compiler_settings
|
|
|
|
{
|
|
|
|
local config_file="$1"
|
2021-12-09 13:37:21 +00:00
|
|
|
local preset="$2"
|
2021-10-26 09:12:45 +00:00
|
|
|
local config_var_name="$3"
|
|
|
|
local evm_version="${4:-"$CURRENT_EVM_VERSION"}"
|
2021-10-26 13:49:00 +00:00
|
|
|
|
|
|
|
printLog "Configuring Hardhat..."
|
|
|
|
echo "-------------------------------------"
|
|
|
|
echo "Config file: ${config_file}"
|
2021-12-09 13:37:21 +00:00
|
|
|
echo "Settings preset: ${preset}"
|
|
|
|
echo "Settings: $(settings_from_preset "$preset" "$evm_version")"
|
2021-10-26 13:49:00 +00:00
|
|
|
echo "EVM version: ${evm_version}"
|
|
|
|
echo "Compiler version: ${SOLCVERSION_SHORT}"
|
|
|
|
echo "Compiler version (full): ${SOLCVERSION}"
|
|
|
|
echo "-------------------------------------"
|
|
|
|
|
2021-12-17 23:36:30 +00:00
|
|
|
local compiler_settings gas_reporter_settings
|
|
|
|
compiler_settings=$(hardhat_compiler_settings "$SOLCVERSION_SHORT" "$preset" "$evm_version")
|
|
|
|
gas_reporter_settings=$(eth_gas_reporter_settings "$preset")
|
2021-10-26 09:12:45 +00:00
|
|
|
if [[ $config_file == *\.js ]]; then
|
|
|
|
[[ $config_var_name == "" ]] || assertFail
|
2021-12-17 23:36:30 +00:00
|
|
|
echo "require('hardhat-gas-reporter');"
|
|
|
|
echo "module.exports.gasReporter = ${gas_reporter_settings};"
|
|
|
|
echo "module.exports.solidity = ${compiler_settings};"
|
2021-10-26 09:12:45 +00:00
|
|
|
else
|
|
|
|
[[ $config_file == *\.ts ]] || assertFail
|
|
|
|
[[ $config_var_name != "" ]] || assertFail
|
2021-12-17 23:36:30 +00:00
|
|
|
echo 'import "hardhat-gas-reporter";'
|
|
|
|
echo "${config_var_name}.gasReporter = ${gas_reporter_settings};"
|
|
|
|
echo "${config_var_name}.solidity = {compilers: [${compiler_settings}]};"
|
|
|
|
fi >> "$config_file"
|
2021-10-26 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
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..."
|
2021-10-27 11:02:42 +00:00
|
|
|
local build_info_files
|
|
|
|
build_info_files=$(find . -path '*artifacts/build-info/*.json')
|
|
|
|
for build_info_file in $build_info_files; do
|
|
|
|
grep '"solcVersion": "'"${solc_version}"'"' --with-filename "$build_info_file" || fail "Wrong compiler version detected in ${build_info_file}."
|
|
|
|
grep '"solcLongVersion": "'"${full_solc_version}"'"' --with-filename "$build_info_file" || fail "Wrong compiler version detected in ${build_info_file}."
|
|
|
|
done
|
2021-10-26 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2021-12-17 23:08:59 +00:00
|
|
|
rm -rf build/ artifacts/ cache/
|
2021-10-26 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 13:37:21 +00:00
|
|
|
function settings_from_preset
|
2021-10-28 09:58:47 +00:00
|
|
|
{
|
2021-12-09 13:37:21 +00:00
|
|
|
local preset="$1"
|
|
|
|
local evm_version="$2"
|
2020-12-07 19:00:53 +00:00
|
|
|
|
2021-12-17 15:01:19 +00:00
|
|
|
[[ " ${AVAILABLE_PRESETS[*]} " == *" $preset "* ]] || assertFail
|
|
|
|
|
2021-12-09 13:37:21 +00:00
|
|
|
case "$preset" in
|
2021-12-09 14:35:16 +00:00
|
|
|
# NOTE: Remember to update `parallelism` of `t_ems_ext` job in CI config if you add/remove presets
|
|
|
|
legacy-no-optimize) echo "{evmVersion: '${evm_version}', viaIR: false, optimizer: {enabled: false}}" ;;
|
|
|
|
ir-no-optimize) echo "{evmVersion: '${evm_version}', viaIR: true, optimizer: {enabled: false}}" ;;
|
|
|
|
legacy-optimize-evm-only) echo "{evmVersion: '${evm_version}', viaIR: false, optimizer: {enabled: true, details: {yul: false}}}" ;;
|
|
|
|
ir-optimize-evm-only) echo "{evmVersion: '${evm_version}', viaIR: true, optimizer: {enabled: true, details: {yul: false}}}" ;;
|
|
|
|
legacy-optimize-evm+yul) echo "{evmVersion: '${evm_version}', viaIR: false, optimizer: {enabled: true, details: {yul: true}}}" ;;
|
|
|
|
ir-optimize-evm+yul) echo "{evmVersion: '${evm_version}', viaIR: true, optimizer: {enabled: true, details: {yul: true}}}" ;;
|
2020-12-07 19:00:53 +00:00
|
|
|
*)
|
2021-12-09 13:37:21 +00:00
|
|
|
fail "Unknown settings preset: '${preset}'."
|
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-12-17 23:36:30 +00:00
|
|
|
function eth_gas_reporter_settings
|
|
|
|
{
|
|
|
|
local preset="$1"
|
|
|
|
|
|
|
|
echo "{"
|
|
|
|
echo " enabled: true,"
|
|
|
|
echo " gasPrice: 1," # Gas price does not matter to us at all. Set to whatever to avoid API call.
|
|
|
|
echo " noColors: true,"
|
|
|
|
echo " showTimeSpent: false," # We're not interested in test timing
|
|
|
|
echo " onlyCalledMethods: true," # Exclude entries with no gas for shorter report
|
|
|
|
echo " showMethodSig: true," # Should make diffs more stable if there are overloaded functions
|
|
|
|
echo " outputFile: \"$(gas_report_path "$preset")\""
|
|
|
|
echo "}"
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:58:47 +00:00
|
|
|
function truffle_compiler_settings
|
|
|
|
{
|
2020-12-08 15:21:46 +00:00
|
|
|
local solc_path="$1"
|
2021-12-09 13:37:21 +00:00
|
|
|
local preset="$2"
|
2020-12-08 15:21:46 +00:00
|
|
|
local evm_version="$3"
|
|
|
|
|
|
|
|
echo "{"
|
|
|
|
echo " solc: {"
|
|
|
|
echo " version: \"${solc_path}\","
|
2021-12-09 13:37:21 +00:00
|
|
|
echo " settings: $(settings_from_preset "$preset" "$evm_version")"
|
2020-12-08 15:21:46 +00:00
|
|
|
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"
|
2021-10-26 09:12:45 +00:00
|
|
|
local language="$5"
|
2021-12-03 13:51:05 +00:00
|
|
|
|
|
|
|
[[ $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
|
|
|
|
2021-10-26 09:12:45 +00:00
|
|
|
if [[ $language == js ]]; then
|
|
|
|
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) => {"
|
|
|
|
else
|
|
|
|
[[ $language == ts ]] || assertFail
|
|
|
|
echo "import {TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD} from 'hardhat/builtin-tasks/task-names';"
|
|
|
|
echo "import assert = require('assert');"
|
|
|
|
echo "import {subtask} from 'hardhat/config';"
|
|
|
|
echo
|
|
|
|
echo "subtask(TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD, async (args: any, _hre: any, _runSuper: any) => {"
|
|
|
|
fi
|
|
|
|
|
2021-10-26 13:49:00 +00:00
|
|
|
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"
|
2021-12-09 13:37:21 +00:00
|
|
|
local preset="$2"
|
2021-10-26 13:49:00 +00:00
|
|
|
local evm_version="$3"
|
|
|
|
|
|
|
|
echo "{"
|
|
|
|
echo " version: '${solc_version}',"
|
2021-12-09 13:37:21 +00:00
|
|
|
echo " settings: $(settings_from_preset "$preset" "$evm_version")"
|
2021-10-26 13:49:00 +00:00
|
|
|
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"
|
2021-12-10 18:27:18 +00:00
|
|
|
local preset="$4"
|
|
|
|
local compile_only_presets="$5"
|
|
|
|
|
|
|
|
[[ $preset != *" "* ]] || assertFail "Preset names must not contain spaces."
|
2019-09-25 13:59:52 +00:00
|
|
|
|
2021-10-25 12:29:43 +00:00
|
|
|
printLog "Running compile function..."
|
2021-11-29 13:20:42 +00:00
|
|
|
time $compile_fn
|
2021-10-25 12:29:43 +00:00
|
|
|
$verify_fn "$SOLCVERSION_SHORT" "$SOLCVERSION"
|
2019-04-01 12:32:53 +00:00
|
|
|
|
2021-12-10 18:27:18 +00:00
|
|
|
if [[ "$COMPILE_ONLY" == 1 || " $compile_only_presets " == *" $preset "* ]]; then
|
2021-10-25 12:29:43 +00:00
|
|
|
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"
|
2021-12-09 13:37:21 +00:00
|
|
|
local preset="$4"
|
2021-12-10 18:27:18 +00:00
|
|
|
local compile_only_presets="$5"
|
|
|
|
local compile_fn="$6"
|
|
|
|
local test_fn="$7"
|
2021-10-25 12:29:43 +00:00
|
|
|
|
|
|
|
truffle_clean
|
2021-12-09 13:37:21 +00:00
|
|
|
force_truffle_compiler_settings "$config_file" "$binary_type" "$solc_path" "$preset"
|
2021-12-10 18:27:18 +00:00
|
|
|
compile_and_run_test compile_fn test_fn truffle_verify_compiler_version "$preset" "$compile_only_presets"
|
2019-04-01 12:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:00 +00:00
|
|
|
function hardhat_run_test
|
|
|
|
{
|
|
|
|
local config_file="$1"
|
2021-12-09 13:37:21 +00:00
|
|
|
local preset="$2"
|
2021-12-10 18:27:18 +00:00
|
|
|
local compile_only_presets="$3"
|
|
|
|
local compile_fn="$4"
|
|
|
|
local test_fn="$5"
|
2021-10-26 09:12:45 +00:00
|
|
|
local config_var_name="$6"
|
2021-10-26 13:49:00 +00:00
|
|
|
|
|
|
|
hardhat_clean
|
2021-10-26 09:12:45 +00:00
|
|
|
force_hardhat_compiler_settings "$config_file" "$preset" "$config_var_name"
|
2021-12-10 18:27:18 +00:00
|
|
|
compile_and_run_test compile_fn test_fn hardhat_verify_compiler_version "$preset" "$compile_only_presets"
|
2021-10-26 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
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."
|
|
|
|
}
|
2021-12-17 23:36:30 +00:00
|
|
|
|
|
|
|
function gas_report_path
|
|
|
|
{
|
|
|
|
local preset="$1"
|
|
|
|
|
|
|
|
echo "${DIR}/gas-report-${preset}.rst"
|
|
|
|
}
|
|
|
|
|
|
|
|
function gas_report_to_json
|
|
|
|
{
|
|
|
|
cat - | "${REPO_ROOT}/scripts/externalTests/parse_eth_gas_report.py" | jq '{gas: .}'
|
|
|
|
}
|
|
|
|
|
|
|
|
function detect_hardhat_artifact_dir
|
|
|
|
{
|
|
|
|
if [[ -e build/ && -e artifacts/ ]]; then
|
|
|
|
fail "Cannot determine Hardhat artifact location. Both build/ and artifacts/ exist"
|
|
|
|
elif [[ -e build/ ]]; then
|
|
|
|
echo -n build/artifacts
|
|
|
|
elif [[ -e artifacts/ ]]; then
|
|
|
|
echo -n artifacts
|
|
|
|
else
|
|
|
|
fail "Hardhat build artifacts not found."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function bytecode_size_json_from_truffle_artifacts
|
|
|
|
{
|
|
|
|
# NOTE: The output of this function is a series of concatenated JSON dicts rather than a list.
|
|
|
|
|
|
|
|
for artifact in build/contracts/*.json; do
|
|
|
|
if [[ $(jq '. | has("unlinked_binary")' "$artifact") == false ]]; then
|
|
|
|
# Each artifact represents compilation output for a single contract. Some top-level keys contain
|
|
|
|
# bits of Standard JSON output while others are generated by Truffle. Process it into a dict
|
|
|
|
# of the form `{"<file>": {"<contract>": <size>}}`.
|
|
|
|
# NOTE: The `bytecode` field starts with 0x, which is why we subtract 1 from size.
|
|
|
|
jq '{
|
|
|
|
(.ast.absolutePath): {
|
|
|
|
(.contractName): (.bytecode | length / 2 - 1)
|
|
|
|
}
|
|
|
|
}' "$artifact"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function bytecode_size_json_from_hardhat_artifacts
|
|
|
|
{
|
|
|
|
# NOTE: The output of this function is a series of concatenated JSON dicts rather than a list.
|
|
|
|
|
|
|
|
for artifact in "$(detect_hardhat_artifact_dir)"/build-info/*.json; do
|
|
|
|
# Each artifact contains Standard JSON output under the `output` key.
|
|
|
|
# Process it into a dict of the form `{"<file>": {"<contract>": <size>}}`,
|
|
|
|
# Note that one Hardhat artifact often represents multiple input files.
|
|
|
|
jq '.output.contracts | to_entries[] | {
|
|
|
|
"\(.key)": .value | to_entries[] | {
|
|
|
|
"\(.key)": (.value.evm.bytecode.object | length / 2)
|
|
|
|
}
|
|
|
|
}' "$artifact"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function combine_artifact_json
|
|
|
|
{
|
|
|
|
# Combine all dicts into a list with `jq --slurp` and then use `reduce` to merge them into one
|
|
|
|
# big dict with keys of the form `"<file>:<contract>"`. Then run jq again to filter out items
|
|
|
|
# with zero size and put the rest under under a top-level `bytecode_size` key. Also add another
|
|
|
|
# key with total bytecode size.
|
|
|
|
# NOTE: The extra inner `bytecode_size` key is there only to make diffs more readable.
|
|
|
|
cat - |
|
|
|
|
jq --slurp 'reduce (.[] | to_entries[]) as {$key, $value} ({}; . + {
|
|
|
|
($key + ":" + ($value | to_entries[].key)): {
|
|
|
|
bytecode_size: $value | to_entries[].value
|
|
|
|
}
|
|
|
|
})' |
|
|
|
|
jq --indent 4 --sort-keys '{
|
|
|
|
bytecode_size: [. | to_entries[] | select(.value.bytecode_size > 0)] | from_entries,
|
|
|
|
total_bytecode_size: (reduce (. | to_entries[]) as {$key, $value} (0; . + $value.bytecode_size))
|
|
|
|
}'
|
|
|
|
}
|
|
|
|
|
|
|
|
function project_info_json
|
|
|
|
{
|
|
|
|
local project_url="$1"
|
|
|
|
|
|
|
|
echo "{"
|
|
|
|
echo " \"project\": {"
|
|
|
|
# NOTE: Given that we clone with `--depth 1`, we'll only get useful output out of `git describe`
|
|
|
|
# if we directly check out a tag. Still better than nothing.
|
|
|
|
echo " \"version\": \"$(git describe --always)\","
|
|
|
|
echo " \"commit\": \"$(git rev-parse HEAD)\","
|
|
|
|
echo " \"url\": \"${project_url}\""
|
|
|
|
echo " }"
|
|
|
|
echo "}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function store_benchmark_report
|
|
|
|
{
|
|
|
|
local framework="$1"
|
|
|
|
local project_name="$2"
|
|
|
|
local project_url="$3"
|
|
|
|
local preset="$4"
|
|
|
|
|
|
|
|
[[ $framework == truffle || $framework == hardhat ]] || assertFail
|
|
|
|
[[ " ${AVAILABLE_PRESETS[*]} " == *" $preset "* ]] || assertFail
|
|
|
|
|
|
|
|
local report_dir="${REPO_ROOT}/reports/externalTests"
|
|
|
|
local output_file="${report_dir}/benchmark-${project_name}-${preset}.json"
|
|
|
|
mkdir -p "$report_dir"
|
|
|
|
|
|
|
|
{
|
|
|
|
if [[ -e $(gas_report_path "$preset") ]]; then
|
|
|
|
gas_report_to_json < "$(gas_report_path "$preset")"
|
|
|
|
fi
|
|
|
|
|
|
|
|
"bytecode_size_json_from_${framework}_artifacts" | combine_artifact_json
|
|
|
|
project_info_json "$project_url"
|
|
|
|
} | jq --slurp "{\"${project_name}\": {\"${preset}\": add}}" --indent 4 --sort-keys > "$output_file"
|
|
|
|
}
|