Ethereum under the hood

Sep 18, 2023 by Arnaud Stoz | 727 views

Blockchain Ethereum

https://cylab.be/blog/281/ethereum-under-the-hood

If you have already looked at blockchain technology, you might have noticed that two different names are often opposed:

  • Bitcoin
  • Ethereum

Even though those two blockchain implementations and visions rely on the same idea at the very beginning (distributed ledger and removing any central authority), they are technically very different.

The most obvious differences between these two are the following:

  • the consensus algorithm. Bitcoin rely on a proof of work (POW) to ensure consensus while Ethereum (recently) rely on a proof of stake (POS).
  • While Bitcoin Blockchain is only used to keep track of transaction (a bit like an accountant book) ethereum blockchain also allow you to store code and execute it (smart contract).

This blog post will focus on ethereum. If you are completely new to the blockchain world and concept, feel free to have a look at this other blog post to have a quick introduction.

What is Ethereum?

From a computer science perspective, Ethereum is a deterministic state machine, consisting of a globally accessible singleton state and a virtual machine that applies changes to that state..

From a more practical perspective, Ethereum is an open-source, globally decentralized computing infrastructure that execute program called smart contracts. It relies on blockchain to synchronize, store and secure the system's state changes.

There is three big part for understanding Ethereum:

  1. Ethereum block
  2. Ethereum data structure and state
  3. the Ethereum Virtual Machine (EVM)

If all of this sounds a bit obscure to you, let's deep dive into how Ethereum works.

The Ethereum block

A block in ethereum has the following structure: block_format_ethereum.png

As it can be seen in the diagram above there is two main parts in a block in Ethereum:

  • header
  • body

Header

The header contain a lot of information. We will not focus on every attributes present in the header, the interested reader can have a look at the Ethereum yellow paper .

Fields we will focus on are:

  • parentHash: the hash of the parent as required in any blockchain
  • stateRoot: the hash of the root node of the state Merkle-Patricia tree (more on that shortly)
  • transactionsRoot: the hash of the root node of the transactions Merkle-Patricia tree
  • receiptsRoot: the hash of the root node of the transactions Merkle-Patricia tree

Body

The body contains two part:

  1. a list of uncle block (more on this here)
  2. the list of all transactions included in this block.

State in Ethereum

As seen in the previous section, Ethereum maintain in each block, three differents hash states:

  • stateRoot also called world state. This is a mapping between every account address and their state.
    • Account State: the account state is composed of four things:
      1. a nonce
      2. a balance
      3. a storage hash
      4. a code hash
  • transactionState: the state of all transactions included in the block
  • receiptState: the state of the result of all transactions included in the block.

All those state rely on a special data structure called Merkle (Patricia) tree.

Ethereum data structure

As you have seen in the previous section, there is three hashes included in the block header (stateRoot, transactionsRoot and receiptsRoot). Actually those hash are the root node of a so called Merkle (Patricia) tree.

Merkle tree

A Merkle tree is a specific data structure characterized by its balanced binary tree structure, where the leaf nodes contain actual data, and each higher-level node represents the hash of its two child nodes merkle_tree.drawio.png

This is a very convenient structure as if one data (leaf) changes, it will have an impact directly on the root hash. This allows to very quickly check if all the data are the same or if a change happened. This will be used later when explaining how Ethereum propagate blocks and check their validity.

Merkle trees are used for both transactions and receipts as those two are immutable and belong to only on block.

Those merkle tree are not stored on the blockchain, only their root hash are stored. All node keep a local copy of those Merkle tree and compute them locally

With that knowledge, the full picture of Ethereum data structure can be seen on the following picture: ethereum_data_representation.png

Ethereum Virtual Machine (EVM)

As said previously Ethereum, unlike Bitcoin, gives the possibility to store and execute code on the blockchain. This is what we call smart contract.

Smart contract are code written by a human in a specific language (solidity, vyper,....) compiled to be run by the Ethereum Virtual Machine (EVM). The EVM can be seen a bit like the JVM. It defines a set of opcode and specific operation to be completely agnostic of the underlying OS/Hardware, this is the runtime environment for smart contract. This allows for a high portability of smart contract.

Even if Ethereum is often referred as a world computer this term is a bit misleading. Once a compiled contract stored on the blockchain needs to be run, it is actually run by a physical node using the EVM.

New Block Process

Now that main characteristics of Ethereum has been explained let's see in practice how this works

Ethereum_Transaction_Workflow.png

The flow is as follows (see picture):

  1. Alice start a transaction (Transfer X Ether to Bob). This transaction enter the ethereum network via the local node Alice run and is then broadcasted to all peer of that node.
  2. Eventually the transaction request reach the forger node. It verify the transaction, update locally the world state and produce a new block. This new block is just a proposal, it is not yet added on the blockchain.
  3. The proposed block reach the attestator. It make sure the block is valid (the transaction can be executed, correctly signed, correct parent,....) and once the block is checked, it send the attested block.
  4. Alice's local node receive the block and update accordingly its local copy of the world state.
  • For simplicity, only three node are shown. In practice there is 128 attestator and at least attestations from 2/3rd of attestators are required to be finalized and added to the blockchain.
  • For simplicity, a transaction from human to human (Externally Owned Account (EOA) to EOA) was shown. When sending a transaction to a contract (calling the contract) the idea is more or less the same except the transaction body differ a bit and each node need to execute the contract to calculate the output.

You have now seen all major building blocks composing Ethereum. In the futur post we will focus on smart contract and more specially on smart contract security.

This blog post is licensed under CC BY-SA 4.0