2015-05-06 08:43:59 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-05-06 08:43:59 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-05-06 08:43:59 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-05-06 08:43:59 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-05-06 08:43:59 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-05-06 08:43:59 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Unit tests for the gas estimator.
|
|
|
|
*/
|
|
|
|
|
2016-06-09 16:54:29 +00:00
|
|
|
#include <test/libsolidity/SolidityExecutionFramework.h>
|
2015-05-19 22:27:07 +00:00
|
|
|
#include <libevmasm/GasMeter.h>
|
|
|
|
#include <libevmasm/KnownState.h>
|
2015-05-22 07:33:57 +00:00
|
|
|
#include <libevmasm/PathGasMeter.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/interface/GasEstimator.h>
|
2015-05-06 08:43:59 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::frontend::test;
|
2015-05-06 08:43:59 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::frontend::test
|
2015-05-06 08:43:59 +00:00
|
|
|
{
|
|
|
|
|
2016-11-28 00:21:01 +00:00
|
|
|
class GasMeterTestFramework: public SolidityExecutionFramework
|
2015-05-06 08:43:59 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
void compile(string const& _sourceCode)
|
|
|
|
{
|
2019-03-20 18:07:12 +00:00
|
|
|
m_compiler.reset();
|
2020-05-12 09:56:28 +00:00
|
|
|
m_compiler.setSources({{"", "pragma solidity >=0.0;\n"
|
|
|
|
"// SPDX-License-Identifier: GPL-3.0\n" + _sourceCode}});
|
2020-01-14 16:48:17 +00:00
|
|
|
m_compiler.setOptimiserSettings(solidity::test::CommonOptions::get().optimize);
|
2018-02-23 18:29:20 +00:00
|
|
|
m_compiler.setEVMVersion(m_evmVersion);
|
2017-09-20 09:52:41 +00:00
|
|
|
BOOST_REQUIRE_MESSAGE(m_compiler.compile(), "Compiling contract failed");
|
2015-05-06 08:43:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:58:20 +00:00
|
|
|
void testCreationTimeGas(string const& _sourceCode, u256 const& _tolerance = u256(0))
|
2015-05-19 22:27:07 +00:00
|
|
|
{
|
|
|
|
compileAndRun(_sourceCode);
|
|
|
|
auto state = make_shared<KnownState>();
|
2020-01-14 16:48:17 +00:00
|
|
|
PathGasMeter meter(*m_compiler.assemblyItems(m_compiler.lastContractName()), solidity::test::CommonOptions::get().evmVersion());
|
2015-05-22 07:33:57 +00:00
|
|
|
GasMeter::GasConsumption gas = meter.estimateMax(0, state);
|
2017-08-21 20:49:58 +00:00
|
|
|
u256 bytecodeSize(m_compiler.runtimeObject(m_compiler.lastContractName()).bytecode.size());
|
2016-06-21 16:13:07 +00:00
|
|
|
// costs for deployment
|
2017-10-03 10:48:47 +00:00
|
|
|
gas += bytecodeSize * GasCosts::createDataGas;
|
2016-06-21 16:13:07 +00:00
|
|
|
// costs for transaction
|
2017-08-21 20:49:58 +00:00
|
|
|
gas += gasForTransaction(m_compiler.object(m_compiler.lastContractName()).bytecode, true);
|
2016-06-21 16:13:07 +00:00
|
|
|
|
2020-11-19 15:58:41 +00:00
|
|
|
// Skip the tests when we use ABIEncoderV2.
|
2019-02-19 17:55:36 +00:00
|
|
|
// TODO: We should enable this again once the yul optimizer is activated.
|
2020-11-19 15:58:41 +00:00
|
|
|
if (solidity::test::CommonOptions::get().useABIEncoderV1)
|
2019-02-19 17:55:36 +00:00
|
|
|
{
|
|
|
|
BOOST_REQUIRE(!gas.isInfinite);
|
|
|
|
BOOST_CHECK_LE(m_gasUsed, gas.value);
|
|
|
|
BOOST_CHECK_LE(gas.value - _tolerance, m_gasUsed);
|
|
|
|
}
|
2015-05-19 22:27:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 08:48:54 +00:00
|
|
|
/// Compares the gas computed by PathGasMeter for the given signature (but unknown arguments)
|
|
|
|
/// against the actual gas usage computed by the VM on the given set of argument variants.
|
2018-12-12 13:58:20 +00:00
|
|
|
void testRunTimeGas(string const& _sig, vector<bytes> _argumentVariants, u256 const& _tolerance = u256(0))
|
2015-05-22 07:33:57 +00:00
|
|
|
{
|
|
|
|
u256 gasUsed = 0;
|
2016-06-21 16:13:07 +00:00
|
|
|
GasMeter::GasConsumption gas;
|
2019-12-23 15:50:30 +00:00
|
|
|
util::FixedHash<4> hash(util::keccak256(_sig));
|
2015-05-22 07:33:57 +00:00
|
|
|
for (bytes const& arguments: _argumentVariants)
|
|
|
|
{
|
|
|
|
sendMessage(hash.asBytes() + arguments, false, 0);
|
2018-06-15 10:18:00 +00:00
|
|
|
BOOST_CHECK(m_transactionSuccessful);
|
2015-05-22 07:33:57 +00:00
|
|
|
gasUsed = max(gasUsed, m_gasUsed);
|
2016-06-21 16:13:07 +00:00
|
|
|
gas = max(gas, gasForTransaction(hash.asBytes() + arguments, false));
|
2015-05-22 07:33:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 16:48:17 +00:00
|
|
|
gas += GasEstimator(solidity::test::CommonOptions::get().evmVersion()).functionalEstimation(
|
2017-08-21 20:49:58 +00:00
|
|
|
*m_compiler.runtimeAssemblyItems(m_compiler.lastContractName()),
|
2015-05-22 08:48:54 +00:00
|
|
|
_sig
|
|
|
|
);
|
2020-11-19 15:58:41 +00:00
|
|
|
// Skip the tests when we use ABIEncoderV2.
|
2019-02-19 17:55:36 +00:00
|
|
|
// TODO: We should enable this again once the yul optimizer is activated.
|
2020-11-19 15:58:41 +00:00
|
|
|
if (solidity::test::CommonOptions::get().useABIEncoderV1)
|
2019-02-19 17:55:36 +00:00
|
|
|
{
|
|
|
|
BOOST_REQUIRE(!gas.isInfinite);
|
|
|
|
BOOST_CHECK_LE(m_gasUsed, gas.value);
|
|
|
|
BOOST_CHECK_LE(gas.value - _tolerance, m_gasUsed);
|
|
|
|
}
|
2015-05-22 07:33:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 16:13:07 +00:00
|
|
|
static GasMeter::GasConsumption gasForTransaction(bytes const& _data, bool _isCreation)
|
|
|
|
{
|
2020-01-14 16:48:17 +00:00
|
|
|
auto evmVersion = solidity::test::CommonOptions::get().evmVersion();
|
2017-10-03 10:48:47 +00:00
|
|
|
GasMeter::GasConsumption gas = _isCreation ? GasCosts::txCreateGas : GasCosts::txGas;
|
2016-06-21 16:13:07 +00:00
|
|
|
for (auto i: _data)
|
2019-11-07 14:10:08 +00:00
|
|
|
gas += i != 0 ? GasCosts::txDataNonZeroGas(evmVersion) : GasCosts::txDataZeroGas;
|
2016-06-21 16:13:07 +00:00
|
|
|
return gas;
|
|
|
|
}
|
2015-05-06 08:43:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(GasMeterTests, GasMeterTestFramework)
|
|
|
|
|
2015-05-19 22:27:07 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(simple_contract)
|
|
|
|
{
|
|
|
|
// Tests a simple "deploy contract" code without constructor. The actual contract is not relevant.
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
bytes32 public shaValue;
|
2018-07-11 13:57:07 +00:00
|
|
|
function f(uint a) public {
|
2018-05-23 13:39:26 +00:00
|
|
|
shaValue = keccak256(abi.encodePacked(a));
|
2015-05-19 22:27:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
}
|
|
|
|
|
2017-05-11 12:43:29 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(store_keccak256)
|
2015-05-19 22:27:07 +00:00
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
bytes32 public shaValue;
|
2020-06-23 12:14:24 +00:00
|
|
|
constructor() {
|
2019-05-15 16:31:07 +00:00
|
|
|
shaValue = keccak256(abi.encodePacked(this));
|
2015-05-19 22:27:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(updating_store)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
uint data;
|
|
|
|
uint data2;
|
2020-06-23 12:14:24 +00:00
|
|
|
constructor() {
|
2015-05-19 22:27:07 +00:00
|
|
|
data = 1;
|
|
|
|
data = 2;
|
|
|
|
data2 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
2019-12-23 15:50:30 +00:00
|
|
|
testCreationTimeGas(sourceCode, m_evmVersion < langutil::EVMVersion::constantinople() ? u256(0) : u256(9600));
|
2015-05-19 22:27:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 07:33:57 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(branches)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
uint data;
|
|
|
|
uint data2;
|
2018-07-11 13:57:07 +00:00
|
|
|
function f(uint x) public {
|
2015-05-22 07:33:57 +00:00
|
|
|
if (x > 7)
|
|
|
|
data2 = 1;
|
|
|
|
else
|
|
|
|
data = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
testRunTimeGas("f(uint256)", vector<bytes>{encodeArgs(2), encodeArgs(8)});
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(function_calls)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
uint data;
|
|
|
|
uint data2;
|
2018-07-11 13:57:07 +00:00
|
|
|
function f(uint x) public {
|
2015-05-22 07:33:57 +00:00
|
|
|
if (x > 7)
|
2020-09-15 14:16:30 +00:00
|
|
|
{ unchecked { data2 = g(x**8) + 1; } }
|
2015-05-22 07:33:57 +00:00
|
|
|
else
|
|
|
|
data = 1;
|
|
|
|
}
|
|
|
|
function g(uint x) internal returns (uint) {
|
|
|
|
return data2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
testRunTimeGas("f(uint256)", vector<bytes>{encodeArgs(2), encodeArgs(8)});
|
|
|
|
}
|
|
|
|
|
2015-05-22 08:48:54 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(multiple_external_functions)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
uint data;
|
|
|
|
uint data2;
|
2018-07-11 13:57:07 +00:00
|
|
|
function f(uint x) public {
|
2015-05-22 08:48:54 +00:00
|
|
|
if (x > 7)
|
2020-09-15 14:16:30 +00:00
|
|
|
{ unchecked { data2 = g(x**8) + 1; } }
|
2015-05-22 08:48:54 +00:00
|
|
|
else
|
|
|
|
data = 1;
|
|
|
|
}
|
2018-07-11 13:57:07 +00:00
|
|
|
function g(uint x) public returns (uint) {
|
2015-05-22 08:48:54 +00:00
|
|
|
return data2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
testRunTimeGas("f(uint256)", vector<bytes>{encodeArgs(2), encodeArgs(8)});
|
|
|
|
testRunTimeGas("g(uint256)", vector<bytes>{encodeArgs(2)});
|
|
|
|
}
|
|
|
|
|
2017-01-05 13:08:55 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(exponent_size)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract A {
|
2020-01-16 14:34:45 +00:00
|
|
|
function f(uint x) public returns (uint) {
|
2020-09-15 14:16:30 +00:00
|
|
|
unchecked { return x ** 0; }
|
2020-01-16 14:34:45 +00:00
|
|
|
}
|
2018-07-11 13:57:07 +00:00
|
|
|
function g(uint x) public returns (uint) {
|
2020-09-15 14:16:30 +00:00
|
|
|
unchecked { return x ** 0x100; }
|
2017-01-05 13:08:55 +00:00
|
|
|
}
|
2018-07-11 13:57:07 +00:00
|
|
|
function h(uint x) public returns (uint) {
|
2020-09-15 14:16:30 +00:00
|
|
|
unchecked { return x ** 0x10000; }
|
2017-01-05 13:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
2020-01-16 14:34:45 +00:00
|
|
|
testRunTimeGas("f(uint256)", vector<bytes>{encodeArgs(2)});
|
2017-01-05 13:08:55 +00:00
|
|
|
testRunTimeGas("g(uint256)", vector<bytes>{encodeArgs(2)});
|
2017-02-24 19:09:38 +00:00
|
|
|
testRunTimeGas("h(uint256)", vector<bytes>{encodeArgs(2)});
|
2017-01-05 13:08:55 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 13:14:17 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(balance_gas)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract A {
|
2018-07-11 13:57:07 +00:00
|
|
|
function lookup_balance(address a) public returns (uint) {
|
2017-01-05 13:14:17 +00:00
|
|
|
return a.balance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
2017-02-24 19:09:38 +00:00
|
|
|
testRunTimeGas("lookup_balance(address)", vector<bytes>{encodeArgs(2), encodeArgs(100)});
|
2017-01-05 13:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 13:20:22 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(extcodesize_gas)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract A {
|
2018-07-11 13:57:07 +00:00
|
|
|
function f() public returns (uint _s) {
|
2017-01-05 13:20:22 +00:00
|
|
|
assembly {
|
|
|
|
_s := extcodesize(0x30)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
testRunTimeGas("f()", vector<bytes>{encodeArgs()});
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:30:20 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(regular_functions_exclude_fallback)
|
|
|
|
{
|
|
|
|
// A bug in the estimator caused the costs for a specific function
|
|
|
|
// to always include the costs for the fallback.
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract A {
|
|
|
|
uint public x;
|
2019-09-09 16:22:02 +00:00
|
|
|
fallback() external { x = 2; }
|
2018-04-11 16:30:20 +00:00
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
testRunTimeGas("x()", vector<bytes>{encodeArgs()});
|
|
|
|
}
|
2018-05-04 15:46:47 +00:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(complex_control_flow)
|
|
|
|
{
|
|
|
|
// This crashed the gas estimator previously (or took a very long time).
|
|
|
|
// Now we do not follow branches if they start out with lower gas costs than the ones
|
|
|
|
// we previously considered. This of course reduces accuracy.
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract log {
|
2018-07-11 13:57:07 +00:00
|
|
|
function ln(int128 x) public pure returns (int128 result) {
|
2020-09-15 14:16:30 +00:00
|
|
|
unchecked {
|
|
|
|
int128 t = x / 256;
|
|
|
|
int128 y = 5545177;
|
|
|
|
x = t;
|
|
|
|
t = x * 16; if (t <= 1000000) { x = t; y = y - 2772588; }
|
|
|
|
t = x * 4; if (t <= 1000000) { x = t; y = y - 1386294; }
|
|
|
|
t = x * 2; if (t <= 1000000) { x = t; y = y - 693147; }
|
|
|
|
t = x + x / 2; if (t <= 1000000) { x = t; y = y - 405465; }
|
|
|
|
t = x + x / 4; if (t <= 1000000) { x = t; y = y - 223144; }
|
|
|
|
t = x + x / 8; if (t <= 1000000) { x = t; y = y - 117783; }
|
|
|
|
t = x + x / 16; if (t <= 1000000) { x = t; y = y - 60624; }
|
|
|
|
t = x + x / 32; if (t <= 1000000) { x = t; y = y - 30771; }
|
|
|
|
t = x + x / 64; if (t <= 1000000) { x = t; y = y - 15504; }
|
|
|
|
t = x + x / 128; if (t <= 1000000) { x = t; y = y - 7782; }
|
|
|
|
t = x + x / 256; if (t <= 1000000) { x = t; y = y - 3898; }
|
|
|
|
t = x + x / 512; if (t <= 1000000) { x = t; y = y - 1951; }
|
|
|
|
t = x + x / 1024; if (t <= 1000000) { x = t; y = y - 976; }
|
|
|
|
t = x + x / 2048; if (t <= 1000000) { x = t; y = y - 488; }
|
|
|
|
t = x + x / 4096; if (t <= 1000000) { x = t; y = y - 244; }
|
|
|
|
t = x + x / 8192; if (t <= 1000000) { x = t; y = y - 122; }
|
|
|
|
t = x + x / 16384; if (t <= 1000000) { x = t; y = y - 61; }
|
|
|
|
t = x + x / 32768; if (t <= 1000000) { x = t; y = y - 31; }
|
|
|
|
t = x + x / 65536; if (t <= 1000000) { y = y - 15; }
|
|
|
|
return y;
|
|
|
|
}
|
2018-05-04 15:46:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
// max gas is used for small x
|
|
|
|
testRunTimeGas("ln(int128)", vector<bytes>{encodeArgs(0), encodeArgs(10), encodeArgs(105), encodeArgs(30000)});
|
|
|
|
}
|
|
|
|
|
2015-05-06 08:43:59 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|