Fabcoin Core  0.16.2
P2P Digital Currency
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/RLP.h>
26 #include <libdevcore/TrieDB.h>
27 #include <libdevcore/SHA3.h>
28 #include <libethcore/Common.h>
29 
30 namespace dev
31 {
32 namespace eth
33 {
34 
67 class Account
68 {
69 public:
72  {
77  };
78 
80  Account() {}
81 
85  Account(u256 _nonce, u256 _balance, Changedness _c = Changed): m_isAlive(true), m_isUnchanged(_c == Unchanged), m_nonce(_nonce), m_balance(_balance) {}
86 
88  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); }
89 
90 
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 
124 
128 
130  std::unordered_map<u256, u256> const& storageOverlay() const { return m_storageOverlay; }
131 
134  void setStorage(u256 _p, u256 _v) { m_storageOverlay[_p] = _v; changed(); }
135 
138  void setStorageCache(u256 _p, u256 _v) const { const_cast<decltype(m_storageOverlay)&>(m_storageOverlay)[_p] = _v; }
139 
141  h256 codeHash() const { return m_codeHash; }
142 
143  bool hasNewCode() const { return m_hasNewCode; }
144 
146  void setNewCode(bytes&& _code);
147 
149  void resetCode() { m_codeCache.clear(); m_hasNewCode = false; m_codeHash = EmptySHA3; }
150 
153  void noteCode(bytesConstRef _code) { assert(sha3(_code) == m_codeHash); m_codeCache = _code.toBytes(); }
154 
156  bytes const& code() const { return m_codeCache; }
157 
158 private:
160  void changed() { m_isUnchanged = false; }
161 
163  bool m_isAlive = false;
164 
166  bool m_isUnchanged = false;
167 
169  bool m_hasNewCode = false;
170 
173 
176 
180 
188 
190  std::unordered_map<u256, u256> m_storageOverlay;
191 
195 
198 };
199 
201 {
202 public:
203  AccountMask(bool _all = false):
204  m_hasBalance(_all),
205  m_hasNonce(_all),
206  m_hasCode(_all),
207  m_hasStorage(_all)
208  {}
209 
211  bool _hasBalance,
212  bool _hasNonce,
213  bool _hasCode,
214  bool _hasStorage,
215  bool _shouldNotExist = false
216  ):
217  m_hasBalance(_hasBalance),
218  m_hasNonce(_hasNonce),
219  m_hasCode(_hasCode),
220  m_hasStorage(_hasStorage),
221  m_shouldNotExist(_shouldNotExist)
222  {}
223 
224  bool allSet() const { return m_hasBalance && m_hasNonce && m_hasCode && m_hasStorage; }
225  bool hasBalance() const { return m_hasBalance; }
226  bool hasNonce() const { return m_hasNonce; }
227  bool hasCode() const { return m_hasCode; }
228  bool hasStorage() const { return m_hasStorage; }
229  bool shouldExist() const { return !m_shouldNotExist; }
230 
231 private:
234  bool m_hasCode;
236  bool m_shouldNotExist = false;
237 };
238 
239 using AccountMap = std::unordered_map<Address, Account>;
240 using AccountMaskMap = std::unordered_map<Address, AccountMask>;
241 
242 class PrecompiledContract;
243 using PrecompiledContractMap = std::unordered_map<Address, PrecompiledContract>;
244 
245 AccountMap jsonToAccountMap(std::string const& _json, u256 const& _defaultNonce = 0, AccountMaskMap* o_mask = nullptr, PrecompiledContractMap* o_precompiled = nullptr);
246 
247 }
248 }
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
std::unordered_map< Address, PrecompiledContract > PrecompiledContractMap
Definition: Account.h:243
std::unordered_map< u256, u256 > m_storageOverlay
The map with is overlaid onto whatever storage is implied by the m_storageRoot in the trie...
Definition: Account.h:190
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:88
u256 nonce() const
Definition: Account.h:115
Account starts as though it has been changed.
Definition: Account.h:74
bytes m_codeCache
The associated code for this account.
Definition: Account.h:194
std::unordered_map< Address, Account > AccountMap
Definition: Account.h:239
u256 m_balance
Account&#39;s balance.
Definition: Account.h:175
Models the state of a single Ethereum account.
Definition: Account.h:67
bool hasBalance() const
Definition: Account.h:225
Account()
Construct a dead Account.
Definition: Account.h:80
Account starts as though it has not been changed.
Definition: Account.h:76
bool isDirty() const
Definition: Account.h:100
std::unordered_map< u256, u256 > const & storageOverlay() const
Definition: Account.h:130
void changed()
Note that we&#39;ve altered the account.
Definition: Account.h:160
h256 const EmptyTrie
Definition: OverlayDB.cpp:33
bool allSet() const
Definition: Account.h:224
void noteCode(bytesConstRef _code)
Specify to the object what the actual code is for the account.
Definition: Account.h:153
bool m_hasNewCode
True if new code was deployed to the account.
Definition: Account.h:169
void setStorageCache(u256 _p, u256 _v) const
Set a key/value pair in the account&#39;s storage to a value that is already present inside the database...
Definition: Account.h:138
assert(len-trim+(2 *lenIndices)<=WIDTH)
void setNonce(u256 const &_nonce)
Set nonce to a new value.
Definition: Account.h:122
AccountMask(bool _all=false)
Definition: Account.h:203
h256 baseRoot() const
Definition: Account.h:127
h256 m_storageRoot
The base storage root.
Definition: Account.h:179
AccountMask(bool _hasBalance, bool _hasNonce, bool _hasCode, bool _hasStorage, bool _shouldNotExist=false)
Definition: Account.h:210
bool hasCode() const
Definition: Account.h:227
static const h256 c_contractConceptionCodeHash
Value for m_codeHash when this account is having its code determined.
Definition: Account.h:197
bool m_isUnchanged
True if we&#39;ve not made any alteration to the account having been given it&#39;s properties directly...
Definition: Account.h:166
h256 codeHash() const
Definition: Account.h:141
bytes const & code() const
Definition: Account.h:156
void setNewCode(bytes &&_code)
Sets the code of the account. Used by "create" messages.
Definition: Account.cpp:31
void untouch()
Definition: Account.h:102
std::vector< byte > bytes
Definition: Common.h:75
std::vector< unsigned char > toBytes() const
Definition: vector_ref.h:45
bool isEmpty() const
Definition: Account.h:106
h256 m_codeHash
If c_contractConceptionCodeHash then we&#39;re in the limbo where we&#39;re running the initialisation code...
Definition: Account.h:187
Account(u256 _nonce, u256 _balance, Changedness _c=Changed)
Construct an alive Account, with given endowment, for either a normal (non-contract) account or for a...
Definition: Account.h:85
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, AccountMask > AccountMaskMap
Definition: Account.h:240
u256 const & balance() const
Definition: Account.h:109
Changedness
Changedness of account to create.
Definition: Account.h:71
void kill()
Kill this account. Useful for the suicide opcode. Following this call, isAlive() returns false...
Definition: Account.h:92
void addBalance(u256 _value)
Increments the balance of this account by the given amount.
Definition: Account.h:112
bool m_isAlive
Is this account existant? If not, it represents a deleted account.
Definition: Account.h:163
void setStorage(u256 _p, u256 _v)
Set a key/value pair in the account&#39;s storage.
Definition: Account.h:134
AccountMap jsonToAccountMap(std::string const &_json, u256 const &_defaultNonce=0, AccountMaskMap *o_mask=nullptr, PrecompiledContractMap *o_precompiled=nullptr)
Definition: Account.cpp:50
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
bool hasStorage() const
Definition: Account.h:228
bool hasNewCode() const
Definition: Account.h:143
u256 m_nonce
Account&#39;s nonce.
Definition: Account.h:172
bool shouldExist() const
Definition: Account.h:229
bool hasNonce() const
Definition: Account.h:226
void resetCode()
Reset the code set by previous CREATE message.
Definition: Account.h:149
void incNonce()
Increment the nonce of the account by one.
Definition: Account.h:118