This project demonstrates a very simple blockchain implementation. The blockchain is represented using JSON files, and it includes basic functionalities such as adding transactions, checking balances, and mining blocks. I made this project to help me better understand how blockchains work.
-
Ledger Creation Script (
ledgers.py)- Continuously creates new ledgers every minute.
- Marks the latest ledger with
"islatest": true. - Saves the ledgers to a JSON file (
ledgers.json).
-
Transaction Script (
transactions.py)- Allows users to add transactions to the latest ledger.
- Provides functionality to check the balance of a user by summing the amounts received and sent.
- Updates the JSON file (
ledgers.json) with new transactions.
-
Mining Script (
mining.py)- Continuously checks for ledgers that are not hashed and not marked as the latest.
- Mines these ledgers by finding a
stampthat generates a valid hash with a certain difficulty (e.g., leading zeros). - Updates the JSON file (
ledgers.json) with thestampandhashvalues.
-
Blockchain Explorer (
data_grabber.py,templates/index.html)- Provides a web interface to view the blockchain data.
- Displays blocks, transactions, and user balances.
- Uses Flask and Socket.IO to serve the web interface and update data in real-time.
The ledgers are stored in a JSON file (ledgers.json) with the following structure:
{
"ledger1": [
{
"date": "2024-07-24 12:00:00",
"hash": "",
"stamp": "",
"islatest": true
},
...
],
...
}The mining algorithm works as follows:
- Combine the content of the ledger, the hash of the previous ledger, and a
stamp. - Hash the combined string using MD5.
- Check if the hash meets the difficulty requirement (e.g., starts with 5 zeros).
- If the hash is valid, store the
stampandhashin the JSON file.
To run the ledger creation script:
python ledgers.pyThis script will continuously create new ledgers every minute and save them to ledgers.json.
To run the transaction script:
python transactions.pyThis script allows users to check their balance or make transactions.
To run the mining script:
python mining.pyThis script continuously mines new ledgers that are ready to be mined and updates the ledgers.json file with the results.
To run the blockchain explorer:
python data_grabber.pyThis script starts a Flask web server and serves the blockchain explorer interface at http://localhost:5000.
This simple blockchain project provides a basic implementation of a blockchain using Python and JSON files. It covers the fundamental aspects of blockchain technology, including ledger creation, transaction handling, and mining. This project is a great starting point for anyone looking to learn about blockchains and their underlying principles.
- Added a blockchain explorer with a web interface using Flask and Socket.IO.
- Displays blocks, transactions, and user balances in real-time.
- Added
data_grabber.pyandtemplates/index.htmlfor the web interface.