© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. Wu, B. WuBlockchain for Teenshttps://doi.org/10.1007/978-1-4842-8808-5_4

4. Ethereum: A Gateway to Cryptocurrency

Brian Wu1   and Bridget Wu1
(1)
Livingston, NJ, USA
 

Alan Turing, a mathematician, logician, and computer scientist, is widely considered to be the father of computer science. In the 1930s, he invented the Universal Turing Machine. Assuming enough memory is available, the Turing Machine could calculate anything using only two symbols (0 or 1) arranged in a potentially infinite one-dimensional sequence. This is the basis for the first computer. Turing-completeness, therefore, refers to any computation problem that can be solved and implemented in a Turing-complete environment, no matter how complex.

Ethereum, the second-largest cryptocurrency after Bitcoin, is considered a distributed Turing machine. It introduced a built-in Turing-complete programming language—smart contract, which can be used for creating various decentralized applications (also called Dapps).

The previous chapter introduced Bitcoin as the first embodiment of blockchain technology and the world’s most popular cryptocurrency.

In this chapter, we’ll continue exploring the Ethereum blockchain, built as a Turing-complete blockchain. The chapter begins with the history of Ethereum. From there, we cover many Ethereum basic concepts and elementary operations, including Ether, Gas, and Ethereum Account. Then, for a deeper and more comprehensive understanding of Ethereum, we explore a big-picture overview of the Ethereum Virtual Machine (EVM). We will also see the important Ethereum clients and node implementations. Finally, we discuss how Ethereum works and explores the internal Ethereum architecture.

One of this chapter’s goals is to help you acquire the necessary technical background to understand Ethereum mechanics and get you ready to develop your first decentralized application in the next chapter.

This chapter is organized around a few major topics:
  • The history of Ethereum

  • Getting to know Ethereum

  • How Ethereum works

The History of Ethereum

Vitalik Buterin is a Russian-Canadian writer and programmer who has been involved in Bitcoin and crypto since 2011 just two years after Bitcoin was created. Vitalik became a writer, earning 5 Bitcoins for every post for the Bitcoin Magazine website. Soon, he became the co-founder of Bitcoin Magazine. Having improved his understanding of Bitcoin, Buterin became a Bitcoin expert and realized the limited functionality of Bitcoin. In 2013, for six months, Vitalik traveled around the world to learn, meet, and speak with Bitcoin developers. He recognized that he could build a new, potentially better version of blockchain by iterating on the Bitcoin.

Whitepaper Released (November 2013)

In November 2013, Vitalik, just 19 years old, published a white paper entitled Ethereum: A Next-Generation Smart Contract and Decentralized Application Platform, exploring the general idea of Ethereum.

The white paper explaining the concept of Ethereum that includes the following:
  • It provides a built-in Turing-complete programming language that can be used to create a “smart contract”—simply a self-executing program that runs on the blockchain.

  • It establishes peer-to-peer transactions in the blockchain. The platform can create and build a smart contract and decentralized application, allowing anyone to define, create, and exchange types of value: cryptocurrencies, shares, and many other assets.

Yellow Paper Released (April 2014)

In April 2014, Dr. Gavin Wood published the Ethereum yellow paper, giving a technical definition of the Ethereum protocol—Ethereum: a secure decentralized generalized transaction ledger, which describes a technical definition of the Ethereum protocol.

The Birth of Ethereum (July 2014)

Ethereum was publicly announced at the North American Bitcoin Conference in Miami in January 2014. Ethereum Foundation, as a nonprofit organization, was formed on July 6th, 2014, in Zug, Switzerland. Ethereum’s founding members were Vitalik Buterin, Gavin Wood, Charles Hoskinson, Anthony Di Iorio, Mihai Alise, and Joe Lubin.

Launching the Ether Sale (July–September 2014)

On July 20th, 2014, the Ethereum Foundation launched a 42-day crowdfunding campaign. On September 2nd, 2014, the public crowd sale ended. Ethereum foundation raised 31,591 Bitcoin, that’s about $18 million at the time of the sale’s close.

Ethereum Released (June 2015)

During 2014 and 2015, many proofs-of-concept were developed. “Olympic” was the ninth and last prototype. On June 30, 2015, Ethereum went live, and the first “genesis block” was created.

DAO Attack (July 2016)

In May 2016, a Decentralised Anonymous Organisation (DAO) was created on the Ethereum blockchain by developers. The DAO uses a smart contract to self-govern and automate decisions without a typical centralized management structure. Anyone has the right to participate and vote regardless of their location. The first DAO crowd sale is very successful. It raised a record 12.7M Ether (worth around $150 million at the time) to fund the project.

However, on June 17, 2016, a hacker exploited some vulnerabilities in the smart contract of the DAO. The hacker was able to invoke the DAO smart contract to give the Ether back multiple times before the smart contract could update its balance. The hackers managed to steal more than 3.6 million ETH (worth around $50 million at the time).

Because of the huge amount of funds lost among DAO investors, the Ethereum community decided to reverse the attack to refund the lost money, which lead to Ethereum forked into two blockchains. One is the current Ethereum blockchain. The token owners were given an exchange rate of 1 ETH to 100 DAO tokens, the same rate as the initial offering. The lost funds of the DAO investors were recovered. In September 2016, digital exchange currencies de-listed the DAO token. In the meantime, part of the Ethereum community did not agree with the hard fork and decided to continue to maintain the old blockchain, which is now known as Ethereum Classic.

Ethereum 2.0 (The Merge)

In recent years, the Ethereum community started to migrate from Ethereum 1.0 to Ethereum 2.0, also known as Eth2, The Merge, or Serenity. Ethereum 1.0 was created based on a proof-of-work blockchain. Compared to Ethereum 1.0, 2.0 has several major advantages. Table 4-1 shows these differences.
Table 4-1

Comparing Ethereum 1.0 and 2.0

 

Ethereum 1.0

Ethereum 2.0

Consensus

Uses proof-of-work (PoW) consensus.

Uses proof-of-stake (PoS) consensus.

Speed

The network can process around 15 transactions per second (15 TPS); it often causes network congestion and delay.

