How to build Ethereum blockchain applications
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.
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:
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:
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:
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):
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.
Here is the list of our free webinars that are highly recommended:
Here is the list of our 10 free self-paced courses that are highly recommended:
If you like to learn more about Hyperledger Fabric, Hyperledger Sawtooth, Ethereum or Corda, taking the following self-paced classes is highly recommended:
If you want to master Hyperledger Fabric, Ethereum or Corda, taking the following live classes is highly recommended:
If you like to learn more about blockchain technology and how it works, reading the following articles is highly recommended:
If you like to learn more about blockchain development in Ethereum with Solidity, reading the following articles and tutorials is highly recommended:
If you like to learn more about blockchain development with Hyperledger, reading the following articles and tutorials is highly recommended:
If you like to learn more about blockchain development on Corda , reading the following articles and tutorials is highly recommended:
If you like to learn more about blockchain development in other platforms, reading the following articles and tutorials is highly recommended:
We offer private custom tutoring classes both online and in DC, MD and VA for almost all of our courses or bootcamps. Give us a call or email us to discuss your needs.
$50 Limited OfferREGISTER NOW