Halborn Logo

// Blog

What Is Ethereum Virtual Machine (EVM)?


profile

Rob Behnke

February 27th, 2023


EVM - which stands for Ethereum Virtual Machine - is a runtime environment for smart contracts in the Ethereum blockchain. The EVM executes code for a smart contract and ensures that the code is executed in a safe and reliable manner.

The EVM is a sandboxed environment, meaning that it is isolated from the rest of the network and has limited access to resources. This ensures that code executed on the EVM is secure and cannot harm the Ethereum network, in addition to allowing developers to test their apps isolated as well.

The EVM allows for the execution of complex, deterministic computations on the Ethereum blockchain, making it possible for developers to create decentralized applications (dApps) that can interact with the Ethereum network.

EVM is Quasi-Turing-Complete due to the gas mechanism, which means that EVM is Turing complete but its functioning is also dependent on the gas available, so there is a limiting parameter to it.

EVM is a very useful tool for developers as it allows them to write smart contracts that can execute on the blockchain in addition to facilitating the transfer or extension of dApps to other EVM compatible chains.

In essence, the EVM is a runtime environment for executing smart contracts on the Ethereum blockchain. EVM is an essential component of the Ethereum network that allows for the creation of dApps and the execution of complex, deterministic computations on the blockchain.

How does the EVM function? 

To understand the EVM and how it functions, it is necessary to have some fundamental ideas about:

Virtual Machines (VMs)

Virtual Machines have components such as memory, storage, and CPU attributes just as an ordinary (hardware) computer does. They are working locally on the user’s computer.

Virtual Machines are often used to benefit other operating systems’ attributes. For example, a Linux user needs to use software that is specific to Windows, and so he downloads a Windows virtual machine to run on his Linux computer and has access to that software.

Decentralized Virtual Machines (DVMs)

Virtual Machines use the power of the user’s computer to operate. Unlike VMs, a Decentralized Virtual Machine, or DVM, is not tied to a computer but instead, relies on a network of participating devices or nodes to provide the computing power and resources necessary to execute its operations/code. 

This distributed, decentralized nature of the DVMs like EVM makes them resistant to censorship and tampering as there is not a single point of failure due to decentralization. 

Therefore, altering the records of a node or two with malicious intent would not make a difference as the consensus is made upon the records of all the nodes and the records of the majority will render the records of the nodes that are in the minority with the altered data useless.

Like VMs, DVMs have the attributes of a hardware computer such as memory, storage, etc. and, DVMs such as EVM are specifically designed to allow the execution of smart contracts in blockchain technologies.

The Manual and the Language of the EVM: Opcodes

The EVM executes low-level instructions known as Opcodes (operation codes). Opcodes are the essential building elements of the EVM and are used for a variety of operations, including sending transactions, checking account balances, and gaining access to blockchain data.

How can EVM execute Opcodes?

For EVM to be able to execute Opcodes, EVM needs to be given the code in the machine-readable bytecode, which is encoded from opcodes as binary numbers. 

For that reason, smart contracts are written in high-level programming languages (such as Solidity) and are then compiled into bytecode that contains the opcodes that the EVM can execute. 

A smart contract is compiled into bytecode and deployed on the Ethereum blockchain, where the EVM then runs the code. Depending on the executed smart contract (ie. depending on how many and which Opcodes will need to be used for the request), there will be an associated gas fee for the execution of the smart contract. 

Currently, there are around 158 Opcodes available on the EVM. Each opcode performs a specific function. While allowing for the execution of complicated computations, the EVM's limited collection of opcodes also keeps the system simple and predictable, making it simpler to reason about and audit the code run on the EVM.

External Connection with Oracles (Smart Contracts)

The concept of Oracles fits better under the umbrella of smart contracts rather than the EVM. However, as smart contracts are a vital part of EVM and Oracles are unique in function for smart contracts, they should be mentioned. 

