Blockchain Basics
Blockchain Basics
Internet - communication
Blockchain - Trust
Idea - Stuart Haber and W Scott Stornetta --> How to digitally time stamp a transaction? - paper name
What is BlockChain?
Blockchain is a Distributed immutable ledger which is completely transparent . It is a Decentralized
System.
Ledger Book becomes a --> Block --> data once entered in block can’t be changed
So Immutable
Application of BlockChain
1. Product Tracking → every product has a QR code → we will get the name and complete
information.
2. Smart contract → Program which runs on Ethereum Blockchain. For eg consider a Delivery
man who has to maintain a temperature less than 15 degree then the shop keeper will take it and pay
him back else he won’t pay him No need to take legal actions like if he provided the rotten fruits.
3. International Wire Transfer → Sender bank to Correspondent Bank then it is received by the
Receiver . So a lot of steps are involved so time taking → Huge Fees as a lot of steps are involved
(commission).
4. Health Care System → Eg. person A reached out to City A. But in another case because of a
medical emergency , he reached out to city B . But B has no medical history so it takes time to analyze
and then provide treatment. If the Database is decentralized in the form of a Single Block chain , then a
lot of time will be saved.
Hashing Algorithm
Immutable Ledger
You liked a House . Wanted to buy it . Sign a Contract that the House is on our name with the Seller.
This data will be saved in a centralized Database or maintain a Register. This biggest problem is that it is
mutable and it can be corrupted or destroyed easily.
The idea of decentralization and distribution networks can make our lives easier.
If a Hacker hacks a particular block then the following will be corrupted because of avalanche effect
and this process goes on until the whole block chain gets corrupted . The other systems owning the
same blockchain it rectify and restore this blockchain
Distributed P2P network - peers are connected to each other . If Data in system A is corrupted , then
system B will provide the data needed .
Blockchain mining
Transaction --> Solve Math problem (as known as proof of work)--> if solved, a block is created (it
contains the transaction) --> miner verifies the Validity of the block --> block is added --> miner is
rewarded.
So Miguel Castro came up with a solution to this problem. Byzantine fault tolerance
The recovery mechanism allows the algorithm to tolerate any number of faults over the lifetime of the
system provided fewer than 1/3 of the replicas become faulty within a small window of vulnerability.
Consensus Protocol
1. Prevent Attacks
2. Competing chain problem
Prevent Attacks
1. Proof of Work
2. Proof of state , etc
Proof of work → when a hacker tries to add a block at the end of the blockchain we wouldn’t be able to
know that. Because the concept of immutable ledger can’t be applied.
Let's consider a miner mines a block but solving a problem , it’s very difficult to solve and the resources
of great amounts are used. After the proof that they have created a block So it is called as proof of
work. Then the block is added to the blockchain and similarly they will be rewarded. No one will waste
their time and money by doing malicious activities after doing this much hard work.
When system A adds a block then other nodes try to run an algorithm and check the validity of the
algorithm. Then they add that block to their blockchain.
Solution : The blockchain with the longest number of blocks is accepted and the nodes that have the
largest computing power will be chosen as they add the block faster and become longest.
What is Bitcoin?
First crypto currency .
Founder of bitcoin - 2008 - Satoshi Nakamoto
Bitcoin vs Blockchain
1. Technology - Blockchain
2. Protocol/coin - Bitcoin , ethereum, waves (protocol is nothing but rules to be followed)
3. Token - used mainly for investment. Waves , ethereal have tokens as the follow the smart
contract but bitcoin doesn’t.they are mainly made from the protocols.
1. The halving
2. Block Frequency
The halving:
The number of bitcoins will be reduced to half for every 4 year that it is for every 210,000 blocks.
Note - Supply cap of bitcoin is 21 million. This is making it rare like gold.
Block Frequency:
This states that on an average it will take 10 minutes to create a new block.Different cryptocurrencies
will have different average time.
Both the processes are halving and block frequency is controlled by the blockchain algorithm which
makes it trustworthy and more beautiful.
Point to note : hash value should be less than or equal to the target value. Every miner will be provided
with a target value which is like a math problem that he has to solve with the help of nonce value.
The other parts of the block like block number, data (transactions)and prev hash can’t be edited . So
miners have to change and try different nonce values so that the hash value is in range (less than or
equal to target value). If the value comes in range then he will be rewarded for mining a block.
The whole math problem is about finding the hash value within the target value.
The target
Hexadecimal number - output of the SHA256 is in this form. (64 characters * 4bits = 256 bits)
In all the possible hashes a particular target is set , above which hashes are considered invalid.
** 2014 stats
Mining pools
When the hashing power of an individual miner is less, they form a network and try to hash together so
that --> they increase the chance of solving.
This is achieved by forming mining pools. In mining pool
1. Reward will be distributed in proportion to that hashing power.
2. The hashing range will be divided between the miner in the mining pool(network) so that
maximum resources are being utilized and no computational power is wasted.
3. This concept was developed because the large mining companies which have the highest
computational power succeed very easily and get rewarded . This becomes disadvantageous to the
small miner with minimal resources .So they joined together forming a mining pool.
Nonce Range
Nonce is a 32 bit number
Range of nonce 0 to 2^32-1 = 4 * 10^9 (4 billion)
Total number of possible hashes = 16*16*16* (64 times) = 10^77
A modest mines does 10^8 hashes/ second (it means nonces/ second)
4* 10^9 nonce will be covered in (4*10^9)/(10^8) = 40 seconds.
So what do the miners do when all the nonces get exhausted and miners have not hit the target?
Time Stamp
In a block here is a section that contains Timestamp (UNIX time).
As we know that due to the avalanche effect, there will be a greater change in the hash value.
A miner exhausts 4 billion nonce in 40seconds. --> A miner will exhaust 0.1 Billion nonce in 1 seconds.
In 1 sec , 0.1 billion nonce will be exhausted and the time stamp changes (1 sec) and the nonce can be
reused. This overcomes the problem faced above.
Current easing rate is equal to 180 million trillion hashes / sec (of a complete network nowadays) 10^6 -
million 10^12 - trillion
4 * 10^9 will be covered in = (10^6 * 10^12) = 4*10^-9 seconds
4* 10^-9 sec is very very very small than 1 second.
So now the timestamp is not that useful. As nonces are getting exhausted before 1 sec .
What should the miners do in their idle time ? Should they wait for the timestamp to change?
Mempool
Every transaction has a id and transaction fees
Mempool- contains thousands of transactions. Miners choose unconfirmed transactions from it which
have the higher transaction fees.
If the nonces are drained and time is not one second then the transaction with the lowest transaction
with the highest transaction present in the mempool . This change because of the avalanche effect
creates nonces to start from first. This process repeats until the timestamp increases by one second.
The nonces will be hashed as usual.
Fig a Fig b
When a transaction is carried out by a node it will be added to mempool this information is passed to all
the mempools and miners present in the network. By continuing this process, mempool contains
thousands of transactions. Miner has access to mempools . Miner choose a transaction and add it in the
blockchain. After it is verified by others, everyone adds the block in the blockchain and that added
transactions are removed from the mempools.
It can be clearly seen that how a transaction takes place in a transaction takes place and the
immutability is maintained . It is an example showing a transaction which we have made for buying the
coffee.
Transaction Fee
For authorizing a transaction , miners have to be provided with some commission , that is called
transaction fees.
Transaction fees are usually decided by the node .
Higher the fee, the more the miner will be interested , lower the fee no tier will pick that transaction and
finally after two or more days it will be removed from the lists.
Here 0.1 etc will be added in UTXOs of the miner.
Cryptocurrency Wallets
Wallets try to find the unused transaction and try to sum them up. With the help of UTXOs.
Bitcoin address
If in future hackers are able to find the private key from the public then hackers may steal or corrupt the
data.
Bitcoin addresses are used to get the transaction from others. This is derived by the 256SHA algorithm.
Public is used when we are providing the bitcoins and bitcoin address is used when others are
providing the bitcoins to us. It provides another layer of security .
Segregated Witness
Increase in block size can increase the number of transactions which in turn lead to higher commission
of the miners. But the main disadvantage is that as the size increases the traveling of the block across
the network takes much.
Every transaction has the following fields
Here signature and public key are together called as Script sig
Master public is used to track down the transaction and they can’t make payment. Only using private
keys , we can perform a transaction.
Ethereum basics
Types of nodes
1. Full node
2. Light node
3. Achieved node
Full node
Light node
Archive nide
Ethereum Accounts
This unity has an ether(ETH) balance that can send or receive transactions on ethereum .
When we create a wallet , an externally owned account is created which can be assessed only through
the private key . Private key is used to open the wallet.This wallet is used for
● sending ether
● receiving ether , and
● interaction with the smart contracts.
1. Contract account
It is controlled by a contract pool. Contract account has a unique address which is called contract code.
Through this account the interaction between the ethereum and smart contract. We have to deploy a
smart contract in the ethereal blockchain . This deployment leads to the generation of unique
addresses.
Smart Contract
A program which runs on the ethereum blockchain.
For bitcoin generation we use bitcoin script
For ethereal we use the solidity language
Bitcoin script - not Turing complete (it doesn’t have the concept of loops)
Solidity - Turing complete (any logics can be implemented)
If we write an infinite loop then what will happen ? It will lead to an ethereum crash. So to rectify
this , for running a program in ethereum we need to buy something called gas (we need to pay for it). If
the gas ends it will lead to stop of the running of the program( which includes an infinite loop as well).
Decentralized Apps(Dapps)
Ethereum Virtual Machine (EVM)
Apps will be running on all the nodes. to keep the nodes safe we need to use the virtual machine on
our computer.
Ethereum Gas
For running a code, energy(gas) is required. Every operation has its gas.for example add has 3 gas,
multiply needs 5 gas.
Some important point to note
● any transaction that modifies the blockchain costs gas
● the user that generated the transaction pays for the gas.
Solution
Case 1:
A sets the gas price per unit = 100gwei
Transaction gas limit = 21000 units
Total fees = gas units(limit) * gas price per unit
Hard Fork
The ethereum split into 2. One was ethereum classics and the second one was ethereum . There was
an error in the smart contract. Some wanted to rectify it but others didn’t want to change and follow the
principles of blockchain .So there came a split in the ethereum.
● During a hard fork, software implementing a protocol and its mining procedures is upgraded.
● Once a user upgrades the software , that version rejects all the transactions from the older
software effectively creating a new branch of the blockchain.
● However , those users who retain the old software continue to process transactions.
Soft Fork
● Soft fork is the change to the protocol, but the end product remains unchanged.
● A soft fork is a backward compatible upgrade, meaning that the upgraded nodes can still
communicate with the non-upgraded ones.
● Old nodes (not upgraded nodes ) could still validate blocks and transactions (the formatting
didn’t break the rules) but they just wouldn’t understand them.
Because of the longest chain rule, the 0.8mb block is removed as the majority has the maximum
hashing power and hence generates the block more rapidly than the non-upgraded blocks. Here we
can see that the blockchain didn’t split and still communicate with each other.
● Scalability
● Security
● Sustainability
Proof of stake
No miner, only validator.
Validator has to given some ethers to the system (there is a minimum requirement for eg 32 eth)
Attaching of block => attestation
After his job is completed , he will provide back the ether and transaction fees. If the validator is trying
to do sth wrong then it won’t be provided with ether and fees as well. This concept is called as proof of
stack.
There is no competition in mining between the validators. They are selected randomly. But the more
ether you pay , the more chance of getting randomly selected.
Sharding
What are the issues in proof of work?
Information lag may take place
Miners get paid for mining and not validation and verifying has to be done ( which is a time taking
process) so they are paid less and it is indirectly leading to time waste.
Maintenance becomes very difficult.
Sharding is similar to splitting a database because miners can deal with the particular amount of data.
Major benefits
● transaction per second increase
● powerful and expensive computer will not be needed
● More validator will job
● Envy consumption can be reduced
Beacon chain + ethereum main net + shard chain will be integrated and this will be a revolutionary
technology.
Alt coins
Coins which are not bitcoin are called alt coins. Example tether, chainlink, litecoin, finance coin.
● Consensus protocol
● New capabilities