Update and rename PoWAlgorithm.py to pow_algorithm.py

This commit is contained in:
Anurag Singh 2024-10-03 15:25:02 +05:30 committed by GitHub
parent 95732058fb
commit 5ebea50f6f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,27 +2,15 @@
# Title: Proof of Work Algorithm for Blockchain # Title: Proof of Work Algorithm for Blockchain
## Algorithm Statement: ## Algorithm Statement:
The algorithm implements the Proof of Work (PoW) consensus mechanism used in blockchain to validate blocks. PoW is used to ensure that participants (miners) must perform a computational task to create a valid block and add it to the blockchain. The difficulty of the task is defined by the number of leading zeros required in the hash of the block. The algorithm implements the Proof of Work (PoW) consensus mechanism used in
blockchain to validate blocks. PoW ensures participants (miners) perform a
## Approach: computational task to create a valid block and add it to the blockchain. The
1. Each block contains a list of transactions, a previous block's hash, a timestamp, and a nonce (random value). difficulty is defined by the number of leading zeros required in the block hash.
2. The block is hashed using the SHA-256 cryptographic hash function.
3. The miner's goal is to find a nonce such that the resulting hash has a certain number of leading zeros, which defines the difficulty level.
4. The difficulty is adjustable. The more leading zeros required, the harder it is to find a valid nonce.
5. This process is repeated until a valid hash is found, which demonstrates that computational work has been done (Proof of Work).
## Steps:
1. Create a `Block` class to hold block details (transactions, previous hash, timestamp, and nonce).
2. Implement a `Blockchain` class that adds new blocks to the chain by solving the PoW problem.
3. Implement the hashing function using SHA-256.
4. Adjust the difficulty by varying the number of leading zeros required in the hash.
5. Use test cases to validate the PoW algorithm.
""" """
import hashlib import hashlib
import time import time
class Block: class Block:
def __init__(self, index, previous_hash, transactions, timestamp, difficulty): def __init__(self, index, previous_hash, transactions, timestamp, difficulty):
self.index = index self.index = index
@ -39,24 +27,25 @@ class Block:
Combines index, previous hash, transactions, timestamp, and nonce into a string, Combines index, previous hash, transactions, timestamp, and nonce into a string,
which is then hashed using SHA-256. which is then hashed using SHA-256.
""" """
block_string = f"{self.index}{self.previous_hash}{self.transactions}{self.timestamp}{self.nonce}" block_string = (
f"{self.index}{self.previous_hash}{self.transactions}{self.timestamp}"
f"{self.nonce}"
)
return hashlib.sha256(block_string.encode()).hexdigest() return hashlib.sha256(block_string.encode()).hexdigest()
def mine_block(self): def mine_block(self):
""" """
Performs Proof of Work by adjusting the nonce until a valid hash is found. Performs Proof of Work by adjusting the nonce until a valid hash is found.
A valid hash has the required number of leading zeros based on the difficulty level. A valid hash has the required number of leading zeros based on the difficulty
level.
""" """
target = ( target = '0' * self.difficulty # Target hash should start with 'difficulty' zeros
"0" * self.difficulty
) # Target hash should start with 'difficulty' number of zeros
while self.hash[:self.difficulty] != target: while self.hash[:self.difficulty] != target:
self.nonce += 1 self.nonce += 1
self.hash = self.compute_hash() self.hash = self.compute_hash()
print(f"Block mined with nonce {self.nonce}, hash: {self.hash}") print(f"Block mined with nonce {self.nonce}, hash: {self.hash}")
class Blockchain: class Blockchain:
def __init__(self, difficulty): def __init__(self, difficulty):
self.chain = [] self.chain = []
@ -76,20 +65,15 @@ class Blockchain:
Adds a new block to the blockchain after performing Proof of Work. Adds a new block to the blockchain after performing Proof of Work.
""" """
previous_block = self.chain[-1] previous_block = self.chain[-1]
new_block = Block( new_block = Block(len(self.chain), previous_block.hash, transactions, time.time(),
len(self.chain), self.difficulty)
previous_block.hash,
transactions,
time.time(),
self.difficulty,
)
new_block.mine_block() new_block.mine_block()
self.chain.append(new_block) self.chain.append(new_block)
def is_chain_valid(self): def is_chain_valid(self):
""" """
Verifies the integrity of the blockchain by ensuring each block's previous hash matches Verifies the integrity of the blockchain by ensuring each block's previous
and that all blocks meet the Proof of Work requirement. hash matches and that all blocks meet the Proof of Work requirement.
""" """
for i in range(1, len(self.chain)): for i in range(1, len(self.chain)):
current_block = self.chain[i] current_block = self.chain[i]
@ -105,10 +89,8 @@ class Blockchain:
return True return True
# Test cases # Test cases
def test_blockchain(): def test_blockchain():
""" """
Test cases for the Blockchain proof of work algorithm. Test cases for the Blockchain proof of work algorithm.
@ -121,19 +103,14 @@ def test_blockchain():
blockchain.add_block("Transaction 2: Bob pays Charlie 3 BTC") blockchain.add_block("Transaction 2: Bob pays Charlie 3 BTC")
# Verify the integrity of the blockchain # Verify the integrity of the blockchain
assert blockchain.is_chain_valid() == True, "Blockchain should be valid" assert blockchain.is_chain_valid(), "Blockchain should be valid"
# Tamper with the blockchain and check validation # Tamper with the blockchain and check validation
blockchain.chain[ blockchain.chain[1].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
1 assert not blockchain.is_chain_valid(), "Blockchain should be invalid due to tampering"
].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering the transaction
assert (
blockchain.is_chain_valid() == False
), "Blockchain should be invalid due to tampering"
print("All test cases passed.") print("All test cases passed.")
if __name__ == "__main__": if __name__ == "__main__":
test_blockchain() test_blockchain()