Ethereum  PoC-8
The C++ Implementation of Ethereum
dev::eth::State Class Reference

#include <State.h>

Public Types

enum  CommitBehaviour { CommitBehaviour::KeepEmptyAccounts, CommitBehaviour::RemoveEmptyAccounts }
 
enum  NullType { Null }
 
using AddressMap = std::map< h256, Address >
 

Public Member Functions

 State (u256 const &_accountStartNonce)
 Default constructor; creates with a blank database prepopulated with the genesis block. More...
 
 State (u256 const &_accountStartNonce, OverlayDB const &_db, BaseState _bs=BaseState::PreExisting)
 
 State (NullType)
 
 State (State const &_s)
 Copy state object. More...
 
Stateoperator= (State const &_s)
 Copy state object. More...
 
OverlayDB const & db () const
 
OverlayDBdb ()
 
void populateFrom (AccountMap const &_map)
 Populate the state from the given AccountMap. Just uses dev::eth::commit(). More...
 
std::unordered_map< Address, u256addresses () const
 
std::pair< AddressMap, h256addresses (h256 const &_begin, size_t _maxResults) const
 
std::pair< ExecutionResult, TransactionReceiptexecute (EnvInfo const &_envInfo, SealEngineFace const &_sealEngine, Transaction const &_t, Permanence _p=Permanence::Committed, OnOpFunc const &_onOp=OnOpFunc())
 
void executeBlockTransactions (Block const &_block, unsigned _txCount, LastBlockHashesFace const &_lastHashes, SealEngineFace const &_sealEngine)
 
bool addressInUse (Address const &_address) const
 Check if the address is in use. More...
 
bool accountNonemptyAndExisting (Address const &_address) const
 
bool addressHasCode (Address const &_address) const
 Check if the address contains executable code. More...
 
u256 balance (Address const &_id) const
 
void addBalance (Address const &_id, u256 const &_amount)
 
void subBalance (Address const &_addr, u256 const &_value)
 
void setBalance (Address const &_addr, u256 const &_value)
 
void transferBalance (Address const &_from, Address const &_to, u256 const &_value)
 Transfers "the balance _value between two accounts. More...
 
h256 storageRoot (Address const &_contract) const
 Get the root of the storage of an account. More...
 
u256 storage (Address const &_contract, u256 const &_memory) const
 
void setStorage (Address const &_contract, u256 const &_location, u256 const &_value)
 Set the value of a storage position of an account. More...
 
u256 originalStorageValue (Address const &_contract, u256 const &_key) const
 
void clearStorage (Address const &_contract)
 Clear the storage root hash of an account to the hash of the empty trie. More...
 
void createContract (Address const &_address)
 Create a contract at the given address (with unset code and unchanged balance). More...
 
void setCode (Address const &_address, bytes &&_code)
 Sets the code of the account. Must only be called during / after contract creation. More...
 
void kill (Address _a)
 Delete an account (used for processing suicides). More...
 
std::map< h256, std::pair< u256, u256 > > storage (Address const &_contract) const
 
bytes const & code (Address const &_addr) const
 
h256 codeHash (Address const &_contract) const
 
size_t codeSize (Address const &_contract) const
 
void incNonce (Address const &_id)
 Increament the account nonce. More...
 
void setNonce (Address const &_addr, u256 const &_newNonce)
 Set the account nonce. More...
 
u256 getNonce (Address const &_addr) const
 
h256 rootHash () const
 The hash of the root of our state tree. More...
 
void commit (CommitBehaviour _commitBehaviour)
 
void setRoot (h256 const &_root)
 Resets any uncommitted changes to the cache. More...
 
u256 const & accountStartNonce () const
 Get the account start nonce. May be required. More...
 
u256 const & requireAccountStartNonce () const
 
void noteAccountStartNonce (u256 const &_actual)
 
size_t savepoint () const
 
void rollback (size_t _savepoint)
 Revert all recent changes up to the given _savepoint savepoint. More...
 
ChangeLog const & changeLog () const
 

Static Public Member Functions

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. More...
 

Friends

class ExtVM
 
class dev::test::ImportTest
 
class dev::test::StateLoader
 
class BlockChain
 
std::ostream & operator<< (std::ostream &_out, State const &_s)
 

Detailed Description

Model of an Ethereum state, essentially a facade for the trie.

Allows you to query the state of accounts as well as creating and modifying accounts. It has built-in caching for various aspects of the state.

State Changelog

Any atomic change to any account is registered and appended in the changelog. In case some changes must be reverted, the changes are popped from the changelog and undone. For possible atomic changes list

See also
Change::Kind. The changelog is managed by savepoint(), rollback() and commit() methods.

Definition at line 159 of file State.h.

Member Typedef Documentation

◆ AddressMap

Definition at line 173 of file State.h.

Member Enumeration Documentation

◆ CommitBehaviour

Enumerator
KeepEmptyAccounts 
RemoveEmptyAccounts 

Definition at line 167 of file State.h.

◆ NullType

Enumerator
Null 

Definition at line 184 of file State.h.

Constructor & Destructor Documentation

