This project demonstrates a basic blockchain implementation using Python and Flask. The blockchain supports mining new blocks, adding transactions, and viewing the blockchain.
- Mine Blocks: Generate new blocks by solving a proof of work algorithm.
- Add Transactions: Create new transactions that will be added to the next mined block.
- View Blockchain: Retrieve the full blockchain and pending transactions.
- Register Nodes: Add new nodes to the network.
- Python 3.11 or higher
- Flask
-
Clone the repository:
git clone https://github.com/javeria2108/Blockchain-project-in-python.git cd Blockchain-project-in-python -
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install dependencies:
pip install Flask
-
Run the Flask application:
python app.py
-
API Endpoints:
-
Mine Block
- URL:
/mine_block - Method:
GET - Description: Mines a new block and adds it to the blockchain.
- Response: Details of the mined block.
{ "message": "A block is MINED", "index": 2, "timestamp": 1625074762.851, "proof": 35293, "previous_hash": "abc123...", "transactions": [{"sender": "sender", "recipient": "node_identifier", "amount": "10BTC"}] } - URL:
-
Create Transaction
- URL:
/transactions/new - Method:
POST - Description: Adds a new transaction to the list of pending transactions.
- Body: JSON containing
sender,recipient, andamount. - Response: Confirmation message.
{ "message": "Transaction will be added to next Block" } - URL:
-
View Blockchain
- URL:
/get_chain - Method:
GET - Description: Retrieves the current blockchain and pending transactions.
- Response: The full blockchain and pending transactions.
{ "chain": [...], "pending_transactions": [...], "length": 1 } - URL:
-
Register Nodes
- URL:
/nodes/register - Method:
POST - Description: Registers new nodes to the blockchain network.
- Body: JSON containing
nodes(list of node addresses). - Response: Confirmation message.
{ "message": "New nodes have been added", "total_nodes": [...] } - URL:
-
__init__: Initializes the blockchain with the genesis block.new_block: Creates a new block and adds it to the chain.last_block: Returns the last block in the chain.new_transaction: Adds a new transaction to the list of pending transactions.hash: Hashes a block.proof: Proof of work algorithm.
/mine_block: Mines a new block./transactions/new: Creates a new transaction./get_chain: Returns the blockchain./nodes/register: Registers new nodes.