Ethereum Source Code Analysis: The Secrets of Mining and Consensus Algorithms

·

Understanding how new blocks are created in Ethereum requires diving into its core mechanics—mining and consensus algorithms. This deep dive explores the inner workings of Ethereum’s block creation process, from transaction assembly to final sealing via consensus. Whether you're a blockchain developer or an enthusiast, this guide unpacks the technical layers behind Ethereum’s decentralized security model.


How a New Block Is Assembled in Ethereum

At the heart of Ethereum’s mining mechanism lies the miner package, responsible for generating new blocks through a structured workflow. The primary components include:

The Worker prepares a complete block environment—transactions, uncle blocks, executed receipts—and sends it to registered Agent instances. Once an agent completes mining, it returns a signed block and updated work data.

👉 Discover how blockchain networks validate transactions securely and efficiently.

A unique feature is the unconfirmedBlocks list, which tracks locally mined blocks. After some time, these are checked against the canonical chain to confirm inclusion, logging results for transparency.

Key Phases of Block Creation

  1. Block Assembly
    Performed by the worker, this phase includes:

    • Selecting pending transactions from the transaction pool (TxPool)
    • Executing all transactions and collecting receipts
    • Gathering valid uncle blocks
    • Building the block header with basic fields (number, parent hash, timestamp)
  2. Block Sealing (Authorization)
    Handled by the Agent using a consensus engine (Engine). This step finalizes the block by setting cryptographic proofs like Difficulty, Nonce, and MixDigest.

This separation ensures efficiency: one component focuses on data integrity, while another handles computational validation.


Core Functions Behind Mining Workflow

Miner Initialization: New() and Update()

The mining process starts with Miner.New(), where:

Update() listens for downloader events:

This prevents conflicts between local mining and network synchronization—ensuring each node processes only one source of truth for new blocks.

Worker Operations

worker.update()

Listens to three key events:

Notably, when a miner successfully adds a block, it emits its own ChainHeadEvent, enabling continuous mining.

worker.wait()

Waits on a channel for completed blocks from agents. Upon receipt:

commitNewWork()

This critical function orchestrates block assembly under mutex protection:

  1. Sets block time (must be > parent block time)
  2. Increments block number
  3. Assigns parent hash
  4. Calls Engine.Prepare() to initialize header fields
  5. Applies hard fork rules (e.g., DAO fork logic if applicable)
  6. Creates a new Work object
  7. Executes transactions from TxPool
  8. Selects up to two uncles from possibleUncles
  9. Calls Engine.Finalize() to set:

    • State root (Header.Root)
    • Transaction hash (TxHash)
    • Receipt hash (ReceiptHash)
    • Uncle hash (UncleHash)
  10. Sends the prepared Work to all agents via channel

Once finalized, the block is ready for sealing.


Consensus Algorithms: The Heart of Mining

Ethereum uses the Engine interface to abstract consensus logic. Two main implementations exist:

Key Engine Interface Methods

MethodPurpose
VerifyHeader()Validates individual block headers
VerifyHeaders()Batch validation of headers
VerifyUncles()Ensures uncle blocks meet criteria
Prepare()Initializes header fields before sealing
Finalize()Finalizes block after transaction execution
Seal()Performs final authorization (mining)
VerifySeal()Validates whether a block was properly sealed

Among these, Seal() is central to mining—it produces a cryptographically valid block.


Ethash: Ethereum’s Proof-of-Work Algorithm

Ethash, also known as Proof-of-Work (PoW), secures Ethereum through computational effort.

Core Equation

RAND(h, n) ≤ M / d

Where:

The goal is to find a nonce such that the result of RAND() falls below the target threshold (M / d). Higher difficulty means fewer valid solutions—increasing mining time.

Mining Process in Ethash

The mine() function runs in parallel across multiple threads (Ethash.threads). Each thread:

Nonce increments with every iteration, ensuring uniqueness.

👉 Learn how consensus algorithms maintain trust in decentralized systems.

The Role of Hashimoto Functions

