Proof of Work in Blockchain (Part 7- Blockchain Series)

Techskill Brew
Blockchain 101 by Techskill Brew
14 min readJan 21, 2022

Welcome to the seventh part of the 100 part series on Blockchain.

Part 1: What is Blockchain technology?

Part 2: Components of a Block in the Blockchain

Part 3: Hash Functions in Blockchain

Part 4: Types of Blockchain

Part 5: Merkle Tree in Blockchain

Part 6: Cryptography in Blockchain

Unlike centralized systems like banks, there is no central authority involved that makes the decisions on what, when, or how information gets stored on the Blockchain network. Since Blockchains operate as decentralized, they need to have a reliable, robust, efficient, and secure consensus mechanism. Consensus means reaching an agreement about something within a group of people.

Types of consensus: Decentralized and Centralized

The purpose of a consensus mechanism is to validate and verify that the data being appended to the Blockchain is valid. There are various mechanisms for consensus that are used currently. Each of them serves the same purpose of validating the data being added to the Blockchain, though they do it differently. And quite naturally, each one of them has its own pros and cons. The major difference between these mechanisms is in the way they reward and delegate the verification of transactions.

Consensus mechanisms for public blockchains: The two most notable Consensus mechanisms for public blockchains are — Proof of Work or POW and Proof of Stake or POS.

Proof of Work (POW)

Proof of Work is one of the older consensus mechanisms that came into existence in the 1990s. It is a consensus algorithm to confirm the transactions and create a new block to the Blockchain. It has been used by various crypto projects like Bitcoin, Litecoin, and Ethereum 1.0. Proof of Work uses the computational power of the computers on the network to create new virtual coins. In this algorithm, miners (specialized nodes involved in creating and adding blocks on Blockchain) compete against each other to solve complex mathematical problems, i.e., finding the nonce value in order to complete the transaction on the network. The nonce is responsible for generating a valid hash value for the block to make it valid and adding it to the existing blockchain. The process of validating transactions and adding them to the Blockchain is called mining. As soon as a miner successfully creates a valid block, he gets rewarded with new virtual coins, for example, Bitcoins in the Bitcoin network.

Let’s understand how POW and mining work in the context of the Bitcoin blockchain.

Validating transactions by nodes

(i) When a user submits a new Bitcoin transaction to the network, it goes through transaction verification. It means that transaction is validated by every computer holding a copy of the Bitcoin blockchain. These computers are called nodes.

(ii) Two types of verification are done: one is, verifying the authenticity of each transaction, and the second is, ensuring whether the user actually has the Bitcoins in his account balance that he wants to spend.

(iii) Each transaction is signed with a private key (digital signature). Since the public key is the sender’s address, it can be used to identify if it is the correct key for the given address from which the transaction has been done. Hence the authenticity of each transaction can be verified. If the transaction fails this verification test, it will be discarded. In this way, each transaction is validated for knowing its authenticity.

Verification of transaction using sender’s public key

(iv) Nodes hold a full copy of the Bitcoin blockchain, which is a universal ledger system. The ledger contains the complete transaction history of all previous bitcoin transactions. To perform transactions on the blockchain, you need a wallet that allows you to store and exchange your bitcoins (BTC). But, how does a node know your account balance and ensure that you are actually spending the Bitcoins that you have in your wallet? For this, the blockchain ledger doesn’t keep track of the account balances of users; instead, it only records each and every transaction that is broadcasted within the network and is verified and approved. For example, suppose a user Phil generates a transaction request to send 5 BTC to Jane. To complete the transaction, the nodes verify all the links to previous incoming transactions of Phil that add up to at least 5 BTC. These links are called “inputs.” Then the nodes verify and ensure that these 5 BTC inputs haven’t been spent yet or given to someone. The spent transactions are called “outputs.” Therefore, the nodes need to analyze and verify all the previous transactions (inputs and outputs) that ever took place on the whole network connected to your wallet. By referencing to the previous transactions on the Blockchain, nodes ensure that the sender of a transaction is not spending the same BTC twice and actually have the Bitcoins in his account that he wants to spend. This also solves the problem of “double-spending.”