The ETH 2.0 network will scale up to a potential 100,000 TPS, compared to Visa, 30,000 TPS.

Energy

Proof-of-Work requires miners to consume large amounts of computing power to solve a complex mathematical puzzle.

Ethereum 2.0 use PoS Consensus by staking its tokens as a collateral asset to check and validate the transactions and add block. It requires minimum hardware power, which can reduce 99% fewer resources than the proof of work consensus.

Security

Some powerful group of miners could control more than 50% of the network’s activities which could lead to vulnerabilities like a 51% attack.

In Ethereum 2.0, it is more decentralized. The network requires around 16,384 validators. The user just needs to stake 32 ethers to participate in validating the Ethereum network. Even with less ether, users can join a mining pool that enables everyone to stake together and share the rewards. There are no miners controlling the blockchain.

Gas fee (transaction fees)

As the network can only handle a limited number of transactions per second, leading to high transaction fees known as “gas,” and slow transactions. Typically, avg gas fee per transaction is around $12. With demand rising sharply, the gas fee could be much higher, such as $100.

The Ethereum 2.0 uses PoS consensus to process transactions that require a nearly zero gas fee. So, it just charges some basic fee to avoid malicious activities on the Network.

There are three phases to launching Ethereum 2.0, it will take several years to completely roll out Ethereum 2.0 as Figure 4-1 shows.

An illustration of three phases of Ethereum 2.0 depicts Ethereum 1.0 to Ethereum 2.0 from phase 0 of beacon chain, to phase 1 of merge, and lastly phase 2 of shard chain.

Figure 4-1

Three phases of Ethereum 2.0

Phase 0 – Beacon Chain

Phase 0 started with the official launch of Beacon Chain in December 2020. Beacon Chain builds based PoS in the Ethereum network and manages the registry of validators.

Phase 1 – Merge

The Ethereum main net merged with the Beacon Chain in this phase. It officially ends the POW consensus on the network and starts with PoS. On September 15, 2022, Ethereum switched from the original proof-of-work mechanism to proof-of-stake, called “the merge.” The merge reduced Ethereum’s energy consumption by ~99.95%.

Users who staked Ethereum on the Beacon Chain can become validators.

Transactions and Dapps will continue to run the same behavior as before.

Phase 2 – Shard Chain

In this phase, Shard Chains will introduce to the network. At the same time, the Ethereum main network will be split into 64 shards. Each shard can run a fully functional smart contract. The network allows each shard to communicate with each other.

Getting to Know Ethereum

In the Bitcoin network, Bitcoin is digital gold, designed as a medium of exchange and a way to store value. Bitcoin provides a payment way for people who can transfer value from one to another using a decentralized way without a central bank. On the other hand, the Ethereum network is built as a Turing-complete blockchain. The network was built in a Turing-complete programming language called Solidity, which can run in the Ethereum Virtual Machine (EVM). Users can create and run decentralized applications (Dapps) in EVM. EVM is where all Ethereum accounts and smart contracts live. As the native currency in the Ethereum blockchain, Ether was used internally by Dapp in the Ethereum network to process transactions. For example, when a smart contract executes a transaction, Dapp must pay a gas fee (Ether) for the miner to perform mining work.

Ether (Unit)

Each blockchain has its own native currency. Similar to Bitcoin, the native currency is called Ether (ETH) in the Ethereum blockchain.

Ether acts as the “fuel,” to pay for the execution of smart contracts on the EVM. When miners solve the computational puzzle, they will get Ether as a currency reward. Ether can also be used for payments and users can send ether to other users as payment.

Like the US dollar, which come in seven denominations: $1, $2, $5, $10, $20, $50, and $100, Ether is broken into denominations. The smallest denomination unit of ether is called Wei, named after a digital money and cryptography pioneer, Wei Dai. Wei created Crypto++ cryptographic library and invented B-money. Other units include a Gwei, Mwei, Kwei, microether, and milliether. They are known by other names as well. For example, a milliether is also called Finney, named after another digital money pioneer, Harold Thomas Finney II, who in 2004 implemented the world’s first reusable proofs of work system (RPOW) before Bitcoin. In January 2009, Finney was the recipient of the Bitcoin network’s first transaction. Table 4-2 lists the named denominations of Ether and other units:
Table 4-2

Ether denominations and unit name

Unit Name

Value (in Wei)

Ether

Wei

1 wei

10-18 ETH

Kilowei (Babbage)

1,000 wei

10-15 ETH

Mwei (Lovelace)

106 wei

10-12 ETH

Gwei (Shannon)

109 wei

10-9 ETH

Microether (Szabo)

1012 wei

10-6 ETH

Millether (Finny)

1015 wei

0.001 ETH

Ether

1018 wei

1 ETH

Gas, Gas Price, and Gas Limit

Like Bitcoin, Ethereum currently uses a proof-of-work (PoW) consensus mechanism. It required miners to compute and solve complex mathematical puzzles, verify transactions, and create a block of transactions to be added to the blockchain. A miner is rewarded with Ether (ETH). When users submit transactions, they need to pay Ether for the miner to execute such work.

According to coinwarz.com, assume you use a machine with a hash rate of 750.00 MH/s (Megahashes per second) and 1350 power consumption in watts. You need about $20,000. Assume the cost of electricity is $0.10 per kWh (depending on your location, the average is $0.14/kWh in the United States). It will take up to around 103 days to mine one Ether. The current ether price is around $1100, it will take a while to become profitable. Figure 4-2 shows the result of the Ethereum mining calculator.

3 radial gauge of mining revenue, mining fees, and electricity costs along with values for Ethereum mining hashrate, power consumption, electricity costs, pool fees, and E T H mined per hour and day.

Figure 4-2

Ethereum mining calculator

Gas refers to such fees required to execute a transaction on Ethereum successfully, that is, the fees that need to be paid to miners for processing transactions.

One of the commonly used Ether units is Gwei. It is used to specify Ethereum gas prices and pay for transaction fees. For example, one Gwei is the same as 0.000000001 ETH. If a transaction cost is 0.000000050 ETH, we can say that the cost was 50 Gwei.

In the current Ethereum blockchain, the standard transaction fee is 21,000 Gwei. The gas fee can be calculated using the following formula:

Total Fee = Gas unit (limits) * (Base fee + Tip)

Gas units (limits) – This refers to the maximum amount of gas you want to spend to execute a transaction.

Base fee – It refers to a minimum amount of gas fee that requires a user transaction to be included in a block. The base fee amount is automatically dynamic and calculated by Ethereum based on market demand at any given time.

Tips – Also known as a priority fee, a miner tip. It is an optional fee, determined by the user and directly paid to miners. The priority fee helps your transaction can be picked and processed faster by the miner.

Let’s take a real Ethereum blockchain transaction as an example (see Figure 4-3, the data is from etherscan.io) and calculate the cost of an Ethereum transaction today.

An image represents the E T H transaction cost consisting of status, block, timestamp, value, transaction fees, gas price, gas limit, and gas fees.

Figure 4-3

Ethereum transaction cost (etherscan.io)

From the preceding example, a transaction’s gas limit is 201,027 units, the base fee is 33.529 Gwei, and the priority fee is 1.101 Gwei; the total transaction fees to execute the transaction would be 0.006961 ETH (201,027* (33.529 + 1.101) = 2,310,000 Gwei). In the current market, one Ether is around $1100 so this transaction fee is about 0.006961* 1100 = $7.65.

It’s important to note that if you pay less than the required transaction fee, the transaction will be reverted, and you won’t receive your gas fee back because the miners have done the amount of mining work to process your transaction. They would collect the fee for their work even if the transaction failed. On the other hand, if you pay more gas fees, the extra gas fee will return to you once the transaction is complete. In the preceding example, there is field “usage by Txn” indicated that only 40.21% gas fee was used, the remaining fee will return to the user (500,000 - 201,027 = 298,973).

Ethereum Account

Each Ethereum account has an Ethereum address. Ethereum has two types of accounts: Externally Owned Account (EOA) and Contract Account.

Externally Owned Account (EOA)

An Externally Owned Account is controlled by a public/private key pair owned by a user. In Chapter 2, we have learned how the Ethereum address is derived from the public key using Keccak-256 hashes. Each Ethereum addresses are 42 hex string characters starting with 0x, representing a hexadecimal format. There is no cost to create an Ethereum account. The Externally Owned Account can be used for fund (Ether) transfer or send transactions to smart contracts. You need the private key to access your fund or send funds to other people.

Contract Account (CA)

A contract account is controlled by code executed by the Ethereum Virtual Machine. These codes are typically referred to as the smart contract code. Creating a contract account is costly because it will use network storage. A contract account can do Ether transfers and create Smart Contract accounts.

Figure 4-4 highlights the USDT token smart contract creator address, which is a contract account:

A screenshot of the U S D T contract account with many fields. On left, contract overview lists values of balance, ether value, and token. On the right, more info lists status of My name tag, contract creator, and token tracker. On top right is 4 pop-up buttons.

Figure 4-4

USDT Contract account (etherscan.io)

All accounts have four common fields:
  • Nonce – Each address has a nonce, which represents the transaction count of an account. Different account addresses can have the same nonce. The number used in nonce is unique to prevent double-spending situations.

  • Balance – It is the Ether owned by this account.

  • Storage hash – Sometimes it is called storageRoot. Each Ethereum contract account has its own storage trie (ordered tree data structure) where the contract data is present. StorageRoot is a Merkle Patricia trie of the current contact state as a mapping between 32-byte integers. A 256-bit hash value is calculated based on these contract data. The hash value will change anytime when its state charges. It could be used to verify the past state. Storage hash is only for contract accounts. Figure 4-5 represents Ethereum account storageRoot.

Account storage trie has circular diagram with of root node divided into 2 blocks and those 2 blocks divided into 2 blocks. Root node leads to storageRoot.

Figure 4-5

Ethereum account storageRoot

  • Code hash – It refers to your contract’s code under this account on the EVM. It’s immutable.

A rectangular block of Ethereum account has blocks of account address 1 to n. Account address 1 leads to account state that has 4 text circles for nonce, balance, storage hash, and code hash. Storage and code hash leads to E V M account and code respectively.

Figure 4-6

Ethereum account structure

Smart Contract

A contract is a written and oral arrangement between two or more parties to do certain things.

Here are just a few examples in our everyday life:
  • House rental contract – A lease agreement between a landlord who rents a property to a tenant in exchange for monthly payments.

  • Landscaping contract – An obligation between the landscaper and the client.

  • Software license agreements – An agreement between the software company and customers to offer the right to use software legally.

  • Personal loan – A written agreement between a lender that lends money to a borrower in exchange for a refund plus interest.

An effective contract will describe the formal requirements in detail, the responsibilities each party must follow, when and how contract items ought to be performed, and what happens when these rules are not followed. As a result, contracts act as a reliable document for each party expected to meet as planned.

Smart contracts are very similar, but the contracts are implemented for these detailed agreements by using computer code. They can’t be changed once created in a decentralized blockchain network. When the conditions are met, smart contracts will be executed automatically instead of by a third party. Since blockchain is decentralized, immutable, and transparent, everyone in the network can publicly verify the smart contract transaction result.

Nick Szabo was the first person to describe smart contracts. He published a paper The Idea of Smart Contracts in 1997. He imagined converting contracts into code to achieve self-enforcing contracts that removed the need for trust among the parties. To illustrate his concept, Nick used a vending machine to explain how smart contracts work. When you insert the correct amount of money into the machine, you get the desired product. The software instructions inside the vending machine guarantee that the contract will be fulfilled as intended. Today, this idea has now spread all over the world.

Ethereum is the most popular smart contract platform. Anyone can create smart contracts in the blockchain. The code is transparent and publicly verifiable. Everyone can see what kind of implementation logic is for smart contracts. Here is an example of SHIBA INU from etherscan.io.

A screenshot of the ethereum S H I B A I N U contract depicts a token application of the overview field select contract, and profile summary. At the top right there are 4 drop down icons.

Figure 4-7

Ethereum SHIBA INU contract example

In Ethereum, smart contracts are written in a variety of programming languages, including Solidity and Vyper. Solidity is the most popular Smart contract language used in Ethereum as we will explore more in a later section.

