HomeBack-End & DatabaseIntroduction to blockchain

Introduction to blockchain

This blog post will act as an prelude to our smart contracts & dapps development series. In the blog we will try to first understand what is a blockchain, how it works and specifically focus on bitcoin/ethereum.

Its required to have good understanding on core concepts to do development on the platform.

Blockchain

Blockchain is basically a decentralize platform, in which miners solve a cryptographic hash and generate blocks. Each block has a number of transactions depending on the blocksize. Once a miner solves a block it gets added to a blockchain. Every miner in the blockchain, has to maintain entire copy of the blockchain from its genisis block till the latest block. Miners compete with each other to solve the blocks as they get rewards (gas/coins) on solving a block.

Since there are many miners in a blockchain, the entire block chain is decentralized and this adds trust to the system. There is no central authority which can make changes to a blockchain, as every miner has the entire block history. If any single miner tries to make changes to the blockchain, that blocks will get invalidated as majority of the miners will have a different blockchain.

We have covered many terminologies in the above explanation lets look at each one in detail.

Miner

Miners are basically computers/server which act as nodes in a blockchain. These servers are the building blocks of the blockchain as they maintain the entire blockchain.

Transactions

Transactions in blockchain can be anything, it can be money transfer/set of instructions to execute or a piece of code to execute.

It’s a very common misunderstanding that, blockchain only means financial transaction e.g bitcoin but this not true.

Transaction can be anything as written above, its depends on the blockchain on what exactly is a transaction. We will discuss this in detail later.

Blocks

Blocks basically are a group of transactions. Every blockchain has a fixed block size e.g 1MB, 2MB etc and a group of transactions form a block.

Blocks are a very important piece of the blockchain, as from name itself blocks are the building material of a blockchain.

Miners main purpose is for solve blocks, which means solving a crypto hash on a block to validate it. We will discuss this in further detail later since this is a very critical part.

Miner Rewards

Each miner solves a block as discussed above, which uses a lot of computation resources in terms electricity, server costs. So for a miner to part of blockchain they need to be compensated or incentivized. So whenever a miner solves a block he gets a rewards in terms of coins (in case of bitcoin) or gas (in case on ethereum). We will again discuss this in detail later.

 

Bitcoin

We all know what is bitcoin, it’s a crypto currency.  As per wiki

Bitcoin is a cryptocurrency and worldwide payment system.[10]:3 It is the first decentralized digital currency, as the system works without a central bank or single administrator.[10]:1[11] The network is peer-to-peer and transactions take place between users directly, without an intermediary.[10]:4 These transactions are verified by network nodes through the use of cryptography and recorded in a public distributed ledger called a blockchain. Bitcoin was invented by an unknown person or group of people under the name Satoshi Nakamoto[12] and released as open-source software in 2009.[13]

Let’s get into a more technical view into what is bitcoin and how it fits with blockchain.

Public Key

For any financial transaction, first we need a way to identify people. When we use our bank accounts, we have our account address which acts a unique address to identify a person. Similarly in bitcoin we have a public key/private key.

When go to any exchange like coinbase, koinex, binance and register an account they provide us with a public key.  Public key can be shared with others to receive payments. It similar to a bank account number which you can distribute to other and ask for payments. This is usually a 256bit number, i.e a cryptographic hash.

Private Key

Every user in bitcoin also needs to have a private key. Private key is a secret key, which gives access to your funds i.e bitcoins.  So if we again take analogy of a bank, public key is your account number but private key is the actual username/password or the pin number which authorizes you to withdraw/send money to other users.

Private key makes you owner to your account, without private key you cannot make any debits from your accounts.

When we create accounts in exchanges we don’t get private keys so we are not actually owners of the our funds.

If you need access to your private key, you should create account in bitcoin wallet.

*Read more about public/private keys https://www.linkedin.com/pulse/bitcoin-blockchain-part-ii-keys-addresses-junbang-huang/

