diff --git a/README.md b/README.md index 06453b5..dde0c29 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,81 @@ -# bitcoin-mining-simulation +# Bitcoin Mining Simulation -This project is a simple Python simulation of Bitcoin mining. It demonstrates the core concepts of blockchain mining, such as generating transactions, calculating hashes, and solving proof-of-work problems by finding a valid nonce. \ No newline at end of file +## Overview + +This project is a simple Python simulation of Bitcoin mining. It demonstrates the core concepts of blockchain mining, such as generating transactions, calculating hashes, and solving proof-of-work problems by finding a valid nonce. + +## Features + +- Random transaction generation. +- SHA-256 hashing of block data. +- Proof-of-work implementation with adjustable difficulty. +- Simulation of blockchain block creation. + +## Requirements + +- Python 3.6+ + +## Installation + +1. Clone the repository or download the source code: + ```bash + git clone + ``` + +## Usage + +1. Run the script: + ```bash + python main.py + ``` +2. The program will generate random transactions, mine blocks, and display the resulting hashes. + +### Example Output + +``` +Mining Block 1 with Transactions: 12345->67890->50; 54321->09876->30; 11111->22222->20 +Block found! Nonce: 45678, Hash: 00000f9d6c24... +New Hash: 00000f9d6c24... +``` + +## Configuration + +The script contains configurable constants to adjust the mining simulation: + +- `MAX_NONCE`: Limits the number of nonce iterations for mining. Default is `1,000,000`. +- `DIFFICULTY`: Specifies the number of leading zeros required in the hash. Default is `6`. + +To modify these values, edit the script directly: + +```python +MAX_NONCE = 1000000 # Set a higher or lower limit as needed +DIFFICULTY = 6 # Adjust difficulty for faster or slower mining +``` + +## Project Structure + +- `mining_simulation.py`: Main script containing all functionality for the simulation. + +## How It Works + +1. **Transaction Generation**: Random transactions are created with a sender, receiver, and amount of bitcoins. +2. **Hash Calculation**: Each block is hashed using SHA-256. +3. **Proof-of-Work**: The script iterates through possible nonce values to find a hash matching the difficulty. +4. **Blockchain Simulation**: Each block references the previous block's hash, forming a simple blockchain. + +## Limitations + +- This is a simplified simulation and does not represent the full complexity of real-world Bitcoin mining. +- The script is single-threaded and not optimized for performance. + +## License + +This project is licensed under the MIT License. See the LICENSE file for details. + +## Contributions + +Contributions are welcome! Feel free to fork the repository and submit pull requests. + +## Contact + +For questions or suggestions, please reach out to [your email or GitHub profile]. diff --git a/main.py b/main.py new file mode 100644 index 0000000..39a14c8 --- /dev/null +++ b/main.py @@ -0,0 +1,51 @@ +# imports +from hashlib import sha256 +from random import randint + +# Constants +MAX_NONCE = 1000000 # Limit for the simulation +DIFFICULTY = 3 # How many lead zeros has the hash + +def random_with_N_digits(n): + """Generate a random string with n chars.""" + range_start = 10 ** (n - 1) + range_end = (10 ** n) - 1 + return randint(range_start, range_end) + +def generate_transactions(count=3): + """Create the list of transactions""" + transactions = [] + for _ in range(count): + sender = random_with_N_digits(5) + receiver = random_with_N_digits(5) + bitcoins = random_with_N_digits(2) + transactions.append(f"{sender}->{receiver}->{bitcoins}") + return "; ".join(transactions) + +def SHA256(text): + """Creates a SHA256-Hash for the text.""" + return sha256(text.encode("ascii")).hexdigest() + +def mine(block_number, transactions, previous_hash, prefix_zeros): + """Simulates the mining of a block.""" + prefix_str = '0' * prefix_zeros + for nonce in range(MAX_NONCE): + text = f"{block_number}{transactions}{previous_hash}{nonce}" + new_hash = SHA256(text) + if new_hash.startswith(prefix_str): + print(f"Block found! Nonce: {nonce}, Hash: {new_hash}") + return new_hash + raise RuntimeError(f"Mining not succesful after {MAX_NONCE} tries.") + +if __name__ == '__main__': + block_number = 1 + previous_hash = "0" * 64 # Star Value for the first block + num_blocks_to_mine = 10 + + for _ in range(num_blocks_to_mine): + transactions = generate_transactions() + print(f"Mining Block {block_number} with transactions: {transactions}") + new_hash = mine(block_number, transactions, previous_hash, DIFFICULTY) + previous_hash = new_hash # Hash wird zum vorherigen Hash für den nächsten Block + block_number += 1 + print(f"New Hash: {new_hash}\n")