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:
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.
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:
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 3 allow Ethereum to upload the code on the blockchain and the creation of the accounts in the world state through the Ethereum mining process:
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:
“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:
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:
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:
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.
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.
Free Webinars on Blockchain
Here is the list of our free webinars that are highly recommended:
- Hyperledger Fabric for system admin versus developers
- How to harness blockchain for environmental and corporate sustainability
- Review of Initial Coin Offering, Security Token Offering and asset tokenization use cases and best practices
- Hyperledger Fabric Deployment on Cloud
- Hyperledger Fabric for entrepreneurship- 21 blockchain business use cases
Here is the list of our 10 free self-paced courses that are highly recommended:
- IT Career Roadmap Explained
- Web Design with Bootstrap
- User Experience Best Practices
- Intro to Search Engine Optimization
- Web Design with WordPress
- Introduction to Drupal CMS
- Intro to Joomla CMS
- Intro to Cybersecurity
- Introduction to Cloud Technology
- Recorded Live Webinars and Classes
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:
- Intro to Blockchain Technology
- Blockchain Management in Hyperledger for System Admins
- Hyperledger Fabric for Developers
- Intro to Blockchain Cybersecurity
- Learn Solidity Programming by Examples
- Introduction to Ethereum Blockchain Development
- Learn Blockchain Dev with Corda R3
- 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:
- Live and self-paced blockchain development with Ethereum
- Live and self-paced blockchain development with Hyperledger Fabric
- Live and self-paced blockchain development with Corda
- Immersive Blockchain Bootcamp with live and self-paced courses
- Live crash course for learning Ethereum with Solidity
- Live crash course for building DApps with Hyperledger Fabric
- Live crash course for building DApps with Corda
- Live full-stack blockchain development in Hyperledger and Ethereum
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:
- History and Evolution of Blockchain Technology from Bitcoin
- Overview of Blockchain evolution and phases from Ethereum to Hyperledger
- Comprehensive overview and analysis of blockchain use cases in many industries
- Blockchain Crowdfunding Security Token or Initial Coin Offerings
- A beginner Guide to Blockchain Technology
- How Decentralized Peer-To-Peer Network Works
- How blocks are added to the blockchain
- How Public and Private Keys of Cryptography Work
- What Is A Cryptographic Hash Function
- How Digital Signature Works In Blockchain
- The role and types of consensus mechanism in blockchain
- How Proof-of-Work Consensus Works in Blockchain
- How Proof of Stake Consensus works in Blockchain
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:
- Review of Architecture and Components of Ethereum
- Comprehensive Blockchain Ethereum Developer Guide from Beginner to Advance Level
- How to Write Ethereum Smart Contracts with Solidity in 1 hour
- Review of Architecture and Components of Ethereum
- How Ethereum Manages Accounts
- How Ethereum Manages Transactions
- How Smart Contracts Work in Ethereum
- How Ether and Gas Work in Ethereum
- How Ethereum Virtual Machine works
- How address and wallet work in Ethereum
- How mining works in Ethereum
- List of Tools and Technologies in Ethereum Ecosystem
- Review of challenges in distributed systems
- Review of Cap Theorem in Distributed Systems
- Horizontal Scaling versus Vertical Scaling in Distributed Systems
- How to Scale up Ethereum Blockchain Applications
- Review of scaling solutions for Ethereum
- How to Manage Ethereum State Channel with Raiden
- How Plasma Chains Work in Ethereum
- How Sharding and Gasper work in Ethereum
- How Proof-of-Stack Consensus Works in Ethereum
- A roadmap for Implementing Ethereum 2.0
- How to work with Decentralized Data and Content Storage in Ethereum
- How Decentralized Messaging with Whisper Works in Ethereum
- Review of Infura for Ethereum Development
- Review of Infura Ethereum API
- How to Use Remix with Infura for Ethereum Development
- How Ethereum Client API Works
- How Ethereum IPFS Storage Works
- How to Install and Start Ethereum IPFS Storage
- How to Run Ethereum IPFS Storage
- How to Work with Ethereum Swarm Storage
- How to Install Ethereum Swarm Storage
- How to Handle Ethereum Messages with Whisper
- Review of Popular Ethereum Smart Contract Libraries
- Review of Private and Permissioned blockchain
- How to Set up a Local Private Ethereum Blockchain
- How to Run Geth on a Local Private Ethereum Blockchain
- How to Build a Local Private Ethereum Blockchain with Mining
- How to Run Geth on a Local Private Ethereum Blockchain with Mining
- How to Create an Account on a Local Private Ethereum Blockchain
- How to Use Ethereum Optional Flags with New Chains
- Review of Ethereum Options for Development and Testing
- Review of Ethereum Developer Chain Options
- Review of Ethereum API and Console Options
- Review of Ethereum Networking Options
- Review of Ethereum Transaction Pool Options
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:
- Introduction to Hyperledger Architecture, Projects, Tools and Libraries
- Complete Review of Hyperledger Fabric Architecture and Components
- Hyperledger Fabric for System Administers versus Developers
- How to use Prometheus and Grafana to monitor Hyperledger Fabric
- Blockchain Developer Guide- How to Install Hyperledger Fabric on AWS
- Blockchain Developer Guide- How to Install and work with Hyperledger Sawtooth
- Blockchain Developer Guide- How to Install Hyperledger Burrow on AWS
- Blockchain Developer Guide- How to Install Hyperledger Iroha on AWS
- Blockchain Developer Guide- How to Install Hyperledger Indy and Indy CLI on AWS
- Blockchain Developer Guide- How to Install Hyperledger Seth and Docker on AWS
- Blockchain Developer Guide- How to Configure Hyperledger Sawtooth Validator and REST API on AWS
- Blockchain Developer Guide- How to Build Transaction Processor as a Service and Python Egg for Hyperledger Sawtooth
- Blockchain Developer Guide- How to Deploy Ethereum Smart Contracts with Hyperledger Burrow
- Blockchain Developer Guide- How to Create Cryptocurrency Using Hyperledger Iroha CLI
- Blockchain Developer Guide- How to Explore Hyperledger Indy Command Line Interface
- Blockchain Developer Guide- Comprehensive Blockchain Hyperledger Developer Guide from Beginner to Advance Level
- Introduction to Hyperledger Sawtooth Blockchain Development
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: