3. Understanding the consensus mechanism

A peer-to-peer mechanism was used in ’99 already by Napster.

The blockchain, too, already existed before Bitcoin.

The ingenious part of Satoshi Nakamoto’s, the mysterious, anonymous founder of Bitcoin, white paper was combining the two with a consensus mechanism based on cryptography. The consensus mechanisms is where the real magic happens: it allows nodes in a peer-to-peer network to work together without having to know or trust each other.

“The purpose of a consensus algorithm is to allow for the secure updating of a state according to some specific state transition rules, where the right to perform the state transitions is distributed among (…) users which are given the right to collectively perform transitions through an algorithm” — Vitalik Buterin

Now, if you didn’t quite catch that, the consensus mechanism is simply a set of rules, which is agreed upon by the nodes in the network by running the software of the network. These rules make sure the network works as intended and stays in sync.

The consensus protocol sets rules on:

How blocks are to be added to the chain,

when blocks are considered to be valid, and

how conflicts of truth are resolved.

Adding blocks to the chain

Different blockchains add blocks to their chains in different ways. The most-known consensus mechanism is Bitcoin’s Proof of Work (PoW).

The first rule of Proof of work is that one block should be added to the blockchain, on average, every ten minutes.

The process to facilitate this is called ‘mining’. Nodes that try to add a block to the chain (called ‘miners’) use computational power of their computers to try and solve a cryptographic ‘puzzle’. The rules state that only when this puzzle is solved, a block can be added to the chain.

The miner that solves the puzzle, and therefore ‘mines’ the new block to add to the chain, is rewarded by the network. A predefined amount of new coins is created and given to him, together with the transaction costs of all transactions contained in the block.

Subsequently, all miners will start mining the next block.

A bitcoin mining rig. To have enough processing power to mine, you need a lot of GPU’s.

Bitcoin’s Proof of Work is not the only consensus mechanism. Proof of Stake (PoS) is also commonly used in distributed ledgers. In a Proof of Stake based mechanism, one can ‘stake’ his coins for a chance to be picked to add the next block. In a sense, a staker says: “ I bet my coins that I add this block correctly”. If he lies, he loses his coins.

There is a larger debate on which consensus mechanisms is best. Still, regardless of how the block is created, other nodes in the network still have to be able to decide on whether the block is valid or not.

The validity of blocks

When a miner solves the puzzle and mines the block, all the nodes in the network will check if the block is valid and add it to their copy of the chain. The nodes first need to reach consensus on the validity. Only then the network synchronises and the state of the blockchain updates.

Nodes will only add a newly mined block to the chain if it follows the rules stated by the consensus mechanism. The software they run checks if a block is valid or not. An invalid block will simply be rejected.

Naturally, a block is only as valid as the transactions contained therein. Using Bitcoin as an example, the rules of the protocol state that nobody can send bitcoins they have not first received from someone else or from mining a block.

In other words, the nodes’ software check for all the transactions in a block if the senders have received enough Bitcoin to make their transactions. To do this, they check the state of the network.

Now, let’s say I have received 1 Bitcoin, which I later send to Alice. I then try to send the same Bitcoin to Bob. As soon as the block with my first transaction in it is added to the chain, all nodes will update the blockchain to reflect that I don’t have that Bitcoin anymore. Any newly mined block that would contain my transaction to Bob would simply be rejected by the nodes. The nodes’ software sees that the block does not follow the rules, thus, they don’t update the blockchain for this block.

The rules also dictate that a transaction is only valid when it is signed with a digital signature from the owner of the bitcoin. Only the person who controls the wallet or address from which the bitcoin is send can sign of on the transaction. Therefore, only you can spend your Bitcoin.

How conflicts of truth are resolved

It can occur that, on accident, two miners add a valid blocks to the chain at the same time. Imagine that a part of the nodes have accepted one valid block and another part has accepted another block. The first group included a block with my transaction to Alice and the other included my transaction to Bob. Now we suddenly have have two different states of the blockchain at the same time!

We call this an (unintentional) ‘fork’: The blockchain has forked in two different chains. Does Alice or Bob have my Bitcoin? Which of the two chains is the ‘true’ blockchain?

Generally, all consensus protocols solve this problem with a simple rule: The longest chains wins.

Visualisation of blockchain fork.

When we have an unintentional fork, some miners will start mining on one chain, and others will start mining on another chain. Inevitably, one chain will have more miners than the other, and as such, will add new blocks to its chain faster. The rest of the miners will then switch over to the longer chain and the forked chain will always die out. No damage is done to the main chain.

Why do we know this will always happen?

Because miners are economic actors that act in self interest. A miner has no interest in mining on a forked chain, knowing it will die out. All transactions (state transactions) on the forked chain have never occurred on the main chain, which means that miners who mined blocks on the forked chain are not rewarded for their work. The miners’ opportunity cost of mining blocks that will not be included in the main chain is simply too high.

In rare occasions, it can occur that a forked chain has a substantial amount of mining power. In this case, it can take a while before it is clear which chain is the main chain. Conventional wisdom dictates that it is therefore wise to wait for 6 blocks to really confirm a transaction.

Visualisation of an extended blockchain fork. This almost never happens for more than 5 blocks.

The rule that the longest chain wins, combined with the fact that one needs immense processing power to add blocks to the chain makes the blockchain incredibly secure. Pretty much the only way to attack the network is by going back to some block in the blockchain, forking the chain and starting to mine new blocks from there. However, to do so, an attacker would have to re-do all the work done by miners from that point and catch up to the main chain. Without having more computational power than the whole network of miners combined, this is simply impossible. Simply said, the electricity and GPU’s needed for such an operation would be far to costly.

Conclusion

The above should be seen as a very brief introduction to how distributed ledgers work. You might now understand why “blockchain” is a bit of a misnomer for the distributed ledgers we see today.

However, next time someone mentions blockchains at a party, please don’t do what I just did and go: “Hey man, a blockchain is only a way to structure data. I think you mean to refer to a distributed ledger used for record-keeping, which is hosted on a peer-to-peer network of participating nodes and miners whose cooperation is both enabled and governed by a consensus protocol which states rules of the network.”

Instead, it might be time to accept that the term has evolved to become an overarching concept to indicate the synthesis of the technologies that make distributed ledgers possible.

In my next article, I will delve into the role that modern cryptography plays in the blockchain and the consensus mechanism.

If that is not an exciting prospect then I don’t know what is…