How to create YOUR own Cryptocurrency!!!

Part - 2: Building blocks and chaining it up (blockchain)

Anantha Perumal
Coinmonks
Published in
6 min readOct 11, 2020

--

Photo by Launchpresso on Unsplash

Welcome back everyone Lets create a simple blockchain. Hoping everyone read my previous blog else please go through it so that you can understand this one. Those who had missed out click here to have some basic knowledge about blockchain.

In this blog, we will be discussing about creating blocks and chaining it up together to create a simple blockchain. Whoever wondered about blockchain the simple definition is a chain of blocks (seems an answer for an unknown question by a student😂😂).

Chain of blocks (source)

A block may have anything like that of developer’s wish, usually, a block will have previous hash(hash value of the previous block), data and self hash. Now, what is meant by hash here 🤔🤔?? A hash is a function that converts an input of characters and numbers into the encrypted text of fixed length. There are a lot of hash functions, here I am using SHA256 as my hash function. If anybody wants to get an idea about hash function or SHA256 kindly mention in comments I will do a separate blog about that.

I hope we are clear about the technical part for this blog let give work to our keyboard. I am gonna use python language to code.

Let’s code a simple block having data, hash and previous hash

Here everything was initialized to zero while the previous hash is initialized with 64 digits of zero because the output of SHA256 is 64 digits and the first block will not have any previous hash. Did you find anything odd in the above code? If not please take a look over nonce any idea about what is it?

A nonce is an abbreviation for “number only used once,” which is a number added to a hashed/encrypted block in a blockchain that, when rehashed, meets the difficulty level restrictions.

The above code snippet is known as the constructor for the block which will initialize the data and the block number respectively. That’s all we have successfully created a block lets go for hashing.

I have defined another method hash for the class block which will return the hash of that block. Looking inside it calls updateHash function:

updateHash function accepts n number of arguments which will be appended to a single string which consists of previous block’s hash, block number, data in it and number only used once naming it as hashing_text. As I told earlier I am using SHA256 hash so creating an instance for it and updating hashing_text as encoded utf-8. Now the hexdigest() function will provide the hash for that block.

Now for printing purpose, I am overriding __str__ method as:

Well done we have created blocks using python!!!..

Let’s code a simple blockchain, the blockchain will usually have a difficulty which is a simple number providing criteria that hash should have that many zeroes at the start of it. The more the difficulty the more time it takes to create a hash.

Now I am creating a constructor for the above class which initializes the chain of block:

If we have a block with all the field initialized we can go for adding that block into a blockchain:

Else if we have only data then we have to mine it to have all the fields to be initialized:

Above we are initializing the previous_hash of a block then we are hashing the block until the difficulty is met.

Good Job guys, we have done our coding part!!

Photo by Alasdair Elmes on Unsplash

Let's see how this is working

Above I have created an instance for BlockChain class and we have 3 data which differs in only one digit. ‘num’ refers to the block number which will be incremented on adding blocks.

Save this code as <filename>.py to execute it go to the location where it is located and type python <filename>.py

So the output looks like

Sorry for the blurry image please right click and select view image to view this image clearly.

To be noticed in the output:

  1. BlockChain: shows how the blocks are linked together with data in it.
  2. Blocks: The first block previous hash will be 64 digits of zero. The second block previous hash will be the previous block’s hash
  3. MOST IMPORTANT: we have modified only the last digit of data but the hash of each block has many differences.

For those who need source code can view it here

Thank you so much for spending your valuable time in my blog. Please support me by doing claps at the rate of 1–50 by the way you feel it worth. If you have any doubts regarding this blog or suggestion to improve it please do comment below and share it to those who want to know about blockchain.

You can find the next part link here. I will get you things as simple as you could understand😉😉.

Have a good day😊😊

Also, Read

--

--