What is Blockchain and Bitcoin?

What is Blockchain and Bitcoin?

Before we go on to understand what is Blockchain, it important that you understand what is Bitcoin:
Bitcoin - Blockchain Tutorial - Edureka
Bitcoins are a crypto-currency and digital payment system invented by an unknown programmer, or a group of programmers, under the name Satoshi Nakamoto. That means they can be used like a usual currency, but don’t physically exist like dollar bills.  They are an online currency which can be used to buy things.  These are similar to “digital cash” that exist as bits on people’s computers. Bitcoins exist only in the cloud, like Paypal, Citrus or Paytm.  Even though they are virtual, rather than physical, they are used like cash when transferred between people through the web. 
The Bitcoin system is peer-to-peer network based and transactions take place between users directly, without an intermediary. These transactions are verified by network nodes and recorded in a public distributed ledger called a Blockchain. Since the system works without a central repository or single administrator, Bitcoin is called the first decentralized digital currency.
Bitcoin production makes them a unique currency. Unlike normal currencies, Bitcoins cannot be created as needed. Only 21 Million Bitcoins can be created, of with 17 million have already been created. Bitcoin get created whenever a block containing valid transactions is added to the Blockchain. This is the only means for creating Bitcoins and through various mathematical and encryption algorithms we ensure no fake Bitcoins are created or circulated. Let us now understand more Blockchain.

What is Blockchain ?

Blockchain - Blockchain Tutorial -EdurekaBlockchain can be called the spine of the entire crypto-currency system. Blockchain technology not only helps with the users perform transactions using crypto-currencies but also ensures the security and anonymity of the users involved. It is a continuously growing list of records called blocks, which are linked and secured using cryptographic techniques. A Blockchain can serve as “an open and  distributed ledger, that can record transactions between two parties in a verifiable and permanent way.” This ledger that is shared among everyone in the network is public for all to view.This brings in transparency and trust into the system.
A block is the ‘current’ part of a Blockchain which records some or all of the recent transactions, and once completed goes into the Vlockchain as permanent database. Each time a block gets completed, a new block is generated.
The Blockchain is typically managed by a peer-to-peer network, collectively adhering to a protocol for validating new blocks. Once recorded, the data in any given block cannot be altered retroactively without the alteration of all subsequent blocks and a collusion of the network majority. Transactions once stored in the Blockchain are permanent. They cannot be hacked or manipulated. We will learn more about this once we get into the concepts of Blockchain.
You may go through this short animated video of What is Blockchain to understant the topics with examples that will help you to understand this concept better.

What Is Blockchain | What Is Bitcoin | Blockchain Tutorial | Edureka


Now I hope you have a better understanding of both Bitcoin and Blockchain. Moving ahead in our Blockchain tutorial blog, let us look at the features of Blockchain technology to help us understand why it has become so popular.

Features of Blockchain

Below are the most important features of Blockchain technology that has made it a revolutionary technology: 
  • SHA256 Hash Function
  • Public Key Cryptography
  • Distributed Ledger & Peer to Peer Network
  • Proof of Work
  • Incentives for Validation
Lets try to understand each one of them one by one. 

SHA256 Hash Function

The core hash alogorithm used in blockchain technology is the SHA256. The purpose of using a hash is because the output is not ‘encryption’ i.e it cannot be decrypted back to the original text. It is a ‘one-way’ cryptographic function, and is a fixed size for any size of source text. To get a better understanding, let us look at an example below:

Hash algorithm - Blockchain Tutorial - Edureka

If you look at the first example, we are feeding the input as “Hello World” and getting an output as “a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e”. However, by just adding an “!” at the end, the output completely changes to “7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069”. If we change “H” to “h” and “W” to “w”, then the output value changes to “7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9”.
I hope with this example you have understood how complex the algorithm is as even the slightest change in the input can cause a massive change in the output. 

Public Key Cryptography