Each network computer node stores a copy of all existing smart contract codes and their current state alongside transaction data. A user is typically required to pay a gas fee to execute the function of the smart contract and include the transaction in a new block.

Ethereum Virtual Machine (EVM)

A “Virtual Machine” or “VM” is a simulated computer system you can use to run software on a physical computer. Virtual machines are essentially establishing an isolate level between a simulated computer system and the running operation system like Windows, macOS, or Linux. For instance, using “Parallels Desktop for Mac,” you can run Windows on your Apple Mac computer, as shown in Figure 4-8.

An image of a laptop showing windows running on a Mac laptop. Menu bar is in the top left corner and status bar is in the top right corner. 15 icons are present in the bottom row.

Figure 4-8

VM example – Window runs on Mac

The Ethereum Virtual Machine (EVM) is a Turing-complete virtual machine that allows EVM Byte Code runs on an isolated and sandboxed runtime environment. Bytecode is compiled from a high-level smart contract programming language such as Solidity.

On Ethereum, smart contracts are typically written in a high-level programming language called Solidity. Solidity compiler compiles smart contract into low-level binary machine code (Bytecode). A user just needs to send an Ethereum transaction containing these Bytecodes, and this transaction doesn’t need to specify any recipient. Once the contract transaction is committed to the blockchain, a new Ethereum account is created. The contract account stores the contract balance, the contract nonce, the code, and the data. The contract account storage hash is effectively a hash of the smart contract’s data. The creation of a contract address is determined based on the sender’s EOA address and nonce. RLP encoded nonce and EOA address data and hashed with keccak-256 algorithm. RLP (Recursive Length Prefix) is the way of encoding arbitrarily nested arrays of binary data. When you call a smart contract function, you interact with this contract address, and the contract storage Opcode will instruct EVM to execute the operation. The contract deployment process is illustrated in Figure 4-9.

A flow chart from a human figure flows through E O A, smart contract, bytecode and O P C O D E, contract address, and block diagram of E V M.

Figure 4-9

Smart contract deployment

Once the contract is compiled, the compiler will generate Abi, Bytecode, and Opcode. ABI (Application Binary Interface) is a JSON (JavaScript Object Notation) file that describes the deployed contract and its functions. It allows us to call contract functions externally. Bytecode and Opcode (operation code) are compact binary representations. They are stored on the blockchain and associated with a contract address.

EVM runtime environment will interpret Bytecode to correspond to a series of Opcode as a set of instructions and execute the Opcode when the user calls smart contracts. Figure 4-10 shows compiled contract Bytecode and Opcode example.

The bytecode and opcodes. They begin with object and opcodes respectively. The codes consist of numbers and alphabets.

Figure 4-10

Solidity compiled Bytecode and Opcode example

