Today’s lesson we’ll look at how consensus is achieved on the blockchain.
What is consensus on the blockchain? Consensus basically means that all nodes in a decentralised network must come to an agreement on what the truth is. For bitcoin, all nodes must agree on the transaction history. In a centralised system, all the participants trust that the authority will behave honestly and share the truth with the rest of the members. Since only the trusted party has the power to modify the data, it is straightforward to achieve consensus. Everyone simply accepts and believes what the central authorities says. For example, you simply trust that your bank will put the correct balance for your account, whenever you send and receive money. However in a decentralised network, there is no central authority, and each node doesn’t trust any other node. The challenge is, how can all these nodes agree on what the correct state of the shared data is? In other words, how can they all achieve consensus? With mutual distrust? In computer Science, this known as the Byzantine General’s the two generals problem, which was originally presented in 1982. The Byzantine General’s problem is a description of consensus problems in computer networks.
More specifically, how can distributed computer systems handle malfunctioning parts that give conflicting information to different parts of the system? This problem is abstractly described as a group of generals of the Byzantine army, camped with their troops surrounding an enemy city. The generals must agree upon a common battle plan, and they can only communicate with other using messengers. However, one or more of the generals may be traitors, who may try to confuse the others. The problem is to find an algorithm that ensures the loyal generals will all reach an agreement on the battleplan, regardless of what the traitors do.
In the case of Bitcoin, each general could be thought of as a node in the network, and all the honest nodes must agree on what the true history of the transactions is.
A malicious node can send conflicting transactions to different parties of the network. For example, Bob is a traitor, and he sends a transaction stating he sent 10 Bitcoins to Alice to one part of the network, while sending another transaction stating he sent 10 bitcoins to Carol to the other parts of the network. Let’s assume that Bob only has 10 bitcoins in total. So, he’s trying to double-spend his bitcoins. So, what algorithm can be used in a bitcoin network to ensure all the honest nodes recognise Bob sent 10 Bitcoins to Alice, but reject that he sent 10 bitcoins to Carol? Bitcoin use the Proof-of-Work algorithm to ensure all the honest nodes reach a consensus on the true history of transactions. The Proof-of-Work algorithm was first developed in the early ’90s, to prevent email spamming. It required computers that wanted to send an email to do some computation work, which took some time to complete before sending out the email. This reduced the amount of spam an email server could get in a given period of time.
In bitcoin, Proof of Work is used to govern the mechanics of how a new block is added to the blockchain. In the previous lesson, we learned that blockchains are append-only, and once a block is added it cannot be modified. Therefore, we need to ensure that all the honest nodes in the system will add the exact same block to their local copy of the blockchain, to achieve consensus. So how does Proof of Work achieve this?
First lets imagine all the nodes in the network are allowed to create a new block at any time, instantly. If this were the case, the network would get flooded with too many new blocks, and no one would be able to agree on which of the new blocks should be added to the blockchain.
However, in reality, in order for a node to create a new block, and broadcast that to the other nodes, it must do some computation work. The computation work is quite intensive, and for bitcoin, it takes roughly 10 minutes on average for any node to complete. Once a node completes this work, it broadcasts the block to the other nodes, who verify it.
Therefore, all the nodes in the network that want to create a new block must race against each other to be the first one to complete this computation, and broadcast their block. This way, all the other honest nodes will receive the new block and verify that the Proof of Work was valid, and that the transactions inside the block were also correct, and then they will add that block to their copy of the blockchain.
Two minor details worth mentioning. First, that in the bitcoin network, each node only connects to a small subset of peers directly. When a node creates a block, it only broadcasts this to its immediate peers. If the block is correct, and meets the Proof-of-Work requirement, then it’s peers will broadcast this block to their immediate peers, until eventually, all the nodes in the network have received this new block.
However if the node is malicious, and tries to broadcast a block without a valid Proof of work, the immediate peers will reject this block, and will not continue broadcasting that block to the other nodes. This prevents Denial-of-Service attacks, in which a malicious node continuously broadcasts incorrectly formed blocks, that flood the network with too many requests. Second, all the nodes that try to create a new block are called mining nodes, or miners, and each of the miners have a set of unconfirmed transactions they received, which are basically transactions that haven’t been added into the blockchain.
Each mining node can have a slightly different set of unconfirmed transactions they’ve received so far, due to the network latencies and other delays. Therefore, each mining node can pick a different set of transactions in the next block they’re creating. Thus, each of the mining nodes are creating new blocks that are different from one another, hence the need for consensus Proof of Work is one way to achieve consensus.
So, what exactly is the computation work being done, and how does a node show the proof? Proof of work involves cryptographic hash functions, which we learned about in our previous lesson. We learned, that given the same input into a hash function, we will always get the same output. However, trying to find an input based on a given output is computationally infeasible proof of work uses this principle to make the miners do computation work to guess what input, when hashed, will produce an output than is less than some difficulty number. The bitcoin network automatically adjusts the difficulty number based on the average time between blocks , or block time, such that it will take roughly 10 minutes for a node to find such an input. Lets look at how this Proof of Work is recorded in the bitcoin blockchain. In the previous lesson, we learned that each block has a unique hash block hash, used to ensure data integrity.
We mentioned that this hash is computed based on two inputs. The previous blocks hash, and the hash of the transactions included in that block. However, this isn’t entirely true. There’s also another parameter called a nonce, that is used to compute the current block’s hash. In bitcoin, the block hash is 256 bits, which represents a number. The computation work is trying to find a nonce such that the block hash is less than the difficulty number. In other words, the block hash must begin with a certain number of zeroes. So, basically, miners repeatedly try different nonce values until they can produce a block hash that satisfies the difficulty requirement.
This is the proof that the mining node presents to the other nodes in the network in order for them to accept this block as legitimate, and add it to their local copy of the blockchain. Note, that it is very easy. For the other nodes to verify the block hash is correct. they simply use the data in the block to compute the hash value, and verify that it has the same block hash. So, it is very computationally intensive to come up with a valid block hash, but it’s very quick to verify.
In this lesson, we learned about hoe consensus is reached on the blockchain using Proof of Work.
In the next lesson, we’ll look at a different consensus protocol called Proofof Stake.
Thank you to Niloo Ravaei
Thanks for reading stay tuned for some more Blockchain lessons.