In this article, we will be learning about Proof of Work and Mining in the Alchemy University Ethereum Developer Bootcamp Week One course.
Disclaimer: Most of the content below is a general summary and retelling of the information from the course.
Before we talk about mining and proof of work, we need to understand what purpose they serve.
Blockchain networks, like Ethereum, are made up of distributed and decentralized databases consisting of many nodes (computers). In a decentralized environment, it's difficult to validate and coordinate blockchain transactions.
How do all nodes agree on the current and future state of user account balances and contract interactions?
Who gets to add new blocks/transactions to a chain? How do we know any blocks added are "valid"?
How the heck are all of these things coordinated without any central actor in place?
The solution is consensus mechanisms (consensus means a general agreement) which act as rules that the decentralized blockchain follows to stay in agreement over what is valid.
A blockchain consensus is when at least 51% of nodes are in agreement over the current global state of the network.
Proof-of-work is the consensus mechanism used by Bitcoin and previously Ethereum before they switched to using proof-of-stake.
An example of the main consensus rules for proof-of-work is:
The "longest" chain will be the one the rest of the nodes accept as the one "true" chain, determined by a chain's cumulative work. Also known as Nakamoto Consensus.
You cannot double-spend.
Mining is the process of creating a block of transactions to be added to the blockchain.
The nodes in the network with the mining software, or miners, are continuously trying to extend the chain with new blocks that contain valid transactions. The network will ask the miners for their "proof-of-work" before they can add a block.
This "proof-of-work" is an output that must be lower than the network target difficulty.
A valid proof-of-work output currently looks like this in the Bitcoin network: 000000000000000000043f43161dc56a08ffd0727df1516c987f7b187f5194c6. It is a hash of the previous block header and new transactions to add to the chain.
Here's what the proof-of-work mining algorithm looks like:
- Take current block’s block header, add mempool transactions
- Append a nonce, starting at nonce = 0
- Hash data from #1 and #2
- Check hash versus target difficulty (provided by protocol)
- If hash < target, puzzle is solved! Get rewarded.
- Else, restart process from step #2, but increment nonce
Performing these difficult calculations requires large amounts of energy and hardware upkeep required to run mining software. The incentive for miners to perform these algorithms regularly is currency as a reward. If the consensus rules are followed, making a secure network, miners get paid.
In proof-of-work, miners must present a proof (in the form of a hash output on valid input data) that they expended energy in order to successfully "mine" a block and have it extend a blockchain.
Alchemy University: Ethereum Developer Bootcamp