What is Blockchain? How does it work? Why do we need it?
Tags: blockchain, web3, beginners
Published: 2022-06-11 10:39:35

In the past few years, the term blockchain has gotten a lot of attention from major media outlets and the public. It's a fairly new technology that has proven to benefit many companies in different industries. It is relatively easy to understand, although it can seem difficult for beginners. This article will explain how it works and why it’s so revolutionary.

## What is Blockchain?
Blockchain is a decentralized and public information database. It's the technology that powers cryptocurrencies like Bitcoin, Ethereum, and Litecoin. Simply put, a blockchain is a database with additional functionalities.

It is not really a database, but rather a ledger. A ledger is a record that contains transaction details such as who sent who, how much, and when the transaction occurred. Unlike a traditional database, data added to the blockchain ledger cannot be changed or removed.

![ledger](https://i.imgur.com/EkUBTks.png)

### Why do we need Blockchain?
Assume Alice owes Joe $100 and has agreed to pay him in three days. They can both agree by recording it on a ledger (a pinky promise?).

A lot can happen in three days.
- Alice has the possibility of reducing the payment from $100 to $10.
- Alice can change Joe's name to something different.
- Alice can easily remove the record.
- The ledger can be hacked.

![editing ledger](https://i.imgur.com/yF4DUPz.png)

This is terrible! What should we do now? The ledger must be encrypted. You may now ask - "Why do we need such a complex technology like Blockchain for this? Aren't powerful encryption techniques used in modern systems? ". No, there will always be a way to bypass current encryption algorithms, or an administrator may become corrupt and leak or alter all data.

**Once data is added into a blockchain ledger, it cannot be changed or erased. It will remain in place permanently.**

But wait! There's another problem.
Assume that Alice and Joe are best friends. Alice is a trustworthy individual who will honor her obligations. Now, Alice does not always keep her money in her pockets or at her house. No one else, of course. She deposits her funds into a bank account.

A lot of things can happen to the bank.
- The bank may declare bankruptcy.
- Some money heist shit can happen 👀.

![bankrupt](https://i.imgur.com/5mSfa2N.png)

This is just another disaster! Because your account is managed by a centralized bank, they have access to your funds. "Don't we have legislation, security, and whatnot to protect our money?" you may ask again. They cannot secure your money completely, and you must waste years chasing after courts. What do we do now?

Blockchain technology has the potential to fix this problem. Blockchain is **Distributed Ledger Technology** (DLT), which means your data is distributed. Your data is not controlled by a single company, person, or authority. This is accomplished by replicating the datastore over a number of internet peers.

So blockchain's two key features - 1) Immutability and 2) Distributed ledger - made it a revolutionary technology.

## How does Blockchain work?
Let's see how these two key features - 1) Immutability and 2) Distributed ledger works.

To encrypt data, Blockchain implements **SHA-256** hashing. Unlike basic encryption methods such as the **[Caesar Cipher](https://en.wikipedia.org/wiki/Caesar-cipher)**, SHA-256 is a one-way function, which means you can calculate the hash with your data but not decrypt it the hash.

How does SHA-256 work?

- You enter data -> the SHA-256 algorithm computes the hash and generates a 64-character hash.
- It will always generate a fixed size (64 ch) hash regardless of the size of the data.

![sha-256](https://i.imgur.com/6l6MbLq.png)

- Try out SHA-256 [here](https://guggero.github.io/blockchain-demo/#!/hash)
- SHA-256 algorithm [implementation in JavaScript](https://www.movable-type.co.uk/scripts/sha256.html#src-code)

Let's understand how the Bitcoin blockchain works.

#### Bitcoin blockchain data structure
Every block in Bitcoin blockchain contains,

- **Block number** - The block number in the chain.
- **Nonce** - A number used to validate a block. Adjusted by miners to meet conditions that make a block valid.
- **Data** - The block's actual data.
- **Previous Hash** - The previous block's hash.
- **Hash** - The hash of the current block. Calculated by appending all the above elements and hash them.

![bitcoin block](https://i.imgur.com/OrwSYAN.png)

So we put our data in the block and hash it with a nonce. (To validate a block, we must change the nonce value until the number of initial zeroes in the hash matches the difficulty level of the block. Miners are responsible for this. More on nonce - [here](https://en.bitcoin.it/wiki/Nonce).)

![nonce](https://i.imgur.com/QRJ52IX.png)

If you try to change anything in the block, the block goes into an invalid state because the hash will change.

![invalid block](https://i.imgur.com/30C5f1W.png)

This is all about how a block works.

How do we add blocks to the chain?
To add a block to the chain, we compute the preceding block's hash and store it in the newly added block. This is how all of the blocks are connected.

![blockchain](https://i.imgur.com/en66LiI.png)

If you modify anything in any block, all subsequent blocks will become invalid since all blocks are linked and any change in the hash in one block impacts the hash in all subsequent blocks.

![blockchain containing invalid blocks](https://i.imgur.com/GhmodyW.png)

This is how a blockchain achieves immutability. Now Alice can't cheat 😂.

You can see how this works visually [here](https://guggero.github.io/blockchain-demo/#!/blockchain)

Now let's understand how distributed ledger works.

We saw how blockchain achieves immutability, but what if data is changed and the hash is recalculated? All of the blocks are now valid again. This is precisely the issue with the current centralized approach.

However, blockchain has another feature: distributed ledger. This means that the ledger is shared across a number of peers (you can also say computers or hosts). The hashes of all blocks will be the same for all peers.

![distributed ledger](https://i.imgur.com/GgFglia.png)

If any peer attempts to modify the data and re-validate the blocks, the ledger will no longer match the other ledgers. As a result, we can conclude that the peer is guilty. For example, in a room of 10 students, assuming everyone has the same copy of the question paper. A single person cannot simply change and claim that he received a different paper.

![guilty peer](https://i.imgur.com/ey1DLlW.png)

But there is a risk with distributed ledger technology: what if 6 out of 10 students altered the question paper? Who's guilty now? Blockchain is constructed on the idea that honest peers are in the majority and the above situation is unlikely to happen.

All of this has resulted in the creation of cryptocurrencies such as Bitcoin, where there will be no banks, no intermediaries, and no third-party individual controlling your money.

However, the major breakthrough occurred following the introduction of **Etherium**. There will be more on this in future articles.

---

I tried to explain blockchain in a simple manner. I hope you enjoyed it. **Follow for more**.

Also please join my **[#100DaysOfWeb3](https://twitter.com/rakesh_at_tweet/status/1532584019158011904?s=20&t=NcleyHGFFGi_w27H3GdXdw)** challenge.