Wallet

Wallet can be software or hardware device which creates and stores public/private keys for you on the bitcoin blockchain.

https://bitcoin.org/en/choose-your-wallet

Wallet provides you both public/private keys unlike exchanges and hence you are in full control of your bitcoin funds. If your private keys gets lost or compromised you lose all your funds.

Decentralized Ledger

For any financial transaction system we need identity and book/ledger. We have already seen how bitcoin solves identity.

Ledgers is a basically a records of all credit, debits for every users in a system.

Bitcoin has a distributed ledger which is maintained on a blockchain. Bitcoin ledger is a chain of blocks and all blocks have a number of confirmed transactions.  The most significant innovation in bitcoin was being able to have consensus in a distribute ledger.  Let’s understand this in more detail so we can grasp the gravity of this statement.

In a traditional bank, we have a single authority which is the bank. Let’s take Word bank, ICICI bank, or any other bank. They maintain a single central database which hold all the transactions, identities of individuals and have full control of the funds. The problem with a central authority is quite apparent, they have complete control of everything. There control transaction fee, they can block your account, they place limits etc. You are at the mercy of a central authority for your funds.

The solution to this is very simple, i.e don’t have a central authority. As soon as we think of distributed ledge we face problems like Double Spend, consensus. Let’s look at them in more detail.

Double Spend Problem

This is simple, in a distributed ledger system means there are multiple people/systems maintain copy of a ledger. A malicious user can make two transactions in different ledgers i.e suppose malicious user A has account balance of Rs. 100. He sends Rs. 100 to B in one ledger and Rs. 100 to C in another ledger. How can we sync/reconcile all the ledgers when ledgers are distributed. This is a major problem!

Read more details here https://www.investopedia.com/terms/d/doublespending.asp

Consensus

This is also related to above problem, but in simple terms how do we sync all ledgers so there are no transactions lost, there are no duplicates and there are no negative balances.

This the most important that Satoshi Nakamoto the creator of bitcoin solved. How to have a distributed ledger. The answer is blockchain!! lets see how.

Proof of Work

From our understanding of blockchain above, we already know what are miners, blocks, transactions.

So in bitcoin blockchain, when a user makes a transaction lets see A sends Rs. 1oo to B. This transaction is sent to all miners in the bitcoin network and it gets added to the bitcoin memory pool. Every miner has a memory pool which it maintains.

Next, miners pick transitions from this memory pool using their own algorithms (mainly based on age of transactions and transaction fee) and create a block. Maximum size of block in bitcoin in 1MB. So the maximum number of transactions a miner can add to a block is 1MB and they create a block. It’s important to note that every miner might pick random transaction based on there on logic and create a block. So in bitcoin network there is no transaction sequence, so it’s doesn’t matter when you made the transaction it might remain unconfirmed depending on many factors.

Next, after a block has made miners start to calculate there SHA-256 value. This is the work that every miner does, which is called proof of work. SHA-256 is nothing but a cryptographic algorithm which created a unique/random 256 character alphanumeric number.  e.g ‘ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad’

Next, once a sha-256 of a block is found the miner needs to check if leading/first 8 values of the string are zeros. If the first 8 values are zero’s the block is considered to be valid and send to all other nodes in the network for confirmation. As we know sha-256 is a totally random algorithm, the probability of sha-256 to have first 8 values a 0 is very less. So miners have to constantly update the block with a random nonce to the block and again calculate sha-256. They keep on doing this till the first 8 digital are zeros.  This is a very time-consuming process, totally random and not every miner is an able to confirm a block always. Also this value of first 8 zero is dynamic and is adjusted based on network. This value is called network difficulty.

Next, once a miner has a confirmed block he distributes that block to the entire network so that all miner can look at the confirmed transaction and add the block to there blockchain and start the entire process again.

Network Difficulty 

