Blockchain networks like Ethereum are distributed and decentralized databases consisting of many nodes, and the way nodes (i.e. computers) agree on the state of the network is through consensus.
Access the slides here.
In a decentralized environment, common issues are:
- How do all nodes agree on what the current and future state of user account balances?
- Who gets to add new blocks/transactions to a chain?
- How do we know any blocks added are "valid"?
- How are all of these things coordinated without any central actor in place?
The answer is consensus mechanisms.
Consensus means coming to a general agreement. Blockchain consensus typically means at least 51% of nodes are in agreement over the current global state of the network.
Consensus mechanisms end up simply being rules that a distributed + decentralized blockchain network follows in order to stay in agreement over what is considered valid. Remember that consensus mechanisms are inter-changeable and there are many out there that we have yet to cover, like proof-of-stake.
The main consensus rules for Proof-of-Work are:
- You cannot double-spend.
- 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.
Bitcoin is a blockchain that uses a consensus mechanism called Proof-of-Work. Ethereum was previously using Proof-of-Work but has since moved to Proof-of-Stake. We will not be covering proof of stake here (for now), but in the meantime you can learn more about this transition here.
The Proof-of-Work consensus mechanism allows decentralized networks like Bitcoin and (previously) Ethereum to reach a consensus, or agree on things like account balances and the order of transactions.
This prevents users from "double spending" their coins and ensures that everyone is following the rules, making Proof-of-Work-based networks resistant to attack. The consensus mechanism ends up providing security to a blockchain network just because it demands that everyone follow the consensus rules if they want to participate!
In Proof-of-Work, mining is the "work" itself.
Mining is a process of creating a block of transactions to be added to a blockchain.
But how does that tie into proof-of-work?
Well, proof-of-work could just as well be called proof-of-mining!
"Mining" can be considered an industry misnomer. The term gained popularity as it has a close tie in to "miners" digging for
In proof-of-work consensus, nodes in the network continuously attempt to extend the chain with new blocks - these are the miners, nodes that contain mining software. Miners are in charge of extending a blockchain by adding blocks that contain "valid" transactions. In order to add a block, the network will ask miners for their 'proof-of-work'.
A Proof-of-Work-based system will typically require miners to produce an output in a very difficult-to-get target range. A valid Proof-of-Work would currently look like this in the Bitcoin network:
How the heck does a miner get an output like this right!?
Well, the automated mining software has one job: take a piece of data (ie. the prev block header + new transactions to add to a chain) and hash it. If the hash output is below a target difficulty, then the miner has found the answer to the puzzle: a valid proof of work.
The proof of work shown above has 19 leading zeroes - that's a ton!
Since the range of each possible character per space is in hexadecimal, this means we have 1/16 character possibilities per space.
The hash outputs for SHA-256 are in hexadecimal which means we have 1/16 possible characters per space -
a-f in letters and
0-9 in decimals = 16 total possibilities.
This means finding one 32-byte SHA-256 output that has just ONE leading zero will take on average 16 tries. Try it now with any random input! How many tries did it take you to find an output with one leading zero?
The thing is, once we start adding more difficulty, things start to get more difficult.
Finding an output with 2 leading zeroes increases the average attempts to 256 - 16 possible characters in the first spot * 16 possible characters in the second spot.
Finding 19 leading zeroes will take, on average,
Proof-of-Work networks will typically have some sort of
target_difficulty. In order for a miner to add a new block, they must find a Proof-of-Work lower than the network target difficulty.
It's basically the network saying: "If you want to add a new block, you must provide Proof-of-Work with 12 leading zeroes." The way the math works, finding such a diffcult-to-find output is proof enough that a miner expended considerable resources to secure the network.
There is no way to cheat the system, you either have a valid proof of work or you don't.
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
The miner nodes in a Proof-of-Work network will perform this algorithm regularly.
This gives the network a way to recognize the true state and validity of proposed transactions via following of the consensus rules. As long as a majority of nodes on the network follow the consensus rules
In exchange for the large amounts of energy and hardware upkeep required to run mining software, miners receive currency as a reward. If the consensus rules are followed, making a secure network, miners get paid.
Alchemy University offers free web3 development bootcamps that explain blockchain consensus mechanisms and help developers master the fundamentals of web3 technology. Sign up for free, and start building today!
Updated 3 months ago