> For the complete documentation index, see [llms.txt](https://thryec.gitbook.io/dev-compedium/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thryec.gitbook.io/dev-compedium/ethereum/solidity/evm.md).

# EVM

### State Machine&#x20;

* Ethereum is a distributed state machine. Ethereum's state is a *machine state*, which can change from block to block according to a pre-defined set of rules, and which can execute arbitrary machine code.
* The specific rules of changing state from block to block are defined by the EVM.
* The state is an enormous data structure called a [modified Merkle Patricia Trie](https://ethereum.org/en/developers/docs/data-structures-and-encoding/patricia-merkle-trie/), which keeps all [accounts](https://ethereum.org/en/developers/docs/accounts/) linked by hashes and reducible to a single root hash stored on the blockchain.
* Deterministic state transition function: `Y(S, T)= S’` → Given an old valid state `(S)` and a new set of valid transactions `(T)`, the Ethereum state transition function `Y(S, T)` produces a new valid output state `S'`

### Bytecode&#x20;

* Bytecode is a hexadecimal representation of the functions in a Solidity contract. The data payload of a transaction creating a smart contract is itself bytecode that runs the contract constructor, sets up the initial contract state and returns the final contract bytecode.
* Constructors are not present in the contract once deployed.

### ABI

ABIs are the definitions that help us know the method names, parameters and arguments, and data types that we can use to interact with a smart contract, and also the structure of events emitted by the smart contract. For functions, here are the properties found in ABIs ([source](https://blog.chain.link/dynamic-nfts-chainlink-keepers-vrf-price-feeds/)):

* **type**: specifies the nature of the function and will be one fof function, constructor, receive or fallback
* **name:** what that function’s name is.
* **inputs**: an array of objects with the following schema:
  * **name**: the name of the parameter.
  * **type**: the type of that parameter.
  * **components**: used when the type is a tuple.
* **outputs:** an array of objects similar to inputs.
* **stateMutability:** a string that specifies the state mutability of this function. Values are view, pure, view, nonpayable, and payable.

### Opcodes&#x20;

Opcodes are 1 byte in length leading to 256 different possible opcodes. The EVM only uses 140 unique opcodes.

### Function Selectors&#x20;

Function selectors are defined as the first four bytes of the Keccak hash of the canonical representation of the function signature.

```solidity
// Examples of function selectors 
keccak256(“store(uint256)”) →  first 4 bytes = 6057361d
keccak256(“retrieve()”) → first 4 bytes = 2e64cec1
```

References: <https://github.com/ethereumbook/ethereumbook/blob/develop/13evm.asciidoc>
