How Proof of Work (PoW) consensus works

What is a consensus algorithm?

It's the procedure through which all the network's peers agree on the network's present state. For example, blockchain networks use different consensus algorithms to decide on the current state of the global ledger.

Types of consensus algorithms

Different types of consensus algorithms are listed below:

  • Proof of Work
  • Proof of Stake
  • Proof of Capacity
  • proof of Burn

Proof of Work (PoW)

To mine the next block, every miner must solve a complex mathematical problem (which takes a lot of computational power). The miners compute the block's hash, and if their hash is less than the current difficulty level set by the network, they have found the correct solution. Otherwise, they can adjust the value of the nonceAn adjustable field in the block header and then recompute the hash of the block (changing anything in the current block will change the hash of the block) and check if they have the correct solution. They will repeat this process until they find a nonce that gives the correct solution.

Depending on the network's current difficulty, nodes might have to compute millions or even billions of hashes to find the correct solution. Then they will send their block to all the other nodes on the network. Then they all will validateCheck if it meets the required criteria e.g. hash is less than the current difficulty of the network and also check the validity of all the transactions in the block. this block and add it to their journalA nodes local view of the blockchain. Then they will start adding their own blocks after this block, thus confirming the previous blocks and all the transactions in them.

Note: All other fields in a candidate blockA block that a miner is trying to mine are fixed except the nonce.

Pseudocode

  1. Start with an initial value of the nonce.
  2. Compute the hash of the block.
  3. While (hash is not less than the difficulty of the network):

  a. Nonce = nonce + 1 (or you can randomly assign a new value to nonce)

  b. Recompute the hash of the block

  1. After we've computed the correct hash, we'll send the block to the entire network.

Figure

You can visualize the steps in the following figure:

Working of proof of work in blockchain

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved