How to create a Blockchain in Python [Basic Tutorial]

Introduction

In this tutorial, we are going to learn about the basics of Blockchain and then create a blockchain in Python. BC is undoubtedly one of the most disruptive technologies that have recently appeared in the world of technology. You will find Blockchain at the core of Bitcoin and other cryptocurrencies. However, you will also find it interesting to know that Blockchain has many other uses e.g in finance, Internet of Things applications, healthcare by storing medical records, e-commerce applications and even in managing government affairs like collecting taxes etc. So, let’s dive right into this!

Motivation

For the past two months, I have been managing the development of a blockchain project in my company. I had no prior expertise in the domain. Consequently, I had to read up on its specific aspects in whatever extra time I could find. The purpose of writing this blogpost is to help me just as much as it is to help you. If you find any errors, feel free to educate me in the comments.

Prerequisites

To follow this tutorial, you should have some prior basic programming knowledge in any language (preferably in Python). Make sure that you have Python 3.6+ installed. Other than that, the rest of the article is pretty easy to understand and follow. We will be using Sublime Text for creating a blockchain in python 3, but feel free to use any code editor that you like.

Blockchain Basic Components

Blockchain is a sequential chain of records aka. blocks. These blocks are a way of storing digital data which can be anything. In the case of Bitcoin, it is the transactions which are the logs of transfers of Bitcoin from one account to another. The data can even be files. The blocks of data are linked or chained together using cryptographic hashes, thus the name blockchain.

Block

Each block contains some data, the hash of the block and the hash of the previous block. The data stored inside the block depends on the blockchain’s type. For instance, the Bitcoin blockchain stores the details about transactions such as the sender, receiver and amount of coins. A block also has a hash which is like a fingerprint; it identifies a block and its content and is always unique just like a fingerprint. Once a block is created, its hash is calculated. Changing something inside the block will cause its hash to change. Thus, hashes are very useful if you want to detect changes within a block. Third element inside each block is the hash of the previous block. This effectively creates a chain of blocks and makes a blockchain very secure.

To visualize how this works, consider there to be three blocks: block A, B and C. Block C points to block B and block B points to block A. Since there is no block before block A, block A cannot point to a previous block and is known as the Genesis block . Let’s say someone tampers with the block b. This causes the hash of the block to change as well. In turn, this will make block C and all other following blocks invalid as they no longer store a valid hash of the previous block. Thus, changing a single block will make all the following blocks invalid.

Proof of Work Mechanism

Hash alone is not enough to prevent tampering. Computers these days are very fast and can calculate hundreds of thousands of hashes per second. You can effectively tamper with a block and recalculate all the hashes of other blocks to make your blockchain valid again. So, to mitigate this, blockchains have something called Proof of Work. It is a mechanism that slows down the creation of more blocks. In bitcoin’s case, it takes about 10 minutes to calculate the required Proof of Work and add a new block to the chain. This mechanism makes it very hard to tamper with a block because if you tamper with one block, you will need to recalculate the Proof of Work of the following blocks. So, the security of a blockchain comes from its creative use of hashing and the Proof of Work mechanism.

Distributed Technology and Consensus

Another attribute that makes blockchains secure is their distributive nature. Instead of using a central entity to manage the chain, blockchains use a P2P network that allows everyone and anyone to join. When someone joins this network, they get a full copy of the blockchain. The node can use that to verify that everything is still in order. When a new block is created, it is sent to everyone within the network. Each node then verifies it to ensure that it has not been tampered with. If everything checks out, each node adds this block to their own blockchain.

All nodes within the network create consensus, i.e. they agree on which blocks are valid and which aren’t. The other nodes in the network reject tempered blocks. So, to successfully tamper with a blockchain, you will need to:

a. tamper with all of the blocks in the blockchain,

b. recalculate the Proof of Work for each block and

c. take control of more than 50% of the P2P network.

Only then will your tampered block become a part of the chain. Practically impossible!

Creating a basic blockchain in Python

The beauty of this technology lies in the way the data is stored and added to the blockchain. As discussed above, blockchain is basically a linked list containing ordered data which cannot be modified or altered once added. This ensures immutability of data. We also get a verifiable trail of the order in which the data was added which provides accountability. There are also specific rules to follow when appending data. However, for the sake of ease of understanding, we will keep our implementation fairly simple and minimalistic. We will not be implementing any of the Proof of Work and Consensus algorithms. This blogs’ scope is limited to creating blocks, adding digital fingerprints (hash values) to them and chaining them together to create a final blockchain.

Step 1: Making the Blocks for our blockchain in Python

