2018-12-06 14:59:58 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-12-06 14:59:58 +00:00
|
|
|
/**
|
|
|
|
* Tests that check that the cost of certain operations stay within range.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/libsolidity/SolidityExecutionFramework.h>
|
2019-11-07 14:10:08 +00:00
|
|
|
#include <liblangutil/EVMVersion.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/IpfsHash.h>
|
2019-05-16 18:13:42 +00:00
|
|
|
#include <libevmasm/GasMeter.h>
|
2018-12-06 14:59:58 +00:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::test;
|
2018-12-06 14:59:58 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::frontend::test
|
2018-12-06 14:59:58 +00:00
|
|
|
{
|
|
|
|
|
2019-11-07 14:10:08 +00:00
|
|
|
#define CHECK_DEPLOY_GAS(_gasNoOpt, _gasOpt, _evmVersion) \
|
2019-05-16 18:13:42 +00:00
|
|
|
do \
|
|
|
|
{ \
|
2020-07-28 21:20:49 +00:00
|
|
|
u256 metaCost = GasMeter::dataGas(m_compiler.cborMetadata(m_compiler.lastContractName()), true, _evmVersion); \
|
2019-05-16 18:13:42 +00:00
|
|
|
u256 gasOpt{_gasOpt}; \
|
|
|
|
u256 gasNoOpt{_gasNoOpt}; \
|
|
|
|
u256 gas = m_optimiserSettings == OptimiserSettings::minimal() ? gasNoOpt : gasOpt; \
|
|
|
|
BOOST_CHECK_MESSAGE( \
|
2020-07-28 21:20:49 +00:00
|
|
|
m_gasUsed >= metaCost, \
|
2019-05-16 18:13:42 +00:00
|
|
|
"Gas used: " + \
|
|
|
|
m_gasUsed.str() + \
|
2020-07-28 21:20:49 +00:00
|
|
|
" is less than the data cost for the cbor metadata: " + \
|
|
|
|
u256(metaCost).str() \
|
2019-05-16 18:13:42 +00:00
|
|
|
); \
|
2020-07-28 21:20:49 +00:00
|
|
|
u256 gasUsed = m_gasUsed - metaCost; \
|
2019-05-16 18:13:42 +00:00
|
|
|
BOOST_CHECK_MESSAGE( \
|
|
|
|
gas == gasUsed, \
|
|
|
|
"Gas used: " + \
|
|
|
|
gasUsed.str() + \
|
|
|
|
" - expected: " + \
|
|
|
|
gas.str() \
|
|
|
|
); \
|
|
|
|
} while(0)
|
|
|
|
|
2018-12-06 14:59:58 +00:00
|
|
|
#define CHECK_GAS(_gasNoOpt, _gasOpt, _tolerance) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
u256 gasOpt{_gasOpt}; \
|
|
|
|
u256 gasNoOpt{_gasNoOpt}; \
|
|
|
|
u256 tolerance{_tolerance}; \
|
2019-03-12 14:14:12 +00:00
|
|
|
u256 gas = m_optimiserSettings == OptimiserSettings::minimal() ? gasNoOpt : gasOpt; \
|
2018-12-06 14:59:58 +00:00
|
|
|
u256 diff = gas < m_gasUsed ? m_gasUsed - gas : gas - m_gasUsed; \
|
|
|
|
BOOST_CHECK_MESSAGE( \
|
|
|
|
diff <= tolerance, \
|
|
|
|
"Gas used: " + \
|
|
|
|
m_gasUsed.str() + \
|
|
|
|
" - expected: " + \
|
|
|
|
gas.str() + \
|
|
|
|
" (tolerance: " + \
|
|
|
|
tolerance.str() + \
|
|
|
|
")" \
|
|
|
|
); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(GasCostTests, SolidityExecutionFramework)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(string_storage)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract C {
|
|
|
|
function f() pure public {
|
|
|
|
require(false, "Not Authorized. This function can only be called by the custodian or owner of this contract");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
2021-02-11 08:46:25 +00:00
|
|
|
m_compiler.setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata);
|
2018-12-06 14:59:58 +00:00
|
|
|
compileAndRun(sourceCode);
|
|
|
|
|
2020-01-14 16:48:17 +00:00
|
|
|
auto evmVersion = solidity::test::CommonOptions::get().evmVersion();
|
2019-11-07 14:10:08 +00:00
|
|
|
|
|
|
|
if (evmVersion <= EVMVersion::byzantium())
|
2020-12-02 13:15:26 +00:00
|
|
|
{
|
|
|
|
if (CommonOptions::get().useABIEncoderV1)
|
|
|
|
CHECK_DEPLOY_GAS(133045, 129731, evmVersion);
|
|
|
|
else
|
2021-09-07 16:01:45 +00:00
|
|
|
CHECK_DEPLOY_GAS(144999, 121229, evmVersion);
|
2020-12-02 13:15:26 +00:00
|
|
|
}
|
2019-02-19 17:55:36 +00:00
|
|
|
// This is only correct on >=Constantinople.
|
2020-11-19 15:58:41 +00:00
|
|
|
else if (!CommonOptions::get().useABIEncoderV1)
|
2019-04-02 15:36:03 +00:00
|
|
|
{
|
2020-05-06 09:49:05 +00:00
|
|
|
if (CommonOptions::get().optimize)
|
2019-11-19 13:22:21 +00:00
|
|
|
{
|
2019-11-19 13:25:40 +00:00
|
|
|
// Costs with 0 are cases which cannot be triggered in tests.
|
2019-11-19 13:22:21 +00:00
|
|
|
if (evmVersion < EVMVersion::istanbul())
|
2021-02-11 08:46:25 +00:00
|
|
|
CHECK_DEPLOY_GAS(0, 109241, evmVersion);
|
2019-11-19 13:22:21 +00:00
|
|
|
else
|
2021-02-11 08:46:25 +00:00
|
|
|
CHECK_DEPLOY_GAS(0, 97697, evmVersion);
|
2019-11-19 13:22:21 +00:00
|
|
|
}
|
2019-04-02 15:36:03 +00:00
|
|
|
else
|
2019-11-19 13:22:21 +00:00
|
|
|
{
|
|
|
|
if (evmVersion < EVMVersion::istanbul())
|
2021-09-07 16:01:45 +00:00
|
|
|
CHECK_DEPLOY_GAS(139013, 123969, evmVersion);
|
2019-11-19 13:22:21 +00:00
|
|
|
else
|
2021-09-07 16:01:45 +00:00
|
|
|
CHECK_DEPLOY_GAS(123361, 110969, evmVersion);
|
2019-11-19 13:22:21 +00:00
|
|
|
}
|
2019-04-02 15:36:03 +00:00
|
|
|
}
|
2019-11-19 12:34:56 +00:00
|
|
|
else if (evmVersion < EVMVersion::istanbul())
|
2020-07-28 21:20:49 +00:00
|
|
|
CHECK_DEPLOY_GAS(125829, 118559, evmVersion);
|
2018-12-06 14:59:58 +00:00
|
|
|
else
|
2021-02-11 08:46:25 +00:00
|
|
|
CHECK_DEPLOY_GAS(114077, 96461, evmVersion);
|
2019-09-06 17:11:07 +00:00
|
|
|
|
2019-11-07 14:10:08 +00:00
|
|
|
if (evmVersion >= EVMVersion::byzantium())
|
2018-12-06 14:59:58 +00:00
|
|
|
{
|
|
|
|
callContractFunction("f()");
|
2019-11-07 14:10:08 +00:00
|
|
|
if (evmVersion == EVMVersion::byzantium())
|
2021-01-14 12:02:14 +00:00
|
|
|
CHECK_GAS(21741, 21522, 20);
|
2019-02-19 17:55:36 +00:00
|
|
|
// This is only correct on >=Constantinople.
|
2020-11-19 15:58:41 +00:00
|
|
|
else if (!CommonOptions::get().useABIEncoderV1)
|
2019-04-02 15:36:03 +00:00
|
|
|
{
|
2020-05-06 09:49:05 +00:00
|
|
|
if (CommonOptions::get().optimize)
|
2019-11-19 13:22:21 +00:00
|
|
|
{
|
|
|
|
if (evmVersion < EVMVersion::istanbul())
|
2021-01-14 12:02:14 +00:00
|
|
|
CHECK_GAS(0, 21526, 20);
|
2019-11-19 13:22:21 +00:00
|
|
|
else
|
2021-01-14 12:02:14 +00:00
|
|
|
CHECK_GAS(0, 21318, 20);
|
2019-11-19 13:22:21 +00:00
|
|
|
}
|
2019-04-02 15:36:03 +00:00
|
|
|
else
|
2019-11-19 13:22:21 +00:00
|
|
|
{
|
|
|
|
if (evmVersion < EVMVersion::istanbul())
|
2020-12-22 22:11:53 +00:00
|
|
|
CHECK_GAS(21736, 21559, 20);
|
2019-11-19 13:22:21 +00:00
|
|
|
else
|
2020-12-22 22:11:53 +00:00
|
|
|
CHECK_GAS(21528, 21351, 20);
|
2019-11-19 13:22:21 +00:00
|
|
|
}
|
2019-04-02 15:36:03 +00:00
|
|
|
}
|
2019-11-19 12:34:56 +00:00
|
|
|
else if (evmVersion < EVMVersion::istanbul())
|
2018-12-06 14:59:58 +00:00
|
|
|
CHECK_GAS(21546, 21526, 20);
|
2019-11-19 12:34:56 +00:00
|
|
|
else
|
|
|
|
CHECK_GAS(21332, 21322, 20);
|
2018-12-06 14:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 15:42:09 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(single_callvaluecheck)
|
|
|
|
{
|
|
|
|
string sourceCode = R"(
|
|
|
|
// All functions nonpayable, we can check callvalue at the beginning
|
|
|
|
contract Nonpayable {
|
|
|
|
address a;
|
|
|
|
function f(address b) public {
|
|
|
|
a = b;
|
|
|
|
}
|
|
|
|
function f1(address b) public pure returns (uint c) {
|
2020-11-09 09:12:38 +00:00
|
|
|
return uint160(b) + 2;
|
2018-12-11 15:42:09 +00:00
|
|
|
}
|
|
|
|
function f2(address b) public pure returns (uint) {
|
2020-11-09 09:12:38 +00:00
|
|
|
return uint160(b) + 8;
|
2018-12-11 15:42:09 +00:00
|
|
|
}
|
|
|
|
function f3(address, uint c) pure public returns (uint) {
|
|
|
|
return c - 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// At least on payable function, we cannot do the optimization.
|
|
|
|
contract Payable {
|
|
|
|
address a;
|
|
|
|
function f(address b) public {
|
|
|
|
a = b;
|
|
|
|
}
|
|
|
|
function f1(address b) public pure returns (uint c) {
|
2020-11-09 09:12:38 +00:00
|
|
|
return uint160(b) + 2;
|
2018-12-11 15:42:09 +00:00
|
|
|
}
|
|
|
|
function f2(address b) public pure returns (uint) {
|
2020-11-09 09:12:38 +00:00
|
|
|
return uint160(b) + 8;
|
2018-12-11 15:42:09 +00:00
|
|
|
}
|
|
|
|
function f3(address, uint c) payable public returns (uint) {
|
|
|
|
return c - 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
size_t bytecodeSizeNonpayable = m_compiler.object("Nonpayable").bytecode.size();
|
|
|
|
size_t bytecodeSizePayable = m_compiler.object("Payable").bytecode.size();
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(bytecodeSizePayable - bytecodeSizeNonpayable, 26);
|
|
|
|
}
|
|
|
|
|
2018-12-06 14:59:58 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|