joplin-notes/joplin notion extension/Web Development/Blockchain/Theory.md
2026-11-04 00:33:23 +05:30

10 KiB

Theory

  • A blockchain is a distributed ledger with growing lists of records (blocks) that are securely linked together via cryptographic hashes.

Web versions

  • Web 1.0 (1994-2004) : Websites r made by programmers, normal users can read the content only and nothing cant be changed by user. only programmer can write. Also knows as static sites
  • Web 2.0 (2004-2022) : Websites which allows normal users to both read and write the content and companies provide hosting platform. Also known as dynamic sites. Eg: social media like youtube, facebook. They have centralized servers. Client server architecture. Users dont have control over flow of thir data or ownership
  • Web 3.0 (2022 - ) : Unlike web 2.0, we have decentralized servers. Peer to peer network. Like torrent services. Applications run on decentralized nodes so those apps are called dapps. Blockchain is the main building block of web 3.0.

Summary Web 1.0 - Read Web 2.0 - Read and Write Web 3.0 - Own your data

Prerequisites

Someone who made transaction that transaction is stored in ledger and it is immutable

  • Cryptography
  • Distributed computing : Distributed computing is the method of making multiple computers work together to solve a common problem. It makes a computer network appear as a powerful single computer that provides large-scale resources to deal with complex challenges.
  • Game theory : game theory, branch of applied mathematics that provides tools for analyzing situations in which parties, called players, make decisions that are interdependent. This interdependence causes each player to consider the other player's possible decisions, or strategies, in formulating strategy.

Blockchain

If A sends 10$ to B then if ledger is with A or B only then they both may change it for their advantage but if its with all other peers sychronized then they cant change it as others will say hey u changed it b98495d939e02a55712241a4ee1d9c3c.png   Each block are having data and hash of next node similar to linked list 6edb124a9296a65e58971e1de0077629.png Hash algorithms like sha-3 is used to calculate hash of the data so if data changes hash also changes thus breaking the link and helps to identify malicious node   Miners Miners get the reward for adding blocks in block chain to provide incentivization. But to control the demand as everyone wants to do that for a simple task so in order to choose miners they r given a challenge and that challenge requires computational power and money. Other people who r using block chain r called nodes Summary So basically blockchain is a chain of blocks and each blocks are having multiple transaction and the one who is responsible to add blocks in block chain are called miners(in case of bitcoin). All the people in the blockchain network are called nodes

Cryptography

Blockchain is very similar to P2P network with the exception of crytpography Reasons for need of crytograpy in block chain

  1. Confidentiality : A sends a sensitive information to B which should not be read by anyone else
  2. Integrity : Data sent should not be changed in between by anyone or get corrupted
  3. Non repudiation : Taking acknowlegement of transaction/message. Eg: If A sends message to B then A cant be able to say that A has not sent it
  4. Authentication : Means someone else say C cant send message to B inpersonating as A.

Digital signature

It is possible to have both digital signature and confidentiality by first encrypting the data from sender's private key then encrypt it again with reciever's public key. When data has reached then first it is decrypted by reciever private key then decrypted by sender public key to verify authenticity. 3792049ef2150220b22a1bb3c6cfe692.png

Nodes in blockchain

  • Full Node : Verify blocks which get added. Full node store whole block chain which is like 5450 of GBs in 2024.
  • Partial Node : lightweight node having partial information. when we use blockchain for wallet so our device becomes node itself
  • Full node + miner : in this case miner needs huge computational poower lie stack of multiple CPU and GPUs  

Hashing in blockchain

  • Input of any length/bit when converted to has using hash function, the hash generated is of fixed bit/size. So even if hige chunk data ie 5gb is passed then only fixed size hash which is merely in bytes will be generated. cf9afb1ce388cc53cb4d2a535032e07c.png
  • Unlike encryption hash cant be converted back to input
  • Some exampples are MD(MD5 was discontinued due to security issue) and SHA where SHA 256 bit is famous. MD stands for Message Digest and SHA stands for Secure Hash Algorithm. SHA is made by NSA
  • For 2 different inputs we never get same hash and if in rare case if it happens then it is called collision
  • If we r downloading ubuntu iso from untrusted site then we can generate hash of iso file and compare it with hash posted in original site and if that mismatch then it means it is modified.
  • For packages having GPG keys, where digital signature is used, hash is signed by encrypting the hash with the key the actual package is not encrypted only its hash is encrypted so to validate it you do 3 steps
    • you hash it yourself
    • you decrypt the hash it came with using the key the maintainer gave you
    • you check if the 2 hashes are the same

