├── .deepsource.toml
├── .dockerignore
├── .gitignore
├── CONTRIBUTING.md
├── Dockerfile.backend
├── Dockerfile.frontend
├── Makefile
├── README.md
├── app
├── __init__.py
├── __init__.pyc
├── templates
│ ├── base.html
│ └── index.html
├── views.py
└── views.pyc
├── compose.yaml
├── node_server.py
├── requirements.txt
├── run_app.py
└── screenshots
├── 1.png
├── 2.png
└── 3.png
/.deepsource.toml:
--------------------------------------------------------------------------------
1 | version = 1
2 |
3 | test_patterns = [
4 |
5 | ]
6 |
7 | exclude_patterns = [
8 |
9 | ]
10 |
11 | [[analyzers]]
12 | name = 'python'
13 | enabled = true
14 | runtime_version = '3.x.x'
15 |
--------------------------------------------------------------------------------
/.dockerignore:
--------------------------------------------------------------------------------
1 | Dockerfile.backend
2 | Dockerfile.frontend
3 | Makefile
4 | compose.yaml
5 | *.md
6 | screenshots
7 | .gitignore
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 | **/*.pyc
3 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | ## How to contribute to the project?
2 |
3 | All kind of contributions are welcome. If the change you are suggesting is significant, please do create an issue before working on the patch.
4 |
5 | ## Things to keep in mind
6 |
7 | - The project is designed to be beginner friendly. There are some conscious trade-offs that I've made to keep it simple (as compared to a real-world blockchain project like Bitcoin which will involve a lot of intricacies).
8 | - The coding style is analogous to PEP-8. It's not strictly followed, but please do keep existing style in mind while writing code and as many comments as possible (so that it's easier for others to understand).
9 |
10 | Lastly, thanks for considering to contribute to the project.
11 |
--------------------------------------------------------------------------------
/Dockerfile.backend:
--------------------------------------------------------------------------------
1 | FROM python:3.11
2 |
3 | RUN mkdir /app
4 | COPY . /app
5 | WORKDIR /app
6 | RUN python -m pip install -r requirements.txt
7 | ENV FLASK_APP=node_server.py
8 | ENTRYPOINT [ "flask", "run", "--host", "0.0.0.0" ]
9 |
--------------------------------------------------------------------------------
/Dockerfile.frontend:
--------------------------------------------------------------------------------
1 | FROM python:3.11
2 |
3 | RUN mkdir /app
4 | COPY . /app
5 | WORKDIR /app
6 | RUN python -m pip install -r requirements.txt
7 | ENV FLASK_RUN_SERVER_NAME=0.0.0.0
8 | EXPOSE 5000
9 | ENTRYPOINT [ "python", "run_app.py" ]
10 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | .PHONY: build
2 | build: build-frontend build-backend
3 |
4 | .PHONY: build-frontend
5 | build-frontend:
6 | docker build -t python-blockchain-frontend -f Dockerfile.frontend .
7 |
8 | .PHONY: build-backend
9 | build-backend:
10 | docker build -t python-blockchain-backend -f Dockerfile.backend .
11 |
12 | .PHONY: run
13 | run: build-backend build-frontend
14 | docker-compose up
15 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # python_blockchain_app
2 |
3 | A simple tutorial for developing a blockchain application from scratch in Python.
4 |
5 | ## What is blockchain? How it is implemented? And how it works?
6 |
7 | Please read the [step-by-step implementation tutorial](https://gist.github.com/satwikkansal/4a857cad2797b9d199547a752933a715) to get your answers :)
8 |
9 | ## Instructions to run
10 |
11 | Clone the project,
12 |
13 | ```sh
14 | $ git clone https://github.com/satwikkansal/python_blockchain_app.git
15 | ```
16 |
17 | Install the dependencies,
18 |
19 | ```sh
20 | $ cd python_blockchain_app
21 | $ pip install -r requirements.txt
22 | ```
23 |
24 | Start a blockchain node server,
25 |
26 | ```sh
27 | $ export FLASK_APP=node_server.py
28 | $ flask run --port 8000
29 | ```
30 |
31 | ### For windows users
32 | ```
33 | set LANG=C.UTF-8
34 | set FLASK_APP=node_server.py
35 | flask run --port 8000
36 | ```
37 | One instance of our blockchain node is now up and running at port 8000.
38 |
39 |
40 | Run the application on a different terminal session,
41 |
42 | ```sh
43 | $ python run_app.py
44 | ```
45 |
46 | ### For windows users
47 | ```
48 | set LANG=C.UTF-8
49 | set FLASK_APP=run_app.py
50 | flask run --port 8000
51 | ```
52 |
53 | The application should be up and running at [http://localhost:5000](http://localhost:5000).
54 |
55 | Here are a few screenshots
56 |
57 | 1. Posting some content
58 |
59 | 
60 |
61 | 2. Requesting the node to mine
62 |
63 | 
64 |
65 | 3. Resyncing with the chain for updated data
66 |
67 | 
68 |
69 | To play around by spinning off multiple custom nodes, use the `register_with/` endpoint to register a new node.
70 |
71 | Here's a sample scenario that you might wanna try,
72 |
73 | ```sh
74 | # Make sure you set the FLASK_APP environment variable to node_server.py before running these nodes
75 | # already running
76 | $ flask run --port 8000 &
77 | # spinning up new nodes
78 | $ flask run --port 8001 &
79 | $ flask run --port 8002 &
80 | ```
81 |
82 | You can use the following cURL requests to register the nodes at port `8001` and `8002` with the already running `8000`.
83 |
84 | ```sh
85 | curl -X POST \
86 | http://127.0.0.1:8001/register_with \
87 | -H 'Content-Type: application/json' \
88 | -d '{"node_address": "http://127.0.0.1:8000"}'
89 | ```
90 |
91 | ```sh
92 | curl -X POST \
93 | http://127.0.0.1:8002/register_with \
94 | -H 'Content-Type: application/json' \
95 | -d '{"node_address": "http://127.0.0.1:8000"}'
96 | ```
97 |
98 | This will make the node at port 8000 aware of the nodes at port 8001 and 8002, and make the newer nodes sync the chain with the node 8000, so that they are able to actively participate in the mining process post registration.
99 |
100 | To update the node with which the frontend application syncs (default is localhost port 8000), change `CONNECTED_NODE_ADDRESS` field in the [views.py](/app/views.py) file.
101 |
102 | Once you do all this, you can run the application, create transactions (post messages via the web inteface), and once you mine the transactions, all the nodes in the network will update the chain. The chain of the nodes can also be inspected by inovking `/chain` endpoint using cURL.
103 |
104 | ```sh
105 | $ curl -X GET http://localhost:8001/chain
106 | $ curl -X GET http://localhost:8002/chain
107 | ```
108 |
--------------------------------------------------------------------------------
/app/__init__.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 |
3 | app = Flask(__name__)
4 |
5 | from app import views
--------------------------------------------------------------------------------
/app/__init__.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/satwikkansal/python_blockchain_app/ac0d1be9cc754869e84b2e3f4f77e6bdeeed93fb/app/__init__.pyc
--------------------------------------------------------------------------------
/app/templates/base.html:
--------------------------------------------------------------------------------
1 |
2 |