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
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-05-06 08:43:59 +00:00
|
|
|
/** @file GasMeter.cpp
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
2021-04-13 07:15:32 +00:00
|
|
|
*
|
|
|
|
* Utilities for tracking gas costs.
|
|
|
|
*
|
|
|
|
* With respect to EIP-2929, we do not track warm accounts or storage slots and they are always
|
|
|
|
* charged the worst-case, i.e., cold-access.
|
2015-05-06 08:43:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-05-19 22:27:07 +00:00
|
|
|
#include <libevmasm/ExpressionClasses.h>
|
2015-05-06 08:43:59 +00:00
|
|
|
#include <libevmasm/AssemblyItem.h>
|
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/EVMVersion.h>
|
2018-03-01 11:06:36 +00:00
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
#include <tuple>
|
2020-04-01 03:04:29 +00:00
|
|
|
#include <utility>
|
2018-03-01 11:06:36 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::evmasm
|
2015-05-06 08:43:59 +00:00
|
|
|
{
|
|
|
|
|
2015-05-19 22:27:07 +00:00
|
|
|
class KnownState;
|
|
|
|
|
2016-04-06 18:55:46 +00:00
|
|
|
namespace GasCosts
|
|
|
|
{
|
|
|
|
static unsigned const stackLimit = 1024;
|
|
|
|
static unsigned const tier0Gas = 0;
|
|
|
|
static unsigned const tier1Gas = 2;
|
|
|
|
static unsigned const tier2Gas = 3;
|
|
|
|
static unsigned const tier3Gas = 5;
|
|
|
|
static unsigned const tier4Gas = 8;
|
|
|
|
static unsigned const tier5Gas = 10;
|
|
|
|
static unsigned const tier6Gas = 20;
|
|
|
|
static unsigned const tier7Gas = 0;
|
|
|
|
static unsigned const expGas = 10;
|
2019-02-25 14:29:57 +00:00
|
|
|
inline unsigned expByteGas(langutil::EVMVersion _evmVersion)
|
2018-03-01 11:06:36 +00:00
|
|
|
{
|
2019-02-25 14:29:57 +00:00
|
|
|
return _evmVersion >= langutil::EVMVersion::spuriousDragon() ? 50 : 10;
|
2018-03-01 11:06:36 +00:00
|
|
|
}
|
2017-05-10 07:48:00 +00:00
|
|
|
static unsigned const keccak256Gas = 30;
|
|
|
|
static unsigned const keccak256WordGas = 6;
|
2021-08-09 08:43:44 +00:00
|
|
|
/// Corresponds to ACCESS_LIST_ADDRESS_COST from EIP-2930
|
|
|
|
static unsigned const accessListAddressCost = 2400;
|
|
|
|
/// Corresponds to ACCESS_LIST_STORAGE_COST from EIP-2930
|
|
|
|
static unsigned const accessListStorageKeyCost = 1900;
|
2021-04-13 07:15:32 +00:00
|
|
|
/// Corresponds to COLD_SLOAD_COST from EIP-2929
|
|
|
|
static unsigned const coldSloadCost = 2100;
|
|
|
|
/// Corresponds to COLD_ACCOUNT_ACCESS_COST from EIP-2929
|
|
|
|
static unsigned const coldAccountAccessCost = 2600;
|
|
|
|
/// Corresponds to WARM_STORAGE_READ_COST from EIP-2929
|
|
|
|
static unsigned const warmStorageReadCost = 100;
|
2019-02-25 14:29:57 +00:00
|
|
|
inline unsigned sloadGas(langutil::EVMVersion _evmVersion)
|
2018-03-01 11:06:36 +00:00
|
|
|
{
|
2021-04-13 07:15:32 +00:00
|
|
|
if (_evmVersion >= langutil::EVMVersion::berlin())
|
|
|
|
return coldSloadCost;
|
|
|
|
else if (_evmVersion >= langutil::EVMVersion::istanbul())
|
2019-11-07 12:56:28 +00:00
|
|
|
return 800;
|
|
|
|
else if (_evmVersion >= langutil::EVMVersion::tangerineWhistle())
|
|
|
|
return 200;
|
|
|
|
else
|
|
|
|
return 50;
|
2018-03-01 11:06:36 +00:00
|
|
|
}
|
2021-04-13 07:15:32 +00:00
|
|
|
/// Corresponds to SSTORE_SET_GAS
|
2016-04-06 18:55:46 +00:00
|
|
|
static unsigned const sstoreSetGas = 20000;
|
2021-04-13 07:15:32 +00:00
|
|
|
/// Corresponds to SSTORE_RESET_GAS from EIP-2929
|
|
|
|
static unsigned const sstoreResetGas = 5000 - coldSloadCost;
|
2021-08-09 08:43:44 +00:00
|
|
|
/// Corresponds to SSTORE_CLEARS_SCHEDULE from EIP-2200
|
|
|
|
inline static unsigned sstoreClearsSchedule(langutil::EVMVersion _evmVersion)
|
|
|
|
{
|
|
|
|
// Changes from EIP-3529
|
|
|
|
if (_evmVersion >= langutil::EVMVersion::london())
|
|
|
|
return sstoreResetGas + accessListStorageKeyCost;
|
|
|
|
else
|
|
|
|
return 15000;
|
|
|
|
}
|
2021-04-13 07:15:32 +00:00
|
|
|
inline static unsigned totalSstoreSetGas(langutil::EVMVersion _evmVersion)
|
|
|
|
{
|
|
|
|
if (_evmVersion >= langutil::EVMVersion::berlin())
|
|
|
|
return sstoreSetGas + coldSloadCost;
|
|
|
|
else
|
|
|
|
return sstoreSetGas;
|
|
|
|
}
|
|
|
|
/// Corresponds to SSTORE_RESET_GAS from EIP-2929
|
|
|
|
/// For Berlin, the maximum is SSTORE_RESET_GAS + COLD_SLOAD_COST = 5000
|
|
|
|
/// For previous versions, it's a fixed 5000
|
|
|
|
inline unsigned totalSstoreResetGas(langutil::EVMVersion _evmVersion)
|
|
|
|
{
|
|
|
|
if (_evmVersion >= langutil::EVMVersion::berlin())
|
|
|
|
return sstoreResetGas + coldSloadCost;
|
|
|
|
else
|
|
|
|
return 5000;
|
|
|
|
}
|
|
|
|
inline unsigned extCodeGas(langutil::EVMVersion _evmVersion)
|
|
|
|
{
|
|
|
|
if (_evmVersion >= langutil::EVMVersion::berlin())
|
|
|
|
return coldAccountAccessCost;
|
|
|
|
else if (_evmVersion >= langutil::EVMVersion::tangerineWhistle())
|
|
|
|
return 700;
|
|
|
|
else
|
|
|
|
return 20;
|
|
|
|
}
|
|
|
|
inline unsigned balanceGas(langutil::EVMVersion _evmVersion)
|
|
|
|
{
|
|
|
|
if (_evmVersion >= langutil::EVMVersion::berlin())
|
|
|
|
return coldAccountAccessCost;
|
|
|
|
else if (_evmVersion >= langutil::EVMVersion::istanbul())
|
|
|
|
return 700;
|
|
|
|
else if (_evmVersion >= langutil::EVMVersion::tangerineWhistle())
|
|
|
|
return 400;
|
|
|
|
else
|
|
|
|
return 20;
|
|
|
|
}
|
2016-04-06 18:55:46 +00:00
|
|
|
static unsigned const jumpdestGas = 1;
|
|
|
|
static unsigned const logGas = 375;
|
|
|
|
static unsigned const logDataGas = 8;
|
|
|
|
static unsigned const logTopicGas = 375;
|
|
|
|
static unsigned const createGas = 32000;
|
2019-02-25 14:29:57 +00:00
|
|
|
inline unsigned callGas(langutil::EVMVersion _evmVersion)
|
2018-03-01 11:06:36 +00:00
|
|
|
{
|
2021-04-13 07:15:32 +00:00
|
|
|
if (_evmVersion >= langutil::EVMVersion::berlin())
|
|
|
|
return coldAccountAccessCost;
|
|
|
|
else if (_evmVersion >= langutil::EVMVersion::tangerineWhistle())
|
|
|
|
return 700;
|
|
|
|
else
|
|
|
|
return 40;
|
2018-03-01 11:06:36 +00:00
|
|
|
}
|
2016-04-06 18:55:46 +00:00
|
|
|
static unsigned const callStipend = 2300;
|
|
|
|
static unsigned const callValueTransferGas = 9000;
|
|
|
|
static unsigned const callNewAccountGas = 25000;
|
2019-02-25 14:29:57 +00:00
|
|
|
inline unsigned selfdestructGas(langutil::EVMVersion _evmVersion)
|
2018-03-01 11:06:36 +00:00
|
|
|
{
|
2021-04-13 07:15:32 +00:00
|
|
|
if (_evmVersion >= langutil::EVMVersion::berlin())
|
|
|
|
return coldAccountAccessCost;
|
|
|
|
else if (_evmVersion >= langutil::EVMVersion::tangerineWhistle())
|
|
|
|
return 5000;
|
|
|
|
else
|
|
|
|
return 0;
|
2018-03-01 11:06:36 +00:00
|
|
|
}
|
2021-08-09 08:43:44 +00:00
|
|
|
inline unsigned selfdestructRefundGas(langutil::EVMVersion _evmVersion)
|
|
|
|
{
|
|
|
|
// Changes from EIP-3529
|
|
|
|
if (_evmVersion >= langutil::EVMVersion::london())
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 24000;
|
|
|
|
}
|
2016-04-06 18:55:46 +00:00
|
|
|
static unsigned const memoryGas = 3;
|
|
|
|
static unsigned const quadCoeffDiv = 512;
|
|
|
|
static unsigned const createDataGas = 200;
|
|
|
|
static unsigned const txGas = 21000;
|
|
|
|
static unsigned const txCreateGas = 53000;
|
|
|
|
static unsigned const txDataZeroGas = 4;
|
2019-11-07 14:10:08 +00:00
|
|
|
inline unsigned txDataNonZeroGas(langutil::EVMVersion _evmVersion)
|
|
|
|
{
|
|
|
|
return _evmVersion >= langutil::EVMVersion::istanbul() ? 16 : 68;
|
|
|
|
}
|
2016-04-06 18:55:46 +00:00
|
|
|
static unsigned const copyGas = 3;
|
|
|
|
}
|
2015-11-21 13:33:55 +00:00
|
|
|
|
2015-05-06 08:43:59 +00:00
|
|
|
/**
|
|
|
|
* Class that helps computing the maximum gas consumption for instructions.
|
2015-05-19 22:27:07 +00:00
|
|
|
* Has to be initialized with a certain known state that will be automatically updated for
|
|
|
|
* each call to estimateMax. These calls have to supply strictly subsequent AssemblyItems.
|
|
|
|
* A new gas meter has to be constructed (with a new state) for control flow changes.
|
2015-05-06 08:43:59 +00:00
|
|
|
*/
|
|
|
|
class GasMeter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct GasConsumption
|
|
|
|
{
|
2016-04-06 18:55:46 +00:00
|
|
|
GasConsumption(unsigned _value = 0, bool _infinite = false): value(_value), isInfinite(_infinite) {}
|
2020-04-01 03:04:29 +00:00
|
|
|
GasConsumption(u256 _value, bool _infinite = false): value(std::move(_value)), isInfinite(_infinite) {}
|
2015-05-06 08:43:59 +00:00
|
|
|
static GasConsumption infinite() { return GasConsumption(0, true); }
|
|
|
|
|
2015-05-22 07:33:57 +00:00
|
|
|
GasConsumption& operator+=(GasConsumption const& _other);
|
2021-01-14 12:02:14 +00:00
|
|
|
GasConsumption operator+(GasConsumption const& _other) const
|
|
|
|
{
|
|
|
|
GasConsumption result = *this;
|
|
|
|
result += _other;
|
|
|
|
return result;
|
|
|
|
}
|
2018-07-10 19:36:29 +00:00
|
|
|
bool operator<(GasConsumption const& _other) const
|
|
|
|
{
|
|
|
|
return std::make_pair(isInfinite, value) < std::make_pair(_other.isInfinite, _other.value);
|
|
|
|
}
|
2015-05-06 08:43:59 +00:00
|
|
|
|
|
|
|
u256 value;
|
|
|
|
bool isInfinite;
|
|
|
|
};
|
|
|
|
|
2015-05-19 22:27:07 +00:00
|
|
|
/// Constructs a new gas meter given the current state.
|
2020-04-01 03:04:29 +00:00
|
|
|
GasMeter(std::shared_ptr<KnownState> _state, langutil::EVMVersion _evmVersion, u256 _largestMemoryAccess = 0):
|
|
|
|
m_state(std::move(_state)), m_evmVersion(_evmVersion), m_largestMemoryAccess(std::move(_largestMemoryAccess)) {}
|
2015-05-19 22:27:07 +00:00
|
|
|
|
|
|
|
/// @returns an upper bound on the gas consumed by the given instruction and updates
|
|
|
|
/// the state.
|
2016-11-14 22:28:26 +00:00
|
|
|
/// @param _inculdeExternalCosts if true, include costs caused by other contracts in calls.
|
|
|
|
GasConsumption estimateMax(AssemblyItem const& _item, bool _includeExternalCosts = true);
|
2015-05-06 08:43:59 +00:00
|
|
|
|
2015-05-22 07:33:57 +00:00
|
|
|
u256 const& largestMemoryAccess() const { return m_largestMemoryAccess; }
|
|
|
|
|
2018-03-01 11:06:36 +00:00
|
|
|
/// @returns gas costs for simple instructions with constant gas costs (that do not
|
|
|
|
/// change with EVM versions)
|
2022-11-23 10:51:34 +00:00
|
|
|
static unsigned runGas(Instruction _instruction, langutil::EVMVersion _evmVersion);
|
2015-06-01 10:32:59 +00:00
|
|
|
|
2018-08-08 22:13:28 +00:00
|
|
|
/// @returns the gas cost of the supplied data, depending whether it is in creation code, or not.
|
2018-09-18 14:50:21 +00:00
|
|
|
/// In case of @a _inCreation, the data is only sent as a transaction and is not stored, whereas
|
|
|
|
/// otherwise code will be stored and have to pay "createDataGas" cost.
|
2019-11-07 14:10:08 +00:00
|
|
|
static u256 dataGas(bytes const& _data, bool _inCreation, langutil::EVMVersion _evmVersion);
|
2018-08-08 22:13:28 +00:00
|
|
|
|
2021-01-14 12:02:14 +00:00
|
|
|
/// @returns the gas cost of non-zero data of the supplied length, depending whether it is in creation code, or not.
|
|
|
|
/// In case of @a _inCreation, the data is only sent as a transaction and is not stored, whereas
|
|
|
|
/// otherwise code will be stored and have to pay "createDataGas" cost.
|
|
|
|
static u256 dataGas(uint64_t _length, bool _inCreation, langutil::EVMVersion _evmVersion);
|
|
|
|
|
2015-05-06 08:43:59 +00:00
|
|
|
private:
|
2015-05-19 22:27:07 +00:00
|
|
|
/// @returns _multiplier * (_value + 31) / 32, if _value is a known constant and infinite otherwise.
|
|
|
|
GasConsumption wordGas(u256 const& _multiplier, ExpressionClasses::Id _value);
|
|
|
|
/// @returns the gas needed to access the given memory position.
|
|
|
|
/// @todo this assumes that memory was never accessed before and thus over-estimates gas usage.
|
|
|
|
GasConsumption memoryGas(ExpressionClasses::Id _position);
|
|
|
|
/// @returns the memory gas for accessing the memory at a specific offset for a number of bytes
|
|
|
|
/// given as values on the stack at the given relative positions.
|
|
|
|
GasConsumption memoryGas(int _stackPosOffset, int _stackPosSize);
|
|
|
|
|
|
|
|
std::shared_ptr<KnownState> m_state;
|
2019-02-25 14:29:57 +00:00
|
|
|
langutil::EVMVersion m_evmVersion;
|
2015-05-19 22:27:07 +00:00
|
|
|
/// Largest point where memory was accessed since the creation of this object.
|
|
|
|
u256 m_largestMemoryAccess;
|
2015-05-06 08:43:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline std::ostream& operator<<(std::ostream& _str, GasMeter::GasConsumption const& _consumption)
|
|
|
|
{
|
|
|
|
if (_consumption.isInfinite)
|
2015-05-22 12:19:58 +00:00
|
|
|
return _str << "[???]";
|
2015-05-06 08:43:59 +00:00
|
|
|
else
|
2015-05-19 22:27:07 +00:00
|
|
|
return _str << std::dec << _consumption.value;
|
2015-05-06 08:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|