mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-17 14:58:10 +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
91a43251d8
commit
66cd40e387
|
@ -11,6 +11,7 @@ difficulty is defined by the number of leading zeros required in the block hash.
|
||||||
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,13 +40,16 @@ class Block:
|
||||||
A valid hash has the required number of leading zeros based on the difficulty
|
A valid hash has the required number of leading zeros based on the difficulty
|
||||||
level.
|
level.
|
||||||
"""
|
"""
|
||||||
target = '0' * self.difficulty # Target hash should start with 'difficulty' zeros
|
target = (
|
||||||
|
"0" * self.difficulty
|
||||||
|
) # Target hash should start with 'difficulty' 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 = []
|
||||||
|
@ -65,8 +69,13 @@ 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(),
|
new_block = Block(
|
||||||
self.difficulty)
|
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)
|
||||||
|
|
||||||
|
@ -89,8 +98,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.
|
||||||
|
@ -106,11 +117,16 @@ def test_blockchain():
|
||||||
assert blockchain.is_chain_valid(), "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[1].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
|
blockchain.chain[
|
||||||
assert not blockchain.is_chain_valid(), "Blockchain should be invalid due to tampering"
|
1
|
||||||
|
].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
|
||||||
|
assert (
|
||||||
|
not blockchain.is_chain_valid()
|
||||||
|
), "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