Best Flask open-source libraries and packages

Blockchain

Basic blockchain implementation in Python
Updated 1 year ago

Build Status Maintainability Test Coverage

Summary

This project is work in progress on a fully TDD basic implementation of blockchain in Python.

Technologies

  • Python (Main language)
  • Flask (RESTful API for nodes)
  • Pytest (TDD)
  • Travis (CI)
  • Docker (Container)
  • Serverspec (TDD of docker build)

Design

Proof of work

Proof of work is based on locating a SHA256 hash of the current transactions, previous hash and a proof that begins with a definied sequence. In this case (to ensure non stubbed testing works in a reasonable time while development takes place) the proof is a simple 00. Over time additional digits can be required which increases the difficulty of finding the proof. Once obtained it is trivial to proof the block as confirming the hash once the proof is provided is computationally easy.

Public/Private Key encryption

Each wallet is produced with a public/private key pair. The public key is used as the wallet address whilst the private key should be kept securely. When sending a transaction the sender signs it with their private key and this can be verified with the public key for authenticity.

API

  • / GET - Homepage
  • /blockchain GET - Returns a json response of the entire blockchain
  • /mine POST - Mines a block
  • /wallet POST - Creates new public and private keys
  • /wallet GET - Loads existing public and private keys
  • /balance GET - Gets current balance
  • /peer POST - Adds a remote peer to this nodes peer list
  • /peer/ DELETE - Removes a peer from this nodes peer list
  • /peers GET - Returns the list of this nodes peers
  • /broadcast POST - Allows a transaction to be broadcast to this node from another peer node

Running a single node

To fire up a single node there is a lightweight Dockerfile based on Alpine linux which runs a node. To build clone the repo and run this locally:

docker build -t blockchain_node:latest . 
docker run -d -p 4000:4000 blockchain_node

You can then connect to http://localhost:4000 using postman and the APIs detailed above.

TODOS

  • Detail the APIs in more detail and add documentation of the backend design
  • Create docker-compose to automatically fire up multiple nodes
  • Add functionality to allow nodes to share created blocks with other nodes
  • Add functionality to allow a new node to sync to other nodes
  • Add SPA front end to control nodes locally and eliminate the need to use Postman
  • Add node autodiscovery so other nodes don't need to be added manually
  • Add increasing hash difficulty
  • Add docstrings

Credits