Transaction Inputs and Outputs

(v) After the successful verification by the nodes, the transaction goes into the memory pool known as the mempool of each node. So you can say mempool is the waiting room for transactions where the transactions sit and wait for the miner to pick it up and pack into a block of transactions. At this point, transactions are said to be “unconfirmed” transactions.

Picking transactions by miners from mempool

A block can hold a finite amount of transactions. In the Bitcoin Blockchain, the limit of a block is 1 megabyte, which limits the transactions between 1500–3000 that can be included in one block. But the transactions are happening all the time, that go into the mempool, waiting to be confirmed. So how do mining nodes (also referred to as miners) prioritize which transactions to pick up from the mempool and create a block? The answer lies in the transaction fees that the miner gets after adding the transaction into the block. The fees is non-compulsory and is decided entirely by the user. So a user can pay zero fee or a higher transaction fee. If the miner successfully creates a block, he gets the fees attached to all these transactions. So obviously, the miner picks the transactions with the highest fees to create a block.

Merkle tree and Root hash

After verification, the hash function SHA-256 groups the number of verified hashed transactions into a Merkle tree, required to form a block containing one root hash. Merkle tree and root hash have been discussed in detail in Part 5. After the maximum amount of transactions have been added (all blocks have a memory limit), the miners now create a valid hash for the block. This process is known as mining.

Merkle tree and Root hash

Difficulty level and Nonce value

A valid hash for the block is a hash that meets a certain difficulty and is less than or equal to the “hash target.” For example, suppose the hash target is 000032c2d3e965, then any hash less than or equal to the hash target is a valid hash for the block. Additionally, the difficulty determines the number of predefined zeros at the beginning of the valid hash. Like in this case, the valid hash should be less than or equal to the hash target and must begin with 0000. More zeros at the beginning of the hash mean a higher difficulty.

Valid hash for the block contains value less than or equal to the hash target

To create a valid hash for the block that meets the difficulty level, the miners compete with each other to find the nonce value that is used to generate a valid hash for the block. The nonce is “number used only once”; it means each time the nonce is changed for the same block, a new hash is created. A nonce is an integer number, and along with the block number, data, and previous hash, the nonce serves as an input for the SHA256 hash function to calculate the current block’s valid hash, that begins with predefined 0’s and whose value is less than or equal to the hash target.

Since miners can’t alter the previous block’s hash or the Merkle root, they randomly choose a nonce value to generate a hash for the block. If the hash produced does not have a sufficient number of zeros, then the miner discards the hash and tries a new nonce. This process is continuously repeated until a miner discovers a nonce value that produces a hash of the desired difficulty. The nonce is the only parameter of the block that the miner changes, with all others remaining static.

For instance, in the below Fig, for Block 1, after the miner inputs nonce value 1, the block is invalid as the hash doesn’t start with 4 O’s. The miner will then try to change the nonce numbers one by one from 1,2,3…, and so on, run the SHA 256 hashing algorithm until he inputs the nonce value 30555, which gives a valid hash that matches the mining difficulty level, i.e., starts with 4 O’s and its value is less than the hash target 000032c2d3e965.

(a) Invalid hash for the block (b) Valid hash for the block as it contains four leading 0’s and the value is less than or equal to the hash target

Finding a valid hash is very computationally intensive, and is the “work” in itself. Hence the consensus mechanism got the name Proof of work.

