Develop Blockchain

“four round silver-colored and gold-colored Bitcoins” by Thought Catalog on Unsplash

Simple introduction to the world of blockchain by guiding you step by step through the work of programming one on your own. We will use the programming language Python, with which (in my opinion) the concepts can be shown in the easiest way. So let’s start!

A short introduction

Since Bitcoin came into the market, blockchain technology not only experienced a big hype, but also a lot of developments. Once you start diving into this pool of knowledge, you might not find a way out. There is so much to discover! The simplest definition of a blockchain in my understanding is the following:

“A Blockchain is a decentralized database, that uses cryptographic methods to keep up integrity.”

So blockchain is a database. There are blocks, each block can contain data. So far, so good. What does decentralization mean? Well, have a look at the classical structure of the internet. Once you visit a website, your computer connects with the server of the one running the site. You are a client, just requesting some information from the server. Such a network architecure may be called centralized. If you design the network in a way, that every participant works both as a server and a client, has to follow some certain rules and is communicating with each other to keep everybody updated, you may achieve decentralization. What is so attractive about that, is that you are no longer forced to rely on some third party. So now we have understood a little more about what a blockchain actually is. Let’s dive a little into code.

In this very article I will focus on the database aspect of a blockchain. We will not create a network, but we will create blocks, in which we can store data, whose integrity is then secured by cryptography.

Cryptographic Hash functions

First of all we start with the probably most important cryptographic concept used in blockchain technology — the hash functions. A hash function is defined as a function, that maps some elements from a pretty big set onto an element of a much smaller set. The elements from the big set may have different lengths, whereas the elements from the target set all have the same. To visualize that, take a look at the following picture.

But the title of this section says cryptographic hash function. Is there a difference? Oh yeah, there is! Such hash functions, as described above, most of the time are not functions as we know them from school. It is more likely, to see them as an algorithm, that some genius IT guy once developed. To get a cryptographic hash function, the algorithm has to fulfill some certain properties.

  1. Whatever element you like to hash, the algorithm is able to do it in a short time.
  2. It does not matter, how often you apply the algorithm to hash your element, you will always get the same value for the hash.
  3. You cannot restore the original element from the hash value — the only possibility is a brute-force attack (means just trying each possible element).
  4. If you change the element, that you like to hash, just a little bit, you get a totally different hash value after applying the algorithm.
  5. The hash algorithm works in a way, that two different elements hashed will never give you the same hash value.

If a hash algorithm developed by one of those genius IT guys mentioned above proves to have all these properties, it is called a cryptographic hash function. After we worked now through a lot of theory, it is time, to programm a little bit. We are going to hash something.

Open your python IDE and start typing some code:

import hashlib
import json
data = “I am gonna be hashed”
data_package = json.dumps(data).encode(‘utf-8’)
Hash = hashlib.sha256(data_package).hexdigest()
print(Hash)

The first line tells the computer to import the hash library in to the program. The hash library contains many hash algorithms, that you can apply to hash everything you ever dreamed of. After that, the json library is being imported. This is just a library, to pack different types of data into a format that can be read by both a computer and a real human. In the line

data = “I am gonna be hashed”

we define a string named data, that contains some information we like to hash. After that we put it into the json-format

data_package = json.dumps(data).encode(‘utf-8’)

and save it in a variable named data_package. This variable then will be hashed. We call the sha256 function, that is included in the hashlib

Hash = hashlib.sha256(data_package).hexdigest()

and turn the hash value, before saving it in the variable Hash, into hexadezimal code. The sha256 hash algorithm by the way is the one also used in the Bitcoin network.
After the hash is calculated, we print it out. The result should look something like this:

ea7ca0fe96c8497152be798c9d57376dc13aa23beb1bc36e9021b29c6590e884

Congratulations! You calculated your first hash value!

Now the Blockchain

Now it is time to code the blockchain part! First, I will give again some short theoretical introduction. The blockchain itself (neglecting this whole decentralization part) is a list of blocks. Each block contains information — in the simplest form a hash value, data, and the hash value of the previous block. The Hash value of the block itself is dependent on the data and the previous Hash. So, a little mathematically, with the sha256 — algorithm:

Hash(Block) = sha256(data + previousHash)

To visualize this concept again, have a look at the picture below.

Without talking too much about theory, here is the code:

import hashlib
import json
blockchain = []
def generateBlock(previousHash,data):
dic = {‘data’:data,’previousHash’:previousHash}
dic_package = json.dumps(dic).encode()
Hash = hashlib.sha256(dic_package).hexdigest()
block = {‘Hash’:Hash,’data’:data,’previousHash’:previousHash}
return block
genesisBlock = generateBlock(0,’I am Lokesh’)
blockchain.append(genesisBlock)
secondBlock = generateBlock(genesisBlock.get(‘Hash’),’I am Lokesh Gupta’)
blockchain.append(secondBlock)
print(blockchain)

So, let’s work through those lines of code. In the first two lines we again imported the libraries hashlib and json. Nothing changed so far. Then we defined the blockchain as an empty list:

blockchain = []

After generating blocks, we will append them to this list. In the next line, we start to define a function

def generateBlock(previousHash,data):

The aim of this function is, as the name apparently says it, to generate blocks — dependent on the hash of the previous block and the data, that shall be included in this block. In the following lines

dic = {‘data’:data,’previousHash’:previousHash}
dic_package = json.dumps(dic).encode()
Hash = hashlib.sha256(dic_package).hexdigest()

we put the previous Hash and the data into a dictionary, to get a structure into all this. It is this very dictionary, that will be hashed in the following lines. The block itself is then also defined as a dictionary:

block = {‘Hash’:Hash,’data’:data,’previousHash’:previousHash}

The block dictionary contains the above calculated hash value, the data and the previous Hash. In the end, the function returns this block and it is thus generated. With the lines

genesisBlock = generateBlock(0,’I am Lokesh’)
blockchain.append(genesisBlock)

we generate the Genesis Block, which is how the first block of a blockchain is called. The argument for the previous Hash is set to 0. The data is just an information, that this is the first block. After generating the Genesis Block, it is appended to the by now still empty list called blockchain. After that we generate the second block:

secondBlock = generateBlock(genesisBlock.get(‘Hash’),’I am Lokesh Gupta’)
blockchain.append(secondBlock)

We get the hash value from the Genesis Block and set the previousHash variable to this value and in the data we say, that this is the second block. Again, we append this block to the blockchain. The result in the end should look something like this:

[
{‘Hash’: ‘488be7d7f7aed0508b519ebcc0847a5304aa97098dc6b06f30df77fbef126aba’, ‘data’: ‘I am Lokesh’, ‘previousHash’: 0},
{‘Hash’: ‘6014a2cf8b80f4b1f0dca57ad6cf32477261921db60176412024914af176ecba’, ‘data’: ‘I am Lokesh Gupta’, ‘previousHash’: ‘488be7d7f7aed0508b519ebcc0847a5304aa97098dc6b06f30df77fbef126aba’}
]

The blockchain in this case is a list of dictionaries, each dictionary contains three information — the hash value, the previous Hash and the actual data. So again, congratulations


Develop Blockchain was originally published in Data Driven Investor on Medium, where people are continuing the conversation by highlighting and responding to this story.