Skip to content

Commit 6a7b49d

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

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

blockchain/pow_algorithm.py

+29-15
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import hashlib
1010
import time
1111

12+
1213
class Block:
1314
def __init__(
1415
self,
1516
index: int,
1617
previous_hash: str,
1718
transactions: str,
1819
timestamp: float,
19-
difficulty: int
20+
difficulty: int,
2021
) -> None:
2122
"""
2223
Initializes a Block object with the specified parameters.
@@ -42,7 +43,7 @@ def compute_hash(self) -> str:
4243
Generates the hash of the block content.
4344
Combines index, previous hash, transactions, timestamp, and nonce into
4445
a string, which is then hashed using SHA-256.
45-
46+
4647
Returns:
4748
- str: The hash of the block.
4849
"""
@@ -57,25 +58,28 @@ def mine_block(self) -> None:
5758
Performs Proof of Work by adjusting the nonce until a valid hash is found.
5859
A valid hash has the required number of leading zeros based on the
5960
difficulty level.
60-
61+
6162
Returns:
6263
- None
6364
"""
64-
target = '0' * self.difficulty # Target hash should start with 'difficulty' zeros
65-
while self.hash[:self.difficulty] != target:
65+
target = (
66+
"0" * self.difficulty
67+
) # Target hash should start with 'difficulty' zeros
68+
while self.hash[: self.difficulty] != target:
6669
self.nonce += 1
6770
self.hash = self.compute_hash()
6871

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

74+
7175
class Blockchain:
7276
def __init__(self, difficulty: int) -> None:
7377
"""
7478
Initializes the blockchain with a given difficulty level.
7579
7680
Parameters:
7781
- difficulty (int): The difficulty level for mining blocks in this blockchain.
78-
82+
7983
Returns:
8084
- None
8185
"""
@@ -86,7 +90,7 @@ def __init__(self, difficulty: int) -> None:
8690
def create_genesis_block(self) -> None:
8791
"""
8892
Creates the first block in the blockchain (the Genesis block).
89-
93+
9094
Returns:
9195
- None
9296
"""
@@ -100,21 +104,24 @@ def add_block(self, transactions: str) -> None:
100104
101105
Parameters:
102106
- transactions (str): The list of transactions to be added in the new block.
103-
107+
104108
Returns:
105109
- None
106110
"""
107111
previous_block = self.chain[-1]
108112
new_block = Block(
109-
len(self.chain), previous_block.hash, transactions, time.time(),
110-
self.difficulty
113+
len(self.chain),
114+
previous_block.hash,
115+
transactions,
116+
time.time(),
117+
self.difficulty,
111118
)
112119
new_block.mine_block()
113120
self.chain.append(new_block)
114121

115122
def is_chain_valid(self) -> bool:
116123
"""
117-
Verifies the integrity of the blockchain by ensuring each block's previous
124+
Verifies the integrity of the blockchain by ensuring each block's previous
118125
hash matches and that all blocks meet the Proof of Work requirement.
119126
120127
Returns:
@@ -134,15 +141,17 @@ def is_chain_valid(self) -> bool:
134141

135142
return True
136143

144+
137145
# Test cases
138146

147+
139148
## Test Case 1: Blockchain Initialization and Genesis Block
140149
# This test verifies if the blockchain is correctly initialized with a Genesis block
141150
# and if the block is successfully mined.
142151
def test_blockchain() -> None:
143152
"""
144153
Test cases for the Blockchain proof of work algorithm.
145-
154+
146155
Returns:
147156
- None
148157
"""
@@ -162,14 +171,19 @@ def test_blockchain() -> None:
162171
## Test Case 4: Tampering with the blockchain
163172
# This test simulates tampering with the blockchain and checks that the validation
164173
# correctly detects the tampering.
165-
blockchain.chain[1].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
166-
assert not blockchain.is_chain_valid(), "Blockchain should be invalid due to tampering"
174+
blockchain.chain[
175+
1
176+
].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
177+
assert (
178+
not blockchain.is_chain_valid()
179+
), "Blockchain should be invalid due to tampering"
167180

168181
## Test Case 5: Correct blockchain validation
169182
# This test checks if the blockchain becomes invalid after tampering and verifies
170183
# if the PoW still holds after tampering is done.
171-
184+
172185
print("All test cases passed.")
173186

187+
174188
if __name__ == "__main__":
175189
test_blockchain()

0 commit comments

Comments
 (0)