If a miner finds such a nonce, called a golden nonce, and successfully completes the proof-of-work, the block is broadcasted to the rest of the network. Other miners on the network then stop the proof-of-work mining. They validate the solution and update the copy of the Bitcoin transaction ledger with the transactions that the winning miner chose to include in the current block. The mining process is challenging, i.e., it is difficult to find the nonce value, but it is very easy to verify the correct solution by the network, i.e., just one has to check whether hash has a value less than hash target and has predefined leading zeroes. After verification, if the block is found to be valid, it is added to the Blockchain. Thus, the consensus mechanism allows the network to achieve consensus on the current state of the blockchain. It allows only a single block to be added, and this addition is broadcasted to every node in the network. Miners are then rewarded in the form of Bitcoins upon the successful verification and addition of block to the global Blockchain. Every single node on the network stores a copy of the Blockchain, so when the Blockchain is updated, this update is broadcasted to all nodes.

The mathematical puzzle of solving nonce value doesn’t require any particular skill set of the user. It is achieved through brute force, i.e., they have to go through an intense race of trial and error to find the nonce for a block. In this puzzle-solving, the odds of solving the puzzle can be improved by acquiring more computational power. Therefore, the miners who have computers with powerful processors will succeed more often, but due to laws of statistical probability, it is highly unlikely that the same miner will be the winner every time.

Increasing difficulty level

The average time for solving a puzzle is 10 minutes. And once a puzzle gets solved, the block gets added to the Blockchain. So over time, if the miners start finding it easier to solve the puzzle, the block generation time gets reduced from the proposed 10 minutes. Therefore, if mining happens too quickly, the hash computations become complex. On the contrary, if the mining is happening too slowly, the hash computations get easier to ensure that the average time for adding a block to the Blockchain is 10 minutes.

The difficulty level of the puzzle is revised after every few days. For instance, while mining Bitcoin, it is revised after the creation of every 2,016 blocks, which approximately takes around 14 days (one block in every 10 mins = 6 blocks per hour, 6 x 24 in a day, and 6 x 24 x 14 = 2016 blocks in 2 weeks). As the difficulty increases, the target value for the hash decreases. This means there have to be more leading zeros at the start of the hash number. The probability of finding a lower hash value decreases, so miners have to test more nonces. This, in turn, means that more computational power is required to successfully solve the puzzle to find the valid hash with the desired difficulty.

Mining Reward

When the verified transactions form a block and this block is appended to the existing Blockchain, new Bitcoins are created/mined. The miner who first solves the puzzle and adds the block to the Blockchain is rewarded with newly mined Bitcoins.

Mining reward to the winning miner

Additionally, the miner also gets the transaction fees attached to the transactions that were inserted in the block. Currently, miners get 6.25 BTC per valid block mined. On average, 144 blocks are mined daily, and with each block, new 6.25 Bitcoins are created. Thus around 900 (144*6.25) new bitcoins are mined per day; in other words, they are added to the Bitcoin supply.

Bitcoin halving

Furthermore, the Bitcoin reward gets halved for every 210,000 blocks added. This process is known as bitcoin halving. It currently takes around four years to add that many blocks, so Bitcoin halving occurs approximately every four-year interval. In 2009, when Bitcoin was launched, the reward for successfully mining a Bitcoin block was 50 BTC. The first halving occurred in November 2012, which reduced the mining reward to 25 BTC. In July 2016, the reward was reduced to 12.5 BTC, and the current bitcoin halving occurred in May 2020, which reduced the reward to 6.25 BTC. The next bitcoin halving is expected to occur in 2024, and the last halving is predicted to occur in 2140. Once 21 million bitcoins have been created, no new bitcoins will be produced. From that point onward, Bitcoin miners will profit solely from the transaction fees given by network users, the people who buy and sell bitcoins.

Bitcoin halving is essential as it:

· Ensures that the amount of bitcoin mined with each block decreases, making bitcoin more scarce, and ultimately, more valuable. In other words, if the new supply of bitcoins decreases, their demand will increase.

· Halvings are also associated with huge surges in the price of bitcoin. It provides miners with the incentive to mine more, although their rewards are halved. For instance, upon the first halving in 2012, the price of bitcoin rose from a mere $12 to around $1,000; after the second halving in 2016, the bitcoin price increased to $20,000. In January 2022, the price of bitcoin was more than $40,000.

