A Technical Beginner’s Introduction to Ethereum

0
28
image from: https://depositphotos.com/vector-images/ethereum.html

The fact that you are here reading this article indicates that you have probably heard of the revolutionary potential of blockchain, and Ethereum is leading the charge. There are many resources on the internet that can help you understand why or how that is the case but they often don’t go into technical detail behind the technology. This article will attempt to explain in more technical detail what Ethereum is and how it works. Therefore, it is assumed that readers have a decent grasp of computer science concepts such as virtual machines, hashing & blockchains, data structures, etc.

Definitions

I will include in this section some definitions that may help you understand some of the concepts discussed later on.

Bytecode: Code compiled to run on a virtual machine.
Client: An implementation of the Ethereum Virtual Machine.
Node:
An instance of the Ethereum client.
Proof-of-work:
Consensus protocol for Ethereum (Set to change in the near future to proof-of-stake). It is the expenditure of computational effort to validate a block and its transactions. See this link for more info.
Turing-Complete
: A language will be Turing-complete if it can theoretically run any program that the universal Turing machine can. This requires the ability to execute certain functionality such as addition, multiplication, if-else statements, etc.
Virtual Machine: A virtualisation of a computer system that emulates the functionality of a physical computer.

Why Ethereum?

Inspired by Bitcoin, Ethereum wanted to provide a blockchain platform that could host decentralised applications (Dapps) and facilitate smart contract functionality. It achieves this by integrating a Turing-complete programming language (EVM code) into the blockchain. EVM code is a stack-based bytecode language. This functionality enables smart contracts to exist on the blockchain which can define their own transactions and state-transition functions. The applications of smart contracts are endless.

Blocks and the Blockchain

At an abstract level, blocks are a collection of transactions and a hash of the previous block. Putting transactions together into a batch in each block allows participants to “maintain a synchronized state and agree on the precise history of transactions”¹. Once a block is put together by a miner, it is propogated to the rest of the network and each node adds it to to the end of their copy of the blockchain. This process is currently specified by Ethereum’s proof-of-work consensus model, but this is scheduled to change in the near future by the Eth 2.0 upgrades.

Each block on the blockchain consists of the following information:

  • Timestamp
  • Block number
  • Difficulty — effort required to “mine” the block
  • Hash — the block’s unique identifier
  • Parent hash — the previous block’s unique identifier
  • A copy of the transaction list
  • State root — the entire state of the system
  • Nonce — a hash that, when combined with the mixHash, proves that the block has gone through proof of work.

The incorporation of the parent hash is what links the blocks together. Each block can only have one parent, thus as each block is “mined”, it is linked to its parent.

Ethereum Virtual Machine

The main component of the Ethereum network is the Ethereum Virtual Machine (EVM). The EVM is one single entity maintained by thousands of nodes running an etheruem client². Each of these nodes is connected, and they share information at a peer-to-peer level. It is vital that the operation of the EVM is continuous and uninterrupted. The EVM stores the blockchain, and its “state”.

image from: https://ethereum.org/en/developers/docs/evm/

Ethereum State

Whereas the EVM is everchanging as blocks are produced and added to the blockchain, the Ethereum state is a “snapshot” of the EVM at any given time. The Ethereum state is a large data structure that stores all accounts and balances, as well as a “machine state”. The machine state, as well as accounts and balances, change from block to block as transactions are executed, according to a set of rules defined by the EVM³. This state is what is stored at each block by the state root. Within the machine state is a stack and memory, as well as two variables; program counter and gas available. This machine state is what enables code execution in Ethereum. Check out this link if you wish to learn more about the exact data structure used.

Accounts, Transactions and Messages

Accounts

The state is made up of accounts which are objects⁴. Accounts have a 20-byte address and is where value is stored. There are two types of accounts.

  1. Externally-owned accounts — these accounts are controlled by private keys
  2. Contract accounts — these accounts are controlled by their contract code

All ethereum accounts have four fields:

  1. Nonce — a counter which keeps track of how many transactions have been sent from the account. It is used to ensure that transactions aren’t executed more than once. In a contract account, this field keeps track of the number of contracts it has created.
  2. Current ether balance — stored in wei (1e+18 wei = 1 ETH)
  3. codeHash — only used in contract accounts. This code will be executed if the account receives a message call.
  4. Storage root — the hash of the root node of a Merkle Patricia Trie that encodes the storage contents of the account.