You can find the Opcodes reference for each Bytecode from Ethereum’s official website (https://ethereum.org/en/developers/docs/evm/opcodes/). There are around 148 unique Opcodes, which enable EVM to compute almost any task to make it a Turing-complete machine.

The Opcodes can be divided into the following categories:
  • Arithmetic operations, comparison, and bitwise logic operations (ADD, SUB, GT, LT, AND, OR, etc.)

  • Execution context inquiries (CALL, DELEGATECALL, CALLCODE, etc.)

  • Stack, memory, and storage access (POP, PUSH, UP, SWAP, LOAD, STORE, MSSTORE 8, M SIZE, etc.)

  • Control flow operations (STOP, RETURN, REVERT, etc.)

  • Logging, calling, and other operators (LOG0, LOG1, LOG2, etc.)

Let’s use the preceding Bytecode example to simulate EVM interpreter. We will focus on the first 16 bytes of the contract Bytecode: 6080604052348015. Based on Opcode reference, we can translate Bytecode to Opcode, as shown in Table 4-3.
Table 4-3

Bytecode to Opcode conversion (6080604052348015)

Bytecode

Opcode

Reference

(Opcode, Name)

Description

Gas

60 80

PUSH1 0x80

0x60 = PUSH1,

Duplicate 1st stack item

3

60 40

PUSH1 0x40

0x60 = PUSH1

Duplicate 1st stack item

3

52

MSTORE

0x52 = MSTORE

Save word to memory

3*

34

CALLVALUE

0x34 = CALLVALUE

Get deposited value by the instruction

2

80

DUP1

0x80 = DUP1

Duplicate 1st stack item

3

15

ISZERO

0x15 = ISZERO

Simple NOT operator

3

The EVM is a simple stack-based execution machine that will execute Opcode instructions. Stack (sometimes called a “push-down stack”) is a linear collection of items where the new element is inserted into the last position (referred to as the “top”), and the removal of existing items always takes place at the top position. It is also called Last In First Out (LIFO). In the preceding interpreted Opcode example, we will expect EVM will execute standard stack operations sequentially:

A flow diagram of bytecode to opcode conversion in a text-form with step-by-step process.

EVM stack has a depth of 1024 items, each item contains a 256-bit (32 bytes) word or 32 chunks where each chunk is 8-bit (1 byte) size. The reason for 256 bits is mainly to apply the Keccak-256 cryptographic hash function to any number of inputs and convert it to a unique 256 bits hash. In EVM, contract can store and read data in the above items. The EVM has three places where it can store items—storage, memory, and stack.

Memory is the location to hold temporary values for the short term. It will be erased between smart contract function calls.

When reading data from memory, EVM will use MLOAD. To write 32 bytes (256-bit) of data, Opcode MSTORE will be used. When writing 1 byte (8-bit) of data, EVM will use MSTORE8. Figure 4-11 represents how EVM uses Opcode to read and write contract memory.

A block diagram consists of stacked blocks of memory with push and pop. It has the label E V M stack. Inward and outward arrows label M S T O R E and M L O A D respectively.

Figure 4-11

Opcode read and write contract memory

Storage is where you save data permanently. When a smart contract running in EVM uses permanent storage, these data will be part of the Ethereum state. You can consider storage as an array in which each array item is 256 bits (32 bytes). There are no fees for externally reading the storage value. However, it is very expensive to write data into storage, Opcode is SSTORE and the current cost per one 32-byte word is 20,000 Gas. Let’s look at how much it costs.

First, convert gas (Gwei) to ETH - 20,000 * 0.000000001 ETH = 0.00002 ETH,

Then, calculate cost: $1100*0.00002 = $0.022.

Writing to storage is more than 6000x the cost compared to writing to memory.

In the previous table (Table 4-3), we list the first few steps of Opcode instruction with gas cost, and every Opcode has its own base gas cost. All Ethereum contract executions are run publicly. Attackers could create a contract to perform massive, expensive operations (DDoS – Distributed Denial of Service attack) to slow down the Ethereum network. By including gas cost in each EVM Opcode execution, the Ethereum network can prevent such attacks.

Ethereum Nodes

On the Internet, any system or device connected to the network is also called a node. The same goes for a blockchain network. When a node is connected to the Ethereum network, it will download a copy of the blockchain data and participate in the network, communicating with other nodes. Based on etherscan.io data, around 4,131,021 nodes are connected to the Ethereum network.

There are three types of nodes: full nodes, archive nodes, and light nodes. Each type of node consumes data differently.

Full Node

Full nodes will store all the recent blockchain data, run their own EVM environment, and can operate EVM instructions. They can be helpful when participating in blockchain transaction validation and maintaining the current state of the blockchain. When transactions in a new block do not comply with the rules defined in the Ethereum specifications, they will be discarded. For example, if Alice sends 50 ETH to Bob, but Alice’s account doesn’t have enough ether or pays a very little gas fee when a full node verifies transactions, it will mark this transaction as invalid and revert it. A full node can directly deploy a smart contract and interact with any smart contract in the network.

A full node stores a limited number of most recent blocks. The default is 128 (or 64 if you use the fast sync option). Each Ethereum block is typically around 80KB in size or around 4 MB in ten minutes. The 128 blocks are about a recent one week of trace data. When you query historical block data that is not accessible from a full node, you will generally get the “Missing trie node” error. The error means you need an archive node.

When a full node is the first time connected to the network, syncing full node data can be very time-consuming, it may take weeks to sync! After that node needs to stay online for block data upgrade and maintenance. Otherwise, it has to repeat the full synchronization process. It typically takes 13 seconds to create a new block. When new data arrives, a full node could delete old blockchain data to conserve disk space.

Hardware requirements to run a full node with fast sync:

Fast CPU with 4+ cores

16 GB+ RAM

Fast SSD with at least 500 GB of free space

25+ MBit/s bandwidth

Archive Node

Archive nodes run with a special configuration called “archive mode.” Archive nodes will store all the blockchain data since the genesis block. It also builds an archive of historical states.

Current archive Ethereum blockchain sizes are ~12 TB.

Typically, in most cases, we don’t need archive node data. A full node can provide most data, such as check account balances, transfer funds, etc. But sometimes, you may need to check last year’s account balance, assets you owned, or transactions. The full node prunes data periodically and only stores the most recent 128 blocks of data (about 25 minutes). The node has to resync to get earlier data, which would be too slow to extract. The archive node has all data locally, which can quickly get past data.

Archive node data are often used for blockchain services such as block explorers, data analysis, etc. Syncing full archive node data will be much longer than full node syncing. It may take at least one month.

These are the hardware requirements to run a full archive node:

Fast CPU with 4+ cores.

16 GB+ of RAM.

Fast SSD drive with at least 6 TB+ of space.

25 MBit/s+ bandwidth.

Light Node

Light nodes only download the minimum block headers information and use it to verify the data validity by checking the state roots in the block headers. Light nodes are designed to interact with full nodes as intermediaries and rely on full nodes to perform blockchain operations, from requesting account balance to smart contract interaction. So, light nodes don’t need to keep running online and store large amounts of Gigabytes of data locally. Light nodes can be very useful for running on low memory and computational devices, like mobile, IoT devices, and laptops.

Ethereum Clients

As we just learned, Light Clients are mostly implemented on mobile devices. Although setting up a full node or archive node will take a long time to sync, there are multiple benefits to running your own node:
  1. 1.

    Your node can be a network validator to verify all the transactions and blocks.

     
  2. 2.

    You can verify your application client transaction data by yourself, without the need for a third party to verify a transaction—“Don’t Trust. Verify.”

     
  3. 3.

    You will have a consistent view of the current network state and not need to rely on other public nodes or services where data could be delayed or untrusted.

     
  4. 4.

    You have more data privacy. When you use third-party software or tool to submit a transaction, these services could read your IP address along with your account information. IP addresses will reveal your current location.

     
To sync and communicate to the Ethereum network, you need to install Ethereum client software. The most used Ethereum clients are Geth. etherscan.io show that 90.3% of Ethereum node install Geth as an Ethereum client to join the network and establish a p2p communication channel with other nodes. Diagram 4-12 shows overall Ethereum client usage.

A pie chart consists of overall ETM client usage. The most used ETM clients are Geth at 90.3%. 8 other clients are estimated.

Figure 4-12

Overall Ethereum client usage

Geth (Go Ethereum) is an open source command line interface (cli) for running an Ethereum node written in Google’s programming language Go.

The Ethereum community built and maintained Go Ethereum.

Using Geth allows the node to perform transactions, mining, transfer ether between accounts, and deploy and interact with smart contracts on the Ethereum blockchain. Geth can be directly downloaded from Geth’s official website—https://geth.ethereum.org/downloads/. The site provides a standard installation guide.

Once installed Geth, you can run Geth in sync mode to become a full, light, archive node.

Geth command to sync full node:

geth --syncmode full

When sync mode is a full mode, Geth will download all blocks and replay all transactions from the genesis block. The state for full nodes will keep the last 128 blocks in memory.

Geth command to sync light node:

geth --syncmode light

When sync mode is a light mode, Geth will download the most recent 2,300 blocks and replay related transactions. As a result, the sync process for light mode is much faster than in full mode.

Geth command to sync archive node:

syncmode full --gcmode archive

Geth will download all blocks and replay all transactions from the genesis block and write all intermediate states to the archive disk.

There are many other Ethereum clients that are available in the Ethereum community. These clients are developed by different teams and implemented in different programming languages. All of these clients are actively used in the industry. Table 4-4 summarizes the different client’s usage.
Table 4-4

Ethereum clients

Client

Programing language

Disk size (fast sync)

Disk size (full archive)

Geth

Go

400 GB+

6 TB+

OpenEthereum

Rust

280 GB+

6 TB+

Besu

Java

750 GB+

5 TB+

Nethermind

C#, .NET

200 GB+

5 TB+

Geth has a JavaScript console built using the GoJa JS Virtual Machine.

Geth has a built-in JavaScript console and supports all standard web3 JSON-RPC APIs, called web3. js, which is compatible with ECMAScript 5.1. You can use JSON-RPC APIs to interact with your node. Geth supports multiple ways to let client applications send raw JSON objects to the node. One of the most widely used protocols is called JSON-RPC over HTTP. JSON stands for JavaScript Object Notation. It is an open standard file format that transmits data between a server and web application. The data are in key/value pairs separated by a comma in the JSON file. Here is an example:
{
    'name': 'Alice',
    'gender': 'Female',
    'account': 12345
}
RPC stands for “Remote Procedure Call” and is used for other remote system processes. When client applications use JSON-RPC over HTTP to send JSON data to Geth, Geth will execute specific tasks provided by Web3 API in the blockchain. Web3 is run on top of the RPC layer, as shown in Figure 4-13.

A schema depicts the interrelation between the client app, Ethereum client, full node E V M, and Ethereum blockchain.

Figure 4-13

Call Ethereum client via JSON-RPC

Geth Console

To start Geth JavaScript console, you can run the command—Geth attach with IPC. IPC (Inter-Process Communication) provides unrestricted access to all Web3 APIs. You can use IPC to connect when the Geth console is running on the same machine as the Geth node.

They opened the console from the running Geth instance, the result will look like this:

A code for Geth JavaScript console. It contains the labels instance, coinbase, at block, datadir, modules, and exit

To get support for web3 APIs including eth,personal, admin, and miner, Geth console provide web3 command. Let’s take a look at eth related API. Type eth in Geth console. It will show all supported eth commands.

A list of eth commands which starts with eth..

To list all of your current accounts in the network, simply run the following:
eth.accounts.

The listed account output should be similar to the following:

A text represents eth. account outputs in the console. It consists of numbers and letters.

To check the account balance, covert wei to ether, run the following command:
eth.getBalance('0x88437244acbb6276de36175740a8d686a9531ba7') to get wei
Or, we can directly convert to ether:
web3.fromWei(eth.getBalance('0x88437244acbb6276de36175740a8d686a9531ba7'),'ether')

An image consists of a code to get the account balance it starts from eth. get a balance and directly convert to the web3.

To get the latest block number of the blockchain, run the following command:

An image consists of a command to get the block number of the blockchain run command>eth. block number and displays 518560.

Then, you can display a matching block summary information by calling:
eth.getBlock (blockNumber)

An image of eth console of eth.getBlock opens a new window of eth. The command will appear in the console.

To exit the Geth console, just simply run exit or press CTRL-C.

Geth JSON-RPC via Command

cURL stands for “Client URL” and is a command-line tool for transferring data using various supported protocols (HTTP, IMAP, SCP, SFTP, SMTP, LDAP, FILE, and many others). The curl syntax is
curl [options] [URL...]
For example, you can open a window or mac terminal, type the below curl command, you will see HTTP response from the remote server:
curl -k www.apress.com/us
To start Geth in http mode, you can use the --http flag as follows:
geth –http

The default port is 8545. Once the Geth node starts, we can run the curl command to query some useful blockchain information.

To get the web3 client version, run the following curl command:
curl -X POST -H 'Content-Type: application/json'
--data '{'jsonrpc':'2.0','method':'web3_clientVersion','params':[],'id':11}'
http://localhost:8545

Here id - 11 is blockchain Id. The response result shows that web3_clientVersion is Geth/v1.10.17-stable-25c9b49f/linux-amd64/gol.18

An image of a code to get a web3 client that runs the curl command in a console and web3_clientVersion is in the console. The code has numbers and letters.

To check account balance, we saw in the previous Geth console example, running the following curl command:
curl -X POST
-H 'Content-Type: application/json'
--data '{'jsonrpc':'2.0','method':'eth_getBalance','params':['0x88437244acbb6276de36175740a8d686a9531ba7','latest'],'id':11}'
http://localhost:8545

An image of a code to check balance. It has texts in numbers and letters.

We get hex result - 0x56bbc5d759fba6400

By converting Hex value to decimal (www.binaryhexconverter.com/hex-to-decimal-converter),

An image of a hex value and a decimal value box. At the bottom left there is the convert icon.

And then, we convert Wei to Ether and divide the decimal number by 1018. The final result is 99.996902986 Ether, which is matched to the previous result we run in Geth console.

Geth Folder Structure

Once Geth installed, it stores the default Geth local data directory based on operating systems:
  • Mac: ~/Library/Ethereum

  • Linux: ~/.ethereum

  • Windows: %LOCALAPPDATA%Ethereum

It is structured as shown in Figure 4-14.

The structure of Geth folder consists of chain data, ancient, ethash, light chain data, node keys, nodes, Keystore, and geth.ipc.

Figure 4-14

Geth folder structure

chaindata – Directory of the downloaded blocks data and EVM state data.

ancient – When chaindata passes approximately 100k blocks, the past blocks are moved to the ancient directory.

ethash – Ethash is Ethereum’s Proof of Work hashing algorithm, the files under this location are part of the Ethereum mining computation. It can be regenerated and deleted safely.

lightchaindata– Contain a light version of the blockchain, just the receipts (not data) and content.

nodekey – File of public key used for other public peer nodes to connect or add a peer node to network.

nodes – Contains peer connection data, used to establish network at the start.

keystore – Stores account information, the account key can be found in the keystore folder.

geth.ipc – File for inter-process-communication as used by Geth connection.

By default, the Geth uses Google LevelDB as the underlying database implementation to store blockchain data, for example, 000002.ldb and 000011.ldb under chaindata folder in the Figure 4-14 folder structure. LevelDB is an open source on-disk key-value storage.

A flow diagram of E V M, flows from world state n to transaction and lastly world state n plus 1.

Figure 4-15

The state of Ethereum changing through a transaction

As we know from Chapter 2, “Cryptography,” every Ethereum account is defined by a private key and public key pair. The account’s address is derived from the public key by taking the last 20 bytes. When we use Geth to generate a new account, the new account address with the private key pair is encoded in a JSON text file as a keyfile. Since it contains your account’s private key, the file content is always encrypted. This keyfile can be used to access your Ethereum account and transfer fund. So, you need to back up your keyfiles regularly and make sure this file is secure and not accessible by others.

Let’s use Geth console to generate a new account address by running command personal.newAccount() and enter a passphrase:

An image consists of the Geth console generating a new account address and running commands personally. new account() and enter a passphrase are written.

The account address 0x0b1400031bea2def60a9d8f28fa373ab95d641f6 is generated. Now check keystore directory, there is a new JSON text keyfile generated for this account:
└── keystore
    ├── UTC--2022-06-28T04-14-13.945107969Z--0b1400031bea2def60a9d8f28fa373ab95d641f6

The content of this keyfile is encrypted, as displayed in the following:

A code to generate the account address in the console and new JSON text for that account and address, new versions, and OS is displayed in the console.

We have now learned Ethereum client—Geth and clarified how remote clients (command line or web app) can call the Ethereum client via web3j API and interact with the Ethereum blockchain.

Ethereum Network

Typically, when people are discussing the Ethereum network and ETH prices, they are talking about the Ethereum mainnet. The mainnet is the primary public Ethereum production blockchain. When we deploy a smart contract to mainnet, we have to pay gas fees, and these gas fees cost real money. Since the nodes connected in Ethereum run a protocol, there are many other similar controlled public environments that run the similar or same protocol to simulate the mainnet environments. Contract developers can run and test smart contracts in these production-like environments to ensure that the result is working as expected. These public networks we call Ethereum testnet.

In testnet, you don’t spend real money when testing your smart contracts. Ethereum testnet provides free ethers you can use to pay for gas fees. These ethers can only use in testnet, not for any other environments, and they have no value in the real world. As the best practice, you should test your smart contract code in the testnet before deploying it to the mainnet.

Many testnets use a proof-of-authority consensus mechanism. A smart number of nodes are chosen as a validator to do consensus work and create new blocks. Testnets do not incentivize proof-of-work mining. There are a few Ethereum test networks available. You can choose your own favorite testnet. Since the Ethereum 2.0 merged on Sep 15, 2022, few public proof-of-work and proof-of-authority testnets became proof-of-stake. Some popular testnet will soon be deprecated, like Ropsten, Rinkeby, and Kovan. We will not discuss these testnets here. Goerli testnet is a proof-of-authority testnet. It merged to proof-of-stake and is expected to be maintained long-term as a proof-of-stake testnet.

The Görli test network was established in March 2019. It is a Proof-of-Authority testnet using the Clique consensus mechanism for Ethereum, originally proposed by Chainsafe and Afri Schoedon. You can use the official Goerli Testnet Faucet (https://goerli-faucet.slock.it/) to acquire free ETH. In a later section, we will use Goerli Testnet to get some ether to our wallet.

Now, we have covered most of the basic Ethereum fundamentals. In the next section, we will go over how Ethereum works.

How Ethereum Works

As we learned earlier, there is a multi-phased upgrade that aims to switch from a current proof-of-work (PoW) consensus mechanism to a proof-of-stake (PoS) model. In PoW, the miner verified transactions and added transactions to a new block. The new block is broadcast to the entire network, and validators will verify and eventually commit to the Ethereum network. The process typically takes at a constant rate of between 10 and 20 seconds. In ETH 2.0 PoS, the network could speed up to 100,000 transactions per second capacity.

In Ethereum, a transaction is initiated and securely signed by a user’s EOA account. For example, Alice wants to send one Ether to bob. Alice will initiate a transaction for this transfer. Next, the Ethereum network transmits the transaction. Once the transfer completes, Bob’s account will be debited one Ether and Alice’s must be credited. Ethereum is a singleton world state machine. Transactions are the only things that can trigger a change and update the state of Ethereum. Figure 4-15 depicts the changing state of Ethereum in a transaction.

There are three types of transactions in Ethereum:
  1. 1.

    Transactions that transfer funds between two EOAs (e.g., Alice sends one Ether to Bob).

     
  2. 2.

    Transactions that deploy a contract.

     
  3. 3.

    Transactions that interact and execute a deployed contract function (e.g., update a total token supply).

     

The Structure of a Transaction

The blockchain requires a gas fee for miner mining and verifying when a user submits a transaction.

Let’s use Geth console to submit one transaction by transfer 0.05 Ether and then, get the transaction receipt from the returning transaction hash as shown in Figure 4-16.

A code to submit a transaction and to get a receipt. The code contains alphanumeric letters.

Figure 4-16

Submit a transaction and get a transaction receipt

From logs of Geth, we can see that transaction is submitted, a transaction hash, and a nonce is generated:

An image of the submitted Geth transaction and the transaction hash is generated with text and letters.

Figure 4-17 shows an example of the transaction detail from etherscan.io.

A screenshot of overview tab that lists transaction details like transaction hash, status, block, from and to, and value. State and comments tab are present.

Figure 4-17

Submit a transaction and get a transaction receipt

A transaction detail contains the following data:

From – The sender’s Ethereum address.

To – The receiver’s Ethereum address.

Nonce – The sequence number of a transaction. The Nonce is issued by the originating EOA, which initiates the transaction. It is a unique number and prevents to replay of the same transaction.

Gas Price – The required transaction fee in the price of gas (in Gwei) that the transaction creator pays.

Gas Limit – Maximum limit of gas that would be consumed for the transaction.

Value – The amount of ether to send to the recipient.

Data – Transaction input binary payload data that is only used for sending a message call and executing functions of the contract.

Signature – v, r, s. It is the identification of the sender. The sender uses EOA to sign the transaction via its private key. It uses a cryptographic ECDSA digital signature. v, r, and s are the values for the transaction’s signature.

Take your time to read this long list. You don’t need to memorize each one of the fields. We describe each field to help you to understand what they mean. These terms may quite often appear when you work on more in Ethereum.

Transaction Receipt

In Figure 4-16, we see the transaction receipt output after running web3.eth.getTransactionReceipt(transactionHash). When the transaction receipt becomes available, it means that the transaction is added to a block. When a transaction is a pending status, the receipt returns null.

Here are the fields that the transaction receipt contains:

BlockHash – Hash of the block where this transaction was in.

BlockNumber – This transaction block number.

TransactionHash – String, 32 Bytes—hash of the transaction.

TransactionIndex – The transactions index position in the block.

From – The sender’s Ethereum address.

To – The receiver’s Ethereum address. Null when it’s a contract creation transaction.

CumulativeGasUsed – The total amount of gas used by this transaction and all previous transactions in the same block.

GasUsed – The total amount of gas used by this specific transaction.

ContractAddress – The contract address is associated with this transaction. If the transaction was a contract creation, otherwise null.

Logs – Log information for this transaction.

Status – “0x0” indicates transaction failure, “0x1” indicates transaction succeeded.

Block

As we learned from Chapter 1, each block has header and body. Block body contains a list of transactions. This is true for both Bitcoin and Ethereum. Ethereum block structure is shown in Figure 4-18.

2 blocks of block header and block body. Block body contains 2 more blocks in it: transactions and uncle blocks headers.

Figure 4-18

Ethereum block structure

Ethereum block body also contains “ommer” blocks, usually called “uncle” blocks. Uncle blocks are created to help reward miners when multiple block solutions are found.

When there are multiple miners who solve a cryptographic puzzle and propose a new block for a chain, only one of the blocks will be accepted in the network. Since other miners did the same work, the network will reward them. Those stabled blocks will be attached to the new accepted block. We call it uncle block, as shown in Figure 4-19.

A flow diagram of 3 layers of cubes labeled 1, 2, and 3 from the top. Cube 1 leads to 2 cubes in second layer that are labeled 2 A and 2 B for Alice and Bob labeled respectively. There is a tick for 2 A and cross for 2 B. Cube 2 B goes to cube 3 through uncle for block 3.

Figure 4-19

Ethereum uncle block

For example, in Figure 4-19, there are two blocks that were proposed by miner Alice and miner Bob. Alice’s block (A) is eventually accepted and added as a new block #2. Bob’s block (B) is ultimately rejected. Then, a miner in the network created a block (#3) using Bob’s block (B) and specified that Alice’s block is the parent block and Bob’s block is an uncle/ommer block. This way, Alice will get the full reward, but Bob still gets a partial reward.

As we mentioned later, each block body contains the list of transactions, here is an example of a block from etherscan.io. We can see there are 374 transactions in this block as shown in Figure 4-20.

A screenshot of a table of transactions with column headers: T x n hash, method, block, age, from, to, value, and t x n fee. There are 374 transactions in this block.

Figure 4-20

A block: a list of transactions

The block number is 15039689. Let’s take a look at other block detail as in Figure 4-21.

An image of the ETM block in detail presents the overview and comments. An overview is a list of details of transactions, timestamps, difficulty, size, gas limit, and fees.

Figure 4-21

Ethereum block detail

The Block Header contains some key information about an Ethereum block. Each block header has the following important fields:
  • Block number – Also called block height. The length of the blockchain ancestor blocks. The first block (genesis block) has the number zero. This number represents the height of the chain.

  • Difficulty – Indicating how hard for hashing or staking effort to mine a block.

  • Total difficulty – Indicating how hard it was to chain up to a specified block by an integer value.

  • Timestamp – A UNIX timestamp for when the block was mined.

    Nonce – Please check the Ethereum Account section.

  • Parent hash – Also called previous hash. The hash came from the previous block (or the parent block). Each block contains a previous hash. By the way, we can link back to the first block in the chain.

  • Beneficiary – Also called “Mined by.” It is the beneficiary miner address that receives a mining reward.

  • Gas price – Please check the Gas, Gas Price, and Gas Limit section.

  • Gas limit – Please check the Gas, Gas Price, and Gas Limit section.

  • Size – The block size in bytes.

  • Hash – A unique Keccak hash of the block.

  • Extra data – A field containing additional data from a block. When miners are creating a block, they can add anything in this field.

  • State root: Hash of the root node of a specialized kind of Merkle tree that stores the entire network state, also known as world state. It contains the Keccak hash of all account balances, contract storage, contract code, and account nonces. If any piece of the data changes, the entire state root value will also change.

  • Transactions root: Hash of the root node of the transactions trie that stores all transactions in this block body.

  • Receipt root: Hash of the root node of the transactions receipt trie that stores all transactions receipt in this block.

At this stage, we have deep dive into how Ethereum works, and especially we are anatomizing Ethereum block, transaction, account, and state. The relation between these Ethereum block structures. Let’s summarize what we have learned and link it all together, as shown in the Ethereum architecture diagram (Figure 4-22).

A schema of ETM architecture consists of header and body in a separate block. The trie of world state, transaction, receipt points to their respective layers in the header block. Table for account state, transaction, and transaction receipt leads to their respective trio. Account storage trio leads to account state table. Body block leads to table of transaction receipt.

Figure 4-22

Ethereum architecture: block, state, transactions

We’ve now described most of the important concepts about Ethereum.

Summary

The main purpose of this chapter was to introduce the key concepts of Ethereum. We’ve started to learn about the history of Ethereum as well as the key components and elements behind Ethereum, including accounts, contracts, and gas. So, now you know the fundamentals of how Ethereum account works. We went over Ethereum node and Ethereum clients—geth technologies with some examples. We delve into the Ethereum architecture, understand how the Ethereum Virtual Machine (EVM) works, how smart contract Opcode is executed within the EVM, the structure of the block, state, and transactions in EVM. At this stage, you should be ready to go for the next chapter and start with developing your first smart contracts and end-to-end decentralized applications. We will show you how to build it step-by-step. Stay tuned.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.177.14