Fabcoin Core  0.16.2
P2P Digital Currency
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 <ctime>
25 #include <boost/filesystem.hpp>
26 #include <boost/timer.hpp>
27 #include <libdevcore/CommonIO.h>
28 #include <libdevcore/Assertions.h>
29 #ifndef FASC_BUILD
30 #include <libdevcore/TrieHash.h>
31 #endif
32 #include <libevmcore/Instruction.h>
33 #include <libethcore/Exceptions.h>
34 #include <libevm/VMFactory.h>
35 #include "BlockChain.h"
36 #include "CodeSizeCache.h"
37 #include "Defaults.h"
38 #include "ExtVM.h"
39 #include "Executive.h"
40 #include "BlockChain.h"
41 #include "TransactionQueue.h"
42 
43 using namespace std;
44 using namespace dev;
45 using namespace dev::eth;
46 using namespace dev::eth::detail;
47 namespace fs = boost::filesystem;
48 
49 const char* StateSafeExceptions::name() { return EthViolet "⚙" EthBlue " ℹ"; }
50 const char* StateDetail::name() { return EthViolet "⚙" EthWhite " ◌"; }
51 const char* StateTrace::name() { return EthViolet "⚙" EthGray " ◎"; }
52 const char* StateChat::name() { return EthViolet "⚙" EthWhite " ◌"; }
53 
54 State::State(u256 const& _accountStartNonce, OverlayDB const& _db, BaseState _bs):
55  m_db(_db),
56  m_state(&m_db),
57  m_accountStartNonce(_accountStartNonce)
58 {
59  if (_bs != BaseState::PreExisting)
60  // Initialise to the state entailed by the genesis block; this guarantees the trie is built correctly.
61  m_state.init();
62 }
63 
64 State::State(State const& _s):
65  m_db(_s.m_db),
67  m_cache(_s.m_cache),
70  m_touched(_s.m_touched),
72 {}
73 
74 OverlayDB State::openDB(std::string const& _basePath, h256 const& _genesisHash, WithExisting _we)
75 {
76  std::string path = _basePath.empty() ? Defaults::get()->m_dbPath : _basePath;
77 
78  if (_we == WithExisting::Kill)
79  {
80  cnote << "Killing state database (WithExisting::Kill).";
81  boost::filesystem::remove_all(path + "/state");
82  }
83 
84  path += "/" + toHex(_genesisHash.ref().cropped(0, 4)) + "/" + toString(c_databaseVersion);
85  boost::filesystem::create_directories(path);
86  DEV_IGNORE_EXCEPTIONS(fs::permissions(path, fs::owner_all));
87 
88  ldb::Options o;
89  o.max_open_files = 256;
90  o.create_if_missing = true;
91  ldb::DB* db = nullptr;
92  ldb::Status status = ldb::DB::Open(o, path + "/state", &db);
93  if (!status.ok() || !db)
94  {
95  if (boost::filesystem::space(path + "/state").available < 1024)
96  {
97  cwarn << "Not enough available space found on hard drive. Please free some up and then re-run. Bailing.";
98  BOOST_THROW_EXCEPTION(NotEnoughAvailableSpace());
99  }
100  else
101  {
102  cwarn << status.ToString();
103  cwarn <<
104  "Database " <<
105  (path + "/state") <<
106  "already open. You appear to have another instance of ethereum running. Bailing.";
107  BOOST_THROW_EXCEPTION(DatabaseAlreadyOpen());
108  }
109  }
110 
111  ctrace << "Opened state DB.";
112  return OverlayDB(db);
113 }
114 
116 {
117  eth::commit(_map, m_state);
119 }
120 
122 {
124  BOOST_THROW_EXCEPTION(InvalidAccountStartNonceInState());
125  return m_accountStartNonce;
126 }
127 
128 void State::noteAccountStartNonce(u256 const& _actual)
129 {
131  m_accountStartNonce = _actual;
132  else if (m_accountStartNonce != _actual)
133  BOOST_THROW_EXCEPTION(IncorrectAccountStartNonceInState());
134 }
135 
137 {
138  for (auto& i: m_cache)
139  if (i.second.isDirty() && i.second.isEmpty())
140  i.second.kill();
141 }
142 
144 {
145  if (&_s == this)
146  return *this;
147 
148  m_db = _s.m_db;
149  m_state.open(&m_db, _s.m_state.root(), Verification::Skip);
150  m_cache = _s.m_cache;
153  m_touched = _s.m_touched;
155  return *this;
156 }
157 
158 Account const* State::account(Address const& _a) const
159 {
160  return const_cast<State*>(this)->account(_a);
161 }
162 
164 {
165  auto it = m_cache.find(_addr);
166  if (it != m_cache.end())
167  return &it->second;
168 
169  if (m_nonExistingAccountsCache.count(_addr))
170  return nullptr;
171 
172  // Populate basic info.
173  string stateBack = m_state.at(_addr);
174  if (stateBack.empty())
175  {
176  m_nonExistingAccountsCache.insert(_addr);
177  return nullptr;
178  }
179 
181 
182  RLP state(stateBack);
183  auto i = m_cache.emplace(
184  std::piecewise_construct,
185  std::forward_as_tuple(_addr),
186  std::forward_as_tuple(state[0].toInt<u256>(), state[1].toInt<u256>(), state[2].toHash<h256>(), state[3].toHash<h256>(), Account::Unchanged)
187  );
188  m_unchangedCacheEntries.push_back(_addr);
189  return &i.first->second;
190 }
191 
193 {
194  // TODO: Find a good magic number
195  while (m_unchangedCacheEntries.size() > 1000)
196  {
197  // Remove a random element
198  size_t const randomIndex = boost::random::uniform_int_distribution<size_t>(0, m_unchangedCacheEntries.size() - 1)(dev::s_fixedHashEngine);
199 
200  Address const addr = m_unchangedCacheEntries[randomIndex];
202  m_unchangedCacheEntries.pop_back();
203 
204  auto cacheEntry = m_cache.find(addr);
205  if (cacheEntry != m_cache.end() && !cacheEntry->second.isDirty())
206  m_cache.erase(cacheEntry);
207  }
208 }
209 
210 void State::commit(CommitBehaviour _commitBehaviour)
211 {
212  if (_commitBehaviour == CommitBehaviour::RemoveEmptyAccounts)
215  m_changeLog.clear();
216  m_cache.clear();
217  m_unchangedCacheEntries.clear();
218 }
219 
220 unordered_map<Address, u256> State::addresses() const
221 {
222 #if ETH_FATDB
223  unordered_map<Address, u256> ret;
224  for (auto& i: m_cache)
225  if (i.second.isAlive())
226  ret[i.first] = i.second.balance();
227  for (auto const& i: m_state)
228  if (m_cache.find(i.first) == m_cache.end())
229  ret[i.first] = RLP(i.second)[1].toInt<u256>();
230  return ret;
231 #else
232  BOOST_THROW_EXCEPTION(InterfaceNotSupported("State::addresses()"));
233 #endif
234 }
235 
236 void State::setRoot(h256 const& _r)
237 {
238  m_cache.clear();
239  m_unchangedCacheEntries.clear();
241 // m_touched.clear();
242  m_state.setRoot(_r);
243 }
244 
245 bool State::addressInUse(Address const& _id) const
246 {
247  return !!account(_id);
248 }
249 
250 bool State::accountNonemptyAndExisting(Address const& _address) const
251 {
252  if (Account const* a = account(_address))
253  return !a->isEmpty();
254  else
255  return false;
256 }
257 
258 bool State::addressHasCode(Address const& _id) const
259 {
260  if (auto a = account(_id))
261  return a->codeHash() != EmptySHA3;
262  else
263  return false;
264 }
265 
266 u256 State::balance(Address const& _id) const
267 {
268  if (auto a = account(_id))
269  return a->balance();
270  else
271  return 0;
272 }
273 
274 void State::incNonce(Address const& _addr)
275 {
276  if (Account* a = account(_addr))
277  {
278  a->incNonce();
279  m_changeLog.emplace_back(Change::Nonce, _addr);
280  }
281  else
282  // This is possible if a transaction has gas price 0.
284 }
285 
286 void State::addBalance(Address const& _id, u256 const& _amount)
287 {
288  if (Account* a = account(_id))
289  {
290  // Log empty account being touched. Empty touched accounts are cleared
291  // after the transaction, so this event must be also reverted.
292  // We only log the first touch (not dirty yet), and only for empty
293  // accounts, as other accounts does not matter.
294  // TODO: to save space we can combine this event with Balance by having
295  // Balance and Balance+Touch events.
296  if (!a->isDirty() && a->isEmpty())
297  m_changeLog.emplace_back(Change::Touch, _id);
298 
299  // Increase the account balance. This also is done for value 0 to mark
300  // the account as dirty. Dirty account are not removed from the cache
301  // and are cleared if empty at the end of the transaction.
302  a->addBalance(_amount);
303  }
304  else
305  createAccount(_id, {requireAccountStartNonce(), _amount});
306 
307  if (_amount)
308  m_changeLog.emplace_back(Change::Balance, _id, _amount);
309 }
310 
311 void State::subBalance(Address const& _addr, u256 const& _value)
312 {
313  if (_value == 0)
314  return;
315 
316  Account* a = account(_addr);
317  if (!a || a->balance() < _value)
318  // TODO: I expect this never happens.
319  BOOST_THROW_EXCEPTION(NotEnoughCash());
320 
321  // Fall back to addBalance().
322  addBalance(_addr, 0 - _value);
323 }
324 
325 void State::createContract(Address const& _address)
326 {
327  createAccount(_address, {requireAccountStartNonce(), 0});
328 }
329 
330 void State::createAccount(Address const& _address, Account const&& _account)
331 {
332  assert(!addressInUse(_address) && "Account already exists");
333  m_cache[_address] = std::move(_account);
334  m_nonExistingAccountsCache.erase(_address);
335  m_changeLog.emplace_back(Change::Create, _address);
336 }
337 
338 void State::kill(Address _addr)
339 {
340  if (auto a = account(_addr))
341  a->kill();
342  // If the account is not in the db, nothing to kill.
343 }
344 
345 u256 State::getNonce(Address const& _addr) const
346 {
347  if (auto a = account(_addr))
348  return a->nonce();
349  else
350  return m_accountStartNonce;
351 }
352 
353 u256 State::storage(Address const& _id, u256 const& _key) const
354 {
355  if (Account const* a = account(_id))
356  {
357  auto mit = a->storageOverlay().find(_key);
358  if (mit != a->storageOverlay().end())
359  return mit->second;
360 
361  // Not in the storage cache - go to the DB.
362  SecureTrieDB<h256, OverlayDB> memdb(const_cast<OverlayDB*>(&m_db), a->baseRoot()); // promise we won't change the overlay! :)
363  string payload = memdb.at(_key);
364  u256 ret = payload.size() ? RLP(payload).toInt<u256>() : 0;
365  a->setStorageCache(_key, ret);
366  return ret;
367  }
368  else
369  return 0;
370 }
371 
372 void State::setStorage(Address const& _contract, u256 const& _key, u256 const& _value)
373 {
374  m_changeLog.emplace_back(_contract, _key, storage(_contract, _key));
375  m_cache[_contract].setStorage(_key, _value);
376 }
377 
378 map<h256, pair<u256, u256>> State::storage(Address const& _id) const
379 {
380  map<h256, pair<u256, u256>> ret;
381 
382  if (Account const* a = account(_id))
383  {
384  // Pull out all values from trie storage.
385  if (h256 root = a->baseRoot())
386  {
387  SecureTrieDB<h256, OverlayDB> memdb(const_cast<OverlayDB*>(&m_db), root); // promise we won't alter the overlay! :)
388 
389  for (auto it = memdb.hashedBegin(); it != memdb.hashedEnd(); ++it)
390  {
391  h256 const hashedKey((*it).first);
392  u256 const key = h256(it.key());
393  u256 const value = RLP((*it).second).toInt<u256>();
394  ret[hashedKey] = make_pair(key, value);
395  }
396  }
397 
398  // Then merge cached storage over the top.
399  for (auto const& i : a->storageOverlay())
400  {
401  h256 const key = i.first;
402  h256 const hashedKey = sha3(key);
403  if (i.second)
404  ret[hashedKey] = i;
405  else
406  ret.erase(hashedKey);
407  }
408  }
409  return ret;
410 }
411 
412 h256 State::storageRoot(Address const& _id) const
413 {
414  string s = m_state.at(_id);
415  if (s.size())
416  {
417  RLP r(s);
418  return r[2].toHash<h256>();
419  }
420  return EmptyTrie;
421 }
422 
423 bytes const& State::code(Address const& _addr) const
424 {
425  Account const* a = account(_addr);
426  if (!a || a->codeHash() == EmptySHA3)
427  return NullBytes;
428 
429  if (a->code().empty())
430  {
431  // Load the code from the backend.
432  Account* mutableAccount = const_cast<Account*>(a);
433  mutableAccount->noteCode(m_db.lookup(a->codeHash()));
434  CodeSizeCache::instance().store(a->codeHash(), a->code().size());
435  }
436 
437  return a->code();
438 }
439 
440 void State::setNewCode(Address const& _address, bytes&& _code)
441 {
442  m_cache[_address].setNewCode(std::move(_code));
443  m_changeLog.emplace_back(Change::NewCode, _address);
444 }
445 
446 h256 State::codeHash(Address const& _a) const
447 {
448  if (Account const* a = account(_a))
449  return a->codeHash();
450  else
451  return EmptySHA3;
452 }
453 
454 size_t State::codeSize(Address const& _a) const
455 {
456  if (Account const* a = account(_a))
457  {
458  if (a->hasNewCode())
459  return a->code().size();
460  auto& codeSizeCache = CodeSizeCache::instance();
461  h256 codeHash = a->codeHash();
462  if (codeSizeCache.contains(codeHash))
463  return codeSizeCache.get(codeHash);
464  else
465  {
466  size_t size = code(_a).size();
467  codeSizeCache.store(codeHash, size);
468  return size;
469  }
470  }
471  else
472  return 0;
473 }
474 
475 size_t State::savepoint() const
476 {
477  return m_changeLog.size();
478 }
479 
480 void State::rollback(size_t _savepoint)
481 {
482  while (_savepoint != m_changeLog.size())
483  {
484  auto& change = m_changeLog.back();
485  auto& account = m_cache[change.address];
486 
487  // Public State API cannot be used here because it will add another
488  // change log entry.
489  switch (change.kind)
490  {
491  case Change::Storage:
492  account.setStorage(change.key, change.value);
493  break;
494  case Change::Balance:
495  account.addBalance(0 - change.value);
496  break;
497  case Change::Nonce:
498  account.setNonce(account.nonce() - 1);
499  break;
500  case Change::Create:
501  m_cache.erase(change.address);
502  break;
503  case Change::NewCode:
504  account.resetCode();
505  break;
506  case Change::Touch:
507  account.untouch();
508  m_unchangedCacheEntries.emplace_back(change.address);
509  break;
510  }
511  m_changeLog.pop_back();
512  }
513 }
514 
515 std::pair<ExecutionResult, TransactionReceipt> State::execute(EnvInfo const& _envInfo, SealEngineFace const& _sealEngine, Transaction const& _t, Permanence _p, OnOpFunc const& _onOp)
516 {
517  auto onOp = _onOp;
518 #if ETH_VMTRACE
519  if (isChannelVisible<VMTraceChannel>())
520  onOp = Executive::simpleTrace(); // override tracer
521 #endif
522 
523  // Create and initialize the executive. This will throw fairly cheaply and quickly if the
524  // transaction is bad in any way.
525  Executive e(*this, _envInfo, _sealEngine);
526  ExecutionResult res;
527  e.setResultRecipient(res);
528  e.initialize(_t);
529 
530  // OK - transaction looks valid - execute.
531  u256 startGasUsed = _envInfo.gasUsed();
532  if (!e.execute())
533  e.go(onOp);
534  e.finalize();
535 
536  if (_p == Permanence::Reverted)
537  m_cache.clear();
538  else
539  {
540  bool removeEmptyAccounts = _envInfo.number() >= _sealEngine.chainParams().u256Param("EIP158ForkBlock");
542  }
543 
544  return make_pair(res, TransactionReceipt(rootHash(), startGasUsed + e.gasUsed(), e.logs()));
545 }
546 
547 std::ostream& dev::eth::operator<<(std::ostream& _out, State const& _s)
548 {
549  _out << "--- " << _s.rootHash() << std::endl;
550  std::set<Address> d;
551  std::set<Address> dtr;
552  auto trie = SecureTrieDB<Address, OverlayDB>(const_cast<OverlayDB*>(&_s.m_db), _s.rootHash());
553  for (auto i: trie)
554  d.insert(i.first), dtr.insert(i.first);
555  for (auto i: _s.m_cache)
556  d.insert(i.first);
557 
558  for (auto i: d)
559  {
560  auto it = _s.m_cache.find(i);
561  Account* cache = it != _s.m_cache.end() ? &it->second : nullptr;
562  string rlpString = dtr.count(i) ? trie.at(i) : "";
563  RLP r(rlpString);
564  assert(cache || r);
565 
566  if (cache && !cache->isAlive())
567  _out << "XXX " << i << std::endl;
568  else
569  {
570  string lead = (cache ? r ? " * " : " + " : " ");
571  if (cache && r && cache->nonce() == r[0].toInt<u256>() && cache->balance() == r[1].toInt<u256>())
572  lead = " . ";
573 
574  stringstream contout;
575 
576  if ((cache && cache->codeHash() == EmptySHA3) || (!cache && r && (h256)r[3] != EmptySHA3))
577  {
578  std::map<u256, u256> mem;
579  std::set<u256> back;
580  std::set<u256> delta;
581  std::set<u256> cached;
582  if (r)
583  {
584  SecureTrieDB<h256, OverlayDB> memdb(const_cast<OverlayDB*>(&_s.m_db), r[2].toHash<h256>()); // promise we won't alter the overlay! :)
585  for (auto const& j: memdb)
586  mem[j.first] = RLP(j.second).toInt<u256>(), back.insert(j.first);
587  }
588  if (cache)
589  for (auto const& j: cache->storageOverlay())
590  {
591  if ((!mem.count(j.first) && j.second) || (mem.count(j.first) && mem.at(j.first) != j.second))
592  mem[j.first] = j.second, delta.insert(j.first);
593  else if (j.second)
594  cached.insert(j.first);
595  }
596  if (!delta.empty())
597  lead = (lead == " . ") ? "*.* " : "*** ";
598 
599  contout << " @:";
600  if (!delta.empty())
601  contout << "???";
602  else
603  contout << r[2].toHash<h256>();
604  if (cache && cache->hasNewCode())
605  contout << " $" << toHex(cache->code());
606  else
607  contout << " $" << (cache ? cache->codeHash() : r[3].toHash<h256>());
608 
609  for (auto const& j: mem)
610  if (j.second)
611  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 ;
612  else
613  contout << std::endl << "XXX " << std::hex << nouppercase << std::setw(64) << j.first << "";
614  }
615  else
616  contout << " [SIMPLE]";
617  _out << lead << i << ": " << std::dec << (cache ? cache->nonce() : r[0].toInt<u256>()) << " #:" << (cache ? cache->balance() : r[1].toInt<u256>()) << contout.str() << std::endl;
618  }
619  }
620  return _out;
621 }
bool addressInUse(Address const &_address) const
Check if the address is in use.
Definition: State.cpp:245
State(u256 const &_accountStartNonce)
Default constructor; creates with a blank database prepopulated with the genesis block.
Definition: State.h:176
u256 balance(Address const &_id) const
Get an account&#39;s balance.
Definition: State.cpp:266
h256 EmptySHA3
Definition: SHA3.cpp:35
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
Verification
Definition: TrieDB.h:43
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
Definition: CommonData.h:54
#define ctrace
Definition: Log.h:305
void commit(CommitBehaviour _commitBehaviour)
Commit all changes waiting in the address cache to the DB.
Definition: State.cpp:210
u256 nonce() const
Definition: Account.h:115
ChainOperationParams const & chainParams() const
Definition: SealEngine.h:75
std::unordered_map< Address, Account > AccountMap
Definition: Account.h:239
bool accountNonemptyAndExisting(Address const &_address) const
Check if the account exists in the state and is non empty (nonce > 0 || balance > 0 || code nonempty)...
Definition: State.cpp:250
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
Definition: Interface.h:284
void setNewCode(Address const &_address, bytes &&_code)
Sets the code of the account. Must only be called during / after contract creation.
Definition: State.cpp:440
Definition: util.h:95
u256 gasUsed() const
Definition: Executive.cpp:183
static OverlayDB openDB(std::string 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:74
Models the state of a single Ethereum account.
Definition: Account.h:67
void noteAccountStartNonce(u256 const &_actual)
Definition: State.cpp:128
std::ostream & operator<<(std::ostream &_out, BlockHeader const &_bi)
Definition: BlockHeader.h:194
void rollback(size_t _savepoint)
Revert all recent changes up to the given _savepoint savepoint.
Definition: State.cpp:480
Account starts as though it has not been changed.
Definition: Account.h:76
std::unordered_map< u256, u256 > const & storageOverlay() const
Definition: Account.h:130
h256 const EmptyTrie
Definition: OverlayDB.cpp:33
void noteCode(bytesConstRef _code)
Specify to the object what the actual code is for the account.
Definition: Account.h:153
std::hash for asio::adress
Definition: Common.h:323
assert(len-trim+(2 *lenIndices)<=WIDTH)
Different view on a GenericTrieDB that can use different key types.
Definition: TrieDB.h:320
void setNonce(u256 const &_nonce)
Set nonce to a new value.
Definition: Account.h:122
u256 const & gasUsed() const
Definition: ExtVMFace.h:243
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
Definition: CommonData.cpp:141
vector_ref< _T > cropped(size_t _begin, size_t _count) const
Definition: vector_ref.h:62
AddressHash m_touched
Tracks all addresses touched so far.
Definition: State.h:334
Description of the result of executing a transaction.
Definition: Transaction.h:69
u256 const & number() const
Definition: ExtVMFace.h:237
h256 storageRoot(Address const &_contract) const
Get the root of the storage of an account.
Definition: State.cpp:412
Model of an Ethereum state, essentially a facade for the trie.
Definition: State.h:161
if(a.IndicesBefore(b, len, lenIndices))
Definition: equihash.cpp:243
OverlayDB const & db() const
Definition: State.h:195
bool go(OnOpFunc const &_onOp=OnOpFunc())
Executes (or continues execution of) the VM.
Definition: Executive.cpp:386
void populateFrom(AccountMap const &_map)
Populate the state from the given AccountMap. Just uses dev::eth::commit().
Definition: State.cpp:115
u256 const & requireAccountStartNonce() const
Definition: State.cpp:121
std::string lookup(h256 const &_h) const
Definition: OverlayDB.cpp:120
void removeEmptyAccounts()
Turns all "touched" empty accounts into non-alive accounts.
Definition: State.cpp:136
void createAccount(Address const &_address, Account const &&_account)
Definition: State.cpp:330
WithExisting
Definition: Common.h:310
#define a(i)
_N toHash(int _flags=Strict) const
Definition: RLP.h:298
boost::random_device s_fixedHashEngine
Definition: FixedHash.cpp:29
virtual void kill(Address _a)
Delete an account (used for processing suicides).
Definition: State.cpp:338
std::pair< ExecutionResult, TransactionReceipt > execute(EnvInfo const &_envInfo, SealEngineFace const &_sealEngine, Transaction const &_t, Permanence _p=Permanence::Committed, OnOpFunc const &_onOp=OnOpFunc())
Execute a given transaction.
Definition: State.cpp:515
bytesRef ref()
Definition: FixedHash.h:133
void store(h256 const &_hash, size_t size)
Definition: CodeSizeCache.h:39
h256 codeHash() const
Definition: Account.h:141
void createContract(Address const &_address)
Create a contract at the given address (with unset code and unchanged balance).
Definition: State.cpp:325
bytes const & code() const
Definition: Account.h:156
BaseState
Definition: State.h:76
const char * name
Definition: rest.cpp:36
void finalize()
Finalise a transaction previously set up with initialize().
Definition: Executive.cpp:466
#define EthWhite
Definition: Terminal.h:119
void insert(KeyType _k, bytesConstRef _value)
Definition: TrieDB.h:333
void untouch()
Definition: Account.h:102
std::vector< detail::Change > m_changeLog
Definition: State.h:339
std::vector< byte > bytes
Definition: Common.h:75
const u256 Invalid256
Definition: Common.cpp:38
u256 storage(Address const &_contract, u256 const &_memory) const
Get the value of a storage position of an account.
Definition: State.cpp:353
u256 getNonce(Address const &_addr) const
Get the account nonce – the number of transactions it has sent.
Definition: State.cpp:345
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VM *, ExtVMFace const *)> OnOpFunc
Definition: ExtVMFace.h:193
#define EthGray
Definition: Terminal.h:118
std::unordered_map< Address, u256 > addresses() const
Definition: State.cpp:220
bytes const & code(Address const &_addr) const
Get the code of an account.
Definition: State.cpp:423
FixedHash< 32 > h256
Definition: FixedHash.h:340
#define cwarn
Definition: Log.h:304
Message-call/contract-creation executor; useful for executing transactions.
Definition: Executive.h:106
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
std::unordered_map< Address, Account > m_cache
Our address cache. This stores the states of each address that has (or at least might have) been chan...
Definition: State.h:331
LogEntries const & logs() const
Definition: Executive.h:148
std::string m_dbPath
Definition: Defaults.h:44
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:84
u256 const & balance() const
Definition: Account.h:109
void setRoot(h256 const &_root)
Resets any uncommitted changes to the cache.
Definition: State.cpp:236
h256 rootHash() const
The hash of the root of our state tree.
Definition: State.h:288
void initialize(bytesConstRef _transaction)
Initializes the executive for evaluating a transaction. You must call finalize() at some point follow...
Definition: Executive.h:135
SecureTrieDB< Address, OverlayDB > m_state
Our state tree, as an OverlayDB DB.
Definition: State.h:330
#define EthViolet
Definition: Terminal.h:128
std::vector< Address > m_unchangedCacheEntries
Tracks entries in m_cache that can potentially be purged if it grows too large.
Definition: State.h:332
u256 u256Param(std::string const &_name) const
Convenience method to get an otherParam as a u256 int.
State & operator=(State const &_s)
Copy state object.
Definition: State.cpp:143
void setStorage(Address const &_contract, u256 const &_location, u256 const &_value)
Set the value of a storage position of an account.
Definition: State.cpp:372
u256 m_accountStartNonce
Definition: State.h:336
#define cnote
Definition: Log.h:303
void addBalance(u256 _value)
Increments the balance of this account by the given amount.
Definition: Account.h:112
uint8_t const size_t const size
Definition: sha3.h:20
void setStorage(u256 _p, u256 _v)
Set a key/value pair in the account&#39;s storage.
Definition: Account.h:134
bool execute()
Begins execution of a transaction.
Definition: Executive.cpp:247
bool isAlive() const
Definition: Account.h:97
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
virtual void addBalance(Address const &_id, u256 const &_amount)
Add some amount to balance.
Definition: State.cpp:286
OverlayDB m_db
Our overlay for the state tree.
Definition: State.h:329
static CodeSizeCache & instance()
Definition: CodeSizeCache.h:57
std::string at(KeyType _k) const
Definition: TrieDB.h:332
#define e(i)
Definition: sha.cpp:733
bool hasNewCode() const
Definition: Account.h:143
const unsigned c_databaseVersion
Current database version.
Definition: Common.cpp:54
void clearCacheIfTooLarge() const
Purges non-modified entries in m_cache if it grows too large.
Definition: State.cpp:192
#define d(i)
Definition: sha.cpp:732
Permanence
Definition: State.h:82
AddressHash commit(AccountMap const &_cache, SecureTrieDB< Address, DB > &_state)
Definition: State.h:345
#define EthBlue
Definition: Terminal.h:127
Account const * account(Address const &_addr) const
Definition: State.cpp:158
size_t codeSize(Address const &_contract) const
Get the byte-size of the code of an account.
Definition: State.cpp:454
static Defaults * get()
Definition: Defaults.h:39
static OnOpFunc simpleTrace()
Operation function for providing a simple trace of the VM execution.
Definition: Executive.cpp:366
size_t savepoint() const
Create a savepoint in the state changelog.
Definition: State.cpp:475
_T toInt(int _flags=Strict) const
Converts to int of type given; if isString(), decodes as big-endian bytestream.
Definition: RLP.h:275
std::set< Address > m_nonExistingAccountsCache
Tracks addresses that are known to not exist.
Definition: State.h:333
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
void subBalance(Address const &_addr, u256 const &_value)
Subtract the _value amount from the balance of _addr account.
Definition: State.cpp:311
#define DEV_IGNORE_EXCEPTIONS(X)
Definition: Common.h:63
bool addressHasCode(Address const &_address) const
Check if the address contains executable code.
Definition: State.cpp:258
h64 Nonce
Definition: Common.h:70
void setResultRecipient(ExecutionResult &_res)
Collect execution results in the result storage provided.
Definition: Executive.h:184
void resetCode()
Reset the code set by previous CREATE message.
Definition: Account.h:149
void incNonce(Address const &_id)
Increament the account nonce.
Definition: State.cpp:274
h256 codeHash(Address const &_contract) const
Get the code hash of an account.
Definition: State.cpp:446