From 6769934a6b4a50f80ea397ddb69ec2520b2e3b49 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Tue, 18 Jun 2024 20:02:53 +0700 Subject: [PATCH] Create pow.py --- blockchain/pow.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 blockchain/pow.py diff --git a/blockchain/pow.py b/blockchain/pow.py new file mode 100644 index 0000000..b50eba5 --- /dev/null +++ b/blockchain/pow.py @@ -0,0 +1,27 @@ +import hashlib +import time + +class ProofOfWork: + def __init__(self, blockchain): + self.blockchain = blockchain + self.difficulty = 2 + + def mine_block(self, wallet): + new_block = Block(len(self.blockchain.chain), self.blockchain.get_latest_block().hash, self.blockchain.pending_transactions) + new_block.mine_block(self.difficulty) + self.blockchain.chain.append(new_block) + self.blockchain.pending_transactions = [Transaction(None, wallet.public_key, 10)] + return new_block + + def calculate_hash(self, block): + data_string = str(block.index) + block.previous_hash + str(block.transactions) + str(block.timestamp) + str(block.nonce) + return hashlib.sha256(data_string.encode()).hexdigest() + + def validate_proof(self, block): + return block.hash[:self.difficulty] == '0' * self.difficulty + + def mine(self, block): + while not self.validate_proof(block): + block.nonce += 1 + block.hash = self.calculate_hash(block) + return block