This is a the number of zeros (0) required in 256 hash to make a block valid. This is adjusted dynamically based on number of miners, number transactions. It can vary from 8 to 18.

Memory Pool

Memory pool is a basically a space with all unconfirmed transactions. Miners pick transactions  from this pool to make a block.

Block consensus

We understood how new transactions get added to the blockchain, now let’s see how these transactions get synchronized across all the distributed ledgers.

So once a miner confirmed a block, he sends to all the miners in the blockchain. All miners verify that it’s a valid block and add it their chain, look at the remaining transaction in mempool and start mining the blocks again. But what happens if a two miners solve two different block and transmit it the network or if a malicious miner confirms a block with invalid transactions and send it to the network.

Support we have a starting block called “Genesis Block” G. All  miners will have a block G with them in there blockchain. Now suppose two miners confirmed different blocks BA1 and BA2 and they individually transmit it to the network. So few miners will add BA1 and few will BA2 to the chain. So now we have a two version of the blockchain

G -> BA1

G -> BA2

So bitcoin solves this problem by having miners choose the longest chain.

So now, miners proceed and confirm another block BB and transmit it.

G -> BA1 -> BB

G -> BA2 -> BB

But what is important to realize in a network with large number of miners, there won’t be a 50% distribution of BA1 and BA2 after there are 2-3 block confirmations. So as per the consensus rule the long blockchain is considered valid and the other is discarded. So one of the blocks will get invalid BA1 or BA2

More details : https://witestlab.poly.edu/blog/get-rich-on-fake-bitcoins/

A transaction is considered confirmed after a minimum of 6 block confirmations.

https://bitcoin.stackexchange.com/questions/13370/how-do-i-detect-a-double-spend

 

Genesis Block

This is the first block created in a blockchain https://blockchain.info/block-index/14849

Coinbase

For every block in a blockchain, the first transaction is called coinbase. This is the transaction which shows the reward which miner gets. 

https://en.bitcoin.it/wiki/Coinbase

https://bitcoin.stackexchange.com/questions/4571/what-is-the-coinbase

Block Confirmations

The number of blocks after which a transaction is deemed to be confirmed. Right now its 6 blocks. 

https://blockchain.info/  is a block explorer for bitcoin, where you can see all the above in practice. You can look at blocks, block sizes, etc everything that we discussed above.

 

Ethereum

Ethereum is much more than bitcoin, ethereum is not just a currency but the ethereum blockchain is capable of running smart contracts, we can deploy apps on ethereum platform called (dapps). Ethereum has a EVM and it is a Turing complete system.  Let’s explore in detail the major differences between bitcoin/ether

 

This is a very good post explains the main differences

Read Abhishek Singh‘s answer to How is the Ethereum blockchain different from the Bitcoin blockchain? on Quora

Gas and EVM

Since in ethereum blockchain we can execute our code,  miner need some reward to code executing. Unlike bitcoin where miners just solve the blocks, in ether minor also execute the smart contracts on EVM once a block gets confirmed. The rewards for miners in calculated in GAS. So miners don’t just get ether in fee, but also GAS.

The reason for having a separate fee called GAS is because the market price is ether changes a lot, but the cost for executing code doesn’t. So GAS price adjusts automatically so that miners are always compensated irrespective of the ether market price.

Also, if any malicious person write a code which tries to flood the network like infinite loop it will soon run of GAS. So gas is also used to prevent flooding of network.

Go through this explanation to understand gas and evm

https://ethereum.stackexchange.com/a/62

https://ethereum.stackexchange.com/a/27410

 

To conclude, Ethereum and Bitcoin are just the primary implementations of blockchain. Currently we have over 1000+ crypto currencies which are mainly forks for ETH, BTC and trying to solve different problems.

 

This is a very nice ideographic to understand the differences between coins.

 

http://www.visualcapitalist.com/wp-content/uploads/2017/09/bitcoin-ethereum-other-cryptocurrencies.html

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: