Black friday

Save big!

All courses under $5 - for a limited time!

Code has been added to clipboard!

Ethereum Virtual Machine: Why and How You Should Use It

Reading time 5 min
Published Jan 5, 2016
Updated Nov 5, 2019

Ethereum virtual machine, or EVM for short, is a blockchain-based software platform. It allows developers to create decentralized applications (Dapps). Programmers value them for having no downtimes and keeping all created objects safe from modifying.

You don’t need extensive coding background to use EVM. It also eliminates the need for powerful hardware, making it perfect for a beginner.

Ethereum Virtual Machine: Main Tips

  • Ethereum has external and contract accounts. With a transaction from one, you can send Ether or binary data to another.
  • Using gas protects EVM from potential attacks that could slow down the system.
  • Depending on its type, Ethereum data is stored in storage, memory, or stack.
  • You can disable EVM accounts, or remove them by using the selfdestruct Solidity operation.

Ethereum Explained

So, how does Ethereum work? Basically, it executes tasks using special instructions. These are called opcodes. Every opcode is 1 byte in size and encoded to bytecode. It gets split into its bytes when you execute a specific task.

Due to a set of 140 opcodes, Ethereum virtual machine is Turing-complete. That means it should be able to solve any computation problem. The code running on it has no access to other processes on your computer, which makes EVM isolated.

Accounts

There are two types of accounts in Ethereum - external and contract. Ethereum virtual machine treats both of them equally.

Type Controlled by Address
External account Public-private key pairs From the public key
Contract account Code kept within the account From the creator’s address and amount of their transactions

Every account has an Ether balance. Any transaction that contains Ether will change it.

Transactions and Gas

A transaction sent from one account to the other can contain Ether or binary data. The latter can also be called payload. Further actions depend on the target account:

  • If it contains code, it is executed. The payload then becomes input data.
  • If it is not set, the code you send is executed and returns code for a new contract. During the construction, the code of the contract is empty.

As there is no central authority, all contracts are executed on all Ethereum nodes. That could cause a risk of someone slowing down the network intentionally. All they’d have to do is create a ton of complex contracts.

As protection from this kind of attacks, every opcode has a base gas cost. Gas is a token used to pay EVM for executing the transaction. Its purpose is to put a limit on how much work a certain operation requires. As Ethereum virtual machine executes the transaction, it uses its gas up bit by bit.

Memory and Logs

The Ethereum virtual machine specification lists three separate storage areas:

  • Storage
  • Memory
  • The stack

Storage areas are located inside each account and store the contract state variables. Storage is assigned during the process of creating a contract. You can only change it with a sendTransaction call.

Note: no contract can read the storage of another contract or write into it.

Memory is linear and holds temporary variables. As they only exist in the calling function, memory gets erased between calls. You can address memory at byte level. However, the limit of one reading is 256 bits, and writes can be of 8 or 256 bits in width.

You need to pay in gas to expand memory. It scales quadratically, and the more it grows, the more it costs. Anyway, it is cheaper to use than storage.

Ethereum virtual machine specification defines it as a stack machine. The stack is where the computations happen. This data area holds up to 1024 small local variables. Each stack item has a size of 256 bits. The stack is also the cheapest of all three data storing areas.

A developer has limited access to the stack. You may copy one of sixteen top elements to the very top, or make the top element change places with one of the sixteen right under it. To get deeper access, you can move stack elements to storage or memory.

Logs technically aren’t a memory type. However, they are also used to store data, so we should cover them as well when introducing Ethereum and Solidity. Logs hold data in an indexed structure with a mapping that reaches block level. Contracts will not access the data stored in the log after it’s created. However, they can be reached from outside the blockchain. Some of this data is also kept in bloom filters.

Using selfdestruct

Solidity selfdestruct is an operation needed for removing code from the blockchain. However, it will remain part of the chain’s history and might remain in most EVM nodes.

When a certain contract performs a Solidity selfdestruct operation, it removes both code and storage from the state. If there is any Ether left in the address, it moves to a designated place.

Ethereum Virtual Machine

If you only wish to deactivate contracts, you can disable them. Change an internal state to revert the functions in the future. This way, the contract will return any Ether received.

Note: if you send Ether to a contract that has already been removed, it will be lost.

Ethereum Virtual Machine: Summary

  • Ethereum accounts can be external and contract. You can send Ether or binary data from one account to another by executing a transaction.
  • Due to using gas, it is practically impossible to slow down EVM performance intentionally.
  • To store the data in Ethereum, you can use storage, memory, or stack.
  • To remove EVM accounts, you may use the selfdestruct Solidity operation. Alternatively, you can deactivate them by modifying an internal state.