What is Proof of Work?
Updated: Aug 21, 2020
Proof of work (PoW) is the consensus mechanism described by Satoshi Nakamoto in his famous white paper “A Peer-to-Peer Electronic Cash System” and used in the Bitcoin Blockchain. It requires nodes to solve complex math problems for the opportunity to validate the next block.
Before getting into technicalities, let's understand the concept
First, let me introduce you to the Byzantine Generals.
Let’s assume that 2 generals with 2 armies are sent to attack an enemy sometime in the 17th century. The 2 generals have to attack from opposite directions. The attack will be successful only if both armies attack simultaneously. If only 1 army attacks then the enemy will be able to defend itself. There are no mobile phones available (Duh!). Communication is done through messengers. If General A sends a messenger to General B with the date/time of the attack, then how is he to be sure that B received the message? Maybe the messenger was captured, maybe the messenger is a traitor and changed the content of the message. General B can send a message back that he has received the message and he is ready to attack on the agreed date/time. But how is General B supposed to be sure that his message reached General A? This problem is further complicated if more that 2 armies are required for the attack and strategy has to be communicated between more parties. Also, what happens if the general himself is a traitor and is sending incorrect messages?
The above problem called the “Byzantine Generals Problem” is what blockchain has tried to tackle. How do we know that what is communicated is true? Especially if we cannot even trust a central authority to coordinate. How do the players know who is loyal and who is a traitor? In the context of bitcoin, if you’re sending 1 bitcoin to Alice, how do you know for sure that the system doesn’t take 10 bitcoins from your wallet and transfer it to Alice or someone else?
Let me now introduce you to "nodes". Nodes are the backbone of any blockchain.
In the world of cryptocurrencies, a loyal node is one that is creating blocks with valid transactions and a traitor node is one that is forging fake transactions to benefit himself. The idea behind POW is that you incentivize nodes to behave themselves.
What do nodes do?
=>Nodes have to solve a math problem. Without getting into details, it's important to understand that the math problem doesn’t require a genius to solve it. It requires money and time. Money on equipment (ASICS chips) and electricity to run the equipment and time devoted to math problem-solving.
But why do nodes spend money and time to solve this math problem?
=> The first node to solve the problem wins the right to build the next block.
But what does building the next block mean?
=> It means the node gets to decide which transactions make it to the block and become an immutable record for history.
But why would nodes want to create a block of transactions?
=> Money. The node is compensated in 2 ways. (i) a fixed number of bitcoins (6.25 per block created as of right now) and (ii) a commission on the transactions it adds to the block.
Sure this sounds cool but what is the point of this convoluted process to create a block of transactions? How is this securing the system?
= Here is the genius of POW. If a node is investing in creating blocks, he is incentivized to maintain the sanctity of the blockchain. If for instance, he enters incorrect transactions, the blockchain will become untrustworthy which would mean that the bitcoins awarded to it as well as the equipment bought by it will become worthless
Now lets get into technicals
To understand Proof of Work one first needs to understand “hashing”.
The security and immutability of the bitcoin blockchain are made possible by cryptographic hashing. Hash can be understood as the unique fingerprint of any data. Any data of any size, whether in the form of text or tables or video or audio can be converted to a unique text of fixed size called ‘hash value” using a hashing algorithm. To be useful the hash has to have the following properties:
The same data will always convert to the same hash value when the hash algorithm is applied
Even a minor change in the data like adding a “.” will change the hash value completely. This is called the Avalanche Effect.
You cannot reverse engineer and guess the input by looking at the output.
No two data can have the same hash value. This is called Collision Resistance.
Bitcoin uses the hashing algorithm called SHA-256. SHA-256 converts any text to a 256-bit value represented in a 64 character hexadecimal text. Needless to say, SHA-256 enjoys the above 4 properties.
=> At this point it would be a good idea to see how hashing algorithms generate a hash value here (https://emn178.github.io/online-tools/sha256.html)
=> See above how hash changes drastically with minor changes like using a capital letter instead of small letter or when a period is added
Now let's talk about Mining
With so many transactions happening, who decides which transactions make it to the next block of the blockchain?
=> The winner of a math contest decides.
Nodes which form the decentralized blockchain network compete to solve a cryptographic hash problem. The node that is the first to solve the hash problem and broadcast it to the rest of the nodes gets to create the next block and hence wins the right to decide which transactions make it to the block. Solving the problem requires money (in the form of equipment and energy) and time. The miner that solves the problem has proved that she has put in a lot of work to solve the problem and hence the term “Proof-of-Work”. As a reward for putting in the work, the winning node is compensated with a fixed number of bitcoin (currently 6.25 bitcoin) and a fee on all the transactions included in the block.
So nodes are solving math problems for the reward of bitcoins. But what purpose does it serve other than making miners rich?
The reason that digital versions of books, songs, and movies have been around for so long but digital version of currency is still not mainstream is because of the “double-spend problem”. When you watch a movie on Netflix, you’re watching a copy of the movie. The original is still with Netflix. The same cannot happen with money. If someone sends you a dollar, the sender should not be able to retain a copy of the dollar with herself and use that same dollar to buy something else.
The math problem-solving miners are the ones who prevent such double spend. When Alice sends 1 bitcoin to Jenny, Alice’s wallet is debited and Jenny’s credited by 1 bitcoin. The maintenance of the ledger with this and other transactions is the job of the miners.
Such data is traditionally stored by centralized authorities like banks. The blockchain community however distrust centralized institutions after events like the Global Financial Crises and aim to decentralize the responsibility of verifying transactions.
How do you ensure that pseudononymous miners recording transactions are honest and keep a true and immutable record of transactions?
You incentivize good behaviour and make bad behaviour pointless or expensive. With proof of work, miners who are spending resources on verifying transactions want to ensure that the block they create makes it to the chain. Verifying wrong transactions would mean that the next honest node will not extend the chain from their block and hence the bitcoins awarded to them will not be valid.
I get this now. So in technical terms, what does the miner do?
A miner first collates 1MB of transactions that she believes should be verified.
Next, she creates an 80-byte block header made of the following 6 components:
Version of the software the bitcoin client is running (4 bytes)
Timestamp of the block (4 bytes)
Hash of the previous block (32 bytes)
Merkle root of this block (32 bytes)
Nonce (4 bytes)
Target value (4 bytes)
Of the above 6 components, 2 require an explanation
Merkle Root- Merkle tree is a simple concept. First, find the hash of all individual transactions using SHA-256. Then combine hashes of 2 transactions and find their parent hash. Keep hashing upwards till you’re left with 1 final hash that combines all transactions. This is called the Merkle root.
Nonce- Number Once (N-Once) is the number that when added to the block header, produces a hash that is smaller than the target value. The only way to find the nonce is through trial and error. Try 1 and check if the hash is smaller than the target value, if not then add 2, then 3, then 4, and so on till you arrive at the correct nonce. The first miner to find a nonce that meets the criteria appends the next block in the blockchain.
Finding the Nonce is the entire point of mining
The nonce is difficult to compute but easy to verify. The first miner to solve the puzzle broadcasts it to all and the result is easily verified by other miners who then immediately focus their efforts on mining the next block.
Criticism of Proof of Work
Proof of work might be secure but is extremely wasteful. so much computing power, equipment, and electricity spent on solving math problems?
As per the International Energy Agency, if Bitcoin were a country, it would rank 45th in energy consumption.