As we have seen in the previous article Consensus in Blockchain , the ordered set of transactions floating in the blocks, helps the network to have a consistent copy of the ledger. Apart from the ordering, the consensus also plays a much important role of making sure that the business rules are followed. For e.g in a network which tracks the ownership of assets, the business rules may not allow for joint ownership of an asset. Thus the miners/validators in the network will make sure that none of the transactions put in the block should allow this condition to happen. If any transaction violates this rule, it should be considered invalid and hence be discarded.
Similarly in the crypto land, a major problem happens to be double spending , which is solved by the consensus protocols running in crypto blockchain networks like bitcoin, ethereum etc.
We will dive into the details of one of the very first consensus protocol, Proof of work (POW) used by Bitcoin and many others.
Before we delve deeper, here are few key terms to keep in mind:
Miner: A miner is a node in the blockchain network, that runs the POW algorithm.
Difficulty: The difficulty is a number that regulates the time taken for miners to add new blocks to the blockchain. Higher the difficulty, the more time it will take a miner to form(mine) the blocks.
Hash: Hashing means taking an input string of any length and giving out an output of a fixed length. Even if the input string changes by a ‘bit’, the hash changes significantly. There are various hashing techniques available. Bitcoin uses SHA-256.
Merkle Root: Each block contains a set of transactions, and their hashes. However, these hashes are not stored in a sequential order on the block, rather in the form of a tree-like structure such that each hash is linked to its parent following a parent-child tree-like relation
As shown in the figure below, if we go up the tree, hashing the left & right child, the top most node gives the merkle root of the tree.

Lets take a look at the block structure and the mining process of Bitcoin.

The miners as a part of the mining process hashes the given block. If the hash generated is less than or equal to the difficulty target, the miner floats that block in the network. This block will be verified by the receiving nodes and since it satisfies the consensus rules (valid transactions and block hash <= difficulty), it will be added to the chain.
We know that the hash of a fixed value will always be a fixed output. Then how will the miner calculate the hash of the block (which contains values that are fixed) to fall in the range that satisfies the difficulty. Remember, to change the hash, we need to change the input. This is the reason of the NONCE field in the block header. The nonce is a random value that the miner keeps changing while calculating the block hash, until it is successful to calculate the hash <= difficulty, or some other miner has already won the race and floated the block.
The lower the target value for difficulty, the smaller the set of valid hashes, and the harder it is to generate one. In practice, this means that these hashes start with a long string of zeros. For e.g. the bitcoin block 1 contains 8 preceding zeroes (low difficulty)

Whereas bitcoin block 429800 has 16 preceding zeroes (higher difficulty)

The difficulty value keeps adjusting periodically. In bitcoin, every 2016 blocks, each node calculates the difficulty, using a fixed formula. Depending on the time interval between the generated blocks, the new difficulty may go up or down. If the blocks are getting generated in less than 10 minutes (bitcoin block time = 10 minutes) the difficulty goes up and vice versa.

Due to the compute intensive nature of this process, it uses a lot of electricity, which is a cause of concern for the world!!
You must be logged in to post a comment.