forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGasCosts.cpp
217 lines (199 loc) · 6.07 KB
/
GasCosts.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
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
/**
* Tests that check that the cost of certain operations stay within range.
*/
#include <test/libsolidity/SolidityExecutionFramework.h>
#include <liblangutil/EVMVersion.h>
#include <libsolutil/IpfsHash.h>
#include <libevmasm/GasMeter.h>
#include <cmath>
using namespace solidity::langutil;
using namespace solidity::langutil;
using namespace solidity::evmasm;
using namespace solidity::frontend;
using namespace solidity::test;
namespace solidity::frontend::test
{
#define CHECK_DEPLOY_GAS(_gasNoOpt, _gasOpt, _evmVersion) \
do \
{ \
u256 metaCost = GasMeter::dataGas(m_compiler.cborMetadata(m_compiler.lastContractName()), true, _evmVersion); \
u256 gasOpt{_gasOpt}; \
u256 gasNoOpt{_gasNoOpt}; \
u256 gas = m_optimiserSettings == OptimiserSettings::minimal() ? gasNoOpt : gasOpt; \
BOOST_CHECK_MESSAGE( \
m_gasUsed >= metaCost, \
"Gas used: " + \
m_gasUsed.str() + \
" is less than the data cost for the cbor metadata: " + \
u256(metaCost).str() \
); \
u256 gasUsed = m_gasUsed - metaCost; \
BOOST_CHECK_MESSAGE( \
gas == gasUsed, \
"Gas used: " + \
gasUsed.str() + \
" - expected: " + \
gas.str() \
); \
} while(0)
#define CHECK_GAS(_gasNoOpt, _gasOpt, _tolerance) \
do \
{ \
u256 gasOpt{_gasOpt}; \
u256 gasNoOpt{_gasNoOpt}; \
u256 tolerance{_tolerance}; \
u256 gas = m_optimiserSettings == OptimiserSettings::minimal() ? gasNoOpt : gasOpt; \
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");
}
}
)";
m_compiler.setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata);
m_appendCBORMetadata = false;
compileAndRun(sourceCode);
auto evmVersion = solidity::test::CommonOptions::get().evmVersion();
if (evmVersion <= EVMVersion::byzantium())
{
if (CommonOptions::get().useABIEncoderV1)
CHECK_DEPLOY_GAS(133045, 129731, evmVersion);
else
CHECK_DEPLOY_GAS(144995, 121229, evmVersion);
}
// This is only correct on >=Constantinople.
else if (!CommonOptions::get().useABIEncoderV1)
{
if (CommonOptions::get().optimize)
{
// Costs with 0 are cases which cannot be triggered in tests.
if (evmVersion < EVMVersion::istanbul())
CHECK_DEPLOY_GAS(0, 109237, evmVersion);
else if (evmVersion < EVMVersion::shanghai())
CHECK_DEPLOY_GAS(0, 97693, evmVersion);
// Shanghai is cheaper due to `push0`
else
CHECK_DEPLOY_GAS(0, 97067, evmVersion);
}
else
{
if (evmVersion < EVMVersion::istanbul())
CHECK_DEPLOY_GAS(139009, 123969, evmVersion);
else if (evmVersion < EVMVersion::shanghai())
CHECK_DEPLOY_GAS(123357, 110969, evmVersion);
// Shanghai is cheaper due to `push0`
else
CHECK_DEPLOY_GAS(121489, 110969, evmVersion);
}
}
else if (evmVersion < EVMVersion::istanbul())
CHECK_DEPLOY_GAS(125829, 118559, evmVersion);
else if (evmVersion < EVMVersion::shanghai())
CHECK_DEPLOY_GAS(114077, 96461, evmVersion);
else
CHECK_DEPLOY_GAS(114077, 95831, evmVersion);
if (evmVersion >= EVMVersion::byzantium())
{
callContractFunction("f()");
if (evmVersion == EVMVersion::byzantium())
CHECK_GAS(21741, 21522, 20);
// This is only correct on >=Constantinople.
else if (!CommonOptions::get().useABIEncoderV1)
{
if (CommonOptions::get().optimize)
{
if (evmVersion < EVMVersion::istanbul())
CHECK_GAS(0, 21526, 20);
else
CHECK_GAS(0, 21318, 20);
}
else
{
if (evmVersion < EVMVersion::istanbul())
CHECK_GAS(21736, 21559, 20);
else
CHECK_GAS(21528, 21351, 20);
}
}
else if (evmVersion < EVMVersion::istanbul())
CHECK_GAS(21546, 21526, 20);
else
CHECK_GAS(21332, 21322, 20);
}
}
BOOST_AUTO_TEST_CASE(single_callvaluecheck)
{
std::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) {
return uint160(b) + 2;
}
function f2(address b) public pure returns (uint) {
return uint160(b) + 8;
}
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) {
return uint160(b) + 2;
}
function f2(address b) public pure returns (uint) {
return uint160(b) + 8;
}
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();
auto evmVersion = solidity::test::CommonOptions::get().evmVersion();
if (evmVersion < EVMVersion::shanghai())
BOOST_CHECK_EQUAL(bytecodeSizePayable - bytecodeSizeNonpayable, 26);
else
BOOST_CHECK_EQUAL(bytecodeSizePayable - bytecodeSizeNonpayable, 24);
}
BOOST_AUTO_TEST_SUITE_END()
}