Ethereum  PoC-8
The C++ Implementation of Ethereum
Account.h
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 #pragma once
23 
24 #include <libdevcore/Common.h>
25 #include <libdevcore/SHA3.h>
26 #include <libdevcore/TrieCommon.h>
27 #include <libethcore/Common.h>
28 
29 #include <boost/filesystem/path.hpp>
30 
31 namespace dev
32 {
33 class OverlayDB;
34 
35 namespace eth
36 {
37 
56 class Account
57 {
58 public:
61  {
65  Unchanged
66  };
67 
69  Account() {}
70 
74  Account(u256 _nonce, u256 _balance, Changedness _c = Changed): m_isAlive(true), m_isUnchanged(_c == Unchanged), m_nonce(_nonce), m_balance(_balance) {}
75 
77  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); }
78 
79 
82  void kill()
83  {
84  m_isAlive = false;
85  m_storageOverlay.clear();
86  m_storageOriginal.clear();
87  m_codeHash = EmptySHA3;
88  m_storageRoot = EmptyTrie;
89  m_balance = 0;
90  m_nonce = 0;
91  changed();
92  }
93 
97  bool isAlive() const { return m_isAlive; }
98 
100  bool isDirty() const { return !m_isUnchanged; }
101 
102  void untouch() { m_isUnchanged = true; }
103 
106  bool isEmpty() const { return nonce() == 0 && balance() == 0 && codeHash() == EmptySHA3; }
107 
109  u256 const& balance() const { return m_balance; }
110 
112  void addBalance(u256 _value) { m_balance += _value; changed(); }
113 
115  u256 nonce() const { return m_nonce; }
116 
118  void incNonce() { ++m_nonce; changed(); }
119 
122  void setNonce(u256 const& _nonce) { m_nonce = _nonce; changed(); }
123 
126  h256 baseRoot() const { assert(m_storageRoot); return m_storageRoot; }
127 
130  u256 storageValue(u256 const& _key, OverlayDB const& _db) const
131  {
132  auto mit = m_storageOverlay.find(_key);
133  if (mit != m_storageOverlay.end())
134  return mit->second;
135 
136  return originalStorageValue(_key, _db);
137  }
138 
141  u256 originalStorageValue(u256 const& _key, OverlayDB const& _db) const;
142 
144  std::unordered_map<u256, u256> const& storageOverlay() const { return m_storageOverlay; }
145 
148  void setStorage(u256 _p, u256 _v) { m_storageOverlay[_p] = _v; changed(); }
149 
152  {
153  m_storageOverlay.clear();
154  m_storageOriginal.clear();
155  m_storageRoot = EmptyTrie;
156  changed();
157  }
158 
160  void setStorageRoot(h256 const& _root)
161  {
162  m_storageOverlay.clear();
163  m_storageOriginal.clear();
164  m_storageRoot = _root;
165  changed();
166  }
167 
169  h256 codeHash() const { return m_codeHash; }
170 
171  bool hasNewCode() const { return m_hasNewCode; }
172 
174  void setCode(bytes&& _code);
175 
178  void noteCode(bytesConstRef _code) { assert(sha3(_code) == m_codeHash); m_codeCache = _code.toBytes(); }
179 
181  bytes const& code() const { return m_codeCache; }
182 
183 private:
185  void changed() { m_isUnchanged = false; }
186 
188  bool m_isAlive = false;
189 
191  bool m_isUnchanged = false;
192 
194  bool m_hasNewCode = false;
195 
197  u256 m_nonce;
198 
200  u256 m_balance = 0;
201 
204  h256 m_storageRoot = EmptyTrie;
205 
212  h256 m_codeHash = EmptySHA3;
213 
215  mutable std::unordered_map<u256, u256> m_storageOverlay;
216 
218  mutable std::unordered_map<u256, u256> m_storageOriginal;
219 
222  bytes m_codeCache;
223 
225  static const h256 c_contractConceptionCodeHash;
226 };
227 
229 {
230 public:
231  AccountMask(bool _all = false):
232  m_hasBalance(_all),
233  m_hasNonce(_all),
234  m_hasCode(_all),
235  m_hasStorage(_all)
236  {}
237 
239  bool _hasBalance,
240  bool _hasNonce,
241  bool _hasCode,
242  bool _hasStorage,
243  bool _shouldNotExist = false
244  ):
245  m_hasBalance(_hasBalance),
246  m_hasNonce(_hasNonce),
247  m_hasCode(_hasCode),
248  m_hasStorage(_hasStorage),
249  m_shouldNotExist(_shouldNotExist)
250  {}
251 
252  bool allSet() const { return m_hasBalance && m_hasNonce && m_hasCode && m_hasStorage; }
253  bool hasBalance() const { return m_hasBalance; }
254  bool hasNonce() const { return m_hasNonce; }
255  bool hasCode() const { return m_hasCode; }
256  bool hasStorage() const { return m_hasStorage; }
257  bool shouldExist() const { return !m_shouldNotExist; }
258 
259 private:
260  bool m_hasBalance;
261  bool m_hasNonce;
262  bool m_hasCode;
263  bool m_hasStorage;
264  bool m_shouldNotExist = false;
265 };
266 
267 using AccountMap = std::unordered_map<Address, Account>;
268 using AccountMaskMap = std::unordered_map<Address, AccountMask>;
269 
270 class PrecompiledContract;
271 using PrecompiledContractMap = std::unordered_map<Address, PrecompiledContract>;
272 
273 AccountMap jsonToAccountMap(std::string const& _json, u256 const& _defaultNonce = 0,
274  AccountMaskMap* o_mask = nullptr, PrecompiledContractMap* o_precompiled = nullptr,
275  const boost::filesystem::path& _configPath = {});
276 }
277 }
dev::eth::Account::Changedness
Changedness
Changedness of account to create.
Definition: Account.h:61
dev::eth::AccountMask
Definition: Account.h:229
dev::EmptyTrie
h256 const EmptyTrie
Definition: TrieCommon.cpp:28
dev::eth::Account::kill
void kill()
Definition: Account.h:82
dev::eth::AccountMask::hasBalance
bool hasBalance() const
Definition: Account.h:253
dev::eth::AccountMask::allSet
bool allSet() const
Definition: Account.h:252
dev::eth::Account::untouch
void untouch()
Definition: Account.h:102
dev::eth::Account::clearStorage
void clearStorage()
Empty the storage. Used when a contract is overwritten.
Definition: Account.h:151
dev::eth::Account::Account
Account(u256 _nonce, u256 _balance, Changedness _c=Changed)
Definition: Account.h:74
dev::eth::Account::setStorageRoot
void setStorageRoot(h256 const &_root)
Set the storage root. Used when clearStorage() is reverted.
Definition: Account.h:160
dev::vector_ref< byte const >
dev::eth::PrecompiledContract
Definition: ChainOperationParams.h:36
dev::sha3
bool sha3(bytesConstRef _input, bytesRef o_output) noexcept
Definition: SHA3.cpp:28
dev::eth::AccountMask::hasCode
bool hasCode() const
Definition: Account.h:255
dev::eth::Account::addBalance
void addBalance(u256 _value)
Increments the balance of this account by the given amount.
Definition: Account.h:112
dev::EmptySHA3
h256 const EmptySHA3
Definition: SHA3.cpp:25
dev::eth::AccountMap
std::unordered_map< Address, Account > AccountMap
Definition: Account.h:267
TrieCommon.h
dev::eth::AccountMaskMap
std::unordered_map< Address, AccountMask > AccountMaskMap
Definition: Account.h:268
dev::FixedHash< 32 >
dev::eth::Account::isEmpty
bool isEmpty() const
Definition: Account.h:106
dev::eth::Account::isAlive
bool isAlive() const
Definition: Account.h:97
dev::eth::Account::setStorage
void setStorage(u256 _p, u256 _v)
Definition: Account.h:148
dev::eth::AccountMask::hasStorage
bool hasStorage() const
Definition: Account.h:256
dev::eth::Account::balance
u256 const & balance() const
Definition: Account.h:109
dev::eth::jsonToAccountMap
AccountMap jsonToAccountMap(std::string const &_json, u256 const &_defaultNonce=0, AccountMaskMap *o_mask=nullptr, PrecompiledContractMap *o_precompiled=nullptr, const boost::filesystem::path &_configPath={})
Common.h
dev::eth::AccountMask::hasNonce
bool hasNonce() const
Definition: Account.h:254
Common.h
dev::eth::Account::originalStorageValue
u256 originalStorageValue(u256 const &_key, OverlayDB const &_db) const
Definition: Account.cpp:44
dev::eth::Account::Unchanged
@ Unchanged
Account starts as though it has not been changed.
Definition: Account.h:65
dev::eth::AccountMask::AccountMask
AccountMask(bool _hasBalance, bool _hasNonce, bool _hasCode, bool _hasStorage, bool _shouldNotExist=false)
Definition: Account.h:238
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::eth::Account::storageValue
u256 storageValue(u256 const &_key, OverlayDB const &_db) const
Definition: Account.h:130
SHA3.h
dev::eth::Account::noteCode
void noteCode(bytesConstRef _code)
Definition: Account.h:178
dev::eth::Account::Account
Account(u256 _nonce, u256 _balance, h256 _contractRoot, h256 _codeHash, Changedness _c)
Explicit constructor for wierd cases of construction or a contract account.
Definition: Account.h:77
dev::OverlayDB
Definition: OverlayDB.h:34
dev::eth::Account::incNonce
void incNonce()
Increment the nonce of the account by one.
Definition: Account.h:118
dev::eth::Account::hasNewCode
bool hasNewCode() const
Definition: Account.h:171
dev::eth::Account::Account
Account()
Construct a dead Account.
Definition: Account.h:69
dev::eth::Account::code
bytes const & code() const
Definition: Account.h:181
dev::eth::Account::isDirty
bool isDirty() const
Definition: Account.h:100
dev::vector_ref::toBytes
std::vector< unsigned char > toBytes() const
Definition: vector_ref.h:43
dev::eth::Account::baseRoot
h256 baseRoot() const
Definition: Account.h:126
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::Account::Changed
@ Changed
Account starts as though it has been changed.
Definition: Account.h:63
dev::eth::Account::setNonce
void setNonce(u256 const &_nonce)
Definition: Account.h:122
dev
Definition: Address.cpp:21
dev::eth::Account::codeHash
h256 codeHash() const
Definition: Account.h:169
dev::eth::AccountMask::AccountMask
AccountMask(bool _all=false)
Definition: Account.h:231
dev::eth::Account::storageOverlay
std::unordered_map< u256, u256 > const & storageOverlay() const
Definition: Account.h:144
dev::eth::Account::nonce
u256 nonce() const
Definition: Account.h:115
dev::eth::Account
Definition: Account.h:57
dev::eth::PrecompiledContractMap
std::unordered_map< Address, PrecompiledContract > PrecompiledContractMap
Definition: Account.h:271
dev::eth::AccountMask::shouldExist
bool shouldExist() const
Definition: Account.h:257
dev::eth::Account::setCode
void setCode(bytes &&_code)
Sets the code of the account. Used by "create" messages.
Definition: Account.cpp:37