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 6854af7 commit 8b79027
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 11 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
34 changes: 26 additions & 8 deletions libethereum/Account.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,19 @@ class Account
Account(u256 _nonce, u256 _balance, h256 _contractRoot, h256 _codeHash, Changedness _c): m_isAlive(true), m_isUnchanged(_c == Unchanged), m_nonce(_nonce), m_balance(_balance), m_storageRoot(_contractRoot), m_codeHash(_codeHash) { assert(_contractRoot); }


/// Kill this account. Useful for the suicide opcode. Following this call, isAlive() returns false.
void kill() { m_isAlive = false; m_storageOverlay.clear(); m_codeHash = EmptySHA3; m_storageRoot = EmptyTrie; m_balance = 0; m_nonce = 0; changed(); }
/// Kill this account. Useful for the suicide opcode. Following this call, isAlive() returns
/// false.
void kill()
{
m_isAlive = false;
m_storageOverlay.clear();
m_storageOriginal.clear();
m_codeHash = EmptySHA3;
m_storageRoot = EmptyTrie;
m_balance = 0;
m_nonce = 0;
changed();
}

/// @returns true iff this object represents an account in the state. Returns false if this object
/// represents an account that should no longer exist in the trie (an account that never existed or was
Expand Down Expand Up @@ -129,6 +140,8 @@ class Account
/// @returns the storage overlay as a simple hash map.
std::unordered_map<u256, u256> const& storageOverlay() const { return m_storageOverlay; }

std::unordered_map<u256, u256> const& storageOriginal() const { return m_storageOriginal; }

/// Set a key/value pair in the account's storage. This actually goes into the overlay, for committing
/// to the trie later.
void setStorage(u256 _p, u256 _v) { m_storageOverlay[_p] = _v; changed(); }
Expand All @@ -141,7 +154,9 @@ class Account

/// Set a key/value pair in the account's storage to a value that is already present inside the
/// database.
void setStorageCache(u256 _p, u256 _v) const { const_cast<decltype(m_storageOverlay)&>(m_storageOverlay)[_p] = _v; }
void setStorageCache(u256 _p, u256 _v) const { m_storageOverlay[_p] = _v; }

void setStorageOriginal(u256 _p, u256 _v) const { m_storageOriginal[_p] = _v; }

/// @returns the hash of the account's code.
h256 codeHash() const { return m_codeHash; }
Expand Down Expand Up @@ -193,10 +208,13 @@ class Account
h256 m_codeHash = EmptySHA3;

/// The map with is overlaid onto whatever storage is implied by the m_storageRoot in the trie.
std::unordered_map<u256, u256> m_storageOverlay;
mutable std::unordered_map<u256, u256> m_storageOverlay;

/// The original values of the modified storage slots stored in m_storageOverlay.
mutable std::unordered_map<u256, u256> m_storageOriginal;

/// The associated code for this account. The SHA3 of this should be equal to m_codeHash unless m_codeHash
/// equals c_contractConceptionCodeHash.
/// The associated code for this account. The SHA3 of this should be equal to m_codeHash unless
/// m_codeHash equals c_contractConceptionCodeHash.
bytes m_codeCache;

/// Value for m_codeHash when this account is having its code determined.
Expand Down Expand Up @@ -249,7 +267,7 @@ class PrecompiledContract;
using PrecompiledContractMap = std::unordered_map<Address, PrecompiledContract>;

AccountMap jsonToAccountMap(std::string const& _json, u256 const& _defaultNonce = 0,
AccountMaskMap* o_mask = nullptr, PrecompiledContractMap* o_precompiled = nullptr,
const boost::filesystem::path& _configPath = {});
AccountMaskMap* o_mask = nullptr, PrecompiledContractMap* o_precompiled = nullptr,
const boost::filesystem::path& _configPath = {});
}
}
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
2 changes: 2 additions & 0 deletions libevm/LegacyVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ class LegacyVM: public VMFace
void adjustStack(unsigned _removed, unsigned _added);
uint64_t gasForMem(u512 _size);
void updateSSGas();
void updateSSGasPreEIP1283(u256 const& _currentValue, u256 const& _newValue);
void updateSSGasEIP1283(u256 const& _currentValue, u256 const& _newValue);
void updateIOGas();
void updateGas();
void updateMem(uint64_t _newMem);
Expand Down

0 comments on commit 8b79027

Please sign in to comment.