Toy Python BlockChain Implementation

Blockchain has been in the news for quite sometime now Though I think it might be a little early to believe people hyping it as the next internet, it is an excellent tool for asset/ownership management. . There are a number of implementations in different languages(and in Python as well) .However there isn’t a Python BlockChain implementation which simple enough to understand while being fully functional.

My implementation borrows very heavily from naive-chain (a very simple blockchain implementation in javascript). If you are more comfortable with Javascript than you are with python, I would really recommend going through the article.

Parts of a Blockchain

A blockchain based system coupled with a proof of work (or a proof of stake) based approach is the perfect decentralized datastore. It can be broadly divided into 3 parts:

  1. Data Layer: The component of the blockchain which actually stores all the data.
  2. Distributed Consensus: The consensus protocols which govern what should/should not be added to the data layer.
  3. External Communication: This is responsible for communicating with other blockchain nodes as well as with the outside world.

I’ll go over snippets of the implementation of the blockchain for each part of the blockchain.

PyNaiveChain – The Python BlockChain

The Data Layer

This is the actual data which gets hashed and chained over a course of time. This forms the actual blockchain. All transactions happening are recorded in a time series fashion .

block_chain_explaination (1) :

Every new transaction has the hash digest of the previous transaction saved into it. This causes the blocks of data to be chained together by a verifiable trail of hashes. This is reason for it being called “BlockChain”. Every block ever added is linked to all other blocks by this relation. Change even a single block hash or data and the whole chain becomes invalid.

Sample implementation:

import json
import hashlib
import datetime

class Block(object):
    This object forms the actual block of the block chain
     def __repr__(self):
        return 'Block <{}::{}>'.format(self.index,

    def __init__(self, index, previous_hash, data, *args, **kwargs):

        # index of the block
        self.index = index

        # hash of the previous time stamp
        self.previous_hash = previous_hash

        # takes data in string = json.dumps(data)

        # hash of the block
        self.curr_hash = self.get_current_hash()

    def serialize(self, json_dump=False):
        Function for getting the dict representation
        of a block
        block_repr = {}
        block_repr['index'] = self.index
        block_repr['previous_hash'] = self.previous_hash
        block_repr['data'] =
        block_repr['curr_hash'] = self.curr_hash

        if json_dump:
            block_repr = json.dumps(block_repr)

        return block_repr

    def get_current_hash(self):
        Class function for getting hash of the current hash
        hasher = hashlib.sha256()
        hasher.update(self.previous_hash.encode() +
         return hasher.hexdigest()


Distributed Consensus

Image result for nakamoto consensus

Why do I need distributed consensus?

A block chain storage scheme offer’s complete protection against data corruption. However data corruption is still possible if a single party has all the data. To overcome this problem blockchain builds on the assumption(read requirement) that there are multiple copies of the data with different parties . Additionally all of them modify/update there data together.

Distributed Consensus protocols govern how these nodes/copies of data interact with each other and modify/update the data.

The BlockChain cluster does not trust a single peer. Only when more than 50% nodes send a positive response for a request does the data get written into the BlockChain. This is where distributed consensus comes into play.

There are different consensus protocols available which can be used quite effectively to ensure distributed data and system integrity . Bitcoin uses Nakamoto consensus(check it out here). There is also a very nice dedicated article here(ethereum). PyNaiveChain uses a  simple count based consensus.

Any addition to the block chain goes through the add_data function in the blockchain.

def add_data(self, data):
    block = self.create_sample_block_data(data)
    consensus_result = self.protocol_processor.get_peer_agreement(({'CMD': 'VALIDATE_BLOCK', 'block_data': block.serialize()}))

    if not consensus_result:
        return False

    self.protocol_processor.write_to_peers(self.peer_connect_dict.keys(), json.dumps({'CMD': 'ADD_BLOCK', 'data': data}))
    return self.protocol_processor.get_peer_agreement(block.serialize())

The function above creates a sample data block, and sends it out for validation to all the other nodes in the distributed system via the get_peer_agreement function. This function implements a basic 50% consensus for the blockChain. The code is:

def get_peer_agreement(self, data):
    Rudamentory consesous
    peers = self.chain_instance.peer_connect_dict
    total_count = len(peers)

    result_list = self.write_to_peers(peers.keys(), json.dumps(data))
    if not result_list.count('ACK') > total_count/2:
        # motion not carried
        return False
    return True

The function only returns True, if 50% or more than 50% nodes in the system responded with a positive response.

External Communication and RPC layer

My implementation has two communication layers implemented into it:

  1. A Web socket layer: To communicate with the other peer nodes.
  2. A HTTP layer to interact with the world outside(simple flask rest plus based views)

Cloning and Running my implementation

You can find the code here (with instructions to run it)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s