This cryptographic technique helps the user by creating a set of keys referred as Public key and Private key. Here the Public key is shared with others whereas the Private key is kept as a secret by the user. To understand the roles of these keys, Let us look at the example below to get a better understanding:
Digital Signature - Blockchain Tutorial - Edureka
If Chandler sends some bitcoins to Joey, that transaction will have three pieces of information:
  • Joey’s bitcoin address.(Joey’s Public key)
  • The amount of bitcoins that Chandler is sending to Joey.
  • Chandler’s bitcoin address.(Chandler’s Public key)
Now all this data along with an encrypted digital signature is sent through the network for verification. The Digital signature is again a hash value achieved by the combination of the Chandler’s bitcoin address and the amount he is sending to joey. This digital signature is encrypted by the private key. Once this data is received by a miner who has to verify this transaction, there are 2 process he does simultaneously:
  1. He takes all the un-encrypted data like transaction amount and public keys of both Joey and Chandler, and feeds it to a hash algorithm to get a hash value which we shall call Hash1
  2. He takes the digital signature and decrypts it using chandler’s public key to get a hash value which we will call as Hash2
If both Hash1 and Hash2 are the same then it means that this a valid transaction.

Distributed Ledger and P2P Network

Distributed Ledger - Blockchain Tutorial - Edureka
Every single person on the network has a copy of the ledger. There is no single centralized copy. Let me help in you understanding what a ledger is with the following example: Suppose you need to send 10 Bitcoins to your friend John where your Bitcoin balance is 974.65 and John here with a balance of 37. Your balance will be deducted by 10 BTC and credited into John’s account.
Blockchain has a unique way to implement this. There are no accounts and balances in the Bitcoin Blockchain ledger. Every transaction from the first one is stored on a continuous growing database called Blockchain. There are blocks averaging around 2050 transactions and as of today, there are 484,000 blocks in the Blockchain with around 250 million transactions.
This ledger is distributed across all users of Bitcoin Blockchain, i.e., the ledger has no central location where it is stored. Everyone on the network owns a copy of the ledger and the true copy is the collection of all the distributed ledgers.

Proof Of Work

Proof of work - Blockchain Tutorial - EdurekaYou might be wondering if everyone equally owns the ledger, who adds blocks to the Blockchain? How can people trust this person?
For this, we have the concept of proof of work. It is basically like solving a very big puzzle. It requires lots of computational effort. This work is done by people in the Bitcoin network we call miners. The work of these miners is to verify the transactions and solve a complex mathematical puzzle associated with the block being created. The difficulty of the problem is adjusted so that on average a block is solved in 10 minutes. Miners search for a specific nonce(mathematical value) which gives the desired hash which is predetermined. The current difficulty level is such that you need to try about 20.6 quadrillion nonce to get the correct hash.
Each block has a hash value which is the combination of the previous block’s final hash, transaction data’s hash value and the nonce. The final resulting hash for the block must start with a specified number of trailing zeroes. It is this computation to find the nonce which satisfies the condition that makes mining so computationally expensive.
So the person who finds this nonce is the successful miner and he/she can add their block to the blockchain. Through our P2P distributed network, he/she broadcasts their block and everyone verifies if hashes match, updates their blockchain and moves on to solving the next block immediately.

Incentives for Validation

The last step of a Bitcoin transaction is to giving a reward to the miner who has created the latest block. This rewards is provided by the Blockchain system for validating the transactions and maintaining the Blockchain. Currently the reward per block is 12.5 BTC (Rs 3,427,850/- or $ 53,390). This is the most interesting part of Bitcoin Mining.
Bitcoin incentives is the only way to generate new currency into the system and it is believed that by 2140, all 21 million bitcoins will be mined.
With this, I hope you now have more understanding and appreciation towards the Blockchain technology. Blockchain is much more than Bitcoin. Finance is just one of the many industries Blockchain aims to disrupt. Moving ahead with our Blockchain tutorial, let us now look at one such example of IBM and Maersk, to understand how the Supply Chain Industry is disrupted by blockchain.

