Introduction

A consensus algorithm is a mechanism that allows users or machines to coordinate in a distributed environment. It ensures that all agents in the system agree on a single source of truth, even if some agents fail. That is, the system must be fault tolerant (see also: Byzantine Fault Tolerance Explained).

In a centralized system, a single entity powers and controls the network. In most cases, the entity can make changes as it sees fit – there is no complex governance system for multiple directors to reach consensus.

In a decentralized system, it's a completely different story. Let's say we're working with a distributed database - how do we agree on what data to add?

Overcoming this challenge in an environment where participants do not trust each other was perhaps the most crucial development that paved the way for blockchains. In this article, we will see why consensus algorithms are essential to the functioning of cryptocurrencies and distributed ledgers.


Consensus algorithms and cryptocurrencies

In cryptocurrencies, user balances are recorded in a database – the blockchain. It is essential that everyone (every node to be precise) maintains an identical copy of the database. Otherwise, conflicting information may emerge, harming the entire network.

Public key cryptography ensures that users cannot spend each other's currencies. However, there must still be a single source of truth that network participants rely on to determine whether funds have already been spent.

Satoshi Nakamoto, the creator of Bitcoin, proposed the Proof of Work system to coordinate participants. We'll see shortly how PoW works – for now let's identify some common traits among many existing consensus algorithms.

First, we ask users who want to add blocks (we'll call them validators) to provide a stake. Staking is a value that a validator must pledge in order to deter them from acting maliciously. If they cheat, they will lose their stake. For example computing power, cryptocurrencies or their reputation.

Why would they risk their own resources? Quite simply because there is a reward at stake. This is generally a native cryptocurrency of the protocol which is based on fees paid by users, on freshly created cryptocurrencies or both.

The last thing we need is transparency. We need to be able to tell when someone is cheating. Ideally, it should be expensive for them to produce blocks, but cheap for anyone to validate them. This ensures that validators are also controlled by ordinary users.


Types of Consensus Algorithms

Proof of Work (PoW)

Proof of Work (PoW) is the godfather of blockchain consensus algorithms. It was first implemented with Bitcoin, although the concept has been around for a long time. In Proof of Work, validators (called miners) hash the data they want to add until they produce a specific solution.

A hash is a random string of letters and numbers created when running a hash function. But if the input data is the same, the output results will be the same. However, the slightest change will generate a completely different hash.

The data at the output does not allow us to determine the data at the input. This is therefore a very effective function to prove that you have known a piece of data for a certain period of time. You can give someone their hash and when you reveal the data, that person can run it through the function to make sure the output is the same.

In Proof of Work, the protocol defines the conditions that make a block valid. This could, for example, say that only a block whose hash begins with 00 is valid. The only way for a miner to create a valid block is to brute force. To produce a different result until the right one is found, miners can modify a parameter in their data.

For the largest blockchains, the level is very, very high. To compete with other miners, you need to have a warehouse full of ASICs, hardware specifically designed to increase your chances of producing a valid block.

Your stake, in mining, corresponds to the costs of these machines and the electricity necessary to run them. ASICs are designed for the sole purpose of mining, so they have no uses outside of cryptocurrency mining. The only way to recoup your initial investment is to mine and earn rewards by successfully adding a new block to the blockchain.

It is very easy for the network to verify that you have created the correct block. A single execution with the function allows you to verify the results of your numerous tests. If your data produces a valid hash, it will be accepted and you will receive a reward. Otherwise, the network will invalidate it and you will have wasted time and electricity for nothing.


Proof of Stake (PoS)

Proof of Stake (PoS) was proposed in the early days of Bitcoin as an alternative to Proof of Work. In PoS, the concepts of miners, specialized hardware and massive energy consumption do not exist. All you need is a computer.

And a little something... You have to put a coin in the machine. In PoS, instead of using an external resource (machines and electricity), you will use an internal resource (a cryptocurrency). The rules differ depending on the protocol, but you will generally need to hold a minimum amount to participate in staking.

You will then need to lock your funds in a wallet (they cannot be moved when you stake). You will generally agree with the other validators on which transactions will pass into the next block. In a sense, you bet on which block will be selected and the protocol decides the rest.

If your block is selected, you will receive a portion of the transaction fees, proportional to your stake. The more funds you lock, the more chances you have of winning. But if you try to cheat by offering invalid trades, you will lose part (or all) of your stake. Therefore, we have a mechanism similar to PoW – acting honestly is more profitable than acting dishonestly.

In general, newly created currencies are not part of validator rewards. The native blockchain currency must therefore be issued in a different way. This can be done through an initial distribution (e.g.: an ICO or an IEO) or through the use of PoW at the beginning of the protocol before its transition to PoS.

To date, pure Proof of Stake is only used by small cryptocurrencies. Therefore, it is unclear whether it can serve as a viable alternative to PoW. Although this seems theoretically valid, it will be quite different in practice.

Once PoS is deployed on a network with a large amount of information, the system becomes a perfect breeding ground for financial incentives and game theory. Anyone with the know-how to “hack” a PoS system will only do so if it is possible to take advantage of it – which is why the only way to find out if it is possible is to try directly on the network .

We will see large-scale testing of PoS shortly – Casper will be implemented as part of a series of upgrades to the Ethereum network (also known as Ethereum 2.0).


Types of Consensus Algorithms

Proof of Work and Proof of Stake are the most common consensus algorithms. But there are obviously many others, with all their advantages and disadvantages. Check out the following articles:

  • Le Delayed Proof of Work expliquĂ©

  • Le consensus Leased Proof of Stake expliquĂ©

  • Le Proof of Authority expliquĂ©

  • Proof of Burn explained

  • Le Delegated Proof of Stake expliquĂ©

  • The Hybrid PoW/PoS consensus explained


To conclude

Mechanisms for achieving consensus are vital for the operation of distributed systems. Many believe that Bitcoin's greatest innovation is the use of Proof of Work to allow users to agree on a shared set of facts.

Consensus algorithms today underpin not only digital currency systems, but also blockchains, which allow developers to execute code across a distributed network. They constitute a cornerstone of blockchain technology and are essential to the long-term viability of the various existing networks.

Of all consensus algorithms, Proof of Work still remains the most used. Indeed, a more reliable and secure alternative has not yet been proposed. That said, there is a tremendous amount of research and development going into replacing PoW and it is highly likely that we will see more consensus emerge in the years to come.