- EVM : a global decentralized computer is the part of Ethereum that handles smart contract deployment and execution. It doesn’t involve simple coin transfer from user to user but everything else will involve a state update computed by the EVM.
- The job of the EVM is to update the Ethereum state by computing valid state transitions as a result of smart contract code execution, as defined by the Ethereum protocol.
- The EVM is a quasi-Turing-complete state machine ; “quasi” because all execution processes are limited to a finite number of computational steps by the amount of gas available.
- EVM has a stack-based architecture, storing all in memory values with word size 256 bits and has program, memory, storage components.
- EVM instruction set offers: arithmetic, logic operation, stack-memory-storage access, control flow operation, logging-calling operators
- EVM has access to account information: address, balance, block number and current gas price
- Available OP codes are:
- Arithmetic operations
- Stack operations
- Process flow operations
- System operations
- Logic operations
- Environmental operations
- Block operations
- each Ethereum address represents an account comprising an ether balance (stored as the number of wei owned by the account)
- a nonce (representing the number of transactions successfully sent from this account if it is an EOA, or the number of contracts created by it if it is a contract account)
- the account’s storage (which is a permanent data store, only used by smart contracts), and
- the account’s program code (again, only if the account is a smart contract account). An EOA will always have no code and an empty storage.
- EVM running on a sandboxed copy of the Ethereum world system, with this sandboxed version being discarded completely if execution can not complete like OOG : Out of Gas.
Bytecode
In order to create a new contract, a special transaction is needed that has its to field set to the special 0x0 address and its data field set to the contract’s initiation code. Contracts live on the blockchain in an Ethereum-specific binary format (EVM bytecode). However, contracts are typically written in some high-level language such as Solidity and then compiled into byte code to be uploaded on the blockchain
Programs available for Disassembling the Bytecode:
Decompiler for EVM bytecode into readable Solidity-syntax contracts — to enable static and dynamic analysis of compiled contracts but also vulnerability discovery
When you send a transaction to an ABI-compatible smart contract (which you can assume all contracts are), the transaction first interacts with that smart contract’s dispatcher. The dispatcher reads in the data field of the transaction and sends the relevant part to the appropriate function.
Turing Completeness :
- a system or programming language is Turing complete if it can run any program
- We have to actually go through with the execution of the program and wait for it to finish to find out whether it will take forever or not to execute; called the halting problem
- Ethereum acts like a single-threaded machine, without any scheduler, and so if it became stuck in an infinite loop (halting problem ) this would mean it would become unusable, the solution is GAS
GAS
- Gas is Ethereum’s unit for measuring the computational and storage resources required to perform actions on the Ethereum blockchain
- Ethereum must account for every computational step performed by transactions and smart contract code execution.
- Each operation performed by a transaction or contract costs a fixed amount of gas
- When an EVM is needed to complete a transaction, in the first instance it is given a gas supply equal to the amount specified by the gas limit in the transaction
- Before each operation, the EVM checks that there is enough gas to pay for the operation’s execution. If there isn’t enough gas, execution is halted and the transaction is reverted.
- If execution is successfull, the gas is paid to miner as transaction fee in ether.
miner fee = gas cost * gas price
- Remaining Gas is refunded to sender in ether
remaining gas = gas limit - gas cost | refunded ether = remaining gas * gas price
- If transaction run out of gas during execution, operation is terminated raising out of gas exception & transaction is reverted and all changes to the state are rolled back but sender will be charged a transaction fee for performed work.
- Every operation cost Gas based on their computational usage like SHA3 function costs 10 times (30Gas) than ADD operation (3 Gas), EVM memory and storing data in contract’s on-chain storage also cost Gas.
- When constructing a new block, miners on the Ethereum network can choose among pending transactions by selecting those that offer to pay a higher gas price which result in transaction getting faster confirmation.
- While gas has a price, it cannot be “owned” nor “spent.” Gas exists only inside the EVM, as a count of how much computational work is being performed. The sender is charged a transaction fee in ether, which is then converted to gas for EVM accounting and then back to ether as a transaction fee paid to the miners.
- Negative gas costs : deletion of used storage variables and accounts by refunding some of the gas ; (i) Deleting a contract (SELFDESTRUCT) (ii) Changing storage address from nonzero value to zero (
SSTORE[x] = 0
) ; maximum refund for a transaction is set to half the total amount of gas used
Block Gas Limit
- The block gas limit is the maximum amount of gas that may be consumed by all the transactions in a block, and constrains how many transactions can fit into a block.
- The miners on the network collectively decide the block gas limit uses a mining program, such as Ethminer, which connects to a Geth or Parity Ethereum client.
- The Ethereum protocol has a built-in mechanism where miners can vote on the gas limit so capacity can be increased or decreased in subsequent blocks.
“Mastering Ethereum by Andreas M. Antonopoulos and Dr. Gavin Wood (O’Reilly). Copyright 2019 The Ethereum Book LLC and Gavin Wood, 978–1–491–97194–9.”
Follow me on Twitter: https://twitter.com/BgxDoc