Blockchain Tutorial: Use Case

Maersk is a Danish business conglomerate with activities in the transport and logistics, and energy sectors. Maersk has been the largest container ship and supply vessel operator in the world since 1996. The company is based in Copenhagen, Denmark with subsidiaries and offices across 130 countries and around 88,000 employees.
IBM is an American multinational technology company mainly working on business solutions, security solutions and storage solutions since 1921
Business need: 
Being a part of an extremely dynamic Supply Chain industry, tracking the slightest change is of highest priority for the client. They needed a solution that could enable them to complete the shipping process without having the delay in paper work. A solution that would be able to bring together all the stakeholders of the system and provide a real-time status on the shipment.
Challenges:
Today, 90% of the goods in global trade are carried by the shipping industry. This supply chain is flowed by the complexity and sheer volume of point-to-point communication. These communications are across a loosely coupled web of land transportation providers .freight forwarders, customs, brokers, government’s ports and ocean carriers processing. Documents and information for a container shipment is estimated to cost more than twice that of the actual physical transportation.
Solution: 
IBM and Maersk are addressing this problem with a distributed permission platform accessible by the supply chain ecosystem designed to exchange event data and handled document workflows.
IBM Maersk use Case - Blockchain Tutorial - Edureka
Merck and IBM are employing Blockchain technology to create a global tamper proof system by the digitizing trade workflow and tracking shipments end-to-end. This eliminates frictions including costly point-to-point communications. The collaboration will launch with potential ability to track millions of container journeys per year and integrate with customs authorities on selected trade lanes.
Results:
  • Provided a secure Data Exchange platform for all the stakeholders involved in the supply chain system.
  • Established a Tamper proof repository to store all the documents involved as part of the process.
  • Regular shipping events help reduce significant Delays and Frauds, saving Billions of dollars annually.
  • Reduced the barrier between trade organisations thereby increasing worldwide GDP by 3%.
  • Helped increase the overall trade volume by 12%.
This is how Blockchain technology helped Maersk and has been helping many other companies worldwide. Finally as part of this Blockchain tutorial, we will look at a demo as to how you set up a private autonomous Blockchain on your system.

Blockchain Tutorial: Demo

We will implement a digital bank using Ethereum Blockchain. Ethereum is an open-source, public, blockchain-based distributed computing platform. The systems will allow us to:
  1. Make a cryptocurrency with a fixed market supply and tokens to represent real world asset values.
  2. Create an autonomous private Blockchain with rules on spending money.
  3. Mine for a new Ether by validating transactions.
The demo can be divided into 4 steps:
  1. Cloning Geth Code
  2. Creating a Genesis Block
  3. Making Rules for our Blockchain
  4. Validating & Mining Ether

Step 1: Cloning Geth Code:

geth is the the command line interface for running a full ethereum node implemented in Go.  By installing and running geth, you can take part in the ethereum frontier live network and
  • Mine real ether
  • Transfer funds between addresses
  • Create contracts and send transactions
  • Explore block history
Cloning the geth repository from github. To do this, open a new terminal and execute the following command:
$ git clone https://github.com/ethereum/go-ethereum 

git command - Blockchain Tutorial -Edureka
After you have successfully cloned the file from github, we need to branch the latest version of geth.
$ cd go-ethereum 
$ git tag
cd command - Blockchain Tutorial -Edureka
git tags- Blockchain Tutorial -Edureka
$ git checkout tags/v1.6.7 -b EdurekaEthereumV1.6.7
$ git branch
branch - Blockchain Tutorial -Edureka
 $ make all

Step 2: Creating Genesis Block

A genesis block is the first block of a block chain. Changing the genesis block is a way to definitively fork yourself away from the bitcoin blockchain, i.e, start a new network with it’s own separate history. To create the genesis file, execute the following commands:
$ cd go-ethereum
$ mkdir genesis
$ cd genesis
$ gedit genesis.json

