Consensus on the Blockchain

2018, Sep 05    

Besides the data structure factor, as discussed on the previous post, the Blockchain must have a consensus protocol/algorithm in order to be a useful Distributed Ledger. That is, there must be measures in place that prevent people from gaming the system, from double-spending, or forging the ledger in any way.

Double-spending

Double-spending is defined as a user spending the same coin more than once, or spending coins that he/she does not posses. Example:

  • an attacker Alice has 1 coin
  • Alice pays the 1 coin to Bob, for some provided service
  • but Alice wants to cheat the system, so she also tries to use the same 1 coin she just gave Bob, to pay Candice.
  • Alice’s trying to cheat the system. Don’t be like Alice!

Double-spending

A distributed ledger should be safe against this type of attack.

Blockchain Consensus

As previously said, the Blockchain is just a sequence of blocks, stored in a hashed linked-list data structure. Each block contains a number of transactions made by any of the users of the Blockchain. From time to time, depending on the specific Blockchain implementation, a new block of transactions is added to the official sequence of blocks.

To prevent the Blockchain against double-spending, and many other types of attacks, there’s a protocol called Consensus. It’s a protocal in which multiple users of the Blockchain verify the transactions that will be added in the next official Blockchain block.

The Consensus works as following:

  • Alice pays Bob 1 coin
  • this transaction is broadcasted to N of the closest users of the Blockchain (remember, the Blockchain system implements a peer-to-pper network architecture)
  • N of the closest users will choose to create a new block, full of transactions, including Alice and Bob’s
  • the fastest of the N users, lets call him Carl, will submit his new block to be the next official block in the Blockchain
  • some other M users of the network will verify Flash’s proposed new block, to check if the transactions within it are valid and come from actual users inside the Blockchain
  • if everybody agrees that the transactions are valid, Flash’s new block is put on the Blockchain!
  • depending on the exact implementation of the Blockchain, Flash could receive a reward for having successfully submitted the next block of transactions. Bitcoin, for example, would reward him a fixed amount of new bitcoins. This operation is called mining bitcoins

One important definition that I purposely left out concerns the fastest of the N users. Usually, this is defined by having a computational puzzle that the users that are trying to submit new blocks must solve in order to be able to do so. Again, the definition and inner-workings of this computational puzzle may vary depending on the specific Blockchain implementation. In Bitcoin, for example, is used a system called Proof-of-work.

Proof-of-work

In the Proof-of-work computational puzzle, the Blockchain specifies a zero-padded hash value, something like 000c8b3793b199122d2359a37..., containing 256 bits of information (256 characters). Now, every user that’s trying to propose new blocks for the blockchain, must solve for the following formula:

000c8b3793b199122d2359a37... < H(block + newBlock + nonce)

Where:

  • block is the last official block in the Blockchain
  • newBlock is the new block that’s being proposed by the user solving the puzzle
  • nonce is a random number that must be found by the user to make the whole formula true
  • H( ) is the hash value

To find the nonce the only way is trying every possible number, until one of them fits the formula. This nonce is then passed to the M users that will validate the new block, to check that the user proposing it did indeed solve the computational puzzle.

What defines the difficulty of finding out the nonce here is the amount of 0s to the left, which is defined by the Blockchain implementation.