- Yair Mark
How Ethereum Proof of Work Mining Works
As you are reading this article, the public Ethereum blockchain is still using Proof of Work (PoW) as its consensus mechanism. This will however change fairly soon as Ethereum switches to Proof of Stake(PoS). Until the switch, I thought it would be valuable to understand how Ethereum mining works using PoW as the consensus mechanism as it is still being used by many other big public blockchains like Bitcoin.
Consensus and mining
Miners on the Ethereum blockchain (an open-source, public, blockchain-based distributed computing platform and operating system featuring smart contract (scripting) functionality) are computers that run a full instance of Ethereum and are responsible for taking incoming transactions and running an algorithm on them.
If the miner successfully completes this algorithm and receives agreement from other miners that the algorithm was completed successfully, then they may add these transactions with the answer to a new block and add this to the blockchain.
The protocol is designed so that it is difficult to successfully mine a block, but easy and fast to verify that a block was mined successfully. The analogy that is often used to describe this protocol is mining physical resources like gold. While gold is hard to mine, it is visually easy to prove and verify that it has been mined. Like physical gold, Ether (the unit of currency on the Ethereum blockchain) is scarce.
Today, miners play an important role in making sure Ethereum works. Ethereum’s mining process is almost the same as Bitcoin’s. For each block of transactions, miners use computers to repeatedly guess answers to a puzzle until one of them wins.Ethereum Mining occurs when:
- A computationally difficult puzzle is completed faster than other miners
- Other miners on the blockchain agree that the work is complete
- The processed/mined block is added to the blockchain
- The miner receives a reward in the form of Ether for completing the work first. There are a limited number of Ether available. Mining is the only way to introduce the remaining Ether onto the blockchain
- The miner receiving an Ether fee as specified by the requestor for executing the transactions they provided. This fee is decided by the sender of the transaction.
The Ethereum Proof of Work (PoW) algorithm
Ethereum uses Ethash as its PoW algorithm. This algorithm is known as a memory-hard or memory bound problem. Memory bound problems are constrained mainly by the amount of memory machines have. The decision to design the algorithm this way is because memory in computers has been heavily optimised – meaning that it is highly unlikely that someone will work out some loophole that will make mining on the Ethereum blockchain significantly easier. The reason for this memory hardness is due to the algorithm’s need to store a large data structure, that is called a Directed Acyclic Graph (DAG). This DAG requires a few gigabytes of memory to store and change every epoch and takes time to generate. An epoch on the blockchain lasts for 30,000 blocks or roughly 100 hours.Mining in action
The overall process to take a list of transactions and process them into a mined block is as follows:
- The miner receives a list of transactions that have been broadcast to the Ethereum network.
- The miner decides which transactions to process out of the ones that have been broadcast.
- This decision is made based on the fee (also known as the gas price) that is included with each transaction
- Miners will choose the transactions with the highest fee first
- The miner finds and includes valid ommers
- An ommer is a none-english word which is a gender neutral way of reffering to an uncle or aunt
- Ommer is often used interchangeably with the word uncle
- Ommers are blocks that were successfully mined by other miners but were not mined fast enough to form part of the longest chain
- The miner receives a small reward for ommers included
- Miners may include up to 2 ommers
- Ommers are considered valid if they are at most six blocks back.
- The miner then runs all chosen transactions through the Ethereum Virtual Machine (EVM).
- Based on the result of the EVM execution, the miner starts to try mine:
- A seed is used for the given epoch.
- Seeds are generated by hashing the previous epoch’s seed
- The first seed on the Ethereum blockchain is generated by hashing a series of 32 bytes of zeros
- The seed is used to generate a 16MB cache
- The cache is then used to generate a 1GB DAG
- The DAG is used in combination with a random nonce (this is an integer where the value is not important) and passed through a computationally intensive algorithm
- If the result of step 4 is less than or equal to the target difficulty, then the miner has successfully mined the block using the given nonce and adds the nonce with the transaction information to the Ethereum blockchain.
- The blockchain difficulty is determined and adjusted by the network to ensure that mining takes no faster nor shorter than a certain amount of time
- The difficulty is currently set so that on average, mining should take no longer or shorter than 12 seconds. It is intentionally not faster than this due to network latency and to give successfully mined transactions enough time to propagate the Ethereum network. Faster mining times would result in more ommers
- If the result of step 4 is greater than the target difficulty, then the miner picks another nonce and repeats from step 4.
- A seed is used for the given epoch.
At a high level proof of work involves trying different nonces together with transaction data in a computationally intensive algorithm until certain criteria are met or another miner finds a solution first.
Solving this mathematical puzzle is computationally intensive and is designed so that there are no shortcuts to finding a solution. Once a solution has been found in the form of a valid nonce, it is fast and easy for other miners to apply the same process (using this nonce) and validate that the work done is valid.
While Ethereum is doing away with the PoW consensus mechanism in exchange for Proof of Stake (PoS), Bitcoin is still sticking to it. Will PoW finally disappear into the ether?
- The Official Ethereum wiki on mining
- Ethash – the Ethereum PoW mining algorithm
- Ethash Design Decision Rationale
- Stackexchange Question on Ethereum Block Architecture
- DEVCON1: Understanding the Ethereum Blockchain Protocol – Vitalik Buterin
- Explanation on What an Ommer/Uncle Block is
- Ethereum Glossary of Terms
This blog is a cross post, it was originally posted here