mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +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
9a6fcd5417
commit
6a7b49d95e
|
@ -9,6 +9,7 @@
|
|||
import hashlib
|
||||
import time
|
||||
|
||||
|
||||
class Block:
|
||||
def __init__(
|
||||
self,
|
||||
|
@ -16,7 +17,7 @@ class Block:
|
|||
previous_hash: str,
|
||||
transactions: str,
|
||||
timestamp: float,
|
||||
difficulty: int
|
||||
difficulty: int,
|
||||
) -> None:
|
||||
"""
|
||||
Initializes a Block object with the specified parameters.
|
||||
|
@ -42,7 +43,7 @@ class Block:
|
|||
Generates the hash of the block content.
|
||||
Combines index, previous hash, transactions, timestamp, and nonce into
|
||||
a string, which is then hashed using SHA-256.
|
||||
|
||||
|
||||
Returns:
|
||||
- str: The hash of the block.
|
||||
"""
|
||||
|
@ -57,17 +58,20 @@ class Block:
|
|||
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.
|
||||
|
||||
|
||||
Returns:
|
||||
- None
|
||||
"""
|
||||
target = '0' * self.difficulty # Target hash should start with 'difficulty' zeros
|
||||
while self.hash[:self.difficulty] != target:
|
||||
target = (
|
||||
"0" * self.difficulty
|
||||
) # Target hash should start with 'difficulty' zeros
|
||||
while self.hash[: self.difficulty] != target:
|
||||
self.nonce += 1
|
||||
self.hash = self.compute_hash()
|
||||
|
||||
print(f"Block mined with nonce {self.nonce}, hash: {self.hash}")
|
||||
|
||||
|
||||
class Blockchain:
|
||||
def __init__(self, difficulty: int) -> None:
|
||||
"""
|
||||
|
@ -75,7 +79,7 @@ class Blockchain:
|
|||
|
||||
Parameters:
|
||||
- difficulty (int): The difficulty level for mining blocks in this blockchain.
|
||||
|
||||
|
||||
Returns:
|
||||
- None
|
||||
"""
|
||||
|
@ -86,7 +90,7 @@ class Blockchain:
|
|||
def create_genesis_block(self) -> None:
|
||||
"""
|
||||
Creates the first block in the blockchain (the Genesis block).
|
||||
|
||||
|
||||
Returns:
|
||||
- None
|
||||
"""
|
||||
|
@ -100,21 +104,24 @@ class Blockchain:
|
|||
|
||||
Parameters:
|
||||
- transactions (str): The list of transactions to be added in the new block.
|
||||
|
||||
|
||||
Returns:
|
||||
- None
|
||||
"""
|
||||
previous_block = self.chain[-1]
|
||||
new_block = Block(
|
||||
len(self.chain), previous_block.hash, transactions, time.time(),
|
||||
self.difficulty
|
||||
len(self.chain),
|
||||
previous_block.hash,
|
||||
transactions,
|
||||
time.time(),
|
||||
self.difficulty,
|
||||
)
|
||||
new_block.mine_block()
|
||||
self.chain.append(new_block)
|
||||
|
||||
def is_chain_valid(self) -> bool:
|
||||
"""
|
||||
Verifies the integrity of the blockchain by ensuring each block's previous
|
||||
Verifies the integrity of the blockchain by ensuring each block's previous
|
||||
hash matches and that all blocks meet the Proof of Work requirement.
|
||||
|
||||
Returns:
|
||||
|
@ -134,15 +141,17 @@ class Blockchain:
|
|||
|
||||
return True
|
||||
|
||||
|
||||
# Test cases
|
||||
|
||||
|
||||
## Test Case 1: Blockchain Initialization and Genesis Block
|
||||
# This test verifies if the blockchain is correctly initialized with a Genesis block
|
||||
# and if the block is successfully mined.
|
||||
def test_blockchain() -> None:
|
||||
"""
|
||||
Test cases for the Blockchain proof of work algorithm.
|
||||
|
||||
|
||||
Returns:
|
||||
- None
|
||||
"""
|
||||
|
@ -162,14 +171,19 @@ def test_blockchain() -> None:
|
|||
## Test Case 4: Tampering with the blockchain
|
||||
# This test simulates tampering with the blockchain and checks that the validation
|
||||
# correctly detects the tampering.
|
||||
blockchain.chain[1].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
|
||||
assert not blockchain.is_chain_valid(), "Blockchain should be invalid due to tampering"
|
||||
blockchain.chain[
|
||||
1
|
||||
].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
|
||||
assert (
|
||||
not blockchain.is_chain_valid()
|
||||
), "Blockchain should be invalid due to tampering"
|
||||
|
||||
## Test Case 5: Correct blockchain validation
|
||||
# This test checks if the blockchain becomes invalid after tampering and verifies
|
||||
# if the PoW still holds after tampering is done.
|
||||
|
||||
|
||||
print("All test cases passed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_blockchain()
|
||||
|
|
Loading…
Reference in New Issue
Block a user