We have defined a class named Block. Each block contains its own hash value, the hash of the previous block, its data and the time at which the block was created. We have defined genesisBlock function inside the Block class that creates the genesis or the very first block.

    import datetime
    import hashlib
    class Block():
        def __init__(self, previous_block_hash, data, timestamp):
            self.previous_block_hash = previous_block_hash
            self.data = data
            # timestamp is when the block is created
            self.timestamp = timestamp
            # Hash function to take all of the data in our block header and run it through sha256 two times (inner and outer encryption)
            self.hash = self.hashFunction()

        @staticmethod
        def genesisBlock():
        # 0: previous block hash
        # 0: data
        # current time for timestamp
            return Block("0", "0", datetime.datetime.now())

Step 2: Adding Digital Fingerprints to our blockchain in Python

The hash function used to calculate hash values for each block is defined within the same block. Python’s hashlib library is to do this and the hash function chosen is SHA256. The inner and outer encryption using this algorithm can be seen in the code below:

    def hashFunction(self):
        # header: previous block hash, data and timestamp
        # convert all header data to string
        block_header = (str(self.previous_block_hash) +str(self.data) +str(self.timestamp))


        # inner and outer encryption using sha256 algorithm
        # encode :create binary representation of all the string data that we have in our header
        inner_hash = hashlib.sha256(block_header.encode()).hexdigest().encode()
        outer_hash = hashlib.sha256(inner_hash).hexdigest()
        return outer_hash

Step 3: Chaining the blocks together to form our blockchain

Finally, we have created the BlockChain class to chain all the blocks together. We first create the genesis block and append it to an empty list. Then, all other blocks are created based on user input and appended to the same list. Each block contains its own hash, hash of the previous block and its data which in this case is the block number, to keep things simple.

    class BlockChain():

        number_of_blocks = int(input("Enter number of blocks in your blockchain: "))
        block_chain = [Block.genesisBlock()]
        print("Block 1 (Genesis Block) created")
        # gives hash value of the first value in the blockchain
        print("Hash of block:- %s" % block_chain[0].hash)
        print("Hash of previous block:- 0")

        for i in range(1, number_of_blocks):
            # adding new blocks to the blockchain using append
            # block_chain[i-1].hash : hash of previous block
            block_chain.append(Block(block_chain[i-1].hash,
                # block number : data of the block
                "Block number %d" % (i+1),
                datetime.datetime.now()))
            print("Block %d created" % (i+1))
            print("Hash of block:- %s" % block_chain[-1].hash)
            print("Hash of previous block:- %s" % block_chain[i-1].hash)

Output:

Enter number of blocks in your blockchain: 7
Block 1 (Genesis Block) created
Hash of block:- 7174ad66f01073723fbcbd0e27bb5fef8a82ec86b6181861281ffa944b16d559
Hash of previous block:- 0
Block 2 created
Hash of block:- 2b4a58b519f9a5532705712fa41f8e02d605aad62e1294b3e5146475eaca9cde
Hash of previous block:- 7174ad66f01073723fbcbd0e27bb5fef8a82ec86b6181861281ffa944b16d559
Block 3 created
Hash of block:- 7b8206a12ec3c5219d51607a1175781700bbb34a1749694f2c36f15ace6888a9
Hash of previous block:- 2b4a58b519f9a5532705712fa41f8e02d605aad62e1294b3e5146475eaca9cde
Block 4 created
Hash of block:- 1893bb80d501b0a8cc0a795f54d043955132bd76c3b04936684352524ce0d739
Hash of previous block:- 7b8206a12ec3c5219d51607a1175781700bbb34a1749694f2c36f15ace6888a9
Block 5 created
Hash of block:- f2e2bade25d77abec05ef8f72effae42580761a5631b95bff10b86e2be26ead8
Hash of previous block:- 1893bb80d501b0a8cc0a795f54d043955132bd76c3b04936684352524ce0d739
Block 6 created
Hash of block:- e82873a2bf316befe31ca092440cf810fc4e02027309531a85954373efa8b3f7
Hash of previous block:- f2e2bade25d77abec05ef8f72effae42580761a5631b95bff10b86e2be26ead8
Block 7 created
Hash of block:- 80b1afb301cccf0e90100e0e706a569461947a5327d1f887d83506fc3363a2f0
Hash of previous block:- e82873a2bf316befe31ca092440cf810fc4e02027309531a85954373efa8b3f7

Conclusion

To sum it up, in this tutorial, we covered some of the fundamentals of blockchain and why it has taken the technological world by storm. We discussed some of its popular uses and went through a lot of its concepts that ensure data immutability and security, e.g. hash functions, proof of work algorithm, distributive technology and consensus algorithm. Lastly, we implemented a very minimal and simple application of blockchain which covered its main concepts like chaining etc. to provide a visualization of a blockchain.

If you’re a university student doing a degree focused in IT and are interested in community building, you might want to give GitHub Campus Expert Program | Application Process a read as well!

Leave a Reply

Discover more from Junaid Khalid

Subscribe now to keep reading and get access to the full archive.

Continue reading