making genesis file - Blockchain Tutorial -EdurekaStep 3 : Making Rules for our Blockchain

The rules for our Blockchain will be included in the genesis.json file we have created. Add the following code in your genesis.json file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
    { "config":{
        "chainId": 123,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0,
        },
    "nonce": "0x3",
    "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000",
    "extraData": "0x0",
    "gasLimit": "0x4c4b40",
    "difficulty": :0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000",
    "coinbase": "0x000000000000000000000000000000000",
    "alloc":{
        }
}
nonce:  A 64-bit hash, which proves, combined with the mix-hash, that a sufficient amount of computation has been carried out on this block.
timestamp: A scalar value equal to the reasonable output of Unix time() function at this block inception.
mixhash: A 256-bit hash which proves, combined with the nonce, that a sufficient amount of computation has been carried out on this block.
difficulty: A scalar value corresponding to the difficulty level applied during the nonce discovering of  block.
allocAllows defining a list of pre-filled wallets. It’s an Ethereum specific functionality to handle the “Ether pre-sale” period.
parentHash: The Keccak 256-bit hash of the entire parent block header (including its nonce and mixhash).
extraData: An optional free, but max. 32-byte long space to conserve smart things for ethernity.
gasLimit: A scalar value equal to the current chain-wide limit of Gas expenditure per block.
coinbase: The very first transaction included in the block by the miners.
Now we need to initialize the blockchain. You can do that by using the following command:
$ /home/edureka/go-ethereum/build/bin/geth --datadir ~/ethereum/net3 init genesis/genesis3.json
Blockchain Initialization command - Blockchain Tutorial -Edureka
Now that we have initialized the blockchain, it’s time that we give geth control access to it. Execute the following command to start the geth console:
$ /home/edureka/go-ethereum/build/bin/geth --datadir ~/ethereum/net3/ --networkid 3 console

Geth console - Blockchain Tutorial -Edureka

Step 4: Validating & Mining Ether

In the Geth console, execute the following command:
personal.newAccount() : it creates a new account as part of your blockchain which has a specific wallet attached to it
persona.newAccount() - Blockchain Tutorial - Edureka
eth.accounts: it helps you check the various accounts which are part of your blockchain
eth.accounts - Blockchain Tutorial -Edureka
eth.blockNumber(): this helps you to identify the number of blocks that are part of your blockchain
miner.start(): this function is used to start the mining process
miner.start() - Blockchain Tutorial -Edureka
Below you can see the mining application running:
mining running - Blockchain Tutorial -Edureka
miner.stop(): it stops the mining process
miner.stop() - Blockchain Tutorial -Edureka
eth.blockNumber(): ececuting this command after the mining process tells you at which block number you are at after performing the mining operation
eth.getBalance: (“account number”): this command is used to check the ether balance in the specified account

eth.getBalance()- Blockchain Tutorial -Edureka

exit: Exit the geth console.
With this we have successfully mined ether and completed our Banking Demo. This brings us to the end of this blog. I hope you liked this Blockchain tutorial blog. This was the first blog of the Blockchain tutorial series. This Blockchain tutorial blog will be followed by my next blog, which will focus on Blockchain technologies and Bitcoin Transactions. Do read them as well to learn more about Blockchain.
origin: 

Edureka

Comments

Most popular posts

Implementing a Custom Back Button in Swift

Using Realm Mobile Database with Swift 4.0 (Insert, Update, Delete, List)

Chronometer Tutorial With Example In Android Studio

WKWebView advanced tutorial (catch JS events, access properties etc...) (Swift)

How to adjust image content mode using aspect fill, aspect fit and scaling

How to clear all activity stack in Android

From Swift to Javascript and Back

How To: Map, Reduce and Filter in Swift

Extension functions in Kotlin: Extend the Android Framework (KAD 08)

SQL injection in Android content providers and how to be protected