2019-05-06 16:55:10 +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
|
2019-05-06 16:55:10 +00:00
|
|
|
|
|
|
|
#include <test/libsolidity/GasTest.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonIO.h>
|
|
|
|
#include <libsolutil/JSON.h>
|
2019-05-06 16:55:10 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatterHuman.h>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2020-01-14 14:55:31 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
2019-05-06 16:55:10 +00:00
|
|
|
#include <boost/throw_exception.hpp>
|
|
|
|
#include <fstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::frontend::test;
|
|
|
|
using namespace solidity;
|
2019-05-06 16:55:10 +00:00
|
|
|
using namespace std;
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
using namespace boost::unit_test;
|
|
|
|
|
2020-03-06 00:22:51 +00:00
|
|
|
GasTest::GasTest(string const& _filename):
|
|
|
|
TestCase(_filename)
|
2019-05-06 16:55:10 +00:00
|
|
|
{
|
2020-03-06 00:22:51 +00:00
|
|
|
m_source = m_reader.source();
|
|
|
|
m_optimise = m_reader.boolSetting("optimize", false);
|
|
|
|
m_optimiseYul = m_reader.boolSetting("optimize-yul", false);
|
|
|
|
m_optimiseRuns = m_reader.sizetSetting("optimize-runs", 200);
|
|
|
|
parseExpectations(m_reader.stream());
|
2019-05-06 16:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GasTest::parseExpectations(std::istream& _stream)
|
|
|
|
{
|
2019-05-06 12:31:49 +00:00
|
|
|
map<std::string, std::string>* currentKind = nullptr;
|
|
|
|
string line;
|
2019-05-06 16:55:10 +00:00
|
|
|
|
|
|
|
while (getline(_stream, line))
|
2019-05-06 12:31:49 +00:00
|
|
|
if (!boost::starts_with(line, "// "))
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Invalid expectation: expected \"// \"."));
|
|
|
|
else if (boost::ends_with(line, ":"))
|
2019-05-06 16:55:10 +00:00
|
|
|
{
|
2019-05-06 12:31:49 +00:00
|
|
|
string kind = line.substr(3, line.length() - 4);
|
|
|
|
boost::trim(kind);
|
|
|
|
currentKind = &m_expectations[move(kind)];
|
2019-05-06 16:55:10 +00:00
|
|
|
}
|
|
|
|
else if (!currentKind)
|
2019-05-06 12:31:49 +00:00
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("No function kind specified. Expected \"creation:\", \"external:\" or \"internal:\"."));
|
2019-05-06 16:55:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto it = line.begin() + 3;
|
|
|
|
skipWhitespace(it, line.end());
|
|
|
|
auto functionNameBegin = it;
|
|
|
|
while (it != line.end() && *it != ':')
|
|
|
|
++it;
|
2019-05-06 12:31:49 +00:00
|
|
|
string functionName(functionNameBegin, it);
|
2019-05-06 16:55:10 +00:00
|
|
|
if (functionName == "fallback")
|
|
|
|
functionName.clear();
|
|
|
|
expect(it, line.end(), ':');
|
|
|
|
skipWhitespace(it, line.end());
|
|
|
|
if (it == line.end())
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Invalid expectation: expected gas cost."));
|
|
|
|
(*currentKind)[functionName] = std::string(it, line.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 12:31:49 +00:00
|
|
|
void GasTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
|
2019-05-06 16:55:10 +00:00
|
|
|
{
|
|
|
|
Json::Value estimates = compiler().gasEstimates(compiler().lastContractName());
|
2019-05-06 12:31:49 +00:00
|
|
|
for (auto groupIt = estimates.begin(); groupIt != estimates.end(); ++groupIt)
|
|
|
|
{
|
|
|
|
_stream << _linePrefix << groupIt.key().asString() << ":" << std::endl;
|
|
|
|
for (auto it = groupIt->begin(); it != groupIt->end(); ++it)
|
2019-05-06 16:55:10 +00:00
|
|
|
{
|
2019-05-06 12:31:49 +00:00
|
|
|
_stream << _linePrefix << " ";
|
|
|
|
if (it.key().asString().empty())
|
|
|
|
_stream << "fallback";
|
|
|
|
else
|
|
|
|
_stream << it.key().asString();
|
|
|
|
_stream << ": " << it->asString() << std::endl;
|
2019-05-06 16:55:10 +00:00
|
|
|
}
|
2019-05-06 12:31:49 +00:00
|
|
|
}
|
2019-05-06 16:55:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 13:33:22 +00:00
|
|
|
TestCase::TestResult GasTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
|
2019-05-06 16:55:10 +00:00
|
|
|
{
|
2020-05-12 09:56:28 +00:00
|
|
|
string const preamble = "pragma solidity >=0.0;\n// SPDX-License-Identifier: GPL-3.0\n";
|
2019-05-06 16:55:10 +00:00
|
|
|
compiler().reset();
|
2019-05-06 12:31:49 +00:00
|
|
|
// Prerelease CBOR metadata varies in size due to changing version numbers and build dates.
|
|
|
|
// This leads to volatile creation cost estimates. Therefore we force the compiler to
|
|
|
|
// release mode for testing gas estimates.
|
|
|
|
compiler().overwriteReleaseFlag(true);
|
2019-05-06 16:55:10 +00:00
|
|
|
OptimiserSettings settings = m_optimise ? OptimiserSettings::standard() : OptimiserSettings::minimal();
|
|
|
|
if (m_optimiseYul)
|
|
|
|
{
|
|
|
|
settings.runYulOptimiser = m_optimise;
|
|
|
|
settings.optimizeStackAllocation = m_optimise;
|
|
|
|
}
|
|
|
|
settings.expectedExecutionsPerDeployment = m_optimiseRuns;
|
|
|
|
compiler().setOptimiserSettings(settings);
|
2020-05-12 09:56:28 +00:00
|
|
|
compiler().setSources({{"", preamble + m_source}});
|
2019-05-06 16:55:10 +00:00
|
|
|
|
|
|
|
if (!compiler().parseAndAnalyze() || !compiler().compile())
|
|
|
|
{
|
2020-06-04 01:19:47 +00:00
|
|
|
SourceReferenceFormatterHuman formatter(_stream, _formatted, false);
|
2019-05-06 16:55:10 +00:00
|
|
|
for (auto const& error: compiler().errors())
|
|
|
|
formatter.printErrorInformation(*error);
|
2019-05-07 13:33:22 +00:00
|
|
|
return TestResult::FatalError;
|
2019-05-06 16:55:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 12:31:49 +00:00
|
|
|
Json::Value estimateGroups = compiler().gasEstimates(compiler().lastContractName());
|
|
|
|
if (
|
|
|
|
m_expectations.size() == estimateGroups.size() &&
|
|
|
|
boost::all(m_expectations, [&](auto const& expectations) {
|
|
|
|
auto const& estimates = estimateGroups[expectations.first];
|
|
|
|
return estimates.size() == expectations.second.size() &&
|
|
|
|
boost::all(expectations.second, [&](auto const& entry) {
|
|
|
|
return entry.second == estimates[entry.first].asString();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
)
|
|
|
|
return TestResult::Success;
|
|
|
|
else
|
2019-05-06 16:55:10 +00:00
|
|
|
{
|
|
|
|
_stream << _linePrefix << "Expected:" << std::endl;
|
2019-05-06 12:31:49 +00:00
|
|
|
for (auto const& expectations: m_expectations)
|
2019-05-06 16:55:10 +00:00
|
|
|
{
|
2019-05-06 12:31:49 +00:00
|
|
|
_stream << _linePrefix << " " << expectations.first << ":" << std::endl;
|
|
|
|
for (auto const& entry: expectations.second)
|
2019-05-06 16:55:10 +00:00
|
|
|
_stream << _linePrefix
|
|
|
|
<< " "
|
|
|
|
<< (entry.first.empty() ? "fallback" : entry.first)
|
|
|
|
<< ": "
|
|
|
|
<< entry.second
|
|
|
|
<< std::endl;
|
2019-05-06 12:31:49 +00:00
|
|
|
}
|
2019-05-06 16:55:10 +00:00
|
|
|
_stream << _linePrefix << "Obtained:" << std::endl;
|
|
|
|
printUpdatedExpectations(_stream, _linePrefix + " ");
|
2019-05-06 12:31:49 +00:00
|
|
|
return TestResult::Failure;
|
2019-05-06 16:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GasTest::printSource(ostream& _stream, string const& _linePrefix, bool) const
|
|
|
|
{
|
2019-05-06 12:31:49 +00:00
|
|
|
string line;
|
|
|
|
istringstream input(m_source);
|
2019-05-06 16:55:10 +00:00
|
|
|
while (getline(input, line))
|
|
|
|
_stream << _linePrefix << line << std::endl;
|
|
|
|
}
|