This project is a simple implementation of a blockchain in Python. It demonstrates the basic concepts of blockchain technology, including blocks, hashing, and chain integrity.
- Creation of a blockchain with a genesis block
- Adding new blocks to the chain
- Calculation of SHA-256 hashes for each block
- Validation of the blockchain's integrity
- Simple demonstration of chain tampering detection
- Python 3.6+
No additional libraries are required as this project only uses Python's standard libraries (hashlib
and time
).
- Clone this repository:
git clone https://github.com/simonedimeglio/easy-blockchain.git
- Navigate to the project directory:
cd easy-blockchain
Run the script using Python:
python simple_blockchain.py
This will create a blockchain, add some sample blocks, print the blockchain, and demonstrate the validation process.
The Block
class represents a single block in the blockchain. Each block contains:
index
: A unique identifier for the block's position in the chainprevious_hash
: The hash of the previous block, creating the chain linkdata
: The data stored in the block (e.g., transaction details)timestamp
: The time when the block was createdhash
: A SHA-256 hash of the block's contents
The Blockchain
class manages the entire chain of blocks. It includes methods to:
- Create the genesis block (the first block in the chain)
- Add new blocks to the chain
- Validate the integrity of the entire chain
- Hashing: Each block's hash is created from its contents, ensuring data integrity.
- Chaining: Each block references the previous block's hash, creating a tamper-evident chain.
- Validation: The
is_chain_valid()
method checks the integrity of the entire blockchain.
This simple implementation can be extended in several ways:
- Implement a proof-of-work system
- Add digital signatures for transactions
- Create a decentralized network of nodes
- Implement a consensus mechanism
This project is open source and available under the MIT License.
Contributions, issues, and feature requests are welcome!
Simone Di Meglio
- GitHub: @simonedimeglio