A basic blockchain data-structure that runs on a distributed network of nodes. The chain has immutability since all blocks contain a hash of the previously generated block to the origin node. Written in python using Flask and Requests. Forked from this guide
- Decentralization
- Each node follows a consensus algorithm
- Transactions are broadcasted to all nodes
- Wallet file generation
- Uses PBKDF2 for key generation given a password
- Wallet is encrypted with AES via this key
- Consensus algorithm
- The node with the longest verifiable chain is what the network agrees on
- Chain is verified
- User interface
- Used by entities of the supply chain
- Takes a wallet file, password, and the data to put on the chain
- Digital Signatures
- Provide integrity for transactions on the blockchain
- Data to be added to the chain is signed by the entity's wallet file
| Action | Route | Path | Method |
|---|---|---|---|
| Save chain | /save | save a copy of the current chain locally | GET |
| Mine block | /mine | tells the node to mine. Needs to have pending unconfirmed transactions. | GET |
| View chain | /chain | pulls copy of current chain and displays it in JSON format. | GET |
| View pending tx's | /pending_tx | query unconfirmed transactions | GET |
| Add peer | /register_with | add new peer to the network | POST |
| Add block | /add_block | add a block mined by someone else to the node's chain | POST |
Optional:
> set FLASK_ENV=developmentInstall dependencies
> cd python_blockchain_app
> pip install -r requirements.txtStart a blockchain node server:
For Windows
> set FLASK_APP=node_server.py
> flask run --port 8000For Unix Bash
$ export FLASK_APP=node_server.py
$ flask run --port 8000One instance of our blockchain node is now up and running at port 8000.
Run the application on a different terminal session
$ python run_app.pyThe application should be up and running at http://localhost:5000.
To play around by spinning off multiple custom nodes, use the /register_with endpoint to register a new node.
Sample scenario:
# already running
$ flask run --port 8000 &
# spinning up new nodes
$ flask run --port 8001 &
$ flask run --port 8002 &You can use the following cURL requests to register the nodes at port 8001 and 8002 with the already running 8000.
curl -X POST \
http://127.0.0.1:8001/register_with \
-H 'Content-Type: application/json' \
-d '{"node_address": "http://127.0.0.1:8000"}'curl -X POST \
http://127.0.0.1:8002/register_with \
-H 'Content-Type: application/json' \
-d '{"node_address": "http://127.0.0.1:8000"}'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.
To update the node with which the frontend application syncs (default is localhost port 8000), change CONNECTED_NODE_ADDRESS field in the views.py file.
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.
$ curl -X GET http://localhost:8001/chain
$ curl -X GET http://localhost:8002/chainNode: A computer that operates on the blockchain network which is able to send and receive transactions.
Full node: A client that operates on the network and maintains a full copy of the blockchain. Sends and receives TX as well, updates the blockchain with block entries and confirmations from miners.
Master nodes: Master nodes enable decentralized governance and budgeting. In summary, aside from a full copy of the blockchain, a node also keeps additional data structures, such as the unspent transaction outputs cache or the unconfirmed transactions’ memory pool, so it can quickly validate new received transactions and mined blocks. If the received transaction or block is valid, the master node updates its data structures and relays it to the connected nodes. It is important to note that a master node does not need to trust other nodes because it independently validates all the information it receives from them.
