BLOCKCHAIN CONSENSUS MADE SIMPLE

Oladokun Oladapo
8 min readJul 11, 2021

BlockChain is one of the biggest trends right now on the internet and also one of the least understood, with most people having little to no idea how it works. The word “decentralized” is what a lot of people attribute to it.

Yes, it is decentralized, which is more like the most powerful tool in its arsenal. However, removing a central authority is a recipe for disorganization, fraud, and decision-making becomes a nightmare for obvious reasons.

In this article, we are going to briefly explain blockchain as elementary as possible, then go deeper into understanding how consensus is possible in such a decentralized system.

Talking about “Blockchain”, the name explains it all. It’s a chain of blocks. Well, what is a block?

A block is nothing but a list of transactions (ledger) and a few other technical information.

Think of a block as a book containing different transactions. Once you are done filling a book with transactions and you are sure they are 100% accurate, you stack the book on the shelf. Then, you open another book and write new transactions, and when complete, you stack it on the previous one.

With that, you already have a chain of books containing validated transactions. Once you have completed a book and verified all transactions and stacked it, you cannot change it again, and you can’t rearrange the books as those books are linked to each other.

That’s as easy as it gets, and the mystery of blockchain is gone.

However, in a blockchain system, things are a bit different. It is decentralized. This means it’s not only you that can write transactions in a book and stack it. Random people all over the world can do that too. There is only one shelf (blockchain), and everyone has access to the shelf, and anyone can stack a book (block) on the shelf (blockchain). Once another block is added to the blockchain, every single node is updated.

A node is a computer that connects to the blockchain network, has a copy of the blockchain, participates in the validation of transactions, enforcing the rules, and even mining a new block (creating and adding a block to the blockchain).

Since tens of thousands of nodes have the same copy each time, if you decide to alter your own node, it’s not going to match with others. With that, it’s easy to tell if a particular node is trying to manipulate the blockchain, and of course, it’s rejected. This is why it’s nearly impossible to alter anything in the blockchain.

With decentralization comes several problems. How do we make sure we don’t have blocks containing the same transactions stacked by different people? How do we make sure people don’t write fraudulent transactions and put them in the blockchain? How do we even agree which blocks/transactions are valid or not since these validators (miners) don’t even know each other? How do we agree on which node to add its own block into the Blockchain?

These are several problems with several solutions, but we want to talk about how these tens of thousands of anonymous nodes agree (have a consensus) on what blocks are valid and accurate to be added to the blockchain.

Consensus in a decentralized blockchain system

To describe the problem more accurately, there are several thousands of nodes, each creating a block to be added to the blockchain. Only one can be added at a particular time, and the accepted one must be accurate and not malicious. All the nodes have to reach an agreement for the block to be added. If there are malicious nodes that do not agree with the consensus or pass in a malicious block, then that’s also a problem to be solved since consensus still needs to be reached anyway.

This is usually regarded as the Byzantine’s General Problem, which is a logical dilemma conceived in 1982 about a group of generals about to attack a city. These groups of generals are far apart and need to communicate to know exactly if they should attack or retreat, and at what particular time. They can only win if they all attack at the same time. Since the communication could be easily disrupted or one of the generals could deliberately act maliciously by passing false information to the next general, there needs to be a way to solve this problem and have the correct information passed to the generals.

This gave rise to the Byzantines Fault Tolerance (BFT), which takes into consideration, the possibility of nodes failing to communicate or even act maliciously, yet ensures consensus is still reached anyway by the honest nodes. There are several ways to operate a Byzantine Fault Tolerant Blockchain, and they are regarded as Consensus Algorithms.

Consensus Algorithms are often used to make all the nodes come into an agreement, and there are several of them, in which we would talk about the two most popular.

Proof of Work

This is presently the most popular consensus algorithm since it’s used by the Bitcoin blockchain for validating transactions and adding new blocks into the bitcoin blockchain.

All mining nodes create new candidate blocks (blocks that have chances to be added into the blockchain) in which the only one will be accepted and added to the blockchain. Once a miner creates a block with unconfirmed transactions pulled from the mempool (the pool of unconfirmed transactions), the block has to participate in a very difficult and intensive mathematical puzzle. The first node to correctly solve it is the winner and its block is sent to other nodes to verify all the transactions in the block. It’s easy for other nodes to validate the authenticity. The block is then accepted to be added to the blockchain and the miner is rewarded.

