|
| 1 | +/* |
| 2 | + This file is part of solidity. |
| 3 | +
|
| 4 | + solidity is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + solidity is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with solidity. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | +/** |
| 18 | + * Tests that check that the cost of certain operations stay within range. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <test/libsolidity/SolidityExecutionFramework.h> |
| 22 | + |
| 23 | +#include <cmath> |
| 24 | + |
| 25 | +using namespace std; |
| 26 | +using namespace langutil; |
| 27 | +using namespace dev::eth; |
| 28 | +using namespace dev::solidity; |
| 29 | +using namespace dev::test; |
| 30 | + |
| 31 | +namespace dev |
| 32 | +{ |
| 33 | +namespace solidity |
| 34 | +{ |
| 35 | +namespace test |
| 36 | +{ |
| 37 | + |
| 38 | +#define CHECK_GAS(_gasNoOpt, _gasOpt, _tolerance) \ |
| 39 | + do \ |
| 40 | + { \ |
| 41 | + u256 gasOpt{_gasOpt}; \ |
| 42 | + u256 gasNoOpt{_gasNoOpt}; \ |
| 43 | + u256 tolerance{_tolerance}; \ |
| 44 | + u256 gas = m_optimize ? gasOpt : gasNoOpt; \ |
| 45 | + u256 diff = gas < m_gasUsed ? m_gasUsed - gas : gas - m_gasUsed; \ |
| 46 | + BOOST_CHECK_MESSAGE( \ |
| 47 | + diff <= tolerance, \ |
| 48 | + "Gas used: " + \ |
| 49 | + m_gasUsed.str() + \ |
| 50 | + " - expected: " + \ |
| 51 | + gas.str() + \ |
| 52 | + " (tolerance: " + \ |
| 53 | + tolerance.str() + \ |
| 54 | + ")" \ |
| 55 | + ); \ |
| 56 | + } while(0) |
| 57 | + |
| 58 | +BOOST_FIXTURE_TEST_SUITE(GasCostTests, SolidityExecutionFramework) |
| 59 | + |
| 60 | +BOOST_AUTO_TEST_CASE(string_storage) |
| 61 | +{ |
| 62 | + char const* sourceCode = R"( |
| 63 | + contract C { |
| 64 | + function f() pure public { |
| 65 | + require(false, "Not Authorized. This function can only be called by the custodian or owner of this contract"); |
| 66 | + } |
| 67 | + } |
| 68 | + )"; |
| 69 | + compileAndRun(sourceCode); |
| 70 | + |
| 71 | + if (Options::get().evmVersion() <= EVMVersion::byzantium()) |
| 72 | + CHECK_GAS(134435, 130591, 100); |
| 73 | + else |
| 74 | + CHECK_GAS(127225, 124873, 100); |
| 75 | + if (Options::get().evmVersion() >= EVMVersion::byzantium()) |
| 76 | + { |
| 77 | + callContractFunction("f()"); |
| 78 | + if (Options::get().evmVersion() == EVMVersion::byzantium()) |
| 79 | + CHECK_GAS(21551, 21526, 20); |
| 80 | + else |
| 81 | + CHECK_GAS(21546, 21526, 20); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +BOOST_AUTO_TEST_SUITE_END() |
| 86 | + |
| 87 | +} |
| 88 | +} |
| 89 | +} |
0 commit comments