/fastapi-blockchain-template

Blockchain Network Implementation with FastAPI and Python

Primary LanguagePythonMIT LicenseMIT

fastapi-blockchain-template

Cloud-native Blockchain Network Implementation in Python with FastAPI, Docker, Kubernetes and Okteto Cloud.

Dependencies and Tools

pip packages:

  • fastapi
  • httpx
  • gunicorn
  • uvicorn
  • pif
  • pydantic
  • python-dotenv

dev:

  • Okteto
  • Docker

deployment:

  • Heroku
  • Kubernetes

Documentation

Development

Local:

$ git clone https://github.com/codeamt/fastapi-blockchain-template.git
$ cd fastapi-blockchain-template
$ sh run.sh 

Docker (Build):

$ docker build -f Dockerfile -t app:latest ./code

Docker (Run):
Add a .env file to your root and:

$ docker run -p 5000:5000 --env-file=env_file_name app:latest

Okteto:

$ okteto login 
$ okteto namespace
$ okteto init --deploy

Production

Heroku:

Install Heroku CLI tools, then from the root:

$ git init
$ touch .gitignore 
$ echo ".env" >> .gitignore
$ git add . 
$ git commit -m "deploying to heroku"
$ heroku login
$ heroku create <optional: app_name>
$ heroku config:set PORT=<desired_port>
$ heroku config:set BLOCKCHAIN_URL=http://127.0.0.1:{PORT}
$ heroku config:set INITIAL_DIFFICULTY=2
$ git push heroku main
$ heroku open 

Kubernetes Cluster:

$ kubectl apply -f k8s.yml

Endpoints

Blockchain Server:

Endpoint Description
/ Index endpoint for Blockchain server.
:return: str
/mainchain Ledger endpoint for Blockchain server.
:return: dict -> keys: peers: List[str]
/peers Peers List endpoint for Blockchain Server.
:return: dict -> keys: chain:Lis[dict] -> Serialized Blocks
/new-peer Endpoint for creating new peer.
:param request: Request
:return: dict -> keys: new_peer:str -> IP Address
/new_tx New Transaction endpoint for Blockchain Server.
:param request: Request -> HTTP POST request.
:return: dict -> keys: sender:str; receipt:dict
/mempool Mempool endpoint for Blockchain Server.
:return: dict -> keys: mempool:dict
/new_block ew Block endpoint for Blockchain Server.
:param request: Request -> HTTP POST request.
:return: dict -> keys: miner_address:str, latest_block:dict, new_chain:List[dict]

Bootstrap Node Application:

Endpoint Description
/node Index endpoint for Bootstrap Node Client.
:return: dict: keys: local_chain: List[dict] -> Local chain.
/chain Consensus Endpoint for BootStrap Node App.
:return: dict: keys: local_chain: List[dict]
/peer-nodes Network Peers List Endpoint for Bootstrap Node App.
:return: dict: List[str]
/mine Mining Endpoint for Bootstrap Node App.
:return: dict: block:dict -> New Block (serialized)
/transact New Transaction Endpoint for Bootstrap Node App.
:return: dict: keys: sender:str; receipt:dict
/mem_pool Mempool Endpoint for Bootstrap Node App.
:return: dict: keys: mempool:dict -> Mempool State.

TODO:

  • Configure Postgres DB
  • Reactive Frontend Application for Bootstrap Node
  • Unit Tests