Ethereum  PoC-8
The C++ Implementation of Ethereum
State.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "State.h"
23 
24 #include "Block.h"
25 #include "BlockChain.h"
26 #include "ExtVM.h"
27 #include "TransactionQueue.h"
28 #include <libdevcore/Assertions.h>
29 #include <libdevcore/DBFactory.h>
30 #include <libdevcore/TrieHash.h>
31 #include <libevm/VMFactory.h>
32 #include <boost/filesystem.hpp>
33 #include <boost/timer.hpp>
34 
35 using namespace std;
36 using namespace dev;
37 using namespace dev::eth;
38 namespace fs = boost::filesystem;
39 
40 State::State(u256 const& _accountStartNonce, OverlayDB const& _db, BaseState _bs):
41  m_db(_db),
42  m_state(&m_db),
43  m_accountStartNonce(_accountStartNonce)
44 {
45  if (_bs != BaseState::PreExisting)
46  // Initialise to the state entailed by the genesis block; this guarantees the trie is built correctly.
47  m_state.init();
48 }
49 
50 State::State(State const& _s):
51  m_db(_s.m_db),
52  m_state(&m_db, _s.m_state.root(), Verification::Skip),
53  m_cache(_s.m_cache),
54  m_unchangedCacheEntries(_s.m_unchangedCacheEntries),
55  m_nonExistingAccountsCache(_s.m_nonExistingAccountsCache),
56  m_touched(_s.m_touched),
57  m_accountStartNonce(_s.m_accountStartNonce)
58 {}
59 
60 OverlayDB State::openDB(fs::path const& _basePath, h256 const& _genesisHash, WithExisting _we)
61 {
62  fs::path path = _basePath.empty() ? db::databasePath() : _basePath;
63 
64  if (db::isDiskDatabase() && _we == WithExisting::Kill)
65  {
66  clog(VerbosityDebug, "statedb") << "Killing state database (WithExisting::Kill).";
67  fs::remove_all(path / fs::path("state"));
68  }
69 
70  path /= fs::path(toHex(_genesisHash.ref().cropped(0, 4))) / fs::path(toString(c_databaseVersion));
71  if (db::isDiskDatabase())
72  {
73  fs::create_directories(path);
74  DEV_IGNORE_EXCEPTIONS(fs::permissions(path, fs::owner_all));
75  }
76 
77  try
78  {
79  std::unique_ptr<db::DatabaseFace> db = db::DBFactory::create(path / fs::path("state"));
80  clog(VerbosityTrace, "statedb") << "Opened state DB.";
81  return OverlayDB(std::move(db));
82  }
83  catch (boost::exception const& ex)
84  {
85  cwarn << boost::diagnostic_information(ex) << '\n';
86  if (!db::isDiskDatabase())
87  throw;
88  else if (fs::space(path / fs::path("state")).available < 1024)
89  {
90  cwarn << "Not enough available space found on hard drive. Please free some up and then re-run. Bailing.";
91  BOOST_THROW_EXCEPTION(NotEnoughAvailableSpace());
92  }
93  else
94  {
95  cwarn <<
96  "Database " <<
97  (path / fs::path("state")) <<
98  "already open. You appear to have another instance of ethereum running. Bailing.";
99  BOOST_THROW_EXCEPTION(DatabaseAlreadyOpen());
100  }
101  }
102 }
103 
105 {
106  eth::commit(_map, m_state);
108 }
109 
111 {
112  if (m_accountStartNonce == Invalid256)
113  BOOST_THROW_EXCEPTION(InvalidAccountStartNonceInState());
114  return m_accountStartNonce;
115 }
116 
117 void State::noteAccountStartNonce(u256 const& _actual)
118 {
119  if (m_accountStartNonce == Invalid256)
120  m_accountStartNonce = _actual;
121  else if (m_accountStartNonce != _actual)
122  BOOST_THROW_EXCEPTION(IncorrectAccountStartNonceInState());
123 }
124 
125 void State::removeEmptyAccounts()
126 {
127  for (auto& i: m_cache)
128  if (i.second.isDirty() && i.second.isEmpty())
129  i.second.kill();
130 }
131 
133 {
134  if (&_s == this)
135  return *this;
136 
137  m_db = _s.m_db;
138  m_state.open(&m_db, _s.m_state.root(), Verification::Skip);
139  m_cache = _s.m_cache;
140  m_unchangedCacheEntries = _s.m_unchangedCacheEntries;
141  m_nonExistingAccountsCache = _s.m_nonExistingAccountsCache;
142  m_touched = _s.m_touched;
143  m_accountStartNonce = _s.m_accountStartNonce;
144  return *this;
145 }
146 
147 Account const* State::account(Address const& _a) const
148 {
149  return const_cast<State*>(this)->account(_a);
150 }
151 
152 Account* State::account(Address const& _addr)
153 {
154  auto it = m_cache.find(_addr);
155  if (it != m_cache.end())
156  return &it->second;
157 
158  if (m_nonExistingAccountsCache.count(_addr))
159  return nullptr;
160 
161  // Populate basic info.
162  string stateBack = m_state.at(_addr);
163  if (stateBack.empty())
164  {
165  m_nonExistingAccountsCache.insert(_addr);
166  return nullptr;
167  }
168 
169  clearCacheIfTooLarge();
170 
171  RLP state(stateBack);
172  auto i = m_cache.emplace(
173  std::piecewise_construct,
174  std::forward_as_tuple(_addr),
175  std::forward_as_tuple(state[0].toInt<u256>(), state[1].toInt<u256>(), state[2].toHash<h256>(), state[3].toHash<h256>(), Account::Unchanged)
176  );
177  m_unchangedCacheEntries.push_back(_addr);
178  return &i.first->second;
179 }
180 
181 void State::clearCacheIfTooLarge() const
182 {
183  // TODO: Find a good magic number
184  while (m_unchangedCacheEntries.size() > 1000)
185  {
186  // Remove a random element
187  // FIXME: Do not use random device as the engine. The random device should be only used to seed other engine.
188  size_t const randomIndex = std::uniform_int_distribution<size_t>(0, m_unchangedCacheEntries.size() - 1)(dev::s_fixedHashEngine);
189 
190  Address const addr = m_unchangedCacheEntries[randomIndex];
191  swap(m_unchangedCacheEntries[randomIndex], m_unchangedCacheEntries.back());
192  m_unchangedCacheEntries.pop_back();
193 
194  auto cacheEntry = m_cache.find(addr);
195  if (cacheEntry != m_cache.end() && !cacheEntry->second.isDirty())
196  m_cache.erase(cacheEntry);
197  }
198 }
199 
200 void State::commit(CommitBehaviour _commitBehaviour)
201 {
202  if (_commitBehaviour == CommitBehaviour::RemoveEmptyAccounts)
203  removeEmptyAccounts();
204  m_touched += dev::eth::commit(m_cache, m_state);
205  m_changeLog.clear();
206  m_cache.clear();
207  m_unchangedCacheEntries.clear();
208 }
209 
210 unordered_map<Address, u256> State::addresses() const
211 {
212 #if ETH_FATDB
213  unordered_map<Address, u256> ret;
214  for (auto& i: m_cache)
215  if (i.second.isAlive())
216  ret[i.first] = i.second.balance();
217  for (auto const& i: m_state)
218  if (m_cache.find(i.first) == m_cache.end())
219  ret[i.first] = RLP(i.second)[1].toInt<u256>();
220  return ret;
221 #else
222  BOOST_THROW_EXCEPTION(InterfaceNotSupported() << errinfo_interface("State::addresses()"));
223 #endif
224 }
225 
226 std::pair<State::AddressMap, h256> State::addresses(
227  h256 const& _beginHash, size_t _maxResults) const
228 {
230  h256 nextKey;
231 
232 #if ETH_FATDB
233  for (auto it = m_state.hashedLowerBound(_beginHash); it != m_state.hashedEnd(); ++it)
234  {
235  auto const address = Address(it.key());
236  auto const itCachedAddress = m_cache.find(address);
237 
238  // skip if deleted in cache
239  if (itCachedAddress != m_cache.end() && itCachedAddress->second.isDirty() &&
240  !itCachedAddress->second.isAlive())
241  continue;
242 
243  // break when _maxResults fetched
244  if (addresses.size() == _maxResults)
245  {
246  nextKey = h256((*it).first);
247  break;
248  }
249 
250  h256 const hashedAddress((*it).first);
251  addresses[hashedAddress] = address;
252  }
253 #endif
254 
255  // get addresses from cache with hash >= _beginHash (both new and old touched, we can't
256  // distinguish them) and order by hash
257  AddressMap cacheAddresses;
258  for (auto const& addressAndAccount : m_cache)
259  {
260  auto const& address = addressAndAccount.first;
261  auto const addressHash = sha3(address);
262  auto const& account = addressAndAccount.second;
263  if (account.isDirty() && account.isAlive() && addressHash >= _beginHash)
264  cacheAddresses.emplace(addressHash, address);
265  }
266 
267  // merge addresses from DB and addresses from cache
268  addresses.insert(cacheAddresses.begin(), cacheAddresses.end());
269 
270  // if some new accounts were created in cache we need to return fewer results
271  if (addresses.size() > _maxResults)
272  {
273  auto itEnd = std::next(addresses.begin(), _maxResults);
274  nextKey = itEnd->first;
275  addresses.erase(itEnd, addresses.end());
276  }
277 
278  return {addresses, nextKey};
279 }
280 
281 
282 void State::setRoot(h256 const& _r)
283 {
284  m_cache.clear();
285  m_unchangedCacheEntries.clear();
286  m_nonExistingAccountsCache.clear();
287 // m_touched.clear();
288  m_state.setRoot(_r);
289 }
290 
291 bool State::addressInUse(Address const& _id) const
292 {
293  return !!account(_id);
294 }
295 
296 bool State::accountNonemptyAndExisting(Address const& _address) const
297 {
298  if (Account const* a = account(_address))
299  return !a->isEmpty();
300  else
301  return false;
302 }
303 
304 bool State::addressHasCode(Address const& _id) const
305 {
306  if (auto a = account(_id))
307  return a->codeHash() != EmptySHA3;
308  else
309  return false;
310 }
311 
312 u256 State::balance(Address const& _id) const
313 {
314  if (auto a = account(_id))
315  return a->balance();
316  else
317  return 0;
318 }
319 
320 void State::incNonce(Address const& _addr)
321 {
322  if (Account* a = account(_addr))
323  {
324  auto oldNonce = a->nonce();
325  a->incNonce();
326  m_changeLog.emplace_back(_addr, oldNonce);
327  }
328  else
329  // This is possible if a transaction has gas price 0.
330  createAccount(_addr, Account(requireAccountStartNonce() + 1, 0));
331 }
332 
333 void State::setNonce(Address const& _addr, u256 const& _newNonce)
334 {
335  if (Account* a = account(_addr))
336  {
337  auto oldNonce = a->nonce();
338  a->setNonce(_newNonce);
339  m_changeLog.emplace_back(_addr, oldNonce);
340  }
341  else
342  // This is possible when a contract is being created.
343  createAccount(_addr, Account(_newNonce, 0));
344 }
345 
346 void State::addBalance(Address const& _id, u256 const& _amount)
347 {
348  if (Account* a = account(_id))
349  {
350  // Log empty account being touched. Empty touched accounts are cleared
351  // after the transaction, so this event must be also reverted.
352  // We only log the first touch (not dirty yet), and only for empty
353  // accounts, as other accounts does not matter.
354  // TODO: to save space we can combine this event with Balance by having
355  // Balance and Balance+Touch events.
356  if (!a->isDirty() && a->isEmpty())
357  m_changeLog.emplace_back(Change::Touch, _id);
358 
359  // Increase the account balance. This also is done for value 0 to mark
360  // the account as dirty. Dirty account are not removed from the cache
361  // and are cleared if empty at the end of the transaction.
362  a->addBalance(_amount);
363  }
364  else
365  createAccount(_id, {requireAccountStartNonce(), _amount});
366 
367  if (_amount)
368  m_changeLog.emplace_back(Change::Balance, _id, _amount);
369 }
370 
371 void State::subBalance(Address const& _addr, u256 const& _value)
372 {
373  if (_value == 0)
374  return;
375 
376  Account* a = account(_addr);
377  if (!a || a->balance() < _value)
378  // TODO: I expect this never happens.
379  BOOST_THROW_EXCEPTION(NotEnoughCash());
380 
381  // Fall back to addBalance().
382  addBalance(_addr, 0 - _value);
383 }
384 
385 void State::setBalance(Address const& _addr, u256 const& _value)
386 {
387  Account* a = account(_addr);
388  u256 original = a ? a->balance() : 0;
389 
390  // Fall back to addBalance().
391  addBalance(_addr, _value - original);
392 }
393 
394 void State::createContract(Address const& _address)
395 {
396  createAccount(_address, {requireAccountStartNonce(), 0});
397 }
398 
399 void State::createAccount(Address const& _address, Account const&& _account)
400 {
401  assert(!addressInUse(_address) && "Account already exists");
402  m_cache[_address] = std::move(_account);
403  m_nonExistingAccountsCache.erase(_address);
404  m_changeLog.emplace_back(Change::Create, _address);
405 }
406 
407 void State::kill(Address _addr)
408 {
409  if (auto a = account(_addr))
410  a->kill();
411  // If the account is not in the db, nothing to kill.
412 }
413 
414 u256 State::getNonce(Address const& _addr) const
415 {
416  if (auto a = account(_addr))
417  return a->nonce();
418  else
419  return m_accountStartNonce;
420 }
421 
422 u256 State::storage(Address const& _id, u256 const& _key) const
423 {
424  if (Account const* a = account(_id))
425  return a->storageValue(_key, m_db);
426  else
427  return 0;
428 }
429 
430 void State::setStorage(Address const& _contract, u256 const& _key, u256 const& _value)
431 {
432  m_changeLog.emplace_back(_contract, _key, storage(_contract, _key));
433  m_cache[_contract].setStorage(_key, _value);
434 }
435 
436 u256 State::originalStorageValue(Address const& _contract, u256 const& _key) const
437 {
438  if (Account const* a = account(_contract))
439  return a->originalStorageValue(_key, m_db);
440  else
441  return 0;
442 }
443 
444 void State::clearStorage(Address const& _contract)
445 {
446  h256 const& oldHash{m_cache[_contract].baseRoot()};
447  if (oldHash == EmptyTrie)
448  return;
449  m_changeLog.emplace_back(Change::StorageRoot, _contract, oldHash);
450  m_cache[_contract].clearStorage();
451 }
452 
453 map<h256, pair<u256, u256>> State::storage(Address const& _id) const
454 {
455 #if ETH_FATDB
456  map<h256, pair<u256, u256>> ret;
457 
458  if (Account const* a = account(_id))
459  {
460  // Pull out all values from trie storage.
461  if (h256 root = a->baseRoot())
462  {
463  SecureTrieDB<h256, OverlayDB> memdb(const_cast<OverlayDB*>(&m_db), root); // promise we won't alter the overlay! :)
464 
465  for (auto it = memdb.hashedBegin(); it != memdb.hashedEnd(); ++it)
466  {
467  h256 const hashedKey((*it).first);
468  u256 const key = h256(it.key());
469  u256 const value = RLP((*it).second).toInt<u256>();
470  ret[hashedKey] = make_pair(key, value);
471  }
472  }
473 
474  // Then merge cached storage over the top.
475  for (auto const& i : a->storageOverlay())
476  {
477  h256 const key = i.first;
478  h256 const hashedKey = sha3(key);
479  if (i.second)
480  ret[hashedKey] = i;
481  else
482  ret.erase(hashedKey);
483  }
484  }
485  return ret;
486 #else
487  (void) _id;
488  BOOST_THROW_EXCEPTION(InterfaceNotSupported() << errinfo_interface("State::storage(Address const& _id)"));
489 #endif
490 }
491 
492 h256 State::storageRoot(Address const& _id) const
493 {
494  string s = m_state.at(_id);
495  if (s.size())
496  {
497  RLP r(s);
498  return r[2].toHash<h256>();
499  }
500  return EmptyTrie;
501 }
502 
503 bytes const& State::code(Address const& _addr) const
504 {
505  Account const* a = account(_addr);
506  if (!a || a->codeHash() == EmptySHA3)
507  return NullBytes;
508 
509  if (a->code().empty())
510  {
511  // Load the code from the backend.
512  Account* mutableAccount = const_cast<Account*>(a);
513  mutableAccount->noteCode(m_db.lookup(a->codeHash()));
514  CodeSizeCache::instance().store(a->codeHash(), a->code().size());
515  }
516 
517  return a->code();
518 }
519 
520 void State::setCode(Address const& _address, bytes&& _code)
521 {
522  m_changeLog.emplace_back(_address, code(_address));
523  m_cache[_address].setCode(std::move(_code));
524 }
525 
526 h256 State::codeHash(Address const& _a) const
527 {
528  if (Account const* a = account(_a))
529  return a->codeHash();
530  else
531  return EmptySHA3;
532 }
533 
534 size_t State::codeSize(Address const& _a) const
535 {
536  if (Account const* a = account(_a))
537  {
538  if (a->hasNewCode())
539  return a->code().size();
540  auto& codeSizeCache = CodeSizeCache::instance();
541  h256 codeHash = a->codeHash();
542  if (codeSizeCache.contains(codeHash))
543  return codeSizeCache.get(codeHash);
544  else
545  {
546  size_t size = code(_a).size();
547  codeSizeCache.store(codeHash, size);
548  return size;
549  }
550  }
551  else
552  return 0;
553 }
554 
555 size_t State::savepoint() const
556 {
557  return m_changeLog.size();
558 }
559 
560 void State::rollback(size_t _savepoint)
561 {
562  while (_savepoint != m_changeLog.size())
563  {
564  auto& change = m_changeLog.back();
565  auto& account = m_cache[change.address];
566 
567  // Public State API cannot be used here because it will add another
568  // change log entry.
569  switch (change.kind)
570  {
571  case Change::Storage:
572  account.setStorage(change.key, change.value);
573  break;
574  case Change::StorageRoot:
575  account.setStorageRoot(change.value);
576  break;
577  case Change::Balance:
578  account.addBalance(0 - change.value);
579  break;
580  case Change::Nonce:
581  account.setNonce(change.value);
582  break;
583  case Change::Create:
584  m_cache.erase(change.address);
585  break;
586  case Change::Code:
587  account.setCode(std::move(change.oldCode));
588  break;
589  case Change::Touch:
590  account.untouch();
591  m_unchangedCacheEntries.emplace_back(change.address);
592  break;
593  }
594  m_changeLog.pop_back();
595  }
596 }
597 
598 std::pair<ExecutionResult, TransactionReceipt> State::execute(EnvInfo const& _envInfo, SealEngineFace const& _sealEngine, Transaction const& _t, Permanence _p, OnOpFunc const& _onOp)
599 {
600  // Create and initialize the executive. This will throw fairly cheaply and quickly if the
601  // transaction is bad in any way.
602  Executive e(*this, _envInfo, _sealEngine);
603  ExecutionResult res;
604  e.setResultRecipient(res);
605 
606  auto onOp = _onOp;
607 #if ETH_VMTRACE
608  if (!onOp)
609  onOp = e.simpleTrace();
610 #endif
611  u256 const startGasUsed = _envInfo.gasUsed();
612  bool const statusCode = executeTransaction(e, _t, onOp);
613 
614  bool removeEmptyAccounts = false;
615  switch (_p)
616  {
618  m_cache.clear();
619  break;
621  removeEmptyAccounts = _envInfo.number() >= _sealEngine.chainParams().EIP158ForkBlock;
623  break;
625  break;
626  }
627 
628  TransactionReceipt const receipt = _envInfo.number() >= _sealEngine.chainParams().byzantiumForkBlock ?
629  TransactionReceipt(statusCode, startGasUsed + e.gasUsed(), e.logs()) :
630  TransactionReceipt(rootHash(), startGasUsed + e.gasUsed(), e.logs());
631  return make_pair(res, receipt);
632 }
633 
634 void State::executeBlockTransactions(Block const& _block, unsigned _txCount, LastBlockHashesFace const& _lastHashes, SealEngineFace const& _sealEngine)
635 {
636  u256 gasUsed = 0;
637  for (unsigned i = 0; i < _txCount; ++i)
638  {
639  EnvInfo envInfo(_block.info(), _lastHashes, gasUsed);
640 
641  Executive e(*this, envInfo, _sealEngine);
642  executeTransaction(e, _block.pending()[i], OnOpFunc());
643 
644  gasUsed += e.gasUsed();
645  }
646 }
647 
650 bool State::executeTransaction(Executive& _e, Transaction const& _t, OnOpFunc const& _onOp)
651 {
652  size_t const savept = savepoint();
653  try
654  {
655  _e.initialize(_t);
656 
657  if (!_e.execute())
658  _e.go(_onOp);
659  return _e.finalize();
660  }
661  catch (Exception const&)
662  {
663  rollback(savept);
664  throw;
665  }
666 }
667 
668 std::ostream& dev::eth::operator<<(std::ostream& _out, State const& _s)
669 {
670  _out << "--- " << _s.rootHash() << std::endl;
671  std::set<Address> d;
672  std::set<Address> dtr;
673  auto trie = SecureTrieDB<Address, OverlayDB>(const_cast<OverlayDB*>(&_s.m_db), _s.rootHash());
674  for (auto i: trie)
675  d.insert(i.first), dtr.insert(i.first);
676  for (auto i: _s.m_cache)
677  d.insert(i.first);
678 
679  for (auto i: d)
680  {
681  auto it = _s.m_cache.find(i);
682  Account* cache = it != _s.m_cache.end() ? &it->second : nullptr;
683  string rlpString = dtr.count(i) ? trie.at(i) : "";
684  RLP r(rlpString);
685  assert(cache || r);
686 
687  if (cache && !cache->isAlive())
688  _out << "XXX " << i << std::endl;
689  else
690  {
691  string lead = (cache ? r ? " * " : " + " : " ");
692  if (cache && r && cache->nonce() == r[0].toInt<u256>() && cache->balance() == r[1].toInt<u256>())
693  lead = " . ";
694 
695  stringstream contout;
696 
697  if ((cache && cache->codeHash() == EmptySHA3) || (!cache && r && (h256)r[3] != EmptySHA3))
698  {
699  std::map<u256, u256> mem;
700  std::set<u256> back;
701  std::set<u256> delta;
702  std::set<u256> cached;
703  if (r)
704  {
705  SecureTrieDB<h256, OverlayDB> memdb(const_cast<OverlayDB*>(&_s.m_db), r[2].toHash<h256>()); // promise we won't alter the overlay! :)
706  for (auto const& j: memdb)
707  mem[j.first] = RLP(j.second).toInt<u256>(), back.insert(j.first);
708  }
709  if (cache)
710  for (auto const& j: cache->storageOverlay())
711  {
712  if ((!mem.count(j.first) && j.second) || (mem.count(j.first) && mem.at(j.first) != j.second))
713  mem[j.first] = j.second, delta.insert(j.first);
714  else if (j.second)
715  cached.insert(j.first);
716  }
717  if (!delta.empty())
718  lead = (lead == " . ") ? "*.* " : "*** ";
719 
720  contout << " @:";
721  if (!delta.empty())
722  contout << "???";
723  else
724  contout << r[2].toHash<h256>();
725  if (cache && cache->hasNewCode())
726  contout << " $" << toHex(cache->code());
727  else
728  contout << " $" << (cache ? cache->codeHash() : r[3].toHash<h256>());
729 
730  for (auto const& j: mem)
731  if (j.second)
732  contout << std::endl << (delta.count(j.first) ? back.count(j.first) ? " * " : " + " : cached.count(j.first) ? " . " : " ") << std::hex << nouppercase << std::setw(64) << j.first << ": " << std::setw(0) << j.second ;
733  else
734  contout << std::endl << "XXX " << std::hex << nouppercase << std::setw(64) << j.first << "";
735  }
736  else
737  contout << " [SIMPLE]";
738  _out << lead << i << ": " << std::dec << (cache ? cache->nonce() : r[0].toInt<u256>()) << " #:" << (cache ? cache->balance() : r[1].toInt<u256>()) << contout.str() << std::endl;
739  }
740  }
741  return _out;
742 }
743 
744 State& dev::eth::createIntermediateState(State& o_s, Block const& _block, unsigned _txIndex, BlockChain const& _bc)
745 {
746  o_s = _block.state();
747  u256 const rootHash = _block.stateRootBeforeTx(_txIndex);
748  if (rootHash)
749  o_s.setRoot(rootHash);
750  else
751  {
752  o_s.setRoot(_block.stateRootBeforeTx(0));
753  o_s.executeBlockTransactions(_block, _txIndex, _bc.lastBlockHashes(), *_bc.sealEngine());
754  }
755  return o_s;
756 }
757 
758 template <class DB>
760 {
761  AddressHash ret;
762  for (auto const& i: _cache)
763  if (i.second.isDirty())
764  {
765  if (!i.second.isAlive())
766  _state.remove(i.first);
767  else
768  {
769  RLPStream s(4);
770  s << i.second.nonce() << i.second.balance();
771 
772  if (i.second.storageOverlay().empty())
773  {
774  assert(i.second.baseRoot());
775  s.append(i.second.baseRoot());
776  }
777  else
778  {
779  SecureTrieDB<h256, DB> storageDB(_state.db(), i.second.baseRoot());
780  for (auto const& j: i.second.storageOverlay())
781  if (j.second)
782  storageDB.insert(j.first, rlp(j.second));
783  else
784  storageDB.remove(j.first);
785  assert(storageDB.root());
786  s.append(storageDB.root());
787  }
788 
789  if (i.second.hasNewCode())
790  {
791  h256 ch = i.second.codeHash();
792  // Store the size of the code
793  CodeSizeCache::instance().store(ch, i.second.code().size());
794  _state.db()->insert(ch, &i.second.code());
795  s << ch;
796  }
797  else
798  s << i.second.codeHash();
799 
800  _state.insert(i.first, &s.out());
801  }
802  ret.insert(i.first);
803  }
804  return ret;
805 }
806 
807 
808 template AddressHash dev::eth::commit<OverlayDB>(AccountMap const& _cache, SecureTrieDB<Address, OverlayDB>& _state);
809 template AddressHash dev::eth::commit<StateCacheDB>(AccountMap const& _cache, SecureTrieDB<Address, StateCacheDB>& _state);
dev::eth::OnOpFunc
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VMFace const *, ExtVMFace const *)> OnOpFunc
Definition: ExtVMFace.h:115
dev::eth::Executive::go
bool go(OnOpFunc const &_onOp=OnOpFunc())
Definition: Executive.cpp:441
dev::EmptyTrie
h256 const EmptyTrie
Definition: TrieCommon.cpp:28
dev::eth::State::addressInUse
bool addressInUse(Address const &_address) const
Check if the address is in use.
Definition: State.cpp:291
dev::eth::c_databaseVersion
const unsigned c_databaseVersion
Current database version.
Definition: Common.cpp:51
dev::eth::Account::untouch
void untouch()
Definition: Account.h:102
dev::eth::State::rootHash
h256 rootHash() const
The hash of the root of our state tree.
Definition: State.h:310
dev::eth::State::CommitBehaviour::RemoveEmptyAccounts
@ RemoveEmptyAccounts
dev::eth::Account::setStorageRoot
void setStorageRoot(h256 const &_root)
Set the storage root. Used when clearStorage() is reverted.
Definition: Account.h:160
dev::eth::Transaction
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:86
dev::eth::State::subBalance
void subBalance(Address const &_addr, u256 const &_value)
Definition: State.cpp:371
dev::db::databasePath
fs::path databasePath()
Definition: DBFactory.cpp:97
dev::db::isDiskDatabase
bool isDiskDatabase()
Definition: DBFactory.cpp:80
dev::eth::SealEngineFace
Definition: SealEngine.h:46
dev::eth::State::setNonce
void setNonce(Address const &_addr, u256 const &_newNonce)
Set the account nonce.
Definition: State.cpp:333
dev::eth::State::openDB
static OverlayDB openDB(boost::filesystem::path const &_path, h256 const &_genesisHash, WithExisting _we=WithExisting::Trust)
Open a DB - useful for passing into the constructor & keeping for other states that are necessary.
Definition: State.cpp:60
dev::eth::State::kill
void kill(Address _a)
Delete an account (used for processing suicides).
Definition: State.cpp:407
dev::sha3
bool sha3(bytesConstRef _input, bytesRef o_output) noexcept
Definition: SHA3.cpp:28
dev::eth::Change::Balance
@ Balance
Definition: State.h:92
dev::eth::CodeSizeCache::instance
static CodeSizeCache & instance()
Definition: CodeSizeCache.h:57
dev::eth::State::code
bytes const & code(Address const &_addr) const
Definition: State.cpp:503
dev::eth::Permanence
Permanence
Definition: State.h:73
dev::eth::createIntermediateState
State & createIntermediateState(State &o_s, Block const &_block, unsigned _txIndex, BlockChain const &_bc)
Definition: State.cpp:744
dev::eth::Block::stateRootBeforeTx
h256 stateRootBeforeTx(unsigned _i) const
Definition: Block.cpp:858
dev::eth::Account::addBalance
void addBalance(u256 _value)
Increments the balance of this account by the given amount.
Definition: Account.h:112
dev::FixedHash::ref
bytesRef ref()
Definition: FixedHash.h:132
std::swap
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
Definition: Interface.h:282
dev::EmptySHA3
h256 const EmptySHA3
Definition: SHA3.cpp:25
dev::SpecificTrieDB::insert
void insert(KeyType _k, bytesConstRef _value)
Definition: TrieDB.h:330
dev::h256
FixedHash< 32 > h256
Definition: FixedHash.h:356
dev::eth::State::noteAccountStartNonce
void noteAccountStartNonce(u256 const &_actual)
Definition: State.cpp:117
dev::RLPStream::out
bytes const & out() const
Read the byte stream.
Definition: RLP.h:419
dev::eth::AccountMap
std::unordered_map< Address, Account > AccountMap
Definition: Account.h:267
dev::eth::Change::Storage
@ Storage
Definition: State.h:96
dev::eth::State::getNonce
u256 getNonce(Address const &_addr) const
Definition: State.cpp:414
dev::eth::commit
AddressHash commit(AccountMap const &_cache, SecureTrieDB< Address, DB > &_state)
Definition: State.cpp:759
dev::eth::State::commit
void commit(CommitBehaviour _commitBehaviour)
Definition: State.cpp:200
dev::eth::State::incNonce
void incNonce(Address const &_id)
Increament the account nonce.
Definition: State.cpp:320
dev::eth::CodeSizeCache::store
void store(h256 const &_hash, size_t size)
Definition: CodeSizeCache.h:39
dev::eth
Definition: BasicAuthority.h:32
dev::eth::SealEngineFace::chainParams
ChainOperationParams const & chainParams() const
Definition: SealEngine.h:80
dev::eth::State::codeSize
size_t codeSize(Address const &_contract) const
Definition: State.cpp:534
dev::eth::BlockChain::sealEngine
SealEngineFace * sealEngine() const
Definition: BlockChain.h:308
dev::toString
std::string toString(std::chrono::time_point< T > const &_e, std::string const &_format="%F %T")
Definition: CommonIO.h:86
dev::eth::Executive::gasUsed
u256 gasUsed() const
Definition: Executive.cpp:206
dev::rlp
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:453
dev::eth::ChainOperationParams::EIP158ForkBlock
u256 EIP158ForkBlock
Definition: ChainOperationParams.h:92
dev::eth::Change::Touch
@ Touch
Account was touched for the first time.
Definition: State.h:112
dev::eth::State::setBalance
void setBalance(Address const &_addr, u256 const &_value)
Definition: State.cpp:385
dev::FixedHash< 32 >
dev::eth::Permanence::Reverted
@ Reverted
dev::WithExisting::Kill
@ Kill
dev::eth::State::accountNonemptyAndExisting
bool accountNonemptyAndExisting(Address const &_address) const
Definition: State.cpp:296
dev::eth::Account::isAlive
bool isAlive() const
Definition: Account.h:97
dev::eth::Executive::initialize
void initialize(bytesConstRef _transaction)
Initializes the executive for evaluating a transaction. You must call finalize() at some point follow...
Definition: Executive.h:132
dev::eth::Account::setStorage
void setStorage(u256 _p, u256 _v)
Definition: Account.h:148
dev::eth::Executive::simpleTrace
OnOpFunc simpleTrace()
Operation function for providing a simple trace of the VM execution.
Definition: Executive.cpp:420
dev::eth::State::addBalance
void addBalance(Address const &_id, u256 const &_amount)
Definition: State.cpp:346
dev::eth::Executive::setResultRecipient
void setResultRecipient(ExecutionResult &_res)
Collect execution results in the result storage provided.
Definition: Executive.h:184
dev::WithExisting
WithExisting
Definition: Common.h:292
dev::Exception
Base class for all exceptions.
Definition: Exceptions.h:39
dev::eth::State::clearStorage
void clearStorage(Address const &_contract)
Clear the storage root hash of an account to the hash of the empty trie.
Definition: State.cpp:444
dev::AddressHash
std::unordered_set< h160 > AddressHash
A hash set of Ethereum addresses.
Definition: Address.h:36
dev::eth::Account::balance
u256 const & balance() const
Definition: Account.h:109
dev::eth::Change::Create
@ Create
Account was created (it was not existing before).
Definition: State.h:106
dev::eth::State::operator=
State & operator=(State const &_s)
Copy state object.
Definition: State.cpp:132
dev::eth::Executive::logs
LogEntries const & logs() const
Definition: Executive.h:146
Block.h
ExtVM.h
dev::eth::EnvInfo::gasUsed
u256 const & gasUsed() const
Definition: ExtVMFace.h:165
dev::eth::Permanence::Uncommitted
@ Uncommitted
Uncommitted state for change log readings in tests.
dev::eth::State::executeBlockTransactions
void executeBlockTransactions(Block const &_block, unsigned _txCount, LastBlockHashesFace const &_lastHashes, SealEngineFace const &_sealEngine)
Definition: State.cpp:634
dev::eth::Change::Code
@ Code
New code was added to an account (by "create" message execution).
Definition: State.h:109
dev::OverlayDB::lookup
std::string lookup(h256 const &_h) const
Definition: OverlayDB.cpp:125
dev::vector_ref::cropped
vector_ref< _T > cropped(size_t _begin, size_t _count) const
Definition: vector_ref.h:60
dev::eth::State::populateFrom
void populateFrom(AccountMap const &_map)
Populate the state from the given AccountMap. Just uses dev::eth::commit().
Definition: State.cpp:104
dev::eth::Account::Unchanged
@ Unchanged
Account starts as though it has not been changed.
Definition: Account.h:65
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::SpecificTrieDB::remove
void remove(KeyType _k)
Definition: TrieDB.h:332
dev::eth::Block::pending
Transactions const & pending() const
Get the list of pending transactions.
Definition: Block.h:183
VMFactory.h
dev::eth::Executive
Message-call/contract-creation executor; useful for executing transactions.
Definition: Executive.h:104
dev::eth::BlockChain
Implements the blockchain database. All data this gives is disk-backed. @threadsafe.
Definition: BlockChain.h:105
dev::eth::BlockChain::lastBlockHashes
LastBlockHashesFace const & lastBlockHashes() const
Definition: BlockChain.h:186
dev::eth::Account::noteCode
void noteCode(bytesConstRef _code)
Definition: Account.h:178
TrieHash.h
dev::eth::State::CommitBehaviour::KeepEmptyAccounts
@ KeepEmptyAccounts
dev::eth::Block::state
State const & state() const
Get the backing state object.
Definition: Block.h:158
dev::OverlayDB
Definition: OverlayDB.h:34
dev::eth::State::State
State(u256 const &_accountStartNonce)
Default constructor; creates with a blank database prepopulated with the genesis block.
Definition: State.h:176
BlockChain.h
dev::eth::State::addressHasCode
bool addressHasCode(Address const &_address) const
Check if the address contains executable code.
Definition: State.cpp:304
dev::Invalid256
constexpr u256 Invalid256
Definition: Common.h:147
DBFactory.h
dev::eth::TransactionException::NotEnoughCash
@ NotEnoughCash
dev::RLPStream
Class for writing to an RLP bytestream.
Definition: RLP.h:370
dev::eth::Change::Nonce
@ Nonce
Account nonce was changed.
Definition: State.h:103
dev::NullBytes
bytes const NullBytes
Definition: Common.cpp:33
dev::eth::Account::hasNewCode
bool hasNewCode() const
Definition: Account.h:171
dev::VerbosityDebug
@ VerbosityDebug
Definition: Log.h:71
dev::RLPStream::append
RLPStream & append(unsigned _s)
Append given datum to the byte stream.
Definition: RLP.h:381
dev::eth::BaseState
BaseState
Definition: State.h:67
dev::eth::State::db
OverlayDB const & db() const
Definition: State.h:195
dev::eth::State::AddressMap
std::map< h256, Address > AddressMap
Definition: State.h:173
dev::db::DBFactory::create
static std::unique_ptr< DatabaseFace > create()
Definition: DBFactory.cpp:135
dev::eth::Executive::finalize
bool finalize()
Definition: Executive.cpp:535
dev::eth::State::requireAccountStartNonce
u256 const & requireAccountStartNonce() const
Definition: State.cpp:110
dev::s_fixedHashEngine
std::random_device s_fixedHashEngine
Definition: FixedHash.cpp:24
Assertions.h
dev::eth::Account::code
bytes const & code() const
Definition: Account.h:181
dev::FixedHash::size
@ size
Definition: FixedHash.h:53
dev::eth::State::setStorage
void setStorage(Address const &_contract, u256 const &_location, u256 const &_value)
Set the value of a storage position of an account.
Definition: State.cpp:430
dev::eth::Permanence::Committed
@ Committed
dev::eth::Account::isDirty
bool isDirty() const
Definition: Account.h:100
dev::eth::ChainOperationParams::byzantiumForkBlock
u256 byzantiumForkBlock
Definition: ChainOperationParams.h:93
dev::eth::EnvInfo::number
int64_t number() const
Definition: ExtVMFace.h:159
std
Definition: FixedHash.h:393
dev::eth::State
Definition: State.h:160
dev::Verification::Skip
@ Skip
dev::u256
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
dev::eth::State::savepoint
size_t savepoint() const
Definition: State.cpp:555
dev::RLP::toInt
_T toInt(int _flags=Strict) const
Converts to int of type given; if isData(), decodes as big-endian bytestream.
Definition: RLP.h:257
dev::eth::Change::StorageRoot
@ StorageRoot
Definition: State.h:100
dev::eth::State::storageRoot
h256 storageRoot(Address const &_contract) const
Get the root of the storage of an account.
Definition: State.cpp:492
dev::eth::operator<<
std::ostream & operator<<(std::ostream &_out, BlockHeader const &_bi)
Definition: BlockHeader.h:217
dev::eth::State::originalStorageValue
u256 originalStorageValue(Address const &_contract, u256 const &_key) const
Definition: State.cpp:436
dev::RLP::toHash
_N toHash(int _flags=Strict) const
Definition: RLP.h:288
dev::eth::State::setRoot
void setRoot(h256 const &_root)
Resets any uncommitted changes to the cache.
Definition: State.cpp:282
dev::eth::TransactionReceipt
Definition: TransactionReceipt.h:40
dev::eth::State::addresses
std::unordered_map< Address, u256 > addresses() const
Definition: State.cpp:210
dev::eth::Account::setNonce
void setNonce(u256 const &_nonce)
Definition: Account.h:122
dev
Definition: Address.cpp:21
dev::eth::State::setCode
void setCode(Address const &_address, bytes &&_code)
Sets the code of the account. Must only be called during / after contract creation.
Definition: State.cpp:520
dev::eth::Account::codeHash
h256 codeHash() const
Definition: Account.h:169
dev::eth::State::balance
u256 balance(Address const &_id) const
Definition: State.cpp:312
dev::eth::Block::info
BlockHeader const & info() const
Get the header information on the present block.
Definition: Block.h:273
dev::eth::State::codeHash
h256 codeHash(Address const &_contract) const
Definition: State.cpp:526
dev::eth::ExecutionResult
Description of the result of executing a transaction.
Definition: Transaction.h:71
dev::eth::Account::storageOverlay
std::unordered_map< u256, u256 > const & storageOverlay() const
Definition: Account.h:144
dev::Verification
Verification
Definition: TrieDB.h:35
dev::eth::BaseState::PreExisting
@ PreExisting
clog
#define clog(SEVERITY, CHANNEL)
dev::eth::Account::nonce
u256 nonce() const
Definition: Account.h:115
cwarn
#define cwarn
dev::eth::State::rollback
void rollback(size_t _savepoint)
Revert all recent changes up to the given _savepoint savepoint.
Definition: State.cpp:560
DEV_IGNORE_EXCEPTIONS
#define DEV_IGNORE_EXCEPTIONS(X)
Definition: Common.h:59
dev::errinfo_interface
boost::error_info< struct tag_interface, std::string > errinfo_interface
Definition: Exceptions.h:95
dev::toHex
std::string toHex(Iterator _it, Iterator _end, std::string const &_prefix)
Definition: CommonData.h:46
dev::eth::State::execute
std::pair< ExecutionResult, TransactionReceipt > execute(EnvInfo const &_envInfo, SealEngineFace const &_sealEngine, Transaction const &_t, Permanence _p=Permanence::Committed, OnOpFunc const &_onOp=OnOpFunc())
Definition: State.cpp:598
dev::eth::State::storage
u256 storage(Address const &_contract, u256 const &_memory) const
Definition: State.cpp:422
dev::eth::State::CommitBehaviour
CommitBehaviour
Definition: State.h:168
dev::eth::State::createContract
void createContract(Address const &_address)
Create a contract at the given address (with unset code and unchanged balance).
Definition: State.cpp:394
dev::eth::EnvInfo
Definition: ExtVMFace.h:143
dev::eth::LastBlockHashesFace
Interface for getting a list of recent block hashes @threadsafe.
Definition: LastBlockHashesFace.h:37
dev::RLP
Definition: RLP.h:48
dev::VerbosityTrace
@ VerbosityTrace
Definition: Log.h:72
dev::Address
h160 Address
Definition: Address.h:30
dev::SpecificTrieDB
Definition: TrieDB.h:318
dev::eth::Block
Active model of a block within the block chain. Keeps track of all transactions, receipts and state f...
Definition: Block.h:69
TransactionQueue.h
dev::eth::Account
Definition: Account.h:57
dev::eth::Executive::execute
bool execute()
Definition: Executive.cpp:269
State.h
dev::eth::Account::setCode
void setCode(bytes &&_code)
Sets the code of the account. Used by "create" messages.
Definition: Account.cpp:37