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.
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.
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)
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. |
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.
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.
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.
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.
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.
Code hash – It refers to your contract’s code under this account on the EVM. It’s immutable.
Smart Contract
A contract is a written and oral arrangement between two or more parties to do certain things.
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.
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)
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.
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.
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.
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.)
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:
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.
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
- 1.
Your node can be a network validator to verify all the transactions and blocks.
- 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.
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.
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.
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.
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 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:
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.
The listed account output should be similar to the following:
To get the latest block number of the blockchain, run the following command:
To exit the Geth console, just simply run exit or press CTRL-C.
Geth JSON-RPC via Command
The default port is 8545. Once the Geth node starts, we can run the curl command to query some useful blockchain information.
Here id - 11 is blockchain Id. The response result shows that web3_clientVersion is Geth/v1.10.17-stable-25c9b49f/linux-amd64/gol.18
We get hex result - 0x56bbc5d759fba6400
By converting Hex value to decimal (www.binaryhexconverter.com/hex-to-decimal-converter),
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
Mac: ~/Library/Ethereum
Linux: ~/.ethereum
Windows: %LOCALAPPDATA%Ethereum
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.
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:
The content of this keyfile is encrypted, as displayed in the following:
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.
- 1.
Transactions that transfer funds between two EOAs (e.g., Alice sends one Ether to Bob).
- 2.
Transactions that deploy a contract.
- 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.
From logs of Geth, we can see that transaction is submitted, a transaction hash, and a nonce is generated:
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
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.
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.
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.
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.