fbpx

A Blockchain and A.I First Innovation Hub.

3rd Floor, Plaza 2000, Mombasa Rd.
+254 716 490 808
[email protected]
+254 716 490 808
3rd Floor, Plaza 2000, Mombasa Rd
[email protected]
pythonblockchain

Python Blockchain Coding: The Many different Languages You Need!

Guido van Rossum, a Dutch programmer, created Python back in 1991. Python is based on a simple philosophy: Simplicity and Minimalism. One of the more notable ways that they incorporated simplicity into their language is by using white spaces to signify code blocks instead of curly brackets or keywords. Let’s see what this means.

 

Let’s checkout a simple “hello world” program.

 

print(‘Hello, world!’)

 

Yup, that’s it!

Compare that to the C++ “hello world” program.

See how less complicated it is in comparison?  How about we do something a little more complicated? Let’s say we are adding two numbers and printing the result.

 

num1 = 1.5

num2 = 6.3

sum = float(num1) + float(num2)

print(‘The sum of {0} and {1} is {2}’.format(num1, num2, sum))

 

And that’s it.

The output of this program will be:

  • The sum of 1.5 and 6.3 is 7.8

So, let’s up the ante. How are we going to program an entire blockchain using Python? The following data and code is taken from Gerald Nash’s article in Medium.

Creating the block

Firstly, let’s make our block:

import hashlib as hasher

class Block:
  def __init__(self, index, timestamp, data, previous_hash):
    self.index = index
    self.timestamp = timestamp
    self.data = data
    self.previous_hash = previous_hash
    self.hash = self.hash_block()
  
  def hash_block(self):
    sha = hasher.sha256()
    sha.update(str(self.index) + 
               str(self.timestamp) + 
               str(self.data) + 
               str(self.previous_hash))
    return sha.hexdigest()

Code Analysis

We are starting off by importing the hash library to use the SHA 256 hash finctions (quite like Javascript).

Just like before, the block has the same value:

  • Index.
  • Timestamp.
  • Data.
  • Previous hash.
  • Hash.

Once against, we are filling up the hash values via a function, same as before.

Creating the genesis block

Now, let’s create the Genesis block:

import datetime as date

def create_genesis_block():

return Block(0, date.datetime.now(), “Genesis Block”, “0”)

Code Analysis

We have imported datetime to put in the timestamp.

We simply generated the genesis block and manually given it some data to work with. The previous hash value is “0” because it is pointing to no other block.

Creating the rest of the blocks

Now let’s define how the subsequent blocks will be created.

def next_block(last_block):
  this_index = last_block.index + 1
  this_timestamp = date.datetime.now()
  this_data = "Hey! I'm block " + str(this_index)
  this_hash = last_block.hash
  return Block(this_index, this_timestamp, this_data, this_hash)

Code Analysis

So, how are we going to be determining the values of each and every piece of data inside each and every block?

The block index is simple the index of the last block + 1.

The timestamp is the current date and time.

The data of the block is a simple message: “Hey! I’m block <index number>”.

Hash we are calculating using the function we definted earlier.

And ultimately, we are returning all these values to the block.

Creating the blockchain

Finally, let’s create the blockchain.

blockchain = [create_genesis_block()]
previous_block = blockchain[0]

num_of_blocks_to_add = 15

for i in range(0, num_of_blocks_to_add):
  block_to_add = next_block(previous_block)
  blockchain.append(block_to_add)
  previous_block = block_to_add
  # Tell everyone about it!
  print "Block #{} has been added to the blockchain!".format(block_to_add.index)
  print "Hash: {}n".format(block_to_add.hash)

Code Analysis

Firstly, we create the genesis block and give its value to “previous_block”.

Then we determine how many blocks to add, in this example we are going with 15.

So we are running a loop that goes till 15 and adds each and every block to the blockchain. At the end of the look we are printing which number block has been added to the blockchain via showing their index number. Plus, we are printing the Hash as well.

This is what the output will look like:

pythonblockchain

Image Courtesy: Gerald Nash Medium Article

Obviously in both this and the javascript you could add more complicated features like Proof Of Work. If you want to learn how to implement that then it is highly recommended to go through Gerald Nash’s article. But for now, you at least know how to create a simple blockchain in Python.

Next we’ll look at Solidity