DEV Community

kim-jos
kim-jos

Posted on • Edited on

What is a Blockchain?

What is blockchain?
According to Wikipedia, it is a list of records, or blocks, that are linked together using cryptography.

On a very basic level, a block consists of four main things:

  1. data - any data that you want to store
  2. previous hash - this connects the block to its previous block
  3. hash - code that identifies the block
  4. nonce - arbitrary random number

There are five important concepts of blockchains:

  1. Encryption(SHA256)
  2. Immutable ledger
  3. Distributed p2p ledger
  4. Mining
  5. Consensus protocol

Encryption(SHA256)
This is the code that you can identify any block with. Imagine it as the fingerprint of the block. SHA256 is the encryption tool that creates this fingerprint for each block. SHA stands for secure hashed algorithm and the 256 stands for the number of bits the algorithm has which is 64 characters in length.

Five important characteristics of SHA256 are as follows:

  1. Deterministic - the same input outputs the same hash each time
  2. Avalanche Effect - a minor change in the input outputs a completely different hash
  3. One-way - you cannot reverse engineer the hash meaning you cannot determine the input using the output
  4. Fast computation
  5. Withstands collisions - different inputs cannot have the same output

Immutable ledger
Let's first define the two words. Immutable means unchangeable. A ledger is basically a list of all business transactions. For example, if you went to a nearby store and paid for a coke with your debit card then that transaction is stored in your bank ledger. Why is this ledger important? It's important because that's the way we can keep track of how much money you have left in your account. If you claim that you never bought anything in the store the ledger is the evidence used to prove that your claim is false.

How is this relevant to blockchain?
It's relevant because blockchains can serve as this ledger that's immutable. For example, if you wanted to buy a house then you would pay for it and then register yourself as the owner of the home in some government institution. That way no one can arbitrarily claim that your home is theirs because the government institution serves as the ledger in this case. But with blockchain we are ridding this government institution and replacing it with blockchain. There are many benefits to this because no one can tamper with the data. If someone tries to change the data in the blockchain then it would change the hash of the blockchain which would invalidate the blockchain with the tampered data.

Distributed p2p ledger
Like before, let's define the words first. Distributed just means spread out and p2p which stands for peer-to-peer means links between peers.

How is this relevant to blockchain?
Basically the blockchain is stored not in one central place but distributed among all computers on the network. In other words, all the computers on the network have a copy of the blockchain. Whenever a new transaction is made on a certain computer the transaction is signaled to all the other computers on the network where they make a copy of the newly added blockchain.

Why is this necessary?
This p2p networking makes it extremely difficult for anyone to tamper with blockchain. When someone tries to tamper with a blockchain by, for example, trying to change the name of the owner of a house all the other computers on the network will realize that it was getting tampered with because the majority of the computers on the network have a different copy of blockchain. This prevents the new tampered block from being attached to the chain of blocks. We will go into more detail regarding this issue in the consensus protocol part below.

Mining
Let's review once again what a blockchain is composed of:

  1. data - any data that you want to store
  2. previous hash - this connects the block to its previous block
  3. hash - code that identifies block
  4. nonce
  5. blockchain number

The hash of a blockchain is generated as a combination of the blockchain number, previous hash, data, and nonce. The miners basically mine blockchains by finding the nonce that generates the hash. Only the nonce is computed by the miners because it wouldn't make sense to change anything else. For example, you can't change the data because that would be tampering with the data and you can't change the previous hash because that would break the connection with the previous blockchain. So, the way these blockchains are mined is by someone or something finding the number that goes in the nonce. Miners use brute force to guess the nonce of the blockchain. Whoever guesses the nonce first gets a financial reward in the form of coins in the case of Bitcoin.

Consensus protocol
Consensus means majority opinion and protocol simply means some rule. The reason we need this consensus protocol is because the blockchain uses a p2p network like we discussed above. An important concept that is frequently mentioned is the Byzantine General's Problem. It deals with the problem of who to believe which is very important in blockchain because it is also composed of a network of computers.

What is the Byzantine General's Problem?
Assume there are 5 Byzantine generals and one of them is a traitor. They need to either attack an enemy base or retreat. Now the generals need to be able to distinguish whether a signal is coming from a fellow general or a traitor. The way they do this is by tallying the number of signals they receive from each other and, of course, majority rules. So, if there are 5 people you will receive 4 signals in total (excluding your signal). If there is one traitor then you would receive 3 signals of the same kind which is majority. Now this would not work if more than 30% of the generals are traitors.

How is this relevant to blockchain?
This is relevant to blockchain in the sense that all the computers on the blockchain network are like the generals. They need to be able to tell whether the signals they receive from the other computers are valid or not and they do this by the number of signals they get.

Now how do we actually implement this in real life?
There are two main consensus protocols: Proof-of-Work("PoW") and Proof-of-Stake("PoS"). These consensus protocols allow other computers to distinguish whether a signal is valid or not.

This post will discuss PoW because Bitcoin implements PoW and will provide a good foundation on how other consensus protocols work.

We have already briefly discussed how PoW works in the blockchain mining section. The miners need to brute force their way to find the nonce that generates the hash of the blockchain and once they do they are rewarded financially with a coin. This is PoW.

One main problem is that two different blocks can be added to the chain. Say about 70% of the computers on the network have block "A" and the rest have block "B". What happens then? The network waits for another block to be added to the chain. The longer chain usually wins. What this means is that the longer chain is copied over to the other computers that had block "B" and block "B" is removed from the chain. Whichever side has more computing power has a higher probability of finding the next nonce that generates the hash which is why this is called PoW. The computers that have 51% of the computing power usually wins majority.

Top comments (0)