Building a blockchain

In this tutorial, I am going to show you how to create your own basic blockchain.

If you haven't already checked out our lesson on blockchain basics, I recommend you do that before this tutorial

This tutorial is aimed at people with at least some knowledge in Python.

Things we need:

You will need to have Python 3.6 or higher along with pip installed.
To install Python click here

We are going to create this blockchain in 3 steps.

Building a blueprint for the blockchain

Adding a new transaction to the blockchain

Creating new blocks

Let's get building!

Step 1 - Building a blueprint for the blockchain

Open your favorite text editor or IDE, I personally use Pycharm but any editor will do.

Create a new Python file and save it as blockchain.py

Below we will create our blockchain blueprint.

The first step is to create a blockchain class, this class will be an empty list in which we will store our blockchain in.

classBlockchain(object):def__init__(self):self.chain=[]self.current_transactions=[]defnew_transaction(self):# Adds a new transaction to the list of transactionspassdefnew_block(self):# Creates a new Block and adds it to the chainpassdefproof_of_work():# Simple Proof of Work Algorithmpass@propertydeflast_block(self):returnself.chain[-1]@staticmethoddefhash(block):# creates a SHA-256 hash of a blockpass

As you see above most of the defs have a pass in them, Everything we do in this Tutorial will be based on this blueprint,
so as we move through the tutorial you will just have to take the new code and replace the PASS section within the corresponding def.

Step 2 - Adding a new transaction to the blockchain

We need a way of adding transactions to a Block. Our new_transaction() method is responsible for this:

defnew_transaction(self,sender:str,recipient:str,amount:int)->int:"""
Creates a new transaction to go into the next mined Block
:param sender: Address of the Sender
:param recipient: Address of the Recipient
:param amount: Amount
:return: The index of the Block that will hold this transaction
"""self.current_transactions.append({'sender':sender,'recipient':recipient,'amount':amount,})returnself.last_block['index']+1

After new_transaction() adds a transaction to the list,
it returns the index of the block which then sends the new transaction to the block, and will be the next one to be mined.
This will be useful later on, to the user submitting the transaction.

Step 3 - Creating new blocks

When our Blockchain is instantiated we’ll need to seed it with a genesis block with no predecessors.

A genesis block is the first block of a block chain

We’ll also need to add a “proof” to our genesis block which is the result of mining (or proof of work). We’ll talk more about mining and proof
of work shortly.

Below we will create the new genesis block:

defnew_block(self,proof:int,previous_hash:Optional[str])->Dict[str,Any]:"""
Create a new Block in the Blockchain
:param proof: The proof given by the Proof of Work algorithm
:param previous_hash: Hash of previous Block
:return: New Block
"""block={'index':len(self.chain)+1,'timestamp':time(),'transactions':self.current_transactions,'proof':proof,'previous_hash':previous_hashorself.hash(self.chain[-1]),}# Reset the current list of transactionsself.current_transactions=[]self.chain.append(block)returnblock