mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-20 08:12:02 +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 hashlib
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
class Block:
|
class Block:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
@ -16,7 +17,7 @@ class Block:
|
||||||
previous_hash: str,
|
previous_hash: str,
|
||||||
transactions: str,
|
transactions: str,
|
||||||
timestamp: float,
|
timestamp: float,
|
||||||
difficulty: int
|
difficulty: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Initializes a Block object with the specified parameters.
|
Initializes a Block object with the specified parameters.
|
||||||
|
@ -61,13 +62,16 @@ class Block:
|
||||||
Returns:
|
Returns:
|
||||||
- None
|
- None
|
||||||
"""
|
"""
|
||||||
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: int) -> None:
|
def __init__(self, difficulty: int) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -106,8 +110,11 @@ class Blockchain:
|
||||||
"""
|
"""
|
||||||
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)
|
||||||
|
@ -134,8 +141,10 @@ class Blockchain:
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# Test cases
|
# Test cases
|
||||||
|
|
||||||
|
|
||||||
## Test Case 1: Blockchain Initialization and Genesis Block
|
## Test Case 1: Blockchain Initialization and Genesis Block
|
||||||
# This test verifies if the blockchain is correctly initialized with a Genesis block
|
# This test verifies if the blockchain is correctly initialized with a Genesis block
|
||||||
# and if the block is successfully mined.
|
# and if the block is successfully mined.
|
||||||
|
@ -162,8 +171,12 @@ def test_blockchain() -> None:
|
||||||
## Test Case 4: Tampering with the blockchain
|
## Test Case 4: Tampering with the blockchain
|
||||||
# This test simulates tampering with the blockchain and checks that the validation
|
# This test simulates tampering with the blockchain and checks that the validation
|
||||||
# correctly detects the tampering.
|
# correctly detects the tampering.
|
||||||
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"
|
||||||
|
|
||||||
## Test Case 5: Correct blockchain validation
|
## Test Case 5: Correct blockchain validation
|
||||||
# This test checks if the blockchain becomes invalid after tampering and verifies
|
# This test checks if the blockchain becomes invalid after tampering and verifies
|
||||||
|
@ -171,5 +184,6 @@ def test_blockchain() -> None:
|
||||||
|
|
||||||
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