Blockchain Simplified

A Random Papa John’s Pizza

If we believe the article published by Quartz on May 22, 2018, a programmer purchased two large Papa John’s pizzas for 10,000 bitcoins in 2010, worth about $30 at the time, and $82 million in the current year 2018.

What is bitcoin?

Bitcoin is a digital currency or crypto-currency, and it is based on technology that simplifies the transactions by removing the complexities involving the requirement of a central system (for example, banks).

Besides Bitcoin, several other digital currencies such as Litecoin, Ethereum, Dash, Ripple etc. has been invented since then.

Who can trade using crypto-currencies?

Any individual with a normal internet connectivity can buy, own and exchange these crypto-currencies. The technique or the architecture behind this exchange is blockchain. But how it works, and what makes it so revolutionary?

What is blockchain?

Blockchain can be defined as a digital record of transactions, which contains logs of all transaction in sequential order.

Consider a bank transaction in which a transaction between a sender and a receiver is monitored, logged and validated by a central authority, which is the bank itself. Similarly, blockchain also logs, monitors and validates all the transactions through a public network of computers connected through the internet, thereby eliminating the need for a central authority.

Every single transaction is termed as block, and each block is connected to previous block to form a blockchain.

How are blocks validated without central authority?

A blockchain contains many members in a network who regularly check to ensure the validity of each transaction. Each member denotes a computer that holds a copy of blockchain. Whenever a new block is added, all the computers cross-validate their copies, thereby making the system as decentralized. The threshold for the addition of a new block is validity by 51% of all members of the network. As a block is verified as a valid transaction, it is added to each member’s copy of blockchain. This approach removes the need for a central authority, hence automating the completion of transactions and ensuring a high level of security (we will get back to the security part :P).

The approach in which members of a network work together to validate transactions, thereby eliminating the need for a central authority is defined as decentralization.

What is a Block?

A block represents a transaction or a group of transactions in a blockchain environment. A block contains the following information:

  1. Timestamp: The time when a block is created
  2. Data: The transaction information of a particular block
  3. Previous Hash: This is a reference to the prior block in the blockchain.
  4. Hash: This is a unique digital fingerprint which is produced by combining all the contents within the block.

What is Hashing?

Hashing is a form of cryptography that is intrinsic to the architecture of blockchain. It converts any form of data into a unique string of text. This conversion is achieved using a hash function.

As per Wikipedia, a hash function is defined as any method or function that can be used to convert data of arbitrary size to data of fixed type.

Source: Title: Demonstration of Hash Function

Hashing should not be confused with encryption. Encryption is a two-step process where we have the ability to encrypt and decrypt a function. While hashing in blockchain is deterministic and is one-way, i.e. same input will always produce the same hash, but output cannot be used to produce the original input.

So far, we understood that each block or transaction in a blockchain is “chained” to previous blocks through the hash of the previous block. And if the contents of the previous block are tampered with, its hash changes and the chain breaks. But what about the first block which does not have a block before it? Therefore, the first block is different from the rest, and is termed as Genesis block.

To resolve the issue with Genesis block where we do not have a reference (hash) to the previous block, we hard code the previous hash value with zero.

How blockchain transactions are handled?

Let us suppose we have a blockchain with multiple transactions.

  1. As transactions are carried out, they are placed in a holding area for all the transactions. This special location or area is called as mempool.
  2. The latest transactions in mempool are then broadcasted to all the blockchain members.
  3. Each member collects these transactions in a new block, and each block has a limit for the number of transactions it can hold. As the block reaches its threshold, the next set of transactions wait in mempool. At this instant, the block for a member is in unconfirmed state and transactions inside it are invalidated.

We know that the members of the blockchain network are anonymous, and it would be a wrong expectation from them to verify all transactions honestly.
To handle this issue, we have a security feature in the blockchain, known as Proof-of-Work.


Proof-of-Work presents a computationally difficult math problem, which is quite complex and takes time for even a computer to solve. Therefore, the members, also known as miners now need to solve the computational problem to be qualified enough to broadcast their block.

