From 0b68d093b45b71bf976f8cf37a6b917aa29ac4e3 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 8 Aug 2018 23:13:28 +0100 Subject: [PATCH] Move dataGas calculation helper to GasMeter --- libevmasm/ConstantOptimiser.cpp | 10 +--------- libevmasm/GasMeter.cpp | 14 +++++++++++++- libevmasm/GasMeter.h | 3 +++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/libevmasm/ConstantOptimiser.cpp b/libevmasm/ConstantOptimiser.cpp index 07ece12ca..9844ba3a3 100644 --- a/libevmasm/ConstantOptimiser.cpp +++ b/libevmasm/ConstantOptimiser.cpp @@ -94,15 +94,7 @@ bigint ConstantOptimisationMethod::simpleRunGas(AssemblyItems const& _items) bigint ConstantOptimisationMethod::dataGas(bytes const& _data) const { assertThrow(_data.size() > 0, OptimizerException, "Empty bytecode generated."); - if (m_params.isCreation) - { - bigint gas; - for (auto b: _data) - gas += b ? GasCosts::txDataNonZeroGas : GasCosts::txDataZeroGas; - return gas; - } - else - return GasCosts::createDataGas * _data.size(); + return bigint(GasMeter::dataGas(_data, m_params.isCreation)); } size_t ConstantOptimisationMethod::bytesRequired(AssemblyItems const& _items) diff --git a/libevmasm/GasMeter.cpp b/libevmasm/GasMeter.cpp index caa06fc07..3554f8094 100644 --- a/libevmasm/GasMeter.cpp +++ b/libevmasm/GasMeter.cpp @@ -258,4 +258,16 @@ unsigned GasMeter::runGas(Instruction _instruction) return 0; } - +u256 GasMeter::dataGas(bytes const& _data, bool _inCreation) +{ + bigint gas = 0; + if (_inCreation) + { + for (auto b: _data) + gas += (b != 0) ? GasCosts::txDataNonZeroGas : GasCosts::txDataZeroGas; + } + else + gas = bigint(GasCosts::createDataGas) * _data.size(); + assertThrow(gas < bigint(u256(-1)), OptimizerException, "Gas cost exceeds 256 bits."); + return u256(gas); +} diff --git a/libevmasm/GasMeter.h b/libevmasm/GasMeter.h index fc3740d24..7cb8015f9 100644 --- a/libevmasm/GasMeter.h +++ b/libevmasm/GasMeter.h @@ -136,6 +136,9 @@ public: /// change with EVM versions) static unsigned runGas(Instruction _instruction); + /// @returns the gas cost of the supplied data, depending whether it is in creation code, or not. + static u256 dataGas(bytes const& _data, bool _inCreation); + private: /// @returns _multiplier * (_value + 31) / 32, if _value is a known constant and infinite otherwise. GasConsumption wordGas(u256 const& _multiplier, ExpressionClasses::Id _value);