◆ State() [1/4]

dev::eth::State::State ( u256 const &  _accountStartNonce)
inlineexplicit

Default constructor; creates with a blank database prepopulated with the genesis block.

Definition at line 176 of file State.h.

◆ State() [2/4]

State::State ( u256 const &  _accountStartNonce,
OverlayDB const &  _db,
BaseState  _bs = BaseState::PreExisting 
)
explicit

Basic state object from database. Use the default when you already have a database and you just want to make a State object which uses it. If you have no preexisting database then set BaseState to something other than BaseState::PreExisting in order to prepopulate the Trie.

Definition at line 40 of file State.cpp.

◆ State() [3/4]

dev::eth::State::State ( NullType  )
inline

Definition at line 185 of file State.h.

◆ State() [4/4]

State::State ( State const &  _s)

Copy state object.

Definition at line 50 of file State.cpp.

Member Function Documentation

◆ accountNonemptyAndExisting()

bool State::accountNonemptyAndExisting ( Address const &  _address) const

Check if the account exists in the state and is non empty (nonce > 0 || balance > 0 || code nonempty). These two notions are equivalent after EIP158.

Definition at line 296 of file State.cpp.

◆ accountStartNonce()

u256 const& dev::eth::State::accountStartNonce ( ) const
inline

Get the account start nonce. May be required.

Definition at line 320 of file State.h.

◆ addBalance()

void State::addBalance ( Address const &  _id,
u256 const &  _amount 
)

Add some amount to balance. Will initialise the address if it has never been used.

Definition at line 346 of file State.cpp.

◆ addresses() [1/2]

unordered_map< Address, u256 > State::addresses ( ) const
Returns
the set containing all addresses currently in use in Ethereum.
Warning
This is slowslowslow. Don't use it unless you want to lock the object for seconds or minutes at a time.
Exceptions
InterfaceNotSupportedif compiled without ETH_FATDB.

Definition at line 210 of file State.cpp.

◆ addresses() [2/2]

std::pair< State::AddressMap, h256 > State::addresses ( h256 const &  _begin,
size_t  _maxResults 
) const
Returns
the map with maximum _maxResults elements containing hash->addresses and the next address hash. This method faster then addresses() const;

Definition at line 226 of file State.cpp.

◆ addressHasCode()

bool State::addressHasCode ( Address const &  _address) const

Check if the address contains executable code.

Definition at line 304 of file State.cpp.

◆ addressInUse()

bool State::addressInUse ( Address const &  _address) const

Check if the address is in use.

Definition at line 291 of file State.cpp.

◆ balance()

u256 State::balance ( Address const &  _id) const

Get an account's balance.

Returns
0 if the address has never been used.

Definition at line 312 of file State.cpp.

◆ changeLog()

ChangeLog const& dev::eth::State::changeLog ( ) const
inline

Definition at line 331 of file State.h.

◆ clearStorage()

void State::clearStorage ( Address const &  _contract)

Clear the storage root hash of an account to the hash of the empty trie.

Definition at line 444 of file State.cpp.

◆ code()

bytes const & State::code ( Address const &  _addr) const

Get the code of an account.

Returns
bytes() if no account exists at that address.
Warning
The reference to the code is only valid until the access to other account. Do not keep it.

Definition at line 503 of file State.cpp.

◆ codeHash()

h256 State::codeHash ( Address const &  _contract) const

Get the code hash of an account.

Returns
EmptySHA3 if no account exists at that address or if there is no code associated with the address.

Definition at line 526 of file State.cpp.

◆ codeSize()

size_t State::codeSize ( Address const &  _contract) const

Get the byte-size of the code of an account.

Returns
code(_contract).size(), but utilizes CodeSizeHash.

Definition at line 534 of file State.cpp.

◆ commit()

void State::commit ( CommitBehaviour  _commitBehaviour)

Commit all changes waiting in the address cache to the DB.

Parameters
_commitBehaviourwhether or not to remove empty accounts during commit.

Definition at line 200 of file State.cpp.

◆ createContract()

void State::createContract ( Address const &  _address)

Create a contract at the given address (with unset code and unchanged balance).

Definition at line 394 of file State.cpp.

◆ db() [1/2]

OverlayDB& dev::eth::State::db ( )
inline

Definition at line 196 of file State.h.

◆ db() [2/2]

OverlayDB const& dev::eth::State::db ( ) const
inline

Definition at line 195 of file State.h.

◆ execute()

std::pair< ExecutionResult, TransactionReceipt > State::execute ( EnvInfo const &  _envInfo,
SealEngineFace const &  _sealEngine,
Transaction const &  _t,
Permanence  _p = Permanence::Committed,
OnOpFunc const &  _onOp = OnOpFunc() 
)

Execute a given transaction. This will change the state accordingly.

Definition at line 598 of file State.cpp.

◆ executeBlockTransactions()

void State::executeBlockTransactions ( Block const &  _block,
unsigned  _txCount,
LastBlockHashesFace const &  _lastHashes,
SealEngineFace const &  _sealEngine 
)

