Blockchain is a decentralized, time-stamped series of immutable records that store data of any size. Controlled by a global network of computers—rather than a single entity—each block is secured and linked via cryptographic hashing, preventing unauthorized tampering.
Building a Blockchain with Python
Here’s how to create a blockchain in Python, mine new blocks, and display the entire chain:
Key Components
Data Storage
- Data is stored in JSON format for readability and ease of implementation.
- Each block contains multiple data points, differentiated by unique fingerprints (hashes).
Hashing Algorithm
- The SHA256 algorithm generates fingerprints (hashes) for each block.
Every block stores:
- Its own hash.
- The hash of the previous block (ensuring tamper-proof chaining).
Blockchain Structure
- Blocks are "chained" by referencing the previous block’s hash.
Example block structure:
{ "index": 2, "previous_hash": "734dd03fe5...", "proof": 632238, "timestamp": "2020-06-01 22:47:59.309000" }
Mining New Blocks
- Mining involves solving a proof-of-work puzzle (computationally difficult problem).
- Successfully mined blocks are added to the chain after validation.
Chain Validation
- Regular checks ensure no tampering occurs (e.g., validating hashes and proof-of-work).
Web App Deployment
- Use Flask to deploy the blockchain locally or publicly.
Example Outputs
Mined Block:
{ "index": 2, "message": "A block is MINED", "previous_hash": "734dd03fe5...", "proof": 632238, "timestamp": "2020-06-01 22:47:59.309000" }Full Chain:
{ "chain": [ {"index": 1, "previous_hash": "0", "proof": 1, "timestamp": "2020-06-01 22:47:05.915000"}, {"index": 2, "previous_hash": "734dd03fe5...", "proof": 632238, "timestamp": "2020-06-01 22:47:59.309000"} ], "length": 2 }Validation:
{"message": "The Blockchain is valid."}
Step-by-Step Implementation
Import Libraries:
- Use
hashlibfor cryptographic hashing andFlaskfor the web interface.
- Use
Blockchain Class:
- Initialize the chain and genesis block (first block).
Mining:
- Solve proof-of-work to add new blocks.
Example mining function:
def mine_block(self): previous_block = self.chain[-1] previous_proof = previous_block['proof'] proof = self.proof_of_work(previous_proof) previous_hash = self.hash(previous_block) block = self.create_block(proof, previous_hash) return block
Proof of Work:
- Adjust difficulty to control mining speed (e.g., find a hash starting with
0000).
- Adjust difficulty to control mining speed (e.g., find a hash starting with
Validation:
- Check block hashes and proof-of-work consistency.
Web App (Flask):
Endpoints to:
- Mine blocks (
/mine_block). - Display the full chain (
/get_chain). - Validate the chain (
/valid).
- Mine blocks (
👉 Learn more about blockchain technology
FAQs
Q1: Why use SHA256 for hashing?
A1: SHA256 is cryptographically secure, ensuring block integrity and tamper resistance.
Q2: What’s the role of proof-of-work?
A2: It prevents spam and ensures blocks are added at a controlled rate.
Q3: Can I deploy this on a live network?
A3: Yes! Use Flask’s public deployment options (e.g., ngrok or cloud hosting).
Final Notes
- Expand the chain by increasing proof-of-work difficulty.
- Integrate smart contracts for advanced use cases.