Class StateDB

    • Field Detail

      • EMPTY_CODE_HASH

        public static final Hash EMPTY_CODE_HASH
        Code hash of an empty byte array
      • EMPTY_ROOT_HASH

        public static final Hash EMPTY_ROOT_HASH
        TrieHasher.Root() of an empty byte array
    • Constructor Detail

      • StateDB

        public StateDB​(Database db,
                       Hash root)
        Opens a view on the state at the given state root hash.
        Parameters:
        db - database instance
        root - root hash
    • Method Detail

      • close

        public void close()
                   throws Exception
        Close 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:
        1. account doesn't exist in the StateDB (first time it receives coins);
        2. account has no code (code hash is a keccak256 hash of empty array).
        3. 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 address
        amount - amount to add to account balance
      • subBalance

        public void subBalance​(Address address,
                               BigInteger amount)
        Subtract from balance of given account.
        Parameters:
        address - account address
        amount - amount to subtract from account balance
      • setBalance

        public void setBalance​(Address address,
                               BigInteger amount)
        Set balance of account balance
        Parameters:
        address - account address
        amount - 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 address
        nonce - 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 address
        code - 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 address
        key - storage key
        Returns:
        storage value, always 32 bytes
      • getCommittedStorage

        public Hash getCommittedStorage​(Address address,
                                        Hash key)
        Read comitted storage trie of given account.
        Parameters:
        address - account address
        key - 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 address
        key - storage key
        value - value to store
      • getProof

        public ProofAccountResult getProof​(Address address,
                                           Hash[] storageKeys)
        Get the Merkle-proof for a given account and optionally some storage keys.
        Parameters:
        address - account address
        storageKeys - 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 context
        txIndex - the index of the transaction in the block
      • accessSetup

        public void accessSetup​(Address sender,
                                Address destination)
        Reset and prepare account access list.
        Parameters:
        sender - sender account
        destination - 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 access
        slot - storage slot to access
        Returns:
        true if the slot was already on the access list, false otherwise