mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-22 01:02:04 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
f5459b8083
commit
95732058fb
|
@ -22,6 +22,7 @@ The algorithm implements the Proof of Work (PoW) consensus mechanism used in blo
|
||||||
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
|
||||||
|
@ -35,7 +36,7 @@ class Block:
|
||||||
def compute_hash(self):
|
def compute_hash(self):
|
||||||
"""
|
"""
|
||||||
Generates the hash of the block content.
|
Generates the hash of the block content.
|
||||||
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}{self.nonce}"
|
||||||
|
@ -46,13 +47,16 @@ class Block:
|
||||||
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 = '0' * self.difficulty # Target hash should start with 'difficulty' number of zeros
|
target = (
|
||||||
while self.hash[:self.difficulty] != target:
|
"0" * self.difficulty
|
||||||
|
) # Target hash should start with 'difficulty' number of zeros
|
||||||
|
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 = []
|
||||||
|
@ -72,13 +76,19 @@ 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(len(self.chain), previous_block.hash, transactions, time.time(), self.difficulty)
|
new_block = Block(
|
||||||
|
len(self.chain),
|
||||||
|
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 hash matches
|
||||||
and that all blocks meet the Proof of Work requirement.
|
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)):
|
||||||
|
@ -95,8 +105,10 @@ 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.
|
||||||
|
@ -112,11 +124,16 @@ def test_blockchain():
|
||||||
assert blockchain.is_chain_valid() == True, "Blockchain should be valid"
|
assert blockchain.is_chain_valid() == True, "Blockchain should be valid"
|
||||||
|
|
||||||
# Tamper with the blockchain and check validation
|
# Tamper with the blockchain and check validation
|
||||||
blockchain.chain[1].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering the transaction
|
blockchain.chain[
|
||||||
assert blockchain.is_chain_valid() == False, "Blockchain should be invalid due to tampering"
|
1
|
||||||
|
].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()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user