Skip to content

Commit 3d91445

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

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

blockchain/pow_algorithm.py

+36-22
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22
# Title: Proof of Work Algorithm for Blockchain
33
44
## Algorithm Statement:
5-
The algorithm implements the Proof of Work (PoW) consensus mechanism used in
6-
blockchain to validate blocks. PoW ensures participants (miners) perform a
7-
computational task to create a valid block and add it to the blockchain. The
5+
The algorithm implements the Proof of Work (PoW) consensus mechanism used in
6+
blockchain to validate blocks. PoW ensures participants (miners) perform a
7+
computational task to create a valid block and add it to the blockchain. The
88
difficulty is defined by the number of leading zeros required in the block hash.
99
"""
1010

1111
import hashlib
1212
import time
1313

14+
1415
class Block:
1516
def __init__(
16-
self,
17-
index: int,
18-
previous_hash: str,
19-
transactions: str,
20-
timestamp: float,
21-
difficulty: int
17+
self,
18+
index: int,
19+
previous_hash: str,
20+
transactions: str,
21+
timestamp: float,
22+
difficulty: int,
2223
) -> None:
2324
"""
2425
Initializes a Block object with the specified parameters.
@@ -43,7 +44,7 @@ def compute_hash(self) -> str:
4344
Generates the hash of the block content.
4445
Combines index, previous hash, transactions, timestamp, and nonce into a string,
4546
which is then hashed using SHA-256.
46-
47+
4748
Returns:
4849
- str: The hash of the block.
4950
"""
@@ -57,25 +58,28 @@ def mine_block(self) -> None:
5758
"""
5859
Performs Proof of Work by adjusting the nonce until a valid hash is found.
5960
A valid hash has the required number of leading zeros based on the 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,12 +141,14 @@ def is_chain_valid(self) -> bool:
134141

135142
return True
136143

144+
137145
# Test cases
138146

147+
139148
def test_blockchain() -> None:
140149
"""
141150
Test cases for the Blockchain proof of work algorithm.
142-
151+
143152
Returns:
144153
- None
145154
"""
@@ -154,10 +163,15 @@ def test_blockchain() -> None:
154163
assert blockchain.is_chain_valid(), "Blockchain should be valid"
155164

156165
# Tamper with the blockchain and check validation
157-
blockchain.chain[1].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
158-
assert not blockchain.is_chain_valid(), "Blockchain should be invalid due to tampering"
166+
blockchain.chain[
167+
1
168+
].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
169+
assert (
170+
not blockchain.is_chain_valid()
171+
), "Blockchain should be invalid due to tampering"
159172

160173
print("All test cases passed.")
161174

175+
162176
if __name__ == "__main__":
163177
test_blockchain()

0 commit comments

Comments
 (0)