Browse Source

correct calculation of LOG cost

cl-refactor
Artur Zawłocki 10 years ago
parent
commit
72b9da5712
  1. 3
      libevmjit/Compiler.cpp
  2. 18
      libevmjit/GasMeter.cpp
  3. 3
      libevmjit/GasMeter.h

3
libevmjit/Compiler.cpp

@ -805,6 +805,9 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode
auto numBytes = stack.pop();
_memory.require(beginIdx, numBytes);
// This will commit the current cost block
_gasMeter.countLogData(numBytes);
std::array<llvm::Value*,4> topics;
auto numTopics = static_cast<size_t>(inst) - static_cast<size_t>(Instruction::LOG0);
for (size_t i = 0; i < numTopics; ++i)

18
libevmjit/GasMeter.cpp

@ -47,6 +47,16 @@ uint64_t getStepCost(Instruction inst) // TODO: Add this function to FeeSructure
case Instruction::CREATE:
return static_cast<uint64_t>(c_createGas);
case Instruction::LOG0:
case Instruction::LOG1:
case Instruction::LOG2:
case Instruction::LOG3:
case Instruction::LOG4:
{
auto numTopics = static_cast<uint64_t>(inst) - static_cast<uint64_t>(Instruction::LOG0);
return static_cast<uint64_t>(c_logGas) + numTopics * static_cast<uint64_t>(c_logTopicGas);
}
default: // Assumes instruction code is valid
return static_cast<uint64_t>(c_stepGas);
}
@ -138,6 +148,14 @@ void GasMeter::countSStore(Ext& _ext, llvm::Value* _index, llvm::Value* _newValu
createCall(m_gasCheckFunc, cost);
}
void GasMeter::countLogData(llvm::Value* _dataLength)
{
assert(m_checkCall);
assert(m_blockCost > 0); // LOGn instruction is already counted
auto cost = m_builder.CreateMul(_dataLength, Constant::get(c_logDataGas), "logdata_cost");
commitCostBlock(cost);
}
void GasMeter::giveBack(llvm::Value* _gas)
{
m_runtimeManager.setGas(m_builder.CreateAdd(m_runtimeManager.getGas(), _gas));

3
libevmjit/GasMeter.h

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

Loading…
Cancel
Save