Everything You Should Know About Blockchain.
Tags: 2articles1week, blockchain, encryption, cryptocurrency
Published: 2022-02-24 16:51:35

<p>A lot has changed since blockchain first arrived in the 1990s and since then it's changing our world in a way no one has predicted. If you have been active on the internet you must have heard people throwing words like NFT, Ethereum or you must have heard about some famous crypto coin like Dogecoin or Bitcoin.</p>
<p>Here is a tweet of Elon musk about Dogecoin.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://twitter.com/elonmusk/status/1284291528328790016">https://twitter.com/elonmusk/status/1284291528328790016</a></div>
<p>Here is a tweet about Jack Dorsey which was sold for  NFT for over $2.9 million</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://twitter.com/jack/status/20?lang=en">https://twitter.com/jack/status/20?lang=en</a></div>
<p>If you wanna know more about NFT read an <a target="_blank" href="https://likam5.hashnode.dev/non-fungible-tokens-nfts">article</a> by <a class="user-mention" href="https://hashnode.com/@Like-iam5">Like I'm 5</a> on NFT.</p>
<p>All the famous cryptocurrencies like BTC, Ethereum, or any other coin like Ethereum classic or Bitcoin cash run on Blockchain and something to know NFT which works on Ethereum which runs on Blockchain.</p>
<p>So the question arises what is this BLOCKCHAIN everything is running on.</p>
<p>let's see</p>
<h1 id="genesis-of-the-blockchain">Genesis of The Blockchain</h1>
<p>Well, the blockchain is originated way before any cryptocurrencies emerged or hypothetically saying, even theorized. A cryptographer named David Chaum first tossed the idea of the blockchain-like protocol in his 1982 dissertation <strong>" Computer Systems Established, Maintained, and Trusted by Mutually Suspicious Groups ".</strong></p>
<p>Further work on a cryptographically secured chain of blocks was described in 1991 by Stuart Haber and W. Scott Stornetta. They wanted to implement a system wherein document timestamps could not be tampered with.</p>
<p>It wasn't till 2008 when the first blockchain was conceptualized by a group/people know as <strong>Satoshi Nakamoto</strong>. Nakamoto improved the design in an important way using a Hashcash-like method to timestamp blocks without requiring them to be signed by a trusted party and introducing a difficulty parameter to stabilize the rate with which blocks are added to the chain. The design was implemented the following year by Nakamoto as a core component of the cryptocurrency bitcoin, where it serves as the public ledger for all transactions on the network.</p>
<h1 id="how-blockchain-works">How Blockchain works</h1>
<p>As the name suggests blockchains are nothing but a big chain of interconnected blocks. Where every block contains their previous block's hash value and that is how they are linked as well as their own unique hash.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631349870665/SeVPXHqCG.png" alt="Blockchain blocks" /></p>
<p>Now that we have a gist of what blockchain is let's dive deeper into the technicality of Blockchains.</p>
<h2 id="why-blockchain">Why Blockchain?</h2>
<p>Blockchains are decentralized and transparent which means anyone can add their own block on the blockchain given that they have to do certain work to add it. This process is called mining and it is well rewarded. We will talk more about mining later in this article.</p>
<p> It is well secured by powerful cryptography which makes it impossible to tamper with. Also, blockchain is publicly broadcasted and are decentralized which makes it immutable and impossible to temper.</p>
<h3 id="but-what-is-decentralization">But what is Decentralization?</h3>
<p>To understand this concept of decentralization let's first understand how banks and transactions work right now.</p>
<p>Suppose you want to share money with your friend Bob who is miles away from you. So what will you do is open your Application on your mobile and transfer the money to Bob's account. </p>
<p>So what is happening behind the scene is you are not sending money to Bob instead you are sending a message to the bank. <code>A message you kidding me?</code> Well, if you think a little more money is nothing but some number in this digital world. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631352245692/CrHcfCNk_.png" alt="image.png" /></p>
<p>So our message might look something like this.</p>
<pre><code> { 
    <span class="hljs-attr">"To"</span> : <span class="hljs-string">"BOB"</span>,
    <span class="hljs-attr">"From"</span> : <span class="hljs-string">"Alice"</span>,
    <span class="hljs-attr">"Ammount"</span> : <span class="hljs-string">"500 $"</span>
}
</code></pre><p>So in this message, we are saying send 500$ to Bob. After the bank receives this message from your device it will check so certain tasks to complete the transaction.</p>
<ol>
<li><p>It will open the ledger ( A ledger balance is computed by a bank at the end of each business day and includes all withdrawals and deposits to calculate the total amount of money in a bank account.) and check if Alice has enough money to transfer i.e if Alice has 500$ or more.</p>
</li>
<li><p>If the above condition is met then the ledger is updated where the new balance of Alice will be the old balance - 500 $ and a new balance of the Bob will be the old balance + 500$.</p>
</li>
<li><p>After this both receive a message that their transaction is successful. </p>
</li>
</ol>
<p>So this was how the transaction works with banks. But the problem with it is you can never verify the transaction yourself or you can never see what is happening inside the book of the bank you trust they can manage your money and all the power is centralized around the bank. They have the power to create new money (as I already said it's just a number) or transfer if they want. </p>
<p>This centralization of power is something from which the community is not happy with and blockchain solves the problem using a decentralized method of adding a transaction.</p>
<p>Now let's understand how Blockchain handles this kind of transaction between Alice and Bob.</p>
<p>Now Alice wants to send some money to Bob through Blockchain. This transaction will happen without the interference of any central power.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631372540075/miwoMBpBL.png" alt="image.png" /></p>
<p>So what's really happening is shown in the below image.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631372425979/NElsU3SXi.png" alt="Transaction In blockchain" /></p>
<p>When a transaction is made it creates a block that block is then sent to every node where miners compete with one another to add that block to the main block. On successfully doing that each miner is paid in cryptocurrencies, but that is the miner's logic which can be discussed in the next article. </p>
<p>For now, we understood how decentralization happens in the blockchain.</p>
<p> So now you might though if no one is governing the transactions then how is it getting validating and adding to blockchain who is doing all that work? Here come's the miners who validate the transaction and add it to the block.</p>
<h1 id="who-are-miners-and-how-mining-logic-works">Who are Miners and how Mining Logic Works</h1>
<p>Well, till now it's clear that the blockchain is made of a number of blocks. So what are these blocks and which kind of data do they contain.</p>
<p>Well if we see an overview block looks something like below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631476105347/a9mYl_Xyp.png" alt="Merkel Tree Example" /></p>
<p>In this, we can see that a single block contain </p>
<ul>
<li>Previous Hash</li>
<li>Nonce </li>
<li>TimeStamp</li>
<li>Merkel Root.</li>
</ul>
<p>But in exact a block look something like this with a lot of data</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631476237071/CgxoVytuQ.png" alt="image.png" /></p>
<p>You can see all the transactions and blocks of the public Blockchain. Here I used <a target="_blank" href="https://etherscan.io">Etherscan</a> to get the block data but there are a lot of services out there.</p>
<p>So what happens is every time transactions are made they came to miners where miners validate the transactions and then try to add that block to the public Blockchain.</p>
<p>Why I used "try to add"? Instead of add, well every miner is trying to add a new block to the Blockchain continuously all the time and it's like a race between every miner.</p>
<p>The way blockchain validates a block is valid or not is called <strong>Consensus Mechanism</strong>. There are different ways the Consensus Mechanism works one which is very popular is <strong>Proof of Work</strong> others are <em>Proof of stake<strong>, </strong>Proof of Capacity<strong>, </strong>Proof of Activity*</em> .</p>
<p>In this article, we will only talk about the Proof of Work.</p>
<p>In Proof of Work, every miner has to compute a hash value for the block in some given condition.</p>
<p>Let say you are a miner and you have to calculate a hash value that should start with 4 0's</p>
<p>for example : 0000x147a9393258780beef7d</p>
<p>to calculate something like this take a lot of trial and error and a lot of computational power. And also there is only one thing you can change in the block which is the value of  Nonce.</p>
<p>So for every time you want a new hash value, you keep incrementing the value of Nonce and by finding a valid hash you can add that hash to the block.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631477900650/hKVQCLFlS.png" alt="image.png" /></p>
<p>Well if any miner can add to the Blockchain isn't that make the Blockchain untrustable and unsafe. </p>
<p>Well here comes the power of Decentralization to save you. Every time a miner adds a block to the Blockchain other miners do a validation check on that block. If the block is valid and matches with their own version of Blockchain if everything is valid they add that Blockchain to their own version of Blockchain and start mining the new block.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631566844776/tTs4pHjlt.png" alt="Blockchain.png" /></p>
<p>Say, if some malicious group tries to add an invalid block to the Blockchain for their own profile. After successfully mining the Blockchain their block will be rejected by other miners which can cost them a lot of money and computation power.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631567043591/1_jWHNeQi.png" alt="Blockchain.png" /></p>
<p>If another miner successfully mines a valid block the whole Blockchain will move to that version of the block and malicious manner have to sync up to mine the next block.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631569630595/hPLQ5gYat.png" alt="image.png" /></p>
<p>Well, for every success full mining each miner is given some reward. For mining on BTC Blockchain, it's BTC coin and on mining on ethereum Blockchain, it's ethereum.</p>
<p>fun fact: It's the only time a new coin is created. (It's just an analogy)</p>
<p>well till now we learned about what is Blockchain and now a new block is created and added. </p>
<p>But, we have no clue about how transactions take place in the blockchain.</p>
<h2 id="how-the-transaction-works-in-blockchain">How the transaction works in Blockchain.</h2>
<p>Let's again take Alice and Bob's example and understand the transaction in-depth and how every transaction is protected.</p>
<p>But before that let us look at the concept of asymmetric encryption.</p>
<h3 id="asymmetric-encryption-and-how-it-works-in-blockchain">Asymmetric encryption and how it works in Blockchain.</h3>
<p>Now, to understand this let's first understand how symmetric encryption works. Suppose Bob wants to send some data to Alice but the data is sensitive and Bob wants to send this in a way that only Alice can read it. Alice will send her on the box so that Bob can put the data in a box and close it. Once the box is closed only Alice can open the Box with her key, let's call this key Alice's Private key.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631566793316/h4NIVWufK.png" alt="image.png" /></p>
<p>Now in cryptography same concept is happen, to send a message to Alice Bob encrypts the data using the Hash function such that only Alice's private key can Hash it back to the original data.</p>
<p>The problem with the above method was for every time Alice wants to receive a message Alice have to create numerous keys and boxes to receive messages from numerous people. This can be really problematic and hence the concept of the Asymmetric key was introduced.</p>
<p>Now in the Asymmetric Encryption key, Alice has two keys one is called Alice's private key and the other is called Alice's Public key. Now the public key is shareable and the private key must not be disclosed.</p>
<p>Now, every time someone wants to send a message to Alice they can use Alice's public key and send it to Alice now that message can only be decrypted by Alice's private key.</p>
<p>The math behind is quite long so we will discuss it in later articles, but for now, we understood the private and public key concept. Let's see how it is implied in Blockchain</p>
<p>In Blockchain when Alice wants to send money to BOB that data will be sent to the miner where they verify some conditions like the following.</p>
<ul>
<li>Does Alice have enough money?</li>
<li>Does Alice is the one who sent the money to Bob or is it someone else.</li>
</ul>
<p>For checking the available balance in the wallet BTC uses something called UTXO. An overview on UTXO will be that it is a set of all the previous transaction is done by Alice and after adding and subtracting everything if the balance matches then miner approve the transaction.</p>
<p>And to verify if the transaction Is came from Alice something called a Digital signature is used where Alice signs with her private key and sends it to the miner with other data. Well, you have to understand that those logics are automatically implemented in the wallet and Alice doesn't actually sign anything. Moving on what kind of data is sent to the miner?</p>
<p>Well, the data might look something like this with other data of course</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631569095992/rFrz7FzKr.png" alt="Blockchain Transaction" /></p>
<p>Once it is received the miner will validate the transaction and add it to the Blockchain. Once added to Blockchain the data becomes immutable.</p>
<h2 id="summary">Summary</h2>
<p>Today we learned about how Blockchain works and how data/transactions are added to the Blockchain. Once the transaction came to the miner, the miner validates the data and calculates a valid nonce value to add his version of the block to the Blockchain. Once added the miner get a reward but if the miner tries to add malicious data then it gets declined by rest of the miner and once more than 50% miners agree to some nonce then that will become the main branch and malicious data will not be added to the main branch of the blockchain.</p>
<p>So, this was an overview of Blockchain technology I hope you liked the article. I will be posting more in-depth knowledge about this topic from time to time. To get notified please subscribe to my newsletter.</p>
<p>References:-</p>
<p>https://en.wikipedia.org/wiki/Blockchain#History </p>
<p>https://blog.ethereum.org/2015/11/15/merkling-in-ethereum/</p>
<p>https://etherscan.io</p>
</br><p style="color:#808080;">This blog is posted using <a href="www.blogtiple.com">Blogtiple</a></p>