A Consensus Algorithm is a mechanism used in computer systems and blockchains to obtain the necessary agreement on a single data value or network state between distributed processes or multi-actor systems, such as cryptocurrencies.
The blockchain saw its first real use case through the integration of a consensus algorithm with P2P and open source technology that, thanks to Satoshi Nakamoto, led to the birth of Bitcoin. Consensus algorithms allow the blockchain to be shared between the participants of that network in a secure, authentic and tendentially immutable manner; they consist of a set of rules and incentives, embedded in a game theory, that allow a distributed network to reach unanimity on its state.
The first use of a consensus algorithm is therefore found with the birth of Bitcoin, which to this day ensures its development and resilience, namely Proof of Work. Deemed too energy-intensive and polluting by its detractors, and in search of a solution with a lower environmental impact, in 2011 inspiration was taken from a protocol introduced by Wei Dai back in 1998, from his B-Money project, which can be defined as the forerunner of an alternative consensus algorithm to PoW, widely used to this day, the Proof of Stake.
The blockchain is a sequential structure in which information is stored in a chain of blocks. As the name suggests, these blocks are concatenated to each other, in chronological order and imprinted in this chain by the miners, the main actors of this protocol, who confirm the information within the blocks, distributing it to all the nodes of the network.
The blockchain is thus a large database that any user can go through and check whether the data entered, in this case the funds, have been previously spent, this because each time a transaction is made, reference is always made to the previous transaction, in a chronological order protected by cryptography that makes it both in theory, but also in practice as we shall see, immutable. The blocks are arranged chronologically and inextricably linked to one another by a chronological timestamp in an incontrovertible manner: each block is linked to the previous one by a hash function that cannot be inverted: the longer the chain of blocks, the more resilient it will be to tampering, even if a single block is altered: the greater the depth of the altered block, the greater the computational effort required to tamper with it, as we shall see later.
Proof of Work
This is a function that requires certain requirements to be met by performing work that is very costly in terms of time and energy, but which allows the requester to easily verify that these requirements are met.
In the case of the Bitcoin protocol, the PoW system used is the hashcash system introduced by Adam Back in his 1997 paper of the same name: although this function was originally created to filter spam in e-mails, in the Bitcoin blockchain, hashcash performs the function of a consensus algorithm where, in a distributed protocol such as Bitcoin‘s, it ensures that all participants (nodes) in the network can agree on the state of the network itself.
How does PoW work
In Proof of Work, the miners are the main actor in this mechanism: they put their computational power at the service of the network to confirm transactions. In order for them to be elected to choose the next block to be added to the blockchain, they must find a solution to a particular mathematical problem, they must then prove that they have done some work and solved that problem before they can be awarded the block containing the new transactions. In return for this service, miners will be rewarded with fees for the transactions to be validated and above all with the minting of new currency, all of which is called coinbase. The process just described is called “mining”, and it has a twofold feature: on the one hand it serves to secure the network by means of the miner’s “proof of work”, and on the other hand it issues new currency for the network, in a controlled and finite release protocol, which makes Bitcoin a deflationary asset.
In a decentralized context in which participants do not know each other and the concept of “trustless” prevails, this consensus is achieved thanks to the PoW that discourages the action of malicious participants by making this system secure, which is in fact referred to as “fault tolerance”. With the use of Proof of Work, Nakamoto was in fact able to solve a hitherto unsolved mathematical and game theory problem: the problem of Byzantine generals!
“The proof-of-work chain is the solution to the synchronisation problem, and to knowing what the globally shared view is without having to trust anyone”.
Going into detail about Bitcoin’s PoW
Every 10 minutes a block is “closed”, i.e. confirmed by the miners, and added to the blockchain; this process is carried out through Proof of Work: miners use their computing power to find the solution to the initial hash of the previous block, to do this they make consecutive attempts called “nonces” until they find the solution, the resolution and subsequent closing of a block is in fact a completely random process with many miners competing for it.
But what is a hash function?
In mathematical and computer language, a hash is a non-invertible function that maps a string of arbitrary length into a string of predefined length. There are numerous algorithms that compute hash functions with particular properties that depend on the application.
Miners then start from the previous hash and add the nonce to it, generating an alphanumeric string that will be the hash of the next block, and so on.
The system used in the Bitcoin protocol makes use of a SHA-256 algorithm that produces a hash that changes with any change in the data, even the slightest (called the “avalanche effect”), it is therefore impossible to derive the input of that hash from its output, the only way to solve this challenge is through very expensive continuous Proof of Work.
The first miner to find the solution to the “proof of work” broadcasts its solution to the entire network notifying all nodes that a new block has been detected, they will check the solution to the hash and, if valid, work on the next block. This work is highly redundant; all miners compete on the same set of transactions to find a solution.
“Proof of Work is necessary to ensure security, to prevent fraud and to perpetuate the veracity of the data”.
The protocol considers only the chain with the highest number of “proofs of work”, i.e. the longest and oldest, to be valid and authentic. A fraudulent chain is not practical in the long run because a miner has a low probability of consistently winning the block reward to maintain the chain. Over time, other miners will extend the valid chain faster than the tampered chain because it will be the one containing the most fees.
The main innovation introduced by Satoshi Nakamoto in his protocol is the use of Proof of Work (PoW) to create distributed consensus without trust between actors and at the same time solve the problem of double spending.
The blockchain is thus a decentralized system consisting of several actors, each acting on the basis of an incentive and the information available to them. Whenever a new transaction is transmitted to the network, the miners have the option of including that transaction in their copy of the ledger, but it is also within their power to ignore it. When the majority of actors in the network decide on a single choice, consensus is reached.
A fundamental problem in distributed systems with several actors who do not know each other is that of achieving overall reliability of the system and thus unanimous consensus. The difficulty in achieving this in a decentralized and distributed network is to require these actors, unaware of each other, to agree on common factors in the absence of trust. In this context, the introduction of consensus algorithms such as Proof of Work plays a key role in the Bitcoin protocol and similar protocols, solving all these problems.
“Proof-of-work has the nice property that it can be relayed through untrusted middlemen. We don’t have to worry about a chain of custody of communication. It doesn’t matter who tells you a longest chain, the proof-of-work speaks for itself”.
– Satoshi Nakamoto
In the next article, we will explore the Proof of Stake!