Repository navigation
Expand file tree
/
Copy pathGasTest.cpp
More file actions
164 lines (151 loc) · 5.59 KB
/
Copy pathGasTest.cpp
File metadata and controls
164 lines (151 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <test/libsolidity/GasTest.h>
#include <test/Common.h>
#include <libsolutil/CommonIO.h>
#include <libsolutil/JSON.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/throw_exception.hpp>
#include <fstream>
#include <stdexcept>
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace solidity;
using namespace std;
using namespace boost::unit_test;
GasTest::GasTest(string const& _filename):
TestCase(_filename)
{
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", OptimiserSettings{}.expectedExecutionsPerDeployment);
parseExpectations(m_reader.stream());
}
void GasTest::parseExpectations(std::istream& _stream)
{
map<std::string, std::string>* currentKind = nullptr;
string line;
while (getline(_stream, line))
if (!boost::starts_with(line, "// "))
BOOST_THROW_EXCEPTION(runtime_error("Invalid expectation: expected \"// \"."));
else if (boost::ends_with(line, ":"))
{
string kind = line.substr(3, line.length() - 4);
boost::trim(kind);
currentKind = &m_expectations[move(kind)];
}
else if (!currentKind)
BOOST_THROW_EXCEPTION(runtime_error("No function kind specified. Expected \"creation:\", \"external:\" or \"internal:\"."));
else
{
auto it = line.begin() + 3;
skipWhitespace(it, line.end());
auto functionNameBegin = it;
while (it != line.end() && *it != ':')
++it;
string functionName(functionNameBegin, it);
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());
}
}
void GasTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
Json::Value estimates = compiler().gasEstimates(compiler().lastContractName());
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)
{
_stream << _linePrefix << " ";
if (it.key().asString().empty())
_stream << "fallback";
else
_stream << it.key().asString();
_stream << ": " << it->asString() << std::endl;
}
}
}
TestCase::TestResult GasTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
{
string const preamble = "pragma solidity >=0.0;\n// SPDX-License-Identifier: GPL-3.0\n";
compiler().reset();
// 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().setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata);
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);
compiler().setSources({{"", preamble + m_source}});
if (!compiler().parseAndAnalyze() || !compiler().compile())
{
SourceReferenceFormatter{_stream, compiler(), _formatted, false}
.printErrorInformation(compiler().errors());
return TestResult::FatalError;
}
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
{
_stream << _linePrefix << "Expected:" << std::endl;
for (auto const& expectations: m_expectations)
{
_stream << _linePrefix << " " << expectations.first << ":" << std::endl;
for (auto const& entry: expectations.second)
_stream << _linePrefix
<< " "
<< (entry.first.empty() ? "fallback" : entry.first)
<< ": "
<< entry.second
<< std::endl;
}
_stream << _linePrefix << "Obtained:" << std::endl;
printUpdatedExpectations(_stream, _linePrefix + " ");
return TestResult::Failure;
}
}
void GasTest::printSource(ostream& _stream, string const& _linePrefix, bool) const
{
string line;
istringstream input(m_source);
while (getline(input, line))
_stream << _linePrefix << line << std::endl;
}