Browse Source

Count gas cost for SSTORE instruction

[#79942174]
cl-refactor
Paweł Bylica 10 years ago
parent
commit
01b95883f2
  1. 1
      evmcc/Compiler.cpp
  2. 23
      evmcc/GasMeter.cpp
  3. 3
      evmcc/GasMeter.h

1
evmcc/Compiler.cpp

@ -609,6 +609,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
{ {
auto index = stack.pop(); auto index = stack.pop();
auto value = stack.pop(); auto value = stack.pop();
gasMeter.countSStore(ext, index, value);
ext.setStore(index, value); ext.setStore(index, value);
break; break;
} }

23
evmcc/GasMeter.cpp

@ -9,6 +9,7 @@
#include "Type.h" #include "Type.h"
#include "Utils.h" #include "Utils.h"
#include "Ext.h"
namespace evmcc namespace evmcc
{ {
@ -100,12 +101,34 @@ void GasMeter::count(Instruction _inst)
m_checkCall = m_builder.CreateCall(m_gasCheckFunc, llvm::UndefValue::get(Type::i256)); m_checkCall = m_builder.CreateCall(m_gasCheckFunc, llvm::UndefValue::get(Type::i256));
} }
if (_inst != Instruction::SSTORE) // Handle cost of SSTORE separately in countSStore()
m_blockCost += getStepCost(_inst); m_blockCost += getStepCost(_inst);
if (isCostBlockEnd(_inst)) if (isCostBlockEnd(_inst))
commitCostBlock(); commitCostBlock();
} }
void GasMeter::countSStore(Ext& _ext, llvm::Value* _index, llvm::Value* _newValue)
{
assert(!m_checkCall); // Everything should've been commited before
static const auto sstoreCost = static_cast<uint64_t>(c_sstoreGas);
// [ADD] if oldValue == 0 and newValue != 0 => 2*cost
// [DEL] if oldValue != 0 and newValue == 0 => 0
auto oldValue = _ext.store(_index);
auto oldValueIsZero = m_builder.CreateICmpEQ(oldValue, Constant::get(0), "oldValueIsZero");
auto newValueIsZero = m_builder.CreateICmpEQ(_newValue, Constant::get(0), "newValueIsZero");
auto oldValueIsntZero = m_builder.CreateICmpNE(oldValue, Constant::get(0), "oldValueIsntZero");
auto newValueIsntZero = m_builder.CreateICmpNE(_newValue, Constant::get(0), "newValueIsntZero");
auto isAdd = m_builder.CreateAnd(oldValueIsZero, newValueIsntZero, "isAdd");
auto isDel = m_builder.CreateAnd(oldValueIsntZero, newValueIsZero, "isDel");
auto cost = m_builder.CreateSelect(isAdd, Constant::get(2 * sstoreCost), Constant::get(sstoreCost), "cost");
cost = m_builder.CreateSelect(isDel, Constant::get(0), cost, "cost");
m_builder.CreateCall(m_gasCheckFunc, cost);
}
void GasMeter::giveBack(llvm::Value* _gas) void GasMeter::giveBack(llvm::Value* _gas)
{ {
llvm::Value* gasCounter = m_builder.CreateLoad(m_gas, "gas"); llvm::Value* gasCounter = m_builder.CreateLoad(m_gas, "gas");

3
evmcc/GasMeter.h

@ -19,6 +19,9 @@ public:
/// Count step cost of instruction /// Count step cost of instruction
void count(dev::eth::Instruction _inst); void count(dev::eth::Instruction _inst);
/// Calculate & count gas cost for SSTORE instruction
void countSStore(class Ext& _ext, llvm::Value* _index, llvm::Value* _newValue);
/// Finalize cost-block by checking gas needed for the block before the block /// Finalize cost-block by checking gas needed for the block before the block
/// @param _additionalCost adds additional cost to cost-block before commit /// @param _additionalCost adds additional cost to cost-block before commit
void commitCostBlock(llvm::Value* _additionalCost = nullptr); void commitCostBlock(llvm::Value* _additionalCost = nullptr);

Loading…
Cancel
Save