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:

 

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.

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:

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)

I am a developer and tech enthusiast based out of New Delhi, India. I love python , have a love-hate relationship with Javascript, and feel that filing out tax forms are easier than Java.

1 Comment

Leave a Reply