Create a Blockchain from scratch using Python

Create a Blockchain from scratch using Python

Blockchain Technology has taken the world by storm – since being introduced in 2009 – 2010. In this article we are going to cover how one would go about creating a peer to peer, proof of work blockchain from scratch using Python programming language.

create blockchain from scratch python
create blockchain from scratch python

Blockchain Origins

We were first introduced in to the concept of Blockchains by Satoshi Nakamoto in 2009 when he released his white paper on Bitcoin – A Peer to Peer Electronic Cash System.

Since then, the concept of a peer-to-peer electronic cash system has evolved and the concept has been replicated to create multiple variations of the blockchain.

Blockchain a Database

In put tutorial we focus on Blockchain as a database or sorts. A store of data and information – only, it is distributed across multiple nodes and peers. Technically a blockchain can be used to store various types of data and information – in the case of Bitcoin, it records the movement of the digital currency and maintnains a ledger of the currency distribution.

In our Tutorial – we will be building a peer to peer system that will maintain simple data – string variables. The aim is to demonstrate various technical terms and concepts that build up blockchain technology.

create python blockchain
create python blockchain

Development Environment – Create a Blockchain from scratch using python

We selected a virtual machine running on Ubuntu 20.04 operating system for this project. You will need more than one – to test the peer-to-peer functionality.

Python Development

Python will be the language of choice, we will be developing in Python3. The entire set-up process is captured on the video tutorial attached below.

Pre-requisites before starting:

  • Setting up the Python 3 virtual working environmen
  • Installing Git on both the personal machine and Virtual Private Server, so we can write out code on our personal machine, but run it in the server.
  • A good code editor like Atom.

Blockchain Python Classes

In the Tutorial – there are three main classes that we will be developing: (1) Blockchain Class, (2) Account Class and (3) Peer to Peer Class.

Blockchain Class

In the blockchain class – all the blockchain related functions will be written here. These are functions dealing with the direct operation and integrity of the blockchain.

In this class we will have the following functions:

  • Function to create the genesis block of the blockchain
  • Mining a new block, removing transactions from the pool and adding a new block to the chain
  • Function to add a transaction to the transaction pool of out blockchain
  • Check for valid transactions – comparing signatures with the data

Example function

blockchain class
blockchain class

Account class

In this class of the program, we will be writing functions that deal with the blockchain account. These are functions related to creating a key-pair and transaction objects.

The functions included in the class are:

  • Generate key-pair – public and private signing keys and exporting them from the program for future access
  • Create a private key
  • Generate a public key from private key
  • Creating a transaction object

Example transaction:

blockchain account class
blockchain account class

Peer to Peer Class

This is the last class in our program and deals specifically with functions that will allow for the Peer to Peer server.

In typical server arrangements – there is always a client and a server. The server send out all the information and the client receives and processes. In our case – we require all the participating servers to be peers. They will be broadcasting data but also receiving data.

Functions included in the class are:

  • Adding a new peer to our peer database
  • Binding ports, making them available for transaction and chain broadcast
  • Subscribing to a node to receive transaction and chain broadcasts

Example Transaction

blockchain peer to peer class
blockchain peer to peer class

Full Tutorial

Blockchain development is more complex that what can be covered in an article, the full Tutorial is available on Skolo Online

Leave a Reply

Your email address will not be published.

Subscribe to our mailing list

And get this 💃💃 Free😀 eBook !

Holler Box