How Cryptocurrency Works Explained Visually
Tags: blockchain, web3, beginners
Published: 2022-01-29 13:30:52

In 2008, when Lehman Brothers Holdings Inc. filed for bankruptcy, it shook people's faith in banks so much that a new class of assets appeared without formal bank backing. The first cryptocurrency named Bitcoin was invented in 2008 by an unknown person or group of people using the name Satoshi Nakamoto. The idea was to make a decentralized open source digital currency, without a central bank or single administrator. Here, is the [link to the proposed whitepaper by Satoshi Nakamoto](https://bitcoin.org/bitcoin.pdf)

In this article, we will explore how cryptocurrency actually works, from transactions to data storage. Since, Bitcoin is the first and most well-known cryptocurrency, I will focus on it in this article.

## Bitcoin ≠ Blockchain

It is a common misconception that people use the word **blockchain** and **Bitcoin** interchangeably, but that's not true

Bitcoin is not just a coin but a protocol based on the technology of **Blockchain.** Protocol is set of rules that guides the participants on the network how to communicate with one another. An example of a protocol for bitcoins is how public and private keys for authentication should be managed, how mining should be done for transactions to be confirmed, etc. Ethereum, waves, neo, and ripple are a few other examples of similar protocols like bitcoin.

This protocol, like Bitcoin and Ethereum, has a coin that is typically named after the protocol name, which enables human interaction. This coin is used to reward people who mine, to add blocks to the blockchain, and, most importantly, to buy stuff from one another.

Tokens are layer 3 and rely on smart contracts that are constructed on protocols from layer 2. Ethereum is the most popular protocol for creating smart contracts and for creating token. Bitcoin protocol does not have token because it does not support creating smart contract on their protocol.

![blockchain protocol](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qspotuocjtfpf6ij68so.png)
 
## A Secure Alternative to Traditional Banks 🤔

To truly comprehend cryptocurrencies, let us first examine how blockchain and traditional client-server architecture function.

### Client Server Architecture

Basic concept of this architecture is, client(user) will make a http request to the server and if everything is correct it will send back that webpage served from the centralized database as a response.

![Client server architechture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvoz54fscpr7ltdqzkft.jpg)

The fact that all data is stored on a centralized server controlled by the bank and companies is a huge issue. Furthermore, because the data is housed on a centralized server, the likelihood of the server being hacked is very high, implying that your data and privacy is at risk.

## Blockchain

Blockchain solves major two flaws that are present in traditional client server architecture i.e. data privacy and security of data. Blockchain is a **distributed immutable ledger** which is completely **transparent.** 

Let's break down the definition in simple terms. **Distributed** means that the data is stored in a network of computers, eliminating the need for **centralized servers**. You can think of **ledgers** as an account book, keeping track of all the transactions taking place on the blockchain, and it is **immutable**, meaning that once it is added to the blockchain, it can never be changed.

Blockchain transactions are **pseudonymous**, so you can see other people's transactions but not alter the data contained within them. Some cryptocurrencies do allow you to encrypt a data payload or require it by defualt.

![p2p network](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25iti1433mbvn6j1192m.png)

## Bitcoin Mining

Now that we've covered the fundamentals of blockchain, let's look at how transactions work in the bitcoin protocol. As an example, suppose Bob wishes to send 1BTC to Alice. 

Bob conducts a transaction in order to send money to Alice. The transaction initiated by Bob is initially saved in Mempool (It is an area where all unconfirmed transactions are stored). To add a transaction from mempool to the bitcoin blockchain, miners must first solve a mathematical problem that takes an average of 9 minutes to solve. The miner who solves the mathematical problem first will tell all other miner in the network that it has successfully solved the problem. Now, other miners in the network will first check if the mined block is genuinely from Bob or a hacker attempting to penetrate the chain. After double-checking everything, the transaction is successfully posted to the blockchain, and Alice receives 1BTC from Bob.

It should be noted that the miner who solves the mathematical problem first will receive a reward or transaction fee in the form of bitocin.

![blockchain mining](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rd10zaomwg5zpjevdazd.png)
 
## Mathematical Problem

Now that we have a fundamental grasp of mining, let us look at the mathematical problem that miners are solving in order to add it to the bitcoin network.

When Bob conducted a transaction with Alice, the transaction looked like the one shown below. Each transaction is identified by a Block number, a nonce, data, a previous hash, and a hash.

**The block number,** as the name implies, is the current block number that is to be added to the blockchain. **Data** contains the transaction details of Bob wallet to Alice wallet. In the blockchain, **hashes** are like fingerprints, and they are used to remember previous blocks. The **previous hash** contains the hash value of the previous block that is already mined in the blockchain.