Importance of increasing the difficulty level of the mathematical puzzle

It is important to revise the difficulty level of the puzzle to find the valid hash for the block. If the difficulty is not adjusted, there would be a linear relationship between the hashing power and block rewards. As more miners join the network and hashing power increases, there would be a corresponding increase in the Bitcoin rewards. This would undermine the inflation controls on the Bitcoin protocol.

Longest chain rule

Since solving the mathematical puzzle is complex, the probability that two or more miners complete the blocks at similar times is rare. Even if they do it, two or three branches are created in the Blockchain. Imagine that the blockchain is 1000 blocks long, and three miners simultaneously find the 1001st valid block and broadcast them to the network. Therefore, three branches will be created with the three 1001st blocks: 1001A, 1001B, and 1001C. So which of these blocks will become part of the valid Blockchain? This depends on the “longest chain rule.” This problem is solved when the 1002nd block is mined. If this block is added to the 1001A block, then the chain with the 1001A block becomes longer than with 1001B and 1001C blocks. The longest chain in the Blockchain created by the 1001A block is accepted and trusted, and therefore, everybody switches to the longest chain. As a result, all other subsequent blocks will be added to the longest chain.

The transactions of 1001B and 1001C simply go back to mempool, which the miners again pick to pack them into the blocks.

(a) Three branches of Blockchain with 1001 block, (b) 1002 block gets added to 1001A block, (c) Longest chain created by 1001A block is accepted

Mining pool

The mining process can be carried out either individually (individual mining) or in a group (mining pool). In individual mining, the user has to register himself/herself as a miner. As soon as the nodes verify the transactions, all the miners in the blockchain network will pick up the transactions and solve a mathematical puzzle to pack the transactions into a valid block. The first one to solve the complex mathematical puzzle gets rewarded and has the privilege to add the block to the Blockchain.

On the other hand, in the mining pool, miners group together to form a pool. In other words, they combine their mining power to compete more effectively to solve the mathematical puzzle. If the pool manages to win, the reward is split between the pool members, depending on how much mining power each of them contributed. In this way, even small miners can join the mining process and hence have a chance of earning Bitcoin.

Advantages of Proof of Work (POW)

(i) Proof of work makes it extremely difficult to alter any block/transaction of the Blockchain since such an alteration would require re-mining of the altered block and all subsequent blocks because each block contains the hash of the previous block. If one block is tampered with, the old hash becomes invalid, and a new hash is generated for the block. This affects all the subsequent blocks in the Blockchain, thus making all of them invalid.

(ii) To consistently create malicious yet valid blocks, you’d need over 51% of the miners on the network controlling a majority of a network’s computing power to achieve consensus on adding a block containing fraudulent transactions to the network. However, such an attack is highly improbable in a widely distributed ledger with many nodes.

(iii) Mining also solves the problem of double-spend problem. Because of the verification done by nodes, one can’t attempt to send the same digital asset to various parties at once. Therefore, if one attempts to double-spend the currency, that transaction will be rejected during verification.

Challenges of Proof of Work (POW)

(i) The biggest challenge of proof of work centers on the computational capabilities needed to solve mathematical puzzles in authenticating blockchain transactions. Therefore, the miners must have a powerful computer equipped with advanced hardware.

(ii) Powerful computers inherently consume a lot of electricity and energy. Moreover, these machines require effective heat management or cooling system to remain operational and prevent overheating, as well as associated damages to hardware components due to internal heat build-up. As of the reports in 2021, Bitcoin miners were responsible for the consumption of around 90 terawatt-hours of electricity annually. That’s more than the electricity used annually to power Finland, which is a country of 5.5 million people. And also is almost 0.5% of global electricity consumption worldwide.

The drawbacks of PoW represent a significant reason why other blockchain platforms have utilized alternative consensus mechanisms like proof of stake.

If you liked this article and want to know more about Blockchain, NFTs, Metaverse, and their applications, click the below link.

Happy learning!

--

--