Motivation

This project is a POC (Proof of concept) of a public 1.0 blockchain, covering the requirements at the ‘white paper’ of bitcoin. We intended to understand more about how blockchains worked and how we could implement one using Python with Flask micro web framework and Docker.

QuickStart

The project can be used in two ways either by using Docker or setting it up locally, we highly recommend using Docker as it removes complexity.

Building the Blockchain

Before we run anything we need to download the project:

>> git clone https://github.com/tvukasovic/blockchain

Once the repository has been cloned we can either build the docker image or run it locally

Docker

Docker install only needs one command:

>> cd /path/to/project/blockchain
>> docker-compose build -f docker/Dockerfile blockchain

Thats it, we just have to wait for the image to finish building. The advantage on this approach is that we are not installing anything on our local machines we build everything inside the container, once we are done we can delete it without having any side effects or having packages that we won’t use in the future.

Locally

To make the local installation we will need to run the following commands:

>> cd /path/to/project/blockchain/docker
>> pip install requirements.txt

Once PIP is done installing all the python packages, we will have to install textract. Following THIS tutorial.

Ater textract installation its done we are ready to run the blockchain.

Running the Blockchain

Docker

If you built the docker image, we just have to run it and thats that by doing:

>> docker-compose -f docker/docker-compose.yml up

And thats it. The blockchain is up and running at the port 5000.

You can try it by going to the browser or to Postman and making a GET request to:

localhost:5000/chain

You may also try to start a bash session inside the docker container to check the files by running the command:

>> docker-compose -f docker/docker-compose.yml run blockchain bash

Locally

Running the project locally demands a few extra steps:

>> cd /path/to/project/blockchain/src
>> export FLASK_APP=app.py
>> flask run

And thats it!

Congratulations you have build and run the blockchain POC, now its time to use it!

Running the blockchain!

Now is where the fun begins! This example follows the local instalation result. If you are using the docker instalation the only difference is that the PDF files uploaded to the blockchain should be placed inside the files folder of the project. And at the body of the request at the new_transaction endpoint you will write something like:

"file": "files/<name-of-file>.pdf"

Lets start by obtaining a fresh chain. Go to postman and run this endpoint:

localhost:5000/chain

You should see something like this:

_images/get_chain1.png

Now its time to add a new transaction, head to postman again and we go to the endopint and post a transaction as follows:

localhost:5000/new_trans
_images/new_trans.png

Lets check now that the transaction has been posted and its pending to be processed:

localhost:5000/pending_tx
_images/pending_tx.png

Now that there is a transaction pending, lets mine it so it is appended into a new block that will be part of the blockchain:

localhost:5000/mine
_images/mine.png

Finally lets see the results! Head to the first endpoint and see the result. A new block has been added and it has our transaction!:

localhost:5000/chain
_images/get_chain2.png

Integration Test for files

We are going to make a small test, when we add a file we obtain a hash of its content but if we change the integrity of the files content we should obtain a different hasn. Lets make the test!

  1. Lets add our original file and check the hash of the content! The hash we obtained for the file is:

    1dd2496fa30d062689e5f9589069e487e1e1bc25243ac0c40473022178fd2d1b
    

Adding the transaction

_images/new_trans.png

Checking the transaction

_images/pending_tx.png
  1. Now lets modify the file by removing some pages and lets upload it as a new transaction, the hash we now obtained is:

    684bd9509d71f417a914db7a4514d247a8fa347f79e657122914e6562361a7ee
    

Adding the transaction

_images/new_trans_verify.png

Checking the transaction

_images/pending_trans_verify.png

Thats it! Our hashes are different! Therefor we maintain the integrity and with the timestamp of our original transaction and the one we received later one we can check that the content of our files are different and the integrity has been lost at our file.

Indices and tables