![blockchain blocks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00rfubxyn4dlupkesbnl.png)
 
Nonce is a variable value or number that the miner can alter to get the proper hash, all other data in the block, such as the block number, data, and previous hash, are immutable by nature and cannot be changed by anyone.

### SHA 256 Algorithm

When a file is processed by this algorithm, SHA 256 creates an encrypted data file with 64 hexadecimal characters and 4 bits per character.

![SHA 256 algorithm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gagdp5kufjepsye2cwzr.png)
 
Hash is generated when we pass block no, data and prev hash & nonce in **SHA 256 algorithm**. Miners are given a **target value by the bitcoin protocol,**

The hash generated by miners should be less than the bitcoin protocol's target value in order to be successfully added to the blockchain. Miners change the nonce number to generate hash numbers, and the miner who generates the hash that is less than the target value **first** receives a reward for successfully solving the mathematical problem.

To explore the demo yourself visit to this website: [https://demoblockchain.org/block](https://demoblockchain.org/block)

![blockchain demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zeje30yqricfndpx4cgg.png)

Above is a demonstration of the transaction that we were attempting; you can try it yourself on the website. Be sure to observe how the hash changes when you change the block no, nonce, or data in the block. A change like this prevents hackers from hacking blockchains, which we will discuss in the following sections.

## Chain Of Blocks

Now that you have a basic understanding of how bitcoin is mined using the SHA 256 algorithm, let's look at how a successfully mined block is added in the blockchain.

The first block on the blockchain is also known as the Genesis block, and it’s **Previous Hash**  value is 0. Our Bob and Alice transaction is the third block that has been successfully added, with the previous hash value equal to the value of the second block's hash.

![chain of blocks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsqk22osog9jad5ezzn7.png)
 
## Distributed P2P Networks

Let's zoom out a bit to see how this blockchain is kept on decentralized distributed Peer to Peer networks or P2P networks. 

If one peer in a distributed P2P network requires data, it will contact the peer who has that data stored. The benefit of this P2P network is that it cannot be hacked by a hacker since the data is kept in many locations and if one data is tampered with, the other data will stay safe. We will look at the benefits of p2p in the following section, but first let's see how the successfully minned block of Bob and Alice is stored in p2p networks.

Let the figure below serve as an example of a P2P network with miners and users.

![P2P network initial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/quls0gz143084wcejluy.png)
 
Consider 'A' to be the miner who, successfully solves the mathematical problem of the transaction between Bob and Alice and adds it to the local blockchain i.e. at block 3. Once uploaded to the blockchain, it will alert the other user on the network of the newly mined block and instruct them to include it in their own block. Below is an animation that will help you visualize it.

![p2p mining gif 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymgy194c0d48mrf2egpq.gif)
 
The other user will first verify that the block mined by 'A' is correct, and then the block will be successfully added to the blockchain.

![p2p mining gif 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ktk5cud2lzbis6xstrb.gif)
 
When the block is successfully added, our peer-to-peer network will resemble the following image.

![p2p network after adding block](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gh7tkoab9gjl2gn3gxgs.png)

## Resources

- [https://bitcoin.org/bitcoin.pdf](https://bitcoin.org/bitcoin.pdf)
- [https://ethereum.org/en/whitepaper/](https://ethereum.org/en/whitepaper/)
- [YouTube Course In Hindi](https://www.youtube.com/watch?v=6aF6p2VUORE&list=PLgPmWS2dQHW-cdHYZQmpAalgSp-xv_ewY&ab_channel=CodeEater)


![https://media.giphy.com/media/l4pTjOu0NsrLApt0Q/giphy.gif?cid=ecf05e47dtlkk3fe19ovkz96zbsihgjhtu6injewu9oy5v8e&rid=giphy.gif&ct=g](https://media.giphy.com/media/l4pTjOu0NsrLApt0Q/giphy.gif?cid=ecf05e47dtlkk3fe19ovkz96zbsihgjhtu6injewu9oy5v8e&rid=giphy.gif&ct=g)
 

So, that’s all folks. This was a quick introduction of how cryptocurrency works, and I hope you learned something new from this blog today. If you want to learn in more depth about blockchain and cryptocurrency in general I recommend you following sources mentioned below.


<a href="https://www.buymeacoffee.com/narottam"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=narottam&button_colour=FFDD00&font_colour=000000&font_family=Poppins&outline_colour=000000&coffee_colour=ffffff"></a>

For more such insights, checkout my blog website [blog.webdrip.in](http://blog.webdrip.in/)