The main difference between externally-owned accounts and contract accounts is where the account is stored and how it is managed. Externally-owned accounts are managed with key-pairs which gives the owner custody over the assets associated with that account. Key-pairs are not stored on the blockchain.

Contract accounts on the other hand are stored on the blockchain, and therefore cost to create. Transactions from external accounts to contract accounts are required to execute transactions from the contract account, but these transactions are not limited to the transfer of ETH.

Transactions

In Ethereum, a transaction is a package of data verified by the sender (signed) that stores a message to be sent from an externally-owned account. These transaction messages contain the following information:

  1. The recipient
  2. A signature of the sender
  3. The amount of ether to transfer
  4. An optional data field
  5. A STARTGAS value — the max number of computational steps the execution of the transaction is permitted to take
  6. A GASPRICE value — a fee in ether that the sender will pay per computational step.

The total fee of the transaction is calculated by multiplying the number of computational steps taken by the GASPRICE. While the data field is usually empty in standard transactions, contracts can access it through the EVM.

The STARTGAS and GASPRICE fields are included to prevent hostile attacks or accidental wastage of computational resources by limiting the number of computational steps the transaction can take, and setting the price of each step.

Messages

Messages are very similar to transactions, but are sent by contracts to other contracts. Messages are virtual objects that are never serialized and exist only in the Ethereum execution environment⁵. A message contains:

  1. The sender of the message (implicit)
  2. The recipient of the message
  3. The amount of ether to transfer alongside the message
  4. An optional data field
  5. A STARTGAS value

Ethereum State-Transition Functions

Ethereum state-transition functions are like any other function that you may encounter in computer science; given an input, it will produce a deterministic output. The state-transition function is the process by which the Ethereum state is modified and is often shown as the following:

Y(S, T) = S'

Where S is the old state, T is a set of one or more transactions, and S' is the new state produced by the state-transition function Y.⁶

The state-transition function is the application of a transaction to the current state to create a new state where the transaction has occured. A block contains a list of transactions and the process of adding a block to the blockchain involves “applying” each transaction to the current state and creating a new “final state”. Transactions are “applied” one at a time and the resulting state becomes the new current state for the next transaction. The resulting “final state” then becomes the current state for the next block⁷.

image from: https://ethereum.org/en/developers/docs/blocks/

Addding a block to the blockchain is called block validation, and is undertaken by the mining pool in the Ethereum network. Of course there will be situations where a transaction can not be executed, such as when the sender is attempting to send more ETH than they actually own. The EVM is designed to manage these situations such that miners who expend work for a transaction which ultimately fails is still rewarded for their effort. For a more detailed step-by-step process of Ethereum’s block validation process, check out the state-transition function discussion in the Ethereum whitepaper .

Code Execution

The EVM code is a low-level stack-based bytecode language; where the code is a series of bytes, each of which represents an operation. During execution, the code has access to three structures:

  1. stack — LIFO where values can be pushed and popped
  2. the memory — which is infinitely expandable
  3. the storage — which persists long term

The physical hardware that actually executes the code is the hardware that validates the block which contains the code. Code is contained in accounts, which is executed during the state-transition function if the account receives a message call in a transaction, and this is done during block validation⁸. By this definition, the code in the block will be physically executed by every node that validates that block.

Of course developers are not expected to write code in this low-level language which is why Solidity and Vyper exist.

Smart Contracts

I will save an in-depth discussion of smart contracts for another time, but you cannot talk about Ethereum without at least mentioning them. A smart contract is the executable EVM code stored on the blockchain and they are what make Ethereum so powerful. At an abstract level, they can be described as a series of logic that, given certain inputs, will produce certain outputs. If you wish to learn more, there are plenty of resources online, but the Ethereum dev docs is a good place to start.

Conclusion

I hope this information helped you learn more about the technical side of Ethereum. While Ethereum 2.0 may slightly modify some of this information, it is still important to understand how Ethereum works. Keep an eye out for a future article, where I discuss Ethereum 2.0 in detail.

References

[1] https://ethereum.org/en/developers/docs/blocks/.
[2,6] https://ethereum.org/en/developers/docs/evm/.
[3] see [2].
[4,5,7,8] https://ethereum.org/en/whitepaper/

Also, Read


A Technical Beginner’s Introduction to Ethereum was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.