Package io.horizen.evm
Class StateDB
- java.lang.Object
-
- io.horizen.evm.ResourceHandle
-
- io.horizen.evm.StateDB
-
- All Implemented Interfaces:
AutoCloseable
public class StateDB extends ResourceHandle
-
-
Field Summary
Fields Modifier and Type Field Description static HashEMPTY_CODE_HASHCode hash of an empty byte arraystatic HashEMPTY_ROOT_HASHTrieHasher.Root() of an empty byte array
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaccessAccount(Address address)Add the given account to the access list.voidaccessSetup(Address sender, Address destination, Address coinbase, ForkRules rules)Reset and prepare account access list.booleanaccessSlot(Address address, Hash slot)Add given account storage slot to the access list.voidaddBalance(Address address, BigInteger amount)Add amount to balance of given account.voidaddLog(EvmLog evmLog)Add a new log entry.voidaddRefund(BigInteger gas)Add gas refund.voidclose()Close this instance and free up any native resources.Hashcommit()Commit any pending changes.voidfinalizeChanges()Finalize any pending changes, clear the current journal and reset refund counter.BigIntegergetBalance(Address address)Get balance of given account.byte[]getCode(Address address)Get code for the given account.HashgetCodeHash(Address address)Get account code hash.HashgetCommittedStorage(Address address, Hash key)Read committed storage trie of given account.HashgetIntermediateRoot()Get current state root hash including any currently pending changes, but without committing.EvmLog[]getLogs(Hash txHash)Get log entries created during the execution of given transaction.BigIntegergetNonce(Address address)Get account nonce.ProofAccountResultgetProof(Address address, Hash root, Hash[] storageKeys)Get the Merkle-proof for a given account and optionally some storage keys.BigIntegergetRefund()Get refunded gas.HashgetStorage(Address address, Hash key)Read storage trie of given account.booleanisEmpty(Address address)Check if the account with the given address is emptybooleanisEoaAccount(Address address)Check if account is an EAO one.booleanisSmartContractAccount(Address address)Check if an account is a smart contract accountvoidrevertToSnapshot(int revisionId)Rollback all state modifications since the snapshot with the given revision id was created.voidsetBalance(Address address, BigInteger amount)Set balance of account balancevoidsetCode(Address address, byte[] code)Set code for the given account.voidsetNonce(Address address, BigInteger nonce)Set account nonce.voidsetStorage(Address address, Hash key, Hash value)Write to storage trie of given account.voidsetTxContext(Hash txHash, int txIndex)Set tx context, used when the EVM emits new state logs.intsnapshot()Create a lightweight snapshot at the current state.voidsubBalance(Address address, BigInteger amount)Subtract from balance of given account.voidsubRefund(BigInteger gas)Remove gas refund.StringtoString()
-
-
-
Method Detail
-
close
public void close() throws ExceptionClose this instance and free up any native resources. Must not use this instance afterwards.- Throws:
Exception
-
finalizeChanges
public void finalizeChanges()
Finalize any pending changes, clear the current journal and reset refund counter. Note: Must be called between separate transactions as rollbacks are not supported over multiple transactions, therefore also invalidates all snapshots.
-
getIntermediateRoot
public Hash getIntermediateRoot()
Get current state root hash including any currently pending changes, but without committing.- Returns:
- state root hash
-
commit
public Hash commit()
Commit any pending changes. Invalidates all snapshots taken before.- Returns:
- updated state root hash
-
isEmpty
public boolean isEmpty(Address address)
Check if the account with the given address is empty- Parameters:
address- account address- Returns:
- true if account state is empty, otherwise false
-
isEoaAccount
public boolean isEoaAccount(Address address)
Check if account is an EAO one. Account is considered an EOA in two cases:- account doesn't exist in the StateDB (first time it receives coins);
- account has no code (code hash is a keccak256 hash of empty array).
- account address does not match any of the precompiled native contracts.
- Parameters:
address- account address- Returns:
- true if account is EOA, otherwise false
-
isSmartContractAccount
public boolean isSmartContractAccount(Address address)
Check if an account is a smart contract account- Parameters:
address- account address- Returns:
- true if account is a smart contract one, otherwise false
-
getBalance
public BigInteger getBalance(Address address)
Get balance of given account.- Parameters:
address- account address- Returns:
- account balance, 0 if account not exist
-
addBalance
public void addBalance(Address address, BigInteger amount)
Add amount to balance of given account. Will implicity create account if necessary.- Parameters:
address- account addressamount- amount to add to account balance
-
subBalance
public void subBalance(Address address, BigInteger amount)
Subtract from balance of given account.- Parameters:
address- account addressamount- amount to subtract from account balance
-
setBalance
public void setBalance(Address address, BigInteger amount)
Set balance of account balance- Parameters:
address- account addressamount- amount to assign to the account balance
-
getNonce
public BigInteger getNonce(Address address)
Get account nonce.- Parameters:
address- account address- Returns:
- account nonce
-
setNonce
public void setNonce(Address address, BigInteger nonce)
Set account nonce.- Parameters:
address- account addressnonce- value to set account nonce to
-
getCodeHash
public Hash getCodeHash(Address address)
Get account code hash.- Parameters:
address- account address- Returns:
- code hash
-
getCode
public byte[] getCode(Address address)
Get code for the given account.- Parameters:
address- account address
-
setCode
public void setCode(Address address, byte[] code)
Set code for the given account. Will also recalculate and set code hash accordingly.- Parameters:
address- account addresscode- code binary
-
addRefund
public void addRefund(BigInteger gas)
Add gas refund.- Parameters:
gas- amount to add to refund counter
-
subRefund
public void subRefund(BigInteger gas)
Remove gas refund.- Parameters:
gas- amount to remove from refund counter
-
getRefund
public BigInteger getRefund()
Get refunded gas.- Returns:
- refunded gas
-
getStorage
public Hash getStorage(Address address, Hash key)
Read storage trie of given account.- Parameters:
address- account addresskey- storage key- Returns:
- storage value, always 32 bytes
-
getCommittedStorage
public Hash getCommittedStorage(Address address, Hash key)
Read committed storage trie of given account.- Parameters:
address- account addresskey- storage key- Returns:
- comitted storage value, always 32 bytes
-
setStorage
public void setStorage(Address address, Hash key, Hash value)
Write to storage trie of given account. Note: Do not mix RAW and CHUNKED strategies for the same key, this can potentially lead to dangling nodes in the storage Trie and de facto infinite-loops.- Parameters:
address- account addresskey- storage keyvalue- value to store
-
getProof
public ProofAccountResult getProof(Address address, Hash root, Hash[] storageKeys)
Get the Merkle-proof for a given account and optionally some storage keys.- Parameters:
address- account addressstorageKeys- storage keys- Returns:
- proofs
-
snapshot
public int snapshot()
Create a lightweight snapshot at the current state.- Returns:
- revision id of the snapshot
-
revertToSnapshot
public void revertToSnapshot(int revisionId)
Rollback all state modifications since the snapshot with the given revision id was created.- Parameters:
revisionId- revision id of the snapshot to revert to
-
getLogs
public EvmLog[] getLogs(Hash txHash)
Get log entries created during the execution of given transaction.- Parameters:
txHash- transaction hash- Returns:
- log entries related to given transaction hash
-
addLog
public void addLog(EvmLog evmLog)
Add a new log entry.- Parameters:
evmLog- log entry
-
setTxContext
public void setTxContext(Hash txHash, int txIndex)
Set tx context, used when the EVM emits new state logs.- Parameters:
txHash- the hash of the transaction to be set in contexttxIndex- the index of the transaction in the block
-
accessSetup
public void accessSetup(Address sender, Address destination, Address coinbase, ForkRules rules)
Reset and prepare account access list.- Parameters:
sender- sender accountdestination- destination account
-
accessAccount
public boolean accessAccount(Address address)
Add the given account to the access list.- Parameters:
address- account to access- Returns:
- true if the account was already on the access list, false otherwise
-
accessSlot
public boolean accessSlot(Address address, Hash slot)
Add given account storage slot to the access list.- Parameters:
address- account to accessslot- storage slot to access- Returns:
- true if the slot was already on the access list, false otherwise
-
toString
public String toString()
- Overrides:
toStringin classResourceHandle
-
-