This project is a simplified blockchain simulation written in Python. It demonstrates the core concepts of blockchain technology, including creating blocks, managing transactions with digital signatures, and implementing a basic Proof of Stake (PoS) consensus mechanism. The project is intended for educational purposes, illustrating how blockchain technology works under the hood.
- Block Creation: Generates new blocks and adds them to the blockchain, ensuring integrity through hashing.
- Transaction Management: Handles transactions, including the creation, signing with digital signatures, and verification.
- Proof of Stake (PoS) Consensus: Simulates a basic PoS mechanism for miner selection based on stake.
- Multi-threading: Demonstrates concurrent blockchain operations using Python's threading module.
- Python 3.7 or higher
cryptography
library
- Clone the Repository
git clone https://github.com/mikehsin/blockchain-simulation.git
- Navigate to the Project Directory
cd blockchain-simulation
- Install Dependencies
Make sure you have Python installed on your system. Then, install the required Python packages:
pip install cryptography
Execute the main.py
script to start the blockchain simulation:
python main.py
This will initiate two concurrent blockchain processes, showcasing the creation and validation of blocks and transactions.
crypto_utils.py
: Contains cryptographic utilities for key generation, signing, and verifying signatures.AdvancedBlockchain.py
: Implements the blockchain logic, including block creation, transaction handling, and the PoS consensus mechanism.main.py
: The entry point of the simulation, demonstrating concurrent blockchain operations.
- Key Generation: Public-private key pairs are generated for transaction signing and verification.
- Creating Transactions: Transactions are created, signed by the sender, and verified using digital signatures.
- Adding Blocks: Valid transactions are grouped into blocks, which are then added to the blockchain following the PoS consensus rules.
- Validation: The integrity of the blockchain is maintained through cryptographic hashes and signature verification.
This project also demonstrates the use of multi-programming to simulate concurrent blockchain operations. By leveraging Python's threading
module, the simulation can run multiple blockchain processes in parallel, showcasing how a real-world blockchain system might handle simultaneous transactions and block creation across different nodes.
To see multi-programming in action, refer to the main.py
script, where multiple threads are initiated to run different blockchain instances concurrently. This approach highlights the blockchain's ability to operate in a distributed and parallelized environment, which is fundamental to its scalability and efficiency.
Below is an example of how to use the sign_transaction
function within this project to sign transaction data with a private key and then verify the signature with the corresponding public key:
from crypto_utils import generate_keys, sign_transaction, verify_signature
# Generate public and private keys
private_key, public_key = generate_keys()
# Serialize the transaction data
transaction_data = "Sender: Alice, Receiver: Bob, Amount: 100"
# Sign the transaction data using the private key
signature = sign_transaction(private_key, transaction_data)
# Verify the signature using the public key
is_signature_valid = verify_signature(public_key, transaction_data, signature)
print(f"Signature Valid: {is_signature_valid}")
We welcome contributions to this project! Please feel free to fork the repository, make your changes, and submit a pull request.
This project is open-source and available under the MIT License.