diff options
author | chriseth <c@ethdev.com> | 2015-05-20 06:27:07 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-05-20 06:28:15 +0800 |
commit | d015945a1db28ba55ce674a73091742b781d2d9d (patch) | |
tree | 1dfbc3afa30e700181e04b99c9685ff707043024 /GasMeter.h | |
parent | 3ecd54a83513d8b59b5e27c671a036870cf1bc90 (diff) | |
download | dexon-solidity-d015945a1db28ba55ce674a73091742b781d2d9d.tar dexon-solidity-d015945a1db28ba55ce674a73091742b781d2d9d.tar.gz dexon-solidity-d015945a1db28ba55ce674a73091742b781d2d9d.tar.bz2 dexon-solidity-d015945a1db28ba55ce674a73091742b781d2d9d.tar.lz dexon-solidity-d015945a1db28ba55ce674a73091742b781d2d9d.tar.xz dexon-solidity-d015945a1db28ba55ce674a73091742b781d2d9d.tar.zst dexon-solidity-d015945a1db28ba55ce674a73091742b781d2d9d.zip |
Gas estimation taking known state into account.
Diffstat (limited to 'GasMeter.h')
-rw-r--r-- | GasMeter.h | 27 |
1 files changed, 25 insertions, 2 deletions
@@ -22,6 +22,7 @@ #pragma once #include <ostream> +#include <libevmasm/ExpressionClasses.h> #include <libevmasm/AssemblyItem.h> namespace dev @@ -29,8 +30,13 @@ namespace dev namespace eth { +class KnownState; + /** * Class that helps computing the maximum gas consumption for instructions. + * 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. */ class GasMeter { @@ -47,11 +53,28 @@ public: bool isInfinite; }; - /// Returns an upper bound on the gas consumed by the given instruction. + /// Constructs a new gas meter given the current state. + GasMeter(std::shared_ptr<KnownState> const& _state): m_state(_state) {} + + /// @returns an upper bound on the gas consumed by the given instruction and updates + /// the state. GasConsumption estimateMax(AssemblyItem const& _item); private: + /// @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); + static GasConsumption runGas(Instruction _instruction); + + std::shared_ptr<KnownState> m_state; + /// Largest point where memory was accessed since the creation of this object. + u256 m_largestMemoryAccess; }; inline std::ostream& operator<<(std::ostream& _str, GasMeter::GasConsumption const& _consumption) @@ -59,7 +82,7 @@ inline std::ostream& operator<<(std::ostream& _str, GasMeter::GasConsumption con if (_consumption.isInfinite) return _str << "inf"; else - return _str << _consumption.value; + return _str << std::dec << _consumption.value; } |