Understand BlockChain by Building one-Part 01

Kavindu Pathberiya
5 min readApr 2, 2020

--

Source: https://supplychainbeyond.com

Hi! This article is a simple guide to build a standard Blockchain in python. We create Clients and transactions as separate classes and use them to create blocks in a more clear and understandable manner. The whole article will come with 2 parts. This is the first part of it and here we define the necessary classes and build the first block of the blockchain. It is super easy and anyone can build your own blockchain by following this article. So let’s get started!

We use Jupyter notebook for implementation and first of all, we import the necessary libraries to the notebook as follows.

Client Class

First, we create the Client Class. The client is someone who is able to send money from his wallet to another known person. Also, a client is a person who is able to accept money from a third party.

In the Client class, we generate 2 types of keys such as Private Key and Public Key. You should never expose your private key for others. The generated public key is used as the client’s identity in transactions. We generate those key values using the RSA algorithm. During the object initialization, we create these keys and store them in the instance variables. We define a property called ‘Identity’ to return the Hex representation of the client’s public key. (PKCS1 is the standard defines the mathematical definitions and properties that RSA public and private keys must have)

Now let us test the instance of a Client class and its generated public key

Transaction Class

Now let us create the transaction class. Here we define the sender, receiver, time of transaction and the value of the transaction. To complete the transaction we need the receiver’s public key. For example, when you want to receive money, some other sender will create a transaction and specify your public address in it. In the method, we take 3 parameters such as the sender’s public key, receiver’s public key and the value of the transaction. (Use of transactions list and last_block_hash variable will be explained later)

Next, we create a method called to_dict to combine all the 4 values in a transaction to a dictionary object. In the blockchain, the first block of the chain is called the “Genesis block”. The Genesis block contains the first transaction originated by the creator of the blockchain. Thus, while creating the dictionary, we check if the sender is Genesis and if so we simply assign some string value (“Genesis”) to the identity variable. Otherwise, we assign the sender’s identity to the identity variable

Next, we sign the dictionary object using the private key of the sender. We create a method sign_transaction to create a signature and convert it to ASCII representation to store in our blockchain.

Finally, we create a method to display the transactions in a clear manner.

Let’s create a few instances of Transaction Class and append it to the transactions list that we create earlier. Note that the first parameter is the sender, the second parameter is the public key of the recipient and the third parameter is the amount to be transferred.

The sign_transaction method retrieves the sender’s private key from the first parameter for signing the transaction. After the transaction object is created, you will sign it by calling its sign_transaction method. This method returns the generated signature in the printable format.

Now let us display all the transactions. As you see below all the information including the generated keys of the clients will be displayed.

Block Class

So far we create two classes; Client and transaction. Now let us look at the Block Class. A block can consist of many transactions. For our explanation, we will assume it has a fixed number of transactions which is three.

First, we create an instance variable “verified_transactions” to include the valid transactions. Each block also keeps the hash value of the previous block, so that the chain of blocks becomes unchangeable. To store the previous hash, we declare an instance variable called “previous_block_hash”. Next, we declare another variable called “Nonce” to store the nonce value generated by the miner in the mining process. In mining, every single miner starts trying to find the solution to that one Nonce that will satisfy the hash for the block

Create the Genesis Block

Now we create the first block of the blockchain; The Genesis block. First, we create a Client object and transaction object and set the parameters accordingly.

As this is the first block, we do not have the sender’s public key. So we send the string “Genesis” as the parameter. Next, we create the block object and append the transaction details and hash the entire block and store its value in a variable called last_block_hash. This value will be used for the “previous_block_hash” in the second block that we create.

Create the BlockChain and display the Blocks

We create a list called TPCoins to store all the blocks. Then we define a method to display the blocks inside the TPCoins list.

Let’s append our Genesis block to the list and display its details.

Congratulations! You have complete the first part of the building the Blockchain. In the next part, we will create the mining function and add more blocks to the chain to see the output of the fully designed blockchain.

Thanks for reading. See you soon with part 02.

References: https://www.javatpoint.com/blockchain-tutorial, https://www.youtube.com/watch?v=baJYhYsHkLM&t=1s, https://www.tutorialspoint.com/blockchain/index.htm

--

--