Two variants support different use cases:

Both call the core hashimoto() function but differ in data access patterns.

Inside hashimoto()

This function performs complex hashing operations:

  1. Combines header hash and nonce → SHA-512 seed (64 bytes)
  2. Expands seed into a 32-element uint32 array (mix[])
  3. Uses lookup() to fetch data from large datasets (dataset[])
  4. Mixes in 64 rounds using FNV-style XOR operations
  5. Compresses final mix → digest (32 bytes)
  6. Returns:

    • digest: Stored in Header.MixDigest
    • result: Compared against target difficulty

The use of non-linear table lookups makes memory access patterns unpredictable—resisting ASIC dominance and promoting fairness.


Dataset and Cache Generation

Due to their size, Ethash uses memory-mapped files for efficiency.

Cache and Dataset Structure

Both share similar structures:

Each corresponds to an "epoch" (every 30,000 blocks).

Size Calculation

For epoch beyond 2048:

size = 2^24 + 2^17 * epoch - 64

Then adjusted to the nearest prime number for cryptographic strength.

Even cache sizes exceed 256MB, making lightweight verification feasible while keeping mining resource-intensive.

Seed Generation

Each epoch uses a unique seed derived from repeated Keccak256 hashing:

seed = keccak256^(epoch) initial_seed

This ensures dataset uniqueness per epoch.


Clique: Proof-of-Authority for Testnets

Clique implements Proof-of-Authority (PoA) using digital signatures instead of computational work.

Sealing Logic

Uses ECDSA to sign block headers:

signature = ECDSA(private_key, header_hash)

Only authorized signers can produce valid blocks.

Dynamic Authorization via Voting

Nodes vote to add or remove validators:

Votes accumulate in snapshots. A change passes when:

Votes ≥ 50% of current signers

Snapshot applies changes only after several confirmations—preventing rapid manipulation.

👉 See how blockchain consensus models compare across networks.


Frequently Asked Questions (FAQ)

Q1: What is the difference between Ethash and Clique?

A: Ethash uses Proof-of-Work requiring heavy computation; Clique uses Proof-of-Authority where trusted nodes sign blocks. Ethash secures mainnet; Clique powers testnets like Rinkeby.

Q2: Why does Ethereum separate block assembly from sealing?

A: Separation allows parallelization and modularity. Worker handles transaction processing efficiently, while agents focus on computationally intensive sealing—improving performance and scalability.

Q3: How does Ethash resist ASIC dominance?

A: By relying on large memory datasets and non-linear lookups, Ethash favors high-bandwidth memory over raw compute speed—making specialized hardware less advantageous.

Q4: What role do uncle blocks play in Ethereum?

A: Uncles improve network efficiency by rewarding stale blocks. They increase security by incorporating otherwise wasted computation into consensus.

Q5: Can anyone become a validator in Clique?

A: No—only pre-authorized addresses can sign blocks. New members require approval via voting by existing signers, ensuring controlled governance.

Q6: Is mining still relevant in Ethereum post-Merge?

A: Not in its current form. After Ethereum's transition to Proof-of-Stake (PoS), traditional PoW mining ended. However, understanding Ethash remains valuable for legacy chains and academic insight.


Summary: From Code to Consensus

Creating a new Ethereum block involves two stages:

  1. Assembly: Gather transactions, execute them, finalize state roots.
  2. Sealing: Apply consensus rules—either PoW (Ethash) or PoA (Clique).

While Ethash relies on brute-force computation secured by massive datasets, Clique leverages trusted identities with democratic governance. Both reflect different trade-offs between decentralization, performance, and security.

Though Ethereum has moved to Proof-of-Stake, studying these mechanisms offers deep insights into blockchain design principles—from incentive structures to anti-centralization tactics.

Whether you're building dApps or auditing smart contracts, understanding how blocks are formed strengthens your foundation in decentralized systems.

Core Keywords: Ethereum source code analysis, mining process, consensus algorithm, Ethash, Clique, Proof-of-Work, Proof-of-Authority, blockchain development