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
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @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>
|
|
|
|
#include <libsolidity/interface/SourceReferenceFormatter.h>
|
2015-05-06 08:43:59 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev::eth;
|
|
|
|
using namespace dev::solidity;
|
2016-11-28 00:58:07 +00:00
|
|
|
using namespace dev::test;
|
2015-05-06 08:43:59 +00:00
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
2016-11-28 00:21:01 +00:00
|
|
|
class GasMeterTestFramework: public SolidityExecutionFramework
|
2015-05-06 08:43:59 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
GasMeterTestFramework() { }
|
|
|
|
void compile(string const& _sourceCode)
|
|
|
|
{
|
2017-07-18 13:34:22 +00:00
|
|
|
m_compiler.reset(false);
|
|
|
|
m_compiler.addSource("", "pragma solidity >=0.0;\n" + _sourceCode);
|
2017-07-17 10:54:02 +00:00
|
|
|
m_compiler.setOptimiserSettings(dev::test::Options::get().optimize);
|
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
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
AssemblyItems const* items = m_compiler.runtimeAssemblyItems("");
|
2015-09-08 12:30:21 +00:00
|
|
|
ASTNode const& sourceUnit = m_compiler.ast();
|
2015-05-06 08:43:59 +00:00
|
|
|
BOOST_REQUIRE(items != nullptr);
|
2015-05-22 08:48:54 +00:00
|
|
|
m_gasCosts = GasEstimator::breakToStatementLevel(
|
|
|
|
GasEstimator::structuralEstimation(*items, vector<ASTNode const*>({&sourceUnit})),
|
2015-05-06 08:43:59 +00:00
|
|
|
{&sourceUnit}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-05-22 07:33:57 +00:00
|
|
|
void testCreationTimeGas(string const& _sourceCode)
|
2015-05-19 22:27:07 +00:00
|
|
|
{
|
|
|
|
compileAndRun(_sourceCode);
|
|
|
|
auto state = make_shared<KnownState>();
|
2015-08-31 16:44:29 +00:00
|
|
|
PathGasMeter meter(*m_compiler.assemblyItems());
|
2015-05-22 07:33:57 +00:00
|
|
|
GasMeter::GasConsumption gas = meter.estimateMax(0, state);
|
2015-09-10 10:01:05 +00:00
|
|
|
u256 bytecodeSize(m_compiler.runtimeObject().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
|
|
|
|
gas += gasForTransaction(m_compiler.object().bytecode, true);
|
|
|
|
|
2015-05-19 22:27:07 +00:00
|
|
|
BOOST_REQUIRE(!gas.isInfinite);
|
|
|
|
BOOST_CHECK(gas.value == m_gasUsed);
|
|
|
|
}
|
|
|
|
|
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.
|
2015-05-26 09:29:41 +00:00
|
|
|
void testRunTimeGas(string const& _sig, vector<bytes> _argumentVariants)
|
2015-05-22 07:33:57 +00:00
|
|
|
{
|
|
|
|
u256 gasUsed = 0;
|
2016-06-21 16:13:07 +00:00
|
|
|
GasMeter::GasConsumption gas;
|
2016-10-05 10:30:28 +00:00
|
|
|
FixedHash<4> hash(dev::keccak256(_sig));
|
2015-05-22 07:33:57 +00:00
|
|
|
for (bytes const& arguments: _argumentVariants)
|
|
|
|
{
|
|
|
|
sendMessage(hash.asBytes() + arguments, false, 0);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-06-21 16:13:07 +00:00
|
|
|
gas += GasEstimator::functionalEstimation(
|
2015-08-31 16:44:29 +00:00
|
|
|
*m_compiler.runtimeAssemblyItems(),
|
2015-05-22 08:48:54 +00:00
|
|
|
_sig
|
|
|
|
);
|
2015-05-22 07:33:57 +00:00
|
|
|
BOOST_REQUIRE(!gas.isInfinite);
|
|
|
|
BOOST_CHECK(gas.value == m_gasUsed);
|
|
|
|
}
|
|
|
|
|
2016-06-21 16:13:07 +00:00
|
|
|
static GasMeter::GasConsumption gasForTransaction(bytes const& _data, bool _isCreation)
|
|
|
|
{
|
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)
|
2017-10-03 10:48:47 +00:00
|
|
|
gas += i != 0 ? GasCosts::txDataNonZeroGas : GasCosts::txDataZeroGas;
|
2016-06-21 16:13:07 +00:00
|
|
|
return gas;
|
|
|
|
}
|
|
|
|
|
2015-05-06 08:43:59 +00:00
|
|
|
protected:
|
|
|
|
map<ASTNode const*, eth::GasMeter::GasConsumption> m_gasCosts;
|
|
|
|
};
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(GasMeterTests, GasMeterTestFramework)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(non_overlapping_filtered_costs)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
bytes x;
|
|
|
|
function f(uint a) returns (uint b) {
|
|
|
|
x.length = a;
|
|
|
|
for (; a < 200; ++a) {
|
|
|
|
x[a] = 9;
|
|
|
|
b = a * a;
|
|
|
|
}
|
|
|
|
return f(a - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
compile(sourceCode);
|
|
|
|
for (auto first = m_gasCosts.cbegin(); first != m_gasCosts.cend(); ++first)
|
|
|
|
{
|
|
|
|
auto second = first;
|
|
|
|
for (++second; second != m_gasCosts.cend(); ++second)
|
2015-08-31 16:44:29 +00:00
|
|
|
if (first->first->location().intersects(second->first->location()))
|
2015-05-06 08:43:59 +00:00
|
|
|
{
|
|
|
|
BOOST_CHECK_MESSAGE(false, "Source locations should not overlap!");
|
2016-03-31 22:54:07 +00:00
|
|
|
auto scannerFromSource = [&](string const&) -> Scanner const& { return m_compiler.scanner(); };
|
|
|
|
SourceReferenceFormatter::printSourceLocation(cout, &first->first->location(), scannerFromSource);
|
|
|
|
SourceReferenceFormatter::printSourceLocation(cout, &second->first->location(), scannerFromSource);
|
2015-05-06 08:43:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
function f(uint a) {
|
2017-05-11 12:43:29 +00:00
|
|
|
shaValue = keccak256(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;
|
|
|
|
function test(uint a) {
|
2017-05-11 12:43:29 +00:00
|
|
|
shaValue = keccak256(a);
|
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;
|
|
|
|
function test() {
|
|
|
|
data = 1;
|
|
|
|
data = 2;
|
|
|
|
data2 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
}
|
|
|
|
|
2015-05-22 07:33:57 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(branches)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
uint data;
|
|
|
|
uint data2;
|
|
|
|
function f(uint x) {
|
|
|
|
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;
|
|
|
|
function f(uint x) {
|
|
|
|
if (x > 7)
|
|
|
|
data2 = g(x**8) + 1;
|
|
|
|
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;
|
|
|
|
function f(uint x) {
|
|
|
|
if (x > 7)
|
|
|
|
data2 = g(x**8) + 1;
|
|
|
|
else
|
|
|
|
data = 1;
|
|
|
|
}
|
|
|
|
function g(uint x) returns (uint) {
|
|
|
|
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 {
|
|
|
|
function g(uint x) returns (uint) {
|
|
|
|
return x ** 0x100;
|
|
|
|
}
|
|
|
|
function h(uint x) returns (uint) {
|
|
|
|
return x ** 0x10000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
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 {
|
|
|
|
function lookup_balance(address a) returns (uint) {
|
|
|
|
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 {
|
|
|
|
function f() returns (uint _s) {
|
|
|
|
assembly {
|
|
|
|
_s := extcodesize(0x30)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
testCreationTimeGas(sourceCode);
|
|
|
|
testRunTimeGas("f()", vector<bytes>{encodeArgs()});
|
|
|
|
}
|
|
|
|
|
2015-05-06 08:43:59 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|