A consensus algorithm is a mechanism that allows users or machines to coordinate in a distributed setting. It needs to ensure that all agents in the system can agree on a single source of truth, even if some agents fail. In other words, the system must be fault-tolerant (see also: Byzantine Fault Tolerance Explained).
In a centralized setup, a single entity has power over the system. In most cases, they can make changes as they please – there isn’t some complex governance system for reaching consensus amongst many administrators.
But in a decentralized setup, it’s a whole other story. Say we’re working with a distributed database – how do we reach an agreement on what entries get added?
Overcoming this challenge in an environment where strangers don’t trust each other was perhaps the most crucial development paving the way for blockchains. In this article, we’ll take a look at how consensus algorithms are vital to the functioning of cryptocurrencies and distributed ledgers.
Consensus algorithms and cryptocurrency
In cryptocurrencies, users’ balances are recorded in a database – the blockchain. It’s essential that everyone (or more accurately, every node) maintains an identical copy of the database. Otherwise, you’d soon end up with conflicting information, undermining the entire purpose of the cryptocurrency network.
Public-key cryptography ensures that users cannot spend each other’s coins. But there still needs to be a single source of truth that network participants rely on, to be able to determine whether funds have already been spent.
Satoshi Nakamoto, the creator of Bitcoin, proposed a Proof of Work system to coordinate participants. We’ll get into how PoW works shortly – for now, we’ll identify some of the common traits of the many consensus algorithms in existence.
Firstly, we require that users that want to add blocks (we’ll call them validators) provide a stake. The stake is some kind of value that a validator must put forward, which discourages them from acting dishonestly. If they cheat, they’ll lose their stake. Examples include computing power, cryptocurrency, or even reputation.
Why would they bother risking their own resources? Well, there’s also a reward available. This usually consists of the protocol’s native cryptocurrency and is made up of fees paid by other users, freshly-generated cryptocurrency units, or both.
The last thing we need is transparency. We need to be able to detect when someone is cheating. Ideally, it should be costly for them to produce blocks, but cheap for anyone to validate them. This ensures that validators are kept in check by regular users.
Types of consensus algorithms
Proof of Work (PoW)
Proof of Work (PoW) is the godfather of blockchain consensus algorithms. It was first implemented in Bitcoin, but the actual concept has been around for some time. In Proof of Work, validators (referred to as miners) hash the data they want to add until they produce a specific solution.
A hash is a seemingly random string of letters and numbers that’s created when you run data through a hash function. But, if you run the same data through it again, you’ll always end up with the same output. Change even one detail, though, and your hash will be completely different.
Looking at the output, you can’t possibly tell what information was fed into the function. They’re therefore useful for proving that you knew a piece of data before a certain time. You can give someone its hash, and when you later 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 sets out conditions for what makes a block valid. It might say, for instance, only a block whose hash begins with 00 will be valid. The only way for the miner to create one that matches that combination is to brute-force inputs. They can tweak a parameter in their data to produce a different outcome for every guess until they get the right hash.
With major blockchains, the bar is set incredibly high. To compete with other miners, you would need a warehouse full of special hashing hardware (ASICs) to be in with a chance of producing a valid block.
Your stake, when mining, is the cost of these machines and the electricity required to run them. ASICs are built for one purpose, so they have no use in applications outside of cryptocurrency mining. Your only way to recoup your initial investment is to mine, which yields a significant reward if you successfully add a new block to the blockchain.
It’s trivial for the network to verify that you have indeed created the right block. Even if you’ve tried trillions of combinations to get the right hash, they just need to run your data through a function once. If your data produces a valid hash, it will be accepted, and you’ll get a reward. Otherwise, the network will reject it, and you’ll 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 a PoS system, there’s no concept of miners, specialized hardware, or massive energy consumption. All you need is a regular PC.
Well, not all. You still need to put some skin in the game. In PoS, you don’t put forward an external resource (like electricity or hardware), but an internal one – cryptocurrency. Rules differ with every protocol, but there’s generally a minimum amount of funds you must hold to be eligible for staking.
From there, you lock up your funds in a wallet (they can’t be moved while you’re staking). You’ll typically agree with other validators on what transactions will go into the next block. In a sense, you’re betting on the block that will be selected, and the protocol will choose one.
If your block is selected, you’ll receive a proportion of the transaction fees, depending on your stake. The more funds you have locked up, the more you stand to gain. But if you attempt to cheat by proposing invalid transactions, you’ll lose a portion (or all) of your stake. Therefore, we have a similar mechanism to PoW – acting honestly is more profitable than acting dishonestly.
Generally, there aren’t freshly-created coins as part of the reward for validators. The blockchain’s native currency must thus be issued in some other way. This can be done either via an initial distribution (i.e., an ICO or IEO) or by having the protocol launch with PoW before later transitioning to PoS.
To date, pure Proof of Stake has only really been deployed in smaller cryptocurrencies. Therefore, it’s unclear if it can serve as a viable alternative to PoW. While it appears theoretically sound, it will be very different in practice.
Once PoS is rolled out on a network with a large amount of value, the system becomes a playing field of game theory and financial incentives. Anyone with the know-how to “hack” a PoS system would likely only do so if they could gain from it – therefore, the only way to find out if it’s feasible is on a live network.
Other consensus algorithms
Proof of Work and Proof of Stake are the most-discussed consensus algorithms. But there’s a wide variety of other ones, all with their own advantages and disadvantages. Check out the following articles:
Mechanisms for achieving consensus are vital to the functioning of distributed systems. Many believe that the greatest innovation in Bitcoin was the use of Proof of Work to enable users to agree on a shared set of facts.
Consensus algorithms today underpin not only digital money systems, but blockchains allowing developers to run code across a distributed network. They’re now a cornerstone of blockchain technology and are critical to the long-term viability of the various networks in existence.
Of all of the consensus algorithms, Proof of Work remains the dominant offering. An alternative that’s more reliable and more secure has yet to be proposed. That said, there’s a tremendous amount of research and development into replacements for PoW, and we’re likely to see more of them surface in the coming years.