Since the smart contracts on the Ethereum blockchain are isolated, they are not able to access the external/real world data on their own.  Instead, smart contracts get external data via oracles, which are third-party services. There are various types of Oracles: Software (the most commonly used type in the blockchain), Hardware, Inbound and Outbound oracles.

A nice example would be a smart contract which is designed to or required to track real time prices of some particular assets.  This contract could use oracles to receive third party information from various sources to stay updated regarding the value of assets. After receiving the information, oracle would give the smart contract access to that information so it could run its code and check to see whether specific requirements had been met.

Another example might be an NFT of Paris designed to track the current weather. For example, when it is rainy in Paris, the smart contract of the NFT could change its interface to a rainy scene of that place, when it is sunny, a sunny scene, etc. This is made possible by the NFT receiving external data about the weather via Oracles.

Nodes as the Power Source

The EVM is a runtime environment for executing smart contracts on the Ethereum network, and nodes are an essential component of the Ethereum network that the EVM relies on. When a smart contract is deployed to the Ethereum network, it is executed by the EVM on the nodes that make up the network. The nodes are responsible for validating and relaying transactions, as well as maintaining the distributed ledger, which the EVM uses to store and access data. 

Shortly, nodes give EVM the power to run.

Gas System for Security and Utility

The gas system is an essential part of the EVM since it makes sure the network is efficiently working and safe.

What is gas?

Gas is a unit of measurement that is used to assign a cost to each operation that is executed on the EVM.

What are its functions?

The primary function of gas is to prevent inefficient or excessive use of the Ethereum network. By attaching a cost to each operation, the gas system provides an economic incentive for users to operate efficiently and not waste network resources.

The second function of gas is security. The gas mechanism disincentivizes attackers from spamming or performing denial-of-service (DoS) on the Ethereum blockchain by making users pay for the gas that their transactions require.  The attackers would have to spend a lot of money to carry out a successful attack since they would have to pay for the gas that their transactions consume. This makes the attacks too expensive, and so less profitable.

EVM Pros And Cons

Pros

  • The EVM allows complex and deterministic computations on blockchain and allows smart contracts to be executed.

  • The EVM executes smart contracts isolated (within a sandboxed environment) from the rest of the blockchain. This helps to ensure that code allowed to execute in the Ethereum network cannot harm the blockchain. 

  • Allowing the execution of smart contracts and the creation of dApps that can communicate across the Ethereum network.

 Cons

  • Gas fee/Not Turing-completeness

EVM could be Turing complete if the gas system was not implemented. However, gas is necessary to make some malicious operations (such as spamming) too expensive to be profitable. The use of gas and gas limits per block makes EVM quasi-Turing-complete as some applications will require too much gas to run even though they are theoretically supported by the EVM. 

  • Limited programming language support 

The EVM currently only supports a limited number of  programming languages.  This can make it difficult for developers who are more comfortable with other languages to build on the platform.

Conclusion

The Ethereum Virtual Machine (EVM) is an essential part of the Ethereum blockchain.   The EVM is a DVM and executes code using the combined power of thousands of nodes on the Ethereum network. Sophisticated, deterministic computations may be carried out on the blockchain, and decentralized apps (dApps) can be developed and used to communicate with the Ethereum network.

Developers can create and execute smart contracts on the blockchain using the EVM’s runtime environment.  Some key takeaways about the EVM include: 

  • Internally, the EVM executes bytecode, which is composed of opcodes.  These are low-level instructions that tell the EVM what operations to perform when running the smart contract's code.  

  • Smart contracts, though running isolated on the EVM, can receive external data via. oracles. 

  • For security and utility, EVM uses the gas system to give users a financial incentive to be efficient and not waste network resources of the EVM whilst rendering some malicious code too expensive to use.

Overall, the EVM is a crucial part of the Ethereum blockchain that enables the execution of intricate, deterministic computations and the development of decentralized apps. It is a potent tool that enhances the development and the utility of blockchain technology.

© Halborn 2024. All rights reserved.