Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Implement net gas metering for SSTORE in LegacyVM
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Aug 29, 2018
1 parent c83f749 commit 40ccda3
Show file tree
Hide file tree
Showing 8 changed files with 434 additions and 318 deletions.
4 changes: 4 additions & 0 deletions libethcore/EVMSchedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct EVMSchedule
bool haveDelegateCall = true;
bool eip150Mode = false;
bool eip158Mode = false;
bool eip1283Mode = false;
bool haveBitwiseShifting = false;
bool haveRevert = false;
bool haveReturnData = false;
Expand All @@ -47,7 +48,9 @@ struct EVMSchedule
unsigned sloadGas = 50;
unsigned sstoreSetGas = 20000;
unsigned sstoreResetGas = 5000;
unsigned sstoreUnchangedGas = 200;
unsigned sstoreRefundGas = 15000;
unsigned sstoreRefundNonzeroGas = 4800;
unsigned jumpdestGas = 1;
unsigned logGas = 375;
unsigned logDataGas = 8;
Expand Down Expand Up @@ -135,6 +138,7 @@ static const EVMSchedule ConstantinopleSchedule = []
schedule.haveCreate2 = true;
schedule.haveBitwiseShifting = true;
schedule.haveExtcodehash = true;
schedule.eip1283Mode = true;
return schedule;
}();

Expand Down
294 changes: 156 additions & 138 deletions libethereum/Account.h

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions libethereum/ExtVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class ExtVM : public ExtVMFace
/// Write a value in storage.
void setStore(u256 _n, u256 _v) final;

/// Read original storage value (before modifications in the current transaction).
u256 originalStorageValue(u256 const& _key) final
{
return m_s.originalStorageValue(myAddress, _key);
}

/// Read address's code.
bytes const& codeAt(Address _a) final { return m_s.code(_a); }

Expand Down
26 changes: 25 additions & 1 deletion libethereum/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ u256 State::storage(Address const& _id, u256 const& _key) const
string payload = memdb.at(_key);
u256 ret = payload.size() ? RLP(payload).toInt<u256>() : 0;
a->setStorageCache(_key, ret);
a->setStorageOriginal(_key, ret);
return ret;
}
else
Expand All @@ -437,7 +438,30 @@ u256 State::storage(Address const& _id, u256 const& _key) const
void State::setStorage(Address const& _contract, u256 const& _key, u256 const& _value)
{
m_changeLog.emplace_back(_contract, _key, storage(_contract, _key));
m_cache[_contract].setStorage(_key, _value);
Account& a = m_cache[_contract];
a.setStorage(_key, _value);
if (a.storageOriginal().find(_key) == a.storageOriginal().end())
{
SecureTrieDB<h256, OverlayDB> memdb(const_cast<OverlayDB*>(&m_db), a.baseRoot());
string payload = memdb.at(_key);
u256 original = payload.size() ? RLP(payload).toInt<u256>() : 0;
a.setStorageOriginal(_key, original);
}
}

u256 State::originalStorageValue(Address const& _contract, u256 const& _key) const
{
if (Account const* a = account(_contract))
{
auto it = a->storageOriginal().find(_key);
if (it != a->storageOriginal().end())
return it->second;

assert(a->storageOverlay().find(_key) == a->storageOverlay().end());
return storage(_contract, _key);
}
else
return 0;
}

void State::clearStorage(Address const& _contract)
Expand Down
5 changes: 5 additions & 0 deletions libethereum/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ class State
/// Set the value of a storage position of an account.
void setStorage(Address const& _contract, u256 const& _location, u256 const& _value);

/// Get the original value of a storage position of an account (before modifications saved in
/// account cache).
/// @returns 0 if no account exists at that address.
u256 originalStorageValue(Address const& _contract, u256 const& _key) const;

/// Clear the storage root hash of an account to the hash of the empty trie.
void clearStorage(Address const& _contract);

Expand Down
3 changes: 3 additions & 0 deletions libevm/ExtVMFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ class ExtVMFace: public evmc_context
/// Write a value in storage.
virtual void setStore(u256, u256) {}

/// Read original storage value (before modifications in the current transaction).
virtual u256 originalStorageValue(u256 const&) { return 0; }

/// Read address's balance.
virtual u256 balance(Address) { return 0; }

Expand Down
58 changes: 56 additions & 2 deletions libevm/LegacyVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,20 @@ void LegacyVM::adjustStack(unsigned _removed, unsigned _added)

void LegacyVM::updateSSGas()
{
if (!m_ext->store(m_SP[0]) && m_SP[1])
u256 const currentValue = m_ext->store(m_SP[0]);
u256 const newValue = m_SP[1];

if (m_schedule->eip1283Mode)
updateSSGasEIP1283(currentValue, newValue);
else
updateSSGasPreEIP1283(currentValue, newValue);
}

void LegacyVM::updateSSGasPreEIP1283(u256 const& _currentValue, u256 const& _newValue)
{
if (!_currentValue && _newValue)
m_runGas = toInt63(m_schedule->sstoreSetGas);
else if (m_ext->store(m_SP[0]) && !m_SP[1])
else if (_currentValue && !_newValue)
{
m_runGas = toInt63(m_schedule->sstoreResetGas);
m_ext->sub.refunds += m_schedule->sstoreRefundGas;
Expand All @@ -108,6 +119,49 @@ void LegacyVM::updateSSGas()
m_runGas = toInt63(m_schedule->sstoreResetGas);
}

void LegacyVM::updateSSGasEIP1283(u256 const& _currentValue, u256 const& _newValue)
{
if (_currentValue == _newValue)
m_runGas = m_schedule->sstoreUnchangedGas;
else
{
u256 const originalValue = m_ext->originalStorageValue(m_SP[0]);
if (originalValue == _currentValue)
{
if (originalValue == 0)
m_runGas = m_schedule->sstoreSetGas;
else
{
m_runGas = m_schedule->sstoreResetGas;
if (_newValue == 0)
m_ext->sub.refunds += m_schedule->sstoreRefundGas;
}
}
else
{
m_runGas = m_schedule->sstoreUnchangedGas;
if (originalValue != 0)
{
if (_currentValue == 0)
{
assert(m_ext->sub.refunds >= m_schedule->sstoreRefundGas);
m_ext->sub.refunds -= m_schedule->sstoreRefundGas;
}
else
m_ext->sub.refunds += m_schedule->sstoreRefundGas;
}
if (originalValue == _newValue)
{
if (originalValue == 0)
m_ext->sub.refunds +=
m_schedule->sstoreRefundGas + m_schedule->sstoreRefundNonzeroGas;
else
m_ext->sub.refunds += m_schedule->sstoreRefundNonzeroGas;
}
}
}
}


uint64_t LegacyVM::gasForMem(u512 _size)
{
Expand Down
Loading

0 comments on commit 40ccda3

Please sign in to comment.