This computation requires expensive equipment and substantial energy and when a node successfully solves the puzzle, it has proved that it has done enough computational energy to mine the particular block. That is the proof-of-work

What is this mathematical puzzle and how does it work?

A target hash (a numeric value that a hashed block header must be less than or equal to) is given. This target hash is determined entirely by the difficulty (which in its turn is determined by the timestamps of the previous blocks in the chain it is built on). In other words, it’s adjusted to being more or less difficult based on how long it took the previous blocks in the chain to meet the former target hash.

The miner simply has to hash three things; the hash of the previous block, the set of transactions to be added to the blockchain, and a nonce (an arbitrary number used only once in a cryptographic communication). The resulting hash must be less or equal to the target hash.

Let’s see a simple example of a hash:

If you hash the word “Hello Oladapo” using the sha-256 hashing algorithm, you get “b271626dd1b2348625d0a7b52c35156c1954007a98678f201d4517d383923fd9”. If you make the slightest change to the word, you’ll get an extremely different hash value.

Let’s change the letter “O” to lower case. “Hello oladapo” is going to give “70548770fb84ef4ef52635d45fbe895ec0096370c4662ceecb79b157a0ea2e9a”, which is a completely different hash value.

Back to the puzzle, once the block has been created, the list of transactions is already in the block with the hash value of the previous block. The miner has to keep passing different random strings as nonce until he finds the nonce value that meets the target requirement.

It’s usually based on the number of leading zeros in your hash. A miner has to find a nonce value that gives a hash value with a certain amount of leading zeros.

The 690557th block, which was mined on 11th of June 2021 for example has the hash value of “0000000000000000000065bda8f8a88f2e1e00d9a6887a43d640e52a4c7660f2”. This hash has 20 leading zeros. This means that the target hash required the miner to find a nonce value that if hashed with the other block details, gives a hash value with at least, 20 leading zeros.

Let’s compare that with the first bitcoin block ever mined on January 03, 2009 with the hash value of “000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f”. This only has 10 leading zeros, which means the target hash is higher, therefore is way easier to compute.

The more leading zeros are needed in the hash, the more difficult it is to compute.

It’s a brute force approach as you have to keep guessing until you find the right nonce value, and miners need to make trillions of guesses until the one emerges.

How does proof-of-work solve the consensus problem?

Mining a new block is a very expensive operation and miners are incentivized, to be honest. Even if a miner meets the target hash on time and has a proof-of-work, the block will be rejected by other nodes if fraudulent. This is called an orphan block and all the expensive compute power and equipment for performing the proof-of-work would be for nothing. Therefore, every miner is compelled to do the right thing to stand a chance of being accepted and earning the reward.

Blockchain transaction Illustration from euromoney

Proof of Stake

This is an alternative consensus algorithm to the proof-of-work already in use by altcoins such as Cardano and proposed in Ethereum 2.0

In proof-of-stake, the node to forge (create and add) the next block to the blockchain has to stake (invest/lock) some amount of cryptocurrency. The higher the miner stakes, the higher he has of being selected. However, the amount staked is not the only criteria as this may lead to only a few elites controlling the entire blockchain. The algorithm also includes randomization and the age of staked coins. The longer a miner has staked, the better the chances.

With this, instead of spending lots of money buying and consuming computational power, you only buy and stake coins, which you can still withdraw if you are no longer interested in participating in the network.

The proof-of-stake makes it difficult to attack the network as you’ll need at least 51% of total staked coins to successfully attack it. This is incredibly expensive for large cryptocurrencies. Also, if a miner owns 51%, attacking the network is to his own detriment.

How does proof-of-stake help in consensus?

Just like proof-of-work, miners are incentivized to do the right thing and punished for trying to cheat the system. Since a miner has to stake some amount, if a selected miner tries to submit a fraudulent block, other nodes are going to reject it and the miner will lose some or all of his stakes and be removed from the network.

There are several other consensus algorithms including Proof of Authority, Delegated proof of stakes, proof of Elapsed time, and a couple of others.

--

--