Ok, currently the BTC is 68 491,28 USD and instead of feeling bad for not buying it, you can try to simulate it the mining process with Python just for fun. It is an interesting way to have fun, but hey, who am I to judge you 🙂
So, we need only 3 classes that work with each other – Block, Miner and Blockchain.
1 2 |
import hashlib import time |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Block: def __init__(self, index, previous_hash, timestamp, data, hash_value): self.index = index # Position of the block in the chain self.previous_hash = previous_hash # Hash of the previous block self.timestamp = timestamp # Time when the block was created self.data = data # Data stored in the block self.hash = hash_value # Hash of the current block def __repr__(self): return (f"Block(Index: {self.index}, " f"Previous Hash: {self.previous_hash}, " f"Hash: {self.hash}, " f"Timestamp: {self.timestamp}, " f"Data: {self.data})") |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Miner: def __init__(self, id): self.id = id self.difficulty = 3 def mine_block(self, blockchain, data): index = len(blockchain.chain) + 1 previous_hash = blockchain.chain[-1].hash if blockchain.chain else '0' timestamp = time.time() nonce = 0 hash_value = self.calculate_hash(index, previous_hash, timestamp, data, nonce) # Keep incrementing the nonce until a valid hash is found while hash_value[:self.difficulty] != "0" * self.difficulty: nonce += 1 hash_value = self.calculate_hash(index, previous_hash, timestamp, data, nonce) new_block = Block(index, previous_hash, timestamp, data, hash_value) blockchain.add_block(new_block) print(f"Miner {self.id} mined block {index} with hash: {hash_value} (Previous Hash: {previous_hash}, Nonce: {nonce})") def calculate_hash(self, index, previous_hash, timestamp, data, nonce): value = str(index) + previous_hash + str(timestamp) + data + str(nonce) return hashlib.sha1(value.encode()).hexdigest()[:8] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Blockchain: def __init__(self): self.chain = [self.create_genesis_block()] def create_genesis_block(self): return Block(0, "0", time.time(), "Genesis Block", "genesis_hash") def add_block(self, block): """Add a mined block to the chain.""" self.chain.append(block) def print_chain(self): for block in self.chain: print(block) |
These 3 classes are related with the simulate_mining function, that produces some nice output at the end:
1 2 3 4 5 6 7 8 9 |
simulate_mining(): blockchain = Blockchain() miners = [Miner(i) for i in range(10, 13)] for miner in miners: miner.mine_block(blockchain, "Transaction data") # Mine a block print("\nBlockchain ledger:") blockchain.print_chain() |
The output looks like this:
1 2 3 4 5 6 7 8 9 |
Miner 10 mined block 2 with hash: 00058061 (Previous Hash: genesis_hash, Nonce: 1100) Miner 11 mined block 3 with hash: 000578ef (Previous Hash: 00058061, Nonce: 6546) Miner 12 mined block 4 with hash: 0000944f (Previous Hash: 000578ef, Nonce: 4402) Blockchain ledger: Block(Index: 0, Previous Hash: 0, Hash: genesis_hash, Timestamp: 1729454107.3189948, Data: Genesis Block) Block(Index: 2, Previous Hash: genesis_hash, Hash: 00058061, Timestamp: 1729454107.3189948, Data: Transaction data) Block(Index: 3, Previous Hash: 00058061, Hash: 000578ef, Timestamp: 1729454107.3239613, Data: Transaction data) Block(Index: 4, Previous Hash: 000578ef, Hash: 0000944f, Timestamp: 1729454107.3519597, Data: Transaction data) |
And actually the whole code is explained in the YouTube video:
Enjoy it! 🙂