When a smart contract runs on Ethereum it's executed by the Ethereum virtual machine. In this short article, I will give you a quick intro to the Ethereum Virtual Machine (EVM), the most interesting part of Ethereum. If you are new here, I'm Ayush, a member of the Get Into Web3 community, where we make it easy for people to get started in Web3.
The Ethereum network is composed of computers called nodes. The EVM runs inside each of these Ethereum instances and the role of the EVM is to run smart contracts. The EVM is a state machine. In input, there is the state of the existing blockchain, and in output, the state is updated after a transaction has been applied.
The state of the blockchain is a mapping of accounts identified by the address to four fields-
- the nonce that is incremented for each transaction,
- the balance of ether owned by this address,
- the hash of the smart contract code, and
- the hash of the smart contract storage
The last two fields are populated only for smart contracts. For regular addresses called EOA, they are empty. The EVM can be executed in read mode and write mode.
In read mode, a user sends a request to any node of the network and the EVM of this node will process the request. In write mode, users send transactions that are mined by miners in the Ethereum network.
In each mining node, the EVM is going to process all the transactions of the new block. Each transaction is processed sequentially by the EVM in an order decided arbitrarily by each miner. For each transaction, the EVM is going to check if the transaction is valid, compute the new state of the blockchain and persist the state. For example, an address can increase its ether balance or its storage is modified. The process is repeated until all transactions are executed for this block. The miner that wins the block will propagate the new block to the network containing the hash of the new state. One important detail of the EVM is how it processes smart contracts?
Smart Contracts are basically a set of promises, specified in digital form, including protocols within which the party is performing on these promises. It's a reusable snippet of code that a developer publishes into the EVM memory. Smart contracts are often written in a programming language called Solidity. Smart contract languages like Solidity cannot be executed by the EVM directly, instead, they are compiled to a low-level machine language called opcodes. To execute specific tasks in order to efficiently store these occurrences, they are further encoded to bytecodes. Now, anyone can request the smart contract code to be executed by making a transaction request.
There comes the concept of Gas. In Ethereum, gas is essentially the fuel that allows the Ethereum network to operate. They have a market price determined by the demand for resources on the network. The amount of gas you need depends on how complex of a smart contract function you are trying to execute and how fast you want to execute it. The gas fee keeps the Ethereum network secure, preventing actors from scamming the network.
The potential use case of the EVM and decentralized apps are huge and developers are working every day to make the entire ecosystem better.
That's it for this quick intro to the Ethereum virtual machine (EVM). Going forward, if you want to know more about the technologies associated with Web3, don’t forget to follow and be a part of the Get Into Web3 (GIW3) community for the latest updates. Also, don't forget to follow us on Twitter and join the Discord community to learn more.
See you soon reading my next blog :)