Merkle Tree

We have nodes and we have transaction in nodes too. As there are multiple transaction we cant store the hash of all transactions so using merkle tree we try to generate single hash for all the transaction. Let's say we have 10 transactions so lets try to merge them in pairs. H1 and H2, H3 and H4 ... and so we will combine hashes. 7616ab6198c376cc0e4a25b02405dd59.png If there r even number of nodes then we will just repeat the last one. (9,10) was the odd one out so a copy is created and it become (9,10,9,10).  

Blockchain Architecture

  • Blocks will contain data but each block will have block header which is like meta-data. Blockheader will contain

    • Timestamp
    • Version
    • Merkle root
    • Difficulty Target
    • Nonce
    • Previous Hash

    Difficulty and Nonce comes under proof of work(POW) section.

  • Data is immutable bcz of POW as hash will change 8e27f90cf1d8e785503f9f1b3b976308.png POW

    • block has to be validated before making change
    • takes atleast 10 min to add block. So if there r 5 blocks which needs to be changed then it will take atleast 50 min
    • blockchain stored in multiple machines. Consensus algorithm will be used as it is verified by everyone on blockchain
    • need supercomputer to change block
    • need 50% majority in blockchain network which is almost impossible
    • First block is known as genesis block and is created by the one who started the chain. Its previous hash value is 0.

Types of Blockchain

  • Public Blockchain
    1. Open : Means data about is available to public(not secrets but meta-data and other whose visibility is public to everyone)
    2. Anyone can join that blockchain
  • Private Blockchain
    1. For single company
  • Federated Blockchain
    1. Group of people/ companies but not to everyone in world

Advantage of private over public is that there is no need to implement POW to verify nodes as all nodes are known and no need to wait for 10 min.

Consensus Blockchain

  • In order to add a block consensus algorithm is used to determine who will add the block. Some examples are
    1. POW(Proof of Work) : To solve computational program requiring huge computational power. This algo is used by bitcoin and whoever wins the puzzle will given the opportuity as they have spent computational power.
    2. POS(Proof of Stake) : Unlike proof of work which is resposible for so much energy consumption. Before applying for that, they put some stake like spending some coins, Used by Etherium
    3. POET(Prooof of Elapsed Time) : By intel
    4. Proof of Depsit : Like POS but depositing amount before entering block chain
    5. Proof of Capacity(POC) : one with huge capacity will win

Proof of Work

Why is it useless to alter blocks in bitcoin?

In a blockchain, each block contains a cryptographic hash of the previous block, creating a chain of blocks linked together. This ensures the integrity and immutability of the blockchain because altering any block would require changing all subsequent blocks, which becomes increasingly difficult as more blocks are added due to the computational effort required (proof of work).

Let's consider your scenario where you want to change the 25th block in a chain of 30 blocks. To successfully alter the 25th block, you would need to recalculate its hash and the hash of all subsequent blocks in the chain. This is because each block's hash includes the hash of the previous block.

Additionally, in a proof-of-work system like Bitcoin's, miners compete to solve complex mathematical puzzles to add new blocks to the blockchain. Changing historical blocks would require redoing all the proof-of-work for those blocks and then outpacing the current network's proof-of-work to have the altered version of the blockchain accepted by the majority of nodes.

The reason it would be considered useless is that the computational power required to achieve this, especially for a significant number of blocks deep in the chain, is prohibitively high. The Bitcoin blockchain, for example, has an immense amount of computational power securing it, making it practically infeasible to alter past transactions without expending an exorbitant amount of resources.

Even if someone were to succeed in altering the 25th block, the rest of the network would reject it because the altered chain would not have the longest proof-of-work chain, which is the criterion for consensus in Bitcoin and many other blockchain networks. This is why attempting to alter past blocks in a blockchain is often referred to as "useless" because of the immense computational effort required and the near-impossibility of success.