Simple blockchain implementation in python
├── backend
│ ├── blockchain.py
│ ├── block.py
│ ├── node.py
│ ├── properties.py
│ ├── rest.py
│ ├── transaction.py
│ └── wallet.py
├── client
│ ├── client.py
│ └── prompt.py
├── README.md
└── requirements.txt
blockchain.py:Specifies the Blockchain class that holds the blocks and the transactionsblock.py:Specifies the Block class that holds transaction info along with its hash, the hash of the previous block and the nonce used for the proof of worknode.py:Holds the nodes id along with info of every other node. Most functions such as mining, signing or broadcasting are performed through the Node classproperties.py:Holds the proof of work difficulty, block transaction capacity and coordinator info constantsrest.py:Specifies a REST API running with Flask and defines the endpoints where the nodes will exchange datatransaction.py:Specifies the Transaction class that holds the tranasaction details like the sender address, the recipient address, the amount of the transaction, the hash of the transaction, its inputs and its outputs and the Transaction Output class that holds information about the output of a transactionwallet.py:Specifies a Wallet class that holds the private and public key of the wallet using the RSA algorithm to generate those keys
client.py:This class is responsible for handling user requests and initializing the blockchain after the Flask server has started by sending an init request to the local serverprompt.py:This class provides a CLI supporting a few user operations specifically:t <recipient_address> <amount>:Sends amount Noobcash Coins(NBC) to the recipient addresstransactions <transactions_folder>:Issues the transactions inside the specified folder. It searches for files of the format transactions<local_id>.txtview:Prints out the details of all the transactions in the last validated block in the blockchainviewall:Prints out the details of all the transactions in the blockchainbalance:Prints out the wallet's current balancehelp:Explains what each command does if given an argument or provides a list of available commands when no argument is suppliedstats:Prints if mining has finished, the number of pending transactions, throughput and average time a block needs to be added to the chainEOF/quit:Exits the client CLI
requirements.txt: Python package requirements
Optional: Change the values in the properties file to specify the difficulty of the proof of work, the capacity of each block and the IP/port that the coordinator will use
- Set up a virtual environment:
python3 -m venv .venv - Activate the virtual environment:
source .venv/bin/activate - Install the requirements:
pip3 install -r requirements.txt - Run the flask server on each node:
python3 backend/rest.py -i <IP_that_will_host_the_flask_server> -p <local_flask_port> - Run
client.pyon one node as a coordinator:python3 client/client.py -i <IP_that_hosts_the_flask_server> -p <local_flask_port> -t coordinator -m <number_of_members_in_the_network>(The IP and port need to be the same as the ones in the properties file so that others know how to connect to the network) - Run
client.pyon all other nodes as members:python3 client/client.py -i <IP_that_hosts_the_flask_server> -p <local_flask_port> - Issue commands on the client CLI