mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-20 03:36:44 +00:00
parent
1c70f3d099
commit
0d679f0c73
3 changed files with 28 additions and 1 deletions
|
|
@ -609,6 +609,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
|||
{
|
||||
auto index = stack.pop();
|
||||
auto value = stack.pop();
|
||||
gasMeter.countSStore(ext, index, value);
|
||||
ext.setStore(index, value);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include "Type.h"
|
||||
#include "Utils.h"
|
||||
#include "Ext.h"
|
||||
|
||||
namespace evmcc
|
||||
{
|
||||
|
|
@ -100,12 +101,34 @@ void GasMeter::count(Instruction _inst)
|
|||
m_checkCall = m_builder.CreateCall(m_gasCheckFunc, llvm::UndefValue::get(Type::i256));
|
||||
}
|
||||
|
||||
m_blockCost += getStepCost(_inst);
|
||||
if (_inst != Instruction::SSTORE) // Handle cost of SSTORE separately in countSStore()
|
||||
m_blockCost += getStepCost(_inst);
|
||||
|
||||
if (isCostBlockEnd(_inst))
|
||||
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)
|
||||
{
|
||||
llvm::Value* gasCounter = m_builder.CreateLoad(m_gas, "gas");
|
||||
|
|
|
|||
|
|
@ -19,6 +19,9 @@ public:
|
|||
/// Count step cost of instruction
|
||||
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
|
||||
/// @param _additionalCost adds additional cost to cost-block before commit
|
||||
void commitCostBlock(llvm::Value* _additionalCost = nullptr);
|
||||
|
|
|
|||
Loading…
Reference in a new issue