SHASHANK KHANNA
  • Blog
  • Gallery
  • About
  • Contact

BlockChain in Python

 

Purpose

To implement a Blockchain protocol by creating a cryptographically secure data structure in Python.
​

What is Blockchain

Before we look at the implementation, lets take a quick look at what is a Blockchain. As per Wikipedia:

"A blockchain is a growing list of records, called blocks, which are linked using cryptography. Each block contains a cryptographic hash of the previous block, a timestamp, and transaction data (generally represented as a merkle tree root hash). By design, a blockchain is resistant to modification of the data. It is "an open, distributed ledger that can record transactions between two parties efficiently and in a verifiable and permanent way."

We will implement the Blockchain data structure by providing ability to group current transactions, create blocks and then link that block with the current blockchain by applying a proof-of-work (PoW) algorithm. 

Required Libraries and Initializing Schema

We will need Python's hashlib and json libraries.
  • We will use json to convert list of transactions into a JSON format message that can be then used to create a block. 
  • hashlib will be used to perform the Proof-of-Work (PoW) using SHA-256 Cryptographic hash algorithm.
 ​
​​Next, we need to define the schema for Transactions and Blocks.
  • Each Transaction will have a Sender, a Recipient, and an quantity. Let's name them From, To, and Amount.
  • Each Block will have:
    • Index representing Block number in the Blockchain. 
    • Timestamp representing exact timestamp the block was created aka mined. 
    • Transactions  lists out all the transactions done since the previous block got created
    • Hash contains the Proof-of-Work (PoW) that when hashed with previous block's hash returns a value beginning with four zeroes or "0000". This can be changed and thereby setting the difficulty level of the PoW algorithm. 
    • PreviousHash is the SHA-256 hash representation of previous block. 
Imports and Setting schema for Transaction & Block

    

Create a Genesis block

We need to initialize the Blockchain by first creating an empty block, called the genesis block.

Since, this is the first block we manually specify value for Previous Hash and a Proof Hash and use that to create this block. This is then added to our blockchain. 

Below code performs this functionality as seen from the output logs. 
Genesis Block

    

Now let's add some transactions

To create a transaction, we need 3 required values - From, To, and Amount, as seen in the schema above.

​Below functions helps us in creating these transactions and have them added to the list of pending (aka mineable transactions). 
Create transactions

    

Create Block and Mine

Now, its time to do the real stuff - Proof-of-Work (PoW) implementation by using SHA-256 cryptographic hash algorithms. 

In order to perform PoW, we need two inputs:
  1. PreviousBlock 
  2. LastProof or Previous Block's Hash attribute in our case

We use input 1) - and dump it into JSON format (as show below in hash_block function) in an ordered way to maintain sanity over hashes. This dump is then passed onto hashlib's sha256 algorithm (also shown below). 

Now, we have LastProof and Hash of  LastBlock. Let's start the guess work (aka Proof-of-work) by incrementing a variable, called guess, by 1 and then computing its hash concatenated with already computed LastProof and LastBlock's hash. As soon as we get a hash that starts with "0000" (or we can change it depending on difficulty level we want to set), that "guess" becomes our proof for this block. 
Functions to create block and perform PoW

    

Source-code

​Full source-code available at my GitHub repository @ ​https://github.com/shashank-khanna/Blockchain-Py

    Author

    I am a passionate, driven polyglot programmer and architect with a knack of solving complex problems in quick and efficient way.  Along with programming, software development, financial products, and management expertise, I also bring skills in statistical modeling, empowering me to work on challenging projects that require combination of software development and quantitative analysis.

    Categories

    All
    American Option Prices
    Black-Scholes Method
    Blockchain
    Bokeh
    Brownian Motion
    Cryptography
    Derivative Pricing
    European Option Prices
    Gaussian Process
    Linear Regression
    Matplotlib
    Monte Carlo Simulations
    Monte-Carlo Simulations
    Numpy
    Ordinary Least Square (OLS)
    Pandas
    Put-Call Parity
    Python
    Sha-256 Hash
    Statistical Analysis
    Stochastic Process
    Stock Analyzer
    Time Series Models
    Wiener Process

    RSS Feed

  • Blog
  • Gallery
  • About
  • Contact