Learn how Blockchain networks work by building a simple one in Python. Blockchain is an immutable, sequential chain of records called blocks. Blocks can hold transactions, documents, files or any data you like, really, but the important thing is that blocks are chained together using hashes. The blockchain networks consists of interconnected nodes which update the blocks together using a consensus algorithm (like proof-of-work).
Original source: hackernoon
- Python 3.6+
- Python IDE
- HTTP client (Postman or cURL)
- Flask and Requests Library for Python
Go to Python and download “Python 3.6” (the x86 “executable installer” version). Install it.
Install the “Flask” library using the command:
pip3 install Flask==0.12.2 requests==2.18.4
Go to Postman, download and install it
Once the application is fully set-up, you can grab a different machine if you like and spin up different nodes on your network. Or spin up processes using different ports on the same machine.
- It is easier to spin up another node on your machine, on a different port, and register it with your current node (using -p or --port switches and then the port number).
- Thus, you will have two nodes: http://localhost:5000 and http://localhost:5001.
- Now open command shell and go to your project file directory.
- Start your first blockchain node by running the command:
python3 blockchain.py
If everything is OK you will see the following result:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
- Now open Postman.
- Mine a block by making a GET request to http://localhost:5000/mine.
- Create a new transaction by making a POST request to http://localhost:5000/transactions/new with a body containing our transaction JSON structure.
- Inspect the full chain by requesting http://localhost:5000/chain.
- Register a new node by making a POST request to http://localhost:5000/nodes/register. Do not forget to run another node with port 5001 on your machine.
- Finally, resolve chain-length conflicts by making a GET request to http://localhost:5000/nodes/resolve, thus determing the authoritative chain.
MI1: Module 2: E1