Advertise with Us!
We have a variety of advertising options which would give your courses an instant visibility to a very large set of developers, designers and data scientists.View Plans
Blockchain Programming: Step-by-Step Guide for Beginners (with Code)
Table of Contents
Blockchain Technology Overview
Traditionally, there have been middlemen who have been controlling the data. For instance, Facebook is the middleman between users and advertisers. Banks are middlemen between borrowers and lenders.
The issue here is that the data is controlled by a central authority - Facebook and banks. They, therefore, control the prices and also technically own the data. In this scenario, all the power lies with a middleman and users have to trust them to remain ethical and continue to think about user interest. Blockchain was invented to make the data decentralized and trust minimized with any centralized party.
In order to understand it better, let us talk about torrents. In torrents, no file is owned by a single person. It is a kind of “peer-to-peer” architecture where multiple copies of the file are present at multiple workstations/users. Therefore, no single person controls that file. This kind of architecture is highly scalable and fault tolerant since there is no single point of failure.
Blockchain essentially borrows the same idea: what if databases could be decentralized. This solves a lot of issues:
- You can directly connect to a borrower without a middleman like a bank. You will get more profits.
- You can connect directly to advertisers to tell them the kind of ads that you like, without going through Facebook.
One of the well-known use cases of Blockchain is that of cryptocurrency which is a digital currency that isn’t controlled by any central authority. Similarly, we can use Blockchain for smart digital contracts where no middleman is verifying the contract. There are several applications and use cases of Blockchain for which we've written an in-depth article: https://hackr.io/blog/applications-and-use-cases-of-blockchains
Blockchain Programming Fundamentals
In order to understand Blockchain deeply, let us first talk about the concept of a Digital Signature or a Hash.
Digital Signature is basically a function that takes a string as input and returns a fixed-size alphanumeric string. The output string is known as the Digital Signature or the Hash of the input message. The important point to note here is that the function via which we obtain the Digital Signature is “irreversible” in that given an input string, it can compute the Hash. However, given the Hash, it is virtually impossible to compute the input string. Further, it is also virtually impossible to find 2 values that have the same Hash.
hash1 = Hash(input1)
hash2 = Hash(input2)
Here, what we are essentially trying to say is the following:
- It is easy to compute hash1 from input1 and hash2 from input2.
- It is virtually impossible to compute input1 given the value of hash1. Similarly for input2 and hash2.
- It is virtually impossible to find distinct input1 and input2 such that hash1 = hash2.
Such Hashing functions are carefully designed by cryptographers after years of research. Most programming languages have a built-in library function to compute the Hash of a particular input string.
Why are we talking about Hash function?
Well, Blockchain as a concept relies heavily on Hashing. The idea is that in a Blockchain, we have an ordered chain of blocks such that each block contains the following information:
- Hash of the previous block.
- List of transactions.
- Hash of itself.
Let us take an example. Consider the following simple block: [0, “X paid $100 to Y”, 91b452].
Here, since this is the first block of the Blockchain, the Hash of the previous block is 0. The list of transactions contains just 1 transaction - X paid $100 to Y. The Hash of itself is computed by the following way:
hash_itself = Hash(List of transactions, Hash of the previous block)
Basically, we combine the List of transactions and the Hash of the previous block as a single input string and feed it to the Hash function to get the hash_itself value.
Such blocks where the Hash of the previous block is 0 are termed as Generis Blocks. A Genesis block is basically the very first block in a Blockchain.
Now, suppose we want to add some more blocks to this Blockchain. Let us have block1 = [91b452, “Y paid $20 to Z, X paid $10 to P”, 8ab32k].
Here, 91b452 is nothing but the Hash of the previous block (the Genesis block). There are 2 transactions:
- Y paid $20 to Z
- X paid $10 to P
Finally, we have the hash_itself value which is basically Hash(“Y paid $20 to Z, X paid $10 to P”, 91b452). This turns out to be 8ab32k.
Representing pictographically, our Blockchain looks like the following:
So, what’s so special about this “data structure”?
Well, the idea is that if suppose someone were to mutilate the Blockchain by say altering the transaction in the Genesis Block - changing “X paid $100 to Y” to “Y paid $100 to X”, this will change the hash value of 91b452. As a result, there will be a mismatch in the value of this hash in block1 (remember, the first value of each block is the hash value of its parent block). As a result, the chain becomes invalid. This effectively holds for each block in the Blockchain because as soon as we modify a block, the hashes of all subsequent blocks become invalid and so, the chain collapses. Therefore Blockchain provides a high level of data security.
Blockchain Programming Code Example
Now suppose, if we were to mutilate the genesis_block
The output will be as follows:
Here, the value of genesis_block_hash and block1_parent_hash are clearly different while they should actually be the same in the correct Blockchain. As a result, the Blockchain is now corrupted.
Think of Blockchain as a distributed and secured data structure that can be used in places where no middlemen are involved. The decentralized nature of Blockchain is what helps in removing the middlemen and it comes from the above immutability of Blockchain. It is an interesting data structure and as we all have seen cryptocurrency is a real-life implementation of it. PS: Looking for Blockchain tutorials? Here are the programming community-recommended best Blockchain tutorials.
People are also Reading: