# State management and VM instance creation

The VM itself is owned and instantiated by the `State` class, which reflects a crucial design decision: centralizing virtual machine resource management like this ensures that each contract execution context is cleanly managed and isolated. Whenever a new transaction or contract call needs to be executed, regardless of its nature (be it an EVM/C++ contract execution or a simple native transfer), the `State` class is responsible for instantiating a new `ContractHost` object with the relevant parameters required for execution:

```cpp
ContractHost(
  evmc_vm* vm,
  DumpManager& manager,
  const Storage& storage,
  const Hash& randomnessSeed,
  const evmc_tx_context& currentTxContext,
  boost::unordered_flat_map<Address, std::unique_ptr<BaseContract>, SafeHash>& contracts,
  boost::unordered_flat_map<Address, NonNullUniquePtr<Account>, SafeHash>& accounts,
  boost::unordered_flat_map<StorageKey, Hash, SafeHash>& vmStorage,
  const Hash& txHash,
  const uint64_t txIndex,
  const Hash& blockHash,
  int64_t& txGasLimit
);
```

Once an instance of `ContractHost` is created, it offers methods like `execute()` to run the contract, `simulate()` for simulating the transaction (useful for gas estimation), and `ethCallView()` for making calls to other contracts within a non-state-changing context.

`ContractHost` also extends the functionalities of `evmc::Host` by overriding several key functions that interface directly with the Ethereum Virtual Machine, which are obligatory for the VM to interact with the State:

```cpp
bool account_exists(const evmc::address& addr) const noexcept final;
evmc::bytes32 get_storage(const evmc::address& addr, const evmc::bytes32& key) const noexcept final;
evmc_storage_status set_storage(const evmc::address& addr, const evmc::bytes32& key, const evmc::bytes32& value) noexcept final;
evmc::uint256be get_balance(const evmc::address& addr) const noexcept final;
size_t get_code_size(const evmc::address& addr) const noexcept final;
evmc::bytes32 get_code_hash(const evmc::address& addr) const noexcept final;
size_t copy_code(const evmc::address& addr, size_t code_offset, uint8_t* buffer_data, size_t buffer_size) const noexcept final;
bool selfdestruct(const evmc::address& addr, const evmc::address& beneficiary) noexcept final;
evmc::Result call(const evmc_message& msg) noexcept final;
evmc_tx_context get_tx_context() const noexcept final;
evmc::bytes32 get_block_hash(int64_t number) const noexcept final;
void emit_log(const evmc::address& addr, const uint8_t* data, size_t data_size, const evmc::bytes32 topics[], size_t topics_count) noexcept final;
evmc_access_status access_account(const evmc::address& addr) noexcept final;
evmc_access_status access_storage(const evmc::address& addr, const evmc::bytes32& key) noexcept final;
evmc::bytes32 get_transient_storage(const evmc::address &addr, const evmc::bytes32 &key) const noexcept final;
void set_transient_storage(const evmc::address &addr, const evmc::bytes32 &key, const evmc::bytes32 &value) noexcept final;
```

These methods manage everything from account validation to logging, providing access to the state and storage, and handling calls between contracts. The `ContractHost` class encapsulates these functions, ensuring that each contract execution is isolated and secure.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.applayer.com/testnet/evm-contracts/state-management-and-vm-instance-creation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
