Skip to content

Commit 9573205

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent f5459b8 commit 9573205

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

blockchain/PoWAlgorithm.py

+24-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import hashlib
2323
import time
2424

25+
2526
class Block:
2627
def __init__(self, index, previous_hash, transactions, timestamp, difficulty):
2728
self.index = index
@@ -35,7 +36,7 @@ def __init__(self, index, previous_hash, transactions, timestamp, difficulty):
3536
def compute_hash(self):
3637
"""
3738
Generates the hash of the block content.
38-
Combines index, previous hash, transactions, timestamp, and nonce into a string,
39+
Combines index, previous hash, transactions, timestamp, and nonce into a string,
3940
which is then hashed using SHA-256.
4041
"""
4142
block_string = f"{self.index}{self.previous_hash}{self.transactions}{self.timestamp}{self.nonce}"
@@ -46,13 +47,16 @@ def mine_block(self):
4647
Performs Proof of Work by adjusting the nonce until a valid hash is found.
4748
A valid hash has the required number of leading zeros based on the difficulty level.
4849
"""
49-
target = '0' * self.difficulty # Target hash should start with 'difficulty' number of zeros
50-
while self.hash[:self.difficulty] != target:
50+
target = (
51+
"0" * self.difficulty
52+
) # Target hash should start with 'difficulty' number of zeros
53+
while self.hash[: self.difficulty] != target:
5154
self.nonce += 1
5255
self.hash = self.compute_hash()
5356

5457
print(f"Block mined with nonce {self.nonce}, hash: {self.hash}")
5558

59+
5660
class Blockchain:
5761
def __init__(self, difficulty):
5862
self.chain = []
@@ -72,13 +76,19 @@ def add_block(self, transactions):
7276
Adds a new block to the blockchain after performing Proof of Work.
7377
"""
7478
previous_block = self.chain[-1]
75-
new_block = Block(len(self.chain), previous_block.hash, transactions, time.time(), self.difficulty)
79+
new_block = Block(
80+
len(self.chain),
81+
previous_block.hash,
82+
transactions,
83+
time.time(),
84+
self.difficulty,
85+
)
7686
new_block.mine_block()
7787
self.chain.append(new_block)
7888

7989
def is_chain_valid(self):
8090
"""
81-
Verifies the integrity of the blockchain by ensuring each block's previous hash matches
91+
Verifies the integrity of the blockchain by ensuring each block's previous hash matches
8292
and that all blocks meet the Proof of Work requirement.
8393
"""
8494
for i in range(1, len(self.chain)):
@@ -95,8 +105,10 @@ def is_chain_valid(self):
95105

96106
return True
97107

108+
98109
# Test cases
99110

111+
100112
def test_blockchain():
101113
"""
102114
Test cases for the Blockchain proof of work algorithm.
@@ -112,11 +124,16 @@ def test_blockchain():
112124
assert blockchain.is_chain_valid() == True, "Blockchain should be valid"
113125

114126
# Tamper with the blockchain and check validation
115-
blockchain.chain[1].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering the transaction
116-
assert blockchain.is_chain_valid() == False, "Blockchain should be invalid due to tampering"
127+
blockchain.chain[
128+
1
129+
].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering the transaction
130+
assert (
131+
blockchain.is_chain_valid() == False
132+
), "Blockchain should be invalid due to tampering"
117133

118134
print("All test cases passed.")
119135

136+
120137
if __name__ == "__main__":
121138
test_blockchain()
122139

0 commit comments

Comments
 (0)