Members who calculate Proof-of-Work to mine new blocks are Miners.

The members use an arbitrary number, which is then combined with contents of the block, and repeats this process until the desired hash is generated. The first miner or member to solve the problem broadcasts his/her unconfirmed block with the correct arbitrary number with which the problem was solved. Other members of the network validate this calculation, and if the majority of them agree, the Proof-of-Work of a block is said to be complete.

Till now we understood the basics of the blockchain. Let’s build a small blockchain of our own through our favorite programming language Python.

Implementation in Python

Below is the script say where we represent blocks as objects, print all the blocks of a blockchain and generate a hash for each block.

# To represent a block in python we can create a bigger dictionary and store our data inside this dictionary
# But we know that blocks can be represented as objects, we will create a Block class which we can use further to create new blocks
# Every Block in the blockchain has a timestamp associated with it. In order to  dynamically generate a timestamp, we must import a Python module that returns the current date and time.
# Importing datetime module from datetime library
from datetime import datetime
from hashlib import sha256
class Block:
def __init__(self, transactions, previous_hash, nonce = 0):
self.timestamp =
self.transactions = transactions
self.previous_hash = previous_hash
self.nonce = nonce
self.hash = self.generate_hash()

def print_block(self):
# prints block contents
print("timestamp:", self.timestamp)
print("transactions:", self.transactions)
print("current hash:", self.generate_hash())

def generate_hash(self):
# hash the blocks contents
block_contents = str(self.timestamp) + str(self.transactions) + str(self.previous_hash) + str(self.nonce)
block_hash = sha256(block_contents.encode())
return block_hash

Next, we create a script say,, where create a genesis block, and add, validate and print all blocks of a blockchain.

# Each computer participant has their own copy of the blockchain. Ideally, each copy of the blockchain should have the same properties and functionality to add and validate blocks.
#imports the Block class from
from block import Block
class Blockchain:
def __init__(self):
self.chain = []
self.all_transactions = []
def genesis_block(self):
transactions = {}
genesis_block = Block(transactions, "0")
return self.chain
# prints contents of blockchain
def print_blocks(self):
for i in range(len(self.chain)):
current_block = self.chain[i]
print("Block {} {}".format(i, current_block))

# add block to blockchain `chain`
def add_block(self, transactions):
previous_block_hash = self.chain[len(self.chain)-1].hash
new_block = Block(transactions, previous_block_hash)
# Iterate through the entire blockchain and validate if the previous hash value of current block matches with the hash value inside our previous block
  def validate_chain(self):
for i in range(1, len(self.chain)):
current = self.chain[i]
previous = self.chain[i-1]
if(current.hash != current.generate_hash()):
print("The current hash of the block does not equal the generated hash of the block.")
return False
if(current.previous_hash != previous.generate_hash()):
print("The previous block's hash does not equal the previous hash value stored in the current block.")
return False
return True
def proof_of_work(self,block, difficulty=2):
# Create a loop that increments the nonce value until the hash with the required difficulty has been generated
while proof[:difficulty] != '0'*difficulty:
block.nonce += 1
proof = block.generate_hash()
block.nonce = 0
return proof

Finally, we create a script say, which creates a dummy set of transactions and uses all the functions for adding and validating blocks in a blockchain.

from blockchain import Blockchain
block_one_transactions = {"sender":"Romeo", "receiver": "Juliet", "amount":"50"}
block_two_transactions = {"sender": "Juliet", "receiver":"Mafia", "amount":"25"}
block_three_transactions = {"sender":"Romeo", "receiver":"Mafia", "amount":"35"}
fake_transactions = {"sender": "Juliet", "receiver":"Mafia, Romeo", "amount":"25"}
local_blockchain = Blockchain()
local_blockchain.chain[2].transactions = fake_transactions

Above scripts demonstrate a basic implementation of blockchain discussed at the beginning of this article. Feel free to reach out to me for any queries.

Blockchain Simplified was originally published in Data Driven Investor on Medium, where people are continuing the conversation by highlighting and responding to this story.