Execute _txCount transactions of a given block. This will change the state accordingly.

Definition at line 634 of file State.cpp.

◆ getNonce()

u256 State::getNonce ( Address const &  _addr) const

Get the account nonce – the number of transactions it has sent.

Returns
0 if the address has never been used.

Definition at line 414 of file State.cpp.

◆ incNonce()

void State::incNonce ( Address const &  _id)

Increament the account nonce.

Definition at line 320 of file State.cpp.

◆ kill()

void State::kill ( Address  _a)

Delete an account (used for processing suicides).

Definition at line 407 of file State.cpp.

◆ noteAccountStartNonce()

void State::noteAccountStartNonce ( u256 const &  _actual)

Definition at line 117 of file State.cpp.

◆ openDB()

OverlayDB State::openDB ( boost::filesystem::path const &  _path,
h256 const &  _genesisHash,
WithExisting  _we = WithExisting::Trust 
)
static

Open a DB - useful for passing into the constructor & keeping for other states that are necessary.

Definition at line 60 of file State.cpp.

◆ operator=()

State & State::operator= ( State const &  _s)

Copy state object.

Definition at line 132 of file State.cpp.

◆ originalStorageValue()

u256 State::originalStorageValue ( Address const &  _contract,
u256 const &  _key 
) const

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.

Definition at line 436 of file State.cpp.

◆ populateFrom()

void State::populateFrom ( AccountMap const &  _map)

Populate the state from the given AccountMap. Just uses dev::eth::commit().

Definition at line 104 of file State.cpp.

◆ requireAccountStartNonce()

u256 const & State::requireAccountStartNonce ( ) const

Definition at line 110 of file State.cpp.

◆ rollback()

void State::rollback ( size_t  _savepoint)

Revert all recent changes up to the given _savepoint savepoint.

Definition at line 560 of file State.cpp.

◆ rootHash()

h256 dev::eth::State::rootHash ( ) const
inline

The hash of the root of our state tree.

Definition at line 310 of file State.h.

◆ savepoint()

size_t State::savepoint ( ) const

Create a savepoint in the state changelog.

Returns
The savepoint index that can be used in rollback() function.

Definition at line 555 of file State.cpp.

◆ setBalance()

void State::setBalance ( Address const &  _addr,
u256 const &  _value 
)

Set the balance of _addr to _value. Will instantiate the address if it has never been used.

Definition at line 385 of file State.cpp.

◆ setCode()

void State::setCode ( Address const &  _address,
bytes &&  _code 
)

Sets the code of the account. Must only be called during / after contract creation.

Definition at line 520 of file State.cpp.

◆ setNonce()

void State::setNonce ( Address const &  _addr,
u256 const &  _newNonce 
)

Set the account nonce.

Definition at line 333 of file State.cpp.

◆ setRoot()

void State::setRoot ( h256 const &  _root)

Resets any uncommitted changes to the cache.

Definition at line 282 of file State.cpp.

◆ setStorage()

void State::setStorage ( Address const &  _contract,
u256 const &  _location,
u256 const &  _value 
)

Set the value of a storage position of an account.

Definition at line 430 of file State.cpp.

◆ storage() [1/2]

map< h256, pair< u256, u256 > > State::storage ( Address const &  _contract) const

Get the storage of an account.

Note
This is expensive. Don't use it unless you need to.
Returns
map of hashed keys to key-value pairs or empty map if no account exists at that address.

Definition at line 453 of file State.cpp.

◆ storage() [2/2]

u256 State::storage ( Address const &  _contract,
u256 const &  _memory 
) const

Get the value of a storage position of an account.

Returns
0 if no account exists at that address.

Definition at line 422 of file State.cpp.

◆ storageRoot()

h256 State::storageRoot ( Address const &  _contract) const

Get the root of the storage of an account.

Definition at line 492 of file State.cpp.

◆ subBalance()

void State::subBalance ( Address const &  _addr,
u256 const &  _value 
)

Subtract the _value amount from the balance of _addr account.

Exceptions
NotEnoughCashif the balance of the account is less than the amount to be subtrackted (also in case the account does not exist).

Definition at line 371 of file State.cpp.

◆ transferBalance()

void dev::eth::State::transferBalance ( Address const &  _from,
Address const &  _to,
u256 const &  _value 
)
inline

Transfers "the balance _value between two accounts.

Parameters
_fromAccount from which _value will be deducted.
_toAccount to which _value will be added.
_valueAmount to be transferred.

Definition at line 251 of file State.h.

Friends And Related Function Documentation

◆ BlockChain

friend class BlockChain
friend

Definition at line 164 of file State.h.

◆ dev::test::ImportTest

friend class dev::test::ImportTest
friend

Definition at line 162 of file State.h.

◆ dev::test::StateLoader

friend class dev::test::StateLoader
friend

Definition at line 163 of file State.h.

◆ ExtVM

friend class ExtVM
friend

Definition at line 161 of file State.h.

◆ operator<<

std::ostream& operator<< ( std::ostream &  _out,
State const &  _s 
)
friend

The documentation for this class was generated from the following files: