Note

If you are new to the blockchain technology, taking our Introduction to Blockchain Technology self-paced course is highly recommended. Also, for a comprehensive coverage of blockchain development in Ethereum or mastering Solidity programming, taking our below self paced courses is highly recommended:

Recap

In our previous article (How Ether and Gas Work in Ethereum), we learned about how Ether and Gas are used in Ethereum.

In this article, we discuss how Ethereum Virtual Machine works.

The Ethereum Virtual Machine

In the Java world, developers write Java code in any Java IDE and compile it into bytecodes, which in turn are loaded into a virtual machine called the Java Virtual Machine, or JVM, behind the scenes through a Java class loader, and executed in the JVM. The JVM specification describes what is required of a JVM implementation and ensures the interoperability of Java programs across different implementations and underlying hardware platforms.

Similarly, in Ethereum, smart contracts and DApp developers code smart contracts in an Ethereum high-level language such as Solidity, compile them into bytecodes, and upload them on blockchain for invocation and execution. The EVM is the runtime execution environment for smart contracts in Ethereum. It comes with a different implementation of Ethereum clients, as we discussed earlier. Each implementation follows the EVM specification defined in the Ethereum Yellow Paper: https://ethereum.github.io/yellowpaper/paper.pdf.

 

One-to-One Live Blockchain Classes

Coding Bootcamps school offers One-to-One Live Blockchain Classes for Beginners.

 

Now, let’s delve deeper to understand what the EVM is and how it executes a smart contract.

The EVM is a simple stack-based architecture. When executing a smart contract, it performs all operations, or in technical terms, opcodes, as defined in the EVM code, or bytecode.
Ethereum provides three types of space in the EVM for the operations to access and store data:

  • Stack: This is a last-in, first-out container with a fixed size and maximum depth of 1,024 items to which values can be pushed and popped. Each stack item is 256 bits long. This was chosen to facilitate the Keccak- 256 hash scheme and elliptic- curve computations.
  • Memory: This is a volatile and expandable word-addressed byte array.
  • Key/value store: This is a word-addressable word array. It is meant to be used as the long term account storage for the smart contract. Depending on whether you use account storage, your smart contract may be considered stateful or stateless.

During smart contract execution, the EVM has access to the incoming message, as well as block header data. Additionally, the EVM keeps track of the available gas and a Program Counter (PC) during execution. The following diagram shows the EVM:

Ethereum blockchain

Unlike Java, Ethereum smart contract bytecode is uploaded through contract creation transactions, instead of some class loader within the virtual machine. As we discussed earlier, all transactions are digitally signed by the EOA. As shown in the following diagram, when a new smart contract needs to be created, the developer typically follows these steps to get a smart contract deployed into the EVM:

  • The developer writes the smart contract in Solidity and compiles it into bytecodes.
  • They use their own Ethereum account to sign the account creation transaction with the bytecode.
  • They send an account creation transaction to the Ethereum network.

 

Steps 2 and allow Ethereum to upload the code on the blockchain and the creation of the accounts in the world state through the Ethereum mining process:

Ethereum blockchain

Depending on the tools you use, you will likely invoke a JSON-RPC call such
as web3.eth_sendTransaction to create a new contract on the blockchain, with parameters as follows:

params: [{

“from”: “< EOA Address >”, “to”: “”, “gas”: “<gas>”, “gasPrice”: “<gasPrice>”, “value”: “<value>”, “data”: “0x <compiled smart contract code>” }]

Once the contract account is created through the mining process, you can invoke a JSON- RPC call such as web3.eth_getTransactionReciept to find out the contract address. You can always go to the Etherscan site to view and search your contract code using the contract address.

The following screenshot was taken on March 9, 2019 and shows the latest 1,000 verified Solidity contracts on the Ethereum blockchain. A verified smart contract means the ownership of the Ethereum address used to create the smart contract has been verified, and the owner has an Etherscan account. You can click any address and further view the smart contract source code:

Ethereum blockchain

As we discussed earlier, the contract can be invoked by an externally owned account via a transaction or a contract account via a function call from another smart contract.

Check out the following diagram:

Ethereum blockchain

As shown in the preceding diagram, we can see the following:

  • Once invoked, the EVM will load contract bytecodes into memory, loop through opcodes while checking the available gas, and perform all operations on the EVM stack.
  • During the execution, if there is not enough gas or an error occurs, the execution will aborted.
  • The transaction will not go through, but the sender will not get the spent gas back.

 

EVM executes around 140 operations or opcodes that are divided into the following 11 categories. All of the opcodes and their complete descriptions are available in the Ethereum Yellow Paper (https://github.com/ethereum/yellowpaper):

  • Stop and arithmetic operations (0x00 – 0x0b)
  • Comparison and bitwise logic operation (0x10 – 0x1a)
  • SHA3 (0x20)
  • Environmental information (0x30 – 0x3e)
  • Block information (0x40 – 0x45)
  • Stack, memory, storage, and flow operations (0x50 – 0x5b)
  • Push operations (0x60 – 0x7f)
  • Duplication operations (0x80 – 0x8f)
  • Exchange operations (0x90 – 0x9f)
  • Logging operations (0xa0 – 0xa4)
  • System operations (0xf0 – 0xff)

You may remember the relationship between transaction fees, gas, and gas prices from our earlier discussion. In fact, the winning node adding the new block to the blockchain gets paid with the transaction fee for every smart contract executed within its EVM. The payment is calculated for all of the computations the miner made to store, compute, and execute the smart contract. The EVM specification defines a fee schedule for every operation or opcode. You can check the Ethereum wiki site for all opcodes supported by Ethereum. The following is a screenshot captured from the Ethereum Yellow Paper. For more details, please check out the complete Paper from the previously provided link:

Ethereum blockchain

In addition to smart contract execution, the EVM also performs transactions between two EOAs to transfer ethers from one to another account. In all cases, whenever there is a transaction submitted to the Ethereum network, as the Ethereum mining node, the EVM and Ethereum client perform the mining operations and add new transactions to the blockchain. We will talk about mining in our upcoming article.

If you come from a Java development background, you may remember the Java Just-In- Time (JIT) compiler and its advantage in Java runtime. It is a feature that compiles the bytecode into the machine code instructions of the running machine. This makes sense since, from the bottom line machine execution view, it was machine code being executed, not the bytecode. By doing the initial compilation, it avoids instant interpretation from bytecode to machine code at execution time, hence improving the performance of Java applications.

Some newer EVM implementations support a JIT compiler too, including Parity and Geth. Similar to Java JIT, EVM JIT is a library for the just-in-time compilation of EVM bytecode. Before the EVM can run any code, it must first compile the bytecode into components that can be understood by the JIT-enabled EVM. By compiling the bytecode into logical pieces, the JIT can analyze the code more precisely and optimize where and whenever necessary. It may seem slower at the beginning since it needs to check whether the JIT-compiled code is in the library and compile the bytecodes if it is not. Interested readers can check out both the Go Ethereum and Parity sites for more details.

 

Next Article

In our next article (How address and wallet work in Ethereum), we discuss how Ethereum address and wallet work.

 

This article is written in collaboration with Brian Wu who is a leading author of “Learn Ethereum: Build your own decentralized applications with Ethereum and smart contracts” book. He has written 7 books on blockchain development.

Resources

Free Webinars on Blockchain

Here is the list of our free webinars that are highly recommended:

 

Free Courses

Here is the list of our 10 free self-paced courses that are highly recommended:

 

Self-Paced Blockchain Courses

If you like to learn more about Hyperledger Fabric, Hyperledger Sawtooth, Ethereum or Corda, taking the following self-paced classes is highly recommended:

  1. Intro to Blockchain Technology
  2. Blockchain Management in Hyperledger for System Admins
  3. Hyperledger Fabric for Developers
  4. Intro to Blockchain Cybersecurity
  5. Learn Solidity Programming by Examples
  6. Introduction to Ethereum Blockchain Development
  7. Learn Blockchain Dev with Corda R3
  8. Intro to Hyperledger Sawtooth for System Admins

 

Live Blockchain Courses

If you want to master Hyperledger Fabric, Ethereum or Corda, taking the following live classes is highly recommended:

 

Articles and Tutorials on Blockchain Technology

If you like to learn more about blockchain technology and how it works, reading the following articles is highly recommended:

 

Articles and Tutorials on Ethereum and Solidity

If you like to learn more about blockchain development in Ethereum with Solidity, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Hyperledger Family

If you like to learn more about blockchain development with Hyperledger, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on R3 Corda

If you like to learn more about blockchain development on Corda , reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Other Blockchain Platforms

If you like to learn more about blockchain development in other platforms, reading the following articles and tutorials is highly recommended: