Understanding The Web

Ethereum Bootcamp: Introduction to Ethereum

Posted

After having built a foundation of blockchain cryptography and blockchain data structures, the third week of Alchemy University’s Ethereum bootcamp introduces students to Ethereum.  Ethereum has key features that are important to understand before jumping into smart contracts.  Ethereum is computer with a built-in virtual machine.  Ethereum was initially built with a proof of work consensus model that has since been changed to proof of stake.  Ethereum uses “gas” to perform actions within the network.  Sending requests to the Ethereum network is a fundamental operation that all developers should be aware.  Understanding that interacting with Ethereum involves transactions is a foundational concept that will aid in smart contract development.  Front-end libraries exist that aid in abstracting away low level details from smart contract development.  With this knowledge, students will be set up to be proficient smart contract developers.        

Ethereum Features

What is Ethereum

Ethereum is a globally decentralized computing infrastructure that executes smart contracts.  Ethereum uses blockchain technology to synchronize and store system changes while using a cryptocurrency to meter and constrain execution costs.  All the nodes in Ethereum follow the same set of rules which is what allows them to collectively maintain and update the network.

Ethereum = Computer

Ethereum can be viewed as a computer.  It functions as an extremely slow computer.  Computers are intuitively thought of as physical machines, however Ethereum does not reside in a single machine or maintain a physical presence in the world.  Computation and storage on the Ethereum computer requires a high cost in memory.

Properties of the Ethereum Computer

Just as Ethereum does not reside in any single machine, there is no central authority that owns Ethereum with the power to shut it down.  Where there is internet, Ethereum can be utilized.  There are no barriers to participation, but there is a requirement to own the native currency, ether, in order to pay gas fees.  The programming language to interact with Ethereum is Solidity, and it is very similar to JavaScript.  Any code that is deployed to the Ethereum network inherits the cryptographic properties of the Ethereum computer. 

The purpose of Ethereum isn’t to be fast or cheap. The purpose is to be trustworthy. Any program that runs on Ethereum is guaranteed to run the same everywhere.  The data stored on the Ethereum computer is permanent and available everywhere.

The EVM

A key difference between Ethereum and Bitcoin is that Ethereum has a virtual machine built into it that supports Turing complete languages.  Programs written in Turing Complete languages have a property that makes it impossible to tell if the program will ever terminate.  In contrast, Bitcoin’s language, Script, is intentionally not Turing complete as it is restricted to true/false evaluations.  Bitcoin was created in this way to avoid code that caused an infinite loop. Code that results in an infinite loop would cause all of the nodes in the Ethereum network to be stuck if not for the Ethereum Virtual Machine (EVM).

An infinite loop would lead to a type of attack known as denial of service.  To account for these types of attacks, the EVM associates a price with each operation that occurs on it’s network.  This price is paid in Ethereum’s native currency, ether (ETH), and is known as gas.  Additionally, the EVM has the ability to “upgrade” through forks. Each update is specified as an Ethereum Improvement Proposal (EIP).  Some upgrades are planned while others are in response to an attack.  The Tangerine Whistle EIP was in response to a DOS attack.  The DAO Fork EIP wasn’t accepted by all members of the community leading to two separate versions of Ethereum with Ethereum Classic rejecting the changes from this EIP.

Proof of Stake

Another important aspect of Ethereum is the consensus mechanism.  Like Bitcoin, Ethereum was initially a proof of work blockchain. As planned, Ethereum transitioned to a proof of stake consensus (PoS) model in 2022.  PoS is less energy intensive and has the potential to make Ethereum more secure and scalable.  The proof of work model is a protocol in which nodes compete to propose a block.  This competitive nature is what creates such a high energy demand.  In contrast, the PoS model randomly chooses a validator to propose the next block every 12 seconds.  Validators in this PoS setting do not require elite hardware as in a competitive proof of stake environment.  In Ethereum, the requirement to become a validator is 32 ETH.  The 32 ETH is “staked” to a contract which is used as collateral against bad actors in the network.  These conditions lead to less energy requirements and allows for easier participation as a node. 

The PoS model also has implications for development. A new framework for block finality was introduced. Finality is the confidence in which a block will not get changed or forked away.  The older the block, the higher the finality.  This means that there is a less of a chance the block will get removed from the “canonical chain”. Two new levels of block finality were introduced: safe and finalized.  There are various block tags that can define a block: earliest, finalized, safe, latest, pending.  Each of the block tags have varying levels of finality with earliest blocks having the highest level of finality and pending blocks which have not been mined yet.

Gas on Ethereum

It is important to understand the mechanics of gas  as every operation that occurs on the EVM has an associated gas price.  Gas prices change on each block based on demand. EIP-1559 proposed to improve the calculation of gas prices.

Ether (ETH) has different denominations to express small values similar to how the dollar can be broken into pennies. As 1 dollar is equal to 100 pennies, 1 ether is equal to 10^18 Wei (the smallest denomination of Ether) or 10^9 Gwei.

Gas is often listed in Gwei but that can change based on demand.  Every block has a maximum number of gas that can be used within it.  Every block has the capacity to use 30 million gas with a target of 15 million.  As transactions fill a block, the amount of gas used determines the price of gas for the next block.  If the gas used in a block exceed 15 million, the price of gas goes up.  If the gas used is below 15 million, the price of gas goes down.

Accounts in Ethereum

There are two types of accounts in Ethereum: externally owned accounts and contract accounts.  Externally owned accounts (EOAs) are similar to Bitcoin private/public key pairs.  Addresses and public keys are associated to a private key through an Eliptical Curve Digital Signature.

Unlike the UTXO model used with Bitcoin, Ethereum follows an account model.  This means that each Ethereum account has an associated balance.  The global state of Ethereum tracks how much ether each active account holds. 

The second type of account in the Ethereum network is a contract account.  Smart contracts are programs that run in the blockchain execution environment.  Smart contracts on the Ethereum network are written in a high level programming language such as Solidity or Vyper.  Once a smart contract is deployed to the Ethereum blockchain, it exists as an account. Unlike externally owned accounts, smart contracts are not controlled by private keys.  Externally owned accounts can interact with smart contracts and call functions on the smart contract which in turn can allow the smart contract to interact with other smart contracts.  Once a contract is deployed, the code cannot be changed.  The storage is persistent and the contract can be updated through transactions.   

Reading Data From Ethereum

Intro to JSON RPC

Ethereum is essentially a computer that is spread out over thousands of nodes.  All the nodes carry the same instance of Ethereum.  In order to run an Ethereum node, one of these client implementations are required.  Communicating with any one of these nodes will result in the same information.  The method of communication required to interact with the Ethereum node is known as JSON-RPC.  It is a remote procedural call (RPC) that uses JSON to encode messages.  JSON-RPC is a type of application programming interface (API) standard.  It deals exclusively with transporting data in the syntax form of JSON.  All Ethereum nodes contain a JSON-RPC interface. This interface governs a standard method that can be used to retrieve information from the Ethereum blockchain.

JSON RPC FLOW

JSON-RPC works in terms of requests and responses.  The client sends the request and the Ethereum node sends a response.  On the client side, a user will make a request that could be tied to an interaction such as clicking a button.  The web wallet that the user interacts with will act as the provider sending a request to the Ethereum node.  The Ethereum node will receive the request, run the method that was called, and return a response.

In the image above, a request is made to get the balance of the account provided in the parameters.

The response to such a request would look like the image above.

Ethereum Nodes

Ethereum nodes maintain the integrity and data on the network. There are different types of nodes that serve different purposes.  Full nodes validate all transactions in each block providing security and data accuracy.  Light nodes store a minimal amount of data such as block headers and timestamps.  Archive nodes are full nodes that also store the complete historical data of the Ethereum network. 

Ethereum Transactions

A transaction based state machine

JSON-RPC requests allow users to read information from the Ethereum blockchain.  These requests don’t cause any change. They are methods to observe the Ethereum state. Transactions are the way in which data is written. Writing data changes the state of the Ethereum computer.  The Ethereum computer exists to facilitate transactions. 

What is a transaction

An Ethereum transaction is an action initiated by an EOA.  It is a single cryptographically signed instruction.  A transaction signals intent from the owner of an EOA to change Ethereum state.  While reading data from the Ethereum computer can be done by anyone, writing data requires access to the private keys of an EOA.

Two types of Transactions

There are two types of transactions in Ethereum: contract creation and message calls.  Contract creation is a type of contract that that deploys a new smart contract.  This type of transaction creates a new entry in the Ethereum world state.  A message call is a transaction initiated by an EOA that interacts with another EOA or smart contract.  Message calls do not create a new state. Instead, an existing entry in the Ethereum world state is updated.

At the heart of the blockchain is the transaction. A blockchain can be simplified to a globally shared transaction database.  The data base is decentralized and peer to peer.  When new blocks are created in the blockchain, the block is verified by every node in the peer to peer network. 

Interacting with the network

Along with contract creation and message calls, users can simply inspect the network.  Inspection does not require an account and can be done by anyone.  Contract creation and message calls involved signed JSON-RPC requests while inspection does not.

Front-End Libraries

Introduction to Front-end libraries

Without front-end libraries, developers would have to make raw API calls for fundamental actions such as smart contract deployment, wallet creation, signing transactions, and querying the blockchain.  Front-end libraries work to take away low level abstractions so developers can focus on other aspects of development. 

Ethers.js

Two popular front-end libraries are ethers.js and web3.js.  Ethers.js is a newer library that can do everything web3.js can do. Ethers.js is open source allowing developers to use it in any way they would like while web3.js requires modifications to be documented. Ethers.js is very small in size which makes it ideal for performance. 

One of the drawbacks of ethers.js is that it is so new.  It hasn’t been used in foundational, older projects.  Older projects would require a broader understanding of lower level aspects of development. 

However, ethers.js is ideal for new projects.  It is very popular and has a lot of community support in the form of tutorials.  Ethers.js provides three important class abstractions that make the development process easier: Provider, Wallet, and Contract.  By taking care of a lot of the low level code involved with these classes, developers can focus on other aspects of the project.

Conclusion

The Ethereum virtual machine allows developers to communicate with and manipulate the Ethereum world state through smart contracts.  Understanding fundamental concepts such as the mechanics of gas and the different types of transactions provide developers with insight that will produce efficient code.  Using a front-end library such as ethers.js allows developers to focus on ideas rather than worrying about the nuances of low-level code.  The third week of the Ethereum bootcamp provides essential knowledge that will be foundational in understanding how to create smart contracts.